1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
5 * Authors: Igor Grinberg <grinberg@compulab.co.il>
9 #include <bmp_layout.h>
14 #include <fdt_support.h>
20 #include <spi_flash.h>
24 #include <asm/global_data.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CONFIG_SPI_FLASH
29 static struct spi_flash *sf;
30 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
33 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
35 CONFIG_SF_DEFAULT_SPEED,
36 CONFIG_SF_DEFAULT_MODE);
41 return spi_flash_read(sf, offset, read_size, (void *)(uintptr_t)bmp_load_addr);
44 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
46 debug("%s: sf support not available\n", __func__);
51 #ifdef CONFIG_CMD_NAND
52 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
54 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
55 return nand_read_skip_bad(mtd, offset,
58 (u_char *)bmp_load_addr);
61 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
63 debug("%s: nand support not available\n", __func__);
68 static int splash_mmc_read_raw(u32 bmp_load_addr, struct splash_location *location,
71 struct disk_partition partition;
72 struct blk_desc *desc;
76 if (!IS_ENABLED(CONFIG_CMD_MMC)) {
77 debug("%s: mmc support not available\n", __func__);
81 ret = part_get_info_by_dev_and_name_or_num("mmc", location->devpart, &desc,
86 blkcnt = DIV_ROUND_UP(read_size, partition.blksz);
87 n = blk_dread(desc, partition.start, blkcnt, (void *)(uintptr_t)bmp_load_addr);
89 return (n == blkcnt) ? 0 : -EIO;
92 static int splash_storage_read_raw(struct splash_location *location,
93 u32 bmp_load_addr, size_t read_size)
100 offset = location->offset;
101 switch (location->storage) {
102 case SPLASH_STORAGE_MMC:
103 return splash_mmc_read_raw(bmp_load_addr, location, read_size);
104 case SPLASH_STORAGE_NAND:
105 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
106 case SPLASH_STORAGE_SF:
107 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
109 printf("Unknown splash location\n");
115 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
117 struct bmp_header *bmp_hdr;
119 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
121 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
122 goto splash_address_too_high;
124 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
128 bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr;
129 bmp_size = le32_to_cpu(bmp_hdr->file_size);
131 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
132 goto splash_address_too_high;
134 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
136 splash_address_too_high:
137 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
142 static int splash_select_fs_dev(struct splash_location *location)
146 switch (location->storage) {
147 case SPLASH_STORAGE_MMC:
148 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
150 case SPLASH_STORAGE_USB:
151 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
153 case SPLASH_STORAGE_SATA:
154 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
156 case SPLASH_STORAGE_NAND:
157 if (location->ubivol != NULL)
158 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
163 printf("Error: unsupported location storage.\n");
168 printf("Error: could not access storage.\n");
173 #ifdef CONFIG_USB_STORAGE
174 static int splash_init_usb(void)
182 #ifndef CONFIG_DM_USB
183 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
189 static inline int splash_init_usb(void)
191 printf("Cannot load splash image: no USB support\n");
197 static int splash_init_sata(void)
199 return sata_probe(0);
202 static inline int splash_init_sata(void)
204 printf("Cannot load splash image: no SATA support\n");
209 static int splash_init_virtio(void)
211 if (!IS_ENABLED(CONFIG_VIRTIO)) {
212 printf("Cannot load splash image: no virtio support\n");
215 return virtio_init();
219 #ifdef CONFIG_CMD_UBIFS
220 static int splash_mount_ubifs(struct splash_location *location)
225 sprintf(cmd, "ubi part %s", location->mtdpart);
226 res = run_command(cmd, 0);
230 sprintf(cmd, "ubifsmount %s", location->ubivol);
231 res = run_command(cmd, 0);
236 static inline int splash_umount_ubifs(void)
238 return run_command("ubifsumount", 0);
241 static inline int splash_mount_ubifs(struct splash_location *location)
243 printf("Cannot load splash image: no UBIFS support\n");
247 static inline int splash_umount_ubifs(void)
249 printf("Cannot unmount UBIFS: no UBIFS support\n");
254 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
256 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
263 splash_file = env_get("splashfile");
265 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
267 if (location->storage == SPLASH_STORAGE_USB)
268 res = splash_init_usb();
270 if (location->storage == SPLASH_STORAGE_SATA)
271 res = splash_init_sata();
273 if (location->storage == SPLASH_STORAGE_VIRTIO)
274 res = splash_init_virtio();
276 if (location->ubivol != NULL)
277 res = splash_mount_ubifs(location);
282 res = splash_select_fs_dev(location);
286 res = fs_size(splash_file, &bmp_size);
288 printf("Error (%d): cannot determine file size\n", res);
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");
298 splash_select_fs_dev(location);
299 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
302 if (location->ubivol != NULL)
303 splash_umount_ubifs();
309 * select_splash_location - return the splash location based on board support
310 * and env variable "splashsource".
312 * @locations: An array of supported splash locations.
313 * @size: Size of splash_locations array.
315 * @return: If a null set of splash locations is given, or
316 * splashsource env variable is set to unsupported value
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.
323 static struct splash_location *select_splash_location(
324 struct splash_location *locations, uint size)
327 char *env_splashsource;
329 if (!locations || size == 0)
332 env_splashsource = env_get("splashsource");
333 if (env_splashsource == NULL)
334 return &locations[0];
336 for (i = 0; i < size; i++) {
337 if (!strcmp(locations[i].name, env_splashsource))
338 return &locations[i];
341 printf("splashsource env variable set to unsupported value\n");
346 static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
350 const char *splash_file;
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;
356 struct legacy_img_hdr *img_header;
357 const u32 *fit_header;
359 const size_t header_size = sizeof(struct legacy_img_hdr);
361 /* Read in image header */
362 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
366 img_header = (struct legacy_img_hdr *)bmp_load_addr;
367 if (image_get_magic(img_header) != FDT_MAGIC) {
368 printf("Could not find FDT magic\n");
372 fit_size = fdt_totalsize(img_header);
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);
380 res = fit_check_format(fit_header, IMAGE_SIZE_INVAL);
382 debug("Could not find valid FIT image\n");
386 /* Get the splash image node */
387 splash_file = env_get("splashfile");
389 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
391 node_offset = fit_image_get_node(fit_header, splash_file);
392 if (node_offset < 0) {
393 debug("Could not find splash image '%s' in FIT\n",
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;
413 printf("Failed to get splash image from FIT\n");
417 if (is_splash_external) {
418 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
420 printf("Failed to get size of splash image (err=%d)\n", res);
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);
433 #endif /* CONFIG_FIT */
436 * splash_source_load - load splash image from a supported location.
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.
442 * @locations: An array of supported splash locations.
443 * @size: Size of splash_locations array.
445 * @return: 0 on success, negative value on failure.
447 int splash_source_load(struct splash_location *locations, uint size)
449 struct splash_location *splash_location;
450 char *env_splashimage_value;
454 env_splashimage_value = env_get("splashimage");
455 if (env_splashimage_value == NULL)
458 bmp_load_addr = hextoul(env_splashimage_value, 0);
459 if (bmp_load_addr == 0) {
460 printf("Error: bad splashimage address specified\n");
464 splash_location = select_splash_location(locations, size);
465 if (!splash_location)
468 devpart = env_get("splashdevpart");
470 splash_location->devpart = devpart;
472 if (splash_location->flags == SPLASH_STORAGE_RAW)
473 return splash_load_raw(splash_location, bmp_load_addr);
474 else if (splash_location->flags == SPLASH_STORAGE_FS)
475 return splash_load_fs(splash_location, bmp_load_addr);
477 else if (splash_location->flags == SPLASH_STORAGE_FIT)
478 return splash_load_fit(splash_location, bmp_load_addr);