]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/splash_source.c
ARM: add psci_arch_init() declaration for CONFIG_ARMV7_PSCI
[thirdparty/u-boot.git] / common / splash_source.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
f4a40f05
IG
2/*
3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 *
5 * Authors: Igor Grinberg <grinberg@compulab.co.il>
f4a40f05
IG
6 */
7
8#include <common.h>
7583f1f5 9#include <bmp_layout.h>
288b29e4 10#include <command.h>
7b51b576 11#include <env.h>
6947e3f5 12#include <errno.h>
7583f1f5 13#include <fs.h>
db1b79b8 14#include <fdt_support.h>
7583f1f5 15#include <image.h>
f7ae49fc 16#include <log.h>
7583f1f5 17#include <nand.h>
18#include <sata.h>
7e8d7f2a 19#include <spi.h>
7583f1f5 20#include <spi_flash.h>
21#include <splash.h>
9bb4e947 22#include <usb.h>
f4a40f05
IG
23
24DECLARE_GLOBAL_DATA_PTR;
25
7e8d7f2a
NK
26#ifdef CONFIG_SPI_FLASH
27static struct spi_flash *sf;
bcbb6448 28static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
29{
30 if (!sf) {
31 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
32 CONFIG_SF_DEFAULT_CS,
33 CONFIG_SF_DEFAULT_SPEED,
34 CONFIG_SF_DEFAULT_MODE);
35 if (!sf)
36 return -ENODEV;
37 }
38
39 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
40}
41#else
bcbb6448 42static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
43{
44 debug("%s: sf support not available\n", __func__);
45 return -ENOSYS;
46}
47#endif
48
f4a40f05 49#ifdef CONFIG_CMD_NAND
bcbb6448 50static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c 51{
edba8cc4
GS
52 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
53 return nand_read_skip_bad(mtd, offset,
7be4cd2c 54 &read_size, NULL,
edba8cc4 55 mtd->size,
7be4cd2c
NK
56 (u_char *)bmp_load_addr);
57}
58#else
bcbb6448 59static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c
NK
60{
61 debug("%s: nand support not available\n", __func__);
62 return -ENOSYS;
63}
64#endif
65
bcbb6448 66static int splash_storage_read_raw(struct splash_location *location,
fd29dd55 67 u32 bmp_load_addr, size_t read_size)
7be4cd2c 68{
fd29dd55
NK
69 u32 offset;
70
71 if (!location)
72 return -EINVAL;
73
74 offset = location->offset;
75 switch (location->storage) {
76 case SPLASH_STORAGE_NAND:
bcbb6448 77 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
7e8d7f2a 78 case SPLASH_STORAGE_SF:
bcbb6448 79 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
fd29dd55
NK
80 default:
81 printf("Unknown splash location\n");
82 }
83
84 return -EINVAL;
7be4cd2c
NK
85}
86
fd29dd55 87static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
f4a40f05
IG
88{
89 struct bmp_header *bmp_hdr;
90 int res;
91 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
92
93 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
94 goto splash_address_too_high;
95
bcbb6448 96 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
f4a40f05
IG
97 if (res < 0)
98 return res;
99
100 bmp_hdr = (struct bmp_header *)bmp_load_addr;
101 bmp_size = le32_to_cpu(bmp_hdr->file_size);
102
103 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
104 goto splash_address_too_high;
105
bcbb6448 106 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
f4a40f05
IG
107
108splash_address_too_high:
f82eb2fa 109 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
f4a40f05 110
6947e3f5 111 return -EFAULT;
f4a40f05 112}
f4a40f05 113
870dd309
NK
114static int splash_select_fs_dev(struct splash_location *location)
115{
116 int res;
117
118 switch (location->storage) {
119 case SPLASH_STORAGE_MMC:
120 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
121 break;
9bb4e947
NK
122 case SPLASH_STORAGE_USB:
123 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
124 break;
50c2d2e1
NK
125 case SPLASH_STORAGE_SATA:
126 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
127 break;
1cb075c6
EM
128 case SPLASH_STORAGE_NAND:
129 if (location->ubivol != NULL)
130 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
131 else
132 res = -ENODEV;
133 break;
870dd309
NK
134 default:
135 printf("Error: unsupported location storage.\n");
136 return -ENODEV;
137 }
138
139 if (res)
140 printf("Error: could not access storage.\n");
141
142 return res;
143}
144
9bb4e947
NK
145#ifdef CONFIG_USB_STORAGE
146static int splash_init_usb(void)
147{
148 int err;
149
150 err = usb_init();
151 if (err)
152 return err;
153
d7b60fbf
AB
154#ifndef CONFIG_DM_USB
155 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
156#endif
157
158 return err;
9bb4e947
NK
159}
160#else
161static inline int splash_init_usb(void)
162{
163 printf("Cannot load splash image: no USB support\n");
164 return -ENOSYS;
165}
166#endif
167
10e40d54 168#ifdef CONFIG_SATA
50c2d2e1
NK
169static int splash_init_sata(void)
170{
f19f1ecb 171 return sata_probe(0);
50c2d2e1
NK
172}
173#else
174static inline int splash_init_sata(void)
175{
176 printf("Cannot load splash image: no SATA support\n");
177 return -ENOSYS;
178}
179#endif
180
1cb075c6
EM
181#ifdef CONFIG_CMD_UBIFS
182static int splash_mount_ubifs(struct splash_location *location)
183{
184 int res;
185 char cmd[32];
186
187 sprintf(cmd, "ubi part %s", location->mtdpart);
188 res = run_command(cmd, 0);
189 if (res)
190 return res;
191
192 sprintf(cmd, "ubifsmount %s", location->ubivol);
193 res = run_command(cmd, 0);
194
195 return res;
196}
197
198static inline int splash_umount_ubifs(void)
199{
200 return run_command("ubifsumount", 0);
201}
202#else
203static inline int splash_mount_ubifs(struct splash_location *location)
204{
205 printf("Cannot load splash image: no UBIFS support\n");
206 return -ENOSYS;
207}
208
209static inline int splash_umount_ubifs(void)
210{
211 printf("Cannot unmount UBIFS: no UBIFS support\n");
212 return -ENOSYS;
213}
214#endif
215
870dd309
NK
216#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
217
218static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
219{
9bb4e947 220 int res = 0;
870dd309 221 loff_t bmp_size;
3cc6e707 222 loff_t actread;
870dd309
NK
223 char *splash_file;
224
00caae6d 225 splash_file = env_get("splashfile");
870dd309
NK
226 if (!splash_file)
227 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
228
9bb4e947
NK
229 if (location->storage == SPLASH_STORAGE_USB)
230 res = splash_init_usb();
231
50c2d2e1
NK
232 if (location->storage == SPLASH_STORAGE_SATA)
233 res = splash_init_sata();
234
1cb075c6
EM
235 if (location->ubivol != NULL)
236 res = splash_mount_ubifs(location);
237
9bb4e947
NK
238 if (res)
239 return res;
240
870dd309
NK
241 res = splash_select_fs_dev(location);
242 if (res)
1cb075c6 243 goto out;
870dd309
NK
244
245 res = fs_size(splash_file, &bmp_size);
246 if (res) {
247 printf("Error (%d): cannot determine file size\n", res);
1cb075c6 248 goto out;
870dd309
NK
249 }
250
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");
1cb075c6
EM
253 res = -EFAULT;
254 goto out;
870dd309
NK
255 }
256
257 splash_select_fs_dev(location);
3cc6e707 258 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
1cb075c6
EM
259
260out:
261 if (location->ubivol != NULL)
262 splash_umount_ubifs();
263
264 return res;
870dd309
NK
265}
266
fd29dd55
NK
267/**
268 * select_splash_location - return the splash location based on board support
269 * and env variable "splashsource".
270 *
271 * @locations: An array of supported splash locations.
272 * @size: Size of splash_locations array.
273 *
274 * @return: If a null set of splash locations is given, or
275 * splashsource env variable is set to unsupported value
276 * return NULL.
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.
281 */
282static struct splash_location *select_splash_location(
283 struct splash_location *locations, uint size)
284{
285 int i;
286 char *env_splashsource;
287
288 if (!locations || size == 0)
289 return NULL;
290
00caae6d 291 env_splashsource = env_get("splashsource");
fd29dd55
NK
292 if (env_splashsource == NULL)
293 return &locations[0];
294
295 for (i = 0; i < size; i++) {
296 if (!strcmp(locations[i].name, env_splashsource))
297 return &locations[i];
298 }
299
300 printf("splashsource env variable set to unsupported value\n");
301 return NULL;
302}
303
db1b79b8 304#ifdef CONFIG_FIT
305static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
306{
307 int res;
308 int node_offset;
3d92f317 309 const char *splash_file;
d5006836
LR
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;
db1b79b8 315 struct image_header *img_header;
316 const u32 *fit_header;
317 u32 fit_size;
318 const size_t header_size = sizeof(struct image_header);
319
320 /* Read in image header */
321 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
322 if (res < 0)
323 return res;
324
325 img_header = (struct image_header *)bmp_load_addr;
a7126edc
NM
326 if (image_get_magic(img_header) != FDT_MAGIC) {
327 printf("Could not find FDT magic\n");
328 return -EINVAL;
329 }
330
db1b79b8 331 fit_size = fdt_totalsize(img_header);
332
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);
336 if (res < 0)
337 return res;
338
339 res = fit_check_format(fit_header);
340 if (!res) {
341 debug("Could not find valid FIT image\n");
342 return -EINVAL;
343 }
344
3d92f317
LR
345 /* Get the splash image node */
346 splash_file = env_get("splashfile");
347 if (!splash_file)
348 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
349
350 node_offset = fit_image_get_node(fit_header, splash_file);
db1b79b8 351 if (node_offset < 0) {
352 debug("Could not find splash image '%s' in FIT\n",
3d92f317 353 splash_file);
db1b79b8 354 return -ENOENT;
355 }
356
d5006836
LR
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;
371 } else {
372 printf("Failed to get splash image from FIT\n");
373 return -ENODATA;
db1b79b8 374 }
375
d5006836
LR
376 if (is_splash_external) {
377 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
378 if (res < 0) {
379 printf("Failed to get size of splash image (err=%d)\n", res);
380 return res;
381 }
382
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);
386 if (res < 0)
387 return res;
db1b79b8 388 }
389
db1b79b8 390 return 0;
391}
392#endif /* CONFIG_FIT */
393
f82eb2fa
NK
394/**
395 * splash_source_load - load splash image from a supported location.
396 *
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.
400 *
401 * @locations: An array of supported splash locations.
402 * @size: Size of splash_locations array.
403 *
404 * @return: 0 on success, negative value on failure.
405 */
406int splash_source_load(struct splash_location *locations, uint size)
f4a40f05 407{
fd29dd55 408 struct splash_location *splash_location;
f4a40f05
IG
409 char *env_splashimage_value;
410 u32 bmp_load_addr;
411
00caae6d 412 env_splashimage_value = env_get("splashimage");
f4a40f05 413 if (env_splashimage_value == NULL)
6947e3f5 414 return -ENOENT;
f4a40f05
IG
415
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");
6947e3f5 419 return -EFAULT;
f4a40f05
IG
420 }
421
fd29dd55
NK
422 splash_location = select_splash_location(locations, size);
423 if (!splash_location)
424 return -EINVAL;
425
3b593f90 426 if (splash_location->flags == SPLASH_STORAGE_RAW)
870dd309 427 return splash_load_raw(splash_location, bmp_load_addr);
3b593f90 428 else if (splash_location->flags == SPLASH_STORAGE_FS)
870dd309 429 return splash_load_fs(splash_location, bmp_load_addr);
db1b79b8 430#ifdef CONFIG_FIT
431 else if (splash_location->flags == SPLASH_STORAGE_FIT)
432 return splash_load_fit(splash_location, bmp_load_addr);
433#endif
870dd309 434 return -EINVAL;
f4a40f05 435}