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