bootm.c 7.4 KB

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