zimage.c 8.9 KB

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