misc-embedded.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Originally adapted by Gary Thomas. Much additional work by
  3. * Cort Dougan <cort@fsmlabs.com>. On top of that still more work by
  4. * Dan Malek <dmalek@jlc.net>.
  5. *
  6. * Currently maintained by: Tom Rini <trini@kernel.crashing.org>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #include <asm/bootinfo.h>
  11. #include <asm/mmu.h>
  12. #include <asm/page.h>
  13. #include <asm/residual.h>
  14. #if defined(CONFIG_4xx)
  15. #include <asm/ibm4xx.h>
  16. #elif defined(CONFIG_8xx)
  17. #include <asm/mpc8xx.h>
  18. #elif defined(CONFIG_8260)
  19. #include <asm/mpc8260.h>
  20. #endif
  21. #include "nonstdio.h"
  22. /* The linker tells us where the image is. */
  23. extern char __image_begin, __image_end;
  24. extern char __ramdisk_begin, __ramdisk_end;
  25. extern char _end[];
  26. /* Because of the limited amount of memory on embedded, it presents
  27. * loading problems. The biggest is that we load this boot program
  28. * into a relatively low memory address, and the Linux kernel Bss often
  29. * extends into this space when it get loaded. When the kernel starts
  30. * and zeros the BSS space, it also writes over the information we
  31. * save here and pass to the kernel (usually board info).
  32. * On these boards, we grab some known memory holes to hold this information.
  33. */
  34. char cmd_buf[256];
  35. char *cmd_line = cmd_buf;
  36. char *avail_ram;
  37. char *end_avail;
  38. char *zimage_start;
  39. /* This is for 4xx treeboot. It provides a place for the bootrom
  40. * give us a pointer to a rom environment command line.
  41. */
  42. char *bootrom_cmdline = "";
  43. /* This is the default cmdline that will be given to the user at boot time..
  44. * If none was specified at compile time, we'll give it one that should work.
  45. * -- Tom */
  46. #ifdef CONFIG_CMDLINE_BOOL
  47. char compiled_string[] = CONFIG_CMDLINE;
  48. #endif
  49. char ramroot_string[] = "root=/dev/ram";
  50. char netroot_string[] = "root=/dev/nfs rw ip=on";
  51. /* Serial port to use. */
  52. unsigned long com_port;
  53. /* We need to make sure that this is before the images to ensure
  54. * that it's in a mapped location. - Tom */
  55. bd_t hold_resid_buf __attribute__ ((__section__ (".data.boot")));
  56. bd_t *hold_residual = &hold_resid_buf;
  57. extern unsigned long serial_init(int chan, bd_t *bp);
  58. extern void serial_close(unsigned long com_port);
  59. extern unsigned long start;
  60. extern void flush_instruction_cache(void);
  61. extern void gunzip(void *, int, unsigned char *, int *);
  62. extern void embed_config(bd_t **bp);
  63. /* Weak function for boards which don't need to build the
  64. * board info struct because they are using PPCBoot/U-Boot.
  65. */
  66. void __attribute__ ((weak))
  67. embed_config(bd_t **bdp)
  68. {
  69. }
  70. unsigned long
  71. load_kernel(unsigned long load_addr, int num_words, unsigned long cksum, bd_t *bp)
  72. {
  73. char *cp, ch;
  74. int timer = 0, zimage_size;
  75. unsigned long initrd_size;
  76. /* First, capture the embedded board information. Then
  77. * initialize the serial console port.
  78. */
  79. embed_config(&bp);
  80. #if defined(CONFIG_SERIAL_CPM_CONSOLE) || \
  81. defined(CONFIG_SERIAL_8250_CONSOLE) || \
  82. defined(CONFIG_SERIAL_UARTLITE_CONSOLE)
  83. com_port = serial_init(0, bp);
  84. #endif
  85. /* Grab some space for the command line and board info. Since
  86. * we no longer use the ELF header, but it was loaded, grab
  87. * that space.
  88. */
  89. #ifdef CONFIG_MBX
  90. /* Because of the way the MBX loads the ELF image, we can't
  91. * tell where we started. We read a magic variable from the NVRAM
  92. * that gives us the intermediate buffer load address.
  93. */
  94. load_addr = *(uint *)0xfa000020;
  95. load_addr += 0x10000; /* Skip ELF header */
  96. #endif
  97. /* copy board data */
  98. if (bp)
  99. memcpy(hold_residual,bp,sizeof(bd_t));
  100. /* Set end of memory available to us. It is always the highest
  101. * memory address provided by the board information.
  102. */
  103. end_avail = (char *)(bp->bi_memsize);
  104. puts("\nloaded at: "); puthex(load_addr);
  105. puts(" "); puthex((unsigned long)(load_addr + (4*num_words))); puts("\n");
  106. if ( (unsigned long)load_addr != (unsigned long)&start ) {
  107. puts("relocated to: "); puthex((unsigned long)&start);
  108. puts(" ");
  109. puthex((unsigned long)((unsigned long)&start + (4*num_words)));
  110. puts("\n");
  111. }
  112. if ( bp ) {
  113. puts("board data at: "); puthex((unsigned long)bp);
  114. puts(" ");
  115. puthex((unsigned long)((unsigned long)bp + sizeof(bd_t)));
  116. puts("\nrelocated to: ");
  117. puthex((unsigned long)hold_residual);
  118. puts(" ");
  119. puthex((unsigned long)((unsigned long)hold_residual + sizeof(bd_t)));
  120. puts("\n");
  121. }
  122. /*
  123. * We link ourself to an arbitrary low address. When we run, we
  124. * relocate ourself to that address. __image_being points to
  125. * the part of the image where the zImage is. -- 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. puts("zimage at: "); puthex((unsigned long)zimage_start);
  137. puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
  138. puts("\n");
  139. if ( initrd_size ) {
  140. puts("initrd at: ");
  141. puthex((unsigned long)(&__ramdisk_begin));
  142. puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
  143. }
  144. /*
  145. * setup avail_ram - this is the first part of ram usable
  146. * by the uncompress code. Anything after this program in RAM
  147. * is now fair game. -- Tom
  148. */
  149. avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
  150. puts("avail ram: "); puthex((unsigned long)avail_ram); puts(" ");
  151. puthex((unsigned long)end_avail); puts("\n");
  152. puts("\nLinux/PPC load: ");
  153. cp = cmd_line;
  154. /* This is where we try and pick the right command line for booting.
  155. * If we were given one at compile time, use it. It Is Right.
  156. * If we weren't, see if we have a ramdisk. If so, thats root.
  157. * When in doubt, give them the netroot (root=/dev/nfs rw) -- Tom
  158. */
  159. #ifdef CONFIG_CMDLINE_BOOL
  160. memcpy (cmd_line, compiled_string, sizeof(compiled_string));
  161. #else
  162. if ( initrd_size )
  163. memcpy (cmd_line, ramroot_string, sizeof(ramroot_string));
  164. else
  165. memcpy (cmd_line, netroot_string, sizeof(netroot_string));
  166. #endif
  167. while ( *cp )
  168. putc(*cp++);
  169. while (timer++ < 5*1000) {
  170. if (tstc()) {
  171. while ((ch = getc()) != '\n' && ch != '\r') {
  172. if (ch == '\b' || ch == '\177') {
  173. if (cp != cmd_line) {
  174. cp--;
  175. puts("\b \b");
  176. }
  177. } else if (ch == '\030' /* ^x */
  178. || ch == '\025') { /* ^u */
  179. while (cp != cmd_line) {
  180. cp--;
  181. puts("\b \b");
  182. }
  183. } else {
  184. *cp++ = ch;
  185. putc(ch);
  186. }
  187. }
  188. break; /* Exit 'timer' loop */
  189. }
  190. udelay(1000); /* 1 msec */
  191. }
  192. *cp = 0;
  193. puts("\nUncompressing Linux...");
  194. gunzip(0, 0x400000, zimage_start, &zimage_size);
  195. flush_instruction_cache();
  196. puts("done.\n");
  197. {
  198. struct bi_record *rec;
  199. unsigned long initrd_loc = 0;
  200. unsigned long rec_loc = _ALIGN((unsigned long)(zimage_size) +
  201. (1 << 20) - 1, (1 << 20));
  202. rec = (struct bi_record *)rec_loc;
  203. /* We need to make sure that the initrd and bi_recs do not
  204. * overlap. */
  205. if ( initrd_size ) {
  206. initrd_loc = (unsigned long)(&__ramdisk_begin);
  207. /* If the bi_recs are in the middle of the current
  208. * initrd, move the initrd to the next MB
  209. * boundary. */
  210. if ((rec_loc > initrd_loc) &&
  211. ((initrd_loc + initrd_size)
  212. > rec_loc)) {
  213. initrd_loc = _ALIGN((unsigned long)(zimage_size)
  214. + (2 << 20) - 1, (2 << 20));
  215. memmove((void *)initrd_loc, &__ramdisk_begin,
  216. initrd_size);
  217. puts("initrd moved: "); puthex(initrd_loc);
  218. puts(" "); puthex(initrd_loc + initrd_size);
  219. puts("\n");
  220. }
  221. }
  222. rec->tag = BI_FIRST;
  223. rec->size = sizeof(struct bi_record);
  224. rec = (struct bi_record *)((unsigned long)rec + rec->size);
  225. rec->tag = BI_CMD_LINE;
  226. memcpy( (char *)rec->data, cmd_line, strlen(cmd_line)+1);
  227. rec->size = sizeof(struct bi_record) + strlen(cmd_line) + 1;
  228. rec = (struct bi_record *)((unsigned long)rec + rec->size);
  229. if ( initrd_size ) {
  230. rec->tag = BI_INITRD;
  231. rec->data[0] = initrd_loc;
  232. rec->data[1] = initrd_size;
  233. rec->size = sizeof(struct bi_record) + 2 *
  234. sizeof(unsigned long);
  235. rec = (struct bi_record *)((unsigned long)rec +
  236. rec->size);
  237. }
  238. rec->tag = BI_LAST;
  239. rec->size = sizeof(struct bi_record);
  240. rec = (struct bi_record *)((unsigned long)rec + rec->size);
  241. }
  242. puts("Now booting the kernel\n");
  243. #if defined(CONFIG_SERIAL_CPM_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
  244. serial_close(com_port);
  245. #endif
  246. return (unsigned long)hold_residual;
  247. }