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