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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/version.h>
17 #include <asm/sections.h>
20 #include <asm/bootx.h>
21 #include <asm/bootinfo.h>
22 #include <asm/btext.h>
29 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
31 #define DBG(fmt...) do { } while(0)
34 extern void __start(unsigned long r3
, unsigned long r4
, unsigned long r5
);
36 static unsigned long __initdata bootx_dt_strbase
;
37 static unsigned long __initdata bootx_dt_strend
;
38 static unsigned long __initdata bootx_node_chosen
;
39 static boot_infos_t
* __initdata bootx_info
;
40 static char __initdata bootx_disp_path
[256];
42 /* Is boot-info compatible ? */
43 #define BOOT_INFO_IS_COMPATIBLE(bi) \
44 ((bi)->compatible_version <= BOOT_INFO_VERSION)
45 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
46 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
48 #ifdef CONFIG_BOOTX_TEXT
49 static void __init
bootx_printf(const char *format
, ...)
51 const char *p
, *q
, *s
;
55 va_start(args
, format
);
56 for (p
= format
; *p
!= 0; p
= q
) {
57 for (q
= p
; *q
!= 0 && *q
!= '\n' && *q
!= '%'; ++q
)
60 btext_drawtext(p
, q
- p
);
66 btext_drawstring("\r\n");
76 s
= va_arg(args
, const char *);
83 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
)
164 u32 val
= _MACH_Pmac
;
166 bootx_dt_add_prop("linux,platform", &val
, 4, 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
)
187 bootx_dt_add_prop("linux,boot-display", NULL
, 0, mem_end
);
188 bootx_dt_add_prop("linux,opened", NULL
, 0, mem_end
);
191 static void __init
bootx_dt_add_string(char *s
, unsigned long *mem_end
)
193 unsigned int l
= strlen(s
) + 1;
194 memcpy((void *)*mem_end
, s
, l
);
195 bootx_dt_strend
= *mem_end
= *mem_end
+ l
;
198 static void __init
bootx_scan_dt_build_strings(unsigned long base
,
200 unsigned long *mem_end
)
202 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
203 u32
*cpp
, *ppp
= &np
->properties
;
207 /* Keep refs to known nodes */
208 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
210 bootx_printf("Node without a full name !\n");
213 DBG("* strings: %s\n", namep
);
215 if (!strcmp(namep
, "/chosen")) {
216 DBG(" detected /chosen ! adding properties names !\n");
217 bootx_dt_add_string("linux,platform", mem_end
);
218 bootx_dt_add_string("linux,stdout-path", mem_end
);
219 bootx_dt_add_string("linux,initrd-start", mem_end
);
220 bootx_dt_add_string("linux,initrd-end", mem_end
);
221 bootx_dt_add_string("bootargs", mem_end
);
222 bootx_node_chosen
= node
;
224 if (node
== bootx_info
->dispDeviceRegEntryOffset
) {
225 DBG(" detected display ! adding properties names !\n");
226 bootx_dt_add_string("linux,boot-display", mem_end
);
227 bootx_dt_add_string("linux,opened", mem_end
);
228 strncpy(bootx_disp_path
, namep
, 255);
231 /* get and store all property names */
233 struct bootx_dt_prop
*pp
=
234 (struct bootx_dt_prop
*)(base
+ *ppp
);
236 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
237 if (namep
== NULL
|| strcmp(namep
, "name") == 0)
239 /* get/create string entry */
240 soff
= bootx_dt_find_string(namep
);
242 bootx_dt_add_string(namep
, mem_end
);
247 /* do all our children */
250 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
251 bootx_scan_dt_build_strings(base
, *cpp
, mem_end
);
256 static void __init
bootx_scan_dt_build_struct(unsigned long base
,
258 unsigned long *mem_end
)
260 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
261 u32
*cpp
, *ppp
= &np
->properties
;
262 char *namep
, *p
, *ep
, *lp
;
265 dt_push_token(OF_DT_BEGIN_NODE
, mem_end
);
267 /* get the node's full name */
268 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
273 DBG("* struct: %s\n", namep
);
275 /* Fixup an Apple bug where they have bogus \0 chars in the
276 * middle of the path in some properties, and extract
277 * the unit name (everything after the last '/').
279 memcpy((void *)*mem_end
, namep
, l
+ 1);
280 namep
= (char *)*mem_end
;
281 for (lp
= p
= namep
, ep
= namep
+ l
; p
< ep
; p
++) {
288 *mem_end
= _ALIGN_UP((unsigned long)lp
+ 1, 4);
290 /* get and store all properties */
292 struct bootx_dt_prop
*pp
=
293 (struct bootx_dt_prop
*)(base
+ *ppp
);
295 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
297 if (namep
== NULL
|| !strcmp(namep
, "name"))
299 /* Skip "bootargs" in /chosen too as we replace it */
300 if (node
== bootx_node_chosen
&& !strcmp(namep
, "bootargs"))
303 /* push property head */
304 bootx_dt_add_prop(namep
,
305 pp
->value
? (void *)(base
+ pp
->value
): NULL
,
306 pp
->length
, mem_end
);
311 if (node
== bootx_node_chosen
)
312 bootx_add_chosen_props(base
, mem_end
);
313 if (node
== bootx_info
->dispDeviceRegEntryOffset
)
314 bootx_add_display_props(base
, mem_end
);
316 /* do all our children */
319 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
320 bootx_scan_dt_build_struct(base
, *cpp
, mem_end
);
324 dt_push_token(OF_DT_END_NODE
, mem_end
);
327 static unsigned long __init
bootx_flatten_dt(unsigned long start
)
329 boot_infos_t
*bi
= bootx_info
;
330 unsigned long mem_start
, mem_end
;
331 struct boot_param_header
*hdr
;
335 /* Start using memory after the big blob passed by BootX, get
336 * some space for the header
338 mem_start
= mem_end
= _ALIGN_UP(((unsigned long)bi
) + start
, 4);
339 DBG("Boot params header at: %x\n", mem_start
);
340 hdr
= (struct boot_param_header
*)mem_start
;
341 mem_end
+= sizeof(struct boot_param_header
);
342 rsvmap
= (u64
*)(_ALIGN_UP(mem_end
, 8));
343 hdr
->off_mem_rsvmap
= ((unsigned long)rsvmap
) - mem_start
;
344 mem_end
= ((unsigned long)rsvmap
) + 8 * sizeof(u64
);
346 /* Get base of tree */
347 base
= ((unsigned long)bi
) + bi
->deviceTreeOffset
;
349 /* Build string array */
350 DBG("Building string array at: %x\n", mem_end
);
351 DBG("Device Tree Base=%x\n", base
);
352 bootx_dt_strbase
= mem_end
;
354 bootx_dt_strend
= mem_end
;
355 bootx_scan_dt_build_strings(base
, 4, &mem_end
);
356 hdr
->off_dt_strings
= bootx_dt_strbase
- mem_start
;
357 hdr
->dt_strings_size
= bootx_dt_strend
- bootx_dt_strbase
;
359 /* Build structure */
360 mem_end
= _ALIGN(mem_end
, 16);
361 DBG("Building device tree structure at: %x\n", mem_end
);
362 hdr
->off_dt_struct
= mem_end
- mem_start
;
363 bootx_scan_dt_build_struct(base
, 4, &mem_end
);
364 dt_push_token(OF_DT_END
, &mem_end
);
367 hdr
->boot_cpuid_phys
= 0;
368 hdr
->magic
= OF_DT_HEADER
;
369 hdr
->totalsize
= mem_end
- mem_start
;
370 hdr
->version
= OF_DT_VERSION
;
371 /* Version 16 is not backward compatible */
372 hdr
->last_comp_version
= 0x10;
374 /* Reserve the whole thing and copy the reserve map in, we
375 * also bump mem_reserve_cnt to cause further reservations to
376 * fail since it's too late.
378 mem_end
= _ALIGN(mem_end
, PAGE_SIZE
);
379 DBG("End of boot params: %x\n", mem_end
);
380 rsvmap
[0] = mem_start
;
385 return (unsigned long)hdr
;
389 #ifdef CONFIG_BOOTX_TEXT
390 static void __init
btext_welcome(boot_infos_t
*bi
)
395 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE
"\n");
396 bootx_printf("\nlinked at : 0x%x", KERNELBASE
);
397 bootx_printf("\nframe buffer at : 0x%x", bi
->dispDeviceBase
);
398 bootx_printf(" (phys), 0x%x", bi
->logicalDisplayBase
);
399 bootx_printf(" (log)");
400 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit
);
401 bootx_printf("\nboot_info at : 0x%x", bi
);
402 __asm__
__volatile__ ("mfmsr %0" : "=r" (flags
));
403 bootx_printf("\nMSR : 0x%x", flags
);
404 __asm__
__volatile__ ("mfspr %0, 287" : "=r" (pvr
));
405 bootx_printf("\nPVR : 0x%x", pvr
);
408 __asm__
__volatile__ ("mfspr %0, 1008" : "=r" (flags
));
409 bootx_printf("\nHID0 : 0x%x", flags
);
411 if (pvr
== 8 || pvr
== 12 || pvr
== 0x800c) {
412 __asm__
__volatile__ ("mfspr %0, 1019" : "=r" (flags
));
413 bootx_printf("\nICTC : 0x%x", flags
);
416 bootx_printf("\n\n");
417 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
418 bi
->deviceTreeOffset
);
419 bootx_printf("bi->deviceTreeSize : 0x%x\n",
422 bootx_printf("\n\n");
424 #endif /* CONFIG_BOOTX_TEXT */
426 void __init
bootx_init(unsigned long r3
, unsigned long r4
)
428 boot_infos_t
*bi
= (boot_infos_t
*) r4
;
431 unsigned long ptr
, x
;
433 unsigned long offset
= reloc_offset();
439 /* We haven't cleared any bss at this point, make sure
440 * what we need is initialized
442 bootx_dt_strbase
= bootx_dt_strend
= 0;
443 bootx_node_chosen
= 0;
444 bootx_disp_path
[0] = 0;
446 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi
))
447 bi
->logicalDisplayBase
= bi
->dispDeviceBase
;
449 #ifdef CONFIG_BOOTX_TEXT
450 btext_setup_display(bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0],
451 bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1],
452 bi
->dispDeviceDepth
, bi
->dispDeviceRowBytes
,
453 (unsigned long)bi
->logicalDisplayBase
);
456 #endif /* CONFIG_BOOTX_TEXT */
459 * Test if boot-info is compatible. Done only in config
460 * CONFIG_BOOTX_TEXT since there is nothing much we can do
461 * with an incompatible version, except display a message
462 * and eventually hang the processor...
464 * I'll try to keep enough of boot-info compatible in the
465 * future to always allow display of this message;
467 if (!BOOT_INFO_IS_COMPATIBLE(bi
)) {
468 bootx_printf(" !!! WARNING - Incompatible version"
469 " of BootX !!!\n\n\n");
473 if (bi
->architecture
!= BOOT_ARCH_PCI
) {
474 bootx_printf(" !!! WARNING - Usupported machine"
475 " architecture !\n");
480 #ifdef CONFIG_BOOTX_TEXT
483 /* New BootX enters kernel with MMU off, i/os are not allowed
484 * here. This hack will have been done by the boostrap anyway.
486 if (bi
->version
< 4) {
488 * XXX If this is an iMac, turn off the USB controller.
490 model
= (char *) bootx_early_getprop(r4
+ bi
->deviceTreeOffset
,
493 && (strcmp(model
, "iMac,1") == 0
494 || strcmp(model
, "PowerMac1,1") == 0)) {
495 bootx_printf("iMac,1 detected, shutting down USB \n");
496 out_le32((unsigned *)0x80880008, 1); /* XXX */
500 /* Get a pointer that points above the device tree, args, ramdisk,
501 * etc... to use for generating the flattened tree
503 if (bi
->version
< 5) {
504 space
= bi
->deviceTreeOffset
+ bi
->deviceTreeSize
;
506 space
= bi
->ramDisk
+ bi
->ramDiskSize
;
508 space
= bi
->totalParamsSize
;
510 bootx_printf("Total space used by parameters & ramdisk: %x \n", space
);
512 /* New BootX will have flushed all TLBs and enters kernel with
513 * MMU switched OFF, so this should not be useful anymore.
515 if (bi
->version
< 4) {
516 bootx_printf("Touching pages...\n");
519 * Touch each page to make sure the PTEs for them
520 * are in the hash table - the aim is to try to avoid
521 * getting DSI exceptions while copying the kernel image.
523 for (ptr
= ((unsigned long) &_stext
) & PAGE_MASK
;
524 ptr
< (unsigned long)bi
+ space
; ptr
+= PAGE_SIZE
)
525 x
= *(volatile unsigned long *)ptr
;
528 /* Ok, now we need to generate a flattened device-tree to pass
531 bootx_printf("Preparing boot params...\n");
533 hdr
= bootx_flatten_dt(space
);
535 #ifdef CONFIG_BOOTX_TEXT
537 bootx_printf("Preparing BAT...\n");
546 __start(hdr
, KERNELBASE
+ offset
, 0);