1 /* 2 * (C) Copyright 2010 3 * Texas Instruments, <www.ti.com> 4 * 5 * Aneesh V <aneesh@ti.com> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <dm.h> 12 #include <spl.h> 13 #include <asm/sections.h> 14 #include <asm/u-boot.h> 15 #include <nand.h> 16 #include <fat.h> 17 #include <version.h> 18 #include <image.h> 19 #include <malloc.h> 20 #include <dm/root.h> 21 #include <linux/compiler.h> 22 #include <fdt_support.h> 23 24 DECLARE_GLOBAL_DATA_PTR; 25 26 #ifndef CONFIG_SYS_UBOOT_START 27 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE 28 #endif 29 #ifndef CONFIG_SYS_MONITOR_LEN 30 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */ 31 #define CONFIG_SYS_MONITOR_LEN (200 * 1024) 32 #endif 33 34 u32 *boot_params_ptr = NULL; 35 36 /* Define board data structure */ 37 static bd_t bdata __attribute__ ((section(".data"))); 38 39 /* 40 * Board-specific Platform code can reimplement show_boot_progress () if needed 41 */ 42 __weak void show_boot_progress(int val) {} 43 44 /* 45 * Default function to determine if u-boot or the OS should 46 * be started. This implementation always returns 1. 47 * 48 * Please implement your own board specific funcion to do this. 49 * 50 * RETURN 51 * 0 to not start u-boot 52 * positive if u-boot should start 53 */ 54 #ifdef CONFIG_SPL_OS_BOOT 55 __weak int spl_start_uboot(void) 56 { 57 puts("SPL: Please implement spl_start_uboot() for your board\n"); 58 puts("SPL: Direct Linux boot not active!\n"); 59 return 1; 60 } 61 62 /* weak default platform specific function to initialize 63 * dram banks 64 */ 65 __weak int dram_init_banksize(void) 66 { 67 return 0; 68 } 69 70 /* 71 * Weak default function for arch specific zImage check. Return zero 72 * and fill start and end address if image is recognized. 73 */ 74 int __weak bootz_setup(ulong image, ulong *start, ulong *end) 75 { 76 return 1; 77 } 78 #endif 79 80 void spl_fixup_fdt(void) 81 { 82 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR) 83 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR; 84 int err; 85 86 err = fdt_check_header(fdt_blob); 87 if (err < 0) { 88 printf("fdt_root: %s\n", fdt_strerror(err)); 89 return; 90 } 91 92 /* fixup the memory dt node */ 93 err = fdt_shrink_to_minimum(fdt_blob, 0); 94 if (err == 0) { 95 printf("spl: fdt_shrink_to_minimum err - %d\n", err); 96 return; 97 } 98 99 err = arch_fixup_fdt(fdt_blob); 100 if (err) { 101 printf("spl: arch_fixup_fdt err - %d\n", err); 102 return; 103 } 104 #endif 105 } 106 107 /* 108 * Weak default function for board specific cleanup/preparation before 109 * Linux boot. Some boards/platforms might not need it, so just provide 110 * an empty stub here. 111 */ 112 __weak void spl_board_prepare_for_linux(void) 113 { 114 /* Nothing to do! */ 115 } 116 117 __weak void spl_board_prepare_for_boot(void) 118 { 119 /* Nothing to do! */ 120 } 121 122 void spl_set_header_raw_uboot(struct spl_image_info *spl_image) 123 { 124 spl_image->size = CONFIG_SYS_MONITOR_LEN; 125 spl_image->entry_point = CONFIG_SYS_UBOOT_START; 126 spl_image->load_addr = CONFIG_SYS_TEXT_BASE; 127 spl_image->os = IH_OS_U_BOOT; 128 spl_image->name = "U-Boot"; 129 } 130 131 int spl_parse_image_header(struct spl_image_info *spl_image, 132 const struct image_header *header) 133 { 134 if (image_get_magic(header) == IH_MAGIC) { 135 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT 136 u32 header_size = sizeof(struct image_header); 137 138 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) { 139 /* 140 * On some system (e.g. powerpc), the load-address and 141 * entry-point is located at address 0. We can't load 142 * to 0-0x40. So skip header in this case. 143 */ 144 spl_image->load_addr = image_get_load(header); 145 spl_image->entry_point = image_get_ep(header); 146 spl_image->size = image_get_data_size(header); 147 } else { 148 spl_image->entry_point = image_get_load(header); 149 /* Load including the header */ 150 spl_image->load_addr = spl_image->entry_point - 151 header_size; 152 spl_image->size = image_get_data_size(header) + 153 header_size; 154 } 155 spl_image->os = image_get_os(header); 156 spl_image->name = image_get_name(header); 157 debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n", 158 IH_NMLEN, spl_image->name, 159 spl_image->load_addr, spl_image->size); 160 #else 161 /* LEGACY image not supported */ 162 debug("Legacy boot image support not enabled, proceeding to other boot methods\n"); 163 return -EINVAL; 164 #endif 165 } else { 166 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE 167 /* 168 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the 169 * code which loads images in SPL cannot guarantee that 170 * absolutely all read errors will be reported. 171 * An example is the LPC32XX MLC NAND driver, which 172 * will consider that a completely unreadable NAND block 173 * is bad, and thus should be skipped silently. 174 */ 175 panic("** no mkimage signature but raw image not supported"); 176 #endif 177 178 #ifdef CONFIG_SPL_OS_BOOT 179 ulong start, end; 180 181 if (!bootz_setup((ulong)header, &start, &end)) { 182 spl_image->name = "Linux"; 183 spl_image->os = IH_OS_LINUX; 184 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR; 185 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR; 186 spl_image->size = end - start; 187 debug("spl: payload zImage, load addr: 0x%lx size: %d\n", 188 spl_image->load_addr, spl_image->size); 189 return 0; 190 } 191 #endif 192 193 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT 194 /* Signature not found - assume u-boot.bin */ 195 debug("mkimage signature not found - ih_magic = %x\n", 196 header->ih_magic); 197 spl_set_header_raw_uboot(spl_image); 198 #else 199 /* RAW image not supported, proceed to other boot methods. */ 200 debug("Raw boot image support not enabled, proceeding to other boot methods\n"); 201 return -EINVAL; 202 #endif 203 } 204 205 return 0; 206 } 207 208 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) 209 { 210 typedef void __noreturn (*image_entry_noargs_t)(void); 211 212 image_entry_noargs_t image_entry = 213 (image_entry_noargs_t)spl_image->entry_point; 214 215 debug("image entry point: 0x%lX\n", spl_image->entry_point); 216 image_entry(); 217 } 218 219 static int spl_common_init(bool setup_malloc) 220 { 221 int ret; 222 223 debug("spl_early_init()\n"); 224 225 #if CONFIG_VAL(SYS_MALLOC_F_LEN) 226 if (setup_malloc) { 227 #ifdef CONFIG_MALLOC_F_ADDR 228 gd->malloc_base = CONFIG_MALLOC_F_ADDR; 229 #endif 230 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN); 231 gd->malloc_ptr = 0; 232 } 233 #endif 234 ret = bootstage_init(true); 235 if (ret) { 236 debug("%s: Failed to set up bootstage: ret=%d\n", __func__, 237 ret); 238 return ret; 239 } 240 bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl"); 241 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { 242 ret = fdtdec_setup(); 243 if (ret) { 244 debug("fdtdec_setup() returned error %d\n", ret); 245 return ret; 246 } 247 } 248 if (CONFIG_IS_ENABLED(DM)) { 249 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl"); 250 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */ 251 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA)); 252 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL); 253 if (ret) { 254 debug("dm_init_and_scan() returned error %d\n", ret); 255 return ret; 256 } 257 } 258 259 return 0; 260 } 261 262 #if !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD) 263 static void spl_setup_relocate(void) 264 { 265 gd->relocaddr = CONFIG_SPL_RELOC_TEXT_BASE; 266 gd->new_gd = (gd_t *)gd; 267 gd->start_addr_sp = gd->relocaddr; 268 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32); 269 270 gd->start_addr_sp -= gd->fdt_size; 271 gd->new_fdt = (void *)gd->start_addr_sp; 272 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size); 273 gd->fdt_blob = gd->new_fdt; 274 275 gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start; 276 } 277 #else 278 static void spl_setup_relocate(void) 279 { 280 281 } 282 #endif 283 284 void spl_set_bd(void) 285 { 286 if (!gd->bd) 287 gd->bd = &bdata; 288 } 289 290 int spl_early_init(void) 291 { 292 int ret; 293 294 ret = spl_common_init(true); 295 if (ret) 296 return ret; 297 gd->flags |= GD_FLG_SPL_EARLY_INIT; 298 299 spl_setup_relocate(); 300 301 return 0; 302 } 303 304 int spl_init(void) 305 { 306 int ret; 307 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) && 308 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE)); 309 310 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) { 311 ret = spl_common_init(setup_malloc); 312 if (ret) 313 return ret; 314 } 315 gd->flags |= GD_FLG_SPL_INIT; 316 317 return 0; 318 } 319 320 #ifndef BOOT_DEVICE_NONE 321 #define BOOT_DEVICE_NONE 0xdeadbeef 322 #endif 323 324 __weak void board_boot_order(u32 *spl_boot_list) 325 { 326 spl_boot_list[0] = spl_boot_device(); 327 } 328 329 static struct spl_image_loader *spl_ll_find_loader(uint boot_device) 330 { 331 struct spl_image_loader *drv = 332 ll_entry_start(struct spl_image_loader, spl_image_loader); 333 const int n_ents = 334 ll_entry_count(struct spl_image_loader, spl_image_loader); 335 struct spl_image_loader *entry; 336 337 for (entry = drv; entry != drv + n_ents; entry++) { 338 if (boot_device == entry->boot_device) 339 return entry; 340 } 341 342 /* Not found */ 343 return NULL; 344 } 345 346 static int spl_load_image(struct spl_image_info *spl_image, 347 struct spl_image_loader *loader) 348 { 349 struct spl_boot_device bootdev; 350 351 bootdev.boot_device = loader->boot_device; 352 bootdev.boot_device_name = NULL; 353 354 return loader->load_image(spl_image, &bootdev); 355 } 356 357 /** 358 * boot_from_devices() - Try loading an booting U-Boot from a list of devices 359 * 360 * @spl_image: Place to put the image details if successful 361 * @spl_boot_list: List of boot devices to try 362 * @count: Number of elements in spl_boot_list 363 * @return 0 if OK, -ve on error 364 */ 365 static int boot_from_devices(struct spl_image_info *spl_image, 366 u32 spl_boot_list[], int count) 367 { 368 int i; 369 370 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) { 371 struct spl_image_loader *loader; 372 373 loader = spl_ll_find_loader(spl_boot_list[i]); 374 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 375 if (loader) 376 printf("Trying to boot from %s\n", loader->name); 377 else 378 puts("SPL: Unsupported Boot Device!\n"); 379 #endif 380 if (loader && !spl_load_image(spl_image, loader)) 381 return 0; 382 } 383 384 return -ENODEV; 385 } 386 387 #if defined(CONFIG_DM) && !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD) 388 static int spl_initr_dm(void) 389 { 390 int ret; 391 392 /* Save the pre-reloc driver model and start a new one */ 393 gd->dm_root_f = gd->dm_root; 394 gd->dm_root = NULL; 395 bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r"); 396 ret = dm_init_and_scan(false); 397 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R); 398 if (ret) 399 return ret; 400 401 #if defined(CONFIG_TIMER) 402 gd->timer = NULL; 403 #endif 404 serial_init(); 405 406 return 0; 407 } 408 #else 409 static int spl_initr_dm(void) 410 { 411 return 0; 412 } 413 #endif 414 415 void board_init_r(gd_t *dummy1, ulong dummy2) 416 { 417 u32 spl_boot_list[] = { 418 BOOT_DEVICE_NONE, 419 BOOT_DEVICE_NONE, 420 BOOT_DEVICE_NONE, 421 BOOT_DEVICE_NONE, 422 BOOT_DEVICE_NONE, 423 }; 424 struct spl_image_info spl_image; 425 426 debug(">>spl:board_init_r()\n"); 427 428 spl_initr_dm(); 429 430 spl_set_bd(); 431 432 #ifdef CONFIG_SPL_OS_BOOT 433 dram_init_banksize(); 434 #endif 435 436 #if defined(CONFIG_SYS_SPL_MALLOC_START) 437 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START, 438 CONFIG_SYS_SPL_MALLOC_SIZE); 439 gd->flags |= GD_FLG_FULL_MALLOC_INIT; 440 #endif 441 if (!(gd->flags & GD_FLG_SPL_INIT)) { 442 if (spl_init()) 443 hang(); 444 } 445 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6) 446 /* 447 * timer_init() does not exist on PPC systems. The timer is initialized 448 * and enabled (decrementer) in interrupt_init() here. 449 */ 450 timer_init(); 451 #endif 452 453 #if CONFIG_IS_ENABLED(BOARD_INIT) 454 spl_board_init(); 455 #endif 456 457 memset(&spl_image, '\0', sizeof(spl_image)); 458 459 #if CONFIG_IS_ENABLED(ATF) 460 /* 461 * Bl32 ep is optional, initial it as an invalid value. 462 * BL33 ep is mandatory, but initial it as a default value is better. 463 */ 464 spl_image.entry_point_bl32 = -1; 465 spl_image.entry_point_bl33 = CONFIG_SYS_TEXT_BASE; 466 #endif 467 468 #ifdef CONFIG_SYS_SPL_ARGS_ADDR 469 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR; 470 #endif 471 board_boot_order(spl_boot_list); 472 473 if (boot_from_devices(&spl_image, spl_boot_list, 474 ARRAY_SIZE(spl_boot_list))) { 475 puts("SPL: failed to boot from all boot devices\n"); 476 hang(); 477 } 478 479 #ifdef CONFIG_CPU_V7M 480 spl_image.entry_point |= 0x1; 481 #endif 482 switch (spl_image.os) { 483 case IH_OS_U_BOOT: 484 debug("Jumping to U-Boot\n"); 485 break; 486 #if CONFIG_IS_ENABLED(ATF) 487 case IH_OS_ARM_TRUSTED_FIRMWARE: 488 printf("Jumping to U-Boot via ARM Trusted Firmware\n\n"); 489 spl_invoke_atf(&spl_image); 490 break; 491 #endif 492 #if CONFIG_IS_ENABLED(OPTEE) 493 case IH_OS_OP_TEE: 494 debug("Jumping to U-Boot via OP-TEE\n"); 495 spl_optee_entry(NULL, NULL, (void *)spl_image.fdt_addr, 496 (void *)spl_image.entry_point); 497 break; 498 #endif 499 #ifdef CONFIG_SPL_OS_BOOT 500 case IH_OS_LINUX: 501 debug("Jumping to Linux\n"); 502 spl_fixup_fdt(); 503 spl_board_prepare_for_linux(); 504 jump_to_image_linux(&spl_image); 505 #endif 506 default: 507 debug("Unsupported OS image.. Jumping nevertheless..\n"); 508 } 509 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE) 510 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr, 511 gd->malloc_ptr / 1024); 512 #endif 513 514 debug("loaded - jumping to U-Boot...\n"); 515 #ifdef CONFIG_BOOTSTAGE_STASH 516 int ret; 517 518 bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl"); 519 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR, 520 CONFIG_BOOTSTAGE_STASH_SIZE); 521 if (ret) 522 debug("Failed to stash bootstage: err=%d\n", ret); 523 #endif 524 525 debug("loaded - jumping to U-Boot...\n"); 526 spl_board_prepare_for_boot(); 527 jump_to_image_no_args(&spl_image); 528 } 529 530 /* 531 * This requires UART clocks to be enabled. In order for this to work the 532 * caller must ensure that the gd pointer is valid. 533 */ 534 void preloader_console_init(void) 535 { 536 gd->baudrate = CONFIG_BAUDRATE; 537 538 serial_init(); /* serial communications setup */ 539 540 gd->have_console = 1; 541 542 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \ 543 U_BOOT_TIME ")\n"); 544 #ifdef CONFIG_SPL_DISPLAY_PRINT 545 spl_display_print(); 546 #endif 547 } 548 549 /** 550 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution 551 * 552 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM 553 * for the main board_init_r() execution. This is typically because we need 554 * more stack space for things like the MMC sub-system. 555 * 556 * This function calculates the stack position, copies the global_data into 557 * place, sets the new gd (except for ARM, for which setting GD within a C 558 * function may not always work) and returns the new stack position. The 559 * caller is responsible for setting up the sp register and, in the case 560 * of ARM, setting up gd. 561 * 562 * All of this is done using the same layout and alignments as done in 563 * board_init_f_init_reserve() / board_init_f_alloc_reserve(). 564 * 565 * @return new stack location, or 0 to use the same stack 566 */ 567 ulong spl_relocate_stack_gd(void) 568 { 569 #ifdef CONFIG_SPL_STACK_R 570 gd_t *new_gd; 571 ulong ptr = CONFIG_SPL_STACK_R_ADDR; 572 573 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN) 574 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) { 575 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 576 gd->malloc_base = ptr; 577 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN; 578 gd->malloc_ptr = 0; 579 } 580 #endif 581 /* Get stack position: use 8-byte alignment for ABI compliance */ 582 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16); 583 new_gd = (gd_t *)ptr; 584 memcpy(new_gd, (void *)gd, sizeof(gd_t)); 585 #if CONFIG_IS_ENABLED(DM) 586 dm_fixup_for_gd_move(new_gd); 587 #endif 588 #if !defined(CONFIG_ARM) 589 gd = new_gd; 590 #endif 591 return ptr; 592 #else 593 return 0; 594 #endif 595 } 596