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