blob: af836ca15b8897de463db1ce442bc70f3d149366 [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>
Sean Andersonb02c4e92023-10-14 16:47:55 -04005#include <mapmem.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06006#include <part.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02007#include <spl.h>
8#include <asm/u-boot.h>
9#include <ext4fs.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +020010#include <errno.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +020011#include <image.h>
12
Simon Glassb4a6c2a2016-09-24 18:20:14 -060013int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010014 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -060015 struct blk_desc *block_dev, int partition,
16 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020017{
18 s32 err;
Simon Glassf3543e62022-09-06 20:26:52 -060019 struct legacy_img_hdr *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080020 loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060021 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020022
Marek Vasut04ce5422018-08-14 11:27:02 +020023 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET592f9222014-10-15 17:53:12 +020024
Simon Glass3e8bd462016-02-29 15:25:48 -070025 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-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
Mayuresh Chitale7a5f4e42023-05-02 21:40:20 +053032 err = ext4fs_mount(part_info.size);
Guillaume GARDET592f9222014-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 Schaeferea5003a2020-06-16 22:03:52 +020037 return -1;
Guillaume GARDET592f9222014-10-15 17:53:12 +020038 }
39
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080040 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020041 if (err < 0) {
42 puts("spl: ext4fs_open failed\n");
43 goto end;
44 }
Simon Glassf3543e62022-09-06 20:26:52 -060045 err = ext4fs_read((char *)header, 0, sizeof(struct legacy_img_hdr), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010046 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020047 puts("spl: ext4fs_read failed\n");
48 goto end;
49 }
50
Pali Rohár2e0429b2022-01-14 14:31:38 +010051 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020052 if (err < 0) {
Petr Kulhavy9ab165d2016-06-18 12:21:17 +020053 puts("spl: ext: failed to parse image header\n");
Marek Vasut7e0f2262016-04-29 00:44:54 +020054 goto end;
55 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020056
Sean Andersonb02c4e92023-10-14 16:47:55 -040057 err = ext4fs_read(map_sysmem(spl_image->load_addr, filelen), 0, filelen,
58 &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020059
60end:
61#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010062 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020063 printf("%s: error reading image %s, err - %d\n",
64 __func__, filename, err);
65#endif
66
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010067 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020068}
69
Tom Rini71150072021-10-30 23:03:48 -040070#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glassb4a6c2a2016-09-24 18:20:14 -060071int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010072 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -060073 struct blk_desc *block_dev, int partition)
Guillaume GARDET592f9222014-10-15 17:53:12 +020074{
75 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080076 __maybe_unused loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060077 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020078 __maybe_unused char *file;
79
Simon Glass3e8bd462016-02-29 15:25:48 -070080 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-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
Mayuresh Chitale7a5f4e42023-05-02 21:40:20 +053087 err = ext4fs_mount(part_info.size);
Guillaume GARDET592f9222014-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 Kulhavy58c95d52016-06-14 12:06:36 +020094#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass00caae62017-08-03 12:22:12 -060095 file = env_get("falcon_args_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +020096 if (file) {
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080097 err = ext4fs_open(file, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020098 if (err < 0) {
99 puts("spl: ext4fs_open failed\n");
100 goto defaults;
101 }
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600102 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100103 if (err < 0) {
Guillaume GARDET592f9222014-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 Glass00caae62017-08-03 12:22:12 -0600108 file = env_get("falcon_image_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +0200109 if (file) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100110 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600111 partition, file);
Guillaume GARDET592f9222014-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 Ramasami9f12cd02014-11-17 14:39:36 -0800128 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200129 if (err < 0)
130 puts("spl: ext4fs_open failed\n");
131
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600132 err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100133 if (err < 0) {
Guillaume GARDET592f9222014-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ár2e0429b2022-01-14 14:31:38 +0100141 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET592f9222014-10-15 17:53:12 +0200142 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
143}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200144#else
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600145int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100146 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600147 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200148{
149 return -ENOSYS;
150}
Guillaume GARDET592f9222014-10-15 17:53:12 +0200151#endif