Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 2 | |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 3 | #include <env.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 4 | #include <part.h> |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 5 | #include <spl.h> |
Sean Anderson | f727cc1 | 2023-11-08 11:48:48 -0500 | [diff] [blame] | 6 | #include <spl_load.h> |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 7 | #include <ext4fs.h> |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 9 | #include <image.h> |
| 10 | |
Sean Anderson | f727cc1 | 2023-11-08 11:48:48 -0500 | [diff] [blame] | 11 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 12 | ulong size, void *buf) |
| 13 | { |
| 14 | int ret; |
| 15 | loff_t actlen; |
| 16 | |
| 17 | ret = ext4fs_read(buf, file_offset, size, &actlen); |
| 18 | if (ret) |
| 19 | return ret; |
| 20 | return actlen; |
| 21 | } |
| 22 | |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 23 | int spl_load_image_ext(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 24 | struct spl_boot_device *bootdev, |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 25 | struct blk_desc *block_dev, int partition, |
| 26 | const char *filename) |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 27 | { |
| 28 | s32 err; |
Sean Anderson | f727cc1 | 2023-11-08 11:48:48 -0500 | [diff] [blame] | 29 | loff_t filelen; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 30 | struct disk_partition part_info = {}; |
Sean Anderson | f727cc1 | 2023-11-08 11:48:48 -0500 | [diff] [blame] | 31 | struct spl_load_info load; |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 32 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 33 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 34 | printf("spl: no partition table found\n"); |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 39 | |
Sean Anderson | ab125f5 | 2023-11-08 12:51:09 -0500 | [diff] [blame] | 40 | err = ext4fs_mount(); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 41 | if (!err) { |
| 42 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 43 | printf("%s: ext4fs mount err - %d\n", __func__, err); |
| 44 | #endif |
Thomas Schaefer | 37aea85 | 2020-06-16 22:03:52 +0200 | [diff] [blame] | 45 | return -1; |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 48 | err = ext4fs_open(filename, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 49 | if (err < 0) { |
| 50 | puts("spl: ext4fs_open failed\n"); |
| 51 | goto end; |
| 52 | } |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 53 | |
Sean Anderson | f727cc1 | 2023-11-08 11:48:48 -0500 | [diff] [blame] | 54 | spl_set_bl_len(&load, 1); |
| 55 | load.read = spl_fit_read; |
| 56 | err = spl_load(spl_image, bootdev, &load, filelen, 0); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 57 | |
| 58 | end: |
| 59 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 60 | if (err < 0) |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 61 | printf("%s: error reading image %s, err - %d\n", |
| 62 | __func__, filename, err); |
| 63 | #endif |
| 64 | |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 65 | return err < 0; |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 68 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 69 | int spl_load_image_ext_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 70 | struct spl_boot_device *bootdev, |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 71 | struct blk_desc *block_dev, int partition) |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 72 | { |
| 73 | int err; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 74 | __maybe_unused loff_t filelen, actlen; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 75 | struct disk_partition part_info = {}; |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 76 | __maybe_unused char *file; |
| 77 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 78 | if (part_get_info(block_dev, partition, &part_info)) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 79 | printf("spl: no partition table found\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | ext4fs_set_blk_dev(block_dev, &part_info); |
| 84 | |
Sean Anderson | ab125f5 | 2023-11-08 12:51:09 -0500 | [diff] [blame] | 85 | err = ext4fs_mount(); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 86 | 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 Kulhavy | ecb2364 | 2016-06-14 12:06:36 +0200 | [diff] [blame] | 92 | #if defined(CONFIG_SPL_ENV_SUPPORT) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 93 | file = env_get("falcon_args_file"); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 94 | if (file) { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 95 | err = ext4fs_open(file, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 96 | if (err < 0) { |
| 97 | puts("spl: ext4fs_open failed\n"); |
| 98 | goto defaults; |
| 99 | } |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 100 | err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen); |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 101 | if (err < 0) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 102 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 103 | file, err); |
| 104 | goto defaults; |
| 105 | } |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 106 | file = env_get("falcon_image_file"); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 107 | if (file) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 108 | err = spl_load_image_ext(spl_image, bootdev, block_dev, |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 109 | partition, file); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 110 | 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 | |
| 123 | defaults: |
| 124 | #endif |
| 125 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 126 | err = ext4fs_open(CONFIG_SPL_FS_LOAD_ARGS_NAME, &filelen); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 127 | if (err < 0) |
| 128 | puts("spl: ext4fs_open failed\n"); |
| 129 | |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 130 | err = ext4fs_read((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0, filelen, &actlen); |
Guillaume GARDET | 8a3aeaf | 2014-11-25 15:34:16 +0100 | [diff] [blame] | 131 | if (err < 0) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 132 | #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ár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 139 | return spl_load_image_ext(spl_image, bootdev, block_dev, partition, |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 140 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
| 141 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 142 | #else |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 143 | int spl_load_image_ext_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 144 | struct spl_boot_device *bootdev, |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 145 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 146 | { |
| 147 | return -ENOSYS; |
| 148 | } |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 149 | #endif |