bootm.c 7.4 KB

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