bootx_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. #include <linux/config.h>
  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>
  17. #include <asm/prom.h>
  18. #include <asm/page.h>
  19. #include <asm/bootx.h>
  20. #include <asm/bootinfo.h>
  21. #include <asm/btext.h>
  22. #include <asm/io.h>
  23. #undef DEBUG
  24. #define SET_BOOT_BAT
  25. #ifdef DEBUG
  26. #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
  27. #else
  28. #define DBG(fmt...) do { } while(0)
  29. #endif
  30. extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
  31. static unsigned long __initdata bootx_dt_strbase;
  32. static unsigned long __initdata bootx_dt_strend;
  33. static unsigned long __initdata bootx_node_chosen;
  34. static boot_infos_t * __initdata bootx_info;
  35. static char __initdata bootx_disp_path[256];
  36. /* Is boot-info compatible ? */
  37. #define BOOT_INFO_IS_COMPATIBLE(bi) \
  38. ((bi)->compatible_version <= BOOT_INFO_VERSION)
  39. #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
  40. #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
  41. #ifdef CONFIG_BOOTX_TEXT
  42. static void __init bootx_printf(const char *format, ...)
  43. {
  44. const char *p, *q, *s;
  45. va_list args;
  46. unsigned long v;
  47. va_start(args, format);
  48. for (p = format; *p != 0; p = q) {
  49. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  50. ;
  51. if (q > p)
  52. btext_drawtext(p, q - p);
  53. if (*q == 0)
  54. break;
  55. if (*q == '\n') {
  56. ++q;
  57. btext_flushline();
  58. btext_drawstring("\r\n");
  59. btext_flushline();
  60. continue;
  61. }
  62. ++q;
  63. if (*q == 0)
  64. break;
  65. switch (*q) {
  66. case 's':
  67. ++q;
  68. s = va_arg(args, const char *);
  69. if (s == NULL)
  70. s = "<NULL>";
  71. btext_drawstring(s);
  72. break;
  73. case 'x':
  74. ++q;
  75. v = va_arg(args, unsigned long);
  76. btext_drawhex(v);
  77. break;
  78. }
  79. }
  80. }
  81. #else /* CONFIG_BOOTX_TEXT */
  82. static void __init bootx_printf(const char *format, ...) {}
  83. #endif /* CONFIG_BOOTX_TEXT */
  84. static void * __init bootx_early_getprop(unsigned long base,
  85. unsigned long node,
  86. char *prop)
  87. {
  88. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  89. u32 *ppp = &np->properties;
  90. while(*ppp) {
  91. struct bootx_dt_prop *pp =
  92. (struct bootx_dt_prop *)(base + *ppp);
  93. if (strcmp((char *)((unsigned long)pp->name + base),
  94. prop) == 0) {
  95. return (void *)((unsigned long)pp->value + base);
  96. }
  97. ppp = &pp->next;
  98. }
  99. return NULL;
  100. }
  101. #define dt_push_token(token, mem) \
  102. do { \
  103. *(mem) = _ALIGN_UP(*(mem),4); \
  104. *((u32 *)*(mem)) = token; \
  105. *(mem) += 4; \
  106. } while(0)
  107. static unsigned long __init bootx_dt_find_string(char *str)
  108. {
  109. char *s, *os;
  110. s = os = (char *)bootx_dt_strbase;
  111. s += 4;
  112. while (s < (char *)bootx_dt_strend) {
  113. if (strcmp(s, str) == 0)
  114. return s - os;
  115. s += strlen(s) + 1;
  116. }
  117. return 0;
  118. }
  119. static void __init bootx_dt_add_prop(char *name, void *data, int size,
  120. unsigned long *mem_end)
  121. {
  122. unsigned long soff = bootx_dt_find_string(name);
  123. if (data == NULL)
  124. size = 0;
  125. if (soff == 0) {
  126. bootx_printf("WARNING: Can't find string index for <%s>\n",
  127. name);
  128. return;
  129. }
  130. if (size > 0x20000) {
  131. bootx_printf("WARNING: ignoring large property ");
  132. bootx_printf("%s length 0x%x\n", name, size);
  133. return;
  134. }
  135. dt_push_token(OF_DT_PROP, mem_end);
  136. dt_push_token(size, mem_end);
  137. dt_push_token(soff, mem_end);
  138. /* push property content */
  139. if (size && data) {
  140. memcpy((void *)*mem_end, data, size);
  141. *mem_end = _ALIGN_UP(*mem_end + size, 4);
  142. }
  143. }
  144. static void __init bootx_add_chosen_props(unsigned long base,
  145. unsigned long *mem_end)
  146. {
  147. u32 val;
  148. if (bootx_info->kernelParamsOffset) {
  149. char *args = (char *)((unsigned long)bootx_info) +
  150. bootx_info->kernelParamsOffset;
  151. bootx_dt_add_prop("bootargs", args, strlen(args) + 1, mem_end);
  152. }
  153. if (bootx_info->ramDisk) {
  154. val = ((unsigned long)bootx_info) + bootx_info->ramDisk;
  155. bootx_dt_add_prop("linux,initrd-start", &val, 4, mem_end);
  156. val += bootx_info->ramDiskSize;
  157. bootx_dt_add_prop("linux,initrd-end", &val, 4, mem_end);
  158. }
  159. if (strlen(bootx_disp_path))
  160. bootx_dt_add_prop("linux,stdout-path", bootx_disp_path,
  161. strlen(bootx_disp_path) + 1, mem_end);
  162. }
  163. static void __init bootx_add_display_props(unsigned long base,
  164. unsigned long *mem_end)
  165. {
  166. bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
  167. bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
  168. }
  169. static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
  170. {
  171. unsigned int l = strlen(s) + 1;
  172. memcpy((void *)*mem_end, s, l);
  173. bootx_dt_strend = *mem_end = *mem_end + l;
  174. }
  175. static void __init bootx_scan_dt_build_strings(unsigned long base,
  176. unsigned long node,
  177. unsigned long *mem_end)
  178. {
  179. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  180. u32 *cpp, *ppp = &np->properties;
  181. unsigned long soff;
  182. char *namep;
  183. /* Keep refs to known nodes */
  184. namep = np->full_name ? (char *)(base + np->full_name) : NULL;
  185. if (namep == NULL) {
  186. bootx_printf("Node without a full name !\n");
  187. namep = "";
  188. }
  189. DBG("* strings: %s\n", namep);
  190. if (!strcmp(namep, "/chosen")) {
  191. DBG(" detected /chosen ! adding properties names !\n");
  192. bootx_dt_add_string("linux,platform", mem_end);
  193. bootx_dt_add_string("linux,stdout-path", mem_end);
  194. bootx_dt_add_string("linux,initrd-start", mem_end);
  195. bootx_dt_add_string("linux,initrd-end", mem_end);
  196. bootx_dt_add_string("bootargs", mem_end);
  197. bootx_node_chosen = node;
  198. }
  199. if (node == bootx_info->dispDeviceRegEntryOffset) {
  200. DBG(" detected display ! adding properties names !\n");
  201. bootx_dt_add_string("linux,boot-display", mem_end);
  202. bootx_dt_add_string("linux,opened", mem_end);
  203. strncpy(bootx_disp_path, namep, 255);
  204. }
  205. /* get and store all property names */
  206. while (*ppp) {
  207. struct bootx_dt_prop *pp =
  208. (struct bootx_dt_prop *)(base + *ppp);
  209. namep = pp->name ? (char *)(base + pp->name) : NULL;
  210. if (namep == NULL || strcmp(namep, "name") == 0)
  211. goto next;
  212. /* get/create string entry */
  213. soff = bootx_dt_find_string(namep);
  214. if (soff == 0)
  215. bootx_dt_add_string(namep, mem_end);
  216. next:
  217. ppp = &pp->next;
  218. }
  219. /* do all our children */
  220. cpp = &np->child;
  221. while(*cpp) {
  222. np = (struct bootx_dt_node *)(base + *cpp);
  223. bootx_scan_dt_build_strings(base, *cpp, mem_end);
  224. cpp = &np->sibling;
  225. }
  226. }
  227. static void __init bootx_scan_dt_build_struct(unsigned long base,
  228. unsigned long node,
  229. unsigned long *mem_end)
  230. {
  231. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  232. u32 *cpp, *ppp = &np->properties;
  233. char *namep, *p, *ep, *lp;
  234. int l;
  235. dt_push_token(OF_DT_BEGIN_NODE, mem_end);
  236. /* get the node's full name */
  237. namep = np->full_name ? (char *)(base + np->full_name) : NULL;
  238. if (namep == NULL)
  239. namep = "";
  240. l = strlen(namep);
  241. DBG("* struct: %s\n", namep);
  242. /* Fixup an Apple bug where they have bogus \0 chars in the
  243. * middle of the path in some properties, and extract
  244. * the unit name (everything after the last '/').
  245. */
  246. memcpy((void *)*mem_end, namep, l + 1);
  247. namep = (char *)*mem_end;
  248. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  249. if (*p == '/')
  250. lp = namep;
  251. else if (*p != 0)
  252. *lp++ = *p;
  253. }
  254. *lp = 0;
  255. *mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
  256. /* get and store all properties */
  257. while (*ppp) {
  258. struct bootx_dt_prop *pp =
  259. (struct bootx_dt_prop *)(base + *ppp);
  260. namep = pp->name ? (char *)(base + pp->name) : NULL;
  261. /* Skip "name" */
  262. if (namep == NULL || !strcmp(namep, "name"))
  263. goto next;
  264. /* Skip "bootargs" in /chosen too as we replace it */
  265. if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
  266. goto next;
  267. /* push property head */
  268. bootx_dt_add_prop(namep,
  269. pp->value ? (void *)(base + pp->value): NULL,
  270. pp->length, mem_end);
  271. next:
  272. ppp = &pp->next;
  273. }
  274. if (node == bootx_node_chosen)
  275. bootx_add_chosen_props(base, mem_end);
  276. if (node == bootx_info->dispDeviceRegEntryOffset)
  277. bootx_add_display_props(base, mem_end);
  278. /* do all our children */
  279. cpp = &np->child;
  280. while(*cpp) {
  281. np = (struct bootx_dt_node *)(base + *cpp);
  282. bootx_scan_dt_build_struct(base, *cpp, mem_end);
  283. cpp = &np->sibling;
  284. }
  285. dt_push_token(OF_DT_END_NODE, mem_end);
  286. }
  287. static unsigned long __init bootx_flatten_dt(unsigned long start)
  288. {
  289. boot_infos_t *bi = bootx_info;
  290. unsigned long mem_start, mem_end;
  291. struct boot_param_header *hdr;
  292. unsigned long base;
  293. u64 *rsvmap;
  294. /* Start using memory after the big blob passed by BootX, get
  295. * some space for the header
  296. */
  297. mem_start = mem_end = _ALIGN_UP(((unsigned long)bi) + start, 4);
  298. DBG("Boot params header at: %x\n", mem_start);
  299. hdr = (struct boot_param_header *)mem_start;
  300. mem_end += sizeof(struct boot_param_header);
  301. rsvmap = (u64 *)(_ALIGN_UP(mem_end, 8));
  302. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
  303. mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
  304. /* Get base of tree */
  305. base = ((unsigned long)bi) + bi->deviceTreeOffset;
  306. /* Build string array */
  307. DBG("Building string array at: %x\n", mem_end);
  308. DBG("Device Tree Base=%x\n", base);
  309. bootx_dt_strbase = mem_end;
  310. mem_end += 4;
  311. bootx_dt_strend = mem_end;
  312. bootx_scan_dt_build_strings(base, 4, &mem_end);
  313. hdr->off_dt_strings = bootx_dt_strbase - mem_start;
  314. hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
  315. /* Build structure */
  316. mem_end = _ALIGN(mem_end, 16);
  317. DBG("Building device tree structure at: %x\n", mem_end);
  318. hdr->off_dt_struct = mem_end - mem_start;
  319. bootx_scan_dt_build_struct(base, 4, &mem_end);
  320. dt_push_token(OF_DT_END, &mem_end);
  321. /* Finish header */
  322. hdr->boot_cpuid_phys = 0;
  323. hdr->magic = OF_DT_HEADER;
  324. hdr->totalsize = mem_end - mem_start;
  325. hdr->version = OF_DT_VERSION;
  326. /* Version 16 is not backward compatible */
  327. hdr->last_comp_version = 0x10;
  328. /* Reserve the whole thing and copy the reserve map in, we
  329. * also bump mem_reserve_cnt to cause further reservations to
  330. * fail since it's too late.
  331. */
  332. mem_end = _ALIGN(mem_end, PAGE_SIZE);
  333. DBG("End of boot params: %x\n", mem_end);
  334. rsvmap[0] = mem_start;
  335. rsvmap[1] = mem_end;
  336. rsvmap[2] = 0;
  337. rsvmap[3] = 0;
  338. return (unsigned long)hdr;
  339. }
  340. #ifdef CONFIG_BOOTX_TEXT
  341. static void __init btext_welcome(boot_infos_t *bi)
  342. {
  343. unsigned long flags;
  344. unsigned long pvr;
  345. bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
  346. bootx_printf("\nlinked at : 0x%x", KERNELBASE);
  347. bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);
  348. bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
  349. bootx_printf(" (log)");
  350. bootx_printf("\nklimit : 0x%x",(unsigned long)klimit);
  351. bootx_printf("\nboot_info at : 0x%x", bi);
  352. __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
  353. bootx_printf("\nMSR : 0x%x", flags);
  354. __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
  355. bootx_printf("\nPVR : 0x%x", pvr);
  356. pvr >>= 16;
  357. if (pvr > 1) {
  358. __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
  359. bootx_printf("\nHID0 : 0x%x", flags);
  360. }
  361. if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
  362. __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
  363. bootx_printf("\nICTC : 0x%x", flags);
  364. }
  365. #ifdef DEBUG
  366. bootx_printf("\n\n");
  367. bootx_printf("bi->deviceTreeOffset : 0x%x\n",
  368. bi->deviceTreeOffset);
  369. bootx_printf("bi->deviceTreeSize : 0x%x\n",
  370. bi->deviceTreeSize);
  371. #endif
  372. bootx_printf("\n\n");
  373. }
  374. #endif /* CONFIG_BOOTX_TEXT */
  375. void __init bootx_init(unsigned long r3, unsigned long r4)
  376. {
  377. boot_infos_t *bi = (boot_infos_t *) r4;
  378. unsigned long hdr;
  379. unsigned long space;
  380. unsigned long ptr, x;
  381. char *model;
  382. unsigned long offset = reloc_offset();
  383. reloc_got2(offset);
  384. bootx_info = bi;
  385. /* We haven't cleared any bss at this point, make sure
  386. * what we need is initialized
  387. */
  388. bootx_dt_strbase = bootx_dt_strend = 0;
  389. bootx_node_chosen = 0;
  390. bootx_disp_path[0] = 0;
  391. if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
  392. bi->logicalDisplayBase = bi->dispDeviceBase;
  393. #ifdef CONFIG_BOOTX_TEXT
  394. btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
  395. bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
  396. bi->dispDeviceDepth, bi->dispDeviceRowBytes,
  397. (unsigned long)bi->logicalDisplayBase);
  398. btext_clearscreen();
  399. btext_flushscreen();
  400. #endif /* CONFIG_BOOTX_TEXT */
  401. /*
  402. * Test if boot-info is compatible. Done only in config
  403. * CONFIG_BOOTX_TEXT since there is nothing much we can do
  404. * with an incompatible version, except display a message
  405. * and eventually hang the processor...
  406. *
  407. * I'll try to keep enough of boot-info compatible in the
  408. * future to always allow display of this message;
  409. */
  410. if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
  411. bootx_printf(" !!! WARNING - Incompatible version"
  412. " of BootX !!!\n\n\n");
  413. for (;;)
  414. ;
  415. }
  416. if (bi->architecture != BOOT_ARCH_PCI) {
  417. bootx_printf(" !!! WARNING - Usupported machine"
  418. " architecture !\n");
  419. for (;;)
  420. ;
  421. }
  422. #ifdef CONFIG_BOOTX_TEXT
  423. btext_welcome(bi);
  424. #endif
  425. /* New BootX enters kernel with MMU off, i/os are not allowed
  426. * here. This hack will have been done by the boostrap anyway.
  427. */
  428. if (bi->version < 4) {
  429. /*
  430. * XXX If this is an iMac, turn off the USB controller.
  431. */
  432. model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
  433. 4, "model");
  434. if (model
  435. && (strcmp(model, "iMac,1") == 0
  436. || strcmp(model, "PowerMac1,1") == 0)) {
  437. bootx_printf("iMac,1 detected, shutting down USB \n");
  438. out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */
  439. }
  440. }
  441. /* Get a pointer that points above the device tree, args, ramdisk,
  442. * etc... to use for generating the flattened tree
  443. */
  444. if (bi->version < 5) {
  445. space = bi->deviceTreeOffset + bi->deviceTreeSize;
  446. if (bi->ramDisk)
  447. space = bi->ramDisk + bi->ramDiskSize;
  448. } else
  449. space = bi->totalParamsSize;
  450. bootx_printf("Total space used by parameters & ramdisk: %x \n", space);
  451. /* New BootX will have flushed all TLBs and enters kernel with
  452. * MMU switched OFF, so this should not be useful anymore.
  453. */
  454. if (bi->version < 4) {
  455. bootx_printf("Touching pages...\n");
  456. /*
  457. * Touch each page to make sure the PTEs for them
  458. * are in the hash table - the aim is to try to avoid
  459. * getting DSI exceptions while copying the kernel image.
  460. */
  461. for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
  462. ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
  463. x = *(volatile unsigned long *)ptr;
  464. }
  465. /* Ok, now we need to generate a flattened device-tree to pass
  466. * to the kernel
  467. */
  468. bootx_printf("Preparing boot params...\n");
  469. hdr = bootx_flatten_dt(space);
  470. #ifdef CONFIG_BOOTX_TEXT
  471. #ifdef SET_BOOT_BAT
  472. bootx_printf("Preparing BAT...\n");
  473. btext_prepare_BAT();
  474. #else
  475. btext_unmap();
  476. #endif
  477. #endif
  478. reloc_got2(-offset);
  479. __start(hdr, KERNELBASE + offset, 0);
  480. }