You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lkrnprefix.S 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright (C) 2000, Entity Cyber, Inc.
  3. Authors: Gary Byers (gb@thinguin.org)
  4. Marty Connor (mdc@thinguin.org)
  5. This software may be used and distributed according to the terms
  6. of the GNU Public License (GPL), incorporated herein by reference.
  7. Description:
  8. This is just a little bit of code and data that can get prepended
  9. to a ROM image in order to allow bootloaders to load the result
  10. as if it were a Linux kernel image.
  11. A real Linux kernel image consists of a one-sector boot loader
  12. (to load the image from a floppy disk), followed a few sectors
  13. of setup code, followed by the kernel code itself. There's
  14. a table in the first sector (starting at offset 497) that indicates
  15. how many sectors of setup code follow the first sector and which
  16. contains some other parameters that aren't interesting in this
  17. case.
  18. When a bootloader loads the sectors that comprise a kernel image,
  19. it doesn't execute the code in the first sector (since that code
  20. would try to load the image from a floppy disk.) The code in the
  21. first sector below doesn't expect to get executed (and prints an
  22. error message if it ever -is- executed.)
  23. We don't require much in the way of setup code. Historically, the
  24. Linux kernel required at least 4 sectors of setup code.
  25. Therefore, at least 4 sectors must be present even though we don't
  26. use them.
  27. */
  28. FILE_LICENCE ( GPL_ANY )
  29. #define SETUPSECS 4 /* Minimal nr of setup-sectors */
  30. #define PREFIXSIZE ((SETUPSECS+1)*512)
  31. #define PREFIXPGH (PREFIXSIZE / 16 )
  32. #define BOOTSEG 0x07C0 /* original address of boot-sector */
  33. #define INITSEG 0x9000 /* we move boot here - out of the way */
  34. #define SETUPSEG 0x9020 /* setup starts here */
  35. #define SYSSEG 0x1000 /* system loaded at 0x10000 (65536). */
  36. .text
  37. .code16
  38. .arch i386
  39. .org 0
  40. .section ".prefix", "ax", @progbits
  41. /*
  42. This is a minimal boot sector. If anyone tries to execute it (e.g., if
  43. a .lilo file is dd'ed to a floppy), print an error message.
  44. */
  45. bootsector:
  46. jmp $BOOTSEG, $1f /* reload cs:ip to match relocation addr */
  47. 1:
  48. movw $0x2000, %di /* 0x2000 is arbitrary value >= length
  49. of bootsect + room for stack */
  50. movw $BOOTSEG, %ax
  51. movw %ax,%ds
  52. movw %ax,%es
  53. cli
  54. movw %ax, %ss /* put stack at BOOTSEG:0x2000. */
  55. movw %di,%sp
  56. sti
  57. movw $why_end-why, %cx
  58. movw $why, %si
  59. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  60. movb $0x0e, %ah /* write char, tty mode */
  61. prloop:
  62. lodsb
  63. int $0x10
  64. loop prloop
  65. freeze: jmp freeze
  66. why: .ascii "This image cannot be loaded from a floppy disk.\r\n"
  67. why_end:
  68. /*
  69. The following header is documented in the Linux source code at
  70. Documentation/i386/boot.txt
  71. */
  72. .org 497
  73. setup_sects:
  74. .byte SETUPSECS
  75. root_flags:
  76. .word 0
  77. syssize:
  78. .long -PREFIXPGH
  79. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  80. .ascii "ADDL"
  81. .long syssize
  82. .long 16
  83. .long 0
  84. .previous
  85. ram_size:
  86. .word 0
  87. vid_mode:
  88. .word 0
  89. root_dev:
  90. .word 0
  91. boot_flag:
  92. .word 0xAA55
  93. jump:
  94. /* Manually specify a two-byte jmp instruction here rather
  95. * than leaving it up to the assembler. */
  96. .byte 0xeb
  97. .byte setup_code - header
  98. header:
  99. .byte 'H', 'd', 'r', 'S'
  100. version:
  101. .word 0x0207 /* 2.07 */
  102. realmode_swtch:
  103. .long 0
  104. start_sys:
  105. .word 0
  106. kernel_version:
  107. .word 0
  108. type_of_loader:
  109. .byte 0
  110. loadflags:
  111. .byte 0
  112. setup_move_size:
  113. .word 0
  114. code32_start:
  115. .long 0
  116. ramdisk_image:
  117. .long 0
  118. ramdisk_size:
  119. .long 0
  120. bootsect_kludge:
  121. .long 0
  122. heap_end_ptr:
  123. .word 0
  124. pad1:
  125. .word 0
  126. cmd_line_ptr:
  127. .long 0
  128. initrd_addr_max:
  129. /* We don't use an initrd but some bootloaders (e.g. SYSLINUX) have
  130. * been known to require this field. Set the value to 2 GB. This
  131. * value is also used by the Linux kernel. */
  132. .long 0x7fffffff
  133. kernel_alignment:
  134. .long 0
  135. relocatable_kernel:
  136. .byte 0
  137. pad2:
  138. .byte 0, 0, 0
  139. cmdline_size:
  140. .long 0
  141. hardware_subarch:
  142. .long 0
  143. hardware_subarch_data:
  144. .byte 0, 0, 0, 0, 0, 0, 0, 0
  145. /*
  146. We don't need to do too much setup.
  147. This code gets loaded at SETUPSEG:0. It wants to start
  148. executing the image that's loaded at SYSSEG:0 and
  149. whose entry point is SYSSEG:0.
  150. */
  151. setup_code:
  152. /* We expect to be contiguous in memory once loaded. The Linux image
  153. * boot process requires that setup code is loaded separately from
  154. * "non-real code". Since we don't need any information that's left
  155. * in the prefix, it doesn't matter: we just have to ensure that
  156. * %cs:0000 is where the start of the image *would* be.
  157. */
  158. ljmp $(SYSSEG-(PREFIXSIZE/16)), $run_ipxe
  159. .org PREFIXSIZE
  160. /*
  161. We're now at the beginning of the kernel proper.
  162. */
  163. run_ipxe:
  164. /* Set up stack just below 0x7c00 */
  165. xorw %ax, %ax
  166. movw %ax, %ss
  167. movw $0x7c00, %sp
  168. /* Install iPXE */
  169. call install
  170. /* Set up real-mode stack */
  171. movw %bx, %ss
  172. movw $_estack16, %sp
  173. /* Jump to .text16 segment */
  174. pushw %ax
  175. pushw $1f
  176. lret
  177. .section ".text16", "awx", @progbits
  178. 1:
  179. pushl $main
  180. pushw %cs
  181. call prot_call
  182. popl %ecx /* discard */
  183. /* Uninstall iPXE */
  184. call uninstall
  185. /* Boot next device */
  186. int $0x18