zimage.c 10 KB

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