1 /*
2  *  Early boot support code for BootX bootloader
3  *
4  *  Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
5  *
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.
10  */
11 
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>
18 #include <asm/prom.h>
19 #include <asm/page.h>
20 #include <asm/bootx.h>
21 #include <asm/bootinfo.h>
22 #include <asm/btext.h>
23 #include <asm/io.h>
24 
25 #undef DEBUG
26 #define SET_BOOT_BAT
27 
28 #ifdef DEBUG
29 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
30 #else
31 #define DBG(fmt...) do { } while(0)
32 #endif
33 
34 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
35 
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];
41 
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)
47 
48 #ifdef CONFIG_BOOTX_TEXT
49 static void __init bootx_printf(const char *format, ...)
50 {
51 	const char *p, *q, *s;
52 	va_list args;
53 	unsigned long v;
54 
55 	va_start(args, format);
56 	for (p = format; *p != 0; p = q) {
57 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
58 			;
59 		if (q > p)
60 			btext_drawtext(p, q - p);
61 		if (*q == 0)
62 			break;
63 		if (*q == '\n') {
64 			++q;
65 			btext_flushline();
66 			btext_drawstring("\r\n");
67 			btext_flushline();
68 			continue;
69 		}
70 		++q;
71 		if (*q == 0)
72 			break;
73 		switch (*q) {
74 		case 's':
75 			++q;
76 			s = va_arg(args, const char *);
77 			if (s == NULL)
78 				s = "<NULL>";
79 			btext_drawstring(s);
80 			break;
81 		case 'x':
82 			++q;
83 			v = va_arg(args, unsigned long);
84 			btext_drawhex(v);
85 			break;
86 		}
87 	}
88 }
89 #else /* CONFIG_BOOTX_TEXT */
90 static void __init bootx_printf(const char *format, ...) {}
91 #endif /* CONFIG_BOOTX_TEXT */
92 
93 static void * __init bootx_early_getprop(unsigned long base,
94 					 unsigned long node,
95 					 char *prop)
96 {
97 	struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
98 	u32 *ppp = &np->properties;
99 
100 	while(*ppp) {
101 		struct bootx_dt_prop *pp =
102 			(struct bootx_dt_prop *)(base + *ppp);
103 
104 		if (strcmp((char *)((unsigned long)pp->name + base),
105 			   prop) == 0) {
106 			return (void *)((unsigned long)pp->value + base);
107 		}
108 		ppp = &pp->next;
109 	}
110 	return NULL;
111 }
112 
113 #define dt_push_token(token, mem) \
114 	do { \
115 		*(mem) = _ALIGN_UP(*(mem),4); \
116 		*((u32 *)*(mem)) = token; \
117 		*(mem) += 4; \
118 	} while(0)
119 
120 static unsigned long __init bootx_dt_find_string(char *str)
121 {
122 	char *s, *os;
123 
124 	s = os = (char *)bootx_dt_strbase;
125 	s += 4;
126 	while (s <  (char *)bootx_dt_strend) {
127 		if (strcmp(s, str) == 0)
128 			return s - os;
129 		s += strlen(s) + 1;
130 	}
131 	return 0;
132 }
133 
134 static void __init bootx_dt_add_prop(char *name, void *data, int size,
135 				  unsigned long *mem_end)
136 {
137 	unsigned long soff = bootx_dt_find_string(name);
138 	if (data == NULL)
139 		size = 0;
140 	if (soff == 0) {
141 		bootx_printf("WARNING: Can't find string index for <%s>\n",
142 			     name);
143 		return;
144 	}
145 	if (size > 0x20000) {
146 		bootx_printf("WARNING: ignoring large property ");
147 		bootx_printf("%s length 0x%x\n", name, size);
148 		return;
149 	}
150 	dt_push_token(OF_DT_PROP, mem_end);
151 	dt_push_token(size, mem_end);
152 	dt_push_token(soff, mem_end);
153 
154 	/* push property content */
155 	if (size && data) {
156 		memcpy((void *)*mem_end, data, size);
157 		*mem_end = _ALIGN_UP(*mem_end + size, 4);
158 	}
159 }
160 
161 static void __init bootx_add_chosen_props(unsigned long base,
162 					  unsigned long *mem_end)
163 {
164 	u32 val = _MACH_Pmac;
165 
166 	bootx_dt_add_prop("linux,platform", &val, 4, mem_end);
167 
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);
172 	}
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);
178 	}
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);
182 }
183 
184 static void __init bootx_add_display_props(unsigned long base,
185 					   unsigned long *mem_end)
186 {
187 	bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
188 	bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
189 }
190 
191 static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
192 {
193 	unsigned int l = strlen(s) + 1;
194 	memcpy((void *)*mem_end, s, l);
195 	bootx_dt_strend = *mem_end = *mem_end + l;
196 }
197 
198 static void __init bootx_scan_dt_build_strings(unsigned long base,
199 					       unsigned long node,
200 					       unsigned long *mem_end)
201 {
202 	struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
203 	u32 *cpp, *ppp = &np->properties;
204 	unsigned long soff;
205 	char *namep;
206 
207 	/* Keep refs to known nodes */
208 	namep = np->full_name ? (char *)(base + np->full_name) : NULL;
209        	if (namep == NULL) {
210 		bootx_printf("Node without a full name !\n");
211 		namep = "";
212 	}
213 	DBG("* strings: %s\n", namep);
214 
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;
223 	}
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);
229 	}
230 
231 	/* get and store all property names */
232 	while (*ppp) {
233 		struct bootx_dt_prop *pp =
234 			(struct bootx_dt_prop *)(base + *ppp);
235 
236 		namep = pp->name ? (char *)(base + pp->name) : NULL;
237  		if (namep == NULL || strcmp(namep, "name") == 0)
238  			goto next;
239 		/* get/create string entry */
240 		soff = bootx_dt_find_string(namep);
241 		if (soff == 0)
242 			bootx_dt_add_string(namep, mem_end);
243 	next:
244 		ppp = &pp->next;
245 	}
246 
247 	/* do all our children */
248 	cpp = &np->child;
249 	while(*cpp) {
250 		np = (struct bootx_dt_node *)(base + *cpp);
251 		bootx_scan_dt_build_strings(base, *cpp, mem_end);
252 		cpp = &np->sibling;
253 	}
254 }
255 
256 static void __init bootx_scan_dt_build_struct(unsigned long base,
257 					      unsigned long node,
258 					      unsigned long *mem_end)
259 {
260 	struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
261 	u32 *cpp, *ppp = &np->properties;
262 	char *namep, *p, *ep, *lp;
263 	int l;
264 
265 	dt_push_token(OF_DT_BEGIN_NODE, mem_end);
266 
267 	/* get the node's full name */
268 	namep = np->full_name ? (char *)(base + np->full_name) : NULL;
269 	if (namep == NULL)
270 		namep = "";
271 	l = strlen(namep);
272 
273 	DBG("* struct: %s\n", namep);
274 
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 '/').
278 	 */
279 	memcpy((void *)*mem_end, namep, l + 1);
280 	namep = (char *)*mem_end;
281 	for (lp = p = namep, ep = namep + l; p < ep; p++) {
282 		if (*p == '/')
283 			lp = namep;
284 		else if (*p != 0)
285 			*lp++ = *p;
286 	}
287 	*lp = 0;
288 	*mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
289 
290 	/* get and store all properties */
291 	while (*ppp) {
292 		struct bootx_dt_prop *pp =
293 			(struct bootx_dt_prop *)(base + *ppp);
294 
295 		namep = pp->name ? (char *)(base + pp->name) : NULL;
296 		/* Skip "name" */
297  		if (namep == NULL || !strcmp(namep, "name"))
298  			goto next;
299 		/* Skip "bootargs" in /chosen too as we replace it */
300 		if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
301 			goto next;
302 
303 		/* push property head */
304 		bootx_dt_add_prop(namep,
305 				  pp->value ? (void *)(base + pp->value): NULL,
306 				  pp->length, mem_end);
307 	next:
308 		ppp = &pp->next;
309 	}
310 
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);
315 
316 	/* do all our children */
317 	cpp = &np->child;
318 	while(*cpp) {
319 		np = (struct bootx_dt_node *)(base + *cpp);
320 		bootx_scan_dt_build_struct(base, *cpp, mem_end);
321 		cpp = &np->sibling;
322 	}
323 
324 	dt_push_token(OF_DT_END_NODE, mem_end);
325 }
326 
327 static unsigned long __init bootx_flatten_dt(unsigned long start)
328 {
329 	boot_infos_t *bi = bootx_info;
330 	unsigned long mem_start, mem_end;
331 	struct boot_param_header *hdr;
332 	unsigned long base;
333 	u64 *rsvmap;
334 
335 	/* Start using memory after the big blob passed by BootX, get
336 	 * some space for the header
337 	 */
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);
345 
346 	/* Get base of tree */
347 	base = ((unsigned long)bi) + bi->deviceTreeOffset;
348 
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;
353 	mem_end += 4;
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;
358 
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);
365 
366 	/* Finish header */
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;
373 
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.
377 	 */
378 	mem_end = _ALIGN(mem_end, PAGE_SIZE);
379 	DBG("End of boot params: %x\n", mem_end);
380 	rsvmap[0] = mem_start;
381 	rsvmap[1] = mem_end;
382 	rsvmap[2] = 0;
383 	rsvmap[3] = 0;
384 
385 	return (unsigned long)hdr;
386 }
387 
388 
389 #ifdef CONFIG_BOOTX_TEXT
390 static void __init btext_welcome(boot_infos_t *bi)
391 {
392 	unsigned long flags;
393 	unsigned long pvr;
394 
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);
406 	pvr >>= 16;
407 	if (pvr > 1) {
408 	    __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
409 	    bootx_printf("\nHID0             : 0x%x", flags);
410 	}
411 	if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
412 	    __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
413 	    bootx_printf("\nICTC             : 0x%x", flags);
414 	}
415 #ifdef DEBUG
416 	bootx_printf("\n\n");
417 	bootx_printf("bi->deviceTreeOffset   : 0x%x\n",
418 		     bi->deviceTreeOffset);
419 	bootx_printf("bi->deviceTreeSize     : 0x%x\n",
420 		     bi->deviceTreeSize);
421 #endif
422 	bootx_printf("\n\n");
423 }
424 #endif /* CONFIG_BOOTX_TEXT */
425 
426 void __init bootx_init(unsigned long r3, unsigned long r4)
427 {
428 	boot_infos_t *bi = (boot_infos_t *) r4;
429 	unsigned long hdr;
430 	unsigned long space;
431 	unsigned long ptr, x;
432 	char *model;
433 	unsigned long offset = reloc_offset();
434 
435 	reloc_got2(offset);
436 
437 	bootx_info = bi;
438 
439 	/* We haven't cleared any bss at this point, make sure
440 	 * what we need is initialized
441 	 */
442 	bootx_dt_strbase = bootx_dt_strend = 0;
443 	bootx_node_chosen = 0;
444 	bootx_disp_path[0] = 0;
445 
446 	if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
447 		bi->logicalDisplayBase = bi->dispDeviceBase;
448 
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);
454 	btext_clearscreen();
455 	btext_flushscreen();
456 #endif /* CONFIG_BOOTX_TEXT */
457 
458 	/*
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...
463 	 *
464 	 * I'll try to keep enough of boot-info compatible in the
465 	 * future to always allow display of this message;
466 	 */
467 	if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
468 		bootx_printf(" !!! WARNING - Incompatible version"
469 			     " of BootX !!!\n\n\n");
470 		for (;;)
471 			;
472 	}
473 	if (bi->architecture != BOOT_ARCH_PCI) {
474 		bootx_printf(" !!! WARNING - Usupported machine"
475 			     " architecture !\n");
476 		for (;;)
477 			;
478 	}
479 
480 #ifdef CONFIG_BOOTX_TEXT
481 	btext_welcome(bi);
482 #endif
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.
485 	 */
486 	if (bi->version < 4) {
487 		/*
488 		 * XXX If this is an iMac, turn off the USB controller.
489 		 */
490 		model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
491 						     4, "model");
492 		if (model
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 */
497 		}
498 	}
499 
500 	/* Get a pointer that points above the device tree, args, ramdisk,
501 	 * etc... to use for generating the flattened tree
502 	 */
503 	if (bi->version < 5) {
504 		space = bi->deviceTreeOffset + bi->deviceTreeSize;
505 		if (bi->ramDisk)
506 			space = bi->ramDisk + bi->ramDiskSize;
507 	} else
508 		space = bi->totalParamsSize;
509 
510 	bootx_printf("Total space used by parameters & ramdisk: %x \n", space);
511 
512 	/* New BootX will have flushed all TLBs and enters kernel with
513 	 * MMU switched OFF, so this should not be useful anymore.
514 	 */
515 	if (bi->version < 4) {
516 		bootx_printf("Touching pages...\n");
517 
518 		/*
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.
522 		 */
523 		for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
524 		     ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
525 			x = *(volatile unsigned long *)ptr;
526 	}
527 
528 	/* Ok, now we need to generate a flattened device-tree to pass
529 	 * to the kernel
530 	 */
531 	bootx_printf("Preparing boot params...\n");
532 
533 	hdr = bootx_flatten_dt(space);
534 
535 #ifdef CONFIG_BOOTX_TEXT
536 #ifdef SET_BOOT_BAT
537 	bootx_printf("Preparing BAT...\n");
538 	btext_prepare_BAT();
539 #else
540 	btext_unmap();
541 #endif
542 #endif
543 
544 	reloc_got2(-offset);
545 
546 	__start(hdr, KERNELBASE + offset, 0);
547 }
548