zimage.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Linux x86 zImage and bzImage loading
  25. *
  26. * based on the procdure described in
  27. * linux/Documentation/i386/boot.txt
  28. */
  29. #include <common.h>
  30. #include <asm/io.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/zimage.h>
  33. #include <asm/realmode.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/bootparam.h>
  36. /*
  37. * Memory lay-out:
  38. *
  39. * relative to setup_base (which is 0x90000 currently)
  40. *
  41. * 0x0000-0x7FFF Real mode kernel
  42. * 0x8000-0x8FFF Stack and heap
  43. * 0x9000-0x90FF Kernel command line
  44. */
  45. #define DEFAULT_SETUP_BASE 0x90000
  46. #define COMMAND_LINE_OFFSET 0x9000
  47. #define HEAP_END_OFFSET 0x8e00
  48. #define COMMAND_LINE_SIZE 2048
  49. static void build_command_line(char *command_line, int auto_boot)
  50. {
  51. char *env_command_line;
  52. command_line[0] = '\0';
  53. env_command_line = getenv("bootargs");
  54. /* set console= argument if we use a serial console */
  55. if (NULL == strstr(env_command_line, "console=")) {
  56. if (0==strcmp(getenv("stdout"), "serial")) {
  57. /* We seem to use serial console */
  58. sprintf(command_line, "console=ttyS0,%s ",
  59. getenv("baudrate"));
  60. }
  61. }
  62. if (auto_boot) {
  63. strcat(command_line, "auto ");
  64. }
  65. if (NULL != env_command_line) {
  66. strcat(command_line, env_command_line);
  67. }
  68. printf("Kernel command line: \"%s\"\n", command_line);
  69. }
  70. void *load_zimage(char *image, unsigned long kernel_size,
  71. unsigned long initrd_addr, unsigned long initrd_size,
  72. int auto_boot)
  73. {
  74. void *setup_base;
  75. int setup_size;
  76. int bootproto;
  77. int big_image;
  78. void *load_address;
  79. struct setup_header *hdr = (struct setup_header *)(image + SETUP_SECTS_OFF);
  80. setup_base = (void*)DEFAULT_SETUP_BASE; /* base address for real-mode segment */
  81. if (KERNEL_MAGIC != hdr->boot_flag) {
  82. printf("Error: Invalid Boot Flag (found 0x%04x, expected 0x%04x)\n",
  83. hdr->boot_flag, KERNEL_MAGIC);
  84. return 0;
  85. } else {
  86. printf("Valid Boot Flag\n");
  87. }
  88. /* determine boot protocol version */
  89. if (KERNEL_V2_MAGIC == hdr->header) {
  90. printf("Magic signature found\n");
  91. bootproto = hdr->version;
  92. } else {
  93. /* Very old kernel */
  94. printf("Magic signature not found\n");
  95. bootproto = 0x0100;
  96. }
  97. /* determine size of setup */
  98. if (0 == hdr->setup_sects) {
  99. printf("Setup Sectors = 0 (defaulting to 4)\n");
  100. setup_size = 5 * 512;
  101. } else {
  102. setup_size = (hdr->setup_sects + 1) * 512;
  103. }
  104. printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
  105. if (setup_size > SETUP_MAX_SIZE) {
  106. printf("Error: Setup is too large (%d bytes)\n", setup_size);
  107. }
  108. /* Determine image type */
  109. big_image = (bootproto >= 0x0200) && (hdr->loadflags & BIG_KERNEL_FLAG);
  110. /* Determine load address */
  111. load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR : ZIMAGE_LOAD_ADDR);
  112. /* load setup */
  113. printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n", (ulong)setup_base, setup_size);
  114. memmove(setup_base, image, setup_size);
  115. printf("Using boot protocol version %x.%02x\n",
  116. (bootproto & 0xff00) >> 8, bootproto & 0xff);
  117. if (bootproto == 0x0100) {
  118. *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
  119. *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
  120. /* A very old kernel MUST have its real-mode code
  121. * loaded at 0x90000 */
  122. if ((u32)setup_base != 0x90000) {
  123. /* Copy the real-mode kernel */
  124. memmove((void*)0x90000, setup_base, setup_size);
  125. /* Copy the command line */
  126. memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET,
  127. COMMAND_LINE_SIZE);
  128. setup_base = (void*)0x90000; /* Relocated */
  129. }
  130. /* It is recommended to clear memory up to the 32K mark */
  131. memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size);
  132. }
  133. /* We are now setting up the real-mode version of the header */
  134. hdr = (struct setup_header *)(setup_base + SETUP_SECTS_OFF);
  135. if (bootproto >= 0x0200) {
  136. hdr->type_of_loader = 8;
  137. if (hdr->setup_sects >= 15)
  138. printf("Linux kernel version %s\n", (char *)
  139. (setup_base + (hdr->kernel_version + 0x200)));
  140. else
  141. printf("Setup Sectors < 15 - Cannot print kernel version.\n");
  142. if (initrd_addr) {
  143. printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
  144. initrd_addr, initrd_size);
  145. hdr->ramdisk_image = initrd_addr;
  146. hdr->ramdisk_size = initrd_size;
  147. }
  148. }
  149. if (bootproto >= 0x0201) {
  150. hdr->heap_end_ptr = HEAP_END_OFFSET;
  151. hdr->loadflags |= HEAP_FLAG;
  152. }
  153. if (bootproto >= 0x0202) {
  154. hdr->cmd_line_ptr = (u32)setup_base + COMMAND_LINE_OFFSET;
  155. } else if (bootproto >= 0x0200) {
  156. *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
  157. *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
  158. hdr->setup_move_size = 0x9100;
  159. }
  160. if (bootproto >= 0x0204)
  161. kernel_size = hdr->syssize * 16;
  162. else
  163. kernel_size -= setup_size;
  164. if (big_image) {
  165. if ((kernel_size) > BZIMAGE_MAX_SIZE) {
  166. printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
  167. kernel_size, BZIMAGE_MAX_SIZE);
  168. return 0;
  169. }
  170. } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
  171. printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
  172. kernel_size, ZIMAGE_MAX_SIZE);
  173. return 0;
  174. }
  175. /* build command line at COMMAND_LINE_OFFSET */
  176. build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
  177. printf("Loading %czImage at address 0x%08x (%ld bytes)\n", big_image ? 'b' : ' ',
  178. (u32)load_address, kernel_size);
  179. memmove(load_address, image + setup_size, kernel_size);
  180. /* ready for booting */
  181. return setup_base;
  182. }
  183. void boot_zimage(void *setup_base)
  184. {
  185. struct pt_regs regs;
  186. memset(&regs, 0, sizeof(struct pt_regs));
  187. regs.xds = (u32)setup_base >> 4;
  188. regs.xes = regs.xds;
  189. regs.xss = regs.xds;
  190. regs.esp = 0x9000;
  191. regs.eflags = 0;
  192. enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, &regs, &regs);
  193. }
  194. int do_zboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  195. {
  196. void *base_ptr;
  197. void *bzImage_addr = NULL;
  198. char *s;
  199. ulong bzImage_size = 0;
  200. disable_interrupts();
  201. /* Setup board for maximum PC/AT Compatibility */
  202. setup_pcat_compatibility();
  203. if (argc >= 2)
  204. /* argv[1] holds the address of the bzImage */
  205. s = argv[1];
  206. else
  207. s = getenv("fileaddr");
  208. if (s)
  209. bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
  210. if (argc >= 3)
  211. /* argv[2] holds the size of the bzImage */
  212. bzImage_size = simple_strtoul(argv[2], NULL, 16);
  213. /* Lets look for*/
  214. base_ptr = load_zimage (bzImage_addr, bzImage_size, 0, 0, 0);
  215. if (NULL == base_ptr) {
  216. printf ("## Kernel loading failed ...\n");
  217. } else {
  218. printf ("## Transferring control to Linux (at address %08x) ...\n",
  219. (u32)base_ptr);
  220. /* we assume that the kernel is in place */
  221. printf("\nStarting kernel ...\n\n");
  222. boot_zimage(base_ptr);
  223. /* does not return */
  224. }
  225. return -1;
  226. }
  227. U_BOOT_CMD(
  228. zboot, 2, 0, do_zboot,
  229. "Boot bzImage",
  230. ""
  231. );