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 DECLARE_GLOBAL_DATA_PTR;
26 #ifdef CONFIG_SPI_FLASH
27 static struct spi_flash *sf;
28 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
31 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
33 CONFIG_SF_DEFAULT_SPEED,
34 CONFIG_SF_DEFAULT_MODE);
39 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
42 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
44 debug("%s: sf support not available\n", __func__);
49 #ifdef CONFIG_CMD_NAND
50 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
52 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
53 return nand_read_skip_bad(mtd, offset,
56 (u_char *)bmp_load_addr);
59 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
61 debug("%s: nand support not available\n", __func__);
66 static int splash_storage_read_raw(struct splash_location *location,
67 u32 bmp_load_addr, size_t read_size)
74 offset = location->offset;
75 switch (location->storage) {
76 case SPLASH_STORAGE_NAND:
77 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
78 case SPLASH_STORAGE_SF:
79 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
81 printf("Unknown splash location\n");
87 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
89 struct bmp_header *bmp_hdr;
91 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
93 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
94 goto splash_address_too_high;
96 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
100 bmp_hdr = (struct bmp_header *)bmp_load_addr;
101 bmp_size = le32_to_cpu(bmp_hdr->file_size);
103 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
104 goto splash_address_too_high;
106 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
108 splash_address_too_high:
109 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
114 static int splash_select_fs_dev(struct splash_location *location)
118 switch (location->storage) {
119 case SPLASH_STORAGE_MMC:
120 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
122 case SPLASH_STORAGE_USB:
123 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
125 case SPLASH_STORAGE_SATA:
126 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
128 case SPLASH_STORAGE_NAND:
129 if (location->ubivol != NULL)
130 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
135 printf("Error: unsupported location storage.\n");
140 printf("Error: could not access storage.\n");
145 #ifdef CONFIG_USB_STORAGE
146 static int splash_init_usb(void)
154 #ifndef CONFIG_DM_USB
155 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
161 static inline int splash_init_usb(void)
163 printf("Cannot load splash image: no USB support\n");
169 static int splash_init_sata(void)
171 return sata_probe(0);
174 static inline int splash_init_sata(void)
176 printf("Cannot load splash image: no SATA support\n");
181 #ifdef CONFIG_CMD_UBIFS
182 static int splash_mount_ubifs(struct splash_location *location)
187 sprintf(cmd, "ubi part %s", location->mtdpart);
188 res = run_command(cmd, 0);
192 sprintf(cmd, "ubifsmount %s", location->ubivol);
193 res = run_command(cmd, 0);
198 static inline int splash_umount_ubifs(void)
200 return run_command("ubifsumount", 0);
203 static inline int splash_mount_ubifs(struct splash_location *location)
205 printf("Cannot load splash image: no UBIFS support\n");
209 static inline int splash_umount_ubifs(void)
211 printf("Cannot unmount UBIFS: no UBIFS support\n");
216 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
218 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
225 splash_file = env_get("splashfile");
227 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
229 if (location->storage == SPLASH_STORAGE_USB)
230 res = splash_init_usb();
232 if (location->storage == SPLASH_STORAGE_SATA)
233 res = splash_init_sata();
235 if (location->ubivol != NULL)
236 res = splash_mount_ubifs(location);
241 res = splash_select_fs_dev(location);
245 res = fs_size(splash_file, &bmp_size);
247 printf("Error (%d): cannot determine file size\n", res);
251 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
252 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
257 splash_select_fs_dev(location);
258 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
261 if (location->ubivol != NULL)
262 splash_umount_ubifs();
268 * select_splash_location - return the splash location based on board support
269 * and env variable "splashsource".
271 * @locations: An array of supported splash locations.
272 * @size: Size of splash_locations array.
274 * @return: If a null set of splash locations is given, or
275 * splashsource env variable is set to unsupported value
277 * If splashsource env variable is not defined
278 * return the first entry in splash_locations as default.
279 * If splashsource env variable contains a supported value
280 * return the location selected by splashsource.
282 static struct splash_location *select_splash_location(
283 struct splash_location *locations, uint size)
286 char *env_splashsource;
288 if (!locations || size == 0)
291 env_splashsource = env_get("splashsource");
292 if (env_splashsource == NULL)
293 return &locations[0];
295 for (i = 0; i < size; i++) {
296 if (!strcmp(locations[i].name, env_splashsource))
297 return &locations[i];
300 printf("splashsource env variable set to unsupported value\n");
305 static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
309 const char *splash_file;
310 const void *internal_splash_data;
311 size_t internal_splash_size;
312 int external_splash_addr;
313 int external_splash_size;
314 bool is_splash_external = false;
315 struct image_header *img_header;
316 const u32 *fit_header;
318 const size_t header_size = sizeof(struct image_header);
320 /* Read in image header */
321 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
325 img_header = (struct image_header *)bmp_load_addr;
326 if (image_get_magic(img_header) != FDT_MAGIC) {
327 printf("Could not find FDT magic\n");
331 fit_size = fdt_totalsize(img_header);
333 /* Read in entire FIT */
334 fit_header = (const u32 *)(bmp_load_addr + header_size);
335 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
339 res = fit_check_format(fit_header);
341 debug("Could not find valid FIT image\n");
345 /* Get the splash image node */
346 splash_file = env_get("splashfile");
348 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
350 node_offset = fit_image_get_node(fit_header, splash_file);
351 if (node_offset < 0) {
352 debug("Could not find splash image '%s' in FIT\n",
357 /* Extract the splash data from FIT */
358 /* 1. Test if splash is in FIT internal data. */
359 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
360 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
361 /* 2. Test if splash is in FIT external data with fixed position. */
362 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
363 is_splash_external = true;
364 /* 3. Test if splash is in FIT external data with offset. */
365 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
366 /* Align data offset to 4-byte boundary */
367 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
368 /* External splash offset means the offset by end of FIT header */
369 external_splash_addr += location->offset + fit_size;
370 is_splash_external = true;
372 printf("Failed to get splash image from FIT\n");
376 if (is_splash_external) {
377 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
379 printf("Failed to get size of splash image (err=%d)\n", res);
383 /* Read in the splash data */
384 location->offset = external_splash_addr;
385 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
392 #endif /* CONFIG_FIT */
395 * splash_source_load - load splash image from a supported location.
397 * Select a splash image location based on the value of splashsource environment
398 * variable and the board supported splash source locations, and load a
399 * splashimage to the address pointed to by splashimage environment variable.
401 * @locations: An array of supported splash locations.
402 * @size: Size of splash_locations array.
404 * @return: 0 on success, negative value on failure.
406 int splash_source_load(struct splash_location *locations, uint size)
408 struct splash_location *splash_location;
409 char *env_splashimage_value;
412 env_splashimage_value = env_get("splashimage");
413 if (env_splashimage_value == NULL)
416 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
417 if (bmp_load_addr == 0) {
418 printf("Error: bad splashimage address specified\n");
422 splash_location = select_splash_location(locations, size);
423 if (!splash_location)
426 if (splash_location->flags == SPLASH_STORAGE_RAW)
427 return splash_load_raw(splash_location, bmp_load_addr);
428 else if (splash_location->flags == SPLASH_STORAGE_FS)
429 return splash_load_fs(splash_location, bmp_load_addr);
431 else if (splash_location->flags == SPLASH_STORAGE_FIT)
432 return splash_load_fit(splash_location, bmp_load_addr);