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