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