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