misc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Misc. bootloader code for many machines. This assumes you have are using
  3. * a 6xx/7xx/74xx CPU in your machine. This assumes the chunk of memory
  4. * below 8MB is free. Finally, it assumes you have a NS16550-style uart for
  5. * your serial console. If a machine meets these requirements, it can quite
  6. * likely use this code during boot.
  7. *
  8. * Author: Matt Porter <mporter@mvista.com>
  9. * Derived from arch/ppc/boot/prep/misc.c
  10. *
  11. * 2001 (c) MontaVista, Software, Inc. This file is licensed under
  12. * the terms of the GNU General Public License version 2. This program
  13. * is licensed "as is" without any warranty of any kind, whether express
  14. * or implied.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/config.h>
  18. #include <linux/string.h>
  19. #include <asm/page.h>
  20. #include <asm/mmu.h>
  21. #include <asm/bootinfo.h>
  22. #ifdef CONFIG_4xx
  23. #include <asm/ibm4xx.h>
  24. #endif
  25. #include <asm/reg.h>
  26. #include "nonstdio.h"
  27. /* Default cmdline */
  28. #ifdef CONFIG_CMDLINE
  29. #define CMDLINE CONFIG_CMDLINE
  30. #else
  31. #define CMDLINE ""
  32. #endif
  33. /* Keyboard (and VGA console)? */
  34. #ifdef CONFIG_VGA_CONSOLE
  35. #define HAS_KEYB 1
  36. #else
  37. #define HAS_KEYB 0
  38. #endif
  39. /* Will / Can the user give input?
  40. * Val Henson has requested that Gemini doesn't wait for the
  41. * user to edit the cmdline or not.
  42. */
  43. #if (defined(CONFIG_SERIAL_8250_CONSOLE) \
  44. || defined(CONFIG_VGA_CONSOLE) \
  45. || defined(CONFIG_SERIAL_MPC52xx_CONSOLE) \
  46. || defined(CONFIG_SERIAL_MPSC_CONSOLE)) \
  47. && !defined(CONFIG_GEMINI)
  48. #define INTERACTIVE_CONSOLE 1
  49. #endif
  50. char *avail_ram;
  51. char *end_avail;
  52. char *zimage_start;
  53. char cmd_preset[] = CMDLINE;
  54. char cmd_buf[256];
  55. char *cmd_line = cmd_buf;
  56. int keyb_present = HAS_KEYB;
  57. int zimage_size;
  58. unsigned long com_port;
  59. unsigned long initrd_size = 0;
  60. /* The linker tells us various locations in the image */
  61. extern char __image_begin, __image_end;
  62. extern char __ramdisk_begin, __ramdisk_end;
  63. extern char _end[];
  64. /* Original location */
  65. extern unsigned long start;
  66. extern int CRT_tstc(void);
  67. extern unsigned long serial_init(int chan, void *ignored);
  68. extern void serial_close(unsigned long com_port);
  69. extern void gunzip(void *, int, unsigned char *, int *);
  70. extern void serial_fixups(void);
  71. /* Allow get_mem_size to be hooked into. This is the default. */
  72. unsigned long __attribute__ ((weak))
  73. get_mem_size(void)
  74. {
  75. return 0;
  76. }
  77. #if defined(CONFIG_40x)
  78. #define PPC4xx_EMAC0_MR0 EMAC0_BASE
  79. #endif
  80. #if defined(CONFIG_44x) && defined(PPC44x_EMAC0_MR0)
  81. #define PPC4xx_EMAC0_MR0 PPC44x_EMAC0_MR0
  82. #endif
  83. struct bi_record *
  84. decompress_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
  85. {
  86. #ifdef INTERACTIVE_CONSOLE
  87. int timer = 0;
  88. char ch;
  89. #endif
  90. char *cp;
  91. struct bi_record *rec;
  92. unsigned long initrd_loc = 0, TotalMemory = 0;
  93. #if defined(CONFIG_SERIAL_8250_CONSOLE) || defined(CONFIG_SERIAL_MPSC_CONSOLE)
  94. com_port = serial_init(0, NULL);
  95. #endif
  96. #if defined(PPC4xx_EMAC0_MR0)
  97. /* Reset MAL */
  98. mtdcr(DCRN_MALCR(DCRN_MAL_BASE), MALCR_MMSR);
  99. /* Wait for reset */
  100. while (mfdcr(DCRN_MALCR(DCRN_MAL_BASE)) & MALCR_MMSR) {};
  101. /* Reset EMAC */
  102. *(volatile unsigned long *)PPC4xx_EMAC0_MR0 = 0x20000000;
  103. __asm__ __volatile__("eieio");
  104. #endif
  105. /*
  106. * Call get_mem_size(), which is memory controller dependent,
  107. * and we must have the correct file linked in here.
  108. */
  109. TotalMemory = get_mem_size();
  110. /* assume the chunk below 8M is free */
  111. end_avail = (char *)0x00800000;
  112. /*
  113. * Reveal where we were loaded at and where we
  114. * were relocated to.
  115. */
  116. puts("loaded at: "); puthex(load_addr);
  117. puts(" "); puthex((unsigned long)(load_addr + (4*num_words)));
  118. puts("\n");
  119. if ( (unsigned long)load_addr != (unsigned long)&start )
  120. {
  121. puts("relocated to: "); puthex((unsigned long)&start);
  122. puts(" ");
  123. puthex((unsigned long)((unsigned long)&start + (4*num_words)));
  124. puts("\n");
  125. }
  126. /*
  127. * We link ourself to 0x00800000. When we run, we relocate
  128. * ourselves there. So we just need __image_begin for the
  129. * start. -- Tom
  130. */
  131. zimage_start = (char *)(unsigned long)(&__image_begin);
  132. zimage_size = (unsigned long)(&__image_end) -
  133. (unsigned long)(&__image_begin);
  134. initrd_size = (unsigned long)(&__ramdisk_end) -
  135. (unsigned long)(&__ramdisk_begin);
  136. /*
  137. * The zImage and initrd will be between start and _end, so they've
  138. * already been moved once. We're good to go now. -- Tom
  139. */
  140. avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
  141. puts("zimage at: "); puthex((unsigned long)zimage_start);
  142. puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
  143. puts("\n");
  144. if ( initrd_size ) {
  145. puts("initrd at: ");
  146. puthex((unsigned long)(&__ramdisk_begin));
  147. puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
  148. }
  149. #ifndef CONFIG_40x /* don't overwrite the 40x image located at 0x00400000! */
  150. avail_ram = (char *)0x00400000;
  151. #endif
  152. end_avail = (char *)0x00800000;
  153. puts("avail ram: "); puthex((unsigned long)avail_ram); puts(" ");
  154. puthex((unsigned long)end_avail); puts("\n");
  155. if (keyb_present)
  156. CRT_tstc(); /* Forces keyboard to be initialized */
  157. #ifdef CONFIG_GEMINI
  158. /*
  159. * If cmd_line is empty and cmd_preset is not, copy cmd_preset
  160. * to cmd_line. This way we can override cmd_preset with the
  161. * command line from Smon.
  162. */
  163. if ( (cmd_line[0] == '\0') && (cmd_preset[0] != '\0'))
  164. memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
  165. #endif
  166. /* Display standard Linux/PPC boot prompt for kernel args */
  167. puts("\nLinux/PPC load: ");
  168. cp = cmd_line;
  169. memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
  170. while ( *cp ) putc(*cp++);
  171. #ifdef INTERACTIVE_CONSOLE
  172. /*
  173. * If they have a console, allow them to edit the command line.
  174. * Otherwise, don't bother wasting the five seconds.
  175. */
  176. while (timer++ < 5*1000) {
  177. if (tstc()) {
  178. while ((ch = getc()) != '\n' && ch != '\r') {
  179. /* Test for backspace/delete */
  180. if (ch == '\b' || ch == '\177') {
  181. if (cp != cmd_line) {
  182. cp--;
  183. puts("\b \b");
  184. }
  185. /* Test for ^x/^u (and wipe the line) */
  186. } else if (ch == '\030' || ch == '\025') {
  187. while (cp != cmd_line) {
  188. cp--;
  189. puts("\b \b");
  190. }
  191. } else {
  192. *cp++ = ch;
  193. putc(ch);
  194. }
  195. }
  196. break; /* Exit 'timer' loop */
  197. }
  198. udelay(1000); /* 1 msec */
  199. }
  200. *cp = 0;
  201. #endif
  202. puts("\n");
  203. puts("Uncompressing Linux...");
  204. gunzip(NULL, 0x400000, zimage_start, &zimage_size);
  205. puts("done.\n");
  206. /* get the bi_rec address */
  207. rec = bootinfo_addr(zimage_size);
  208. /* We need to make sure that the initrd and bi_recs do not
  209. * overlap. */
  210. if ( initrd_size ) {
  211. unsigned long rec_loc = (unsigned long) rec;
  212. initrd_loc = (unsigned long)(&__ramdisk_begin);
  213. /* If the bi_recs are in the middle of the current
  214. * initrd, move the initrd to the next MB
  215. * boundary. */
  216. if ((rec_loc > initrd_loc) &&
  217. ((initrd_loc + initrd_size) > rec_loc)) {
  218. initrd_loc = _ALIGN((unsigned long)(zimage_size)
  219. + (2 << 20) - 1, (2 << 20));
  220. memmove((void *)initrd_loc, &__ramdisk_begin,
  221. initrd_size);
  222. puts("initrd moved: "); puthex(initrd_loc);
  223. puts(" "); puthex(initrd_loc + initrd_size);
  224. puts("\n");
  225. }
  226. }
  227. bootinfo_init(rec);
  228. if ( TotalMemory )
  229. bootinfo_append(BI_MEMSIZE, sizeof(int), (void*)&TotalMemory);
  230. bootinfo_append(BI_CMD_LINE, strlen(cmd_line)+1, (void*)cmd_line);
  231. /* add a bi_rec for the initrd if it exists */
  232. if (initrd_size) {
  233. unsigned long initrd[2];
  234. initrd[0] = initrd_loc;
  235. initrd[1] = initrd_size;
  236. bootinfo_append(BI_INITRD, sizeof(initrd), &initrd);
  237. }
  238. puts("Now booting the kernel\n");
  239. serial_close(com_port);
  240. return rec;
  241. }
  242. void __attribute__ ((weak))
  243. board_isa_init(void)
  244. {
  245. }
  246. /* Allow decompress_kernel to be hooked into. This is the default. */
  247. void * __attribute__ ((weak))
  248. load_kernel(unsigned long load_addr, int num_words, unsigned long cksum,
  249. void *ign1, void *ign2)
  250. {
  251. board_isa_init();
  252. return decompress_kernel(load_addr, num_words, cksum);
  253. }