1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
8 #include <bmp_layout.h>
13 #include <fdt_support.h>
19 #include <spi_flash.h>
23 #include <asm/global_data.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifdef CONFIG_SPI_FLASH
28 static struct spi_flash *sf;
29 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
32 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
34 CONFIG_SF_DEFAULT_SPEED,
35 CONFIG_SF_DEFAULT_MODE);
40 return spi_flash_read(sf, offset, read_size, (void *)(uintptr_t)bmp_load_addr);
43 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
45 debug("%s: sf support not available\n", __func__);
50 #ifdef CONFIG_CMD_NAND
51 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
53 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
54 return nand_read_skip_bad(mtd, offset,
57 (u_char *)bmp_load_addr);
60 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
62 debug("%s: nand support not available\n", __func__);
67 static int splash_mmc_read_raw(u32 bmp_load_addr, struct splash_location *location,
70 struct disk_partition partition;
71 struct blk_desc *desc;
75 if (!IS_ENABLED(CONFIG_CMD_MMC)) {
76 debug("%s: mmc support not available\n", __func__);
80 ret = part_get_info_by_dev_and_name_or_num("mmc", location->devpart, &desc,
85 blkcnt = DIV_ROUND_UP(read_size, partition.blksz);
86 n = blk_dread(desc, partition.start, blkcnt, (void *)(uintptr_t)bmp_load_addr);
88 return (n == blkcnt) ? 0 : -EIO;
91 static int splash_storage_read_raw(struct splash_location *location,
92 u32 bmp_load_addr, size_t read_size)
99 offset = location->offset;
100 switch (location->storage) {
101 case SPLASH_STORAGE_MMC:
102 return splash_mmc_read_raw(bmp_load_addr, location, read_size);
103 case SPLASH_STORAGE_NAND:
104 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
105 case SPLASH_STORAGE_SF:
106 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
108 printf("Unknown splash location\n");
114 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
116 struct bmp_header *bmp_hdr;
118 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
120 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
121 goto splash_address_too_high;
123 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
127 bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr;
128 bmp_size = le32_to_cpu(bmp_hdr->file_size);
130 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
131 goto splash_address_too_high;
133 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
135 splash_address_too_high:
136 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
141 static int splash_select_fs_dev(struct splash_location *location)
145 switch (location->storage) {
146 case SPLASH_STORAGE_MMC:
147 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
149 case SPLASH_STORAGE_USB:
150 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
152 case SPLASH_STORAGE_SATA:
153 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
155 case SPLASH_STORAGE_NAND:
156 if (location->ubivol != NULL)
157 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
162 printf("Error: unsupported location storage.\n");
167 printf("Error: could not access storage.\n");
172 #ifdef CONFIG_USB_STORAGE
173 static int splash_init_usb(void)
181 #ifndef CONFIG_DM_USB
182 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
188 static inline int splash_init_usb(void)
190 printf("Cannot load splash image: no USB support\n");
196 static int splash_init_sata(void)
198 return sata_probe(0);
201 static inline int splash_init_sata(void)
203 printf("Cannot load splash image: no SATA support\n");
208 static int splash_init_virtio(void)
210 if (!IS_ENABLED(CONFIG_VIRTIO)) {
211 printf("Cannot load splash image: no virtio support\n");
214 return virtio_init();
218 #if defined(CONFIG_CMD_UBIFS) && !defined(CONFIG_XPL_BUILD)
219 static int splash_mount_ubifs(struct splash_location *location)
224 sprintf(cmd, "ubi part %s", location->mtdpart);
225 res = run_command(cmd, 0);
229 sprintf(cmd, "ubifsmount %s", location->ubivol);
230 res = run_command(cmd, 0);
235 static inline int splash_umount_ubifs(void)
237 return run_command("ubifsumount", 0);
240 static inline int splash_mount_ubifs(struct splash_location *location)
242 printf("Cannot load splash image: no UBIFS support\n");
246 static inline int splash_umount_ubifs(void)
248 printf("Cannot unmount UBIFS: no UBIFS support\n");
253 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
255 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
262 splash_file = env_get("splashfile");
264 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
266 if (location->storage == SPLASH_STORAGE_USB)
267 res = splash_init_usb();
269 if (location->storage == SPLASH_STORAGE_SATA)
270 res = splash_init_sata();
272 if (location->storage == SPLASH_STORAGE_VIRTIO)
273 res = splash_init_virtio();
275 if (location->ubivol != NULL)
276 res = splash_mount_ubifs(location);
281 res = splash_select_fs_dev(location);
285 res = fs_size(splash_file, &bmp_size);
287 printf("Error (%d): cannot determine file size\n", res);
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");
297 splash_select_fs_dev(location);
298 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
301 if (location->ubivol != NULL)
302 splash_umount_ubifs();
308 * select_splash_location - return the splash location based on board support
309 * and env variable "splashsource".
311 * @locations: An array of supported splash locations.
312 * @size: Size of splash_locations array.
314 * @return: If a null set of splash locations is given, or
315 * splashsource env variable is set to unsupported value
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.
322 static struct splash_location *select_splash_location(
323 struct splash_location *locations, uint size)
326 char *env_splashsource;
328 if (!locations || size == 0)
331 env_splashsource = env_get("splashsource");
332 if (env_splashsource == NULL)
333 return &locations[0];
335 for (i = 0; i < size; i++) {
336 if (!strcmp(locations[i].name, env_splashsource))
337 return &locations[i];
340 printf("splashsource env variable set to unsupported value\n");
345 static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
349 const char *splash_file;
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;
355 struct legacy_img_hdr *img_header;
356 const u32 *fit_header;
358 const size_t header_size = sizeof(struct legacy_img_hdr);
360 /* Read in image header */
361 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
365 img_header = (struct legacy_img_hdr *)(uintptr_t)bmp_load_addr;
366 if (image_get_magic(img_header) != FDT_MAGIC) {
367 printf("Could not find FDT magic\n");
371 fit_size = fdt_totalsize(img_header);
373 /* Read in entire FIT */
374 fit_header = (const u32 *)(bmp_load_addr + header_size);
375 res = splash_storage_read_raw(location, (uintptr_t)fit_header, fit_size);
379 res = fit_check_format(fit_header, IMAGE_SIZE_INVAL);
381 debug("Could not find valid FIT image\n");
385 /* Get the splash image node */
386 splash_file = env_get("splashfile");
388 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
390 node_offset = fit_image_get_node(fit_header, splash_file);
391 if (node_offset < 0) {
392 debug("Could not find splash image '%s' in FIT\n",
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))
400 memmove((void *)(uintptr_t)bmp_load_addr, internal_splash_data, internal_splash_size);
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;
412 printf("Failed to get splash image from FIT\n");
416 if (is_splash_external) {
417 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
419 printf("Failed to get size of splash image (err=%d)\n", res);
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);
432 #endif /* CONFIG_FIT */
435 * splash_source_load - load splash image from a supported location.
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.
441 * @locations: An array of supported splash locations.
442 * @size: Size of splash_locations array.
444 * @return: 0 on success, negative value on failure.
446 int splash_source_load(struct splash_location *locations, uint size)
448 struct splash_location *splash_location;
449 char *env_splashimage_value;
453 env_splashimage_value = env_get("splashimage");
454 if (env_splashimage_value == NULL)
457 bmp_load_addr = hextoul(env_splashimage_value, 0);
458 if (bmp_load_addr == 0) {
459 printf("Error: bad splashimage address specified\n");
463 splash_location = select_splash_location(locations, size);
464 if (!splash_location)
467 devpart = env_get("splashdevpart");
469 splash_location->devpart = devpart;
471 if (splash_location->flags == SPLASH_STORAGE_RAW)
472 return splash_load_raw(splash_location, bmp_load_addr);
473 else if (splash_location->flags == SPLASH_STORAGE_FS)
474 return splash_load_fs(splash_location, bmp_load_addr);
476 else if (splash_location->flags == SPLASH_STORAGE_FIT)
477 return splash_load_fit(splash_location, bmp_load_addr);