blob: d37b4b304c2c2ec77af140091ca8e88bd14d1732 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Igor Grinberg86ec16b2014-11-03 11:32:20 +02002/*
3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 *
5 * Authors: Igor Grinberg <grinberg@compulab.co.il>
Igor Grinberg86ec16b2014-11-03 11:32:20 +02006 */
7
8#include <common.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +02009#include <bmp_layout.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Nikita Kiryanovf58c5092015-01-14 10:42:49 +020011#include <errno.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020012#include <fs.h>
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +020013#include <fdt_support.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020014#include <image.h>
15#include <nand.h>
16#include <sata.h>
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020017#include <spi.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020018#include <spi_flash.h>
19#include <splash.h>
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +020020#include <usb.h>
Igor Grinberg86ec16b2014-11-03 11:32:20 +020021
22DECLARE_GLOBAL_DATA_PTR;
23
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020024#ifdef CONFIG_SPI_FLASH
25static struct spi_flash *sf;
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020026static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020027{
28 if (!sf) {
29 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
30 CONFIG_SF_DEFAULT_CS,
31 CONFIG_SF_DEFAULT_SPEED,
32 CONFIG_SF_DEFAULT_MODE);
33 if (!sf)
34 return -ENODEV;
35 }
36
37 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
38}
39#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020040static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020041{
42 debug("%s: sf support not available\n", __func__);
43 return -ENOSYS;
44}
45#endif
46
Igor Grinberg86ec16b2014-11-03 11:32:20 +020047#ifdef CONFIG_CMD_NAND
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020048static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020049{
Grygorii Strashko5f33b822017-06-26 19:12:56 -050050 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
51 return nand_read_skip_bad(mtd, offset,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020052 &read_size, NULL,
Grygorii Strashko5f33b822017-06-26 19:12:56 -050053 mtd->size,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020054 (u_char *)bmp_load_addr);
55}
56#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020057static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020058{
59 debug("%s: nand support not available\n", __func__);
60 return -ENOSYS;
61}
62#endif
63
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020064static int splash_storage_read_raw(struct splash_location *location,
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020065 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020066{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020067 u32 offset;
68
69 if (!location)
70 return -EINVAL;
71
72 offset = location->offset;
73 switch (location->storage) {
74 case SPLASH_STORAGE_NAND:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020075 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020076 case SPLASH_STORAGE_SF:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020077 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020078 default:
79 printf("Unknown splash location\n");
80 }
81
82 return -EINVAL;
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020083}
84
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020085static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinberg86ec16b2014-11-03 11:32:20 +020086{
87 struct bmp_header *bmp_hdr;
88 int res;
89 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
90
91 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
92 goto splash_address_too_high;
93
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020094 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +020095 if (res < 0)
96 return res;
97
98 bmp_hdr = (struct bmp_header *)bmp_load_addr;
99 bmp_size = le32_to_cpu(bmp_hdr->file_size);
100
101 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
102 goto splash_address_too_high;
103
Nikita Kiryanovb8183de2015-10-29 11:54:40 +0200104 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200105
106splash_address_too_high:
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200107 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200108
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200109 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200110}
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200111
Nikita Kiryanov74282712015-10-29 11:54:41 +0200112static int splash_select_fs_dev(struct splash_location *location)
113{
114 int res;
115
116 switch (location->storage) {
117 case SPLASH_STORAGE_MMC:
118 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
119 break;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200120 case SPLASH_STORAGE_USB:
121 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
122 break;
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200123 case SPLASH_STORAGE_SATA:
124 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
125 break;
Eran Matityahu07a04a12016-06-07 10:38:37 +0300126 case SPLASH_STORAGE_NAND:
127 if (location->ubivol != NULL)
128 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
129 else
130 res = -ENODEV;
131 break;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200132 default:
133 printf("Error: unsupported location storage.\n");
134 return -ENODEV;
135 }
136
137 if (res)
138 printf("Error: could not access storage.\n");
139
140 return res;
141}
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200142
143#ifdef CONFIG_USB_STORAGE
144static int splash_init_usb(void)
145{
146 int err;
147
148 err = usb_init();
149 if (err)
150 return err;
151
Alexey Brodkinbfcdf3f2016-07-01 22:47:36 +0300152#ifndef CONFIG_DM_USB
153 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
154#endif
155
156 return err;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200157}
158#else
159static inline int splash_init_usb(void)
160{
161 printf("Cannot load splash image: no USB support\n");
162 return -ENOSYS;
163}
164#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200165
Simon Glassab3055a2017-06-14 21:28:25 -0600166#ifdef CONFIG_SATA
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200167static int splash_init_sata(void)
168{
Simon Glass6c857ad2017-07-29 11:35:13 -0600169 return sata_probe(0);
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200170}
171#else
172static inline int splash_init_sata(void)
173{
174 printf("Cannot load splash image: no SATA support\n");
175 return -ENOSYS;
176}
177#endif
178
Eran Matityahu07a04a12016-06-07 10:38:37 +0300179#ifdef CONFIG_CMD_UBIFS
180static int splash_mount_ubifs(struct splash_location *location)
181{
182 int res;
183 char cmd[32];
184
185 sprintf(cmd, "ubi part %s", location->mtdpart);
186 res = run_command(cmd, 0);
187 if (res)
188 return res;
189
190 sprintf(cmd, "ubifsmount %s", location->ubivol);
191 res = run_command(cmd, 0);
192
193 return res;
194}
195
196static inline int splash_umount_ubifs(void)
197{
198 return run_command("ubifsumount", 0);
199}
200#else
201static inline int splash_mount_ubifs(struct splash_location *location)
202{
203 printf("Cannot load splash image: no UBIFS support\n");
204 return -ENOSYS;
205}
206
207static inline int splash_umount_ubifs(void)
208{
209 printf("Cannot unmount UBIFS: no UBIFS support\n");
210 return -ENOSYS;
211}
212#endif
213
Nikita Kiryanov74282712015-10-29 11:54:41 +0200214#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
215
216static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
217{
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200218 int res = 0;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200219 loff_t bmp_size;
Jonathan Golder26b61312017-02-24 17:46:10 +0100220 loff_t actread;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200221 char *splash_file;
222
Simon Glass64b723f2017-08-03 12:22:12 -0600223 splash_file = env_get("splashfile");
Nikita Kiryanov74282712015-10-29 11:54:41 +0200224 if (!splash_file)
225 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
226
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200227 if (location->storage == SPLASH_STORAGE_USB)
228 res = splash_init_usb();
229
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200230 if (location->storage == SPLASH_STORAGE_SATA)
231 res = splash_init_sata();
232
Eran Matityahu07a04a12016-06-07 10:38:37 +0300233 if (location->ubivol != NULL)
234 res = splash_mount_ubifs(location);
235
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200236 if (res)
237 return res;
238
Nikita Kiryanov74282712015-10-29 11:54:41 +0200239 res = splash_select_fs_dev(location);
240 if (res)
Eran Matityahu07a04a12016-06-07 10:38:37 +0300241 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200242
243 res = fs_size(splash_file, &bmp_size);
244 if (res) {
245 printf("Error (%d): cannot determine file size\n", res);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300246 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200247 }
248
249 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
250 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Eran Matityahu07a04a12016-06-07 10:38:37 +0300251 res = -EFAULT;
252 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200253 }
254
255 splash_select_fs_dev(location);
Jonathan Golder26b61312017-02-24 17:46:10 +0100256 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300257
258out:
259 if (location->ubivol != NULL)
260 splash_umount_ubifs();
261
262 return res;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200263}
264
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200265/**
266 * select_splash_location - return the splash location based on board support
267 * and env variable "splashsource".
268 *
269 * @locations: An array of supported splash locations.
270 * @size: Size of splash_locations array.
271 *
272 * @return: If a null set of splash locations is given, or
273 * splashsource env variable is set to unsupported value
274 * return NULL.
275 * If splashsource env variable is not defined
276 * return the first entry in splash_locations as default.
277 * If splashsource env variable contains a supported value
278 * return the location selected by splashsource.
279 */
280static struct splash_location *select_splash_location(
281 struct splash_location *locations, uint size)
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200282{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200283 int i;
284 char *env_splashsource;
285
286 if (!locations || size == 0)
287 return NULL;
288
Simon Glass64b723f2017-08-03 12:22:12 -0600289 env_splashsource = env_get("splashsource");
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200290 if (env_splashsource == NULL)
291 return &locations[0];
292
293 for (i = 0; i < size; i++) {
294 if (!strcmp(locations[i].name, env_splashsource))
295 return &locations[i];
296 }
297
298 printf("splashsource env variable set to unsupported value\n");
299 return NULL;
300}
301
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200302#ifdef CONFIG_FIT
303static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
304{
305 int res;
306 int node_offset;
Leo Ruane9b74d92019-02-08 10:51:35 +0100307 const char *splash_file;
Leo Ruanefc20c62019-02-08 10:51:36 +0100308 const void *internal_splash_data;
309 size_t internal_splash_size;
310 int external_splash_addr;
311 int external_splash_size;
312 bool is_splash_external = false;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200313 struct image_header *img_header;
314 const u32 *fit_header;
315 u32 fit_size;
316 const size_t header_size = sizeof(struct image_header);
317
318 /* Read in image header */
319 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
320 if (res < 0)
321 return res;
322
323 img_header = (struct image_header *)bmp_load_addr;
Niko Mauno7e9e0b52017-08-03 09:53:24 +0300324 if (image_get_magic(img_header) != FDT_MAGIC) {
325 printf("Could not find FDT magic\n");
326 return -EINVAL;
327 }
328
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200329 fit_size = fdt_totalsize(img_header);
330
331 /* Read in entire FIT */
332 fit_header = (const u32 *)(bmp_load_addr + header_size);
333 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
334 if (res < 0)
335 return res;
336
337 res = fit_check_format(fit_header);
338 if (!res) {
339 debug("Could not find valid FIT image\n");
340 return -EINVAL;
341 }
342
Leo Ruane9b74d92019-02-08 10:51:35 +0100343 /* Get the splash image node */
344 splash_file = env_get("splashfile");
345 if (!splash_file)
346 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
347
348 node_offset = fit_image_get_node(fit_header, splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200349 if (node_offset < 0) {
350 debug("Could not find splash image '%s' in FIT\n",
Leo Ruane9b74d92019-02-08 10:51:35 +0100351 splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200352 return -ENOENT;
353 }
354
Leo Ruanefc20c62019-02-08 10:51:36 +0100355 /* Extract the splash data from FIT */
356 /* 1. Test if splash is in FIT internal data. */
357 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
358 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
359 /* 2. Test if splash is in FIT external data with fixed position. */
360 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
361 is_splash_external = true;
362 /* 3. Test if splash is in FIT external data with offset. */
363 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
364 /* Align data offset to 4-byte boundary */
365 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
366 /* External splash offset means the offset by end of FIT header */
367 external_splash_addr += location->offset + fit_size;
368 is_splash_external = true;
369 } else {
370 printf("Failed to get splash image from FIT\n");
371 return -ENODATA;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200372 }
373
Leo Ruanefc20c62019-02-08 10:51:36 +0100374 if (is_splash_external) {
375 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
376 if (res < 0) {
377 printf("Failed to get size of splash image (err=%d)\n", res);
378 return res;
379 }
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200380
Leo Ruanefc20c62019-02-08 10:51:36 +0100381 /* Read in the splash data */
382 location->offset = external_splash_addr;
383 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
384 if (res < 0)
385 return res;
386 }
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200387
388 return 0;
389}
390#endif /* CONFIG_FIT */
391
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200392/**
393 * splash_source_load - load splash image from a supported location.
394 *
395 * Select a splash image location based on the value of splashsource environment
396 * variable and the board supported splash source locations, and load a
397 * splashimage to the address pointed to by splashimage environment variable.
398 *
399 * @locations: An array of supported splash locations.
400 * @size: Size of splash_locations array.
401 *
402 * @return: 0 on success, negative value on failure.
403 */
404int splash_source_load(struct splash_location *locations, uint size)
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200405{
406 struct splash_location *splash_location;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200407 char *env_splashimage_value;
408 u32 bmp_load_addr;
409
Simon Glass64b723f2017-08-03 12:22:12 -0600410 env_splashimage_value = env_get("splashimage");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200411 if (env_splashimage_value == NULL)
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200412 return -ENOENT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200413
414 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
415 if (bmp_load_addr == 0) {
416 printf("Error: bad splashimage address specified\n");
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200417 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200418 }
419
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200420 splash_location = select_splash_location(locations, size);
421 if (!splash_location)
422 return -EINVAL;
423
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200424 if (splash_location->flags == SPLASH_STORAGE_RAW)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200425 return splash_load_raw(splash_location, bmp_load_addr);
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200426 else if (splash_location->flags == SPLASH_STORAGE_FS)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200427 return splash_load_fs(splash_location, bmp_load_addr);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200428#ifdef CONFIG_FIT
429 else if (splash_location->flags == SPLASH_STORAGE_FIT)
430 return splash_load_fit(splash_location, bmp_load_addr);
431#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200432 return -EINVAL;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200433}