Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Dan Murphy <dmurphy@ti.com> |
| 7 | * |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 8 | * FAT Image Functions copied from spl_mmc.c |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 14 | #include <mapmem.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 15 | #include <spl.h> |
| 16 | #include <asm/u-boot.h> |
| 17 | #include <fat.h> |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 19 | #include <image.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 20 | #include <linux/libfdt.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 21 | |
| 22 | static int fat_registered; |
| 23 | |
Sean Anderson | 93caa3e | 2023-10-14 16:47:54 -0400 | [diff] [blame] | 24 | void spl_fat_force_reregister(void) |
| 25 | { |
| 26 | fat_registered = 0; |
| 27 | } |
| 28 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 29 | static int spl_register_fat_device(struct blk_desc *block_dev, int partition) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 30 | { |
| 31 | int err = 0; |
| 32 | |
| 33 | if (fat_registered) |
| 34 | return err; |
| 35 | |
| 36 | err = fat_register_device(block_dev, partition); |
| 37 | if (err) { |
| 38 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 39 | printf("%s: fat register err - %d\n", __func__, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 40 | #endif |
Guillaume GARDET | 7d2b4e7 | 2014-10-15 17:53:14 +0200 | [diff] [blame] | 41 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | fat_registered = 1; |
| 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 49 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 50 | ulong size, void *buf) |
| 51 | { |
| 52 | loff_t actread; |
| 53 | int ret; |
Sean Anderson | afdd2d9 | 2023-11-08 11:48:42 -0500 | [diff] [blame^] | 54 | char *filename = load->priv; |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 55 | |
| 56 | ret = fat_read_file(filename, buf, file_offset, size, &actread); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | return actread; |
| 61 | } |
| 62 | |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 63 | int spl_load_image_fat(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 64 | struct spl_boot_device *bootdev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 65 | struct blk_desc *block_dev, int partition, |
| 66 | const char *filename) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 67 | { |
| 68 | int err; |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 69 | struct legacy_img_hdr *header; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 70 | |
| 71 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 72 | if (err) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 73 | goto end; |
| 74 | |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 75 | header = spl_get_load_buffer(-sizeof(*header), sizeof(*header)); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 76 | |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 77 | err = file_fat_read(filename, header, sizeof(struct legacy_img_hdr)); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 78 | if (err <= 0) |
| 79 | goto end; |
| 80 | |
Marek Vasut | 8b1531f | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 81 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 82 | image_get_magic(header) == FDT_MAGIC) { |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 83 | err = file_fat_read(filename, |
| 84 | map_sysmem(CONFIG_SYS_LOAD_ADDR, 0), 0); |
Marek Vasut | 8b1531f | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 85 | if (err <= 0) |
| 86 | goto end; |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 87 | err = spl_parse_image_header(spl_image, bootdev, |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 88 | map_sysmem(CONFIG_SYS_LOAD_ADDR, |
| 89 | err)); |
Marek Vasut | 8b1531f | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 90 | if (err == -EAGAIN) |
| 91 | return err; |
| 92 | if (err == 0) |
| 93 | err = 1; |
| 94 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 95 | image_get_magic(header) == FDT_MAGIC) { |
| 96 | struct spl_load_info load; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 97 | |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 98 | debug("Found FIT\n"); |
| 99 | load.read = spl_fit_read; |
Sean Anderson | b63664b | 2023-11-08 11:48:41 -0500 | [diff] [blame] | 100 | load.bl_len = ARCH_DMA_MINALIGN; |
Sean Anderson | afdd2d9 | 2023-11-08 11:48:42 -0500 | [diff] [blame^] | 101 | load.priv = (void *)filename; |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 102 | |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 103 | return spl_load_simple_fit(spl_image, &load, 0, header); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 104 | } else { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 105 | err = spl_parse_image_header(spl_image, bootdev, header); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 106 | if (err) |
| 107 | goto end; |
| 108 | |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 109 | err = file_fat_read(filename, map_sysmem(spl_image->load_addr, |
| 110 | spl_image->size), 0); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 111 | } |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 112 | |
| 113 | end: |
| 114 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 115 | if (err <= 0) |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 116 | printf("%s: error reading image %s, err - %d\n", |
| 117 | __func__, filename, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | return (err <= 0); |
| 121 | } |
| 122 | |
Tom Rini | 7115007 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 123 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 124 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 125 | struct spl_boot_device *bootdev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 126 | struct blk_desc *block_dev, int partition) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 127 | { |
| 128 | int err; |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 129 | __maybe_unused char *file; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 130 | |
| 131 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 132 | if (err) |
| 133 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 134 | |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 135 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 136 | file = env_get("falcon_args_file"); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 137 | if (file) { |
Simon Glass | 9cbdc3a | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 138 | err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 139 | if (err <= 0) { |
| 140 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 141 | file, err); |
| 142 | goto defaults; |
| 143 | } |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 144 | file = env_get("falcon_image_file"); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 145 | if (file) { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 146 | err = spl_load_image_fat(spl_image, bootdev, block_dev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 147 | partition, file); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 148 | if (err != 0) { |
| 149 | puts("spl: falling back to default\n"); |
| 150 | goto defaults; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } else |
| 155 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 156 | } else |
| 157 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 158 | |
| 159 | defaults: |
| 160 | #endif |
| 161 | |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 162 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Simon Glass | 9cbdc3a | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 163 | (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 164 | if (err <= 0) { |
| 165 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 166 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 167 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 168 | #endif |
| 169 | return -1; |
| 170 | } |
| 171 | |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 172 | return spl_load_image_fat(spl_image, bootdev, block_dev, partition, |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 173 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 174 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 175 | #else |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 176 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 177 | struct spl_boot_device *bootdev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 178 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 179 | { |
| 180 | return -ENOSYS; |
| 181 | } |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 182 | #endif |