2 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 * Authors: Igor Grinberg <grinberg@compulab.co.il>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <spi_flash.h>
17 #include <bmp_layout.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #ifdef CONFIG_SPI_FLASH
23 static struct spi_flash *sf;
24 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
27 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
29 CONFIG_SF_DEFAULT_SPEED,
30 CONFIG_SF_DEFAULT_MODE);
35 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
38 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
40 debug("%s: sf support not available\n", __func__);
45 #ifdef CONFIG_CMD_NAND
46 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
48 return nand_read_skip_bad(nand_info[nand_curr_device], offset,
50 nand_info[nand_curr_device]->size,
51 (u_char *)bmp_load_addr);
54 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
56 debug("%s: nand support not available\n", __func__);
61 static int splash_storage_read_raw(struct splash_location *location,
62 u32 bmp_load_addr, size_t read_size)
69 offset = location->offset;
70 switch (location->storage) {
71 case SPLASH_STORAGE_NAND:
72 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
73 case SPLASH_STORAGE_SF:
74 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
76 printf("Unknown splash location\n");
82 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
84 struct bmp_header *bmp_hdr;
86 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
88 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
89 goto splash_address_too_high;
91 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
95 bmp_hdr = (struct bmp_header *)bmp_load_addr;
96 bmp_size = le32_to_cpu(bmp_hdr->file_size);
98 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
99 goto splash_address_too_high;
101 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
103 splash_address_too_high:
104 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
109 static int splash_select_fs_dev(struct splash_location *location)
113 switch (location->storage) {
114 case SPLASH_STORAGE_MMC:
115 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
117 case SPLASH_STORAGE_USB:
118 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
120 case SPLASH_STORAGE_SATA:
121 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
123 case SPLASH_STORAGE_NAND:
124 if (location->ubivol != NULL)
125 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
130 printf("Error: unsupported location storage.\n");
135 printf("Error: could not access storage.\n");
140 #ifdef CONFIG_USB_STORAGE
141 static int splash_init_usb(void)
149 #ifndef CONFIG_DM_USB
150 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
156 static inline int splash_init_usb(void)
158 printf("Cannot load splash image: no USB support\n");
163 #ifdef CONFIG_CMD_SATA
164 static int splash_init_sata(void)
166 return sata_initialize();
169 static inline int splash_init_sata(void)
171 printf("Cannot load splash image: no SATA support\n");
176 #ifdef CONFIG_CMD_UBIFS
177 static int splash_mount_ubifs(struct splash_location *location)
182 sprintf(cmd, "ubi part %s", location->mtdpart);
183 res = run_command(cmd, 0);
187 sprintf(cmd, "ubifsmount %s", location->ubivol);
188 res = run_command(cmd, 0);
193 static inline int splash_umount_ubifs(void)
195 return run_command("ubifsumount", 0);
198 static inline int splash_mount_ubifs(struct splash_location *location)
200 printf("Cannot load splash image: no UBIFS support\n");
204 static inline int splash_umount_ubifs(void)
206 printf("Cannot unmount UBIFS: no UBIFS support\n");
211 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
213 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
219 splash_file = getenv("splashfile");
221 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
223 if (location->storage == SPLASH_STORAGE_USB)
224 res = splash_init_usb();
226 if (location->storage == SPLASH_STORAGE_SATA)
227 res = splash_init_sata();
229 if (location->ubivol != NULL)
230 res = splash_mount_ubifs(location);
235 res = splash_select_fs_dev(location);
239 res = fs_size(splash_file, &bmp_size);
241 printf("Error (%d): cannot determine file size\n", res);
245 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
246 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
251 splash_select_fs_dev(location);
252 res = fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
255 if (location->ubivol != NULL)
256 splash_umount_ubifs();
262 * select_splash_location - return the splash location based on board support
263 * and env variable "splashsource".
265 * @locations: An array of supported splash locations.
266 * @size: Size of splash_locations array.
268 * @return: If a null set of splash locations is given, or
269 * splashsource env variable is set to unsupported value
271 * If splashsource env variable is not defined
272 * return the first entry in splash_locations as default.
273 * If splashsource env variable contains a supported value
274 * return the location selected by splashsource.
276 static struct splash_location *select_splash_location(
277 struct splash_location *locations, uint size)
280 char *env_splashsource;
282 if (!locations || size == 0)
285 env_splashsource = getenv("splashsource");
286 if (env_splashsource == NULL)
287 return &locations[0];
289 for (i = 0; i < size; i++) {
290 if (!strcmp(locations[i].name, env_splashsource))
291 return &locations[i];
294 printf("splashsource env variable set to unsupported value\n");
299 * splash_source_load - load splash image from a supported location.
301 * Select a splash image location based on the value of splashsource environment
302 * variable and the board supported splash source locations, and load a
303 * splashimage to the address pointed to by splashimage environment variable.
305 * @locations: An array of supported splash locations.
306 * @size: Size of splash_locations array.
308 * @return: 0 on success, negative value on failure.
310 int splash_source_load(struct splash_location *locations, uint size)
312 struct splash_location *splash_location;
313 char *env_splashimage_value;
316 env_splashimage_value = getenv("splashimage");
317 if (env_splashimage_value == NULL)
320 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
321 if (bmp_load_addr == 0) {
322 printf("Error: bad splashimage address specified\n");
326 splash_location = select_splash_location(locations, size);
327 if (!splash_location)
330 if (splash_location->flags & SPLASH_STORAGE_RAW)
331 return splash_load_raw(splash_location, bmp_load_addr);
332 else if (splash_location->flags & SPLASH_STORAGE_FS)
333 return splash_load_fs(splash_location, bmp_load_addr);