zimage.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /*
  36. * Memory lay-out:
  37. *
  38. * relative to setup_base (which is 0x90000 currently)
  39. *
  40. * 0x0000-0x7FFF Real mode kernel
  41. * 0x8000-0x8FFF Stack and heap
  42. * 0x9000-0x90FF Kernel command line
  43. */
  44. #define DEFAULT_SETUP_BASE 0x90000
  45. #define COMMAND_LINE_OFFSET 0x9000
  46. #define HEAP_END_OFFSET 0x8e00
  47. #define COMMAND_LINE_SIZE 2048
  48. static void build_command_line(char *command_line, int auto_boot)
  49. {
  50. char *env_command_line;
  51. command_line[0] = '\0';
  52. env_command_line = getenv("bootargs");
  53. /* set console= argument if we use a serial console */
  54. if (NULL == strstr(env_command_line, "console=")) {
  55. if (0==strcmp(getenv("stdout"), "serial")) {
  56. /* We seem to use serial console */
  57. sprintf(command_line, "console=ttyS0,%s ",
  58. getenv("baudrate"));
  59. }
  60. }
  61. if (auto_boot) {
  62. strcat(command_line, "auto ");
  63. }
  64. if (NULL != env_command_line) {
  65. strcat(command_line, env_command_line);
  66. }
  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. void *setup_base;
  74. int setup_size;
  75. int bootproto;
  76. int big_image;
  77. void *load_address;
  78. setup_base = (void*)DEFAULT_SETUP_BASE; /* base address for real-mode segment */
  79. if (KERNEL_MAGIC != *(u16*)(image + BOOT_FLAG_OFF)) {
  80. printf("Error: Invalid kernel magic (found 0x%04x, expected 0xaa55)\n",
  81. *(u16*)(image + BOOT_FLAG_OFF));
  82. return 0;
  83. }
  84. /* determine boot protocol version */
  85. if (KERNEL_V2_MAGIC == *(u32*)(image+HEADER_OFF)) {
  86. bootproto = *(u16*)(image+VERSION_OFF);
  87. } else {
  88. /* Very old kernel */
  89. bootproto = 0x0100;
  90. }
  91. /* determine size of setup */
  92. if (0 == *(u8*)(image + SETUP_SECTS_OFF)) {
  93. setup_size = 5 * 512;
  94. } else {
  95. setup_size = (*(u8*)(image + SETUP_SECTS_OFF) + 1) * 512;
  96. }
  97. if (setup_size > SETUP_MAX_SIZE) {
  98. printf("Error: Setup is too large (%d bytes)\n", setup_size);
  99. }
  100. /* Determine image type */
  101. big_image = (bootproto >= 0x0200) && (*(u8*)(image + LOADFLAGS_OFF) & BIG_KERNEL_FLAG);
  102. /* Derermine load address */
  103. load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR:ZIMAGE_LOAD_ADDR);
  104. /* load setup */
  105. memmove(setup_base, image, setup_size);
  106. printf("Using boot protocol version %x.%02x\n",
  107. (bootproto & 0xff00) >> 8, bootproto & 0xff);
  108. if (bootproto == 0x0100) {
  109. *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
  110. *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
  111. /* A very old kernel MUST have its real-mode code
  112. * loaded at 0x90000 */
  113. if ((u32)setup_base != 0x90000) {
  114. /* Copy the real-mode kernel */
  115. memmove((void*)0x90000, setup_base, setup_size);
  116. /* Copy the command line */
  117. memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET,
  118. COMMAND_LINE_SIZE);
  119. setup_base = (void*)0x90000; /* Relocated */
  120. }
  121. /* It is recommended to clear memory up to the 32K mark */
  122. memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size);
  123. }
  124. if (bootproto >= 0x0200) {
  125. *(u8*)(setup_base + TYPE_OF_LOADER_OFF) = 0xff;
  126. printf("Linux kernel version %s\n",
  127. (char*)(setup_base + SETUP_START_OFFSET +
  128. *(u16*)(setup_base + START_SYS_OFF + 2)));
  129. if (initrd_addr) {
  130. printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
  131. initrd_addr, initrd_size);
  132. *(u32*)(setup_base + RAMDISK_IMAGE_OFF) = initrd_addr;
  133. *(u32*)(setup_base + RAMDISK_SIZE_OFF)=initrd_size;
  134. }
  135. }
  136. if (bootproto >= 0x0201) {
  137. *(u16*)(setup_base + HEAP_END_PTR_OFF) = HEAP_END_OFFSET;
  138. /* CAN_USE_HEAP */
  139. *(u8*)(setup_base + LOADFLAGS_OFF) =
  140. *(u8*)(setup_base + LOADFLAGS_OFF) | HEAP_FLAG;
  141. }
  142. if (bootproto >= 0x0202) {
  143. *(u32*)(setup_base + CMD_LINE_PTR_OFF) = (u32)setup_base + COMMAND_LINE_OFFSET;
  144. } else if (bootproto >= 0x0200) {
  145. *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
  146. *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
  147. *(u16*)(setup_base + SETUP_MOVE_SIZE_OFF) = 0x9100;
  148. }
  149. if (big_image) {
  150. if ((kernel_size - setup_size) > BZIMAGE_MAX_SIZE) {
  151. printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
  152. kernel_size - setup_size, BZIMAGE_MAX_SIZE);
  153. return 0;
  154. }
  155. } else if ((kernel_size - setup_size) > ZIMAGE_MAX_SIZE) {
  156. printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
  157. kernel_size - setup_size, ZIMAGE_MAX_SIZE);
  158. return 0;
  159. }
  160. /* build command line at COMMAND_LINE_OFFSET */
  161. build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
  162. printf("Loading %czImage at address 0x%08x (%ld bytes)\n", big_image ? 'b' : ' ',
  163. (u32)load_address, kernel_size - setup_size);
  164. memmove(load_address, image + setup_size, kernel_size - setup_size);
  165. /* ready for booting */
  166. return setup_base;
  167. }
  168. void boot_zimage(void *setup_base)
  169. {
  170. struct pt_regs regs;
  171. memset(&regs, 0, sizeof(struct pt_regs));
  172. regs.xds = (u32)setup_base >> 4;
  173. regs.xss = 0x9000;
  174. regs.esp = 0x9000;
  175. regs.eflags = 0;
  176. enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, &regs, &regs);
  177. }
  178. image_header_t *fake_zimage_header(image_header_t *hdr, void *ptr, int size)
  179. {
  180. /* There is no way to know the size of a zImage ... *
  181. * so we assume that 2MB will be enough for now */
  182. #define ZIMAGE_SIZE 0x200000
  183. /* load a 1MB, the loaded will have to be moved to its final
  184. * position again later... */
  185. #define ZIMAGE_LOAD 0x100000
  186. ulong checksum;
  187. if (KERNEL_MAGIC != *(u16*)(ptr + BOOT_FLAG_OFF)) {
  188. /* not a zImage or bzImage */
  189. return NULL;
  190. }
  191. if (-1 == size) {
  192. size = ZIMAGE_SIZE;
  193. }
  194. #if 0
  195. checksum = crc32 (0, ptr, size);
  196. #else
  197. checksum = 0;
  198. #endif
  199. memset(hdr, 0, sizeof(image_header_t));
  200. /* Build new header */
  201. hdr->ih_magic = htonl(IH_MAGIC);
  202. hdr->ih_time = 0;
  203. hdr->ih_size = htonl(size);
  204. hdr->ih_load = htonl(ZIMAGE_LOAD);
  205. hdr->ih_ep = 0;
  206. hdr->ih_dcrc = htonl(checksum);
  207. hdr->ih_os = IH_OS_LINUX;
  208. hdr->ih_arch = IH_CPU_I386;
  209. hdr->ih_type = IH_TYPE_KERNEL;
  210. hdr->ih_comp = IH_COMP_NONE;
  211. strncpy((char *)hdr->ih_name, "(none)", IH_NMLEN);
  212. checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
  213. hdr->ih_hcrc = htonl(checksum);
  214. return hdr;
  215. }