blob: 2f960956c441ada3fe71fcd14bc667d5234646c5 [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
3#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06004#include <env.h>
Sean Anderson5ff77722023-10-14 16:47:55 -04005#include <mapmem.h>
Simon Glass655306c2020-05-10 11:39:58 -06006#include <part.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +02007#include <spl.h>
8#include <asm/u-boot.h>
9#include <ext4fs.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +020010#include <errno.h>
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020011#include <image.h>
12
Simon Glass0649e912016-09-24 18:20:14 -060013int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010014 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060015 struct blk_desc *block_dev, int partition,
16 const char *filename)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020017{
18 s32 err;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060019 struct legacy_img_hdr *header;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080020 loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060021 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020022
Marek Vasut1dda46f2018-08-14 11:27:02 +020023 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020024
Simon Glassb89a8442016-02-29 15:25:48 -070025 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020026 printf("spl: no partition table found\n");
27 return -1;
28 }
29
30 ext4fs_set_blk_dev(block_dev, &part_info);
31
Sean Andersonab125f52023-11-08 12:51:09 -050032 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020033 if (!err) {
34#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
35 printf("%s: ext4fs mount err - %d\n", __func__, err);
36#endif
Thomas Schaefer37aea852020-06-16 22:03:52 +020037 return -1;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020038 }
39
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080040 err = ext4fs_open(filename, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020041 if (err < 0) {
42 puts("spl: ext4fs_open failed\n");
43 goto end;
44 }
Simon Glassbb7d3bb2022-09-06 20:26:52 -060045 err = ext4fs_read((char *)header, 0, sizeof(struct legacy_img_hdr), &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010046 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020047 puts("spl: ext4fs_read failed\n");
48 goto end;
49 }
50
Pali Rohárdda8f882022-01-14 14:31:38 +010051 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020052 if (err < 0) {
Petr Kulhavy67715122016-06-18 12:21:17 +020053 puts("spl: ext: failed to parse image header\n");
Marek Vasut02266e22016-04-29 00:44:54 +020054 goto end;
55 }
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020056
Sean Anderson5ff77722023-10-14 16:47:55 -040057 err = ext4fs_read(map_sysmem(spl_image->load_addr, filelen), 0, filelen,
58 &actlen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020059
60end:
61#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010062 if (err < 0)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020063 printf("%s: error reading image %s, err - %d\n",
64 __func__, filename, err);
65#endif
66
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +010067 return err < 0;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020068}
69
Tom Rinic2e78882021-10-30 23:03:48 -040070#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass0649e912016-09-24 18:20:14 -060071int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010072 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -060073 struct blk_desc *block_dev, int partition)
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020074{
75 int err;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080076 __maybe_unused loff_t filelen, actlen;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060077 struct disk_partition part_info = {};
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020078 __maybe_unused char *file;
79
Simon Glassb89a8442016-02-29 15:25:48 -070080 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020081 printf("spl: no partition table found\n");
82 return -1;
83 }
84
85 ext4fs_set_blk_dev(block_dev, &part_info);
86
Sean Andersonab125f52023-11-08 12:51:09 -050087 err = ext4fs_mount();
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020088 if (!err) {
89#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
90 printf("%s: ext4fs mount err - %d\n", __func__, err);
91#endif
92 return -1;
93 }
Petr Kulhavyecb23642016-06-14 12:06:36 +020094#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass64b723f2017-08-03 12:22:12 -060095 file = env_get("falcon_args_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020096 if (file) {
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080097 err = ext4fs_open(file, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +020098 if (err < 0) {
99 puts("spl: ext4fs_open failed\n");
100 goto defaults;
101 }
Simon Glass259cdb42023-09-26 08:14:17 -0600102 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100103 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200104 printf("spl: error reading image %s, err - %d, falling back to default\n",
105 file, err);
106 goto defaults;
107 }
Simon Glass64b723f2017-08-03 12:22:12 -0600108 file = env_get("falcon_image_file");
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200109 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100110 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glass0649e912016-09-24 18:20:14 -0600111 partition, file);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200112 if (err != 0) {
113 puts("spl: falling back to default\n");
114 goto defaults;
115 }
116
117 return 0;
118 } else {
119 puts("spl: falcon_image_file not set in environment, falling back to default\n");
120 }
121 } else {
122 puts("spl: falcon_args_file not set in environment, falling back to default\n");
123 }
124
125defaults:
126#endif
127
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800128 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200129 if (err < 0)
130 puts("spl: ext4fs_open failed\n");
131
Simon Glass259cdb42023-09-26 08:14:17 -0600132 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDET8a3aeaf2014-11-25 15:34:16 +0100133 if (err < 0) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200134#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
135 printf("%s: error reading image %s, err - %d\n",
136 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
137#endif
138 return -1;
139 }
140
Pali Rohárdda8f882022-01-14 14:31:38 +0100141 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200142 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
143}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200144#else
Simon Glass0649e912016-09-24 18:20:14 -0600145int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100146 struct spl_boot_device *bootdev,
Simon Glass0649e912016-09-24 18:20:14 -0600147 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200148{
149 return -ENOSYS;
150}
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200151#endif