zimage.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. static void build_command_line(char *command_line, int auto_boot)
  51. {
  52. char *env_command_line;
  53. command_line[0] = '\0';
  54. env_command_line = getenv("bootargs");
  55. /* set console= argument if we use a serial console */
  56. if (!strstr(env_command_line, "console=")) {
  57. if (!strcmp(getenv("stdout"), "serial")) {
  58. /* We seem to use serial console */
  59. sprintf(command_line, "console=ttyS0,%s ",
  60. getenv("baudrate"));
  61. }
  62. }
  63. if (auto_boot)
  64. strcat(command_line, "auto ");
  65. if (env_command_line)
  66. strcat(command_line, env_command_line);
  67. printf("Kernel command line: \"%s\"\n", command_line);
  68. }
  69. void *load_zimage(char *image, unsigned long kernel_size,
  70. unsigned long initrd_addr, unsigned long initrd_size,
  71. int auto_boot)
  72. {
  73. struct boot_params *setup_base;
  74. int setup_size;
  75. int bootproto;
  76. int big_image;
  77. void *load_address;
  78. struct boot_params *params = (struct boot_params *)image;
  79. struct setup_header *hdr = &params->hdr;
  80. /* base address for real-mode segment */
  81. setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
  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. }
  90. /* determine boot protocol version */
  91. if (KERNEL_V2_MAGIC == hdr->header) {
  92. printf("Magic signature found\n");
  93. bootproto = hdr->version;
  94. } else {
  95. /* Very old kernel */
  96. printf("Magic signature not found\n");
  97. bootproto = 0x0100;
  98. }
  99. /* determine size of setup */
  100. if (0 == hdr->setup_sects) {
  101. printf("Setup Sectors = 0 (defaulting to 4)\n");
  102. setup_size = 5 * 512;
  103. } else {
  104. setup_size = (hdr->setup_sects + 1) * 512;
  105. }
  106. printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
  107. if (setup_size > SETUP_MAX_SIZE)
  108. printf("Error: Setup is too large (%d bytes)\n", setup_size);
  109. /* Determine image type */
  110. big_image = (bootproto >= 0x0200) &&
  111. (hdr->loadflags & BIG_KERNEL_FLAG);
  112. /* Determine load address */
  113. if (big_image)
  114. load_address = (void *)BZIMAGE_LOAD_ADDR;
  115. else
  116. load_address = (void *)ZIMAGE_LOAD_ADDR;
  117. /* load setup */
  118. printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n",
  119. (ulong)setup_base, setup_size);
  120. memmove(setup_base, image, setup_size);
  121. printf("Using boot protocol version %x.%02x\n",
  122. (bootproto & 0xff00) >> 8, bootproto & 0xff);
  123. if (bootproto == 0x0100) {
  124. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  125. setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
  126. /*
  127. * A very old kernel MUST have its real-mode code
  128. * loaded at 0x90000
  129. */
  130. if ((u32)setup_base != 0x90000) {
  131. /* Copy the real-mode kernel */
  132. memmove((void *)0x90000, setup_base, setup_size);
  133. /* Copy the command line */
  134. memmove((void *)0x99000,
  135. (u8 *)setup_base + COMMAND_LINE_OFFSET,
  136. COMMAND_LINE_SIZE);
  137. /* Relocated */
  138. setup_base = (struct boot_params *)0x90000;
  139. }
  140. /* It is recommended to clear memory up to the 32K mark */
  141. memset((u8 *)0x90000 + setup_size, 0,
  142. SETUP_MAX_SIZE - setup_size);
  143. }
  144. /* We are now setting up the real-mode version of the header */
  145. hdr = &setup_base->hdr;
  146. if (bootproto >= 0x0200) {
  147. hdr->type_of_loader = 8;
  148. if (hdr->setup_sects >= 15) {
  149. printf("Linux kernel version %s\n",
  150. (char *)setup_base +
  151. hdr->kernel_version + 0x200);
  152. } else {
  153. printf("Setup Sectors < 15 - "
  154. "Cannot print kernel version.\n");
  155. }
  156. if (initrd_addr) {
  157. printf("Initial RAM disk at linear address "
  158. "0x%08lx, size %ld bytes\n",
  159. initrd_addr, initrd_size);
  160. hdr->ramdisk_image = initrd_addr;
  161. hdr->ramdisk_size = initrd_size;
  162. }
  163. }
  164. if (bootproto >= 0x0201) {
  165. hdr->heap_end_ptr = HEAP_END_OFFSET;
  166. hdr->loadflags |= HEAP_FLAG;
  167. }
  168. if (bootproto >= 0x0202) {
  169. hdr->cmd_line_ptr =
  170. (uintptr_t)setup_base + COMMAND_LINE_OFFSET;
  171. } else if (bootproto >= 0x0200) {
  172. setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
  173. setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
  174. hdr->setup_move_size = 0x9100;
  175. }
  176. if (bootproto >= 0x0204)
  177. kernel_size = hdr->syssize * 16;
  178. else
  179. kernel_size -= setup_size;
  180. if (big_image) {
  181. if ((kernel_size) > BZIMAGE_MAX_SIZE) {
  182. printf("Error: bzImage kernel too big! "
  183. "(size: %ld, max: %d)\n",
  184. kernel_size, BZIMAGE_MAX_SIZE);
  185. return 0;
  186. }
  187. } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
  188. printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
  189. kernel_size, ZIMAGE_MAX_SIZE);
  190. return 0;
  191. }
  192. /* build command line at COMMAND_LINE_OFFSET */
  193. build_command_line((char *)setup_base + COMMAND_LINE_OFFSET, auto_boot);
  194. printf("Loading %czImage at address 0x%08x (%ld bytes)\n",
  195. big_image ? 'b' : ' ', (u32)load_address, kernel_size);
  196. memmove(load_address, image + setup_size, kernel_size);
  197. /* ready for booting */
  198. return setup_base;
  199. }
  200. void boot_zimage(void *setup_base)
  201. {
  202. struct pt_regs regs;
  203. memset(&regs, 0, sizeof(struct pt_regs));
  204. regs.xds = (u32)setup_base >> 4;
  205. regs.xes = regs.xds;
  206. regs.xss = regs.xds;
  207. regs.esp = 0x9000;
  208. regs.eflags = 0;
  209. enter_realmode(((u32)setup_base + SETUP_START_OFFSET) >> 4, 0,
  210. &regs, &regs);
  211. }
  212. int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  213. {
  214. void *base_ptr;
  215. void *bzImage_addr = NULL;
  216. char *s;
  217. ulong bzImage_size = 0;
  218. disable_interrupts();
  219. /* Setup board for maximum PC/AT Compatibility */
  220. setup_pcat_compatibility();
  221. if (argc >= 2) {
  222. /* argv[1] holds the address of the bzImage */
  223. s = argv[1];
  224. } else {
  225. s = getenv("fileaddr");
  226. }
  227. if (s)
  228. bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
  229. if (argc >= 3)
  230. /* argv[2] holds the size of the bzImage */
  231. bzImage_size = simple_strtoul(argv[2], NULL, 16);
  232. /* Lets look for */
  233. base_ptr = load_zimage(bzImage_addr, bzImage_size, 0, 0, 0);
  234. if (!base_ptr) {
  235. printf("## Kernel loading failed ...\n");
  236. } else {
  237. printf("## Transferring control to Linux "
  238. "(at address %08x) ...\n",
  239. (u32)base_ptr);
  240. /* we assume that the kernel is in place */
  241. printf("\nStarting kernel ...\n\n");
  242. boot_zimage(base_ptr);
  243. /* does not return */
  244. }
  245. return -1;
  246. }
  247. U_BOOT_CMD(
  248. zboot, 2, 0, do_zboot,
  249. "Boot bzImage",
  250. ""
  251. );