1 /* Copyright (C) 2011 2 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de> 3 * - Added prep subcommand support 4 * - Reorganized source - modeled after powerpc version 5 * 6 * (C) Copyright 2002 7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 8 * Marius Groeger <mgroeger@sysgo.de> 9 * 10 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) 11 * 12 * SPDX-License-Identifier: GPL-2.0+ 13 */ 14 15 #include <common.h> 16 #include <command.h> 17 #include <dm.h> 18 #include <dm/root.h> 19 #include <image.h> 20 #include <u-boot/zlib.h> 21 #include <asm/byteorder.h> 22 #include <libfdt.h> 23 #include <mapmem.h> 24 #include <fdt_support.h> 25 #include <asm/bootm.h> 26 #include <asm/secure.h> 27 #include <linux/compiler.h> 28 #include <bootm.h> 29 #include <vxworks.h> 30 31 #ifdef CONFIG_ARMV7_NONSEC 32 #include <asm/armv7.h> 33 #endif 34 #include <asm/setup.h> 35 36 DECLARE_GLOBAL_DATA_PTR; 37 38 static struct tag *params; 39 40 static ulong get_sp(void) 41 { 42 ulong ret; 43 44 asm("mov %0, sp" : "=r"(ret) : ); 45 return ret; 46 } 47 48 void arch_lmb_reserve(struct lmb *lmb) 49 { 50 ulong sp; 51 52 /* 53 * Booting a (Linux) kernel image 54 * 55 * Allocate space for command line and board info - the 56 * address should be as high as possible within the reach of 57 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused 58 * memory, which means far enough below the current stack 59 * pointer. 60 */ 61 sp = get_sp(); 62 debug("## Current stack ends at 0x%08lx ", sp); 63 64 /* adjust sp by 4K to be safe */ 65 sp -= 4096; 66 lmb_reserve(lmb, sp, 67 gd->ram_top - sp); 68 } 69 70 __weak void board_quiesce_devices(void) 71 { 72 } 73 74 /** 75 * announce_and_cleanup() - Print message and prepare for kernel boot 76 * 77 * @fake: non-zero to do everything except actually boot 78 */ 79 static void announce_and_cleanup(int fake) 80 { 81 printf("\nStarting kernel ...%s\n\n", fake ? 82 "(fake run for tracing)" : ""); 83 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel"); 84 #ifdef CONFIG_BOOTSTAGE_FDT 85 bootstage_fdt_add_report(); 86 #endif 87 #ifdef CONFIG_BOOTSTAGE_REPORT 88 bootstage_report(); 89 #endif 90 91 #ifdef CONFIG_USB_DEVICE 92 udc_disconnect(); 93 #endif 94 95 #ifdef CONFIG_ARCH_ROCKCHIP 96 /* Enable this flag, call putc to flush console(ns16550_serial_putc)*/ 97 gd->flags |= GD_FLG_OS_RUN; 98 /* 99 * This putc is only for calling ns16550_serial_putc() to flush console. 100 * Console uclass framework is quite complicated, it's not easy to 101 * flush console by privoding a new interface which must provide a 102 * udevice here, so we use an easy way to achieve that. 103 */ 104 putc('\n'); 105 #endif 106 107 board_quiesce_devices(); 108 109 /* 110 * Call remove function of all devices with a removal flag set. 111 * This may be useful for last-stage operations, like cancelling 112 * of DMA operation or releasing device internal buffers. 113 */ 114 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); 115 116 cleanup_before_linux(); 117 } 118 119 static void setup_start_tag (bd_t *bd) 120 { 121 params = (struct tag *)bd->bi_boot_params; 122 123 params->hdr.tag = ATAG_CORE; 124 params->hdr.size = tag_size (tag_core); 125 126 params->u.core.flags = 0; 127 params->u.core.pagesize = 0; 128 params->u.core.rootdev = 0; 129 130 params = tag_next (params); 131 } 132 133 static void setup_memory_tags(bd_t *bd) 134 { 135 int i; 136 137 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 138 params->hdr.tag = ATAG_MEM; 139 params->hdr.size = tag_size (tag_mem32); 140 141 params->u.mem.start = bd->bi_dram[i].start; 142 params->u.mem.size = bd->bi_dram[i].size; 143 144 params = tag_next (params); 145 } 146 } 147 148 static void setup_commandline_tag(bd_t *bd, char *commandline) 149 { 150 char *p; 151 152 if (!commandline) 153 return; 154 155 /* eat leading white space */ 156 for (p = commandline; *p == ' '; p++); 157 158 /* skip non-existent command lines so the kernel will still 159 * use its default command line. 160 */ 161 if (*p == '\0') 162 return; 163 164 params->hdr.tag = ATAG_CMDLINE; 165 params->hdr.size = 166 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2; 167 168 strcpy (params->u.cmdline.cmdline, p); 169 170 params = tag_next (params); 171 } 172 173 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end) 174 { 175 /* an ATAG_INITRD node tells the kernel where the compressed 176 * ramdisk can be found. ATAG_RDIMG is a better name, actually. 177 */ 178 params->hdr.tag = ATAG_INITRD2; 179 params->hdr.size = tag_size (tag_initrd); 180 181 params->u.initrd.start = initrd_start; 182 params->u.initrd.size = initrd_end - initrd_start; 183 184 params = tag_next (params); 185 } 186 187 static void setup_serial_tag(struct tag **tmp) 188 { 189 struct tag *params = *tmp; 190 struct tag_serialnr serialnr; 191 192 get_board_serial(&serialnr); 193 params->hdr.tag = ATAG_SERIAL; 194 params->hdr.size = tag_size (tag_serialnr); 195 params->u.serialnr.low = serialnr.low; 196 params->u.serialnr.high= serialnr.high; 197 params = tag_next (params); 198 *tmp = params; 199 } 200 201 static void setup_revision_tag(struct tag **in_params) 202 { 203 u32 rev = 0; 204 205 rev = get_board_rev(); 206 params->hdr.tag = ATAG_REVISION; 207 params->hdr.size = tag_size (tag_revision); 208 params->u.revision.rev = rev; 209 params = tag_next (params); 210 } 211 212 static void setup_end_tag(bd_t *bd) 213 { 214 params->hdr.tag = ATAG_NONE; 215 params->hdr.size = 0; 216 } 217 218 __weak void setup_board_tags(struct tag **in_params) {} 219 220 #ifdef CONFIG_ARM64 221 static void do_nonsec_virt_switch(void) 222 { 223 smp_kick_all_cpus(); 224 dcache_disable(); /* flush cache before swtiching to EL2 */ 225 } 226 #endif 227 228 /* Subcommand: PREP */ 229 static void boot_prep_linux(bootm_headers_t *images) 230 { 231 char *commandline = env_get("bootargs"); 232 233 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) { 234 #ifdef CONFIG_OF_LIBFDT 235 debug("using: FDT\n"); 236 if (image_setup_linux(images)) { 237 printf("FDT creation failed! hanging..."); 238 hang(); 239 } 240 #endif 241 } else if (BOOTM_ENABLE_TAGS) { 242 debug("using: ATAGS\n"); 243 setup_start_tag(gd->bd); 244 if (BOOTM_ENABLE_SERIAL_TAG) 245 setup_serial_tag(¶ms); 246 if (BOOTM_ENABLE_CMDLINE_TAG) 247 setup_commandline_tag(gd->bd, commandline); 248 if (BOOTM_ENABLE_REVISION_TAG) 249 setup_revision_tag(¶ms); 250 if (BOOTM_ENABLE_MEMORY_TAGS) 251 setup_memory_tags(gd->bd); 252 if (BOOTM_ENABLE_INITRD_TAG) { 253 /* 254 * In boot_ramdisk_high(), it may relocate ramdisk to 255 * a specified location. And set images->initrd_start & 256 * images->initrd_end to relocated ramdisk's start/end 257 * addresses. So use them instead of images->rd_start & 258 * images->rd_end when possible. 259 */ 260 if (images->initrd_start && images->initrd_end) { 261 setup_initrd_tag(gd->bd, images->initrd_start, 262 images->initrd_end); 263 } else if (images->rd_start && images->rd_end) { 264 setup_initrd_tag(gd->bd, images->rd_start, 265 images->rd_end); 266 } 267 } 268 setup_board_tags(¶ms); 269 setup_end_tag(gd->bd); 270 } else { 271 printf("FDT and ATAGS support not compiled in - hanging\n"); 272 hang(); 273 } 274 } 275 276 __weak bool armv7_boot_nonsec_default(void) 277 { 278 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT 279 return false; 280 #else 281 return true; 282 #endif 283 } 284 285 #ifdef CONFIG_ARMV7_NONSEC 286 bool armv7_boot_nonsec(void) 287 { 288 char *s = env_get("bootm_boot_mode"); 289 bool nonsec = armv7_boot_nonsec_default(); 290 291 if (s && !strcmp(s, "sec")) 292 nonsec = false; 293 294 if (s && !strcmp(s, "nonsec")) 295 nonsec = true; 296 297 return nonsec; 298 } 299 #endif 300 301 #ifdef CONFIG_ARM64 302 __weak void update_os_arch_secondary_cores(uint8_t os_arch) 303 { 304 } 305 306 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1 307 static void switch_to_el1(void) 308 { 309 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) && 310 (images.os.arch == IH_ARCH_ARM)) 311 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number, 312 (u64)images.ft_addr, 0, 313 (u64)images.ep, 314 ES_TO_AARCH32); 315 else 316 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0, 317 images.ep, 318 ES_TO_AARCH64); 319 } 320 #endif 321 #endif 322 323 /* Subcommand: GO */ 324 static void boot_jump_linux(bootm_headers_t *images, int flag) 325 { 326 #ifdef CONFIG_ARM64 327 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1, 328 void *res2); 329 int fake = (flag & BOOTM_STATE_OS_FAKE_GO); 330 331 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1, 332 void *res2))images->ep; 333 334 debug("## Transferring control to Linux (at address %lx)...\n", 335 (ulong) kernel_entry); 336 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 337 338 announce_and_cleanup(fake); 339 340 if (!fake) { 341 #ifdef CONFIG_ARMV8_PSCI 342 armv8_setup_psci(); 343 #endif 344 do_nonsec_virt_switch(); 345 346 update_os_arch_secondary_cores(images->os.arch); 347 348 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1 349 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0, 350 (u64)switch_to_el1, ES_TO_AARCH64); 351 #else 352 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) && 353 (images->os.arch == IH_ARCH_ARM)) 354 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number, 355 (u64)images->ft_addr, 0, 356 (u64)images->ep, 357 ES_TO_AARCH32); 358 else 359 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0, 360 images->ep, 361 ES_TO_AARCH64); 362 #endif 363 } 364 #else 365 unsigned long machid = gd->bd->bi_arch_number; 366 char *s; 367 void (*kernel_entry)(int zero, int arch, uint params); 368 unsigned long r2; 369 int fake = (flag & BOOTM_STATE_OS_FAKE_GO); 370 371 kernel_entry = (void (*)(int, int, uint))images->ep; 372 #ifdef CONFIG_CPU_V7M 373 ulong addr = (ulong)kernel_entry | 1; 374 kernel_entry = (void *)addr; 375 #endif 376 s = env_get("machid"); 377 if (s) { 378 if (strict_strtoul(s, 16, &machid) < 0) { 379 debug("strict_strtoul failed!\n"); 380 return; 381 } 382 printf("Using machid 0x%lx from environment\n", machid); 383 } 384 385 debug("## Transferring control to Linux (at address %08lx)" \ 386 "...\n", (ulong) kernel_entry); 387 bootstage_mark(BOOTSTAGE_ID_RUN_OS); 388 announce_and_cleanup(fake); 389 390 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) 391 r2 = (unsigned long)images->ft_addr; 392 else 393 r2 = gd->bd->bi_boot_params; 394 395 if (!fake) { 396 #ifdef CONFIG_ARMV7_NONSEC 397 if (armv7_boot_nonsec()) { 398 armv7_init_nonsec(); 399 secure_ram_addr(_do_nonsec_entry)(kernel_entry, 400 0, machid, r2); 401 } else 402 #endif 403 kernel_entry(0, machid, r2); 404 } 405 #endif 406 } 407 408 /* Main Entry point for arm bootm implementation 409 * 410 * Modeled after the powerpc implementation 411 * DIFFERENCE: Instead of calling prep and go at the end 412 * they are called if subcommand is equal 0. 413 */ 414 int do_bootm_linux(int flag, int argc, char * const argv[], 415 bootm_headers_t *images) 416 { 417 /* No need for those on ARM */ 418 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE) 419 return -1; 420 421 if (flag & BOOTM_STATE_OS_PREP) { 422 boot_prep_linux(images); 423 return 0; 424 } 425 426 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) { 427 boot_jump_linux(images, flag); 428 return 0; 429 } 430 431 boot_prep_linux(images); 432 boot_jump_linux(images, flag); 433 return 0; 434 } 435 436 #if defined(CONFIG_BOOTM_VXWORKS) 437 void boot_prep_vxworks(bootm_headers_t *images) 438 { 439 #if defined(CONFIG_OF_LIBFDT) 440 int off; 441 442 if (images->ft_addr) { 443 off = fdt_path_offset(images->ft_addr, "/memory"); 444 if (off < 0) { 445 if (arch_fixup_fdt(images->ft_addr)) 446 puts("## WARNING: fixup memory failed!\n"); 447 } 448 } 449 #endif 450 cleanup_before_linux(); 451 } 452 void boot_jump_vxworks(bootm_headers_t *images) 453 { 454 /* ARM VxWorks requires device tree physical address to be passed */ 455 ((void (*)(void *))images->ep)(images->ft_addr); 456 } 457 #endif 458