bootm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. void (*theKernel) (int, char **, char **, int *);
  56. /* find kernel entry point */
  57. theKernel = (void (*)(int, char **, char **, int *))images->ep;
  58. debug("## Transferring control to Linux (at address %08lx) ...\n",
  59. (ulong) theKernel);
  60. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  61. /* we assume that the kernel is in place */
  62. printf("\nStarting kernel ...\n\n");
  63. theKernel(linux_argc, linux_argv, linux_env, 0);
  64. }
  65. int do_bootm_linux(int flag, int argc, char * const argv[],
  66. bootm_headers_t *images)
  67. {
  68. /* No need for those on MIPS */
  69. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  70. return -1;
  71. if (flag & BOOTM_STATE_OS_PREP) {
  72. boot_prep_linux(images);
  73. return 0;
  74. }
  75. if (flag & BOOTM_STATE_OS_GO) {
  76. boot_jump_linux(images);
  77. return 0;
  78. }
  79. boot_prep_linux(images);
  80. boot_jump_linux(images);
  81. /* does not return */
  82. return 1;
  83. }
  84. static void linux_params_init(ulong start, char *line)
  85. {
  86. char *next, *quote, *argp;
  87. linux_argc = 1;
  88. linux_argv = (char **) start;
  89. linux_argv[0] = 0;
  90. argp = (char *) (linux_argv + LINUX_MAX_ARGS);
  91. next = line;
  92. while (line && *line && linux_argc < LINUX_MAX_ARGS) {
  93. quote = strchr(line, '"');
  94. next = strchr(line, ' ');
  95. while (next && quote && quote < next) {
  96. /* we found a left quote before the next blank
  97. * now we have to find the matching right quote
  98. */
  99. next = strchr(quote + 1, '"');
  100. if (next) {
  101. quote = strchr(next + 1, '"');
  102. next = strchr(next + 1, ' ');
  103. }
  104. }
  105. if (!next)
  106. next = line + strlen(line);
  107. linux_argv[linux_argc] = argp;
  108. memcpy(argp, line, next - line);
  109. argp[next - line] = 0;
  110. argp += next - line + 1;
  111. linux_argc++;
  112. if (*next)
  113. next++;
  114. line = next;
  115. }
  116. linux_env = (char **) (((ulong) argp + 15) & ~15);
  117. linux_env[0] = 0;
  118. linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
  119. linux_env_idx = 0;
  120. }
  121. static void linux_env_set(char *env_name, char *env_val)
  122. {
  123. if (linux_env_idx < LINUX_MAX_ENVS - 1) {
  124. linux_env[linux_env_idx] = linux_env_p;
  125. strcpy(linux_env_p, env_name);
  126. linux_env_p += strlen(env_name);
  127. strcpy(linux_env_p, "=");
  128. linux_env_p += 1;
  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. }