misc-embedded.c 8.2 KB

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