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