zimage.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. * (C) Copyright 2002
  5. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  6. */
  7. /*
  8. * Linux x86 zImage and bzImage loading
  9. *
  10. * based on the procdure described in
  11. * linux/Documentation/i386/boot.txt
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <env.h>
  16. #include <irq_func.h>
  17. #include <malloc.h>
  18. #include <acpi/acpi_table.h>
  19. #include <asm/io.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/zimage.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/bootm.h>
  24. #include <asm/bootparam.h>
  25. #ifdef CONFIG_SYS_COREBOOT
  26. #include <asm/arch/timestamp.h>
  27. #endif
  28. #include <linux/compiler.h>
  29. #include <linux/libfdt.h>
  30. /*
  31. * Memory lay-out:
  32. *
  33. * relative to setup_base (which is 0x90000 currently)
  34. *
  35. * 0x0000-0x7FFF Real mode kernel
  36. * 0x8000-0x8FFF Stack and heap
  37. * 0x9000-0x90FF Kernel command line
  38. */
  39. #define DEFAULT_SETUP_BASE 0x90000
  40. #define COMMAND_LINE_OFFSET 0x9000
  41. #define HEAP_END_OFFSET 0x8e00
  42. #define COMMAND_LINE_SIZE 2048
  43. static void build_command_line(char *command_line, int auto_boot)
  44. {
  45. char *env_command_line;
  46. command_line[0] = '\0';
  47. env_command_line = env_get("bootargs");
  48. /* set console= argument if we use a serial console */
  49. if (!strstr(env_command_line, "console=")) {
  50. if (!strcmp(env_get("stdout"), "serial")) {
  51. /* We seem to use serial console */
  52. sprintf(command_line, "console=ttyS0,%s ",
  53. env_get("baudrate"));
  54. }
  55. }
  56. if (auto_boot)
  57. strcat(command_line, "auto ");
  58. if (env_command_line)
  59. strcat(command_line, env_command_line);
  60. printf("Kernel command line: \"%s\"\n", command_line);
  61. }
  62. static int kernel_magic_ok(struct setup_header *hdr)
  63. {
  64. if (KERNEL_MAGIC != hdr->boot_flag) {
  65. printf("Error: Invalid Boot Flag "
  66. "(found 0x%04x, expected 0x%04x)\n",
  67. hdr->boot_flag, KERNEL_MAGIC);
  68. return 0;
  69. } else {
  70. printf("Valid Boot Flag\n");
  71. return 1;
  72. }
  73. }
  74. static int get_boot_protocol(struct setup_header *hdr)
  75. {
  76. if (hdr->header == KERNEL_V2_MAGIC) {
  77. printf("Magic signature found\n");
  78. return hdr->version;
  79. } else {
  80. /* Very old kernel */
  81. printf("Magic signature not found\n");
  82. return 0x0100;
  83. }
  84. }
  85. static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
  86. {
  87. int bootproto = get_boot_protocol(hdr);
  88. struct setup_data *sd;
  89. int size;
  90. if (bootproto < 0x0209)
  91. return -ENOTSUPP;
  92. if (!fdt_blob)
  93. return 0;
  94. size = fdt_totalsize(fdt_blob);
  95. if (size < 0)
  96. return -EINVAL;
  97. size += sizeof(struct setup_data);
  98. sd = (struct setup_data *)malloc(size);
  99. if (!sd) {
  100. printf("Not enough memory for DTB setup data\n");
  101. return -ENOMEM;
  102. }
  103. sd->next = hdr->setup_data;
  104. sd->type = SETUP_DTB;
  105. sd->len = fdt_totalsize(fdt_blob);
  106. memcpy(sd->data, fdt_blob, sd->len);
  107. hdr->setup_data = (unsigned long)sd;
  108. return 0;
  109. }
  110. struct boot_params *load_zimage(char *image, unsigned long kernel_size,
  111. ulong *load_addressp)
  112. {
  113. struct boot_params *setup_base;
  114. int setup_size;
  115. int bootproto;
  116. int big_image;
  117. struct boot_params *params = (struct boot_params *)image;
  118. struct setup_header *hdr = &params->hdr;
  119. /* base address for real-mode segment */
  120. setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
  121. if (!kernel_magic_ok(hdr))
  122. return 0;
  123. /* determine size of setup */
  124. if (0 == hdr->setup_sects) {
  125. printf("Setup Sectors = 0 (defaulting to 4)\n");
  126. setup_size = 5 * 512;
  127. } else {
  128. setup_size = (hdr->setup_sects + 1) * 512;
  129. }
  130. printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
  131. if (setup_size > SETUP_MAX_SIZE)
  132. printf("Error: Setup is too large (%d bytes)\n", setup_size);
  133. /* determine boot protocol version */
  134. bootproto = get_boot_protocol(hdr);
  135. printf("Using boot protocol version %x.%02x\n",
  136. (bootproto & 0xff00) >> 8, bootproto & 0xff);
  137. if (bootproto >= 0x0200) {
  138. if (hdr->setup_sects >= 15) {
  139. printf("Linux kernel version %s\n",
  140. (char *)params +
  141. hdr->kernel_version + 0x200);
  142. } else {
  143. printf("Setup Sectors < 15 - "
  144. "Cannot print kernel version.\n");
  145. }
  146. }
  147. /* Determine image type */
  148. big_image = (bootproto >= 0x0200) &&
  149. (hdr->loadflags & BIG_KERNEL_FLAG);
  150. /* Determine load address */
  151. if (big_image)
  152. *load_addressp = BZIMAGE_LOAD_ADDR;
  153. else
  154. *load_addressp = ZIMAGE_LOAD_ADDR;
  155. printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
  156. memset(setup_base, 0, sizeof(*setup_base));
  157. setup_base->hdr = params->hdr;
  158. if (bootproto >= 0x0204)
  159. kernel_size = hdr->syssize * 16;
  160. else
  161. kernel_size -= setup_size;
  162. if (bootproto == 0x0100) {
  163. /*
  164. * A very old kernel MUST have its real-mode code
  165. * loaded at 0x90000
  166. */
  167. if ((ulong)setup_base != 0x90000) {
  168. /* Copy the real-mode kernel */
  169. memmove((void *)0x90000, setup_base, setup_size);
  170. /* Copy the command line */
  171. memmove((void *)0x99000,
  172. (u8 *)setup_base + COMMAND_LINE_OFFSET,
  173. COMMAND_LINE_SIZE);
  174. /* Relocated */
  175. setup_base = (struct boot_params *)0x90000;
  176. }
  177. /* It is recommended to clear memory up to the 32K mark */
  178. memset((u8 *)0x90000 + setup_size, 0,
  179. SETUP_MAX_SIZE - setup_size);
  180. }
  181. if (big_image) {
  182. if (kernel_size > BZIMAGE_MAX_SIZE) {
  183. printf("Error: bzImage kernel too big! "
  184. "(size: %ld, max: %d)\n",
  185. kernel_size, BZIMAGE_MAX_SIZE);
  186. return 0;
  187. }
  188. } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
  189. printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
  190. kernel_size, ZIMAGE_MAX_SIZE);
  191. return 0;
  192. }
  193. printf("Loading %s at address %lx (%ld bytes)\n",
  194. big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
  195. memmove((void *)*load_addressp, image + setup_size, kernel_size);
  196. return setup_base;
  197. }
  198. int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
  199. unsigned long initrd_addr, unsigned long initrd_size)
  200. {
  201. struct setup_header *hdr = &setup_base->hdr;
  202. int bootproto = get_boot_protocol(hdr);
  203. setup_base->e820_entries = install_e820_map(
  204. ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
  205. if (bootproto == 0x0100) {
  206. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  207. setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
  208. }
  209. if (bootproto >= 0x0200) {
  210. hdr->type_of_loader = 8;
  211. if (initrd_addr) {
  212. printf("Initial RAM disk at linear address "
  213. "0x%08lx, size %ld bytes\n",
  214. initrd_addr, initrd_size);
  215. hdr->ramdisk_image = initrd_addr;
  216. hdr->ramdisk_size = initrd_size;
  217. }
  218. }
  219. if (bootproto >= 0x0201) {
  220. hdr->heap_end_ptr = HEAP_END_OFFSET;
  221. hdr->loadflags |= HEAP_FLAG;
  222. }
  223. if (cmd_line) {
  224. if (bootproto >= 0x0202) {
  225. hdr->cmd_line_ptr = (uintptr_t)cmd_line;
  226. } else if (bootproto >= 0x0200) {
  227. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  228. setup_base->screen_info.cl_offset =
  229. (uintptr_t)cmd_line - (uintptr_t)setup_base;
  230. hdr->setup_move_size = 0x9100;
  231. }
  232. /* build command line at COMMAND_LINE_OFFSET */
  233. build_command_line(cmd_line, auto_boot);
  234. }
  235. #ifdef CONFIG_INTEL_MID
  236. if (bootproto >= 0x0207)
  237. hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
  238. #endif
  239. #ifdef CONFIG_GENERATE_ACPI_TABLE
  240. setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
  241. #endif
  242. setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
  243. setup_video(&setup_base->screen_info);
  244. #ifdef CONFIG_EFI_STUB
  245. setup_efi_info(&setup_base->efi_info);
  246. #endif
  247. return 0;
  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(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  255. {
  256. struct boot_params *base_ptr;
  257. void *bzImage_addr = NULL;
  258. ulong 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 = env_get("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. puts("## 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. puts("Setting up boot parameters failed ...\n");
  291. return -1;
  292. }
  293. /* we assume that the kernel is in place */
  294. return boot_linux_kernel((ulong)base_ptr, load_address, false);
  295. }
  296. U_BOOT_CMD(
  297. zboot, 5, 0, do_zboot,
  298. "Boot bzImage",
  299. "[addr] [size] [initrd addr] [initrd size]\n"
  300. " addr - The optional starting address of the bzimage.\n"
  301. " If not set it defaults to the environment\n"
  302. " variable \"fileaddr\".\n"
  303. " size - The optional size of the bzimage. Defaults to\n"
  304. " zero.\n"
  305. " initrd addr - The address of the initrd image to use, if any.\n"
  306. " initrd size - The size of the initrd image to use, if any.\n"
  307. );