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> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 14 | #include <spl.h> |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 15 | #include <spl_load.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 16 | #include <asm/u-boot.h> |
| 17 | #include <fat.h> |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 19 | #include <image.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 20 | #include <linux/libfdt.h> |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 21 | |
| 22 | static int fat_registered; |
| 23 | |
Sean Anderson | 13e6d91 | 2023-10-14 16:47:54 -0400 | [diff] [blame] | 24 | void spl_fat_force_reregister(void) |
| 25 | { |
| 26 | fat_registered = 0; |
| 27 | } |
| 28 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 29 | 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] | 30 | { |
| 31 | int err = 0; |
| 32 | |
| 33 | if (fat_registered) |
| 34 | return err; |
| 35 | |
| 36 | err = fat_register_device(block_dev, partition); |
| 37 | if (err) { |
| 38 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 39 | printf("%s: fat register err - %d\n", __func__, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 40 | #endif |
Guillaume GARDET | a23ec1d | 2014-10-15 17:53:14 +0200 | [diff] [blame] | 41 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | fat_registered = 1; |
| 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 49 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 50 | ulong size, void *buf) |
| 51 | { |
| 52 | loff_t actread; |
| 53 | int ret; |
Sean Anderson | fcb9176 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 54 | char *filename = load->priv; |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 55 | |
| 56 | ret = fat_read_file(filename, buf, file_offset, size, &actread); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | return actread; |
| 61 | } |
| 62 | |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 63 | int spl_load_image_fat(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 64 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 65 | struct blk_desc *block_dev, int partition, |
| 66 | const char *filename) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 67 | { |
| 68 | int err; |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 69 | loff_t size; |
| 70 | struct spl_load_info load; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 71 | |
| 72 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 73 | if (err) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 74 | goto end; |
| 75 | |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 76 | /* |
| 77 | * Avoid pulling in this function for other image types since we are |
| 78 | * very short on space on some boards. |
| 79 | */ |
| 80 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) { |
| 81 | err = fat_size(filename, &size); |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 82 | if (err) |
| 83 | goto end; |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 84 | } else { |
| 85 | size = 0; |
Lokesh Vutla | a0913a3 | 2016-05-24 10:34:39 +0530 | [diff] [blame] | 86 | } |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 87 | |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 88 | load.read = spl_fit_read; |
| 89 | spl_set_bl_len(&load, ARCH_DMA_MINALIGN); |
| 90 | load.priv = (void *)filename; |
| 91 | err = spl_load(spl_image, bootdev, &load, size, 0); |
| 92 | |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 93 | end: |
| 94 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 95 | if (err < 0) |
Dan Murphy | cb367e1 | 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 | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 98 | #endif |
| 99 | |
Sean Anderson | 09a4602 | 2023-11-08 11:48:49 -0500 | [diff] [blame^] | 100 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 103 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 104 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 105 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 106 | struct blk_desc *block_dev, int partition) |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 107 | { |
| 108 | int err; |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 109 | __maybe_unused char *file; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 110 | |
| 111 | err = spl_register_fat_device(block_dev, partition); |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 112 | if (err) |
| 113 | return err; |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 114 | |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 115 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 116 | file = env_get("falcon_args_file"); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 117 | if (file) { |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 118 | err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 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 | } |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 124 | file = env_get("falcon_image_file"); |
Tom Rini | 70566b6 | 2014-03-28 12:03:42 -0400 | [diff] [blame] | 125 | if (file) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 126 | err = spl_load_image_fat(spl_image, bootdev, block_dev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 127 | partition, file); |
Tom Rini | 70566b6 | 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 | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 142 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 143 | (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 144 | if (err <= 0) { |
| 145 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 146 | printf("%s: error reading image %s, err - %d\n", |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 147 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 148 | #endif |
| 149 | return -1; |
| 150 | } |
| 151 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 152 | return spl_load_image_fat(spl_image, bootdev, block_dev, partition, |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 153 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 154 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 155 | #else |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 156 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 157 | struct spl_boot_device *bootdev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 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 |