blob: e1e73dbdc578befb2b9a4e86e0334463fba085b1 [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>
Nikita Kiryanovf58c5092015-01-14 10:42:49 +020010#include <errno.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020011#include <fs.h>
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +020012#include <fdt_support.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020013#include <image.h>
14#include <nand.h>
15#include <sata.h>
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020016#include <spi.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020017#include <spi_flash.h>
18#include <splash.h>
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +020019#include <usb.h>
Igor Grinberg86ec16b2014-11-03 11:32:20 +020020
21DECLARE_GLOBAL_DATA_PTR;
22
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020023#ifdef CONFIG_SPI_FLASH
24static struct spi_flash *sf;
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020025static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020026{
27 if (!sf) {
28 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
29 CONFIG_SF_DEFAULT_CS,
30 CONFIG_SF_DEFAULT_SPEED,
31 CONFIG_SF_DEFAULT_MODE);
32 if (!sf)
33 return -ENODEV;
34 }
35
36 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
37}
38#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020039static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020040{
41 debug("%s: sf support not available\n", __func__);
42 return -ENOSYS;
43}
44#endif
45
Igor Grinberg86ec16b2014-11-03 11:32:20 +020046#ifdef CONFIG_CMD_NAND
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020047static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020048{
Grygorii Strashko5f33b822017-06-26 19:12:56 -050049 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
50 return nand_read_skip_bad(mtd, offset,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020051 &read_size, NULL,
Grygorii Strashko5f33b822017-06-26 19:12:56 -050052 mtd->size,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020053 (u_char *)bmp_load_addr);
54}
55#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020056static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020057{
58 debug("%s: nand support not available\n", __func__);
59 return -ENOSYS;
60}
61#endif
62
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020063static int splash_storage_read_raw(struct splash_location *location,
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020064 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020065{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020066 u32 offset;
67
68 if (!location)
69 return -EINVAL;
70
71 offset = location->offset;
72 switch (location->storage) {
73 case SPLASH_STORAGE_NAND:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020074 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020075 case SPLASH_STORAGE_SF:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020076 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020077 default:
78 printf("Unknown splash location\n");
79 }
80
81 return -EINVAL;
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020082}
83
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020084static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinberg86ec16b2014-11-03 11:32:20 +020085{
86 struct bmp_header *bmp_hdr;
87 int res;
88 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
89
90 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
91 goto splash_address_too_high;
92
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020093 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +020094 if (res < 0)
95 return res;
96
97 bmp_hdr = (struct bmp_header *)bmp_load_addr;
98 bmp_size = le32_to_cpu(bmp_hdr->file_size);
99
100 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
101 goto splash_address_too_high;
102
Nikita Kiryanovb8183de2015-10-29 11:54:40 +0200103 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200104
105splash_address_too_high:
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200106 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200107
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200108 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200109}
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200110
Nikita Kiryanov74282712015-10-29 11:54:41 +0200111static int splash_select_fs_dev(struct splash_location *location)
112{
113 int res;
114
115 switch (location->storage) {
116 case SPLASH_STORAGE_MMC:
117 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
118 break;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200119 case SPLASH_STORAGE_USB:
120 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
121 break;
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200122 case SPLASH_STORAGE_SATA:
123 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
124 break;
Eran Matityahu07a04a12016-06-07 10:38:37 +0300125 case SPLASH_STORAGE_NAND:
126 if (location->ubivol != NULL)
127 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
128 else
129 res = -ENODEV;
130 break;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200131 default:
132 printf("Error: unsupported location storage.\n");
133 return -ENODEV;
134 }
135
136 if (res)
137 printf("Error: could not access storage.\n");
138
139 return res;
140}
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200141
142#ifdef CONFIG_USB_STORAGE
143static int splash_init_usb(void)
144{
145 int err;
146
147 err = usb_init();
148 if (err)
149 return err;
150
Alexey Brodkinbfcdf3f2016-07-01 22:47:36 +0300151#ifndef CONFIG_DM_USB
152 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
153#endif
154
155 return err;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200156}
157#else
158static inline int splash_init_usb(void)
159{
160 printf("Cannot load splash image: no USB support\n");
161 return -ENOSYS;
162}
163#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200164
Simon Glassab3055a2017-06-14 21:28:25 -0600165#ifdef CONFIG_SATA
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200166static int splash_init_sata(void)
167{
Simon Glass6c857ad2017-07-29 11:35:13 -0600168 return sata_probe(0);
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200169}
170#else
171static inline int splash_init_sata(void)
172{
173 printf("Cannot load splash image: no SATA support\n");
174 return -ENOSYS;
175}
176#endif
177
Eran Matityahu07a04a12016-06-07 10:38:37 +0300178#ifdef CONFIG_CMD_UBIFS
179static int splash_mount_ubifs(struct splash_location *location)
180{
181 int res;
182 char cmd[32];
183
184 sprintf(cmd, "ubi part %s", location->mtdpart);
185 res = run_command(cmd, 0);
186 if (res)
187 return res;
188
189 sprintf(cmd, "ubifsmount %s", location->ubivol);
190 res = run_command(cmd, 0);
191
192 return res;
193}
194
195static inline int splash_umount_ubifs(void)
196{
197 return run_command("ubifsumount", 0);
198}
199#else
200static inline int splash_mount_ubifs(struct splash_location *location)
201{
202 printf("Cannot load splash image: no UBIFS support\n");
203 return -ENOSYS;
204}
205
206static inline int splash_umount_ubifs(void)
207{
208 printf("Cannot unmount UBIFS: no UBIFS support\n");
209 return -ENOSYS;
210}
211#endif
212
Nikita Kiryanov74282712015-10-29 11:54:41 +0200213#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
214
215static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
216{
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200217 int res = 0;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200218 loff_t bmp_size;
Jonathan Golder26b61312017-02-24 17:46:10 +0100219 loff_t actread;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200220 char *splash_file;
221
Simon Glass64b723f2017-08-03 12:22:12 -0600222 splash_file = env_get("splashfile");
Nikita Kiryanov74282712015-10-29 11:54:41 +0200223 if (!splash_file)
224 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
225
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200226 if (location->storage == SPLASH_STORAGE_USB)
227 res = splash_init_usb();
228
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200229 if (location->storage == SPLASH_STORAGE_SATA)
230 res = splash_init_sata();
231
Eran Matityahu07a04a12016-06-07 10:38:37 +0300232 if (location->ubivol != NULL)
233 res = splash_mount_ubifs(location);
234
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200235 if (res)
236 return res;
237
Nikita Kiryanov74282712015-10-29 11:54:41 +0200238 res = splash_select_fs_dev(location);
239 if (res)
Eran Matityahu07a04a12016-06-07 10:38:37 +0300240 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200241
242 res = fs_size(splash_file, &bmp_size);
243 if (res) {
244 printf("Error (%d): cannot determine file size\n", res);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300245 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200246 }
247
248 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
249 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Eran Matityahu07a04a12016-06-07 10:38:37 +0300250 res = -EFAULT;
251 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200252 }
253
254 splash_select_fs_dev(location);
Jonathan Golder26b61312017-02-24 17:46:10 +0100255 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300256
257out:
258 if (location->ubivol != NULL)
259 splash_umount_ubifs();
260
261 return res;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200262}
263
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200264/**
265 * select_splash_location - return the splash location based on board support
266 * and env variable "splashsource".
267 *
268 * @locations: An array of supported splash locations.
269 * @size: Size of splash_locations array.
270 *
271 * @return: If a null set of splash locations is given, or
272 * splashsource env variable is set to unsupported value
273 * return NULL.
274 * If splashsource env variable is not defined
275 * return the first entry in splash_locations as default.
276 * If splashsource env variable contains a supported value
277 * return the location selected by splashsource.
278 */
279static struct splash_location *select_splash_location(
280 struct splash_location *locations, uint size)
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200281{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200282 int i;
283 char *env_splashsource;
284
285 if (!locations || size == 0)
286 return NULL;
287
Simon Glass64b723f2017-08-03 12:22:12 -0600288 env_splashsource = env_get("splashsource");
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200289 if (env_splashsource == NULL)
290 return &locations[0];
291
292 for (i = 0; i < size; i++) {
293 if (!strcmp(locations[i].name, env_splashsource))
294 return &locations[i];
295 }
296
297 printf("splashsource env variable set to unsupported value\n");
298 return NULL;
299}
300
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200301#ifdef CONFIG_FIT
302static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
303{
304 int res;
305 int node_offset;
Leo Ruane9b74d92019-02-08 10:51:35 +0100306 const char *splash_file;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200307 int splash_offset;
308 int splash_size;
309 struct image_header *img_header;
310 const u32 *fit_header;
311 u32 fit_size;
312 const size_t header_size = sizeof(struct image_header);
313
314 /* Read in image header */
315 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
316 if (res < 0)
317 return res;
318
319 img_header = (struct image_header *)bmp_load_addr;
Niko Mauno7e9e0b52017-08-03 09:53:24 +0300320 if (image_get_magic(img_header) != FDT_MAGIC) {
321 printf("Could not find FDT magic\n");
322 return -EINVAL;
323 }
324
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200325 fit_size = fdt_totalsize(img_header);
326
327 /* Read in entire FIT */
328 fit_header = (const u32 *)(bmp_load_addr + header_size);
329 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
330 if (res < 0)
331 return res;
332
333 res = fit_check_format(fit_header);
334 if (!res) {
335 debug("Could not find valid FIT image\n");
336 return -EINVAL;
337 }
338
Leo Ruane9b74d92019-02-08 10:51:35 +0100339 /* Get the splash image node */
340 splash_file = env_get("splashfile");
341 if (!splash_file)
342 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
343
344 node_offset = fit_image_get_node(fit_header, splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200345 if (node_offset < 0) {
346 debug("Could not find splash image '%s' in FIT\n",
Leo Ruane9b74d92019-02-08 10:51:35 +0100347 splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200348 return -ENOENT;
349 }
350
351 res = fit_image_get_data_offset(fit_header, node_offset,
352 &splash_offset);
353 if (res < 0) {
354 printf("Failed to load splash image (err=%d)\n", res);
355 return res;
356 }
357
358 res = fit_image_get_data_size(fit_header, node_offset, &splash_size);
359 if (res < 0) {
360 printf("Failed to load splash image (err=%d)\n", res);
361 return res;
362 }
363
364 /* Align data offset to 4-byte boundrary */
365 fit_size = fdt_totalsize(fit_header);
366 fit_size = (fit_size + 3) & ~3;
367
368 /* Read in the splash data */
369 location->offset = (location->offset + fit_size + splash_offset);
370 res = splash_storage_read_raw(location, bmp_load_addr , splash_size);
371 if (res < 0)
372 return res;
373
374 return 0;
375}
376#endif /* CONFIG_FIT */
377
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200378/**
379 * splash_source_load - load splash image from a supported location.
380 *
381 * Select a splash image location based on the value of splashsource environment
382 * variable and the board supported splash source locations, and load a
383 * splashimage to the address pointed to by splashimage environment variable.
384 *
385 * @locations: An array of supported splash locations.
386 * @size: Size of splash_locations array.
387 *
388 * @return: 0 on success, negative value on failure.
389 */
390int splash_source_load(struct splash_location *locations, uint size)
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200391{
392 struct splash_location *splash_location;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200393 char *env_splashimage_value;
394 u32 bmp_load_addr;
395
Simon Glass64b723f2017-08-03 12:22:12 -0600396 env_splashimage_value = env_get("splashimage");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200397 if (env_splashimage_value == NULL)
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200398 return -ENOENT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200399
400 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
401 if (bmp_load_addr == 0) {
402 printf("Error: bad splashimage address specified\n");
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200403 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200404 }
405
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200406 splash_location = select_splash_location(locations, size);
407 if (!splash_location)
408 return -EINVAL;
409
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200410 if (splash_location->flags == SPLASH_STORAGE_RAW)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200411 return splash_load_raw(splash_location, bmp_load_addr);
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200412 else if (splash_location->flags == SPLASH_STORAGE_FS)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200413 return splash_load_fs(splash_location, bmp_load_addr);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200414#ifdef CONFIG_FIT
415 else if (splash_location->flags == SPLASH_STORAGE_FIT)
416 return splash_load_fit(splash_location, bmp_load_addr);
417#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200418 return -EINVAL;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200419}