mips_linux.c 6.5 KB

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