bootm.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <env.h>
  8. #include <image.h>
  9. #include <fdt_support.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/io.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define LINUX_MAX_ENVS 256
  14. #define LINUX_MAX_ARGS 256
  15. static int linux_argc;
  16. static char **linux_argv;
  17. static char *linux_argp;
  18. static char **linux_env;
  19. static char *linux_env_p;
  20. static int linux_env_idx;
  21. static ulong arch_get_sp(void)
  22. {
  23. ulong ret;
  24. __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
  25. return ret;
  26. }
  27. void arch_lmb_reserve(struct lmb *lmb)
  28. {
  29. ulong sp;
  30. sp = arch_get_sp();
  31. debug("## Current stack ends at 0x%08lx\n", sp);
  32. /* adjust sp by 4K to be safe */
  33. sp -= 4096;
  34. lmb_reserve(lmb, sp, gd->ram_top - sp);
  35. }
  36. static void linux_cmdline_init(void)
  37. {
  38. linux_argc = 1;
  39. linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
  40. linux_argv[0] = 0;
  41. linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
  42. }
  43. static void linux_cmdline_set(const char *value, size_t len)
  44. {
  45. linux_argv[linux_argc] = linux_argp;
  46. memcpy(linux_argp, value, len);
  47. linux_argp[len] = 0;
  48. linux_argp += len + 1;
  49. linux_argc++;
  50. }
  51. static void linux_cmdline_dump(void)
  52. {
  53. int i;
  54. debug("## cmdline argv at 0x%p, argp at 0x%p\n",
  55. linux_argv, linux_argp);
  56. for (i = 1; i < linux_argc; i++)
  57. debug(" arg %03d: %s\n", i, linux_argv[i]);
  58. }
  59. static void linux_cmdline_legacy(bootm_headers_t *images)
  60. {
  61. const char *bootargs, *next, *quote;
  62. linux_cmdline_init();
  63. bootargs = env_get("bootargs");
  64. if (!bootargs)
  65. return;
  66. next = bootargs;
  67. while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
  68. quote = strchr(bootargs, '"');
  69. next = strchr(bootargs, ' ');
  70. while (next && quote && quote < next) {
  71. /*
  72. * we found a left quote before the next blank
  73. * now we have to find the matching right quote
  74. */
  75. next = strchr(quote + 1, '"');
  76. if (next) {
  77. quote = strchr(next + 1, '"');
  78. next = strchr(next + 1, ' ');
  79. }
  80. }
  81. if (!next)
  82. next = bootargs + strlen(bootargs);
  83. linux_cmdline_set(bootargs, next - bootargs);
  84. if (*next)
  85. next++;
  86. bootargs = next;
  87. }
  88. }
  89. static void linux_cmdline_append(bootm_headers_t *images)
  90. {
  91. char buf[24];
  92. ulong mem, rd_start, rd_size;
  93. /* append mem */
  94. mem = gd->ram_size >> 20;
  95. sprintf(buf, "mem=%luM", mem);
  96. linux_cmdline_set(buf, strlen(buf));
  97. /* append rd_start and rd_size */
  98. rd_start = images->initrd_start;
  99. rd_size = images->initrd_end - images->initrd_start;
  100. if (rd_size) {
  101. sprintf(buf, "rd_start=0x%08lX", rd_start);
  102. linux_cmdline_set(buf, strlen(buf));
  103. sprintf(buf, "rd_size=0x%lX", rd_size);
  104. linux_cmdline_set(buf, strlen(buf));
  105. }
  106. }
  107. static void linux_env_init(void)
  108. {
  109. linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
  110. linux_env[0] = 0;
  111. linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
  112. linux_env_idx = 0;
  113. }
  114. static void linux_env_set(const char *env_name, const char *env_val)
  115. {
  116. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  117. linux_env[linux_env_idx] = linux_env_p;
  118. strcpy(linux_env_p, env_name);
  119. linux_env_p += strlen(env_name);
  120. if (CONFIG_IS_ENABLED(MALTA)) {
  121. linux_env_p++;
  122. linux_env[++linux_env_idx] = linux_env_p;
  123. } else {
  124. *linux_env_p++ = '=';
  125. }
  126. strcpy(linux_env_p, env_val);
  127. linux_env_p += strlen(env_val);
  128. linux_env_p++;
  129. linux_env[++linux_env_idx] = 0;
  130. }
  131. }
  132. static void linux_env_legacy(bootm_headers_t *images)
  133. {
  134. char env_buf[12];
  135. const char *cp;
  136. ulong rd_start, rd_size;
  137. if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
  138. sprintf(env_buf, "%lu", (ulong)gd->ram_size);
  139. debug("## Giving linux memsize in bytes, %lu\n",
  140. (ulong)gd->ram_size);
  141. } else {
  142. sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
  143. debug("## Giving linux memsize in MB, %lu\n",
  144. (ulong)(gd->ram_size >> 20));
  145. }
  146. rd_start = UNCACHED_SDRAM(images->initrd_start);
  147. rd_size = images->initrd_end - images->initrd_start;
  148. linux_env_init();
  149. linux_env_set("memsize", env_buf);
  150. sprintf(env_buf, "0x%08lX", rd_start);
  151. linux_env_set("initrd_start", env_buf);
  152. sprintf(env_buf, "0x%lX", rd_size);
  153. linux_env_set("initrd_size", env_buf);
  154. sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  155. linux_env_set("flash_start", env_buf);
  156. sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  157. linux_env_set("flash_size", env_buf);
  158. cp = env_get("ethaddr");
  159. if (cp)
  160. linux_env_set("ethaddr", cp);
  161. cp = env_get("eth1addr");
  162. if (cp)
  163. linux_env_set("eth1addr", cp);
  164. if (CONFIG_IS_ENABLED(MALTA)) {
  165. sprintf(env_buf, "%un8r", gd->baudrate);
  166. linux_env_set("modetty0", env_buf);
  167. }
  168. }
  169. static int boot_reloc_fdt(bootm_headers_t *images)
  170. {
  171. /*
  172. * In case of legacy uImage's, relocation of FDT is already done
  173. * by do_bootm_states() and should not repeated in 'bootm prep'.
  174. */
  175. if (images->state & BOOTM_STATE_FDT) {
  176. debug("## FDT already relocated\n");
  177. return 0;
  178. }
  179. #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
  180. boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
  181. return boot_relocate_fdt(&images->lmb, &images->ft_addr,
  182. &images->ft_len);
  183. #else
  184. return 0;
  185. #endif
  186. }
  187. #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
  188. int arch_fixup_fdt(void *blob)
  189. {
  190. u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
  191. u64 mem_size = gd->ram_size;
  192. return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
  193. }
  194. #endif
  195. static int boot_setup_fdt(bootm_headers_t *images)
  196. {
  197. images->initrd_start = virt_to_phys((void *)images->initrd_start);
  198. images->initrd_end = virt_to_phys((void *)images->initrd_end);
  199. return image_setup_libfdt(images, images->ft_addr, images->ft_len,
  200. &images->lmb);
  201. }
  202. static void boot_prep_linux(bootm_headers_t *images)
  203. {
  204. if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
  205. boot_reloc_fdt(images);
  206. boot_setup_fdt(images);
  207. } else {
  208. if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
  209. linux_cmdline_legacy(images);
  210. if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
  211. linux_cmdline_append(images);
  212. linux_cmdline_dump();
  213. }
  214. if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
  215. linux_env_legacy(images);
  216. }
  217. }
  218. static void boot_jump_linux(bootm_headers_t *images)
  219. {
  220. typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
  221. kernel_entry_t kernel = (kernel_entry_t) images->ep;
  222. ulong linux_extra = 0;
  223. debug("## Transferring control to Linux (at address %p) ...\n", kernel);
  224. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  225. if (CONFIG_IS_ENABLED(MALTA))
  226. linux_extra = gd->ram_size;
  227. #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
  228. bootstage_fdt_add_report();
  229. #endif
  230. #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
  231. bootstage_report();
  232. #endif
  233. if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
  234. trap_restore();
  235. if (images->ft_len)
  236. kernel(-2, (ulong)images->ft_addr, 0, 0);
  237. else
  238. kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
  239. linux_extra);
  240. }
  241. int do_bootm_linux(int flag, int argc, char * const argv[],
  242. bootm_headers_t *images)
  243. {
  244. /* No need for those on MIPS */
  245. if (flag & BOOTM_STATE_OS_BD_T)
  246. return -1;
  247. /*
  248. * Cmdline init has been moved to 'bootm prep' because it has to be
  249. * done after relocation of ramdisk to always pass correct values
  250. * for rd_start and rd_size to Linux kernel.
  251. */
  252. if (flag & BOOTM_STATE_OS_CMDLINE)
  253. return 0;
  254. if (flag & BOOTM_STATE_OS_PREP) {
  255. boot_prep_linux(images);
  256. return 0;
  257. }
  258. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  259. boot_jump_linux(images);
  260. return 0;
  261. }
  262. /* does not return */
  263. return 1;
  264. }