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>
12 #include <fdt_support.h>
17 #include <spi_flash.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #ifdef CONFIG_SPI_FLASH
24 static struct spi_flash *sf;
25 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
28 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
30 CONFIG_SF_DEFAULT_SPEED,
31 CONFIG_SF_DEFAULT_MODE);
36 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
39 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
41 debug("%s: sf support not available\n", __func__);
46 #ifdef CONFIG_CMD_NAND
47 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
49 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
50 return nand_read_skip_bad(mtd, offset,
53 (u_char *)bmp_load_addr);
56 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
58 debug("%s: nand support not available\n", __func__);
63 static int splash_storage_read_raw(struct splash_location *location,
64 u32 bmp_load_addr, size_t read_size)
71 offset = location->offset;
72 switch (location->storage) {
73 case SPLASH_STORAGE_NAND:
74 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
75 case SPLASH_STORAGE_SF:
76 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
78 printf("Unknown splash location\n");
84 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
86 struct bmp_header *bmp_hdr;
88 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
90 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
91 goto splash_address_too_high;
93 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
97 bmp_hdr = (struct bmp_header *)bmp_load_addr;
98 bmp_size = le32_to_cpu(bmp_hdr->file_size);
100 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
101 goto splash_address_too_high;
103 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
105 splash_address_too_high:
106 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
111 static int splash_select_fs_dev(struct splash_location *location)
115 switch (location->storage) {
116 case SPLASH_STORAGE_MMC:
117 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
119 case SPLASH_STORAGE_USB:
120 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
122 case SPLASH_STORAGE_SATA:
123 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
125 case SPLASH_STORAGE_NAND:
126 if (location->ubivol != NULL)
127 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
132 printf("Error: unsupported location storage.\n");
137 printf("Error: could not access storage.\n");
142 #ifdef CONFIG_USB_STORAGE
143 static int splash_init_usb(void)
151 #ifndef CONFIG_DM_USB
152 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
158 static inline int splash_init_usb(void)
160 printf("Cannot load splash image: no USB support\n");
166 static int splash_init_sata(void)
168 return sata_probe(0);
171 static inline int splash_init_sata(void)
173 printf("Cannot load splash image: no SATA support\n");
178 #ifdef CONFIG_CMD_UBIFS
179 static int splash_mount_ubifs(struct splash_location *location)
184 sprintf(cmd, "ubi part %s", location->mtdpart);
185 res = run_command(cmd, 0);
189 sprintf(cmd, "ubifsmount %s", location->ubivol);
190 res = run_command(cmd, 0);
195 static inline int splash_umount_ubifs(void)
197 return run_command("ubifsumount", 0);
200 static inline int splash_mount_ubifs(struct splash_location *location)
202 printf("Cannot load splash image: no UBIFS support\n");
206 static inline int splash_umount_ubifs(void)
208 printf("Cannot unmount UBIFS: no UBIFS support\n");
213 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
215 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
222 splash_file = env_get("splashfile");
224 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
226 if (location->storage == SPLASH_STORAGE_USB)
227 res = splash_init_usb();
229 if (location->storage == SPLASH_STORAGE_SATA)
230 res = splash_init_sata();
232 if (location->ubivol != NULL)
233 res = splash_mount_ubifs(location);
238 res = splash_select_fs_dev(location);
242 res = fs_size(splash_file, &bmp_size);
244 printf("Error (%d): cannot determine file size\n", res);
248 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
249 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
254 splash_select_fs_dev(location);
255 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
258 if (location->ubivol != NULL)
259 splash_umount_ubifs();
265 * select_splash_location - return the splash location based on board support
266 * and env variable "splashsource".
268 * @locations: An array of supported splash locations.
269 * @size: Size of splash_locations array.
271 * @return: If a null set of splash locations is given, or
272 * splashsource env variable is set to unsupported value
274 * If splashsource env variable is not defined
275 * return the first entry in splash_locations as default.
276 * If splashsource env variable contains a supported value
277 * return the location selected by splashsource.
279 static struct splash_location *select_splash_location(
280 struct splash_location *locations, uint size)
283 char *env_splashsource;
285 if (!locations || size == 0)
288 env_splashsource = env_get("splashsource");
289 if (env_splashsource == NULL)
290 return &locations[0];
292 for (i = 0; i < size; i++) {
293 if (!strcmp(locations[i].name, env_splashsource))
294 return &locations[i];
297 printf("splashsource env variable set to unsupported value\n");
302 static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
306 const char *splash_file;
307 const void *internal_splash_data;
308 size_t internal_splash_size;
309 int external_splash_addr;
310 int external_splash_size;
311 bool is_splash_external = false;
312 struct image_header *img_header;
313 const u32 *fit_header;
315 const size_t header_size = sizeof(struct image_header);
317 /* Read in image header */
318 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
322 img_header = (struct image_header *)bmp_load_addr;
323 if (image_get_magic(img_header) != FDT_MAGIC) {
324 printf("Could not find FDT magic\n");
328 fit_size = fdt_totalsize(img_header);
330 /* Read in entire FIT */
331 fit_header = (const u32 *)(bmp_load_addr + header_size);
332 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
336 res = fit_check_format(fit_header);
338 debug("Could not find valid FIT image\n");
342 /* Get the splash image node */
343 splash_file = env_get("splashfile");
345 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
347 node_offset = fit_image_get_node(fit_header, splash_file);
348 if (node_offset < 0) {
349 debug("Could not find splash image '%s' in FIT\n",
354 /* Extract the splash data from FIT */
355 /* 1. Test if splash is in FIT internal data. */
356 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
357 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
358 /* 2. Test if splash is in FIT external data with fixed position. */
359 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
360 is_splash_external = true;
361 /* 3. Test if splash is in FIT external data with offset. */
362 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
363 /* Align data offset to 4-byte boundary */
364 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
365 /* External splash offset means the offset by end of FIT header */
366 external_splash_addr += location->offset + fit_size;
367 is_splash_external = true;
369 printf("Failed to get splash image from FIT\n");
373 if (is_splash_external) {
374 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
376 printf("Failed to get size of splash image (err=%d)\n", res);
380 /* Read in the splash data */
381 location->offset = external_splash_addr;
382 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
389 #endif /* CONFIG_FIT */
392 * splash_source_load - load splash image from a supported location.
394 * Select a splash image location based on the value of splashsource environment
395 * variable and the board supported splash source locations, and load a
396 * splashimage to the address pointed to by splashimage environment variable.
398 * @locations: An array of supported splash locations.
399 * @size: Size of splash_locations array.
401 * @return: 0 on success, negative value on failure.
403 int splash_source_load(struct splash_location *locations, uint size)
405 struct splash_location *splash_location;
406 char *env_splashimage_value;
409 env_splashimage_value = env_get("splashimage");
410 if (env_splashimage_value == NULL)
413 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
414 if (bmp_load_addr == 0) {
415 printf("Error: bad splashimage address specified\n");
419 splash_location = select_splash_location(locations, size);
420 if (!splash_location)
423 if (splash_location->flags == SPLASH_STORAGE_RAW)
424 return splash_load_raw(splash_location, bmp_load_addr);
425 else if (splash_location->flags == SPLASH_STORAGE_FS)
426 return splash_load_fs(splash_location, bmp_load_addr);
428 else if (splash_location->flags == SPLASH_STORAGE_FIT)
429 return splash_load_fit(splash_location, bmp_load_addr);