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