blob: 2ff15208a70d77acc68e8cba33f1498a75e2f1be [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>
16#include <nand.h>
17#include <sata.h>
18#include <spi.h>
19#include <spi_flash.h>
20#include <splash.h>
21#include <usb.h>
Igor Grinbergf4a40f02014-11-03 11:32:20 +020022
23DECLARE_GLOBAL_DATA_PTR;
24
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020025#ifdef CONFIG_SPI_FLASH
26static struct spi_flash *sf;
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020027static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020028{
29 if (!sf) {
30 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
31 CONFIG_SF_DEFAULT_CS,
32 CONFIG_SF_DEFAULT_SPEED,
33 CONFIG_SF_DEFAULT_MODE);
34 if (!sf)
35 return -ENODEV;
36 }
37
38 return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
39}
40#else
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020041static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020042{
43 debug("%s: sf support not available\n", __func__);
44 return -ENOSYS;
45}
46#endif
47
Igor Grinbergf4a40f02014-11-03 11:32:20 +020048#ifdef CONFIG_CMD_NAND
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020049static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020050{
Grygorii Strashkoedba8cc2017-06-26 19:12:56 -050051 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
52 return nand_read_skip_bad(mtd, offset,
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020053 &read_size, NULL,
Grygorii Strashkoedba8cc2017-06-26 19:12:56 -050054 mtd->size,
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020055 (u_char *)bmp_load_addr);
56}
57#else
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020058static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020059{
60 debug("%s: nand support not available\n", __func__);
61 return -ENOSYS;
62}
63#endif
64
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020065static int splash_storage_read_raw(struct splash_location *location,
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020066 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020067{
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020068 u32 offset;
69
70 if (!location)
71 return -EINVAL;
72
73 offset = location->offset;
74 switch (location->storage) {
75 case SPLASH_STORAGE_NAND:
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020076 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanov7e8d7f22015-01-14 10:42:52 +020077 case SPLASH_STORAGE_SF:
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020078 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020079 default:
80 printf("Unknown splash location\n");
81 }
82
83 return -EINVAL;
Nikita Kiryanov7be4cd22015-01-14 10:42:50 +020084}
85
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +020086static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinbergf4a40f02014-11-03 11:32:20 +020087{
88 struct bmp_header *bmp_hdr;
89 int res;
90 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
91
92 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
93 goto splash_address_too_high;
94
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +020095 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
Igor Grinbergf4a40f02014-11-03 11:32:20 +020096 if (res < 0)
97 return res;
98
99 bmp_hdr = (struct bmp_header *)bmp_load_addr;
100 bmp_size = le32_to_cpu(bmp_hdr->file_size);
101
102 if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
103 goto splash_address_too_high;
104
Nikita Kiryanovbcbb6442015-10-29 11:54:40 +0200105 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200106
107splash_address_too_high:
Nikita Kiryanovf82eb2f2015-01-14 10:42:54 +0200108 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200109
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200110 return -EFAULT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200111}
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200112
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200113static int splash_select_fs_dev(struct splash_location *location)
114{
115 int res;
116
117 switch (location->storage) {
118 case SPLASH_STORAGE_MMC:
119 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
120 break;
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200121 case SPLASH_STORAGE_USB:
122 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
123 break;
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200124 case SPLASH_STORAGE_SATA:
125 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
126 break;
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300127 case SPLASH_STORAGE_NAND:
128 if (location->ubivol != NULL)
129 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
130 else
131 res = -ENODEV;
132 break;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200133 default:
134 printf("Error: unsupported location storage.\n");
135 return -ENODEV;
136 }
137
138 if (res)
139 printf("Error: could not access storage.\n");
140
141 return res;
142}
143
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200144#ifdef CONFIG_USB_STORAGE
145static int splash_init_usb(void)
146{
147 int err;
148
149 err = usb_init();
150 if (err)
151 return err;
152
Alexey Brodkind7b60fb2016-07-01 22:47:36 +0300153#ifndef CONFIG_DM_USB
154 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
155#endif
156
157 return err;
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200158}
159#else
160static inline int splash_init_usb(void)
161{
162 printf("Cannot load splash image: no USB support\n");
163 return -ENOSYS;
164}
165#endif
166
Simon Glass10e40d52017-06-14 21:28:25 -0600167#ifdef CONFIG_SATA
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200168static int splash_init_sata(void)
169{
Simon Glassf19f1ec2017-07-29 11:35:13 -0600170 return sata_probe(0);
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200171}
172#else
173static inline int splash_init_sata(void)
174{
175 printf("Cannot load splash image: no SATA support\n");
176 return -ENOSYS;
177}
178#endif
179
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300180#ifdef CONFIG_CMD_UBIFS
181static int splash_mount_ubifs(struct splash_location *location)
182{
183 int res;
184 char cmd[32];
185
186 sprintf(cmd, "ubi part %s", location->mtdpart);
187 res = run_command(cmd, 0);
188 if (res)
189 return res;
190
191 sprintf(cmd, "ubifsmount %s", location->ubivol);
192 res = run_command(cmd, 0);
193
194 return res;
195}
196
197static inline int splash_umount_ubifs(void)
198{
199 return run_command("ubifsumount", 0);
200}
201#else
202static inline int splash_mount_ubifs(struct splash_location *location)
203{
204 printf("Cannot load splash image: no UBIFS support\n");
205 return -ENOSYS;
206}
207
208static inline int splash_umount_ubifs(void)
209{
210 printf("Cannot unmount UBIFS: no UBIFS support\n");
211 return -ENOSYS;
212}
213#endif
214
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200215#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
216
217static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
218{
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200219 int res = 0;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200220 loff_t bmp_size;
Jonathan Golder3cc6e702017-02-24 17:46:10 +0100221 loff_t actread;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200222 char *splash_file;
223
Simon Glass00caae62017-08-03 12:22:12 -0600224 splash_file = env_get("splashfile");
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200225 if (!splash_file)
226 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
227
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200228 if (location->storage == SPLASH_STORAGE_USB)
229 res = splash_init_usb();
230
Nikita Kiryanov50c2d2e2015-10-29 11:54:43 +0200231 if (location->storage == SPLASH_STORAGE_SATA)
232 res = splash_init_sata();
233
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300234 if (location->ubivol != NULL)
235 res = splash_mount_ubifs(location);
236
Nikita Kiryanov9bb4e942015-10-29 11:54:42 +0200237 if (res)
238 return res;
239
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200240 res = splash_select_fs_dev(location);
241 if (res)
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300242 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200243
244 res = fs_size(splash_file, &bmp_size);
245 if (res) {
246 printf("Error (%d): cannot determine file size\n", res);
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300247 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200248 }
249
250 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
251 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300252 res = -EFAULT;
253 goto out;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200254 }
255
256 splash_select_fs_dev(location);
Jonathan Golder3cc6e702017-02-24 17:46:10 +0100257 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
Eran Matityahu1cb075c2016-06-07 10:38:37 +0300258
259out:
260 if (location->ubivol != NULL)
261 splash_umount_ubifs();
262
263 return res;
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200264}
265
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200266/**
267 * select_splash_location - return the splash location based on board support
268 * and env variable "splashsource".
269 *
270 * @locations: An array of supported splash locations.
271 * @size: Size of splash_locations array.
272 *
273 * @return: If a null set of splash locations is given, or
274 * splashsource env variable is set to unsupported value
275 * return NULL.
276 * If splashsource env variable is not defined
277 * return the first entry in splash_locations as default.
278 * If splashsource env variable contains a supported value
279 * return the location selected by splashsource.
280 */
281static struct splash_location *select_splash_location(
282 struct splash_location *locations, uint size)
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200283{
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200284 int i;
285 char *env_splashsource;
286
287 if (!locations || size == 0)
288 return NULL;
289
Simon Glass00caae62017-08-03 12:22:12 -0600290 env_splashsource = env_get("splashsource");
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200291 if (env_splashsource == NULL)
292 return &locations[0];
293
294 for (i = 0; i < size; i++) {
295 if (!strcmp(locations[i].name, env_splashsource))
296 return &locations[i];
297 }
298
299 printf("splashsource env variable set to unsupported value\n");
300 return NULL;
301}
302
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200303#ifdef CONFIG_FIT
304static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
305{
306 int res;
307 int node_offset;
Leo Ruan3d92f312019-02-08 10:51:35 +0100308 const char *splash_file;
Leo Ruand5006832019-02-08 10:51:36 +0100309 const void *internal_splash_data;
310 size_t internal_splash_size;
311 int external_splash_addr;
312 int external_splash_size;
313 bool is_splash_external = false;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200314 struct image_header *img_header;
315 const u32 *fit_header;
316 u32 fit_size;
317 const size_t header_size = sizeof(struct image_header);
318
319 /* Read in image header */
320 res = splash_storage_read_raw(location, bmp_load_addr, header_size);
321 if (res < 0)
322 return res;
323
324 img_header = (struct image_header *)bmp_load_addr;
Niko Maunoa7126ed2017-08-03 09:53:24 +0300325 if (image_get_magic(img_header) != FDT_MAGIC) {
326 printf("Could not find FDT magic\n");
327 return -EINVAL;
328 }
329
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200330 fit_size = fdt_totalsize(img_header);
331
332 /* Read in entire FIT */
333 fit_header = (const u32 *)(bmp_load_addr + header_size);
334 res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
335 if (res < 0)
336 return res;
337
338 res = fit_check_format(fit_header);
339 if (!res) {
340 debug("Could not find valid FIT image\n");
341 return -EINVAL;
342 }
343
Leo Ruan3d92f312019-02-08 10:51:35 +0100344 /* Get the splash image node */
345 splash_file = env_get("splashfile");
346 if (!splash_file)
347 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
348
349 node_offset = fit_image_get_node(fit_header, splash_file);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200350 if (node_offset < 0) {
351 debug("Could not find splash image '%s' in FIT\n",
Leo Ruan3d92f312019-02-08 10:51:35 +0100352 splash_file);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200353 return -ENOENT;
354 }
355
Leo Ruand5006832019-02-08 10:51:36 +0100356 /* Extract the splash data from FIT */
357 /* 1. Test if splash is in FIT internal data. */
358 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
359 memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
360 /* 2. Test if splash is in FIT external data with fixed position. */
361 else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
362 is_splash_external = true;
363 /* 3. Test if splash is in FIT external data with offset. */
364 else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
365 /* Align data offset to 4-byte boundary */
366 fit_size = ALIGN(fdt_totalsize(fit_header), 4);
367 /* External splash offset means the offset by end of FIT header */
368 external_splash_addr += location->offset + fit_size;
369 is_splash_external = true;
370 } else {
371 printf("Failed to get splash image from FIT\n");
372 return -ENODATA;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200373 }
374
Leo Ruand5006832019-02-08 10:51:36 +0100375 if (is_splash_external) {
376 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
377 if (res < 0) {
378 printf("Failed to get size of splash image (err=%d)\n", res);
379 return res;
380 }
381
382 /* Read in the splash data */
383 location->offset = external_splash_addr;
384 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
385 if (res < 0)
386 return res;
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200387 }
388
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200389 return 0;
390}
391#endif /* CONFIG_FIT */
392
Nikita Kiryanovf82eb2f2015-01-14 10:42:54 +0200393/**
394 * splash_source_load - load splash image from a supported location.
395 *
396 * Select a splash image location based on the value of splashsource environment
397 * variable and the board supported splash source locations, and load a
398 * splashimage to the address pointed to by splashimage environment variable.
399 *
400 * @locations: An array of supported splash locations.
401 * @size: Size of splash_locations array.
402 *
403 * @return: 0 on success, negative value on failure.
404 */
405int splash_source_load(struct splash_location *locations, uint size)
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200406{
407 struct splash_location *splash_location;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200408 char *env_splashimage_value;
409 u32 bmp_load_addr;
410
Simon Glass00caae62017-08-03 12:22:12 -0600411 env_splashimage_value = env_get("splashimage");
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200412 if (env_splashimage_value == NULL)
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200413 return -ENOENT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200414
415 bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
416 if (bmp_load_addr == 0) {
417 printf("Error: bad splashimage address specified\n");
Nikita Kiryanov6947e3f2015-01-14 10:42:49 +0200418 return -EFAULT;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200419 }
420
Nikita Kiryanovfd29dd52015-01-14 10:42:51 +0200421 splash_location = select_splash_location(locations, size);
422 if (!splash_location)
423 return -EINVAL;
424
tomas.melin@vaisala.com3b593f92016-11-16 13:02:32 +0200425 if (splash_location->flags == SPLASH_STORAGE_RAW)
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200426 return splash_load_raw(splash_location, bmp_load_addr);
tomas.melin@vaisala.com3b593f92016-11-16 13:02:32 +0200427 else if (splash_location->flags == SPLASH_STORAGE_FS)
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200428 return splash_load_fs(splash_location, bmp_load_addr);
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200429#ifdef CONFIG_FIT
430 else if (splash_location->flags == SPLASH_STORAGE_FIT)
431 return splash_load_fit(splash_location, bmp_load_addr);
432#endif
Nikita Kiryanov870dd302015-10-29 11:54:41 +0200433 return -EINVAL;
Igor Grinbergf4a40f02014-11-03 11:32:20 +0200434}