2 * Early boot support code for BootX bootloader
4 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/init.h>
15 #include <linux/utsrelease.h>
16 #include <asm/sections.h>
19 #include <asm/bootx.h>
20 #include <asm/btext.h>
27 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
29 #define DBG(fmt...) do { } while(0)
32 extern void __start(unsigned long r3
, unsigned long r4
, unsigned long r5
);
34 static unsigned long __initdata bootx_dt_strbase
;
35 static unsigned long __initdata bootx_dt_strend
;
36 static unsigned long __initdata bootx_node_chosen
;
37 static boot_infos_t
* __initdata bootx_info
;
38 static char __initdata bootx_disp_path
[256];
40 /* Is boot-info compatible ? */
41 #define BOOT_INFO_IS_COMPATIBLE(bi) \
42 ((bi)->compatible_version <= BOOT_INFO_VERSION)
43 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
44 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
46 #ifdef CONFIG_BOOTX_TEXT
47 static void __init
bootx_printf(const char *format
, ...)
49 const char *p
, *q
, *s
;
53 va_start(args
, format
);
54 for (p
= format
; *p
!= 0; p
= q
) {
55 for (q
= p
; *q
!= 0 && *q
!= '\n' && *q
!= '%'; ++q
)
58 btext_drawtext(p
, q
- p
);
64 btext_drawstring("\r\n");
74 s
= va_arg(args
, const char *);
81 v
= va_arg(args
, unsigned long);
87 #else /* CONFIG_BOOTX_TEXT */
88 static void __init
bootx_printf(const char *format
, ...) {}
89 #endif /* CONFIG_BOOTX_TEXT */
91 static void * __init
bootx_early_getprop(unsigned long base
,
95 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
96 u32
*ppp
= &np
->properties
;
99 struct bootx_dt_prop
*pp
=
100 (struct bootx_dt_prop
*)(base
+ *ppp
);
102 if (strcmp((char *)((unsigned long)pp
->name
+ base
),
104 return (void *)((unsigned long)pp
->value
+ base
);
111 #define dt_push_token(token, mem) \
113 *(mem) = _ALIGN_UP(*(mem),4); \
114 *((u32 *)*(mem)) = token; \
118 static unsigned long __init
bootx_dt_find_string(char *str
)
122 s
= os
= (char *)bootx_dt_strbase
;
124 while (s
< (char *)bootx_dt_strend
) {
125 if (strcmp(s
, str
) == 0)
132 static void __init
bootx_dt_add_prop(char *name
, void *data
, int size
,
133 unsigned long *mem_end
)
135 unsigned long soff
= bootx_dt_find_string(name
);
139 bootx_printf("WARNING: Can't find string index for <%s>\n",
143 if (size
> 0x20000) {
144 bootx_printf("WARNING: ignoring large property ");
145 bootx_printf("%s length 0x%x\n", name
, size
);
148 dt_push_token(OF_DT_PROP
, mem_end
);
149 dt_push_token(size
, mem_end
);
150 dt_push_token(soff
, mem_end
);
152 /* push property content */
154 memcpy((void *)*mem_end
, data
, size
);
155 *mem_end
= _ALIGN_UP(*mem_end
+ size
, 4);
159 static void __init
bootx_add_chosen_props(unsigned long base
,
160 unsigned long *mem_end
)
164 bootx_dt_add_prop("linux,bootx", NULL
, 0, mem_end
);
166 if (bootx_info
->kernelParamsOffset
) {
167 char *args
= (char *)((unsigned long)bootx_info
) +
168 bootx_info
->kernelParamsOffset
;
169 bootx_dt_add_prop("bootargs", args
, strlen(args
) + 1, mem_end
);
171 if (bootx_info
->ramDisk
) {
172 val
= ((unsigned long)bootx_info
) + bootx_info
->ramDisk
;
173 bootx_dt_add_prop("linux,initrd-start", &val
, 4, mem_end
);
174 val
+= bootx_info
->ramDiskSize
;
175 bootx_dt_add_prop("linux,initrd-end", &val
, 4, mem_end
);
177 if (strlen(bootx_disp_path
))
178 bootx_dt_add_prop("linux,stdout-path", bootx_disp_path
,
179 strlen(bootx_disp_path
) + 1, mem_end
);
182 static void __init
bootx_add_display_props(unsigned long base
,
183 unsigned long *mem_end
,
186 boot_infos_t
*bi
= bootx_info
;
190 bootx_dt_add_prop("linux,boot-display", NULL
, 0, mem_end
);
191 bootx_dt_add_prop("linux,opened", NULL
, 0, mem_end
);
193 bootx_dt_add_prop("linux,bootx-noscreen", NULL
, 0, mem_end
);
195 tmp
= bi
->dispDeviceDepth
;
196 bootx_dt_add_prop("linux,bootx-depth", &tmp
, 4, mem_end
);
197 tmp
= bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0];
198 bootx_dt_add_prop("linux,bootx-width", &tmp
, 4, mem_end
);
199 tmp
= bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1];
200 bootx_dt_add_prop("linux,bootx-height", &tmp
, 4, mem_end
);
201 tmp
= bi
->dispDeviceRowBytes
;
202 bootx_dt_add_prop("linux,bootx-linebytes", &tmp
, 4, mem_end
);
203 tmp
= (u32
)bi
->dispDeviceBase
;
205 tmp
= (u32
)bi
->logicalDisplayBase
;
206 tmp
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
207 tmp
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
208 bootx_dt_add_prop("linux,bootx-addr", &tmp
, 4, mem_end
);
211 static void __init
bootx_dt_add_string(char *s
, unsigned long *mem_end
)
213 unsigned int l
= strlen(s
) + 1;
214 memcpy((void *)*mem_end
, s
, l
);
215 bootx_dt_strend
= *mem_end
= *mem_end
+ l
;
218 static void __init
bootx_scan_dt_build_strings(unsigned long base
,
220 unsigned long *mem_end
)
222 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
223 u32
*cpp
, *ppp
= &np
->properties
;
227 /* Keep refs to known nodes */
228 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
230 bootx_printf("Node without a full name !\n");
233 DBG("* strings: %s\n", namep
);
235 if (!strcmp(namep
, "/chosen")) {
236 DBG(" detected /chosen ! adding properties names !\n");
237 bootx_dt_add_string("linux,bootx", mem_end
);
238 bootx_dt_add_string("linux,stdout-path", mem_end
);
239 bootx_dt_add_string("linux,initrd-start", mem_end
);
240 bootx_dt_add_string("linux,initrd-end", mem_end
);
241 bootx_dt_add_string("bootargs", mem_end
);
242 bootx_node_chosen
= node
;
244 if (node
== bootx_info
->dispDeviceRegEntryOffset
) {
245 DBG(" detected display ! adding properties names !\n");
246 bootx_dt_add_string("linux,boot-display", mem_end
);
247 bootx_dt_add_string("linux,opened", mem_end
);
248 strncpy(bootx_disp_path
, namep
, 255);
251 /* get and store all property names */
253 struct bootx_dt_prop
*pp
=
254 (struct bootx_dt_prop
*)(base
+ *ppp
);
256 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
257 if (namep
== NULL
|| strcmp(namep
, "name") == 0)
259 /* get/create string entry */
260 soff
= bootx_dt_find_string(namep
);
262 bootx_dt_add_string(namep
, mem_end
);
267 /* do all our children */
270 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
271 bootx_scan_dt_build_strings(base
, *cpp
, mem_end
);
276 static void __init
bootx_scan_dt_build_struct(unsigned long base
,
278 unsigned long *mem_end
)
280 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
281 u32
*cpp
, *ppp
= &np
->properties
;
282 char *namep
, *p
, *ep
, *lp
;
285 dt_push_token(OF_DT_BEGIN_NODE
, mem_end
);
287 /* get the node's full name */
288 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
293 DBG("* struct: %s\n", namep
);
295 /* Fixup an Apple bug where they have bogus \0 chars in the
296 * middle of the path in some properties, and extract
297 * the unit name (everything after the last '/').
299 memcpy((void *)*mem_end
, namep
, l
+ 1);
300 namep
= (char *)*mem_end
;
301 for (lp
= p
= namep
, ep
= namep
+ l
; p
< ep
; p
++) {
308 *mem_end
= _ALIGN_UP((unsigned long)lp
+ 1, 4);
310 /* get and store all properties */
312 struct bootx_dt_prop
*pp
=
313 (struct bootx_dt_prop
*)(base
+ *ppp
);
315 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
317 if (namep
== NULL
|| !strcmp(namep
, "name"))
319 /* Skip "bootargs" in /chosen too as we replace it */
320 if (node
== bootx_node_chosen
&& !strcmp(namep
, "bootargs"))
323 /* push property head */
324 bootx_dt_add_prop(namep
,
325 pp
->value
? (void *)(base
+ pp
->value
): NULL
,
326 pp
->length
, mem_end
);
331 if (node
== bootx_node_chosen
) {
332 bootx_add_chosen_props(base
, mem_end
);
333 if (bootx_info
->dispDeviceRegEntryOffset
== 0)
334 bootx_add_display_props(base
, mem_end
, 0);
336 else if (node
== bootx_info
->dispDeviceRegEntryOffset
)
337 bootx_add_display_props(base
, mem_end
, 1);
339 /* do all our children */
342 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
343 bootx_scan_dt_build_struct(base
, *cpp
, mem_end
);
347 dt_push_token(OF_DT_END_NODE
, mem_end
);
350 static unsigned long __init
bootx_flatten_dt(unsigned long start
)
352 boot_infos_t
*bi
= bootx_info
;
353 unsigned long mem_start
, mem_end
;
354 struct boot_param_header
*hdr
;
358 /* Start using memory after the big blob passed by BootX, get
359 * some space for the header
361 mem_start
= mem_end
= _ALIGN_UP(((unsigned long)bi
) + start
, 4);
362 DBG("Boot params header at: %x\n", mem_start
);
363 hdr
= (struct boot_param_header
*)mem_start
;
364 mem_end
+= sizeof(struct boot_param_header
);
365 rsvmap
= (u64
*)(_ALIGN_UP(mem_end
, 8));
366 hdr
->off_mem_rsvmap
= ((unsigned long)rsvmap
) - mem_start
;
367 mem_end
= ((unsigned long)rsvmap
) + 8 * sizeof(u64
);
369 /* Get base of tree */
370 base
= ((unsigned long)bi
) + bi
->deviceTreeOffset
;
372 /* Build string array */
373 DBG("Building string array at: %x\n", mem_end
);
374 DBG("Device Tree Base=%x\n", base
);
375 bootx_dt_strbase
= mem_end
;
377 bootx_dt_strend
= mem_end
;
378 bootx_scan_dt_build_strings(base
, 4, &mem_end
);
379 /* Add some strings */
380 bootx_dt_add_string("linux,bootx-noscreen", &mem_end
);
381 bootx_dt_add_string("linux,bootx-depth", &mem_end
);
382 bootx_dt_add_string("linux,bootx-width", &mem_end
);
383 bootx_dt_add_string("linux,bootx-height", &mem_end
);
384 bootx_dt_add_string("linux,bootx-linebytes", &mem_end
);
385 bootx_dt_add_string("linux,bootx-addr", &mem_end
);
386 /* Wrap up strings */
387 hdr
->off_dt_strings
= bootx_dt_strbase
- mem_start
;
388 hdr
->dt_strings_size
= bootx_dt_strend
- bootx_dt_strbase
;
390 /* Build structure */
391 mem_end
= _ALIGN(mem_end
, 16);
392 DBG("Building device tree structure at: %x\n", mem_end
);
393 hdr
->off_dt_struct
= mem_end
- mem_start
;
394 bootx_scan_dt_build_struct(base
, 4, &mem_end
);
395 dt_push_token(OF_DT_END
, &mem_end
);
398 hdr
->boot_cpuid_phys
= 0;
399 hdr
->magic
= OF_DT_HEADER
;
400 hdr
->totalsize
= mem_end
- mem_start
;
401 hdr
->version
= OF_DT_VERSION
;
402 /* Version 16 is not backward compatible */
403 hdr
->last_comp_version
= 0x10;
405 /* Reserve the whole thing and copy the reserve map in, we
406 * also bump mem_reserve_cnt to cause further reservations to
407 * fail since it's too late.
409 mem_end
= _ALIGN(mem_end
, PAGE_SIZE
);
410 DBG("End of boot params: %x\n", mem_end
);
411 rsvmap
[0] = mem_start
;
413 if (bootx_info
->ramDisk
) {
414 rsvmap
[2] = ((unsigned long)bootx_info
) + bootx_info
->ramDisk
;
415 rsvmap
[3] = rsvmap
[2] + bootx_info
->ramDiskSize
;
423 return (unsigned long)hdr
;
427 #ifdef CONFIG_BOOTX_TEXT
428 static void __init
btext_welcome(boot_infos_t
*bi
)
433 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE
"\n");
434 bootx_printf("\nlinked at : 0x%x", KERNELBASE
);
435 bootx_printf("\nframe buffer at : 0x%x", bi
->dispDeviceBase
);
436 bootx_printf(" (phys), 0x%x", bi
->logicalDisplayBase
);
437 bootx_printf(" (log)");
438 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit
);
439 bootx_printf("\nboot_info at : 0x%x", bi
);
440 __asm__
__volatile__ ("mfmsr %0" : "=r" (flags
));
441 bootx_printf("\nMSR : 0x%x", flags
);
442 __asm__
__volatile__ ("mfspr %0, 287" : "=r" (pvr
));
443 bootx_printf("\nPVR : 0x%x", pvr
);
446 __asm__
__volatile__ ("mfspr %0, 1008" : "=r" (flags
));
447 bootx_printf("\nHID0 : 0x%x", flags
);
449 if (pvr
== 8 || pvr
== 12 || pvr
== 0x800c) {
450 __asm__
__volatile__ ("mfspr %0, 1019" : "=r" (flags
));
451 bootx_printf("\nICTC : 0x%x", flags
);
454 bootx_printf("\n\n");
455 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
456 bi
->deviceTreeOffset
);
457 bootx_printf("bi->deviceTreeSize : 0x%x\n",
460 bootx_printf("\n\n");
462 #endif /* CONFIG_BOOTX_TEXT */
464 void __init
bootx_init(unsigned long r3
, unsigned long r4
)
466 boot_infos_t
*bi
= (boot_infos_t
*) r4
;
469 unsigned long ptr
, x
;
471 unsigned long offset
= reloc_offset();
477 /* We haven't cleared any bss at this point, make sure
478 * what we need is initialized
480 bootx_dt_strbase
= bootx_dt_strend
= 0;
481 bootx_node_chosen
= 0;
482 bootx_disp_path
[0] = 0;
484 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi
))
485 bi
->logicalDisplayBase
= bi
->dispDeviceBase
;
487 /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
488 if (bi
->dispDeviceDepth
== 16)
489 bi
->dispDeviceDepth
= 15;
492 #ifdef CONFIG_BOOTX_TEXT
493 ptr
= (unsigned long)bi
->logicalDisplayBase
;
494 ptr
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
495 ptr
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
496 btext_setup_display(bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0],
497 bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1],
498 bi
->dispDeviceDepth
, bi
->dispDeviceRowBytes
,
499 (unsigned long)bi
->logicalDisplayBase
);
502 #endif /* CONFIG_BOOTX_TEXT */
505 * Test if boot-info is compatible. Done only in config
506 * CONFIG_BOOTX_TEXT since there is nothing much we can do
507 * with an incompatible version, except display a message
508 * and eventually hang the processor...
510 * I'll try to keep enough of boot-info compatible in the
511 * future to always allow display of this message;
513 if (!BOOT_INFO_IS_COMPATIBLE(bi
)) {
514 bootx_printf(" !!! WARNING - Incompatible version"
515 " of BootX !!!\n\n\n");
519 if (bi
->architecture
!= BOOT_ARCH_PCI
) {
520 bootx_printf(" !!! WARNING - Usupported machine"
521 " architecture !\n");
526 #ifdef CONFIG_BOOTX_TEXT
530 /* New BootX enters kernel with MMU off, i/os are not allowed
531 * here. This hack will have been done by the boostrap anyway.
533 if (bi
->version
< 4) {
535 * XXX If this is an iMac, turn off the USB controller.
537 model
= (char *) bootx_early_getprop(r4
+ bi
->deviceTreeOffset
,
540 && (strcmp(model
, "iMac,1") == 0
541 || strcmp(model
, "PowerMac1,1") == 0)) {
542 bootx_printf("iMac,1 detected, shutting down USB \n");
543 out_le32((unsigned __iomem
*)0x80880008, 1); /* XXX */
547 /* Get a pointer that points above the device tree, args, ramdisk,
548 * etc... to use for generating the flattened tree
550 if (bi
->version
< 5) {
551 space
= bi
->deviceTreeOffset
+ bi
->deviceTreeSize
;
552 if (bi
->ramDisk
>= space
)
553 space
= bi
->ramDisk
+ bi
->ramDiskSize
;
555 space
= bi
->totalParamsSize
;
557 bootx_printf("Total space used by parameters & ramdisk: 0x%x \n", space
);
559 /* New BootX will have flushed all TLBs and enters kernel with
560 * MMU switched OFF, so this should not be useful anymore.
562 if (bi
->version
< 4) {
563 bootx_printf("Touching pages...\n");
566 * Touch each page to make sure the PTEs for them
567 * are in the hash table - the aim is to try to avoid
568 * getting DSI exceptions while copying the kernel image.
570 for (ptr
= ((unsigned long) &_stext
) & PAGE_MASK
;
571 ptr
< (unsigned long)bi
+ space
; ptr
+= PAGE_SIZE
)
572 x
= *(volatile unsigned long *)ptr
;
575 /* Ok, now we need to generate a flattened device-tree to pass
578 bootx_printf("Preparing boot params...\n");
580 hdr
= bootx_flatten_dt(space
);
582 #ifdef CONFIG_BOOTX_TEXT
584 bootx_printf("Preparing BAT...\n");
593 __start(hdr
, KERNELBASE
+ offset
, 0);