zimage.c 8.6 KB

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