2 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
4 * Authors: Igor Grinberg <grinberg@compulab.co.il>
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <bmp_layout.h>
13 #include <fdt_support.h>
18 #include <spi_flash.h>
22 DECLARE_GLOBAL_DATA_PTR
;
24 #ifdef CONFIG_SPI_FLASH
25 static struct spi_flash
*sf
;
26 static int splash_sf_read_raw(u32 bmp_load_addr
, int offset
, size_t read_size
)
29 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
31 CONFIG_SF_DEFAULT_SPEED
,
32 CONFIG_SF_DEFAULT_MODE
);
37 return spi_flash_read(sf
, offset
, read_size
, (void *)bmp_load_addr
);
40 static int splash_sf_read_raw(u32 bmp_load_addr
, int offset
, size_t read_size
)
42 debug("%s: sf support not available\n", __func__
);
47 #ifdef CONFIG_CMD_NAND
48 static int splash_nand_read_raw(u32 bmp_load_addr
, int offset
, size_t read_size
)
50 struct mtd_info
*mtd
= get_nand_dev_by_index(nand_curr_device
);
51 return nand_read_skip_bad(mtd
, offset
,
54 (u_char
*)bmp_load_addr
);
57 static int splash_nand_read_raw(u32 bmp_load_addr
, int offset
, size_t read_size
)
59 debug("%s: nand support not available\n", __func__
);
64 static int splash_storage_read_raw(struct splash_location
*location
,
65 u32 bmp_load_addr
, size_t read_size
)
72 offset
= location
->offset
;
73 switch (location
->storage
) {
74 case SPLASH_STORAGE_NAND
:
75 return splash_nand_read_raw(bmp_load_addr
, offset
, read_size
);
76 case SPLASH_STORAGE_SF
:
77 return splash_sf_read_raw(bmp_load_addr
, offset
, read_size
);
79 printf("Unknown splash location\n");
85 static int splash_load_raw(struct splash_location
*location
, u32 bmp_load_addr
)
87 struct bmp_header
*bmp_hdr
;
89 size_t bmp_size
, bmp_header_size
= sizeof(struct bmp_header
);
91 if (bmp_load_addr
+ bmp_header_size
>= gd
->start_addr_sp
)
92 goto splash_address_too_high
;
94 res
= splash_storage_read_raw(location
, bmp_load_addr
, bmp_header_size
);
98 bmp_hdr
= (struct bmp_header
*)bmp_load_addr
;
99 bmp_size
= le32_to_cpu(bmp_hdr
->file_size
);
101 if (bmp_load_addr
+ bmp_size
>= gd
->start_addr_sp
)
102 goto splash_address_too_high
;
104 return splash_storage_read_raw(location
, bmp_load_addr
, bmp_size
);
106 splash_address_too_high
:
107 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
112 static int splash_select_fs_dev(struct splash_location
*location
)
116 switch (location
->storage
) {
117 case SPLASH_STORAGE_MMC
:
118 res
= fs_set_blk_dev("mmc", location
->devpart
, FS_TYPE_ANY
);
120 case SPLASH_STORAGE_USB
:
121 res
= fs_set_blk_dev("usb", location
->devpart
, FS_TYPE_ANY
);
123 case SPLASH_STORAGE_SATA
:
124 res
= fs_set_blk_dev("sata", location
->devpart
, FS_TYPE_ANY
);
126 case SPLASH_STORAGE_NAND
:
127 if (location
->ubivol
!= NULL
)
128 res
= fs_set_blk_dev("ubi", NULL
, FS_TYPE_UBIFS
);
133 printf("Error: unsupported location storage.\n");
138 printf("Error: could not access storage.\n");
143 #ifdef CONFIG_USB_STORAGE
144 static int splash_init_usb(void)
152 #ifndef CONFIG_DM_USB
153 err
= usb_stor_scan(1) < 0 ? -ENODEV
: 0;
159 static inline int splash_init_usb(void)
161 printf("Cannot load splash image: no USB support\n");
167 static int splash_init_sata(void)
169 return sata_probe(0);
172 static inline int splash_init_sata(void)
174 printf("Cannot load splash image: no SATA support\n");
179 #ifdef CONFIG_CMD_UBIFS
180 static int splash_mount_ubifs(struct splash_location
*location
)
185 sprintf(cmd
, "ubi part %s", location
->mtdpart
);
186 res
= run_command(cmd
, 0);
190 sprintf(cmd
, "ubifsmount %s", location
->ubivol
);
191 res
= run_command(cmd
, 0);
196 static inline int splash_umount_ubifs(void)
198 return run_command("ubifsumount", 0);
201 static inline int splash_mount_ubifs(struct splash_location
*location
)
203 printf("Cannot load splash image: no UBIFS support\n");
207 static inline int splash_umount_ubifs(void)
209 printf("Cannot unmount UBIFS: no UBIFS support\n");
214 #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
216 static int splash_load_fs(struct splash_location
*location
, u32 bmp_load_addr
)
223 splash_file
= env_get("splashfile");
225 splash_file
= SPLASH_SOURCE_DEFAULT_FILE_NAME
;
227 if (location
->storage
== SPLASH_STORAGE_USB
)
228 res
= splash_init_usb();
230 if (location
->storage
== SPLASH_STORAGE_SATA
)
231 res
= splash_init_sata();
233 if (location
->ubivol
!= NULL
)
234 res
= splash_mount_ubifs(location
);
239 res
= splash_select_fs_dev(location
);
243 res
= fs_size(splash_file
, &bmp_size
);
245 printf("Error (%d): cannot determine file size\n", res
);
249 if (bmp_load_addr
+ bmp_size
>= gd
->start_addr_sp
) {
250 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
255 splash_select_fs_dev(location
);
256 res
= fs_read(splash_file
, bmp_load_addr
, 0, 0, &actread
);
259 if (location
->ubivol
!= NULL
)
260 splash_umount_ubifs();
266 * select_splash_location - return the splash location based on board support
267 * and env variable "splashsource".
269 * @locations: An array of supported splash locations.
270 * @size: Size of splash_locations array.
272 * @return: If a null set of splash locations is given, or
273 * splashsource env variable is set to unsupported value
275 * If splashsource env variable is not defined
276 * return the first entry in splash_locations as default.
277 * If splashsource env variable contains a supported value
278 * return the location selected by splashsource.
280 static struct splash_location
*select_splash_location(
281 struct splash_location
*locations
, uint size
)
284 char *env_splashsource
;
286 if (!locations
|| size
== 0)
289 env_splashsource
= env_get("splashsource");
290 if (env_splashsource
== NULL
)
291 return &locations
[0];
293 for (i
= 0; i
< size
; i
++) {
294 if (!strcmp(locations
[i
].name
, env_splashsource
))
295 return &locations
[i
];
298 printf("splashsource env variable set to unsupported value\n");
303 static int splash_load_fit(struct splash_location
*location
, u32 bmp_load_addr
)
309 struct image_header
*img_header
;
310 const u32
*fit_header
;
312 const size_t header_size
= sizeof(struct image_header
);
314 /* Read in image header */
315 res
= splash_storage_read_raw(location
, bmp_load_addr
, header_size
);
319 img_header
= (struct image_header
*)bmp_load_addr
;
320 fit_size
= fdt_totalsize(img_header
);
322 /* Read in entire FIT */
323 fit_header
= (const u32
*)(bmp_load_addr
+ header_size
);
324 res
= splash_storage_read_raw(location
, (u32
)fit_header
, fit_size
);
328 res
= fit_check_format(fit_header
);
330 debug("Could not find valid FIT image\n");
334 node_offset
= fit_image_get_node(fit_header
, location
->name
);
335 if (node_offset
< 0) {
336 debug("Could not find splash image '%s' in FIT\n",
341 res
= fit_image_get_data_offset(fit_header
, node_offset
,
344 printf("Failed to load splash image (err=%d)\n", res
);
348 res
= fit_image_get_data_size(fit_header
, node_offset
, &splash_size
);
350 printf("Failed to load splash image (err=%d)\n", res
);
354 /* Align data offset to 4-byte boundrary */
355 fit_size
= fdt_totalsize(fit_header
);
356 fit_size
= (fit_size
+ 3) & ~3;
358 /* Read in the splash data */
359 location
->offset
= (location
->offset
+ fit_size
+ splash_offset
);
360 res
= splash_storage_read_raw(location
, bmp_load_addr
, splash_size
);
366 #endif /* CONFIG_FIT */
369 * splash_source_load - load splash image from a supported location.
371 * Select a splash image location based on the value of splashsource environment
372 * variable and the board supported splash source locations, and load a
373 * splashimage to the address pointed to by splashimage environment variable.
375 * @locations: An array of supported splash locations.
376 * @size: Size of splash_locations array.
378 * @return: 0 on success, negative value on failure.
380 int splash_source_load(struct splash_location
*locations
, uint size
)
382 struct splash_location
*splash_location
;
383 char *env_splashimage_value
;
386 env_splashimage_value
= env_get("splashimage");
387 if (env_splashimage_value
== NULL
)
390 bmp_load_addr
= simple_strtoul(env_splashimage_value
, 0, 16);
391 if (bmp_load_addr
== 0) {
392 printf("Error: bad splashimage address specified\n");
396 splash_location
= select_splash_location(locations
, size
);
397 if (!splash_location
)
400 if (splash_location
->flags
== SPLASH_STORAGE_RAW
)
401 return splash_load_raw(splash_location
, bmp_load_addr
);
402 else if (splash_location
->flags
== SPLASH_STORAGE_FS
)
403 return splash_load_fs(splash_location
, bmp_load_addr
);
405 else if (splash_location
->flags
== SPLASH_STORAGE_FIT
)
406 return splash_load_fit(splash_location
, bmp_load_addr
);