zimage.c 7.4 KB

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