blob: 2399e1d806d4eb625aa59540d35f8e19008f8551 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02002
Simon Glass0af6e2d2019-08-01 09:46:52 -06003#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -06004#include <part.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02005#include <spl.h>
Sean Andersonf727cc12023-11-08 11:48:48 -05006#include <spl_load.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02007#include <asm/u-boot.h>
8#include <ext4fs.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +02009#include <errno.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020010#include <image.h>
11
Sean Andersonf727cc12023-11-08 11:48:48 -050012static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
13 ulong size, void *buf)
14{
15 int ret;
16 loff_t actlen;
17
18 ret = ext4fs_read(buf, file_offset, size, &actlen);
19 if (ret)
20 return ret;
21 return actlen;
22}
23
Simon Glass0649e912016-09-24 18:20:14 -060024int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010025 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060026 struct blk_desc *block_dev, int partition,
27 const char *filename)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020028{
29 s32 err;
Sean Andersonf727cc12023-11-08 11:48:48 -050030 loff_t filelen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060031 struct disk_partition part_info = {};
Sean Andersonf727cc12023-11-08 11:48:48 -050032 struct spl_load_info load;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020033
Simon Glassb89a8442016-02-29 15:25:48 -070034 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020035 printf("spl: no partition table found\n");
36 return -1;
37 }
38
39 ext4fs_set_blk_dev(block_dev, &part_info);
40
Sean Andersonab125f52023-11-08 12:51:09 -050041 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020042 if (!err) {
43#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
44 printf("%s: ext4fs mount err - %d\n", __func__, err);
45#endif
Thomas Schaefer37aea852020-06-16 22:03:52 +020046 return -1;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020047 }
48
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080049 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020050 if (err < 0) {
51 puts("spl: ext4fs_open failed\n");
52 goto end;
53 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020054
Sean Andersonf727cc12023-11-08 11:48:48 -050055 spl_set_bl_len(&load, 1);
56 load.read = spl_fit_read;
57 err = spl_load(spl_image, bootdev, &load, filelen, 0);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020058
59end:
60#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010061 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020062 printf("%s: error reading image %s, err - %d\n",
63 __func__, filename, err);
64#endif
65
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010066 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020067}
68
Tom Rinic2e78882021-10-30 23:03:48 -040069#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass0649e912016-09-24 18:20:14 -060070int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010071 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060072 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020073{
74 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080075 __maybe_unused loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060076 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020077 __maybe_unused char *file;
78
Simon Glassb89a8442016-02-29 15:25:48 -070079 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020080 printf("spl: no partition table found\n");
81 return -1;
82 }
83
84 ext4fs_set_blk_dev(block_dev, &part_info);
85
Sean Andersonab125f52023-11-08 12:51:09 -050086 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020087 if (!err) {
88#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
89 printf("%s: ext4fs mount err - %d\n", __func__, err);
90#endif
91 return -1;
92 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020093#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass64b723f2017-08-03 12:22:12 -060094 file = env_get("falcon_args_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020095 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080096 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020097 if (err < 0) {
98 puts("spl: ext4fs_open failed\n");
99 goto defaults;
100 }
Simon Glass259cdb42023-09-26 08:14:17 -0600101 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100102 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200103 printf("spl: error reading image %s, err - %d, falling back to default\n",
104 file, err);
105 goto defaults;
106 }
Simon Glass64b723f2017-08-03 12:22:12 -0600107 file = env_get("falcon_image_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200108 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100109 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glass0649e912016-09-24 18:20:14 -0600110 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200111 if (err != 0) {
112 puts("spl: falling back to default\n");
113 goto defaults;
114 }
115
116 return 0;
117 } else {
118 puts("spl: falcon_image_file not set in environment, falling back to default\n");
119 }
120 } else {
121 puts("spl: falcon_args_file not set in environment, falling back to default\n");
122 }
123
124defaults:
125#endif
126
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800127 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200128 if (err < 0)
129 puts("spl: ext4fs_open failed\n");
130
Simon Glass259cdb42023-09-26 08:14:17 -0600131 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100132 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200133#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
134 printf("%s: error reading image %s, err - %d\n",
135 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
136#endif
137 return -1;
138 }
139
Pali Rohárdda8f882022-01-14 14:31:38 +0100140 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200141 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
142}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200143#else
Simon Glass0649e912016-09-24 18:20:14 -0600144int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100145 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -0600146 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200147{
148 return -ENOSYS;
149}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200150#endif