bootm.c 7.4 KB

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