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>
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_storage_read_raw(struct splash_location *location,
68 u32 bmp_load_addr, size_t read_size)
75 offset = location->offset;
76 switch (location->storage) {
77 case SPLASH_STORAGE_NAND:
78 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
79 case SPLASH_STORAGE_SF:
80 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
82 printf("Unknown splash location\n");
88 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
90 struct bmp_header *bmp_hdr;
92 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
94 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
95 goto splash_address_too_high;
97 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
101 bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr;
102 bmp_size = le32_to_cpu(bmp_hdr->file_size);
104 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
105 goto splash_address_too_high;
107 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
109 splash_address_too_high:
110 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
115 static int splash_select_fs_dev(struct splash_location *location)
119 switch (location->storage) {
120 case SPLASH_STORAGE_MMC:
121 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
123 case SPLASH_STORAGE_USB:
124 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
126 case SPLASH_STORAGE_SATA:
127 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
129 case SPLASH_STORAGE_NAND:
130 if (location->ubivol != NULL)
131 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
136 printf("Error: unsupported location storage.\n");
141 printf("Error: could not access storage.\n");
146 #ifdef CONFIG_USB_STORAGE
147 static int splash_init_usb(void)
155 #ifndef CONFIG_DM_USB
156 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
162 static inline int splash_init_usb(void)
164 printf("Cannot load splash image: no USB support\n");
170 static int splash_init_sata(void)
172 return sata_probe(0);
175 static inline int splash_init_sata(void)
177 printf("Cannot load splash image: no SATA support\n");
182 #ifdef CONFIG_CMD_UBIFS
183 static int splash_mount_ubifs(struct splash_location *location)
188 sprintf(cmd, "ubi part %s", location->mtdpart);
189 res = run_command(cmd, 0);
193 sprintf(cmd, "ubifsmount %s", location->ubivol);
194 res = run_command(cmd, 0);
199 static inline int splash_umount_ubifs(void)
201 return run_command("ubifsumount", 0);
204 static inline int splash_mount_ubifs(struct splash_location *location)
206 printf("Cannot load splash image: no UBIFS support\n");
210 static inline int splash_umount_ubifs(void)
212 printf("Cannot unmount UBIFS: no UBIFS support\n");
217 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
219 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
226 splash_file = env_get("splashfile");
228 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
230 if (location->storage == SPLASH_STORAGE_USB)
231 res = splash_init_usb();
233 if (location->storage == SPLASH_STORAGE_SATA)
234 res = splash_init_sata();
236 if (location->ubivol != NULL)
237 res = splash_mount_ubifs(location);
242 res = splash_select_fs_dev(location);
246 res = fs_size(splash_file, &bmp_size);
248 printf("Error (%d): cannot determine file size\n", res);
252 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
253 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
258 splash_select_fs_dev(location);
259 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
262 if (location->ubivol != NULL)
263 splash_umount_ubifs();
269 * select_splash_location - return the splash location based on board support
270 * and env variable "splashsource".
272 * @locations: An array of supported splash locations.
273 * @size: Size of splash_locations array.
275 * @return: If a null set of splash locations is given, or
276 * splashsource env variable is set to unsupported value
278 * If splashsource env variable is not defined
279 * return the first entry in splash_locations as default.
280 * If splashsource env variable contains a supported value
281 * return the location selected by splashsource.
283 static struct splash_location *select_splash_location(
284 struct splash_location *locations, uint size)
287 char *env_splashsource;
289 if (!locations || size == 0)
292 env_splashsource = env_get("splashsource");
293 if (env_splashsource == NULL)
294 return &locations[0];
296 for (i = 0; i < size; i++) {
297 if (!strcmp(locations[i].name, env_splashsource))
298 return &locations[i];
301 printf("splashsource env variable set to unsupported value\n");
306 static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
310 const char *splash_file;
311 const void *internal_splash_data;
312 size_t internal_splash_size;
313 int external_splash_addr;
314 int external_splash_size;
315 bool is_splash_external = false;
316 struct image_header *img_header;
317 const u32 *fit_header;
319 const size_t header_size = sizeof(struct image_header);
321 /* Read in image header */
322 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
326 img_header = (struct image_header *)bmp_load_addr;
327 if (image_get_magic(img_header) != FDT_MAGIC) {
328 printf("Could not find FDT magic\n");
332 fit_size = fdt_totalsize(img_header);
334 /* Read in entire FIT */
335 fit_header = (const u32 *)(bmp_load_addr + header_size);
336 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
340 res = fit_check_format(fit_header, IMAGE_SIZE_INVAL);
342 debug("Could not find valid FIT image\n");
346 /* Get the splash image node */
347 splash_file = env_get("splashfile");
349 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
351 node_offset = fit_image_get_node(fit_header, splash_file);
352 if (node_offset < 0) {
353 debug("Could not find splash image '%s' in FIT\n",
358 /* Extract the splash data from FIT */
359 /* 1. Test if splash is in FIT internal data. */
360 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
361 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
362 /* 2. Test if splash is in FIT external data with fixed position. */
363 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
364 is_splash_external = true;
365 /* 3. Test if splash is in FIT external data with offset. */
366 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
367 /* Align data offset to 4-byte boundary */
368 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
369 /* External splash offset means the offset by end of FIT header */
370 external_splash_addr += location->offset + fit_size;
371 is_splash_external = true;
373 printf("Failed to get splash image from FIT\n");
377 if (is_splash_external) {
378 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
380 printf("Failed to get size of splash image (err=%d)\n", res);
384 /* Read in the splash data */
385 location->offset = external_splash_addr;
386 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
393 #endif /* CONFIG_FIT */
396 * splash_source_load - load splash image from a supported location.
398 * Select a splash image location based on the value of splashsource environment
399 * variable and the board supported splash source locations, and load a
400 * splashimage to the address pointed to by splashimage environment variable.
402 * @locations: An array of supported splash locations.
403 * @size: Size of splash_locations array.
405 * @return: 0 on success, negative value on failure.
407 int splash_source_load(struct splash_location *locations, uint size)
409 struct splash_location *splash_location;
410 char *env_splashimage_value;
413 env_splashimage_value = env_get("splashimage");
414 if (env_splashimage_value == NULL)
417 bmp_load_addr = hextoul(env_splashimage_value, 0);
418 if (bmp_load_addr == 0) {
419 printf("Error: bad splashimage address specified\n");
423 splash_location = select_splash_location(locations, size);
424 if (!splash_location)
427 if (splash_location->flags == SPLASH_STORAGE_RAW)
428 return splash_load_raw(splash_location, bmp_load_addr);
429 else if (splash_location->flags == SPLASH_STORAGE_FS)
430 return splash_load_fs(splash_location, bmp_load_addr);
432 else if (splash_location->flags == SPLASH_STORAGE_FIT)
433 return splash_load_fit(splash_location, bmp_load_addr);