bootm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <image.h>
  10. #include <u-boot/zlib.h>
  11. #include <asm/byteorder.h>
  12. #include <asm/addrspace.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #define LINUX_MAX_ENVS 256
  15. #define LINUX_MAX_ARGS 256
  16. #if defined(CONFIG_QEMU_MALTA)
  17. #define mips_boot_qemu_malta 1
  18. #else
  19. #define mips_boot_qemu_malta 0
  20. #endif
  21. static int linux_argc;
  22. static char **linux_argv;
  23. static char *linux_argp;
  24. static char **linux_env;
  25. static char *linux_env_p;
  26. static int linux_env_idx;
  27. static ulong arch_get_sp(void)
  28. {
  29. ulong ret;
  30. __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
  31. return ret;
  32. }
  33. void arch_lmb_reserve(struct lmb *lmb)
  34. {
  35. ulong sp;
  36. sp = arch_get_sp();
  37. debug("## Current stack ends at 0x%08lx\n", sp);
  38. /* adjust sp by 4K to be safe */
  39. sp -= 4096;
  40. lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
  41. }
  42. static void linux_cmdline_init(void)
  43. {
  44. linux_argc = 1;
  45. linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
  46. linux_argv[0] = 0;
  47. linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
  48. }
  49. static void linux_cmdline_set(const char *value, size_t len)
  50. {
  51. linux_argv[linux_argc] = linux_argp;
  52. memcpy(linux_argp, value, len);
  53. linux_argp[len] = 0;
  54. linux_argp += len + 1;
  55. linux_argc++;
  56. }
  57. static void linux_cmdline_dump(void)
  58. {
  59. int i;
  60. debug("## cmdline argv at 0x%p, argp at 0x%p\n",
  61. linux_argv, linux_argp);
  62. for (i = 1; i < linux_argc; i++)
  63. debug(" arg %03d: %s\n", i, linux_argv[i]);
  64. }
  65. static void boot_cmdline_linux(bootm_headers_t *images)
  66. {
  67. const char *bootargs, *next, *quote;
  68. linux_cmdline_init();
  69. bootargs = getenv("bootargs");
  70. if (!bootargs)
  71. return;
  72. next = bootargs;
  73. while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
  74. quote = strchr(bootargs, '"');
  75. next = strchr(bootargs, ' ');
  76. while (next && quote && quote < next) {
  77. /*
  78. * we found a left quote before the next blank
  79. * now we have to find the matching right quote
  80. */
  81. next = strchr(quote + 1, '"');
  82. if (next) {
  83. quote = strchr(next + 1, '"');
  84. next = strchr(next + 1, ' ');
  85. }
  86. }
  87. if (!next)
  88. next = bootargs + strlen(bootargs);
  89. linux_cmdline_set(bootargs, next - bootargs);
  90. if (*next)
  91. next++;
  92. bootargs = next;
  93. }
  94. linux_cmdline_dump();
  95. }
  96. static void linux_env_init(void)
  97. {
  98. linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
  99. linux_env[0] = 0;
  100. linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
  101. linux_env_idx = 0;
  102. }
  103. static void linux_env_set(const char *env_name, const char *env_val)
  104. {
  105. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  106. linux_env[linux_env_idx] = linux_env_p;
  107. strcpy(linux_env_p, env_name);
  108. linux_env_p += strlen(env_name);
  109. if (mips_boot_qemu_malta) {
  110. linux_env_p++;
  111. linux_env[++linux_env_idx] = linux_env_p;
  112. } else {
  113. *linux_env_p++ = '=';
  114. }
  115. strcpy(linux_env_p, env_val);
  116. linux_env_p += strlen(env_val);
  117. linux_env_p++;
  118. linux_env[++linux_env_idx] = 0;
  119. }
  120. }
  121. static void boot_prep_linux(bootm_headers_t *images)
  122. {
  123. char env_buf[12];
  124. const char *cp;
  125. ulong rd_start, rd_size;
  126. #ifdef CONFIG_MEMSIZE_IN_BYTES
  127. sprintf(env_buf, "%lu", (ulong)gd->ram_size);
  128. debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
  129. #else
  130. sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
  131. debug("## Giving linux memsize in MB, %lu\n",
  132. (ulong)(gd->ram_size >> 20));
  133. #endif /* CONFIG_MEMSIZE_IN_BYTES */
  134. rd_start = UNCACHED_SDRAM(images->initrd_start);
  135. rd_size = images->initrd_end - images->initrd_start;
  136. linux_env_init();
  137. linux_env_set("memsize", env_buf);
  138. sprintf(env_buf, "0x%08lX", rd_start);
  139. linux_env_set("initrd_start", env_buf);
  140. sprintf(env_buf, "0x%lX", rd_size);
  141. linux_env_set("initrd_size", env_buf);
  142. sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  143. linux_env_set("flash_start", env_buf);
  144. sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  145. linux_env_set("flash_size", env_buf);
  146. cp = getenv("ethaddr");
  147. if (cp)
  148. linux_env_set("ethaddr", cp);
  149. cp = getenv("eth1addr");
  150. if (cp)
  151. linux_env_set("eth1addr", cp);
  152. if (mips_boot_qemu_malta)
  153. linux_env_set("modetty0", "38400n8r");
  154. }
  155. static void boot_jump_linux(bootm_headers_t *images)
  156. {
  157. typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
  158. kernel_entry_t kernel = (kernel_entry_t) images->ep;
  159. ulong linux_extra = 0;
  160. debug("## Transferring control to Linux (at address %p) ...\n", kernel);
  161. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  162. if (mips_boot_qemu_malta)
  163. linux_extra = gd->ram_size;
  164. /* we assume that the kernel is in place */
  165. printf("\nStarting kernel ...\n\n");
  166. kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
  167. }
  168. int do_bootm_linux(int flag, int argc, char * const argv[],
  169. bootm_headers_t *images)
  170. {
  171. /* No need for those on MIPS */
  172. if (flag & BOOTM_STATE_OS_BD_T)
  173. return -1;
  174. if (flag & BOOTM_STATE_OS_CMDLINE) {
  175. boot_cmdline_linux(images);
  176. return 0;
  177. }
  178. if (flag & BOOTM_STATE_OS_PREP) {
  179. boot_prep_linux(images);
  180. return 0;
  181. }
  182. if (flag & BOOTM_STATE_OS_GO) {
  183. boot_jump_linux(images);
  184. return 0;
  185. }
  186. boot_cmdline_linux(images);
  187. boot_prep_linux(images);
  188. boot_jump_linux(images);
  189. /* does not return */
  190. return 1;
  191. }