zimage.c 9.7 KB

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