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/version.h>
16 #include <asm/sections.h>
19 #include <asm/bootx.h>
20 #include <asm/bootinfo.h>
21 #include <asm/btext.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);
88 #else /* CONFIG_BOOTX_TEXT */
89 static void __init
bootx_printf(const char *format
, ...) {}
90 #endif /* CONFIG_BOOTX_TEXT */
92 static void * __init
bootx_early_getprop(unsigned long base
,
96 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
97 u32
*ppp
= &np
->properties
;
100 struct bootx_dt_prop
*pp
=
101 (struct bootx_dt_prop
*)(base
+ *ppp
);
103 if (strcmp((char *)((unsigned long)pp
->name
+ base
),
105 return (void *)((unsigned long)pp
->value
+ base
);
112 #define dt_push_token(token, mem) \
114 *(mem) = _ALIGN_UP(*(mem),4); \
115 *((u32 *)*(mem)) = token; \
119 static unsigned long __init
bootx_dt_find_string(char *str
)
123 s
= os
= (char *)bootx_dt_strbase
;
125 while (s
< (char *)bootx_dt_strend
) {
126 if (strcmp(s
, str
) == 0)
133 static void __init
bootx_dt_add_prop(char *name
, void *data
, int size
,
134 unsigned long *mem_end
)
136 unsigned long soff
= bootx_dt_find_string(name
);
140 bootx_printf("WARNING: Can't find string index for <%s>\n",
144 if (size
> 0x20000) {
145 bootx_printf("WARNING: ignoring large property ");
146 bootx_printf("%s length 0x%x\n", name
, size
);
149 dt_push_token(OF_DT_PROP
, mem_end
);
150 dt_push_token(size
, mem_end
);
151 dt_push_token(soff
, mem_end
);
153 /* push property content */
155 memcpy((void *)*mem_end
, data
, size
);
156 *mem_end
= _ALIGN_UP(*mem_end
+ size
, 4);
160 static void __init
bootx_add_chosen_props(unsigned long base
,
161 unsigned long *mem_end
)
165 bootx_dt_add_prop("linux,bootx", NULL
, 0, mem_end
);
167 if (bootx_info
->kernelParamsOffset
) {
168 char *args
= (char *)((unsigned long)bootx_info
) +
169 bootx_info
->kernelParamsOffset
;
170 bootx_dt_add_prop("bootargs", args
, strlen(args
) + 1, mem_end
);
172 if (bootx_info
->ramDisk
) {
173 val
= ((unsigned long)bootx_info
) + bootx_info
->ramDisk
;
174 bootx_dt_add_prop("linux,initrd-start", &val
, 4, mem_end
);
175 val
+= bootx_info
->ramDiskSize
;
176 bootx_dt_add_prop("linux,initrd-end", &val
, 4, mem_end
);
178 if (strlen(bootx_disp_path
))
179 bootx_dt_add_prop("linux,stdout-path", bootx_disp_path
,
180 strlen(bootx_disp_path
) + 1, mem_end
);
183 static void __init
bootx_add_display_props(unsigned long base
,
184 unsigned long *mem_end
)
186 boot_infos_t
*bi
= bootx_info
;
189 bootx_dt_add_prop("linux,boot-display", NULL
, 0, mem_end
);
190 bootx_dt_add_prop("linux,opened", NULL
, 0, mem_end
);
191 tmp
= bi
->dispDeviceDepth
;
192 bootx_dt_add_prop("linux,bootx-depth", &tmp
, 4, mem_end
);
193 tmp
= bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0];
194 bootx_dt_add_prop("linux,bootx-width", &tmp
, 4, mem_end
);
195 tmp
= bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1];
196 bootx_dt_add_prop("linux,bootx-height", &tmp
, 4, mem_end
);
197 tmp
= bi
->dispDeviceRowBytes
;
198 bootx_dt_add_prop("linux,bootx-linebytes", &tmp
, 4, mem_end
);
199 tmp
= (u32
)bi
->dispDeviceBase
;
201 tmp
= (u32
)bi
->logicalDisplayBase
;
202 tmp
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
203 tmp
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
204 bootx_dt_add_prop("linux,bootx-addr", &tmp
, 4, mem_end
);
207 static void __init
bootx_dt_add_string(char *s
, unsigned long *mem_end
)
209 unsigned int l
= strlen(s
) + 1;
210 memcpy((void *)*mem_end
, s
, l
);
211 bootx_dt_strend
= *mem_end
= *mem_end
+ l
;
214 static void __init
bootx_scan_dt_build_strings(unsigned long base
,
216 unsigned long *mem_end
)
218 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
219 u32
*cpp
, *ppp
= &np
->properties
;
223 /* Keep refs to known nodes */
224 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
226 bootx_printf("Node without a full name !\n");
229 DBG("* strings: %s\n", namep
);
231 if (!strcmp(namep
, "/chosen")) {
232 DBG(" detected /chosen ! adding properties names !\n");
233 bootx_dt_add_string("linux,bootx", mem_end
);
234 bootx_dt_add_string("linux,stdout-path", mem_end
);
235 bootx_dt_add_string("linux,initrd-start", mem_end
);
236 bootx_dt_add_string("linux,initrd-end", mem_end
);
237 bootx_dt_add_string("bootargs", mem_end
);
238 bootx_node_chosen
= node
;
240 if (node
== bootx_info
->dispDeviceRegEntryOffset
) {
241 DBG(" detected display ! adding properties names !\n");
242 bootx_dt_add_string("linux,boot-display", mem_end
);
243 bootx_dt_add_string("linux,opened", mem_end
);
244 bootx_dt_add_string("linux,bootx-depth", mem_end
);
245 bootx_dt_add_string("linux,bootx-width", mem_end
);
246 bootx_dt_add_string("linux,bootx-height", mem_end
);
247 bootx_dt_add_string("linux,bootx-linebytes", mem_end
);
248 bootx_dt_add_string("linux,bootx-addr", mem_end
);
249 strncpy(bootx_disp_path
, namep
, 255);
252 /* get and store all property names */
254 struct bootx_dt_prop
*pp
=
255 (struct bootx_dt_prop
*)(base
+ *ppp
);
257 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
258 if (namep
== NULL
|| strcmp(namep
, "name") == 0)
260 /* get/create string entry */
261 soff
= bootx_dt_find_string(namep
);
263 bootx_dt_add_string(namep
, mem_end
);
268 /* do all our children */
271 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
272 bootx_scan_dt_build_strings(base
, *cpp
, mem_end
);
277 static void __init
bootx_scan_dt_build_struct(unsigned long base
,
279 unsigned long *mem_end
)
281 struct bootx_dt_node
*np
= (struct bootx_dt_node
*)(base
+ node
);
282 u32
*cpp
, *ppp
= &np
->properties
;
283 char *namep
, *p
, *ep
, *lp
;
286 dt_push_token(OF_DT_BEGIN_NODE
, mem_end
);
288 /* get the node's full name */
289 namep
= np
->full_name
? (char *)(base
+ np
->full_name
) : NULL
;
294 DBG("* struct: %s\n", namep
);
296 /* Fixup an Apple bug where they have bogus \0 chars in the
297 * middle of the path in some properties, and extract
298 * the unit name (everything after the last '/').
300 memcpy((void *)*mem_end
, namep
, l
+ 1);
301 namep
= (char *)*mem_end
;
302 for (lp
= p
= namep
, ep
= namep
+ l
; p
< ep
; p
++) {
309 *mem_end
= _ALIGN_UP((unsigned long)lp
+ 1, 4);
311 /* get and store all properties */
313 struct bootx_dt_prop
*pp
=
314 (struct bootx_dt_prop
*)(base
+ *ppp
);
316 namep
= pp
->name
? (char *)(base
+ pp
->name
) : NULL
;
318 if (namep
== NULL
|| !strcmp(namep
, "name"))
320 /* Skip "bootargs" in /chosen too as we replace it */
321 if (node
== bootx_node_chosen
&& !strcmp(namep
, "bootargs"))
324 /* push property head */
325 bootx_dt_add_prop(namep
,
326 pp
->value
? (void *)(base
+ pp
->value
): NULL
,
327 pp
->length
, mem_end
);
332 if (node
== bootx_node_chosen
)
333 bootx_add_chosen_props(base
, mem_end
);
334 if (node
== bootx_info
->dispDeviceRegEntryOffset
)
335 bootx_add_display_props(base
, mem_end
);
337 /* do all our children */
340 np
= (struct bootx_dt_node
*)(base
+ *cpp
);
341 bootx_scan_dt_build_struct(base
, *cpp
, mem_end
);
345 dt_push_token(OF_DT_END_NODE
, mem_end
);
348 static unsigned long __init
bootx_flatten_dt(unsigned long start
)
350 boot_infos_t
*bi
= bootx_info
;
351 unsigned long mem_start
, mem_end
;
352 struct boot_param_header
*hdr
;
356 /* Start using memory after the big blob passed by BootX, get
357 * some space for the header
359 mem_start
= mem_end
= _ALIGN_UP(((unsigned long)bi
) + start
, 4);
360 DBG("Boot params header at: %x\n", mem_start
);
361 hdr
= (struct boot_param_header
*)mem_start
;
362 mem_end
+= sizeof(struct boot_param_header
);
363 rsvmap
= (u64
*)(_ALIGN_UP(mem_end
, 8));
364 hdr
->off_mem_rsvmap
= ((unsigned long)rsvmap
) - mem_start
;
365 mem_end
= ((unsigned long)rsvmap
) + 8 * sizeof(u64
);
367 /* Get base of tree */
368 base
= ((unsigned long)bi
) + bi
->deviceTreeOffset
;
370 /* Build string array */
371 DBG("Building string array at: %x\n", mem_end
);
372 DBG("Device Tree Base=%x\n", base
);
373 bootx_dt_strbase
= mem_end
;
375 bootx_dt_strend
= mem_end
;
376 bootx_scan_dt_build_strings(base
, 4, &mem_end
);
377 hdr
->off_dt_strings
= bootx_dt_strbase
- mem_start
;
378 hdr
->dt_strings_size
= bootx_dt_strend
- bootx_dt_strbase
;
380 /* Build structure */
381 mem_end
= _ALIGN(mem_end
, 16);
382 DBG("Building device tree structure at: %x\n", mem_end
);
383 hdr
->off_dt_struct
= mem_end
- mem_start
;
384 bootx_scan_dt_build_struct(base
, 4, &mem_end
);
385 dt_push_token(OF_DT_END
, &mem_end
);
388 hdr
->boot_cpuid_phys
= 0;
389 hdr
->magic
= OF_DT_HEADER
;
390 hdr
->totalsize
= mem_end
- mem_start
;
391 hdr
->version
= OF_DT_VERSION
;
392 /* Version 16 is not backward compatible */
393 hdr
->last_comp_version
= 0x10;
395 /* Reserve the whole thing and copy the reserve map in, we
396 * also bump mem_reserve_cnt to cause further reservations to
397 * fail since it's too late.
399 mem_end
= _ALIGN(mem_end
, PAGE_SIZE
);
400 DBG("End of boot params: %x\n", mem_end
);
401 rsvmap
[0] = mem_start
;
406 return (unsigned long)hdr
;
410 #ifdef CONFIG_BOOTX_TEXT
411 static void __init
btext_welcome(boot_infos_t
*bi
)
416 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE
"\n");
417 bootx_printf("\nlinked at : 0x%x", KERNELBASE
);
418 bootx_printf("\nframe buffer at : 0x%x", bi
->dispDeviceBase
);
419 bootx_printf(" (phys), 0x%x", bi
->logicalDisplayBase
);
420 bootx_printf(" (log)");
421 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit
);
422 bootx_printf("\nboot_info at : 0x%x", bi
);
423 __asm__
__volatile__ ("mfmsr %0" : "=r" (flags
));
424 bootx_printf("\nMSR : 0x%x", flags
);
425 __asm__
__volatile__ ("mfspr %0, 287" : "=r" (pvr
));
426 bootx_printf("\nPVR : 0x%x", pvr
);
429 __asm__
__volatile__ ("mfspr %0, 1008" : "=r" (flags
));
430 bootx_printf("\nHID0 : 0x%x", flags
);
432 if (pvr
== 8 || pvr
== 12 || pvr
== 0x800c) {
433 __asm__
__volatile__ ("mfspr %0, 1019" : "=r" (flags
));
434 bootx_printf("\nICTC : 0x%x", flags
);
437 bootx_printf("\n\n");
438 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
439 bi
->deviceTreeOffset
);
440 bootx_printf("bi->deviceTreeSize : 0x%x\n",
443 bootx_printf("\n\n");
445 #endif /* CONFIG_BOOTX_TEXT */
447 void __init
bootx_init(unsigned long r3
, unsigned long r4
)
449 boot_infos_t
*bi
= (boot_infos_t
*) r4
;
452 unsigned long ptr
, x
;
454 unsigned long offset
= reloc_offset();
460 /* We haven't cleared any bss at this point, make sure
461 * what we need is initialized
463 bootx_dt_strbase
= bootx_dt_strend
= 0;
464 bootx_node_chosen
= 0;
465 bootx_disp_path
[0] = 0;
467 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi
))
468 bi
->logicalDisplayBase
= bi
->dispDeviceBase
;
470 /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
471 if (bi
->dispDeviceDepth
== 16)
472 bi
->dispDeviceDepth
= 15;
474 #ifdef CONFIG_BOOTX_TEXT
475 ptr
= (unsigned long)bi
->logicalDisplayBase
;
476 ptr
+= bi
->dispDeviceRect
[1] * bi
->dispDeviceRowBytes
;
477 ptr
+= bi
->dispDeviceRect
[0] * ((bi
->dispDeviceDepth
+ 7) / 8);
478 btext_setup_display(bi
->dispDeviceRect
[2] - bi
->dispDeviceRect
[0],
479 bi
->dispDeviceRect
[3] - bi
->dispDeviceRect
[1],
480 bi
->dispDeviceDepth
, bi
->dispDeviceRowBytes
,
481 (unsigned long)bi
->logicalDisplayBase
);
484 #endif /* CONFIG_BOOTX_TEXT */
487 * Test if boot-info is compatible. Done only in config
488 * CONFIG_BOOTX_TEXT since there is nothing much we can do
489 * with an incompatible version, except display a message
490 * and eventually hang the processor...
492 * I'll try to keep enough of boot-info compatible in the
493 * future to always allow display of this message;
495 if (!BOOT_INFO_IS_COMPATIBLE(bi
)) {
496 bootx_printf(" !!! WARNING - Incompatible version"
497 " of BootX !!!\n\n\n");
501 if (bi
->architecture
!= BOOT_ARCH_PCI
) {
502 bootx_printf(" !!! WARNING - Usupported machine"
503 " architecture !\n");
508 #ifdef CONFIG_BOOTX_TEXT
511 /* New BootX enters kernel with MMU off, i/os are not allowed
512 * here. This hack will have been done by the boostrap anyway.
514 if (bi
->version
< 4) {
516 * XXX If this is an iMac, turn off the USB controller.
518 model
= (char *) bootx_early_getprop(r4
+ bi
->deviceTreeOffset
,
521 && (strcmp(model
, "iMac,1") == 0
522 || strcmp(model
, "PowerMac1,1") == 0)) {
523 bootx_printf("iMac,1 detected, shutting down USB \n");
524 out_le32((unsigned __iomem
*)0x80880008, 1); /* XXX */
528 /* Get a pointer that points above the device tree, args, ramdisk,
529 * etc... to use for generating the flattened tree
531 if (bi
->version
< 5) {
532 space
= bi
->deviceTreeOffset
+ bi
->deviceTreeSize
;
534 space
= bi
->ramDisk
+ bi
->ramDiskSize
;
536 space
= bi
->totalParamsSize
;
538 bootx_printf("Total space used by parameters & ramdisk: %x \n", space
);
540 /* New BootX will have flushed all TLBs and enters kernel with
541 * MMU switched OFF, so this should not be useful anymore.
543 if (bi
->version
< 4) {
544 bootx_printf("Touching pages...\n");
547 * Touch each page to make sure the PTEs for them
548 * are in the hash table - the aim is to try to avoid
549 * getting DSI exceptions while copying the kernel image.
551 for (ptr
= ((unsigned long) &_stext
) & PAGE_MASK
;
552 ptr
< (unsigned long)bi
+ space
; ptr
+= PAGE_SIZE
)
553 x
= *(volatile unsigned long *)ptr
;
556 /* Ok, now we need to generate a flattened device-tree to pass
559 bootx_printf("Preparing boot params...\n");
561 hdr
= bootx_flatten_dt(space
);
563 #ifdef CONFIG_BOOTX_TEXT
565 bootx_printf("Preparing BAT...\n");
574 __start(hdr
, KERNELBASE
+ offset
, 0);