bootm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. static int linux_argc;
  33. static char ** linux_argv;
  34. static char ** linux_env;
  35. static char * linux_env_p;
  36. static int linux_env_idx;
  37. static void linux_params_init (ulong start, char * commandline);
  38. static void linux_env_set (char * env_name, char * env_val);
  39. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  40. void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
  41. bootm_headers_t *images)
  42. {
  43. ulong initrd_start, initrd_end;
  44. ulong ep = 0;
  45. void (*theKernel) (int, char **, char **, int *);
  46. char *commandline = getenv ("bootargs");
  47. char env_buf[12];
  48. int ret;
  49. /* find kernel entry point */
  50. if (images->legacy_hdr_valid) {
  51. ep = image_get_ep (&images->legacy_hdr_os_copy);
  52. #if defined(CONFIG_FIT)
  53. } else if (images->fit_uname_os) {
  54. ret = fit_image_get_entry (images->fit_hdr_os,
  55. images->fit_noffset_os, &ep);
  56. if (ret) {
  57. puts ("Can't get entry point property!\n");
  58. goto error;
  59. }
  60. #endif
  61. } else {
  62. puts ("Could not find kernel entry point!\n");
  63. goto error;
  64. }
  65. theKernel = (void (*)(int, char **, char **, int *))ep;
  66. ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_MIPS,
  67. &initrd_start, &initrd_end);
  68. if (ret)
  69. goto error;
  70. show_boot_progress (15);
  71. #ifdef DEBUG
  72. printf ("## Transferring control to Linux (at address %08lx) ...\n",
  73. (ulong) theKernel);
  74. #endif
  75. linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
  76. #ifdef CONFIG_MEMSIZE_IN_BYTES
  77. sprintf (env_buf, "%lu", gd->ram_size);
  78. #ifdef DEBUG
  79. printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
  80. #endif
  81. #else
  82. sprintf (env_buf, "%lu", gd->ram_size >> 20);
  83. #ifdef DEBUG
  84. printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
  85. #endif
  86. #endif /* CONFIG_MEMSIZE_IN_BYTES */
  87. linux_env_set ("memsize", env_buf);
  88. sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
  89. linux_env_set ("initrd_start", env_buf);
  90. sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
  91. linux_env_set ("initrd_size", env_buf);
  92. sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  93. linux_env_set ("flash_start", env_buf);
  94. sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  95. linux_env_set ("flash_size", env_buf);
  96. if (!images->autostart)
  97. return ;
  98. /* we assume that the kernel is in place */
  99. printf ("\nStarting kernel ...\n\n");
  100. theKernel (linux_argc, linux_argv, linux_env, 0);
  101. /* does not return */
  102. return;
  103. error:
  104. if (images->autostart)
  105. do_reset (cmdtp, flag, argc, argv);
  106. return;
  107. }
  108. static void linux_params_init (ulong start, char *line)
  109. {
  110. char *next, *quote, *argp;
  111. linux_argc = 1;
  112. linux_argv = (char **) start;
  113. linux_argv[0] = 0;
  114. argp = (char *) (linux_argv + LINUX_MAX_ARGS);
  115. next = line;
  116. while (line && *line && linux_argc < LINUX_MAX_ARGS) {
  117. quote = strchr (line, '"');
  118. next = strchr (line, ' ');
  119. while (next != NULL && quote != NULL && quote < next) {
  120. /* we found a left quote before the next blank
  121. * now we have to find the matching right quote
  122. */
  123. next = strchr (quote + 1, '"');
  124. if (next != NULL) {
  125. quote = strchr (next + 1, '"');
  126. next = strchr (next + 1, ' ');
  127. }
  128. }
  129. if (next == NULL) {
  130. next = line + strlen (line);
  131. }
  132. linux_argv[linux_argc] = argp;
  133. memcpy (argp, line, next - line);
  134. argp[next - line] = 0;
  135. argp += next - line + 1;
  136. linux_argc++;
  137. if (*next)
  138. next++;
  139. line = next;
  140. }
  141. linux_env = (char **) (((ulong) argp + 15) & ~15);
  142. linux_env[0] = 0;
  143. linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
  144. linux_env_idx = 0;
  145. }
  146. static void linux_env_set (char *env_name, char *env_val)
  147. {
  148. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  149. linux_env[linux_env_idx] = linux_env_p;
  150. strcpy (linux_env_p, env_name);
  151. linux_env_p += strlen (env_name);
  152. strcpy (linux_env_p, "=");
  153. linux_env_p += 1;
  154. strcpy (linux_env_p, env_val);
  155. linux_env_p += strlen (env_val);
  156. linux_env_p++;
  157. linux_env[++linux_env_idx] = 0;
  158. }
  159. }