bootm.c 7.6 KB

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