blob: 3cf926d91a00beafb06f15a13fcbd867128cab37 [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 Glassadaaa482019-11-14 12:57:43 -070010#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Nikita Kiryanovf58c5092015-01-14 10:42:49 +020012#include <errno.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020013#include <fs.h>
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +020014#include <fdt_support.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020015#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020017#include <nand.h>
18#include <sata.h>
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020019#include <spi.h>
tomas.melin@vaisala.com0a7c7322017-01-13 13:20:13 +020020#include <spi_flash.h>
21#include <splash.h>
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +020022#include <usb.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Igor Grinberg86ec16b2014-11-03 11:32:20 +020024
25DECLARE_GLOBAL_DATA_PTR;
26
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020027#ifdef CONFIG_SPI_FLASH
28static struct spi_flash *sf;
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020029static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020030{
31 if (!sf) {
32 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
33 CONFIG_SF_DEFAULT_CS,
34 CONFIG_SF_DEFAULT_SPEED,
35 CONFIG_SF_DEFAULT_MODE);
36 if (!sf)
37 return -ENODEV;
38 }
39
Jaehoon Chung6c0a6322020-12-07 17:18:51 +090040 return spi_flash_read(sf, offset, read_size, (void *)(uintptr_t)bmp_load_addr);
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020041}
42#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020043static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020044{
45 debug("%s: sf support not available\n", __func__);
46 return -ENOSYS;
47}
48#endif
49
Igor Grinberg86ec16b2014-11-03 11:32:20 +020050#ifdef CONFIG_CMD_NAND
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020051static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020052{
Grygorii Strashko5f33b822017-06-26 19:12:56 -050053 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
54 return nand_read_skip_bad(mtd, offset,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020055 &read_size, NULL,
Grygorii Strashko5f33b822017-06-26 19:12:56 -050056 mtd->size,
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020057 (u_char *)bmp_load_addr);
58}
59#else
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020060static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020061{
62 debug("%s: nand support not available\n", __func__);
63 return -ENOSYS;
64}
65#endif
66
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020067static int splash_storage_read_raw(struct splash_location *location,
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020068 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020069{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020070 u32 offset;
71
72 if (!location)
73 return -EINVAL;
74
75 offset = location->offset;
76 switch (location->storage) {
77 case SPLASH_STORAGE_NAND:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020078 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanov1ab4c762015-01-14 10:42:52 +020079 case SPLASH_STORAGE_SF:
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020080 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020081 default:
82 printf("Unknown splash location\n");
83 }
84
85 return -EINVAL;
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020086}
87
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020088static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinberg86ec16b2014-11-03 11:32:20 +020089{
90 struct bmp_header *bmp_hdr;
91 int res;
92 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
93
94 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
95 goto splash_address_too_high;
96
Nikita Kiryanovb8183de2015-10-29 11:54:40 +020097 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +020098 if (res < 0)
99 return res;
100
Jaehoon Chung6c0a6322020-12-07 17:18:51 +0900101 bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200102 bmp_size = le32_to_cpu(bmp_hdr->file_size);
103
104 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
105 goto splash_address_too_high;
106
Nikita Kiryanovb8183de2015-10-29 11:54:40 +0200107 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200108
109splash_address_too_high:
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200110 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200111
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200112 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200113}
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200114
Nikita Kiryanov74282712015-10-29 11:54:41 +0200115static int splash_select_fs_dev(struct splash_location *location)
116{
117 int res;
118
119 switch (location->storage) {
120 case SPLASH_STORAGE_MMC:
121 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
122 break;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200123 case SPLASH_STORAGE_USB:
124 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
125 break;
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200126 case SPLASH_STORAGE_SATA:
127 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
128 break;
Eran Matityahu07a04a12016-06-07 10:38:37 +0300129 case SPLASH_STORAGE_NAND:
130 if (location->ubivol != NULL)
131 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
132 else
133 res = -ENODEV;
134 break;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200135 default:
136 printf("Error: unsupported location storage.\n");
137 return -ENODEV;
138 }
139
140 if (res)
141 printf("Error: could not access storage.\n");
142
143 return res;
144}
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200145
146#ifdef CONFIG_USB_STORAGE
147static int splash_init_usb(void)
148{
149 int err;
150
151 err = usb_init();
152 if (err)
153 return err;
154
Alexey Brodkinbfcdf3f2016-07-01 22:47:36 +0300155#ifndef CONFIG_DM_USB
156 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
157#endif
158
159 return err;
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200160}
161#else
162static inline int splash_init_usb(void)
163{
164 printf("Cannot load splash image: no USB support\n");
165 return -ENOSYS;
166}
167#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200168
Simon Glassab3055a2017-06-14 21:28:25 -0600169#ifdef CONFIG_SATA
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200170static int splash_init_sata(void)
171{
Simon Glass6c857ad2017-07-29 11:35:13 -0600172 return sata_probe(0);
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200173}
174#else
175static inline int splash_init_sata(void)
176{
177 printf("Cannot load splash image: no SATA support\n");
178 return -ENOSYS;
179}
180#endif
181
Eran Matityahu07a04a12016-06-07 10:38:37 +0300182#ifdef CONFIG_CMD_UBIFS
183static int splash_mount_ubifs(struct splash_location *location)
184{
185 int res;
186 char cmd[32];
187
188 sprintf(cmd, "ubi part %s", location->mtdpart);
189 res = run_command(cmd, 0);
190 if (res)
191 return res;
192
193 sprintf(cmd, "ubifsmount %s", location->ubivol);
194 res = run_command(cmd, 0);
195
196 return res;
197}
198
199static inline int splash_umount_ubifs(void)
200{
201 return run_command("ubifsumount", 0);
202}
203#else
204static inline int splash_mount_ubifs(struct splash_location *location)
205{
206 printf("Cannot load splash image: no UBIFS support\n");
207 return -ENOSYS;
208}
209
210static inline int splash_umount_ubifs(void)
211{
212 printf("Cannot unmount UBIFS: no UBIFS support\n");
213 return -ENOSYS;
214}
215#endif
216
Nikita Kiryanov74282712015-10-29 11:54:41 +0200217#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
218
219static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
220{
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200221 int res = 0;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200222 loff_t bmp_size;
Jonathan Golder26b61312017-02-24 17:46:10 +0100223 loff_t actread;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200224 char *splash_file;
225
Simon Glass64b723f2017-08-03 12:22:12 -0600226 splash_file = env_get("splashfile");
Nikita Kiryanov74282712015-10-29 11:54:41 +0200227 if (!splash_file)
228 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
229
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200230 if (location->storage == SPLASH_STORAGE_USB)
231 res = splash_init_usb();
232
Nikita Kiryanov80ca30d2015-10-29 11:54:43 +0200233 if (location->storage == SPLASH_STORAGE_SATA)
234 res = splash_init_sata();
235
Eran Matityahu07a04a12016-06-07 10:38:37 +0300236 if (location->ubivol != NULL)
237 res = splash_mount_ubifs(location);
238
Nikita Kiryanovbf5c3cc2015-10-29 11:54:42 +0200239 if (res)
240 return res;
241
Nikita Kiryanov74282712015-10-29 11:54:41 +0200242 res = splash_select_fs_dev(location);
243 if (res)
Eran Matityahu07a04a12016-06-07 10:38:37 +0300244 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200245
246 res = fs_size(splash_file, &bmp_size);
247 if (res) {
248 printf("Error (%d): cannot determine file size\n", res);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300249 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200250 }
251
252 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
253 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Eran Matityahu07a04a12016-06-07 10:38:37 +0300254 res = -EFAULT;
255 goto out;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200256 }
257
258 splash_select_fs_dev(location);
Jonathan Golder26b61312017-02-24 17:46:10 +0100259 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
Eran Matityahu07a04a12016-06-07 10:38:37 +0300260
261out:
262 if (location->ubivol != NULL)
263 splash_umount_ubifs();
264
265 return res;
Nikita Kiryanov74282712015-10-29 11:54:41 +0200266}
267
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200268/**
269 * select_splash_location - return the splash location based on board support
270 * and env variable "splashsource".
271 *
272 * @locations: An array of supported splash locations.
273 * @size: Size of splash_locations array.
274 *
275 * @return: If a null set of splash locations is given, or
276 * splashsource env variable is set to unsupported value
277 * return NULL.
278 * If splashsource env variable is not defined
279 * return the first entry in splash_locations as default.
280 * If splashsource env variable contains a supported value
281 * return the location selected by splashsource.
282 */
283static struct splash_location *select_splash_location(
284 struct splash_location *locations, uint size)
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200285{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200286 int i;
287 char *env_splashsource;
288
289 if (!locations || size == 0)
290 return NULL;
291
Simon Glass64b723f2017-08-03 12:22:12 -0600292 env_splashsource = env_get("splashsource");
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200293 if (env_splashsource == NULL)
294 return &locations[0];
295
296 for (i = 0; i < size; i++) {
297 if (!strcmp(locations[i].name, env_splashsource))
298 return &locations[i];
299 }
300
301 printf("splashsource env variable set to unsupported value\n");
302 return NULL;
303}
304
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200305#ifdef CONFIG_FIT
306static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
307{
308 int res;
309 int node_offset;
Leo Ruane9b74d92019-02-08 10:51:35 +0100310 const char *splash_file;
Leo Ruanefc20c62019-02-08 10:51:36 +0100311 const void *internal_splash_data;
312 size_t internal_splash_size;
313 int external_splash_addr;
314 int external_splash_size;
315 bool is_splash_external = false;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200316 struct image_header *img_header;
317 const u32 *fit_header;
318 u32 fit_size;
319 const size_t header_size = sizeof(struct image_header);
320
321 /* Read in image header */
322 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
323 if (res < 0)
324 return res;
325
326 img_header = (struct image_header *)bmp_load_addr;
Niko Mauno7e9e0b52017-08-03 09:53:24 +0300327 if (image_get_magic(img_header) != FDT_MAGIC) {
328 printf("Could not find FDT magic\n");
329 return -EINVAL;
330 }
331
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200332 fit_size = fdt_totalsize(img_header);
333
334 /* Read in entire FIT */
335 fit_header = (const u32 *)(bmp_load_addr + header_size);
336 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
337 if (res < 0)
338 return res;
339
Simon Glassd563c252021-02-15 17:08:09 -0700340 res = fit_check_format(fit_header, IMAGE_SIZE_INVAL);
341 if (res) {
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200342 debug("Could not find valid FIT image\n");
Simon Glassd563c252021-02-15 17:08:09 -0700343 return res;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200344 }
345
Leo Ruane9b74d92019-02-08 10:51:35 +0100346 /* Get the splash image node */
347 splash_file = env_get("splashfile");
348 if (!splash_file)
349 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
350
351 node_offset = fit_image_get_node(fit_header, splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200352 if (node_offset < 0) {
353 debug("Could not find splash image '%s' in FIT\n",
Leo Ruane9b74d92019-02-08 10:51:35 +0100354 splash_file);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200355 return -ENOENT;
356 }
357
Leo Ruanefc20c62019-02-08 10:51:36 +0100358 /* Extract the splash data from FIT */
359 /* 1. Test if splash is in FIT internal data. */
360 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
361 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
362 /* 2. Test if splash is in FIT external data with fixed position. */
363 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
364 is_splash_external = true;
365 /* 3. Test if splash is in FIT external data with offset. */
366 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
367 /* Align data offset to 4-byte boundary */
368 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
369 /* External splash offset means the offset by end of FIT header */
370 external_splash_addr += location->offset + fit_size;
371 is_splash_external = true;
372 } else {
373 printf("Failed to get splash image from FIT\n");
374 return -ENODATA;
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200375 }
376
Leo Ruanefc20c62019-02-08 10:51:36 +0100377 if (is_splash_external) {
378 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
379 if (res < 0) {
380 printf("Failed to get size of splash image (err=%d)\n", res);
381 return res;
382 }
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200383
Leo Ruanefc20c62019-02-08 10:51:36 +0100384 /* Read in the splash data */
385 location->offset = external_splash_addr;
386 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
387 if (res < 0)
388 return res;
389 }
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200390
391 return 0;
392}
393#endif /* CONFIG_FIT */
394
Nikita Kiryanov7f9ceea2015-01-14 10:42:54 +0200395/**
396 * splash_source_load - load splash image from a supported location.
397 *
398 * Select a splash image location based on the value of splashsource environment
399 * variable and the board supported splash source locations, and load a
400 * splashimage to the address pointed to by splashimage environment variable.
401 *
402 * @locations: An array of supported splash locations.
403 * @size: Size of splash_locations array.
404 *
405 * @return: 0 on success, negative value on failure.
406 */
407int splash_source_load(struct splash_location *locations, uint size)
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200408{
409 struct splash_location *splash_location;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200410 char *env_splashimage_value;
411 u32 bmp_load_addr;
412
Simon Glass64b723f2017-08-03 12:22:12 -0600413 env_splashimage_value = env_get("splashimage");
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200414 if (env_splashimage_value == NULL)
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200415 return -ENOENT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200416
417 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
418 if (bmp_load_addr == 0) {
419 printf("Error: bad splashimage address specified\n");
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200420 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200421 }
422
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200423 splash_location = select_splash_location(locations, size);
424 if (!splash_location)
425 return -EINVAL;
426
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200427 if (splash_location->flags == SPLASH_STORAGE_RAW)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200428 return splash_load_raw(splash_location, bmp_load_addr);
tomas.melin@vaisala.comf4650ea2016-11-16 13:02:32 +0200429 else if (splash_location->flags == SPLASH_STORAGE_FS)
Nikita Kiryanov74282712015-10-29 11:54:41 +0200430 return splash_load_fs(splash_location, bmp_load_addr);
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200431#ifdef CONFIG_FIT
432 else if (splash_location->flags == SPLASH_STORAGE_FIT)
433 return splash_load_fit(splash_location, bmp_load_addr);
434#endif
Nikita Kiryanov74282712015-10-29 11:54:41 +0200435 return -EINVAL;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200436}