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 <generated/utsrelease.h>
16 #include <asm/sections.h>
19 #include <asm/bootx.h>
20 #include <asm/btext.h>
22 #include <asm/setup.h>
28 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
30 #define DBG(fmt...) do { } while(0)
33 extern void __start(unsigned long r3
, unsigned long r4
, unsigned long r5
);
35 static unsigned long __initdata bootx_dt_strbase
;
36 static unsigned long __initdata bootx_dt_strend
;
37 static unsigned long __initdata bootx_node_chosen
;
38 static boot_infos_t
* __initdata bootx_info
;
39 static char __initdata bootx_disp_path
[256];
41 /* Is boot-info compatible ? */
42 #define BOOT_INFO_IS_COMPATIBLE(bi) \
43 ((bi)->compatible_version <= BOOT_INFO_VERSION)
44 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
45 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
47 #ifdef CONFIG_BOOTX_TEXT
48 static void __init
bootx_printf(const char *format
, ...)
50 const char *p
, *q
, *s
;
54 va_start(args
, format
);
55 for (p
= format
; *p
!= 0; p
= q
) {
56 for (q
= p
; *q
!= 0 && *q
!= '\n' && *q
!= '%'; ++q
)
59 btext_drawtext(p
, q
- p
);
65 btext_drawstring("\r\n");
75 s
= va_arg(args
, const char *);
82 v
= va_arg(args
, unsigned long);
89 #else /* CONFIG_BOOTX_TEXT */
90 static void __init
bootx_printf(const char *format
, ...) {}
91 #endif /* CONFIG_BOOTX_TEXT */
93 static void * __init
bootx_early_getprop(unsigned long base
,
97 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
98 u32
*ppp
= &np
->properties
;
101 struct bootx_dt_prop
*pp
=
102 (struct bootx_dt_prop
*)(base
+ *ppp
);
104 if (strcmp((char *)((unsigned long)pp
->name
+ base
),
106 return (void *)((unsigned long)pp
->value
+ base
);
113 #define dt_push_token(token, mem) \
115 *(mem) = _ALIGN_UP(*(mem),4); \
116 *((u32 *)*(mem)) = token; \
120 static unsigned long __init
bootx_dt_find_string(char *str
)
124 s
= os
= (char *)bootx_dt_strbase
;
126 while (s
< (char *)bootx_dt_strend
) {
127 if (strcmp(s
, str
) == 0)
134 static void __init
bootx_dt_add_prop(char *name
, void *data
, int size
,
135 unsigned long *mem_end
)
137 unsigned long soff
= bootx_dt_find_string(name
);
141 bootx_printf("WARNING: Can't find string index for <%s>\n",
145 if (size
> 0x20000) {
146 bootx_printf("WARNING: ignoring large property ");
147 bootx_printf("%s length 0x%x\n", name
, size
);
150 dt_push_token(OF_DT_PROP
, mem_end
);
151 dt_push_token(size
, mem_end
);
152 dt_push_token(soff
, mem_end
);
154 /* push property content */
156 memcpy((void *)*mem_end
, data
, size
);
157 *mem_end
= _ALIGN_UP(*mem_end
+ size
, 4);
161 static void __init
bootx_add_chosen_props(unsigned long base
,
162 unsigned long *mem_end
)
166 bootx_dt_add_prop("linux,bootx", NULL
, 0, mem_end
);
168 if (bootx_info
->kernelParamsOffset
) {
169 char *args
= (char *)((unsigned long)bootx_info
) +
170 bootx_info
->kernelParamsOffset
;
171 bootx_dt_add_prop("bootargs", args
, strlen(args
) + 1, mem_end
);
173 if (bootx_info
->ramDisk
) {
174 val
= ((unsigned long)bootx_info
) + bootx_info
->ramDisk
;
175 bootx_dt_add_prop("linux,initrd-start", &val
, 4, mem_end
);
176 val
+= bootx_info
->ramDiskSize
;
177 bootx_dt_add_prop("linux,initrd-end", &val
, 4, mem_end
);
179 if (strlen(bootx_disp_path
))
180 bootx_dt_add_prop("linux,stdout-path", bootx_disp_path
,
181 strlen(bootx_disp_path
) + 1, mem_end
);
184 static void __init
bootx_add_display_props(unsigned long base
,
185 unsigned long *mem_end
,
188 boot_infos_t
*bi
= bootx_info
;
192 bootx_dt_add_prop("linux,boot-display", NULL
, 0, mem_end
);
193 bootx_dt_add_prop("linux,opened", NULL
, 0, mem_end
);
195 bootx_dt_add_prop("linux,bootx-noscreen", NULL
, 0, mem_end
);
197 tmp
= bi
->dispDeviceDepth
;
198 bootx_dt_add_prop("linux,bootx-depth", &tmp
, 4, mem_end
);
199 tmp
= bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0];
200 bootx_dt_add_prop("linux,bootx-width", &tmp
, 4, mem_end
);
201 tmp
= bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1];
202 bootx_dt_add_prop("linux,bootx-height", &tmp
, 4, mem_end
);
203 tmp
= bi
->dispDeviceRowBytes
;
204 bootx_dt_add_prop("linux,bootx-linebytes", &tmp
, 4, mem_end
);
205 tmp
= (u32
)bi
->dispDeviceBase
;
207 tmp
= (u32
)bi
->logicalDisplayBase
;
208 tmp
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
209 tmp
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
210 bootx_dt_add_prop("linux,bootx-addr", &tmp
, 4, mem_end
);
213 static void __init
bootx_dt_add_string(char *s
, unsigned long *mem_end
)
215 unsigned int l
= strlen(s
) + 1;
216 memcpy((void *)*mem_end
, s
, l
);
217 bootx_dt_strend
= *mem_end
= *mem_end
+ l
;
220 static void __init
bootx_scan_dt_build_strings(unsigned long base
,
222 unsigned long *mem_end
)
224 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
225 u32
*cpp
, *ppp
= &np
->properties
;
229 /* Keep refs to known nodes */
230 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
232 bootx_printf("Node without a full name !\n");
235 DBG("* strings: %s\n", namep
);
237 if (!strcmp(namep
, "/chosen")) {
238 DBG(" detected /chosen ! adding properties names !\n");
239 bootx_dt_add_string("linux,bootx", mem_end
);
240 bootx_dt_add_string("linux,stdout-path", mem_end
);
241 bootx_dt_add_string("linux,initrd-start", mem_end
);
242 bootx_dt_add_string("linux,initrd-end", mem_end
);
243 bootx_dt_add_string("bootargs", mem_end
);
244 bootx_node_chosen
= node
;
246 if (node
== bootx_info
->dispDeviceRegEntryOffset
) {
247 DBG(" detected display ! adding properties names !\n");
248 bootx_dt_add_string("linux,boot-display", mem_end
);
249 bootx_dt_add_string("linux,opened", mem_end
);
250 strlcpy(bootx_disp_path
, namep
, sizeof(bootx_disp_path
));
253 /* get and store all property names */
255 struct bootx_dt_prop
*pp
=
256 (struct bootx_dt_prop
*)(base
+ *ppp
);
258 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
259 if (namep
== NULL
|| strcmp(namep
, "name") == 0)
261 /* get/create string entry */
262 soff
= bootx_dt_find_string(namep
);
264 bootx_dt_add_string(namep
, mem_end
);
269 /* do all our children */
272 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
273 bootx_scan_dt_build_strings(base
, *cpp
, mem_end
);
278 static void __init
bootx_scan_dt_build_struct(unsigned long base
,
280 unsigned long *mem_end
)
282 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
283 u32
*cpp
, *ppp
= &np
->properties
;
284 char *namep
, *p
, *ep
, *lp
;
287 dt_push_token(OF_DT_BEGIN_NODE
, mem_end
);
289 /* get the node's full name */
290 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
295 DBG("* struct: %s\n", namep
);
297 /* Fixup an Apple bug where they have bogus \0 chars in the
298 * middle of the path in some properties, and extract
299 * the unit name (everything after the last '/').
301 memcpy((void *)*mem_end
, namep
, l
+ 1);
302 namep
= (char *)*mem_end
;
303 for (lp
= p
= namep
, ep
= namep
+ l
; p
< ep
; p
++) {
310 *mem_end
= _ALIGN_UP((unsigned long)lp
+ 1, 4);
312 /* get and store all properties */
314 struct bootx_dt_prop
*pp
=
315 (struct bootx_dt_prop
*)(base
+ *ppp
);
317 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
319 if (namep
== NULL
|| !strcmp(namep
, "name"))
321 /* Skip "bootargs" in /chosen too as we replace it */
322 if (node
== bootx_node_chosen
&& !strcmp(namep
, "bootargs"))
325 /* push property head */
326 bootx_dt_add_prop(namep
,
327 pp
->value
? (void *)(base
+ pp
->value
): NULL
,
328 pp
->length
, mem_end
);
333 if (node
== bootx_node_chosen
) {
334 bootx_add_chosen_props(base
, mem_end
);
335 if (bootx_info
->dispDeviceRegEntryOffset
== 0)
336 bootx_add_display_props(base
, mem_end
, 0);
338 else if (node
== bootx_info
->dispDeviceRegEntryOffset
)
339 bootx_add_display_props(base
, mem_end
, 1);
341 /* do all our children */
344 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
345 bootx_scan_dt_build_struct(base
, *cpp
, mem_end
);
349 dt_push_token(OF_DT_END_NODE
, mem_end
);
352 static unsigned long __init
bootx_flatten_dt(unsigned long start
)
354 boot_infos_t
*bi
= bootx_info
;
355 unsigned long mem_start
, mem_end
;
356 struct boot_param_header
*hdr
;
360 /* Start using memory after the big blob passed by BootX, get
361 * some space for the header
363 mem_start
= mem_end
= _ALIGN_UP(((unsigned long)bi
) + start
, 4);
364 DBG("Boot params header at: %x\n", mem_start
);
365 hdr
= (struct boot_param_header
*)mem_start
;
366 mem_end
+= sizeof(struct boot_param_header
);
367 rsvmap
= (u64
*)(_ALIGN_UP(mem_end
, 8));
368 hdr
->off_mem_rsvmap
= ((unsigned long)rsvmap
) - mem_start
;
369 mem_end
= ((unsigned long)rsvmap
) + 8 * sizeof(u64
);
371 /* Get base of tree */
372 base
= ((unsigned long)bi
) + bi
->deviceTreeOffset
;
374 /* Build string array */
375 DBG("Building string array at: %x\n", mem_end
);
376 DBG("Device Tree Base=%x\n", base
);
377 bootx_dt_strbase
= mem_end
;
379 bootx_dt_strend
= mem_end
;
380 bootx_scan_dt_build_strings(base
, 4, &mem_end
);
381 /* Add some strings */
382 bootx_dt_add_string("linux,bootx-noscreen", &mem_end
);
383 bootx_dt_add_string("linux,bootx-depth", &mem_end
);
384 bootx_dt_add_string("linux,bootx-width", &mem_end
);
385 bootx_dt_add_string("linux,bootx-height", &mem_end
);
386 bootx_dt_add_string("linux,bootx-linebytes", &mem_end
);
387 bootx_dt_add_string("linux,bootx-addr", &mem_end
);
388 /* Wrap up strings */
389 hdr
->off_dt_strings
= bootx_dt_strbase
- mem_start
;
390 hdr
->dt_strings_size
= bootx_dt_strend
- bootx_dt_strbase
;
392 /* Build structure */
393 mem_end
= _ALIGN(mem_end
, 16);
394 DBG("Building device tree structure at: %x\n", mem_end
);
395 hdr
->off_dt_struct
= mem_end
- mem_start
;
396 bootx_scan_dt_build_struct(base
, 4, &mem_end
);
397 dt_push_token(OF_DT_END
, &mem_end
);
400 hdr
->boot_cpuid_phys
= 0;
401 hdr
->magic
= OF_DT_HEADER
;
402 hdr
->totalsize
= mem_end
- mem_start
;
403 hdr
->version
= OF_DT_VERSION
;
404 /* Version 16 is not backward compatible */
405 hdr
->last_comp_version
= 0x10;
407 /* Reserve the whole thing and copy the reserve map in, we
408 * also bump mem_reserve_cnt to cause further reservations to
409 * fail since it's too late.
411 mem_end
= _ALIGN(mem_end
, PAGE_SIZE
);
412 DBG("End of boot params: %x\n", mem_end
);
413 rsvmap
[0] = mem_start
;
415 if (bootx_info
->ramDisk
) {
416 rsvmap
[2] = ((unsigned long)bootx_info
) + bootx_info
->ramDisk
;
417 rsvmap
[3] = rsvmap
[2] + bootx_info
->ramDiskSize
;
425 return (unsigned long)hdr
;
429 #ifdef CONFIG_BOOTX_TEXT
430 static void __init
btext_welcome(boot_infos_t
*bi
)
435 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE
"\n");
436 bootx_printf("\nlinked at : 0x%x", KERNELBASE
);
437 bootx_printf("\nframe buffer at : 0x%x", bi
->dispDeviceBase
);
438 bootx_printf(" (phys), 0x%x", bi
->logicalDisplayBase
);
439 bootx_printf(" (log)");
440 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit
);
441 bootx_printf("\nboot_info at : 0x%x", bi
);
442 __asm__
__volatile__ ("mfmsr %0" : "=r" (flags
));
443 bootx_printf("\nMSR : 0x%x", flags
);
444 __asm__
__volatile__ ("mfspr %0, 287" : "=r" (pvr
));
445 bootx_printf("\nPVR : 0x%x", pvr
);
448 __asm__
__volatile__ ("mfspr %0, 1008" : "=r" (flags
));
449 bootx_printf("\nHID0 : 0x%x", flags
);
451 if (pvr
== 8 || pvr
== 12 || pvr
== 0x800c) {
452 __asm__
__volatile__ ("mfspr %0, 1019" : "=r" (flags
));
453 bootx_printf("\nICTC : 0x%x", flags
);
456 bootx_printf("\n\n");
457 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
458 bi
->deviceTreeOffset
);
459 bootx_printf("bi->deviceTreeSize : 0x%x\n",
462 bootx_printf("\n\n");
464 #endif /* CONFIG_BOOTX_TEXT */
466 void __init
bootx_init(unsigned long r3
, unsigned long r4
)
468 boot_infos_t
*bi
= (boot_infos_t
*) r4
;
471 unsigned long ptr
, x
;
473 unsigned long offset
= reloc_offset();
479 /* We haven't cleared any bss at this point, make sure
480 * what we need is initialized
482 bootx_dt_strbase
= bootx_dt_strend
= 0;
483 bootx_node_chosen
= 0;
484 bootx_disp_path
[0] = 0;
486 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi
))
487 bi
->logicalDisplayBase
= bi
->dispDeviceBase
;
489 /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
490 if (bi
->dispDeviceDepth
== 16)
491 bi
->dispDeviceDepth
= 15;
494 #ifdef CONFIG_BOOTX_TEXT
495 ptr
= (unsigned long)bi
->logicalDisplayBase
;
496 ptr
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
497 ptr
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
498 btext_setup_display(bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0],
499 bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1],
500 bi
->dispDeviceDepth
, bi
->dispDeviceRowBytes
,
501 (unsigned long)bi
->logicalDisplayBase
);
504 #endif /* CONFIG_BOOTX_TEXT */
507 * Test if boot-info is compatible. Done only in config
508 * CONFIG_BOOTX_TEXT since there is nothing much we can do
509 * with an incompatible version, except display a message
510 * and eventually hang the processor...
512 * I'll try to keep enough of boot-info compatible in the
513 * future to always allow display of this message;
515 if (!BOOT_INFO_IS_COMPATIBLE(bi
)) {
516 bootx_printf(" !!! WARNING - Incompatible version"
517 " of BootX !!!\n\n\n");
521 if (bi
->architecture
!= BOOT_ARCH_PCI
) {
522 bootx_printf(" !!! WARNING - Usupported machine"
523 " architecture !\n");
528 #ifdef CONFIG_BOOTX_TEXT
532 /* New BootX enters kernel with MMU off, i/os are not allowed
533 * here. This hack will have been done by the boostrap anyway.
535 if (bi
->version
< 4) {
537 * XXX If this is an iMac, turn off the USB controller.
539 model
= (char *) bootx_early_getprop(r4
+ bi
->deviceTreeOffset
,
542 && (strcmp(model
, "iMac,1") == 0
543 || strcmp(model
, "PowerMac1,1") == 0)) {
544 bootx_printf("iMac,1 detected, shutting down USB\n");
545 out_le32((unsigned __iomem
*)0x80880008, 1); /* XXX */
549 /* Get a pointer that points above the device tree, args, ramdisk,
550 * etc... to use for generating the flattened tree
552 if (bi
->version
< 5) {
553 space
= bi
->deviceTreeOffset
+ bi
->deviceTreeSize
;
554 if (bi
->ramDisk
>= space
)
555 space
= bi
->ramDisk
+ bi
->ramDiskSize
;
557 space
= bi
->totalParamsSize
;
559 bootx_printf("Total space used by parameters & ramdisk: 0x%x\n", space
);
561 /* New BootX will have flushed all TLBs and enters kernel with
562 * MMU switched OFF, so this should not be useful anymore.
564 if (bi
->version
< 4) {
565 bootx_printf("Touching pages...\n");
568 * Touch each page to make sure the PTEs for them
569 * are in the hash table - the aim is to try to avoid
570 * getting DSI exceptions while copying the kernel image.
572 for (ptr
= ((unsigned long) &_stext
) & PAGE_MASK
;
573 ptr
< (unsigned long)bi
+ space
; ptr
+= PAGE_SIZE
)
574 x
= *(volatile unsigned long *)ptr
;
577 /* Ok, now we need to generate a flattened device-tree to pass
580 bootx_printf("Preparing boot params...\n");
582 hdr
= bootx_flatten_dt(space
);
584 #ifdef CONFIG_BOOTX_TEXT
586 bootx_printf("Preparing BAT...\n");
595 __start(hdr
, KERNELBASE
+ offset
, 0);