bootm.c 7.4 KB

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