blob: d73f062762156819d08a7849ec5fa0dea98015da [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guillaume GARDET592f9222014-10-15 17:53:12 +02002
3#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -06004#include <env.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06005#include <part.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02006#include <spl.h>
7#include <asm/u-boot.h>
8#include <ext4fs.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +02009#include <errno.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +020010#include <image.h>
11
Simon Glassb4a6c2a2016-09-24 18:20:14 -060012int spl_load_image_ext(struct spl_image_info *spl_image,
13 struct blk_desc *block_dev, int partition,
14 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020015{
16 s32 err;
17 struct image_header *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080018 loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060019 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020020
Marek Vasut04ce5422018-08-14 11:27:02 +020021 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET592f9222014-10-15 17:53:12 +020022
Simon Glass3e8bd462016-02-29 15:25:48 -070023 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020024 printf("spl: no partition table found\n");
25 return -1;
26 }
27
28 ext4fs_set_blk_dev(block_dev, &part_info);
29
30 err = ext4fs_mount(0);
31 if (!err) {
32#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
33 printf("%s: ext4fs mount err - %d\n", __func__, err);
34#endif
Thomas Schaeferea5003a2020-06-16 22:03:52 +020035 return -1;
Guillaume GARDET592f9222014-10-15 17:53:12 +020036 }
37
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080038 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020039 if (err < 0) {
40 puts("spl: ext4fs_open failed\n");
41 goto end;
42 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010043 err = ext4fs_read((char *)header, 0, sizeof(struct image_header), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010044 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020045 puts("spl: ext4fs_read failed\n");
46 goto end;
47 }
48
Simon Glassb4a6c2a2016-09-24 18:20:14 -060049 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020050 if (err < 0) {
Petr Kulhavy9ab165d2016-06-18 12:21:17 +020051 puts("spl: ext: failed to parse image header\n");
Marek Vasut7e0f2262016-04-29 00:44:54 +020052 goto end;
53 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020054
Stefan Brüns66a47ff2016-11-06 18:33:57 +010055 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020056
57end:
58#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010059 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020060 printf("%s: error reading image %s, err - %d\n",
61 __func__, filename, err);
62#endif
63
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010064 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020065}
66
67#ifdef CONFIG_SPL_OS_BOOT
Simon Glassb4a6c2a2016-09-24 18:20:14 -060068int spl_load_image_ext_os(struct spl_image_info *spl_image,
69 struct blk_desc *block_dev, int partition)
Guillaume GARDET592f9222014-10-15 17:53:12 +020070{
71 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080072 __maybe_unused loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060073 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020074 __maybe_unused char *file;
75
Simon Glass3e8bd462016-02-29 15:25:48 -070076 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020077 printf("spl: no partition table found\n");
78 return -1;
79 }
80
81 ext4fs_set_blk_dev(block_dev, &part_info);
82
83 err = ext4fs_mount(0);
84 if (!err) {
85#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
86 printf("%s: ext4fs mount err - %d\n", __func__, err);
87#endif
88 return -1;
89 }
Petr Kulhavy58c95d52016-06-14 12:06:36 +020090#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass00caae62017-08-03 12:22:12 -060091 file = env_get("falcon_args_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +020092 if (file) {
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080093 err = ext4fs_open(file, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020094 if (err < 0) {
95 puts("spl: ext4fs_open failed\n");
96 goto defaults;
97 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +010098 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010099 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200100 printf("spl: error reading image %s, err - %d, falling back to default\n",
101 file, err);
102 goto defaults;
103 }
Simon Glass00caae62017-08-03 12:22:12 -0600104 file = env_get("falcon_image_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +0200105 if (file) {
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600106 err = spl_load_image_ext(spl_image, block_dev,
107 partition, file);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200108 if (err != 0) {
109 puts("spl: falling back to default\n");
110 goto defaults;
111 }
112
113 return 0;
114 } else {
115 puts("spl: falcon_image_file not set in environment, falling back to default\n");
116 }
117 } else {
118 puts("spl: falcon_args_file not set in environment, falling back to default\n");
119 }
120
121defaults:
122#endif
123
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800124 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200125 if (err < 0)
126 puts("spl: ext4fs_open failed\n");
127
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100128 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100129 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200130#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
131 printf("%s: error reading image %s, err - %d\n",
132 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
133#endif
134 return -1;
135 }
136
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600137 return spl_load_image_ext(spl_image, block_dev, partition,
Guillaume GARDET592f9222014-10-15 17:53:12 +0200138 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
139}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200140#else
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600141int spl_load_image_ext_os(struct spl_image_info *spl_image,
142 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200143{
144 return -ENOSYS;
145}
Guillaume GARDET592f9222014-10-15 17:53:12 +0200146#endif