blob: 2bf34344391c1bb85977838aaae057930697e684 [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>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06005#include <part.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02006#include <spl.h>
7#include <asm/u-boot.h>
8#include <ext4fs.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +02009#include <errno.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +020010#include <image.h>
11
Simon Glassb4a6c2a2016-09-24 18:20:14 -060012int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010013 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -060014 struct blk_desc *block_dev, int partition,
15 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020016{
17 s32 err;
Simon Glassf3543e62022-09-06 20:26:52 -060018 struct legacy_img_hdr *header;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080019 loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060020 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020021
Marek Vasut04ce5422018-08-14 11:27:02 +020022 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Guillaume GARDET592f9222014-10-15 17:53:12 +020023
Simon Glass3e8bd462016-02-29 15:25:48 -070024 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020025 printf("spl: no partition table found\n");
26 return -1;
27 }
28
29 ext4fs_set_blk_dev(block_dev, &part_info);
30
Mayuresh Chitale7a5f4e42023-05-02 21:40:20 +053031 err = ext4fs_mount(part_info.size);
Guillaume GARDET592f9222014-10-15 17:53:12 +020032 if (!err) {
33#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
34 printf("%s: ext4fs mount err - %d\n", __func__, err);
35#endif
Thomas Schaeferea5003a2020-06-16 22:03:52 +020036 return -1;
Guillaume GARDET592f9222014-10-15 17:53:12 +020037 }
38
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080039 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020040 if (err < 0) {
41 puts("spl: ext4fs_open failed\n");
42 goto end;
43 }
Simon Glassf3543e62022-09-06 20:26:52 -060044 err = ext4fs_read((char *)header, 0, sizeof(struct legacy_img_hdr), &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010045 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020046 puts("spl: ext4fs_read failed\n");
47 goto end;
48 }
49
Pali Rohár2e0429b2022-01-14 14:31:38 +010050 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020051 if (err < 0) {
Petr Kulhavy9ab165d2016-06-18 12:21:17 +020052 puts("spl: ext: failed to parse image header\n");
Marek Vasut7e0f2262016-04-29 00:44:54 +020053 goto end;
54 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020055
Stefan Brüns66a47ff2016-11-06 18:33:57 +010056 err = ext4fs_read((char *)spl_image->load_addr, 0, filelen, &actlen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020057
58end:
59#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010060 if (err < 0)
Guillaume GARDET592f9222014-10-15 17:53:12 +020061 printf("%s: error reading image %s, err - %d\n",
62 __func__, filename, err);
63#endif
64
Guillaume GARDETd3e488e2014-11-25 15:34:16 +010065 return err < 0;
Guillaume GARDET592f9222014-10-15 17:53:12 +020066}
67
Tom Rini71150072021-10-30 23:03:48 -040068#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glassb4a6c2a2016-09-24 18:20:14 -060069int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010070 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -060071 struct blk_desc *block_dev, int partition)
Guillaume GARDET592f9222014-10-15 17:53:12 +020072{
73 int err;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080074 __maybe_unused loff_t filelen, actlen;
Simon Glass05289792020-05-10 11:39:57 -060075 struct disk_partition part_info = {};
Guillaume GARDET592f9222014-10-15 17:53:12 +020076 __maybe_unused char *file;
77
Simon Glass3e8bd462016-02-29 15:25:48 -070078 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020079 printf("spl: no partition table found\n");
80 return -1;
81 }
82
83 ext4fs_set_blk_dev(block_dev, &part_info);
84
Mayuresh Chitale7a5f4e42023-05-02 21:40:20 +053085 err = ext4fs_mount(part_info.size);
Guillaume GARDET592f9222014-10-15 17:53:12 +020086 if (!err) {
87#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
88 printf("%s: ext4fs mount err - %d\n", __func__, err);
89#endif
90 return -1;
91 }
Petr Kulhavy58c95d52016-06-14 12:06:36 +020092#if defined(CONFIG_SPL_ENV_SUPPORT)
Simon Glass00caae62017-08-03 12:22:12 -060093 file = env_get("falcon_args_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +020094 if (file) {
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080095 err = ext4fs_open(file, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020096 if (err < 0) {
97 puts("spl: ext4fs_open failed\n");
98 goto defaults;
99 }
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100100 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100101 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200102 printf("spl: error reading image %s, err - %d, falling back to default\n",
103 file, err);
104 goto defaults;
105 }
Simon Glass00caae62017-08-03 12:22:12 -0600106 file = env_get("falcon_image_file");
Guillaume GARDET592f9222014-10-15 17:53:12 +0200107 if (file) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100108 err = spl_load_image_ext(spl_image, bootdev, block_dev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600109 partition, file);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200110 if (err != 0) {
111 puts("spl: falling back to default\n");
112 goto defaults;
113 }
114
115 return 0;
116 } else {
117 puts("spl: falcon_image_file not set in environment, falling back to default\n");
118 }
119 } else {
120 puts("spl: falcon_args_file not set in environment, falling back to default\n");
121 }
122
123defaults:
124#endif
125
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800126 err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +0200127 if (err < 0)
128 puts("spl: ext4fs_open failed\n");
129
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100130 err = ext4fs_read((void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, filelen, &actlen);
Guillaume GARDETd3e488e2014-11-25 15:34:16 +0100131 if (err < 0) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200132#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
133 printf("%s: error reading image %s, err - %d\n",
134 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
135#endif
136 return -1;
137 }
138
Pali Rohár2e0429b2022-01-14 14:31:38 +0100139 return spl_load_image_ext(spl_image, bootdev, block_dev, partition,
Guillaume GARDET592f9222014-10-15 17:53:12 +0200140 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
141}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200142#else
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600143int spl_load_image_ext_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100144 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -0600145 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200146{
147 return -ENOSYS;
148}
Guillaume GARDET592f9222014-10-15 17:53:12 +0200149#endif