]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/splash_source.c
Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[people/ms/u-boot.git] / common / splash_source.c
CommitLineData
f4a40f05
IG
1/*
2 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
3 *
4 * Authors: Igor Grinberg <grinberg@compulab.co.il>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <nand.h>
6947e3f5 11#include <errno.h>
f82eb2fa 12#include <splash.h>
7e8d7f2a
NK
13#include <spi_flash.h>
14#include <spi.h>
9bb4e947 15#include <usb.h>
50c2d2e1 16#include <sata.h>
f4a40f05 17#include <bmp_layout.h>
870dd309 18#include <fs.h>
f4a40f05
IG
19
20DECLARE_GLOBAL_DATA_PTR;
21
7e8d7f2a
NK
22#ifdef CONFIG_SPI_FLASH
23static struct spi_flash *sf;
bcbb6448 24static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
25{
26 if (!sf) {
27 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
28 CONFIG_SF_DEFAULT_CS,
29 CONFIG_SF_DEFAULT_SPEED,
30 CONFIG_SF_DEFAULT_MODE);
31 if (!sf)
32 return -ENODEV;
33 }
34
35 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
36}
37#else
bcbb6448 38static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
39{
40 debug("%s: sf support not available\n", __func__);
41 return -ENOSYS;
42}
43#endif
44
f4a40f05 45#ifdef CONFIG_CMD_NAND
bcbb6448 46static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c 47{
b616d9b0 48 return nand_read_skip_bad(nand_info[nand_curr_device], offset,
7be4cd2c 49 &read_size, NULL,
b616d9b0 50 nand_info[nand_curr_device]->size,
7be4cd2c
NK
51 (u_char *)bmp_load_addr);
52}
53#else
bcbb6448 54static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c
NK
55{
56 debug("%s: nand support not available\n", __func__);
57 return -ENOSYS;
58}
59#endif
60
bcbb6448 61static int splash_storage_read_raw(struct splash_location *location,
fd29dd55 62 u32 bmp_load_addr, size_t read_size)
7be4cd2c 63{
fd29dd55
NK
64 u32 offset;
65
66 if (!location)
67 return -EINVAL;
68
69 offset = location->offset;
70 switch (location->storage) {
71 case SPLASH_STORAGE_NAND:
bcbb6448 72 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
7e8d7f2a 73 case SPLASH_STORAGE_SF:
bcbb6448 74 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
fd29dd55
NK
75 default:
76 printf("Unknown splash location\n");
77 }
78
79 return -EINVAL;
7be4cd2c
NK
80}
81
fd29dd55 82static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
f4a40f05
IG
83{
84 struct bmp_header *bmp_hdr;
85 int res;
86 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
87
88 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
89 goto splash_address_too_high;
90
bcbb6448 91 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
f4a40f05
IG
92 if (res < 0)
93 return res;
94
95 bmp_hdr = (struct bmp_header *)bmp_load_addr;
96 bmp_size = le32_to_cpu(bmp_hdr->file_size);
97
98 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
99 goto splash_address_too_high;
100
bcbb6448 101 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
f4a40f05
IG
102
103splash_address_too_high:
f82eb2fa 104 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
f4a40f05 105
6947e3f5 106 return -EFAULT;
f4a40f05 107}
f4a40f05 108
870dd309
NK
109static int splash_select_fs_dev(struct splash_location *location)
110{
111 int res;
112
113 switch (location->storage) {
114 case SPLASH_STORAGE_MMC:
115 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
116 break;
9bb4e947
NK
117 case SPLASH_STORAGE_USB:
118 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
119 break;
50c2d2e1
NK
120 case SPLASH_STORAGE_SATA:
121 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
122 break;
1cb075c6
EM
123 case SPLASH_STORAGE_NAND:
124 if (location->ubivol != NULL)
125 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
126 else
127 res = -ENODEV;
128 break;
870dd309
NK
129 default:
130 printf("Error: unsupported location storage.\n");
131 return -ENODEV;
132 }
133
134 if (res)
135 printf("Error: could not access storage.\n");
136
137 return res;
138}
139
9bb4e947
NK
140#ifdef CONFIG_USB_STORAGE
141static int splash_init_usb(void)
142{
143 int err;
144
145 err = usb_init();
146 if (err)
147 return err;
148
149 return usb_stor_scan(1) < 0 ? -ENODEV : 0;
150}
151#else
152static inline int splash_init_usb(void)
153{
154 printf("Cannot load splash image: no USB support\n");
155 return -ENOSYS;
156}
157#endif
158
50c2d2e1
NK
159#ifdef CONFIG_CMD_SATA
160static int splash_init_sata(void)
161{
162 return sata_initialize();
163}
164#else
165static inline int splash_init_sata(void)
166{
167 printf("Cannot load splash image: no SATA support\n");
168 return -ENOSYS;
169}
170#endif
171
1cb075c6
EM
172#ifdef CONFIG_CMD_UBIFS
173static int splash_mount_ubifs(struct splash_location *location)
174{
175 int res;
176 char cmd[32];
177
178 sprintf(cmd, "ubi part %s", location->mtdpart);
179 res = run_command(cmd, 0);
180 if (res)
181 return res;
182
183 sprintf(cmd, "ubifsmount %s", location->ubivol);
184 res = run_command(cmd, 0);
185
186 return res;
187}
188
189static inline int splash_umount_ubifs(void)
190{
191 return run_command("ubifsumount", 0);
192}
193#else
194static inline int splash_mount_ubifs(struct splash_location *location)
195{
196 printf("Cannot load splash image: no UBIFS support\n");
197 return -ENOSYS;
198}
199
200static inline int splash_umount_ubifs(void)
201{
202 printf("Cannot unmount UBIFS: no UBIFS support\n");
203 return -ENOSYS;
204}
205#endif
206
870dd309
NK
207#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
208
209static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
210{
9bb4e947 211 int res = 0;
870dd309
NK
212 loff_t bmp_size;
213 char *splash_file;
214
215 splash_file = getenv("splashfile");
216 if (!splash_file)
217 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
218
9bb4e947
NK
219 if (location->storage == SPLASH_STORAGE_USB)
220 res = splash_init_usb();
221
50c2d2e1
NK
222 if (location->storage == SPLASH_STORAGE_SATA)
223 res = splash_init_sata();
224
1cb075c6
EM
225 if (location->ubivol != NULL)
226 res = splash_mount_ubifs(location);
227
9bb4e947
NK
228 if (res)
229 return res;
230
870dd309
NK
231 res = splash_select_fs_dev(location);
232 if (res)
1cb075c6 233 goto out;
870dd309
NK
234
235 res = fs_size(splash_file, &bmp_size);
236 if (res) {
237 printf("Error (%d): cannot determine file size\n", res);
1cb075c6 238 goto out;
870dd309
NK
239 }
240
241 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
242 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
1cb075c6
EM
243 res = -EFAULT;
244 goto out;
870dd309
NK
245 }
246
247 splash_select_fs_dev(location);
1cb075c6
EM
248 res = fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
249
250out:
251 if (location->ubivol != NULL)
252 splash_umount_ubifs();
253
254 return res;
870dd309
NK
255}
256
fd29dd55
NK
257/**
258 * select_splash_location - return the splash location based on board support
259 * and env variable "splashsource".
260 *
261 * @locations: An array of supported splash locations.
262 * @size: Size of splash_locations array.
263 *
264 * @return: If a null set of splash locations is given, or
265 * splashsource env variable is set to unsupported value
266 * return NULL.
267 * If splashsource env variable is not defined
268 * return the first entry in splash_locations as default.
269 * If splashsource env variable contains a supported value
270 * return the location selected by splashsource.
271 */
272static struct splash_location *select_splash_location(
273 struct splash_location *locations, uint size)
274{
275 int i;
276 char *env_splashsource;
277
278 if (!locations || size == 0)
279 return NULL;
280
281 env_splashsource = getenv("splashsource");
282 if (env_splashsource == NULL)
283 return &locations[0];
284
285 for (i = 0; i < size; i++) {
286 if (!strcmp(locations[i].name, env_splashsource))
287 return &locations[i];
288 }
289
290 printf("splashsource env variable set to unsupported value\n");
291 return NULL;
292}
293
f82eb2fa
NK
294/**
295 * splash_source_load - load splash image from a supported location.
296 *
297 * Select a splash image location based on the value of splashsource environment
298 * variable and the board supported splash source locations, and load a
299 * splashimage to the address pointed to by splashimage environment variable.
300 *
301 * @locations: An array of supported splash locations.
302 * @size: Size of splash_locations array.
303 *
304 * @return: 0 on success, negative value on failure.
305 */
306int splash_source_load(struct splash_location *locations, uint size)
f4a40f05 307{
fd29dd55 308 struct splash_location *splash_location;
f4a40f05
IG
309 char *env_splashimage_value;
310 u32 bmp_load_addr;
311
312 env_splashimage_value = getenv("splashimage");
313 if (env_splashimage_value == NULL)
6947e3f5 314 return -ENOENT;
f4a40f05
IG
315
316 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
317 if (bmp_load_addr == 0) {
318 printf("Error: bad splashimage address specified\n");
6947e3f5 319 return -EFAULT;
f4a40f05
IG
320 }
321
fd29dd55
NK
322 splash_location = select_splash_location(locations, size);
323 if (!splash_location)
324 return -EINVAL;
325
870dd309
NK
326 if (splash_location->flags & SPLASH_STORAGE_RAW)
327 return splash_load_raw(splash_location, bmp_load_addr);
328 else if (splash_location->flags & SPLASH_STORAGE_FS)
329 return splash_load_fs(splash_location, bmp_load_addr);
330
331 return -EINVAL;
f4a40f05 332}