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> |
Dan Murphy | 773b594 | 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 | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 18 | #include <image.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 20 | |
| 21 | static int fat_registered; |
| 22 | |
Simon Glass | 4101f68 | 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 | 773b594 | 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 | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 33 | printf("%s: fat register err - %d\n", __func__, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 34 | #endif |
Guillaume GARDET | 7d2b4e7 | 2014-10-15 17:53:14 +0200 | [diff] [blame] | 35 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | fat_registered = 1; |
| 39 | |
| 40 | return err; |
| 41 | } |
| 42 | |
Lokesh Vutla | 97ca364 | 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 | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 57 | int spl_load_image_fat(struct spl_image_info *spl_image, |
| 58 | struct blk_desc *block_dev, int partition, |
| 59 | const char *filename) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 60 | { |
| 61 | int err; |
| 62 | struct image_header *header; |
| 63 | |
| 64 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 65 | if (err) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 66 | goto end; |
| 67 | |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 68 | header = spl_get_load_buffer(-sizeof(*header), sizeof(*header)); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 69 | |
| 70 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
| 71 | if (err <= 0) |
| 72 | goto end; |
| 73 | |
Marek Vasut | 8b1531f | 2018-05-31 17:59:19 +0200 | [diff] [blame] | 74 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 75 | image_get_magic(header) == FDT_MAGIC) { |
| 76 | err = file_fat_read(filename, (void *)CONFIG_SYS_LOAD_ADDR, 0); |
| 77 | if (err <= 0) |
| 78 | goto end; |
| 79 | err = spl_parse_image_header(spl_image, |
| 80 | (struct image_header *)CONFIG_SYS_LOAD_ADDR); |
| 81 | if (err == -EAGAIN) |
| 82 | return err; |
| 83 | if (err == 0) |
| 84 | err = 1; |
| 85 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 86 | image_get_magic(header) == FDT_MAGIC) { |
| 87 | struct spl_load_info load; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 88 | |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 89 | debug("Found FIT\n"); |
| 90 | load.read = spl_fit_read; |
| 91 | load.bl_len = 1; |
| 92 | load.filename = (void *)filename; |
| 93 | load.priv = NULL; |
| 94 | |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 95 | return spl_load_simple_fit(spl_image, &load, 0, header); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 96 | } else { |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 97 | err = spl_parse_image_header(spl_image, header); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 98 | if (err) |
| 99 | goto end; |
| 100 | |
Michal Simek | 1eefe14 | 2016-04-27 16:07:20 +0200 | [diff] [blame] | 101 | err = file_fat_read(filename, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 102 | (u8 *)(uintptr_t)spl_image->load_addr, 0); |
Lokesh Vutla | 97ca364 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 103 | } |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 104 | |
| 105 | end: |
| 106 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 107 | if (err <= 0) |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 108 | printf("%s: error reading image %s, err - %d\n", |
| 109 | __func__, filename, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | return (err <= 0); |
| 113 | } |
| 114 | |
| 115 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 116 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
| 117 | struct blk_desc *block_dev, int partition) |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 118 | { |
| 119 | int err; |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 120 | __maybe_unused char *file; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 121 | |
| 122 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 123 | if (err) |
| 124 | return err; |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 125 | |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 126 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 127 | file = env_get("falcon_args_file"); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 128 | if (file) { |
| 129 | err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 130 | if (err <= 0) { |
| 131 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 132 | file, err); |
| 133 | goto defaults; |
| 134 | } |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 135 | file = env_get("falcon_image_file"); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 136 | if (file) { |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 137 | err = spl_load_image_fat(spl_image, block_dev, |
| 138 | partition, file); |
Tom Rini | ae1590e | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 139 | if (err != 0) { |
| 140 | puts("spl: falling back to default\n"); |
| 141 | goto defaults; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } else |
| 146 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 147 | } else |
| 148 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 149 | |
| 150 | defaults: |
| 151 | #endif |
| 152 | |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 153 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 154 | (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 155 | if (err <= 0) { |
| 156 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 157 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 158 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 159 | #endif |
| 160 | return -1; |
| 161 | } |
| 162 | |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 163 | return spl_load_image_fat(spl_image, block_dev, partition, |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 164 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 165 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 166 | #else |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 167 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
| 168 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 169 | { |
| 170 | return -ENOSYS; |
| 171 | } |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 172 | #endif |