bootm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <command.h>
  9. #include <image.h>
  10. #include <u-boot/zlib.h>
  11. #include <asm/byteorder.h>
  12. #include <asm/addrspace.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_env;
  19. static char *linux_env_p;
  20. static int linux_env_idx;
  21. static void linux_params_init(ulong start, char *commandline);
  22. static void linux_env_set(char *env_name, char *env_val);
  23. static void boot_prep_linux(bootm_headers_t *images)
  24. {
  25. char *commandline = getenv("bootargs");
  26. char env_buf[12];
  27. char *cp;
  28. linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline);
  29. #ifdef CONFIG_MEMSIZE_IN_BYTES
  30. sprintf(env_buf, "%lu", (ulong)gd->ram_size);
  31. debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
  32. #else
  33. sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
  34. debug("## Giving linux memsize in MB, %lu\n",
  35. (ulong)(gd->ram_size >> 20));
  36. #endif /* CONFIG_MEMSIZE_IN_BYTES */
  37. linux_env_set("memsize", env_buf);
  38. sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
  39. linux_env_set("initrd_start", env_buf);
  40. sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
  41. linux_env_set("initrd_size", env_buf);
  42. sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
  43. linux_env_set("flash_start", env_buf);
  44. sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
  45. linux_env_set("flash_size", env_buf);
  46. cp = getenv("ethaddr");
  47. if (cp)
  48. linux_env_set("ethaddr", cp);
  49. cp = getenv("eth1addr");
  50. if (cp)
  51. linux_env_set("eth1addr", cp);
  52. }
  53. static void boot_jump_linux(bootm_headers_t *images)
  54. {
  55. typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
  56. kernel_entry_t kernel = (kernel_entry_t) images->ep;
  57. debug("## Transferring control to Linux (at address %p) ...\n", kernel);
  58. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  59. /* we assume that the kernel is in place */
  60. printf("\nStarting kernel ...\n\n");
  61. kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
  62. }
  63. int do_bootm_linux(int flag, int argc, char * const argv[],
  64. bootm_headers_t *images)
  65. {
  66. /* No need for those on MIPS */
  67. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  68. return -1;
  69. if (flag & BOOTM_STATE_OS_PREP) {
  70. boot_prep_linux(images);
  71. return 0;
  72. }
  73. if (flag & BOOTM_STATE_OS_GO) {
  74. boot_jump_linux(images);
  75. return 0;
  76. }
  77. boot_prep_linux(images);
  78. boot_jump_linux(images);
  79. /* does not return */
  80. return 1;
  81. }
  82. static void linux_params_init(ulong start, char *line)
  83. {
  84. char *next, *quote, *argp;
  85. linux_argc = 1;
  86. linux_argv = (char **)start;
  87. linux_argv[0] = 0;
  88. argp = (char *)(linux_argv + LINUX_MAX_ARGS);
  89. next = line;
  90. while (line && *line && linux_argc < LINUX_MAX_ARGS) {
  91. quote = strchr(line, '"');
  92. next = strchr(line, ' ');
  93. while (next && quote && quote < next) {
  94. /*
  95. * we found a left quote before the next blank
  96. * now we have to find the matching right quote
  97. */
  98. next = strchr(quote + 1, '"');
  99. if (next) {
  100. quote = strchr(next + 1, '"');
  101. next = strchr(next + 1, ' ');
  102. }
  103. }
  104. if (!next)
  105. next = line + strlen(line);
  106. linux_argv[linux_argc] = argp;
  107. memcpy(argp, line, next - line);
  108. argp[next - line] = 0;
  109. argp += next - line + 1;
  110. linux_argc++;
  111. if (*next)
  112. next++;
  113. line = next;
  114. }
  115. linux_env = (char **)(((ulong) argp + 15) & ~15);
  116. linux_env[0] = 0;
  117. linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
  118. linux_env_idx = 0;
  119. }
  120. static void linux_env_set(char *env_name, char *env_val)
  121. {
  122. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  123. linux_env[linux_env_idx] = linux_env_p;
  124. strcpy(linux_env_p, env_name);
  125. linux_env_p += strlen(env_name);
  126. strcpy(linux_env_p, "=");
  127. linux_env_p += 1;
  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. }