Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2014 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * |
| 5 | * Dan Murphy <dmurphy@ti.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
| 9 | * FAT Image Functions copied from spl_mmc.c |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <spl.h> |
| 14 | #include <asm/u-boot.h> |
| 15 | #include <fat.h> |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 17 | #include <image.h> |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 18 | #include <libfdt.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 19 | |
| 20 | static int fat_registered; |
| 21 | |
| 22 | #ifdef CONFIG_SPL_FAT_SUPPORT |
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, |
| 58 | struct blk_desc *block_dev, int partition, |
| 59 | const char *filename) |
Dan Murphy | b7b5b0c | 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 | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 65 | if (err) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 66 | goto end; |
| 67 | |
| 68 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 69 | sizeof(struct image_header)); |
| 70 | |
| 71 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
| 72 | if (err <= 0) |
| 73 | goto end; |
| 74 | |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 75 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 76 | image_get_magic(header) == FDT_MAGIC) { |
| 77 | struct spl_load_info load; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 78 | |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 79 | debug("Found FIT\n"); |
| 80 | load.read = spl_fit_read; |
| 81 | load.bl_len = 1; |
| 82 | load.filename = (void *)filename; |
| 83 | load.priv = NULL; |
| 84 | |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 85 | return spl_load_simple_fit(spl_image, &load, 0, header); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 86 | } else { |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 87 | err = spl_parse_image_header(spl_image, header); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 88 | if (err) |
| 89 | goto end; |
| 90 | |
Michal Simek | a0dfb84 | 2016-04-27 16:07:20 +0200 | [diff] [blame] | 91 | err = file_fat_read(filename, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 92 | (u8 *)(uintptr_t)spl_image->load_addr, 0); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 93 | } |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 94 | |
| 95 | end: |
| 96 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 97 | if (err <= 0) |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 98 | printf("%s: error reading image %s, err - %d\n", |
| 99 | __func__, filename, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | return (err <= 0); |
| 103 | } |
| 104 | |
| 105 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 106 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
| 107 | struct blk_desc *block_dev, int partition) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 108 | { |
| 109 | int err; |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 110 | __maybe_unused char *file; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 111 | |
| 112 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 113 | if (err) |
| 114 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 115 | |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 116 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
| 117 | file = getenv("falcon_args_file"); |
| 118 | if (file) { |
| 119 | err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 120 | if (err <= 0) { |
| 121 | printf("spl: error reading image %s, err - %d, falling back to default\n", |
| 122 | file, err); |
| 123 | goto defaults; |
| 124 | } |
| 125 | file = getenv("falcon_image_file"); |
| 126 | if (file) { |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 127 | err = spl_load_image_fat(spl_image, block_dev, |
| 128 | partition, file); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 129 | if (err != 0) { |
| 130 | puts("spl: falling back to default\n"); |
| 131 | goto defaults; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } else |
| 136 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); |
| 137 | } else |
| 138 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); |
| 139 | |
| 140 | defaults: |
| 141 | #endif |
| 142 | |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 143 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 144 | (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 145 | if (err <= 0) { |
| 146 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 147 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 148 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 149 | #endif |
| 150 | return -1; |
| 151 | } |
| 152 | |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 153 | return spl_load_image_fat(spl_image, block_dev, partition, |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 154 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 155 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 156 | #else |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 157 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
| 158 | struct blk_desc *block_dev, int partition) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 159 | { |
| 160 | return -ENOSYS; |
| 161 | } |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 162 | #endif |
| 163 | #endif |