"Das U-Boot" Source Tree
at master 471 lines 12 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il> 4 * 5 * Authors: Igor Grinberg <grinberg@compulab.co.il> 6 */ 7 8#include <bmp_layout.h> 9#include <command.h> 10#include <env.h> 11#include <errno.h> 12#include <fs.h> 13#include <fdt_support.h> 14#include <image.h> 15#include <log.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> 22#include <virtio.h> 23#include <asm/global_data.h> 24 25DECLARE_GLOBAL_DATA_PTR; 26 27#ifdef CONFIG_SPI_FLASH 28static struct spi_flash *sf; 29static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) 30{ 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 40 return spi_flash_read(sf, offset, read_size, (void *)(uintptr_t)bmp_load_addr); 41} 42#else 43static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) 44{ 45 debug("%s: sf support not available\n", __func__); 46 return -ENOSYS; 47} 48#endif 49 50#ifdef CONFIG_CMD_NAND 51static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) 52{ 53 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); 54 return nand_read_skip_bad(mtd, offset, 55 &read_size, NULL, 56 mtd->size, 57 (u_char *)bmp_load_addr); 58} 59#else 60static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) 61{ 62 debug("%s: nand support not available\n", __func__); 63 return -ENOSYS; 64} 65#endif 66 67static int splash_mmc_read_raw(u32 bmp_load_addr, struct splash_location *location, 68 size_t read_size) 69{ 70 struct disk_partition partition; 71 struct blk_desc *desc; 72 lbaint_t blkcnt; 73 int ret, n; 74 75 if (!IS_ENABLED(CONFIG_CMD_MMC)) { 76 debug("%s: mmc support not available\n", __func__); 77 return -ENOSYS; 78 } 79 80 ret = part_get_info_by_dev_and_name_or_num("mmc", location->devpart, &desc, 81 &partition, 1); 82 if (ret < 0) 83 return ret; 84 85 blkcnt = DIV_ROUND_UP(read_size, partition.blksz); 86 n = blk_dread(desc, partition.start, blkcnt, (void *)(uintptr_t)bmp_load_addr); 87 88 return (n == blkcnt) ? 0 : -EIO; 89} 90 91static int splash_storage_read_raw(struct splash_location *location, 92 u32 bmp_load_addr, size_t read_size) 93{ 94 u32 offset; 95 96 if (!location) 97 return -EINVAL; 98 99 offset = location->offset; 100 switch (location->storage) { 101 case SPLASH_STORAGE_MMC: 102 return splash_mmc_read_raw(bmp_load_addr, location, read_size); 103 case SPLASH_STORAGE_NAND: 104 return splash_nand_read_raw(bmp_load_addr, offset, read_size); 105 case SPLASH_STORAGE_SF: 106 return splash_sf_read_raw(bmp_load_addr, offset, read_size); 107 default: 108 printf("Unknown splash location\n"); 109 } 110 111 return -EINVAL; 112} 113 114static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr) 115{ 116 struct bmp_header *bmp_hdr; 117 int res; 118 size_t bmp_size, bmp_header_size = sizeof(struct bmp_header); 119 120 if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp) 121 goto splash_address_too_high; 122 123 res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size); 124 if (res < 0) 125 return res; 126 127 bmp_hdr = (struct bmp_header *)(uintptr_t)bmp_load_addr; 128 bmp_size = le32_to_cpu(bmp_hdr->file_size); 129 130 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) 131 goto splash_address_too_high; 132 133 return splash_storage_read_raw(location, bmp_load_addr, bmp_size); 134 135splash_address_too_high: 136 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); 137 138 return -EFAULT; 139} 140 141static int splash_select_fs_dev(struct splash_location *location) 142{ 143 int res; 144 145 switch (location->storage) { 146 case SPLASH_STORAGE_MMC: 147 res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY); 148 break; 149 case SPLASH_STORAGE_USB: 150 res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY); 151 break; 152 case SPLASH_STORAGE_SATA: 153 res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY); 154 break; 155 case SPLASH_STORAGE_NAND: 156 if (location->ubivol != NULL) 157 res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS); 158 else 159 res = -ENODEV; 160 break; 161 default: 162 printf("Error: unsupported location storage.\n"); 163 return -ENODEV; 164 } 165 166 if (res) 167 printf("Error: could not access storage.\n"); 168 169 return res; 170} 171 172#ifdef CONFIG_USB_STORAGE 173static int splash_init_usb(void) 174{ 175 int err; 176 177 err = usb_init(); 178 if (err) 179 return err; 180 181#ifndef CONFIG_DM_USB 182 err = usb_stor_scan(1) < 0 ? -ENODEV : 0; 183#endif 184 185 return err; 186} 187#else 188static inline int splash_init_usb(void) 189{ 190 printf("Cannot load splash image: no USB support\n"); 191 return -ENOSYS; 192} 193#endif 194 195#ifdef CONFIG_SATA 196static int splash_init_sata(void) 197{ 198 return sata_probe(0); 199} 200#else 201static inline int splash_init_sata(void) 202{ 203 printf("Cannot load splash image: no SATA support\n"); 204 return -ENOSYS; 205} 206#endif 207 208static int splash_init_virtio(void) 209{ 210 if (!IS_ENABLED(CONFIG_VIRTIO)) { 211 printf("Cannot load splash image: no virtio support\n"); 212 return -ENOSYS; 213 } else { 214 return virtio_init(); 215 } 216} 217 218#if defined(CONFIG_CMD_UBIFS) && !defined(CONFIG_XPL_BUILD) 219static int splash_mount_ubifs(struct splash_location *location) 220{ 221 int res; 222 char cmd[32]; 223 224 sprintf(cmd, "ubi part %s", location->mtdpart); 225 res = run_command(cmd, 0); 226 if (res) 227 return res; 228 229 sprintf(cmd, "ubifsmount %s", location->ubivol); 230 res = run_command(cmd, 0); 231 232 return res; 233} 234 235static inline int splash_umount_ubifs(void) 236{ 237 return run_command("ubifsumount", 0); 238} 239#else 240static inline int splash_mount_ubifs(struct splash_location *location) 241{ 242 printf("Cannot load splash image: no UBIFS support\n"); 243 return -ENOSYS; 244} 245 246static inline int splash_umount_ubifs(void) 247{ 248 printf("Cannot unmount UBIFS: no UBIFS support\n"); 249 return -ENOSYS; 250} 251#endif 252 253#define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp" 254 255static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr) 256{ 257 int res = 0; 258 loff_t bmp_size; 259 loff_t actread; 260 char *splash_file; 261 262 splash_file = env_get("splashfile"); 263 if (!splash_file) 264 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME; 265 266 if (location->storage == SPLASH_STORAGE_USB) 267 res = splash_init_usb(); 268 269 if (location->storage == SPLASH_STORAGE_SATA) 270 res = splash_init_sata(); 271 272 if (location->storage == SPLASH_STORAGE_VIRTIO) 273 res = splash_init_virtio(); 274 275 if (location->ubivol != NULL) 276 res = splash_mount_ubifs(location); 277 278 if (res) 279 return res; 280 281 res = splash_select_fs_dev(location); 282 if (res) 283 goto out; 284 285 res = fs_size(splash_file, &bmp_size); 286 if (res) { 287 printf("Error (%d): cannot determine file size\n", res); 288 goto out; 289 } 290 291 if (bmp_load_addr + bmp_size >= gd->start_addr_sp) { 292 printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); 293 res = -EFAULT; 294 goto out; 295 } 296 297 splash_select_fs_dev(location); 298 res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread); 299 300out: 301 if (location->ubivol != NULL) 302 splash_umount_ubifs(); 303 304 return res; 305} 306 307/** 308 * select_splash_location - return the splash location based on board support 309 * and env variable "splashsource". 310 * 311 * @locations: An array of supported splash locations. 312 * @size: Size of splash_locations array. 313 * 314 * @return: If a null set of splash locations is given, or 315 * splashsource env variable is set to unsupported value 316 * return NULL. 317 * If splashsource env variable is not defined 318 * return the first entry in splash_locations as default. 319 * If splashsource env variable contains a supported value 320 * return the location selected by splashsource. 321 */ 322static struct splash_location *select_splash_location( 323 struct splash_location *locations, uint size) 324{ 325 int i; 326 char *env_splashsource; 327 328 if (!locations || size == 0) 329 return NULL; 330 331 env_splashsource = env_get("splashsource"); 332 if (env_splashsource == NULL) 333 return &locations[0]; 334 335 for (i = 0; i < size; i++) { 336 if (!strcmp(locations[i].name, env_splashsource)) 337 return &locations[i]; 338 } 339 340 printf("splashsource env variable set to unsupported value\n"); 341 return NULL; 342} 343 344#ifdef CONFIG_FIT 345static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr) 346{ 347 int res; 348 int node_offset; 349 const char *splash_file; 350 const void *internal_splash_data; 351 size_t internal_splash_size; 352 int external_splash_addr; 353 int external_splash_size; 354 bool is_splash_external = false; 355 struct legacy_img_hdr *img_header; 356 const u32 *fit_header; 357 u32 fit_size; 358 const size_t header_size = sizeof(struct legacy_img_hdr); 359 360 /* Read in image header */ 361 res = splash_storage_read_raw(location, bmp_load_addr, header_size); 362 if (res < 0) 363 return res; 364 365 img_header = (struct legacy_img_hdr *)(uintptr_t)bmp_load_addr; 366 if (image_get_magic(img_header) != FDT_MAGIC) { 367 printf("Could not find FDT magic\n"); 368 return -EINVAL; 369 } 370 371 fit_size = fdt_totalsize(img_header); 372 373 /* Read in entire FIT */ 374 fit_header = (const u32 *)(bmp_load_addr + header_size); 375 res = splash_storage_read_raw(location, (uintptr_t)fit_header, fit_size); 376 if (res < 0) 377 return res; 378 379 res = fit_check_format(fit_header, IMAGE_SIZE_INVAL); 380 if (res) { 381 debug("Could not find valid FIT image\n"); 382 return res; 383 } 384 385 /* Get the splash image node */ 386 splash_file = env_get("splashfile"); 387 if (!splash_file) 388 splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME; 389 390 node_offset = fit_image_get_node(fit_header, splash_file); 391 if (node_offset < 0) { 392 debug("Could not find splash image '%s' in FIT\n", 393 splash_file); 394 return -ENOENT; 395 } 396 397 /* Extract the splash data from FIT */ 398 if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, 399 &internal_splash_size)) { 400 memmove((void *)(uintptr_t)bmp_load_addr, internal_splash_data, 401 internal_splash_size); 402 } else { 403 printf("Failed to get splash image from FIT\n"); 404 return -ENODATA; 405 } 406 407 if (is_splash_external) { 408 res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size); 409 if (res < 0) { 410 printf("Failed to get size of splash image (err=%d)\n", res); 411 return res; 412 } 413 414 /* Read in the splash data */ 415 location->offset = external_splash_addr; 416 res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size); 417 if (res < 0) 418 return res; 419 } 420 421 return 0; 422} 423#endif /* CONFIG_FIT */ 424 425/** 426 * splash_source_load - load splash image from a supported location. 427 * 428 * Select a splash image location based on the value of splashsource environment 429 * variable and the board supported splash source locations, and load a 430 * splashimage to the address pointed to by splashimage environment variable. 431 * 432 * @locations: An array of supported splash locations. 433 * @size: Size of splash_locations array. 434 * 435 * @return: 0 on success, negative value on failure. 436 */ 437int splash_source_load(struct splash_location *locations, uint size) 438{ 439 struct splash_location *splash_location; 440 char *env_splashimage_value; 441 char *devpart; 442 u32 bmp_load_addr; 443 444 env_splashimage_value = env_get("splashimage"); 445 if (env_splashimage_value == NULL) 446 return -ENOENT; 447 448 bmp_load_addr = hextoul(env_splashimage_value, 0); 449 if (bmp_load_addr == 0) { 450 printf("Error: bad splashimage address specified\n"); 451 return -EFAULT; 452 } 453 454 splash_location = select_splash_location(locations, size); 455 if (!splash_location) 456 return -EINVAL; 457 458 devpart = env_get("splashdevpart"); 459 if (devpart) 460 splash_location->devpart = devpart; 461 462 if (splash_location->flags == SPLASH_STORAGE_RAW) 463 return splash_load_raw(splash_location, bmp_load_addr); 464 else if (splash_location->flags == SPLASH_STORAGE_FS) 465 return splash_load_fs(splash_location, bmp_load_addr); 466#ifdef CONFIG_FIT 467 else if (splash_location->flags == SPLASH_STORAGE_FIT) 468 return splash_load_fit(splash_location, bmp_load_addr); 469#endif 470 return -EINVAL; 471}