blob: 7e0274a3058ce0f1118abceddf95c817b60d54f1 [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{
Anshul Dalal155d2a02025-03-11 15:05:45 +053014 struct legacy_img_hdr *header;
Sean Andersonf727cc12023-11-08 11:48:48 -050015 int ret;
16 loff_t actlen;
17
18 ret = ext4fs_read(buf, file_offset, size, &actlen);
19 if (ret)
20 return ret;
Anshul Dalal155d2a02025-03-11 15:05:45 +053021
22 if (CONFIG_IS_ENABLED(OS_BOOT)) {
23 header = (struct legacy_img_hdr *)buf;
24 if (image_get_magic(header) != FDT_MAGIC)
25 return size;
26 }
27
Sean Andersonf727cc12023-11-08 11:48:48 -050028 return actlen;
29}
30
Simon Glass0649e912016-09-24 18:20:14 -060031int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010032 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060033 struct blk_desc *block_dev, int partition,
34 const char *filename)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020035{
36 s32 err;
Sean Andersonf727cc12023-11-08 11:48:48 -050037 loff_t filelen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060038 struct disk_partition part_info = {};
Sean Andersonf727cc12023-11-08 11:48:48 -050039 struct spl_load_info load;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020040
Simon Glassb89a8442016-02-29 15:25:48 -070041 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020042 printf("spl: no partition table found\n");
43 return -1;
44 }
45
46 ext4fs_set_blk_dev(block_dev, &part_info);
47
Sean Andersonab125f52023-11-08 12:51:09 -050048 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020049 if (!err) {
50#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
51 printf("%s: ext4fs mount err - %d\n", __func__, err);
52#endif
Thomas Schaefer37aea852020-06-16 22:03:52 +020053 return -1;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020054 }
55
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080056 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020057 if (err < 0) {
58 puts("spl: ext4fs_open failed\n");
59 goto end;
60 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020061
Simon Glasse8066862024-08-22 07:55:02 -060062 spl_load_init(&load, spl_fit_read, NULL, 1);
Sean Andersonf727cc12023-11-08 11:48:48 -050063 err = spl_load(spl_image, bootdev, &load, filelen, 0);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020064
65end:
66#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010067 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020068 printf("%s: error reading image %s, err - %d\n",
69 __func__, filename, err);
70#endif
71
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010072 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020073}
74
Tom Rinic2e78882021-10-30 23:03:48 -040075#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass0649e912016-09-24 18:20:14 -060076int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010077 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060078 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020079{
80 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080081 __maybe_unused loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060082 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020083 __maybe_unused char *file;
84
Simon Glassb89a8442016-02-29 15:25:48 -070085 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020086 printf("spl: no partition table found\n");
87 return -1;
88 }
89
90 ext4fs_set_blk_dev(block_dev, &part_info);
91
Sean Andersonab125f52023-11-08 12:51:09 -050092 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020093 if (!err) {
94#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
95 printf("%s: ext4fs mount err - %d\n", __func__, err);
96#endif
97 return -1;
98 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020099#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass64b723f2017-08-03 12:22:12 -0600100 file = env_get("falcon_args_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200101 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800102 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200103 if (err < 0) {
104 puts("spl: ext4fs_open failed\n");
105 goto defaults;
106 }
Simon Glass259cdb42023-09-26 08:14:17 -0600107 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100108 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200109 printf("spl: error reading image %s, err - %d, falling back to default\n",
110 file, err);
111 goto defaults;
112 }
Simon Glass64b723f2017-08-03 12:22:12 -0600113 file = env_get("falcon_image_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200114 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100115 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glass0649e912016-09-24 18:20:14 -0600116 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200117 if (err != 0) {
118 puts("spl: falling back to default\n");
119 goto defaults;
120 }
121
122 return 0;
123 } else {
124 puts("spl: falcon_image_file not set in environment, falling back to default\n");
125 }
126 } else {
127 puts("spl: falcon_args_file not set in environment, falling back to default\n");
128 }
129
130defaults:
131#endif
132
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800133 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200134 if (err < 0)
135 puts("spl: ext4fs_open failed\n");
136
Simon Glass259cdb42023-09-26 08:14:17 -0600137 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100138 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200139#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
140 printf("%s: error reading image %s, err - %d\n",
141 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
142#endif
143 return -1;
144 }
145
Pali Rohárdda8f882022-01-14 14:31:38 +0100146 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200147 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
148}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200149#else
Simon Glass0649e912016-09-24 18:20:14 -0600150int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100151 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -0600152 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200153{
154 return -ENOSYS;
155}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200156#endif