Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | b7b5b0c | 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 | b7b5b0c | 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 | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 14 | #include <spl.h> |
| 15 | #include <asm/u-boot.h> |
| 16 | #include <fat.h> |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 18 | #include <image.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 20 | |
| 21 | static int fat_registered; |
| 22 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 23 | static int spl_register_fat_device(struct blk_desc *block_dev, int partition) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 24 | { |
| 25 | int err = 0; |
| 26 | |
| 27 | if (fat_registered) |
| 28 | return err; |
| 29 | |
| 30 | err = fat_register_device(block_dev, partition); |
| 31 | if (err) { |
| 32 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 33 | printf("%s: fat register err - %d\n", __func__, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 34 | #endif |
Guillaume GARDET | a23ec1d | 2014-10-15 17:53:14 +0200 | [diff] [blame] | 35 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | fat_registered = 1; |
| 39 | |
| 40 | return err; |
| 41 | } |
| 42 | |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 43 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 44 | ulong size, void *buf) |
| 45 | { |
| 46 | loff_t actread; |
| 47 | int ret; |
| 48 | char *filename = (char *)load->filename; |
| 49 | |
| 50 | ret = fat_read_file(filename, buf, file_offset, size, &actread); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | return actread; |
| 55 | } |
| 56 | |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 57 | int spl_load_image_fat(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 58 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 59 | struct blk_desc *block_dev, int partition, |
| 60 | const char *filename) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 61 | { |
| 62 | int err; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 63 | struct legacy_img_hdr *header; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 64 | |
| 65 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 66 | if (err) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 67 | goto end; |
| 68 | |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 69 | header = spl_get_load_buffer(-sizeof(*header), sizeof(*header)); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 70 | |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 71 | err = file_fat_read(filename, header, sizeof(struct legacy_img_hdr)); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 72 | if (err <= 0) |
| 73 | goto end; |
| 74 | |
Marek Vasut | c659e58 | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 75 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 76 | image_get_magic(header) == FDT_MAGIC) { |
| 77 | err = file_fat_read(filename, (void *)CONFIG_SYS_LOAD_ADDR, 0); |
| 78 | if (err <= 0) |
| 79 | goto end; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 80 | err = spl_parse_image_header(spl_image, bootdev, |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 81 | (struct legacy_img_hdr *)CONFIG_SYS_LOAD_ADDR); |
Marek Vasut | c659e58 | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 82 | if (err == -EAGAIN) |
| 83 | return err; |
| 84 | if (err == 0) |
| 85 | err = 1; |
| 86 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 87 | image_get_magic(header) == FDT_MAGIC) { |
| 88 | struct spl_load_info load; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 89 | |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 90 | debug("Found FIT\n"); |
| 91 | load.read = spl_fit_read; |
| 92 | load.bl_len = 1; |
| 93 | load.filename = (void *)filename; |
| 94 | load.priv = NULL; |
| 95 | |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 96 | return spl_load_simple_fit(spl_image, &load, 0, header); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 97 | } else { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 98 | err = spl_parse_image_header(spl_image, bootdev, header); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 99 | if (err) |
| 100 | goto end; |
| 101 | |
Michal Simek | a0dfb84 | 2016-04-27 16:07:20 +0200 | [diff] [blame] | 102 | err = file_fat_read(filename, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 103 | (u8 *)(uintptr_t)spl_image->load_addr, 0); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 104 | } |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 105 | |
| 106 | end: |
| 107 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 108 | if (err <= 0) |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 109 | printf("%s: error reading image %s, err - %d\n", |
| 110 | __func__, filename, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 111 | #endif |
| 112 | |
| 113 | return (err <= 0); |
| 114 | } |
| 115 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 116 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 117 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 118 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 119 | struct blk_desc *block_dev, int partition) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 120 | { |
| 121 | int err; |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 122 | __maybe_unused char *file; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 123 | |
| 124 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 125 | if (err) |
| 126 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 127 | |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 128 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 129 | file = env_get("falcon_args_file"); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 130 | if (file) { |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 131 | err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 132 | if (err <= 0) { |
| 133 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 134 | file, err); |
| 135 | goto defaults; |
| 136 | } |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 137 | file = env_get("falcon_image_file"); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 138 | if (file) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 139 | err = spl_load_image_fat(spl_image, bootdev, block_dev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 140 | partition, file); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 141 | if (err != 0) { |
| 142 | puts("spl: falling back to default\n"); |
| 143 | goto defaults; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } else |
| 148 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 149 | } else |
| 150 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 151 | |
| 152 | defaults: |
| 153 | #endif |
| 154 | |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 155 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 156 | (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 157 | if (err <= 0) { |
| 158 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 159 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 160 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 161 | #endif |
| 162 | return -1; |
| 163 | } |
| 164 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 165 | return spl_load_image_fat(spl_image, bootdev, block_dev, partition, |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 166 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 167 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 168 | #else |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 169 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 170 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 171 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 172 | { |
| 173 | return -ENOSYS; |
| 174 | } |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 175 | #endif |