misc.c 7.2 KB

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