blob: c5478820a9ba2dad565700741d362a000130d007 [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 <ext4fs.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +02008#include <errno.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02009#include <image.h>
10
Sean Andersonf727cc12023-11-08 11:48:48 -050011static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
12 ulong size, void *buf)
13{
14 int ret;
15 loff_t actlen;
16
17 ret = ext4fs_read(buf, file_offset, size, &actlen);
18 if (ret)
19 return ret;
20 return actlen;
21}
22
Simon Glass0649e912016-09-24 18:20:14 -060023int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010024 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060025 struct blk_desc *block_dev, int partition,
26 const char *filename)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020027{
28 s32 err;
Sean Andersonf727cc12023-11-08 11:48:48 -050029 loff_t filelen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060030 struct disk_partition part_info = {};
Sean Andersonf727cc12023-11-08 11:48:48 -050031 struct spl_load_info load;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020032
Simon Glassb89a8442016-02-29 15:25:48 -070033 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020034 printf("spl: no partition table found\n");
35 return -1;
36 }
37
38 ext4fs_set_blk_dev(block_dev, &part_info);
39
Sean Andersonab125f52023-11-08 12:51:09 -050040 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020041 if (!err) {
42#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
43 printf("%s: ext4fs mount err - %d\n", __func__, err);
44#endif
Thomas Schaefer37aea852020-06-16 22:03:52 +020045 return -1;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020046 }
47
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080048 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020049 if (err < 0) {
50 puts("spl: ext4fs_open failed\n");
51 goto end;
52 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020053
Simon Glasse8066862024-08-22 07:55:02 -060054 spl_load_init(&load, spl_fit_read, NULL, 1);
Sean Andersonf727cc12023-11-08 11:48:48 -050055 err = spl_load(spl_image, bootdev, &load, filelen, 0);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020056
57end:
58#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010059 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020060 printf("%s: error reading image %s, err - %d\n",
61 __func__, filename, err);
62#endif
63
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010064 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020065}
66
Tom Rinic2e78882021-10-30 23:03:48 -040067#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass0649e912016-09-24 18:20:14 -060068int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010069 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060070 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020071{
72 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080073 __maybe_unused loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060074 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020075 __maybe_unused char *file;
76
Simon Glassb89a8442016-02-29 15:25:48 -070077 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020078 printf("spl: no partition table found\n");
79 return -1;
80 }
81
82 ext4fs_set_blk_dev(block_dev, &part_info);
83
Sean Andersonab125f52023-11-08 12:51:09 -050084 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020085 if (!err) {
86#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
87 printf("%s: ext4fs mount err - %d\n", __func__, err);
88#endif
89 return -1;
90 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020091#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass64b723f2017-08-03 12:22:12 -060092 file = env_get("falcon_args_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020093 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080094 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020095 if (err < 0) {
96 puts("spl: ext4fs_open failed\n");
97 goto defaults;
98 }
Simon Glass259cdb42023-09-26 08:14:17 -060099 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100100 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200101 printf("spl: error reading image %s, err - %d, falling back to default\n",
102 file, err);
103 goto defaults;
104 }
Simon Glass64b723f2017-08-03 12:22:12 -0600105 file = env_get("falcon_image_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200106 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100107 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glass0649e912016-09-24 18:20:14 -0600108 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200109 if (err != 0) {
110 puts("spl: falling back to default\n");
111 goto defaults;
112 }
113
114 return 0;
115 } else {
116 puts("spl: falcon_image_file not set in environment, falling back to default\n");
117 }
118 } else {
119 puts("spl: falcon_args_file not set in environment, falling back to default\n");
120 }
121
122defaults:
123#endif
124
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800125 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200126 if (err < 0)
127 puts("spl: ext4fs_open failed\n");
128
Simon Glass259cdb42023-09-26 08:14:17 -0600129 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100130 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200131#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
132 printf("%s: error reading image %s, err - %d\n",
133 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
134#endif
135 return -1;
136 }
137
Pali Rohárdda8f882022-01-14 14:31:38 +0100138 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200139 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
140}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200141#else
Simon Glass0649e912016-09-24 18:20:14 -0600142int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100143 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -0600144 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200145{
146 return -ENOSYS;
147}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200148#endif