blob: 2be6f04b02c5e6b2573a5556483dfc8dc094d657 [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>
Sean Andersonb8ed7222023-11-08 11:48:48 -05007#include <spl_load.h>
Guillaume GARDET592f9222014-10-15 17:53:12 +02008#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
Sean Andersonb8ed7222023-11-08 11:48:48 -050013static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
14 ulong size, void *buf)
15{
16 int ret;
17 loff_t actlen;
18
19 ret = ext4fs_read(buf, file_offset, size, &actlen);
20 if (ret)
21 return ret;
22 return actlen;
23}
24
Simon Glassb4a6c2a2016-09-24 18:20:14 -060025int spl_load_image_ext(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010026 struct spl_boot_device *bootdev,
Simon Glassb4a6c2a2016-09-24 18:20:14 -060027 struct blk_desc *block_dev, int partition,
28 const char *filename)
Guillaume GARDET592f9222014-10-15 17:53:12 +020029{
30 s32 err;
Sean Andersonb8ed7222023-11-08 11:48:48 -050031 loff_t filelen;
Simon Glass05289792020-05-10 11:39:57 -060032 struct disk_partition part_info = {};
Sean Andersonb8ed7222023-11-08 11:48:48 -050033 struct spl_load_info load;
Guillaume GARDET592f9222014-10-15 17:53:12 +020034
Simon Glass3e8bd462016-02-29 15:25:48 -070035 if (part_get_info(block_dev, partition, &part_info)) {
Guillaume GARDET592f9222014-10-15 17:53:12 +020036 printf("spl: no partition table found\n");
37 return -1;
38 }
39
40 ext4fs_set_blk_dev(block_dev, &part_info);
41
Sean Anderson7667bde2023-11-08 12:51:09 -050042 err = ext4fs_mount();
Guillaume GARDET592f9222014-10-15 17:53:12 +020043 if (!err) {
44#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
45 printf("%s: ext4fs mount err - %d\n", __func__, err);
46#endif
Thomas Schaeferea5003a2020-06-16 22:03:52 +020047 return -1;
Guillaume GARDET592f9222014-10-15 17:53:12 +020048 }
49
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080050 err = ext4fs_open(filename, &filelen);
Guillaume GARDET592f9222014-10-15 17:53:12 +020051 if (err < 0) {
52 puts("spl: ext4fs_open failed\n");
53 goto end;
54 }
Guillaume GARDET592f9222014-10-15 17:53:12 +020055
Sean Andersonb8ed7222023-11-08 11:48:48 -050056 spl_set_bl_len(&load, 1);
57 load.read = spl_fit_read;
58 err = spl_load(spl_image, bootdev, &load, filelen, 0);
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
Sean Anderson7667bde2023-11-08 12:51:09 -050087 err = ext4fs_mount();
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