zimage.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2002
  4. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * Linux x86 zImage and bzImage loading
  10. *
  11. * based on the procdure described in
  12. * linux/Documentation/i386/boot.txt
  13. */
  14. #include <common.h>
  15. #include <asm/io.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/zimage.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/bootparam.h>
  20. #ifdef CONFIG_SYS_COREBOOT
  21. #include <asm/arch/timestamp.h>
  22. #endif
  23. #include <linux/compiler.h>
  24. /*
  25. * Memory lay-out:
  26. *
  27. * relative to setup_base (which is 0x90000 currently)
  28. *
  29. * 0x0000-0x7FFF Real mode kernel
  30. * 0x8000-0x8FFF Stack and heap
  31. * 0x9000-0x90FF Kernel command line
  32. */
  33. #define DEFAULT_SETUP_BASE 0x90000
  34. #define COMMAND_LINE_OFFSET 0x9000
  35. #define HEAP_END_OFFSET 0x8e00
  36. #define COMMAND_LINE_SIZE 2048
  37. unsigned generic_install_e820_map(unsigned max_entries,
  38. struct e820entry *entries)
  39. {
  40. return 0;
  41. }
  42. unsigned install_e820_map(unsigned max_entries,
  43. struct e820entry *entries)
  44. __attribute__((weak, alias("generic_install_e820_map")));
  45. static void build_command_line(char *command_line, int auto_boot)
  46. {
  47. char *env_command_line;
  48. command_line[0] = '\0';
  49. env_command_line = getenv("bootargs");
  50. /* set console= argument if we use a serial console */
  51. if (!strstr(env_command_line, "console=")) {
  52. if (!strcmp(getenv("stdout"), "serial")) {
  53. /* We seem to use serial console */
  54. sprintf(command_line, "console=ttyS0,%s ",
  55. getenv("baudrate"));
  56. }
  57. }
  58. if (auto_boot)
  59. strcat(command_line, "auto ");
  60. if (env_command_line)
  61. strcat(command_line, env_command_line);
  62. printf("Kernel command line: \"%s\"\n", command_line);
  63. }
  64. static int kernel_magic_ok(struct setup_header *hdr)
  65. {
  66. if (KERNEL_MAGIC != hdr->boot_flag) {
  67. printf("Error: Invalid Boot Flag "
  68. "(found 0x%04x, expected 0x%04x)\n",
  69. hdr->boot_flag, KERNEL_MAGIC);
  70. return 0;
  71. } else {
  72. printf("Valid Boot Flag\n");
  73. return 1;
  74. }
  75. }
  76. static int get_boot_protocol(struct setup_header *hdr)
  77. {
  78. if (hdr->header == KERNEL_V2_MAGIC) {
  79. printf("Magic signature found\n");
  80. return hdr->version;
  81. } else {
  82. /* Very old kernel */
  83. printf("Magic signature not found\n");
  84. return 0x0100;
  85. }
  86. }
  87. struct boot_params *load_zimage(char *image, unsigned long kernel_size,
  88. void **load_address)
  89. {
  90. struct boot_params *setup_base;
  91. int setup_size;
  92. int bootproto;
  93. int big_image;
  94. struct boot_params *params = (struct boot_params *)image;
  95. struct setup_header *hdr = &params->hdr;
  96. /* base address for real-mode segment */
  97. setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
  98. if (!kernel_magic_ok(hdr))
  99. return 0;
  100. /* determine size of setup */
  101. if (0 == hdr->setup_sects) {
  102. printf("Setup Sectors = 0 (defaulting to 4)\n");
  103. setup_size = 5 * 512;
  104. } else {
  105. setup_size = (hdr->setup_sects + 1) * 512;
  106. }
  107. printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
  108. if (setup_size > SETUP_MAX_SIZE)
  109. printf("Error: Setup is too large (%d bytes)\n", setup_size);
  110. /* determine boot protocol version */
  111. bootproto = get_boot_protocol(hdr);
  112. printf("Using boot protocol version %x.%02x\n",
  113. (bootproto & 0xff00) >> 8, bootproto & 0xff);
  114. if (bootproto >= 0x0200) {
  115. if (hdr->setup_sects >= 15) {
  116. printf("Linux kernel version %s\n",
  117. (char *)params +
  118. hdr->kernel_version + 0x200);
  119. } else {
  120. printf("Setup Sectors < 15 - "
  121. "Cannot print kernel version.\n");
  122. }
  123. }
  124. /* Determine image type */
  125. big_image = (bootproto >= 0x0200) &&
  126. (hdr->loadflags & BIG_KERNEL_FLAG);
  127. /* Determine load address */
  128. if (big_image)
  129. *load_address = (void *)BZIMAGE_LOAD_ADDR;
  130. else
  131. *load_address = (void *)ZIMAGE_LOAD_ADDR;
  132. printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
  133. memset(setup_base, 0, sizeof(*setup_base));
  134. setup_base->hdr = params->hdr;
  135. if (bootproto >= 0x0204)
  136. kernel_size = hdr->syssize * 16;
  137. else
  138. kernel_size -= setup_size;
  139. if (bootproto == 0x0100) {
  140. /*
  141. * A very old kernel MUST have its real-mode code
  142. * loaded at 0x90000
  143. */
  144. if ((u32)setup_base != 0x90000) {
  145. /* Copy the real-mode kernel */
  146. memmove((void *)0x90000, setup_base, setup_size);
  147. /* Copy the command line */
  148. memmove((void *)0x99000,
  149. (u8 *)setup_base + COMMAND_LINE_OFFSET,
  150. COMMAND_LINE_SIZE);
  151. /* Relocated */
  152. setup_base = (struct boot_params *)0x90000;
  153. }
  154. /* It is recommended to clear memory up to the 32K mark */
  155. memset((u8 *)0x90000 + setup_size, 0,
  156. SETUP_MAX_SIZE - setup_size);
  157. }
  158. if (big_image) {
  159. if (kernel_size > BZIMAGE_MAX_SIZE) {
  160. printf("Error: bzImage kernel too big! "
  161. "(size: %ld, max: %d)\n",
  162. kernel_size, BZIMAGE_MAX_SIZE);
  163. return 0;
  164. }
  165. } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
  166. printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
  167. kernel_size, ZIMAGE_MAX_SIZE);
  168. return 0;
  169. }
  170. printf("Loading %s at address %p (%ld bytes)\n",
  171. big_image ? "bzImage" : "zImage", *load_address, kernel_size);
  172. memmove(*load_address, image + setup_size, kernel_size);
  173. return setup_base;
  174. }
  175. int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
  176. unsigned long initrd_addr, unsigned long initrd_size)
  177. {
  178. struct setup_header *hdr = &setup_base->hdr;
  179. int bootproto = get_boot_protocol(hdr);
  180. setup_base->e820_entries = install_e820_map(
  181. ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
  182. if (bootproto == 0x0100) {
  183. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  184. setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
  185. }
  186. if (bootproto >= 0x0200) {
  187. hdr->type_of_loader = 8;
  188. if (initrd_addr) {
  189. printf("Initial RAM disk at linear address "
  190. "0x%08lx, size %ld bytes\n",
  191. initrd_addr, initrd_size);
  192. hdr->ramdisk_image = initrd_addr;
  193. hdr->ramdisk_size = initrd_size;
  194. }
  195. }
  196. if (bootproto >= 0x0201) {
  197. hdr->heap_end_ptr = HEAP_END_OFFSET;
  198. hdr->loadflags |= HEAP_FLAG;
  199. }
  200. if (bootproto >= 0x0202) {
  201. hdr->cmd_line_ptr = (uintptr_t)cmd_line;
  202. } else if (bootproto >= 0x0200) {
  203. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  204. setup_base->screen_info.cl_offset =
  205. (uintptr_t)cmd_line - (uintptr_t)setup_base;
  206. hdr->setup_move_size = 0x9100;
  207. }
  208. /* build command line at COMMAND_LINE_OFFSET */
  209. build_command_line(cmd_line, auto_boot);
  210. return 0;
  211. }
  212. /*
  213. * Implement a weak default function for boards that optionally
  214. * need to clean up the system before jumping to the kernel.
  215. */
  216. __weak void board_final_cleanup(void)
  217. {
  218. }
  219. void boot_zimage(void *setup_base, void *load_address)
  220. {
  221. debug("## Transferring control to Linux (at address %08x) ...\n",
  222. (u32)setup_base);
  223. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  224. #ifdef CONFIG_BOOTSTAGE_REPORT
  225. bootstage_report();
  226. #endif
  227. board_final_cleanup();
  228. printf("\nStarting kernel ...\n\n");
  229. #ifdef CONFIG_SYS_COREBOOT
  230. timestamp_add_now(TS_U_BOOT_START_KERNEL);
  231. #endif
  232. /*
  233. * Set %ebx, %ebp, and %edi to 0, %esi to point to the boot_params
  234. * structure, and then jump to the kernel. We assume that %cs is
  235. * 0x10, 4GB flat, and read/execute, and the data segments are 0x18,
  236. * 4GB flat, and read/write. U-boot is setting them up that way for
  237. * itself in arch/i386/cpu/cpu.c.
  238. */
  239. __asm__ __volatile__ (
  240. "movl $0, %%ebp\n"
  241. "cli\n"
  242. "jmp *%[kernel_entry]\n"
  243. :: [kernel_entry]"a"(load_address),
  244. [boot_params] "S"(setup_base),
  245. "b"(0), "D"(0)
  246. : "%ebp"
  247. );
  248. }
  249. void setup_pcat_compatibility(void)
  250. __attribute__((weak, alias("__setup_pcat_compatibility")));
  251. void __setup_pcat_compatibility(void)
  252. {
  253. }
  254. int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  255. {
  256. struct boot_params *base_ptr;
  257. void *bzImage_addr = NULL;
  258. void *load_address;
  259. char *s;
  260. ulong bzImage_size = 0;
  261. ulong initrd_addr = 0;
  262. ulong initrd_size = 0;
  263. disable_interrupts();
  264. /* Setup board for maximum PC/AT Compatibility */
  265. setup_pcat_compatibility();
  266. if (argc >= 2) {
  267. /* argv[1] holds the address of the bzImage */
  268. s = argv[1];
  269. } else {
  270. s = getenv("fileaddr");
  271. }
  272. if (s)
  273. bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
  274. if (argc >= 3) {
  275. /* argv[2] holds the size of the bzImage */
  276. bzImage_size = simple_strtoul(argv[2], NULL, 16);
  277. }
  278. if (argc >= 4)
  279. initrd_addr = simple_strtoul(argv[3], NULL, 16);
  280. if (argc >= 5)
  281. initrd_size = simple_strtoul(argv[4], NULL, 16);
  282. /* Lets look for */
  283. base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
  284. if (!base_ptr) {
  285. printf("## Kernel loading failed ...\n");
  286. return -1;
  287. }
  288. if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
  289. 0, initrd_addr, initrd_size)) {
  290. printf("Setting up boot parameters failed ...\n");
  291. return -1;
  292. }
  293. /* we assume that the kernel is in place */
  294. boot_zimage(base_ptr, load_address);
  295. /* does not return */
  296. return -1;
  297. }
  298. U_BOOT_CMD(
  299. zboot, 5, 0, do_zboot,
  300. "Boot bzImage",
  301. "[addr] [size] [initrd addr] [initrd size]\n"
  302. " addr - The optional starting address of the bzimage.\n"
  303. " If not set it defaults to the environment\n"
  304. " variable \"fileaddr\".\n"
  305. " size - The optional size of the bzimage. Defaults to\n"
  306. " zero.\n"
  307. " initrd addr - The address of the initrd image to use, if any.\n"
  308. " initrd size - The size of the initrd image to use, if any.\n"
  309. );