mips_linux.c 6.5 KB

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