m68k_linux.c 8.3 KB

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