blob: f51ca5ddf37cd8ca3488b134097c21ca508e3dd5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Igor Grinbergf4a40f02014-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 Grinbergf4a40f02014-11-03 11:32:20 +02006 */
7
8#include <common.h>
Igor Grinbergf4a40f02014-11-03 11:32:20 +02009#include <bmp_layout.h>
Simon Glass288b29e2019-11-14 12:57:43 -070010#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
tomas.melin@vaisala.com7583f1f2017-01-13 13:20:13 +020012#include <errno.h>
Nikita Kiryanov870dd302015-10-29 11:54:41 +020013#include <fs.h>
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +020014#include <fdt_support.h>
tomas.melin@vaisala.com7583f1f2017-01-13 13:20:13 +020015#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
tomas.melin@vaisala.com7583f1f2017-01-13 13:20:13 +020017#include <nand.h>
18#include <sata.h>
19#include <spi.h>
20#include <spi_flash.h>
21#include <splash.h>
22#include <usb.h>
Igor Grinbergf4a40f02014-11-03 11:32:20 +020023
24DECLARE_GLOBAL_DATA_PTR;
25
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020026#ifdef CONFIG_SPI_FLASH
27static struct spi_flash *sf;
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020028static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020029{
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
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020042static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020043{
44 debug("%s: sf support not available\n", __func__);
45 return -ENOSYS;
46}
47#endif
48
Igor Grinbergf4a40f02014-11-03 11:32:20 +020049#ifdef CONFIG_CMD_NAND
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020050static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020051{
Grygorii Strashkoedba8cc2017-06-26 19:12:56 -050052 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
53 return nand_read_skip_bad(mtd, offset,
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020054 &read_size, NULL,
Grygorii Strashkoedba8cc2017-06-26 19:12:56 -050055 mtd->size,
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020056 (u_char *)bmp_load_addr);
57}
58#else
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020059static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020060{
61 debug("%s: nand support not available\n", __func__);
62 return -ENOSYS;
63}
64#endif
65
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020066static int splash_storage_read_raw(struct splash_location *location,
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020067 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020068{
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020069 u32 offset;
70
71 if (!location)
72 return -EINVAL;
73
74 offset = location->offset;
75 switch (location->storage) {
76 case SPLASH_STORAGE_NAND:
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020077 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020078 case SPLASH_STORAGE_SF:
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020079 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020080 default:
81 printf("Unknown splash location\n");
82 }
83
84 return -EINVAL;
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020085}
86
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020087static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinbergf4a40f02014-11-03 11:32:20 +020088{
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
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020096 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
Igor Grinbergf4a40f02014-11-03 11:32:20 +020097 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
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +0200106 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200107
108splash_address_too_high:
Nikita Kiryanovf82eb2f2015-01-14 10:42:54 +0200109 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200110
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200111 return -EFAULT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200112}
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200113
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200114static 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;
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200122 case SPLASH_STORAGE_USB:
123 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
124 break;
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200125 case SPLASH_STORAGE_SATA:
126 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
127 break;
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300128 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;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200134 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
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200145#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
Alexey Brodkind7b60fb2016-07-01 22:47:36 +0300154#ifndef CONFIG_DM_USB
155 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
156#endif
157
158 return err;
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200159}
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
Simon Glass10e40d52017-06-14 21:28:25 -0600168#ifdef CONFIG_SATA
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200169static int splash_init_sata(void)
170{
Simon Glassf19f1ec2017-07-29 11:35:13 -0600171 return sata_probe(0);
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200172}
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
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300181#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
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200216#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
217
218static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
219{
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200220 int res = 0;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200221 loff_t bmp_size;
Jonathan Golder3cc6e702017-02-24 17:46:10 +0100222 loff_t actread;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200223 char *splash_file;
224
Simon Glass00caae62017-08-03 12:22:12 -0600225 splash_file = env_get("splashfile");
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200226 if (!splash_file)
227 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
228
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200229 if (location->storage == SPLASH_STORAGE_USB)
230 res = splash_init_usb();
231
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200232 if (location->storage == SPLASH_STORAGE_SATA)
233 res = splash_init_sata();
234
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300235 if (location->ubivol != NULL)
236 res = splash_mount_ubifs(location);
237
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200238 if (res)
239 return res;
240
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200241 res = splash_select_fs_dev(location);
242 if (res)
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300243 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200244
245 res = fs_size(splash_file, &bmp_size);
246 if (res) {
247 printf("Error (%d): cannot determine file size\n", res);
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300248 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200249 }
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");
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300253 res = -EFAULT;
254 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200255 }
256
257 splash_select_fs_dev(location);
Jonathan Golder3cc6e702017-02-24 17:46:10 +0100258 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300259
260out:
261 if (location->ubivol != NULL)
262 splash_umount_ubifs();
263
264 return res;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200265}
266
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200267/**
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)
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200284{
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200285 int i;
286 char *env_splashsource;
287
288 if (!locations || size == 0)
289 return NULL;
290
Simon Glass00caae62017-08-03 12:22:12 -0600291 env_splashsource = env_get("splashsource");
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200292 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
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200304#ifdef CONFIG_FIT
305static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
306{
307 int res;
308 int node_offset;
Leo Ruan3d92f312019-02-08 10:51:35 +0100309 const char *splash_file;
Leo Ruand5006832019-02-08 10:51:36 +0100310 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;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200315 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;
Niko Maunoa7126ed2017-08-03 09:53:24 +0300326 if (image_get_magic(img_header) != FDT_MAGIC) {
327 printf("Could not find FDT magic\n");
328 return -EINVAL;
329 }
330
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200331 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
Leo Ruan3d92f312019-02-08 10:51:35 +0100345 /* 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);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200351 if (node_offset < 0) {
352 debug("Could not find splash image '%s' in FIT\n",
Leo Ruan3d92f312019-02-08 10:51:35 +0100353 splash_file);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200354 return -ENOENT;
355 }
356
Leo Ruand5006832019-02-08 10:51:36 +0100357 /* 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;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200374 }
375
Leo Ruand5006832019-02-08 10:51:36 +0100376 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;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200388 }
389
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200390 return 0;
391}
392#endif /* CONFIG_FIT */
393
Nikita Kiryanovf82eb2f2015-01-14 10:42:54 +0200394/**
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)
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200407{
408 struct splash_location *splash_location;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200409 char *env_splashimage_value;
410 u32 bmp_load_addr;
411
Simon Glass00caae62017-08-03 12:22:12 -0600412 env_splashimage_value = env_get("splashimage");
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200413 if (env_splashimage_value == NULL)
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200414 return -ENOENT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200415
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");
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200419 return -EFAULT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200420 }
421
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200422 splash_location = select_splash_location(locations, size);
423 if (!splash_location)
424 return -EINVAL;
425
tomas.melin@vaisala.com3b593f92016-11-16 13:02:32 +0200426 if (splash_location->flags == SPLASH_STORAGE_RAW)
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200427 return splash_load_raw(splash_location, bmp_load_addr);
tomas.melin@vaisala.com3b593f92016-11-16 13:02:32 +0200428 else if (splash_location->flags == SPLASH_STORAGE_FS)
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200429 return splash_load_fs(splash_location, bmp_load_addr);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200430#ifdef CONFIG_FIT
431 else if (splash_location->flags == SPLASH_STORAGE_FIT)
432 return splash_load_fit(splash_location, bmp_load_addr);
433#endif
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200434 return -EINVAL;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200435}