m68k_linux.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <image.h>
  26. #include <zlib.h>
  27. #include <bzlib.h>
  28. #include <watchdog.h>
  29. #include <environment.h>
  30. #include <asm/byteorder.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #define PHYSADDR(x) x
  33. #define LINUX_MAX_ENVS 256
  34. #define LINUX_MAX_ARGS 256
  35. #define CHUNKSZ (64 * 1024)
  36. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  37. # include <status_led.h>
  38. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  39. #else
  40. # define SHOW_BOOT_PROGRESS(arg)
  41. #endif
  42. extern image_header_t header;
  43. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  44. void do_bootm_linux(cmd_tbl_t * cmdtp, int flag,
  45. int argc, char *argv[],
  46. ulong addr, ulong * len_ptr, int verify)
  47. {
  48. ulong sp;
  49. ulong len;
  50. ulong initrd_start, initrd_end;
  51. ulong cmd_start, cmd_end;
  52. ulong initrd_high;
  53. ulong data;
  54. int initrd_copy_to_ram = 1;
  55. char *cmdline;
  56. char *s;
  57. bd_t *kbd;
  58. void (*kernel) (bd_t *, ulong, ulong, ulong, ulong);
  59. image_header_t *hdr = &header;
  60. if ((s = getenv("initrd_high")) != NULL) {
  61. /* a value of "no" or a similar string will act like 0,
  62. * turning the "load high" feature off. This is intentional.
  63. */
  64. initrd_high = simple_strtoul(s, NULL, 16);
  65. if (initrd_high == ~0)
  66. initrd_copy_to_ram = 0;
  67. } else { /* not set, no restrictions to load high */
  68. initrd_high = ~0;
  69. }
  70. #ifdef CONFIG_LOGBUFFER
  71. kbd = gd->bd;
  72. /* Prevent initrd from overwriting logbuffer */
  73. if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
  74. initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
  75. debug("## Logbuffer at 0x%08lX ", kbd->bi_memsize - LOGBUFF_LEN);
  76. #endif
  77. /*
  78. * Booting a (Linux) kernel image
  79. *
  80. * Allocate space for command line and board info - the
  81. * address should be as high as possible within the reach of
  82. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  83. * memory, which means far enough below the current stack
  84. * pointer.
  85. */
  86. asm("movel %%a7, %%d0\n"
  87. "movel %%d0, %0\n": "=d"(sp): :"%d0");
  88. debug("## Current stack ends at 0x%08lX ", sp);
  89. sp -= 2048; /* just to be sure */
  90. if (sp > CFG_BOOTMAPSZ)
  91. sp = CFG_BOOTMAPSZ;
  92. sp &= ~0xF;
  93. debug("=> set upper limit to 0x%08lX\n", sp);
  94. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  95. kbd = (bd_t *) (((ulong) cmdline - sizeof(bd_t)) & ~0xF);
  96. if ((s = getenv("bootargs")) == NULL)
  97. s = "";
  98. strcpy(cmdline, s);
  99. cmd_start = (ulong) & cmdline[0];
  100. cmd_end = cmd_start + strlen(cmdline);
  101. *kbd = *(gd->bd);
  102. #ifdef DEBUG
  103. printf("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  104. do_bdinfo(NULL, 0, 0, NULL);
  105. #endif
  106. if ((s = getenv("clocks_in_mhz")) != NULL) {
  107. /* convert all clock information to MHz */
  108. kbd->bi_intfreq /= 1000000L;
  109. kbd->bi_busfreq /= 1000000L;
  110. }
  111. kernel =
  112. (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
  113. /*
  114. * Check if there is an initrd image
  115. */
  116. if (argc >= 3) {
  117. debug("Not skipping initrd\n");
  118. SHOW_BOOT_PROGRESS(9);
  119. addr = simple_strtoul(argv[2], NULL, 16);
  120. hdr = (image_header_t *)addr;
  121. printf("## Loading RAMDisk Image at %08lx ...\n", addr);
  122. if (!image_check_magic (hdr)) {
  123. puts("Bad Magic Number\n");
  124. SHOW_BOOT_PROGRESS(-10);
  125. do_reset(cmdtp, flag, argc, argv);
  126. }
  127. if (!image_check_hcrc (hdr)) {
  128. puts("Bad Header Checksum\n");
  129. SHOW_BOOT_PROGRESS(-11);
  130. do_reset(cmdtp, flag, argc, argv);
  131. }
  132. SHOW_BOOT_PROGRESS(10);
  133. print_image_hdr (hdr);
  134. data = image_get_data (hdr);
  135. len = image_get_data_size (hdr);
  136. if (verify) {
  137. puts(" Verifying Checksum ... ");
  138. if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
  139. puts("Bad Data CRC\n");
  140. SHOW_BOOT_PROGRESS(-12);
  141. do_reset(cmdtp, flag, argc, argv);
  142. }
  143. puts("OK\n");
  144. }
  145. SHOW_BOOT_PROGRESS(11);
  146. if (!image_check_os (hdr, IH_OS_LINUX) ||
  147. !image_check_arch (hdr, IH_ARCH_M68K) ||
  148. !image_check_type (hdr, IH_TYPE_RAMDISK)) {
  149. puts("No Linux ColdFire Ramdisk Image\n");
  150. SHOW_BOOT_PROGRESS(-13);
  151. do_reset(cmdtp, flag, argc, argv);
  152. }
  153. /*
  154. * Now check if we have a multifile image
  155. */
  156. } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
  157. u_long tail = image_to_cpu (len_ptr[0]) % 4;
  158. int i;
  159. SHOW_BOOT_PROGRESS(13);
  160. /* skip kernel length and terminator */
  161. data = (ulong) (&len_ptr[2]);
  162. /* skip any additional image length fields */
  163. for (i = 1; len_ptr[i]; ++i)
  164. data += 4;
  165. /* add kernel length, and align */
  166. data += image_to_cpu (len_ptr[0]);
  167. if (tail) {
  168. data += 4 - tail;
  169. }
  170. len = image_to_cpu (len_ptr[1]);
  171. } else {
  172. /*
  173. * no initrd image
  174. */
  175. SHOW_BOOT_PROGRESS(14);
  176. len = data = 0;
  177. }
  178. if (!data) {
  179. debug("No initrd\n");
  180. }
  181. if (data) {
  182. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  183. initrd_start = data;
  184. initrd_end = initrd_start + len;
  185. } else {
  186. initrd_start = (ulong) kbd - len;
  187. initrd_start &= ~(4096 - 1); /* align on page */
  188. if (initrd_high) {
  189. ulong nsp;
  190. /*
  191. * the inital ramdisk does not need to be within
  192. * CFG_BOOTMAPSZ as it is not accessed until after
  193. * the mm system is initialised.
  194. *
  195. * do the stack bottom calculation again and see if
  196. * the initrd will fit just below the monitor stack
  197. * bottom without overwriting the area allocated
  198. * above for command line args and board info.
  199. */
  200. asm("movel %%a7, %%d0\n"
  201. "movel %%d0, %0\n": "=d"(nsp): :"%d0");
  202. nsp -= 2048; /* just to be sure */
  203. nsp &= ~0xF;
  204. if (nsp > initrd_high) /* limit as specified */
  205. nsp = initrd_high;
  206. nsp -= len;
  207. nsp &= ~(4096 - 1); /* align on page */
  208. if (nsp >= sp)
  209. initrd_start = nsp;
  210. }
  211. SHOW_BOOT_PROGRESS(12);
  212. debug
  213. ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  214. data, data + len - 1, len, len);
  215. initrd_end = initrd_start + len;
  216. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  217. initrd_start, initrd_end);
  218. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  219. {
  220. size_t l = len;
  221. void *to = (void *)initrd_start;
  222. void *from = (void *)data;
  223. while (l > 0) {
  224. size_t tail =
  225. (l > CHUNKSZ) ? CHUNKSZ : l;
  226. WATCHDOG_RESET();
  227. memmove(to, from, tail);
  228. to += tail;
  229. from += tail;
  230. l -= tail;
  231. }
  232. }
  233. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  234. memmove((void *)initrd_start, (void *)data, len);
  235. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  236. puts("OK\n");
  237. }
  238. } else {
  239. initrd_start = 0;
  240. initrd_end = 0;
  241. }
  242. debug("## Transferring control to Linux (at address %08lx) ...\n",
  243. (ulong) kernel);
  244. SHOW_BOOT_PROGRESS(15);
  245. /*
  246. * Linux Kernel Parameters (passing board info data):
  247. * r3: ptr to board info data
  248. * r4: initrd_start or 0 if no initrd
  249. * r5: initrd_end - unused if r4 is 0
  250. * r6: Start of command line string
  251. * r7: End of command line string
  252. */
  253. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  254. /* does not return */
  255. }