m68k_linux.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <environment.h>
  29. #include <asm/byteorder.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #define PHYSADDR(x) x
  32. #define LINUX_MAX_ENVS 256
  33. #define LINUX_MAX_ARGS 256
  34. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  35. # include <status_led.h>
  36. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  37. #else
  38. # define SHOW_BOOT_PROGRESS(arg)
  39. #endif
  40. extern image_header_t header;
  41. void do_bootm_linux(cmd_tbl_t * cmdtp, int flag,
  42. int argc, char *argv[],
  43. ulong addr, ulong * len_ptr, int verify)
  44. {
  45. ulong sp;
  46. ulong len, checksum;
  47. ulong initrd_start, initrd_end;
  48. ulong cmd_start, cmd_end;
  49. ulong initrd_high;
  50. ulong data;
  51. int initrd_copy_to_ram = 1;
  52. char *cmdline;
  53. char *s;
  54. bd_t *kbd;
  55. void (*kernel) (bd_t *, ulong, ulong, ulong, ulong);
  56. image_header_t *hdr = &header;
  57. if ((s = getenv("initrd_high")) != NULL) {
  58. /* a value of "no" or a similar string will act like 0,
  59. * turning the "load high" feature off. This is intentional.
  60. */
  61. initrd_high = simple_strtoul(s, NULL, 16);
  62. if (initrd_high == ~0)
  63. initrd_copy_to_ram = 0;
  64. } else { /* not set, no restrictions to load high */
  65. initrd_high = ~0;
  66. }
  67. #ifdef CONFIG_LOGBUFFER
  68. kbd = gd->bd;
  69. /* Prevent initrd from overwriting logbuffer */
  70. if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
  71. initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
  72. debug("## Logbuffer at 0x%08lX ", kbd->bi_memsize - LOGBUFF_LEN);
  73. #endif
  74. /*
  75. * Booting a (Linux) kernel image
  76. *
  77. * Allocate space for command line and board info - the
  78. * address should be as high as possible within the reach of
  79. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  80. * memory, which means far enough below the current stack
  81. * pointer.
  82. */
  83. asm("movel %%a7, %%d0\n"
  84. "movel %%d0, %0\n": "=d"(sp): :"%d0");
  85. debug("## Current stack ends at 0x%08lX ", sp);
  86. sp -= 2048; /* just to be sure */
  87. if (sp > CFG_BOOTMAPSZ)
  88. sp = CFG_BOOTMAPSZ;
  89. sp &= ~0xF;
  90. debug("=> set upper limit to 0x%08lX\n", sp);
  91. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  92. kbd = (bd_t *) (((ulong) cmdline - sizeof(bd_t)) & ~0xF);
  93. if ((s = getenv("bootargs")) == NULL)
  94. s = "";
  95. strcpy(cmdline, s);
  96. cmd_start = (ulong) & cmdline[0];
  97. cmd_end = cmd_start + strlen(cmdline);
  98. *kbd = *(gd->bd);
  99. #ifdef DEBUG
  100. printf("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  101. do_bdinfo(NULL, 0, 0, NULL);
  102. #endif
  103. if ((s = getenv("clocks_in_mhz")) != NULL) {
  104. /* convert all clock information to MHz */
  105. kbd->bi_intfreq /= 1000000L;
  106. kbd->bi_busfreq /= 1000000L;
  107. }
  108. kernel =
  109. (void (*)(bd_t *, ulong, ulong, ulong, ulong))ntohl(hdr->ih_ep);
  110. /*
  111. * Check if there is an initrd image
  112. */
  113. if (argc >= 3) {
  114. debug("Not skipping initrd\n");
  115. SHOW_BOOT_PROGRESS(9);
  116. addr = simple_strtoul(argv[2], NULL, 16);
  117. printf("## Loading RAMDisk Image at %08lx ...\n", addr);
  118. /* Copy header so we can blank CRC field for re-calculation */
  119. memmove(&header, (char *)addr, sizeof(image_header_t));
  120. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  121. puts("Bad Magic Number\n");
  122. SHOW_BOOT_PROGRESS(-10);
  123. do_reset(cmdtp, flag, argc, argv);
  124. }
  125. data = (ulong) & header;
  126. len = sizeof(image_header_t);
  127. checksum = ntohl(hdr->ih_hcrc);
  128. hdr->ih_hcrc = 0;
  129. if (crc32(0, (uchar *) data, len) != checksum) {
  130. puts("Bad Header Checksum\n");
  131. SHOW_BOOT_PROGRESS(-11);
  132. do_reset(cmdtp, flag, argc, argv);
  133. }
  134. SHOW_BOOT_PROGRESS(10);
  135. print_image_hdr(hdr);
  136. data = addr + sizeof(image_header_t);
  137. len = ntohl(hdr->ih_size);
  138. if (verify) {
  139. ulong csum = 0;
  140. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  141. ulong cdata = data, edata = cdata + len;
  142. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  143. puts(" Verifying Checksum ... ");
  144. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  145. while (cdata < edata) {
  146. ulong chunk = edata - cdata;
  147. if (chunk > CHUNKSZ)
  148. chunk = CHUNKSZ;
  149. csum = crc32(csum, (uchar *) cdata, chunk);
  150. cdata += chunk;
  151. WATCHDOG_RESET();
  152. }
  153. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  154. csum = crc32(0, (uchar *) data, len);
  155. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  156. if (csum != ntohl(hdr->ih_dcrc)) {
  157. puts("Bad Data CRC\n");
  158. SHOW_BOOT_PROGRESS(-12);
  159. do_reset(cmdtp, flag, argc, argv);
  160. }
  161. puts("OK\n");
  162. }
  163. SHOW_BOOT_PROGRESS(11);
  164. if ((hdr->ih_os != IH_OS_LINUX) ||
  165. (hdr->ih_arch != IH_CPU_M68K) ||
  166. (hdr->ih_type != IH_TYPE_RAMDISK)) {
  167. puts("No Linux ColdFire Ramdisk Image\n");
  168. SHOW_BOOT_PROGRESS(-13);
  169. do_reset(cmdtp, flag, argc, argv);
  170. }
  171. /*
  172. * Now check if we have a multifile image
  173. */
  174. } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
  175. u_long tail = ntohl(len_ptr[0]) % 4;
  176. int i;
  177. SHOW_BOOT_PROGRESS(13);
  178. /* skip kernel length and terminator */
  179. data = (ulong) (&len_ptr[2]);
  180. /* skip any additional image length fields */
  181. for (i = 1; len_ptr[i]; ++i)
  182. data += 4;
  183. /* add kernel length, and align */
  184. data += ntohl(len_ptr[0]);
  185. if (tail) {
  186. data += 4 - tail;
  187. }
  188. len = ntohl(len_ptr[1]);
  189. } else {
  190. /*
  191. * no initrd image
  192. */
  193. SHOW_BOOT_PROGRESS(14);
  194. len = data = 0;
  195. }
  196. if (!data) {
  197. debug("No initrd\n");
  198. }
  199. if (data) {
  200. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  201. initrd_start = data;
  202. initrd_end = initrd_start + len;
  203. } else {
  204. initrd_start = (ulong) kbd - len;
  205. initrd_start &= ~(4096 - 1); /* align on page */
  206. if (initrd_high) {
  207. ulong nsp;
  208. /*
  209. * the inital ramdisk does not need to be within
  210. * CFG_BOOTMAPSZ as it is not accessed until after
  211. * the mm system is initialised.
  212. *
  213. * do the stack bottom calculation again and see if
  214. * the initrd will fit just below the monitor stack
  215. * bottom without overwriting the area allocated
  216. * above for command line args and board info.
  217. */
  218. asm("movel %%a7, %%d0\n"
  219. "movel %%d0, %0\n": "=d"(nsp): :"%d0");
  220. nsp -= 2048; /* just to be sure */
  221. nsp &= ~0xF;
  222. if (nsp > initrd_high) /* limit as specified */
  223. nsp = initrd_high;
  224. nsp -= len;
  225. nsp &= ~(4096 - 1); /* align on page */
  226. if (nsp >= sp)
  227. initrd_start = nsp;
  228. }
  229. SHOW_BOOT_PROGRESS(12);
  230. debug
  231. ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  232. data, data + len - 1, len, len);
  233. initrd_end = initrd_start + len;
  234. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  235. initrd_start, initrd_end);
  236. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  237. {
  238. size_t l = len;
  239. void *to = (void *)initrd_start;
  240. void *from = (void *)data;
  241. while (l > 0) {
  242. size_t tail =
  243. (l > CHUNKSZ) ? CHUNKSZ : l;
  244. WATCHDOG_RESET();
  245. memmove(to, from, tail);
  246. to += tail;
  247. from += tail;
  248. l -= tail;
  249. }
  250. }
  251. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  252. memmove((void *)initrd_start, (void *)data, len);
  253. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  254. puts("OK\n");
  255. }
  256. } else {
  257. initrd_start = 0;
  258. initrd_end = 0;
  259. }
  260. debug("## Transferring control to Linux (at address %08lx) ...\n",
  261. (ulong) kernel);
  262. SHOW_BOOT_PROGRESS(15);
  263. /*
  264. * Linux Kernel Parameters (passing board info data):
  265. * r3: ptr to board info data
  266. * r4: initrd_start or 0 if no initrd
  267. * r5: initrd_end - unused if r4 is 0
  268. * r6: Start of command line string
  269. * r7: End of command line string
  270. */
  271. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  272. /* does not return */
  273. }