bootm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  33. static int linux_argc;
  34. static char ** linux_argv;
  35. static char ** linux_env;
  36. static char * linux_env_p;
  37. static int linux_env_idx;
  38. static void linux_params_init (ulong start, char * commandline);
  39. static void linux_env_set (char * env_name, char * env_val);
  40. void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
  41. image_header_t *hdr, int verify)
  42. {
  43. ulong rd_data, rd_len;
  44. ulong initrd_start, initrd_end;
  45. image_header_t *rd_hdr;
  46. void (*theKernel) (int, char **, char **, int *);
  47. char *commandline = getenv ("bootargs");
  48. char env_buf[12];
  49. theKernel =
  50. (void (*)(int, char **, char **, int *))image_get_ep (hdr);
  51. /*
  52. * Check if there is an initrd image
  53. */
  54. if (argc >= 3) {
  55. show_boot_progress (9);
  56. rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16);
  57. printf ("## Loading Ramdisk Image at %08lx ...\n", rd_hdr);
  58. if (!image_check_magic (rd_hdr)) {
  59. printf ("Bad Magic Number\n");
  60. show_boot_progress (-10);
  61. do_reset (cmdtp, flag, argc, argv);
  62. }
  63. if (!image_check_hcrc (rd_hdr)) {
  64. printf ("Bad Header Checksum\n");
  65. show_boot_progress (-11);
  66. do_reset (cmdtp, flag, argc, argv);
  67. }
  68. show_boot_progress (10);
  69. print_image_hdr (rd_hdr);
  70. rd_data = image_get_data (rd_hdr);
  71. rd_len = image_get_data_size (rd_hdr);
  72. if (verify) {
  73. printf (" Verifying Checksum ... ");
  74. if (!image_check_dcrc (rd_hdr)) {
  75. printf ("Bad Data CRC\n");
  76. show_boot_progress (-12);
  77. do_reset (cmdtp, flag, argc, argv);
  78. }
  79. printf ("OK\n");
  80. }
  81. show_boot_progress (11);
  82. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  83. !image_check_arch (rd_hdr, IH_ARCH_MIPS) ||
  84. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  85. printf ("No Linux MIPS Ramdisk Image\n");
  86. show_boot_progress (-13);
  87. do_reset (cmdtp, flag, argc, argv);
  88. }
  89. /*
  90. * Now check if we have a multifile image
  91. */
  92. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  93. /*
  94. * Get second entry data start address and len
  95. */
  96. show_boot_progress (13);
  97. image_multi_getimg (hdr, 1, &rd_data, &rd_len);
  98. } else {
  99. /*
  100. * no initrd image
  101. */
  102. show_boot_progress (14);
  103. rd_data = rd_len = 0;
  104. }
  105. #ifdef DEBUG
  106. if (!rd_data) {
  107. printf ("No initrd\n");
  108. }
  109. #endif
  110. if (rd_data) {
  111. initrd_start = rd_data;
  112. initrd_end = initrd_start + rd_len;
  113. } else {
  114. initrd_start = 0;
  115. initrd_end = 0;
  116. }
  117. show_boot_progress (15);
  118. #ifdef DEBUG
  119. printf ("## Transferring control to Linux (at address %08lx) ...\n",
  120. (ulong) theKernel);
  121. #endif
  122. linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
  123. #ifdef CONFIG_MEMSIZE_IN_BYTES
  124. sprintf (env_buf, "%lu", gd->ram_size);
  125. #ifdef DEBUG
  126. printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
  127. #endif
  128. #else
  129. sprintf (env_buf, "%lu", gd->ram_size >> 20);
  130. #ifdef DEBUG
  131. printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
  132. #endif
  133. #endif /* CONFIG_MEMSIZE_IN_BYTES */
  134. linux_env_set ("memsize", env_buf);
  135. sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
  136. linux_env_set ("initrd_start", env_buf);
  137. sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
  138. linux_env_set ("initrd_size", env_buf);
  139. sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  140. linux_env_set ("flash_start", env_buf);
  141. sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  142. linux_env_set ("flash_size", env_buf);
  143. /* we assume that the kernel is in place */
  144. printf ("\nStarting kernel ...\n\n");
  145. theKernel (linux_argc, linux_argv, linux_env, 0);
  146. }
  147. static void linux_params_init (ulong start, char *line)
  148. {
  149. char *next, *quote, *argp;
  150. linux_argc = 1;
  151. linux_argv = (char **) start;
  152. linux_argv[0] = 0;
  153. argp = (char *) (linux_argv + LINUX_MAX_ARGS);
  154. next = line;
  155. while (line && *line && linux_argc < LINUX_MAX_ARGS) {
  156. quote = strchr (line, '"');
  157. next = strchr (line, ' ');
  158. while (next != NULL && quote != NULL && quote < next) {
  159. /* we found a left quote before the next blank
  160. * now we have to find the matching right quote
  161. */
  162. next = strchr (quote + 1, '"');
  163. if (next != NULL) {
  164. quote = strchr (next + 1, '"');
  165. next = strchr (next + 1, ' ');
  166. }
  167. }
  168. if (next == NULL) {
  169. next = line + strlen (line);
  170. }
  171. linux_argv[linux_argc] = argp;
  172. memcpy (argp, line, next - line);
  173. argp[next - line] = 0;
  174. argp += next - line + 1;
  175. linux_argc++;
  176. if (*next)
  177. next++;
  178. line = next;
  179. }
  180. linux_env = (char **) (((ulong) argp + 15) & ~15);
  181. linux_env[0] = 0;
  182. linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
  183. linux_env_idx = 0;
  184. }
  185. static void linux_env_set (char *env_name, char *env_val)
  186. {
  187. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  188. linux_env[linux_env_idx] = linux_env_p;
  189. strcpy (linux_env_p, env_name);
  190. linux_env_p += strlen (env_name);
  191. strcpy (linux_env_p, "=");
  192. linux_env_p += 1;
  193. strcpy (linux_env_p, env_val);
  194. linux_env_p += strlen (env_val);
  195. linux_env_p++;
  196. linux_env[++linux_env_idx] = 0;
  197. }
  198. }