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>
16 #include <bmp_layout.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #ifdef CONFIG_SPI_FLASH
22 static struct spi_flash *sf;
23 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
26 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
28 CONFIG_SF_DEFAULT_SPEED,
29 CONFIG_SF_DEFAULT_MODE);
34 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
37 static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
39 debug("%s: sf support not available\n", __func__);
44 #ifdef CONFIG_CMD_NAND
45 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
47 return nand_read_skip_bad(&nand_info[nand_curr_device], offset,
49 nand_info[nand_curr_device].size,
50 (u_char *)bmp_load_addr);
53 static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
55 debug("%s: nand support not available\n", __func__);
60 static int splash_storage_read_raw(struct splash_location *location,
61 u32 bmp_load_addr, size_t read_size)
68 offset = location->offset;
69 switch (location->storage) {
70 case SPLASH_STORAGE_NAND:
71 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
72 case SPLASH_STORAGE_SF:
73 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
75 printf("Unknown splash location\n");
81 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
83 struct bmp_header *bmp_hdr;
85 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
87 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
88 goto splash_address_too_high;
90 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
94 bmp_hdr = (struct bmp_header *)bmp_load_addr;
95 bmp_size = le32_to_cpu(bmp_hdr->file_size);
97 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
98 goto splash_address_too_high;
100 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
102 splash_address_too_high:
103 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
108 static int splash_select_fs_dev(struct splash_location *location)
112 switch (location->storage) {
113 case SPLASH_STORAGE_MMC:
114 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
116 case SPLASH_STORAGE_USB:
117 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
120 printf("Error: unsupported location storage.\n");
125 printf("Error: could not access storage.\n");
130 #ifdef CONFIG_USB_STORAGE
131 static int splash_init_usb(void)
139 return usb_stor_scan(1) < 0 ? -ENODEV : 0;
142 static inline int splash_init_usb(void)
144 printf("Cannot load splash image: no USB support\n");
149 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
151 static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
157 splash_file = getenv("splashfile");
159 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
161 if (location->storage == SPLASH_STORAGE_USB)
162 res = splash_init_usb();
167 res = splash_select_fs_dev(location);
171 res = fs_size(splash_file, &bmp_size);
173 printf("Error (%d): cannot determine file size\n", res);
177 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
178 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
182 splash_select_fs_dev(location);
183 return fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
187 * select_splash_location - return the splash location based on board support
188 * and env variable "splashsource".
190 * @locations: An array of supported splash locations.
191 * @size: Size of splash_locations array.
193 * @return: If a null set of splash locations is given, or
194 * splashsource env variable is set to unsupported value
196 * If splashsource env variable is not defined
197 * return the first entry in splash_locations as default.
198 * If splashsource env variable contains a supported value
199 * return the location selected by splashsource.
201 static struct splash_location *select_splash_location(
202 struct splash_location *locations, uint size)
205 char *env_splashsource;
207 if (!locations || size == 0)
210 env_splashsource = getenv("splashsource");
211 if (env_splashsource == NULL)
212 return &locations[0];
214 for (i = 0; i < size; i++) {
215 if (!strcmp(locations[i].name, env_splashsource))
216 return &locations[i];
219 printf("splashsource env variable set to unsupported value\n");
224 * splash_source_load - load splash image from a supported location.
226 * Select a splash image location based on the value of splashsource environment
227 * variable and the board supported splash source locations, and load a
228 * splashimage to the address pointed to by splashimage environment variable.
230 * @locations: An array of supported splash locations.
231 * @size: Size of splash_locations array.
233 * @return: 0 on success, negative value on failure.
235 int splash_source_load(struct splash_location *locations, uint size)
237 struct splash_location *splash_location;
238 char *env_splashimage_value;
241 env_splashimage_value = getenv("splashimage");
242 if (env_splashimage_value == NULL)
245 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
246 if (bmp_load_addr == 0) {
247 printf("Error: bad splashimage address specified\n");
251 splash_location = select_splash_location(locations, size);
252 if (!splash_location)
255 if (splash_location->flags & SPLASH_STORAGE_RAW)
256 return splash_load_raw(splash_location, bmp_load_addr);
257 else if (splash_location->flags & SPLASH_STORAGE_FS)
258 return splash_load_fs(splash_location, bmp_load_addr);