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