zimage.c 8.7 KB

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