Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il> |
| 4 | * |
| 5 | * Authors: Igor Grinberg <grinberg@compulab.co.il> |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
tomas.melin@vaisala.com | 0a7c732 | 2017-01-13 13:20:13 +0200 | [diff] [blame] | 8 | #include <bmp_layout.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Nikita Kiryanov | f58c509 | 2015-01-14 10:42:49 +0200 | [diff] [blame] | 11 | #include <errno.h> |
tomas.melin@vaisala.com | 0a7c732 | 2017-01-13 13:20:13 +0200 | [diff] [blame] | 12 | #include <fs.h> |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 13 | #include <fdt_support.h> |
tomas.melin@vaisala.com | 0a7c732 | 2017-01-13 13:20:13 +0200 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
tomas.melin@vaisala.com | 0a7c732 | 2017-01-13 13:20:13 +0200 | [diff] [blame] | 16 | #include <nand.h> |
| 17 | #include <sata.h> |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 18 | #include <spi.h> |
tomas.melin@vaisala.com | 0a7c732 | 2017-01-13 13:20:13 +0200 | [diff] [blame] | 19 | #include <spi_flash.h> |
| 20 | #include <splash.h> |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 21 | #include <usb.h> |
Simon Glass | 61f0958 | 2021-11-19 13:24:05 -0700 | [diff] [blame] | 22 | #include <virtio.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_SPI_FLASH |
| 28 | static struct spi_flash *sf; |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 29 | static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 30 | { |
| 31 | if (!sf) { |
| 32 | sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, |
| 33 | CONFIG_SF_DEFAULT_CS, |
| 34 | CONFIG_SF_DEFAULT_SPEED, |
| 35 | CONFIG_SF_DEFAULT_MODE); |
| 36 | if (!sf) |
| 37 | return -ENODEV; |
| 38 | } |
| 39 | |
Jaehoon Chung | 6c0a632 | 2020-12-07 17:18:51 +0900 | [diff] [blame] | 40 | return spi_flash_read(sf, offset, read_size, (void *)(uintptr_t)bmp_load_addr); |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 41 | } |
| 42 | #else |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 43 | static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 44 | { |
| 45 | debug("%s: sf support not available\n", __func__); |
| 46 | return -ENOSYS; |
| 47 | } |
| 48 | #endif |
| 49 | |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 50 | #ifdef CONFIG_CMD_NAND |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 51 | static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 52 | { |
Grygorii Strashko | 5f33b82 | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 53 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
| 54 | return nand_read_skip_bad(mtd, offset, |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 55 | &read_size, NULL, |
Grygorii Strashko | 5f33b82 | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 56 | mtd->size, |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 57 | (u_char *)bmp_load_addr); |
| 58 | } |
| 59 | #else |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 60 | static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 61 | { |
| 62 | debug("%s: nand support not available\n", __func__); |
| 63 | return -ENOSYS; |
| 64 | } |
| 65 | #endif |
| 66 | |
Julien Masson | c7b67d9 | 2022-10-17 10:33:21 +0200 | [diff] [blame] | 67 | static int splash_mmc_read_raw(u32 bmp_load_addr, struct splash_location *location, |
| 68 | size_t read_size) |
| 69 | { |
| 70 | struct disk_partition partition; |
| 71 | struct blk_desc *desc; |
| 72 | lbaint_t blkcnt; |
| 73 | int ret, n; |
| 74 | |
| 75 | if (!IS_ENABLED(CONFIG_CMD_MMC)) { |
| 76 | debug("%s: mmc support not available\n", __func__); |
| 77 | return -ENOSYS; |
| 78 | } |
| 79 | |
| 80 | ret = part_get_info_by_dev_and_name_or_num("mmc", location->devpart, &desc, |
| 81 | &partition, 1); |
| 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | |
| 85 | blkcnt = DIV_ROUND_UP(read_size, partition.blksz); |
| 86 | n = blk_dread(desc, partition.start, blkcnt, (void *)(uintptr_t)bmp_load_addr); |
| 87 | |
| 88 | return (n == blkcnt) ? 0 : -EIO; |
| 89 | } |
| 90 | |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 91 | static int splash_storage_read_raw(struct splash_location *location, |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 92 | u32 bmp_load_addr, size_t read_size) |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 93 | { |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 94 | u32 offset; |
| 95 | |
| 96 | if (!location) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | offset = location->offset; |
| 100 | switch (location->storage) { |
Julien Masson | c7b67d9 | 2022-10-17 10:33:21 +0200 | [diff] [blame] | 101 | case SPLASH_STORAGE_MMC: |
| 102 | return splash_mmc_read_raw(bmp_load_addr, location, read_size); |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 103 | case SPLASH_STORAGE_NAND: |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 104 | return splash_nand_read_raw(bmp_load_addr, offset, read_size); |
Nikita Kiryanov | 1ab4c76 | 2015-01-14 10:42:52 +0200 | [diff] [blame] | 105 | case SPLASH_STORAGE_SF: |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 106 | return splash_sf_read_raw(bmp_load_addr, offset, read_size); |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 107 | default: |
| 108 | printf("Unknown splash location\n"); |
| 109 | } |
| 110 | |
| 111 | return -EINVAL; |
Nikita Kiryanov | 4b4bfd6 | 2015-01-14 10:42:50 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 114 | static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr) |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 115 | { |
| 116 | struct bmp_header *bmp_hdr; |
| 117 | int res; |
| 118 | size_t bmp_size, bmp_header_size = sizeof(struct bmp_header); |
| 119 | |
| 120 | if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp) |
| 121 | goto splash_address_too_high; |
| 122 | |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 123 | res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size); |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 124 | if (res < 0) |
| 125 | return res; |
| 126 | |
Jaehoon Chung | 6c0a632 | 2020-12-07 17:18:51 +0900 | [diff] [blame] | 127 | bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 128 | bmp_size = le32_to_cpu(bmp_hdr->file_size); |
| 129 | |
| 130 | if (bmp_load_addr + bmp_size >= gd->start_addr_sp) |
| 131 | goto splash_address_too_high; |
| 132 | |
Nikita Kiryanov | b8183de | 2015-10-29 11:54:40 +0200 | [diff] [blame] | 133 | return splash_storage_read_raw(location, bmp_load_addr, bmp_size); |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 134 | |
| 135 | splash_address_too_high: |
Nikita Kiryanov | 7f9ceea | 2015-01-14 10:42:54 +0200 | [diff] [blame] | 136 | printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 137 | |
Nikita Kiryanov | f58c509 | 2015-01-14 10:42:49 +0200 | [diff] [blame] | 138 | return -EFAULT; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 139 | } |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 140 | |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 141 | static int splash_select_fs_dev(struct splash_location *location) |
| 142 | { |
| 143 | int res; |
| 144 | |
| 145 | switch (location->storage) { |
| 146 | case SPLASH_STORAGE_MMC: |
| 147 | res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY); |
| 148 | break; |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 149 | case SPLASH_STORAGE_USB: |
| 150 | res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY); |
| 151 | break; |
Nikita Kiryanov | 80ca30d | 2015-10-29 11:54:43 +0200 | [diff] [blame] | 152 | case SPLASH_STORAGE_SATA: |
| 153 | res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY); |
| 154 | break; |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 155 | case SPLASH_STORAGE_NAND: |
| 156 | if (location->ubivol != NULL) |
| 157 | res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS); |
| 158 | else |
| 159 | res = -ENODEV; |
| 160 | break; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 161 | default: |
| 162 | printf("Error: unsupported location storage.\n"); |
| 163 | return -ENODEV; |
| 164 | } |
| 165 | |
| 166 | if (res) |
| 167 | printf("Error: could not access storage.\n"); |
| 168 | |
| 169 | return res; |
| 170 | } |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 171 | |
| 172 | #ifdef CONFIG_USB_STORAGE |
| 173 | static int splash_init_usb(void) |
| 174 | { |
| 175 | int err; |
| 176 | |
| 177 | err = usb_init(); |
| 178 | if (err) |
| 179 | return err; |
| 180 | |
Alexey Brodkin | bfcdf3f | 2016-07-01 22:47:36 +0300 | [diff] [blame] | 181 | #ifndef CONFIG_DM_USB |
| 182 | err = usb_stor_scan(1) < 0 ? -ENODEV : 0; |
| 183 | #endif |
| 184 | |
| 185 | return err; |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 186 | } |
| 187 | #else |
| 188 | static inline int splash_init_usb(void) |
| 189 | { |
| 190 | printf("Cannot load splash image: no USB support\n"); |
| 191 | return -ENOSYS; |
| 192 | } |
| 193 | #endif |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 194 | |
Simon Glass | ab3055a | 2017-06-14 21:28:25 -0600 | [diff] [blame] | 195 | #ifdef CONFIG_SATA |
Nikita Kiryanov | 80ca30d | 2015-10-29 11:54:43 +0200 | [diff] [blame] | 196 | static int splash_init_sata(void) |
| 197 | { |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 198 | return sata_probe(0); |
Nikita Kiryanov | 80ca30d | 2015-10-29 11:54:43 +0200 | [diff] [blame] | 199 | } |
| 200 | #else |
| 201 | static inline int splash_init_sata(void) |
| 202 | { |
| 203 | printf("Cannot load splash image: no SATA support\n"); |
| 204 | return -ENOSYS; |
| 205 | } |
| 206 | #endif |
| 207 | |
Simon Glass | 61f0958 | 2021-11-19 13:24:05 -0700 | [diff] [blame] | 208 | static int splash_init_virtio(void) |
| 209 | { |
| 210 | if (!IS_ENABLED(CONFIG_VIRTIO)) { |
| 211 | printf("Cannot load splash image: no virtio support\n"); |
| 212 | return -ENOSYS; |
| 213 | } else { |
| 214 | return virtio_init(); |
| 215 | } |
| 216 | } |
| 217 | |
Devarsh Thakkar | 699afcb | 2024-01-24 14:35:09 +0530 | [diff] [blame] | 218 | #if defined(CONFIG_CMD_UBIFS) && !defined(CONFIG_SPL_BUILD) |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 219 | static int splash_mount_ubifs(struct splash_location *location) |
| 220 | { |
| 221 | int res; |
| 222 | char cmd[32]; |
| 223 | |
| 224 | sprintf(cmd, "ubi part %s", location->mtdpart); |
| 225 | res = run_command(cmd, 0); |
| 226 | if (res) |
| 227 | return res; |
| 228 | |
| 229 | sprintf(cmd, "ubifsmount %s", location->ubivol); |
| 230 | res = run_command(cmd, 0); |
| 231 | |
| 232 | return res; |
| 233 | } |
| 234 | |
| 235 | static inline int splash_umount_ubifs(void) |
| 236 | { |
| 237 | return run_command("ubifsumount", 0); |
| 238 | } |
| 239 | #else |
| 240 | static inline int splash_mount_ubifs(struct splash_location *location) |
| 241 | { |
| 242 | printf("Cannot load splash image: no UBIFS support\n"); |
| 243 | return -ENOSYS; |
| 244 | } |
| 245 | |
| 246 | static inline int splash_umount_ubifs(void) |
| 247 | { |
| 248 | printf("Cannot unmount UBIFS: no UBIFS support\n"); |
| 249 | return -ENOSYS; |
| 250 | } |
| 251 | #endif |
| 252 | |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 253 | #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp" |
| 254 | |
| 255 | static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr) |
| 256 | { |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 257 | int res = 0; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 258 | loff_t bmp_size; |
Jonathan Golder | 26b6131 | 2017-02-24 17:46:10 +0100 | [diff] [blame] | 259 | loff_t actread; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 260 | char *splash_file; |
| 261 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 262 | splash_file = env_get("splashfile"); |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 263 | if (!splash_file) |
| 264 | splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME; |
| 265 | |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 266 | if (location->storage == SPLASH_STORAGE_USB) |
| 267 | res = splash_init_usb(); |
| 268 | |
Nikita Kiryanov | 80ca30d | 2015-10-29 11:54:43 +0200 | [diff] [blame] | 269 | if (location->storage == SPLASH_STORAGE_SATA) |
| 270 | res = splash_init_sata(); |
| 271 | |
Simon Glass | 61f0958 | 2021-11-19 13:24:05 -0700 | [diff] [blame] | 272 | if (location->storage == SPLASH_STORAGE_VIRTIO) |
| 273 | res = splash_init_virtio(); |
| 274 | |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 275 | if (location->ubivol != NULL) |
| 276 | res = splash_mount_ubifs(location); |
| 277 | |
Nikita Kiryanov | bf5c3cc | 2015-10-29 11:54:42 +0200 | [diff] [blame] | 278 | if (res) |
| 279 | return res; |
| 280 | |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 281 | res = splash_select_fs_dev(location); |
| 282 | if (res) |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 283 | goto out; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 284 | |
| 285 | res = fs_size(splash_file, &bmp_size); |
| 286 | if (res) { |
| 287 | printf("Error (%d): cannot determine file size\n", res); |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 288 | goto out; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (bmp_load_addr + bmp_size >= gd->start_addr_sp) { |
| 292 | printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 293 | res = -EFAULT; |
| 294 | goto out; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | splash_select_fs_dev(location); |
Jonathan Golder | 26b6131 | 2017-02-24 17:46:10 +0100 | [diff] [blame] | 298 | res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread); |
Eran Matityahu | 07a04a1 | 2016-06-07 10:38:37 +0300 | [diff] [blame] | 299 | |
| 300 | out: |
| 301 | if (location->ubivol != NULL) |
| 302 | splash_umount_ubifs(); |
| 303 | |
| 304 | return res; |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 307 | /** |
| 308 | * select_splash_location - return the splash location based on board support |
| 309 | * and env variable "splashsource". |
| 310 | * |
| 311 | * @locations: An array of supported splash locations. |
| 312 | * @size: Size of splash_locations array. |
| 313 | * |
| 314 | * @return: If a null set of splash locations is given, or |
| 315 | * splashsource env variable is set to unsupported value |
| 316 | * return NULL. |
| 317 | * If splashsource env variable is not defined |
| 318 | * return the first entry in splash_locations as default. |
| 319 | * If splashsource env variable contains a supported value |
| 320 | * return the location selected by splashsource. |
| 321 | */ |
| 322 | static struct splash_location *select_splash_location( |
| 323 | struct splash_location *locations, uint size) |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 324 | { |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 325 | int i; |
| 326 | char *env_splashsource; |
| 327 | |
| 328 | if (!locations || size == 0) |
| 329 | return NULL; |
| 330 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 331 | env_splashsource = env_get("splashsource"); |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 332 | if (env_splashsource == NULL) |
| 333 | return &locations[0]; |
| 334 | |
| 335 | for (i = 0; i < size; i++) { |
| 336 | if (!strcmp(locations[i].name, env_splashsource)) |
| 337 | return &locations[i]; |
| 338 | } |
| 339 | |
| 340 | printf("splashsource env variable set to unsupported value\n"); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 344 | #ifdef CONFIG_FIT |
| 345 | static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr) |
| 346 | { |
| 347 | int res; |
| 348 | int node_offset; |
Leo Ruan | e9b74d9 | 2019-02-08 10:51:35 +0100 | [diff] [blame] | 349 | const char *splash_file; |
Leo Ruan | efc20c6 | 2019-02-08 10:51:36 +0100 | [diff] [blame] | 350 | const void *internal_splash_data; |
| 351 | size_t internal_splash_size; |
| 352 | int external_splash_addr; |
| 353 | int external_splash_size; |
| 354 | bool is_splash_external = false; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 355 | struct legacy_img_hdr *img_header; |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 356 | const u32 *fit_header; |
| 357 | u32 fit_size; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 358 | const size_t header_size = sizeof(struct legacy_img_hdr); |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 359 | |
| 360 | /* Read in image header */ |
| 361 | res = splash_storage_read_raw(location, bmp_load_addr, header_size); |
| 362 | if (res < 0) |
| 363 | return res; |
| 364 | |
Nikhil M Jain | 9298132 | 2023-06-21 16:29:53 +0530 | [diff] [blame] | 365 | img_header = (struct legacy_img_hdr *)(uintptr_t)bmp_load_addr; |
Niko Mauno | 7e9e0b5 | 2017-08-03 09:53:24 +0300 | [diff] [blame] | 366 | if (image_get_magic(img_header) != FDT_MAGIC) { |
| 367 | printf("Could not find FDT magic\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 371 | fit_size = fdt_totalsize(img_header); |
| 372 | |
| 373 | /* Read in entire FIT */ |
| 374 | fit_header = (const u32 *)(bmp_load_addr + header_size); |
Nikhil M Jain | 9298132 | 2023-06-21 16:29:53 +0530 | [diff] [blame] | 375 | res = splash_storage_read_raw(location, (uintptr_t)fit_header, fit_size); |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 376 | if (res < 0) |
| 377 | return res; |
| 378 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 379 | res = fit_check_format(fit_header, IMAGE_SIZE_INVAL); |
| 380 | if (res) { |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 381 | debug("Could not find valid FIT image\n"); |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 382 | return res; |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Leo Ruan | e9b74d9 | 2019-02-08 10:51:35 +0100 | [diff] [blame] | 385 | /* Get the splash image node */ |
| 386 | splash_file = env_get("splashfile"); |
| 387 | if (!splash_file) |
| 388 | splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME; |
| 389 | |
| 390 | node_offset = fit_image_get_node(fit_header, splash_file); |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 391 | if (node_offset < 0) { |
| 392 | debug("Could not find splash image '%s' in FIT\n", |
Leo Ruan | e9b74d9 | 2019-02-08 10:51:35 +0100 | [diff] [blame] | 393 | splash_file); |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 394 | return -ENOENT; |
| 395 | } |
| 396 | |
Leo Ruan | efc20c6 | 2019-02-08 10:51:36 +0100 | [diff] [blame] | 397 | /* Extract the splash data from FIT */ |
| 398 | /* 1. Test if splash is in FIT internal data. */ |
| 399 | if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size)) |
Nikhil M Jain | 9298132 | 2023-06-21 16:29:53 +0530 | [diff] [blame] | 400 | memmove((void *)(uintptr_t)bmp_load_addr, internal_splash_data, internal_splash_size); |
Leo Ruan | efc20c6 | 2019-02-08 10:51:36 +0100 | [diff] [blame] | 401 | /* 2. Test if splash is in FIT external data with fixed position. */ |
| 402 | else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr)) |
| 403 | is_splash_external = true; |
| 404 | /* 3. Test if splash is in FIT external data with offset. */ |
| 405 | else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) { |
| 406 | /* Align data offset to 4-byte boundary */ |
| 407 | fit_size = ALIGN(fdt_totalsize(fit_header), 4); |
| 408 | /* External splash offset means the offset by end of FIT header */ |
| 409 | external_splash_addr += location->offset + fit_size; |
| 410 | is_splash_external = true; |
| 411 | } else { |
| 412 | printf("Failed to get splash image from FIT\n"); |
| 413 | return -ENODATA; |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Leo Ruan | efc20c6 | 2019-02-08 10:51:36 +0100 | [diff] [blame] | 416 | if (is_splash_external) { |
| 417 | res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size); |
| 418 | if (res < 0) { |
| 419 | printf("Failed to get size of splash image (err=%d)\n", res); |
| 420 | return res; |
| 421 | } |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 422 | |
Leo Ruan | efc20c6 | 2019-02-08 10:51:36 +0100 | [diff] [blame] | 423 | /* Read in the splash data */ |
| 424 | location->offset = external_splash_addr; |
| 425 | res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size); |
| 426 | if (res < 0) |
| 427 | return res; |
| 428 | } |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | #endif /* CONFIG_FIT */ |
| 433 | |
Nikita Kiryanov | 7f9ceea | 2015-01-14 10:42:54 +0200 | [diff] [blame] | 434 | /** |
| 435 | * splash_source_load - load splash image from a supported location. |
| 436 | * |
| 437 | * Select a splash image location based on the value of splashsource environment |
| 438 | * variable and the board supported splash source locations, and load a |
| 439 | * splashimage to the address pointed to by splashimage environment variable. |
| 440 | * |
| 441 | * @locations: An array of supported splash locations. |
| 442 | * @size: Size of splash_locations array. |
| 443 | * |
| 444 | * @return: 0 on success, negative value on failure. |
| 445 | */ |
| 446 | int splash_source_load(struct splash_location *locations, uint size) |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 447 | { |
| 448 | struct splash_location *splash_location; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 449 | char *env_splashimage_value; |
Julien Masson | 0cdb0aa | 2022-10-17 10:33:22 +0200 | [diff] [blame] | 450 | char *devpart; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 451 | u32 bmp_load_addr; |
| 452 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 453 | env_splashimage_value = env_get("splashimage"); |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 454 | if (env_splashimage_value == NULL) |
Nikita Kiryanov | f58c509 | 2015-01-14 10:42:49 +0200 | [diff] [blame] | 455 | return -ENOENT; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 456 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 457 | bmp_load_addr = hextoul(env_splashimage_value, 0); |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 458 | if (bmp_load_addr == 0) { |
| 459 | printf("Error: bad splashimage address specified\n"); |
Nikita Kiryanov | f58c509 | 2015-01-14 10:42:49 +0200 | [diff] [blame] | 460 | return -EFAULT; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Nikita Kiryanov | c2a07e3 | 2015-01-14 10:42:51 +0200 | [diff] [blame] | 463 | splash_location = select_splash_location(locations, size); |
| 464 | if (!splash_location) |
| 465 | return -EINVAL; |
| 466 | |
Julien Masson | 0cdb0aa | 2022-10-17 10:33:22 +0200 | [diff] [blame] | 467 | devpart = env_get("splashdevpart"); |
| 468 | if (devpart) |
| 469 | splash_location->devpart = devpart; |
| 470 | |
tomas.melin@vaisala.com | f4650ea | 2016-11-16 13:02:32 +0200 | [diff] [blame] | 471 | if (splash_location->flags == SPLASH_STORAGE_RAW) |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 472 | return splash_load_raw(splash_location, bmp_load_addr); |
tomas.melin@vaisala.com | f4650ea | 2016-11-16 13:02:32 +0200 | [diff] [blame] | 473 | else if (splash_location->flags == SPLASH_STORAGE_FS) |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 474 | return splash_load_fs(splash_location, bmp_load_addr); |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 475 | #ifdef CONFIG_FIT |
| 476 | else if (splash_location->flags == SPLASH_STORAGE_FIT) |
| 477 | return splash_load_fit(splash_location, bmp_load_addr); |
| 478 | #endif |
Nikita Kiryanov | 7428271 | 2015-10-29 11:54:41 +0200 | [diff] [blame] | 479 | return -EINVAL; |
Igor Grinberg | 86ec16b | 2014-11-03 11:32:20 +0200 | [diff] [blame] | 480 | } |