mips_linux.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <asm/byteorder.h>
  28. #include <asm/addrspace.h>
  29. #define LINUX_MAX_ENVS 256
  30. #define LINUX_MAX_ARGS 256
  31. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  32. # include <status_led.h>
  33. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  34. #else
  35. # define SHOW_BOOT_PROGRESS(arg)
  36. #endif
  37. extern image_header_t header; /* from cmd_bootm.c */
  38. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  39. static int linux_argc;
  40. static char ** linux_argv;
  41. static char ** linux_env;
  42. static char * linux_env_p;
  43. static int linux_env_idx;
  44. static void linux_params_init (ulong start, char * commandline);
  45. static void linux_env_set (char * env_name, char * env_val);
  46. void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
  47. ulong addr, ulong * len_ptr, int verify)
  48. {
  49. DECLARE_GLOBAL_DATA_PTR;
  50. ulong len = 0, checksum;
  51. ulong initrd_start, initrd_end;
  52. ulong data;
  53. void (*theKernel) (int, char **, char **, int *);
  54. image_header_t *hdr = &header;
  55. char *commandline = getenv ("bootargs");
  56. char env_buf[12];
  57. theKernel =
  58. (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep);
  59. /*
  60. * Check if there is an initrd image
  61. */
  62. if (argc >= 3) {
  63. SHOW_BOOT_PROGRESS (9);
  64. addr = simple_strtoul (argv[2], NULL, 16);
  65. printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
  66. /* Copy header so we can blank CRC field for re-calculation */
  67. memcpy (&header, (char *) addr, sizeof (image_header_t));
  68. if (ntohl (hdr->ih_magic) != IH_MAGIC) {
  69. printf ("Bad Magic Number\n");
  70. SHOW_BOOT_PROGRESS (-10);
  71. do_reset (cmdtp, flag, argc, argv);
  72. }
  73. data = (ulong) & header;
  74. len = sizeof (image_header_t);
  75. checksum = ntohl (hdr->ih_hcrc);
  76. hdr->ih_hcrc = 0;
  77. if (crc32 (0, (uchar *) data, len) != checksum) {
  78. printf ("Bad Header Checksum\n");
  79. SHOW_BOOT_PROGRESS (-11);
  80. do_reset (cmdtp, flag, argc, argv);
  81. }
  82. SHOW_BOOT_PROGRESS (10);
  83. print_image_hdr (hdr);
  84. data = addr + sizeof (image_header_t);
  85. len = ntohl (hdr->ih_size);
  86. if (verify) {
  87. ulong csum = 0;
  88. printf (" Verifying Checksum ... ");
  89. csum = crc32 (0, (uchar *) data, len);
  90. if (csum != ntohl (hdr->ih_dcrc)) {
  91. printf ("Bad Data CRC\n");
  92. SHOW_BOOT_PROGRESS (-12);
  93. do_reset (cmdtp, flag, argc, argv);
  94. }
  95. printf ("OK\n");
  96. }
  97. SHOW_BOOT_PROGRESS (11);
  98. if ((hdr->ih_os != IH_OS_LINUX) ||
  99. (hdr->ih_arch != IH_CPU_MIPS) ||
  100. (hdr->ih_type != IH_TYPE_RAMDISK)) {
  101. printf ("No Linux MIPS Ramdisk Image\n");
  102. SHOW_BOOT_PROGRESS (-13);
  103. do_reset (cmdtp, flag, argc, argv);
  104. }
  105. /*
  106. * Now check if we have a multifile image
  107. */
  108. } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
  109. ulong tail = ntohl (len_ptr[0]) % 4;
  110. int i;
  111. SHOW_BOOT_PROGRESS (13);
  112. /* skip kernel length and terminator */
  113. data = (ulong) (&len_ptr[2]);
  114. /* skip any additional image length fields */
  115. for (i = 1; len_ptr[i]; ++i)
  116. data += 4;
  117. /* add kernel length, and align */
  118. data += ntohl (len_ptr[0]);
  119. if (tail) {
  120. data += 4 - tail;
  121. }
  122. len = ntohl (len_ptr[1]);
  123. } else {
  124. /*
  125. * no initrd image
  126. */
  127. SHOW_BOOT_PROGRESS (14);
  128. data = 0;
  129. }
  130. #ifdef DEBUG
  131. if (!data) {
  132. printf ("No initrd\n");
  133. }
  134. #endif
  135. if (data) {
  136. initrd_start = data;
  137. initrd_end = initrd_start + len;
  138. } else {
  139. initrd_start = 0;
  140. initrd_end = 0;
  141. }
  142. SHOW_BOOT_PROGRESS (15);
  143. #ifdef DEBUG
  144. printf ("## Transferring control to Linux (at address %08lx) ...\n",
  145. (ulong) theKernel);
  146. #endif
  147. linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
  148. #ifdef CONFIG_MEMSIZE_IN_BYTES
  149. sprintf (env_buf, "%lu", gd->ram_size);
  150. #ifdef DEBUG
  151. printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
  152. #endif
  153. #else
  154. sprintf (env_buf, "%lu", gd->ram_size >> 20);
  155. #ifdef DEBUG
  156. printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
  157. #endif
  158. #endif /* CONFIG_MEMSIZE_IN_BYTES */
  159. linux_env_set ("memsize", env_buf);
  160. sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
  161. linux_env_set ("initrd_start", env_buf);
  162. sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
  163. linux_env_set ("initrd_size", env_buf);
  164. sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  165. linux_env_set ("flash_start", env_buf);
  166. sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  167. linux_env_set ("flash_size", env_buf);
  168. /* we assume that the kernel is in place */
  169. printf ("\nStarting kernel ...\n\n");
  170. theKernel (linux_argc, linux_argv, linux_env, 0);
  171. }
  172. static void linux_params_init (ulong start, char *line)
  173. {
  174. char *next, *quote, *argp;
  175. linux_argc = 1;
  176. linux_argv = (char **) start;
  177. linux_argv[0] = 0;
  178. argp = (char *) (linux_argv + LINUX_MAX_ARGS);
  179. next = line;
  180. while (line && *line && linux_argc < LINUX_MAX_ARGS) {
  181. quote = strchr (line, '"');
  182. next = strchr (line, ' ');
  183. while (next != NULL && quote != NULL && quote < next) {
  184. /* we found a left quote before the next blank
  185. * now we have to find the matching right quote
  186. */
  187. next = strchr (quote + 1, '"');
  188. if (next != NULL) {
  189. quote = strchr (next + 1, '"');
  190. next = strchr (next + 1, ' ');
  191. }
  192. }
  193. if (next == NULL) {
  194. next = line + strlen (line);
  195. }
  196. linux_argv[linux_argc] = argp;
  197. memcpy (argp, line, next - line);
  198. argp[next - line] = 0;
  199. argp += next - line + 1;
  200. linux_argc++;
  201. if (*next)
  202. next++;
  203. line = next;
  204. }
  205. linux_env = (char **) (((ulong) argp + 15) & ~15);
  206. linux_env[0] = 0;
  207. linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
  208. linux_env_idx = 0;
  209. }
  210. static void linux_env_set (char *env_name, char *env_val)
  211. {
  212. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  213. linux_env[linux_env_idx] = linux_env_p;
  214. strcpy (linux_env_p, env_name);
  215. linux_env_p += strlen (env_name);
  216. strcpy (linux_env_p, "=");
  217. linux_env_p += 1;
  218. strcpy (linux_env_p, env_val);
  219. linux_env_p += strlen (env_val);
  220. linux_env_p++;
  221. linux_env[++linux_env_idx] = 0;
  222. }
  223. }