zimage.c 10 KB

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