mips_linux.c 6.3 KB

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