]> Git Repo - J-u-boot.git/blame - common/splash_source.c
common: Drop init.h from common header
[J-u-boot.git] / common / splash_source.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
f4a40f05
IG
2/*
3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 *
5 * Authors: Igor Grinberg <[email protected]>
f4a40f05
IG
6 */
7
8#include <common.h>
7583f1f5 9#include <bmp_layout.h>
288b29e4 10#include <command.h>
7b51b576 11#include <env.h>
6947e3f5 12#include <errno.h>
7583f1f5 13#include <fs.h>
db1b79b8 14#include <fdt_support.h>
7583f1f5 15#include <image.h>
16#include <nand.h>
17#include <sata.h>
7e8d7f2a 18#include <spi.h>
7583f1f5 19#include <spi_flash.h>
20#include <splash.h>
9bb4e947 21#include <usb.h>
f4a40f05
IG
22
23DECLARE_GLOBAL_DATA_PTR;
24
7e8d7f2a
NK
25#ifdef CONFIG_SPI_FLASH
26static struct spi_flash *sf;
bcbb6448 27static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
28{
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
bcbb6448 41static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7e8d7f2a
NK
42{
43 debug("%s: sf support not available\n", __func__);
44 return -ENOSYS;
45}
46#endif
47
f4a40f05 48#ifdef CONFIG_CMD_NAND
bcbb6448 49static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c 50{
edba8cc4
GS
51 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
52 return nand_read_skip_bad(mtd, offset,
7be4cd2c 53 &read_size, NULL,
edba8cc4 54 mtd->size,
7be4cd2c
NK
55 (u_char *)bmp_load_addr);
56}
57#else
bcbb6448 58static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
7be4cd2c
NK
59{
60 debug("%s: nand support not available\n", __func__);
61 return -ENOSYS;
62}
63#endif
64
bcbb6448 65static int splash_storage_read_raw(struct splash_location *location,
fd29dd55 66 u32 bmp_load_addr, size_t read_size)
7be4cd2c 67{
fd29dd55
NK
68 u32 offset;
69
70 if (!location)
71 return -EINVAL;
72
73 offset = location->offset;
74 switch (location->storage) {
75 case SPLASH_STORAGE_NAND:
bcbb6448 76 return splash_nand_read_raw(bmp_load_addr, offset, read_size);
7e8d7f2a 77 case SPLASH_STORAGE_SF:
bcbb6448 78 return splash_sf_read_raw(bmp_load_addr, offset, read_size);
fd29dd55
NK
79 default:
80 printf("Unknown splash location\n");
81 }
82
83 return -EINVAL;
7be4cd2c
NK
84}
85
fd29dd55 86static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
f4a40f05
IG
87{
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
bcbb6448 95 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
f4a40f05
IG
96 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
bcbb6448 105 return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
f4a40f05
IG
106
107splash_address_too_high:
f82eb2fa 108 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
f4a40f05 109
6947e3f5 110 return -EFAULT;
f4a40f05 111}
f4a40f05 112
870dd309
NK
113static 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;
9bb4e947
NK
121 case SPLASH_STORAGE_USB:
122 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
123 break;
50c2d2e1
NK
124 case SPLASH_STORAGE_SATA:
125 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
126 break;
1cb075c6
EM
127 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;
870dd309
NK
133 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
9bb4e947
NK
144#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
d7b60fbf
AB
153#ifndef CONFIG_DM_USB
154 err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
155#endif
156
157 return err;
9bb4e947
NK
158}
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
10e40d54 167#ifdef CONFIG_SATA
50c2d2e1
NK
168static int splash_init_sata(void)
169{
f19f1ecb 170 return sata_probe(0);
50c2d2e1
NK
171}
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
1cb075c6
EM
180#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
870dd309
NK
215#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
216
217static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
218{
9bb4e947 219 int res = 0;
870dd309 220 loff_t bmp_size;
3cc6e707 221 loff_t actread;
870dd309
NK
222 char *splash_file;
223
00caae6d 224 splash_file = env_get("splashfile");
870dd309
NK
225 if (!splash_file)
226 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
227
9bb4e947
NK
228 if (location->storage == SPLASH_STORAGE_USB)
229 res = splash_init_usb();
230
50c2d2e1
NK
231 if (location->storage == SPLASH_STORAGE_SATA)
232 res = splash_init_sata();
233
1cb075c6
EM
234 if (location->ubivol != NULL)
235 res = splash_mount_ubifs(location);
236
9bb4e947
NK
237 if (res)
238 return res;
239
870dd309
NK
240 res = splash_select_fs_dev(location);
241 if (res)
1cb075c6 242 goto out;
870dd309
NK
243
244 res = fs_size(splash_file, &bmp_size);
245 if (res) {
246 printf("Error (%d): cannot determine file size\n", res);
1cb075c6 247 goto out;
870dd309
NK
248 }
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");
1cb075c6
EM
252 res = -EFAULT;
253 goto out;
870dd309
NK
254 }
255
256 splash_select_fs_dev(location);
3cc6e707 257 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
1cb075c6
EM
258
259out:
260 if (location->ubivol != NULL)
261 splash_umount_ubifs();
262
263 return res;
870dd309
NK
264}
265
fd29dd55
NK
266/**
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)
283{
284 int i;
285 char *env_splashsource;
286
287 if (!locations || size == 0)
288 return NULL;
289
00caae6d 290 env_splashsource = env_get("splashsource");
fd29dd55
NK
291 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
db1b79b8 303#ifdef CONFIG_FIT
304static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
305{
306 int res;
307 int node_offset;
3d92f317 308 const char *splash_file;
d5006836
LR
309 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;
db1b79b8 314 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;
a7126edc
NM
325 if (image_get_magic(img_header) != FDT_MAGIC) {
326 printf("Could not find FDT magic\n");
327 return -EINVAL;
328 }
329
db1b79b8 330 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
3d92f317
LR
344 /* 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);
db1b79b8 350 if (node_offset < 0) {
351 debug("Could not find splash image '%s' in FIT\n",
3d92f317 352 splash_file);
db1b79b8 353 return -ENOENT;
354 }
355
d5006836
LR
356 /* 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;
db1b79b8 373 }
374
d5006836
LR
375 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;
db1b79b8 387 }
388
db1b79b8 389 return 0;
390}
391#endif /* CONFIG_FIT */
392
f82eb2fa
NK
393/**
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)
f4a40f05 406{
fd29dd55 407 struct splash_location *splash_location;
f4a40f05
IG
408 char *env_splashimage_value;
409 u32 bmp_load_addr;
410
00caae6d 411 env_splashimage_value = env_get("splashimage");
f4a40f05 412 if (env_splashimage_value == NULL)
6947e3f5 413 return -ENOENT;
f4a40f05
IG
414
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");
6947e3f5 418 return -EFAULT;
f4a40f05
IG
419 }
420
fd29dd55
NK
421 splash_location = select_splash_location(locations, size);
422 if (!splash_location)
423 return -EINVAL;
424
3b593f90 425 if (splash_location->flags == SPLASH_STORAGE_RAW)
870dd309 426 return splash_load_raw(splash_location, bmp_load_addr);
3b593f90 427 else if (splash_location->flags == SPLASH_STORAGE_FS)
870dd309 428 return splash_load_fs(splash_location, bmp_load_addr);
db1b79b8 429#ifdef CONFIG_FIT
430 else if (splash_location->flags == SPLASH_STORAGE_FIT)
431 return splash_load_fit(splash_location, bmp_load_addr);
432#endif
870dd309 433 return -EINVAL;
f4a40f05 434}
This page took 0.357807 seconds and 4 git commands to generate.