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