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)
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)
185 bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
186 bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
189 static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
191 unsigned int l = strlen(s) + 1;
192 memcpy((void *)*mem_end, s, l);
193 bootx_dt_strend = *mem_end = *mem_end + l;
196 static void __init bootx_scan_dt_build_strings(unsigned long base,
198 unsigned long *mem_end)
200 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
201 u32 *cpp, *ppp = &np->properties;
205 /* Keep refs to known nodes */
206 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
208 bootx_printf("Node without a full name !\n");
211 DBG("* strings: %s\n", namep);
213 if (!strcmp(namep, "/chosen")) {
214 DBG(" detected /chosen ! adding properties names !\n");
215 bootx_dt_add_string("linux,platform", mem_end);
216 bootx_dt_add_string("linux,stdout-path", mem_end);
217 bootx_dt_add_string("linux,initrd-start", mem_end);
218 bootx_dt_add_string("linux,initrd-end", mem_end);
219 bootx_dt_add_string("bootargs", mem_end);
220 bootx_node_chosen = node;
222 if (node == bootx_info->dispDeviceRegEntryOffset) {
223 DBG(" detected display ! adding properties names !\n");
224 bootx_dt_add_string("linux,boot-display", mem_end);
225 bootx_dt_add_string("linux,opened", mem_end);
226 strncpy(bootx_disp_path, namep, 255);
229 /* get and store all property names */
231 struct bootx_dt_prop *pp =
232 (struct bootx_dt_prop *)(base + *ppp);
234 namep = pp->name ? (char *)(base + pp->name) : NULL;
235 if (namep == NULL || strcmp(namep, "name") == 0)
237 /* get/create string entry */
238 soff = bootx_dt_find_string(namep);
240 bootx_dt_add_string(namep, mem_end);
245 /* do all our children */
248 np = (struct bootx_dt_node *)(base + *cpp);
249 bootx_scan_dt_build_strings(base, *cpp, mem_end);
254 static void __init bootx_scan_dt_build_struct(unsigned long base,
256 unsigned long *mem_end)
258 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
259 u32 *cpp, *ppp = &np->properties;
260 char *namep, *p, *ep, *lp;
263 dt_push_token(OF_DT_BEGIN_NODE, mem_end);
265 /* get the node's full name */
266 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
271 DBG("* struct: %s\n", namep);
273 /* Fixup an Apple bug where they have bogus \0 chars in the
274 * middle of the path in some properties, and extract
275 * the unit name (everything after the last '/').
277 memcpy((void *)*mem_end, namep, l + 1);
278 namep = (char *)*mem_end;
279 for (lp = p = namep, ep = namep + l; p < ep; p++) {
286 *mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
288 /* get and store all properties */
290 struct bootx_dt_prop *pp =
291 (struct bootx_dt_prop *)(base + *ppp);
293 namep = pp->name ? (char *)(base + pp->name) : NULL;
295 if (namep == NULL || !strcmp(namep, "name"))
297 /* Skip "bootargs" in /chosen too as we replace it */
298 if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
301 /* push property head */
302 bootx_dt_add_prop(namep,
303 pp->value ? (void *)(base + pp->value): NULL,
304 pp->length, mem_end);
309 if (node == bootx_node_chosen)
310 bootx_add_chosen_props(base, mem_end);
311 if (node == bootx_info->dispDeviceRegEntryOffset)
312 bootx_add_display_props(base, mem_end);
314 /* do all our children */
317 np = (struct bootx_dt_node *)(base + *cpp);
318 bootx_scan_dt_build_struct(base, *cpp, mem_end);
322 dt_push_token(OF_DT_END_NODE, mem_end);
325 static unsigned long __init bootx_flatten_dt(unsigned long start)
327 boot_infos_t *bi = bootx_info;
328 unsigned long mem_start, mem_end;
329 struct boot_param_header *hdr;
333 /* Start using memory after the big blob passed by BootX, get
334 * some space for the header
336 mem_start = mem_end = _ALIGN_UP(((unsigned long)bi) + start, 4);
337 DBG("Boot params header at: %x\n", mem_start);
338 hdr = (struct boot_param_header *)mem_start;
339 mem_end += sizeof(struct boot_param_header);
340 rsvmap = (u64 *)(_ALIGN_UP(mem_end, 8));
341 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
342 mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
344 /* Get base of tree */
345 base = ((unsigned long)bi) + bi->deviceTreeOffset;
347 /* Build string array */
348 DBG("Building string array at: %x\n", mem_end);
349 DBG("Device Tree Base=%x\n", base);
350 bootx_dt_strbase = mem_end;
352 bootx_dt_strend = mem_end;
353 bootx_scan_dt_build_strings(base, 4, &mem_end);
354 hdr->off_dt_strings = bootx_dt_strbase - mem_start;
355 hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
357 /* Build structure */
358 mem_end = _ALIGN(mem_end, 16);
359 DBG("Building device tree structure at: %x\n", mem_end);
360 hdr->off_dt_struct = mem_end - mem_start;
361 bootx_scan_dt_build_struct(base, 4, &mem_end);
362 dt_push_token(OF_DT_END, &mem_end);
365 hdr->boot_cpuid_phys = 0;
366 hdr->magic = OF_DT_HEADER;
367 hdr->totalsize = mem_end - mem_start;
368 hdr->version = OF_DT_VERSION;
369 /* Version 16 is not backward compatible */
370 hdr->last_comp_version = 0x10;
372 /* Reserve the whole thing and copy the reserve map in, we
373 * also bump mem_reserve_cnt to cause further reservations to
374 * fail since it's too late.
376 mem_end = _ALIGN(mem_end, PAGE_SIZE);
377 DBG("End of boot params: %x\n", mem_end);
378 rsvmap[0] = mem_start;
383 return (unsigned long)hdr;
387 #ifdef CONFIG_BOOTX_TEXT
388 static void __init btext_welcome(boot_infos_t *bi)
393 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
394 bootx_printf("\nlinked at : 0x%x", KERNELBASE);
395 bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);
396 bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
397 bootx_printf(" (log)");
398 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit);
399 bootx_printf("\nboot_info at : 0x%x", bi);
400 __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
401 bootx_printf("\nMSR : 0x%x", flags);
402 __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
403 bootx_printf("\nPVR : 0x%x", pvr);
406 __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
407 bootx_printf("\nHID0 : 0x%x", flags);
409 if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
410 __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
411 bootx_printf("\nICTC : 0x%x", flags);
414 bootx_printf("\n\n");
415 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
416 bi->deviceTreeOffset);
417 bootx_printf("bi->deviceTreeSize : 0x%x\n",
420 bootx_printf("\n\n");
422 #endif /* CONFIG_BOOTX_TEXT */
424 void __init bootx_init(unsigned long r3, unsigned long r4)
426 boot_infos_t *bi = (boot_infos_t *) r4;
429 unsigned long ptr, x;
431 unsigned long offset = reloc_offset();
437 /* We haven't cleared any bss at this point, make sure
438 * what we need is initialized
440 bootx_dt_strbase = bootx_dt_strend = 0;
441 bootx_node_chosen = 0;
442 bootx_disp_path[0] = 0;
444 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
445 bi->logicalDisplayBase = bi->dispDeviceBase;
447 #ifdef CONFIG_BOOTX_TEXT
448 btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
449 bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
450 bi->dispDeviceDepth, bi->dispDeviceRowBytes,
451 (unsigned long)bi->logicalDisplayBase);
454 #endif /* CONFIG_BOOTX_TEXT */
457 * Test if boot-info is compatible. Done only in config
458 * CONFIG_BOOTX_TEXT since there is nothing much we can do
459 * with an incompatible version, except display a message
460 * and eventually hang the processor...
462 * I'll try to keep enough of boot-info compatible in the
463 * future to always allow display of this message;
465 if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
466 bootx_printf(" !!! WARNING - Incompatible version"
467 " of BootX !!!\n\n\n");
471 if (bi->architecture != BOOT_ARCH_PCI) {
472 bootx_printf(" !!! WARNING - Usupported machine"
473 " architecture !\n");
478 #ifdef CONFIG_BOOTX_TEXT
481 /* New BootX enters kernel with MMU off, i/os are not allowed
482 * here. This hack will have been done by the boostrap anyway.
484 if (bi->version < 4) {
486 * XXX If this is an iMac, turn off the USB controller.
488 model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
491 && (strcmp(model, "iMac,1") == 0
492 || strcmp(model, "PowerMac1,1") == 0)) {
493 bootx_printf("iMac,1 detected, shutting down USB \n");
494 out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */
498 /* Get a pointer that points above the device tree, args, ramdisk,
499 * etc... to use for generating the flattened tree
501 if (bi->version < 5) {
502 space = bi->deviceTreeOffset + bi->deviceTreeSize;
504 space = bi->ramDisk + bi->ramDiskSize;
506 space = bi->totalParamsSize;
508 bootx_printf("Total space used by parameters & ramdisk: %x \n", space);
510 /* New BootX will have flushed all TLBs and enters kernel with
511 * MMU switched OFF, so this should not be useful anymore.
513 if (bi->version < 4) {
514 bootx_printf("Touching pages...\n");
517 * Touch each page to make sure the PTEs for them
518 * are in the hash table - the aim is to try to avoid
519 * getting DSI exceptions while copying the kernel image.
521 for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
522 ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
523 x = *(volatile unsigned long *)ptr;
526 /* Ok, now we need to generate a flattened device-tree to pass
529 bootx_printf("Preparing boot params...\n");
531 hdr = bootx_flatten_dt(space);
533 #ifdef CONFIG_BOOTX_TEXT
535 bootx_printf("Preparing BAT...\n");
544 __start(hdr, KERNELBASE + offset, 0);