bootx_init.c 16 KB

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