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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. .globl _lkrn_start
  42. _lkrn_start:
  43. /*
  44. This is a minimal boot sector. If anyone tries to execute it (e.g., if
  45. a .lilo file is dd'ed to a floppy), print an error message.
  46. */
  47. bootsector:
  48. jmp $BOOTSEG, $1f /* reload cs:ip to match relocation addr */
  49. 1:
  50. movw $0x2000, %di /* 0x2000 is arbitrary value >= length
  51. of bootsect + room for stack */
  52. movw $BOOTSEG, %ax
  53. movw %ax,%ds
  54. movw %ax,%es
  55. cli
  56. movw %ax, %ss /* put stack at BOOTSEG:0x2000. */
  57. movw %di,%sp
  58. sti
  59. movw $why_end-why, %cx
  60. movw $why, %si
  61. movw $0x0007, %bx /* page 0, attribute 7 (normal) */
  62. movb $0x0e, %ah /* write char, tty mode */
  63. prloop:
  64. lodsb
  65. int $0x10
  66. loop prloop
  67. freeze: jmp freeze
  68. why: .ascii "This image cannot be loaded from a floppy disk.\r\n"
  69. why_end:
  70. /*
  71. The following header is documented in the Linux source code at
  72. Documentation/x86/boot.txt
  73. */
  74. .org 497
  75. setup_sects:
  76. .byte SETUPSECS
  77. root_flags:
  78. .word 0
  79. syssize:
  80. .long -PREFIXPGH
  81. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  82. .ascii "ADDL"
  83. .long syssize
  84. .long 16
  85. .long 0
  86. .previous
  87. ram_size:
  88. .word 0
  89. vid_mode:
  90. .word 0
  91. root_dev:
  92. .word 0
  93. boot_flag:
  94. .word 0xAA55
  95. jump:
  96. /* Manually specify a two-byte jmp instruction here rather
  97. * than leaving it up to the assembler. */
  98. .byte 0xeb
  99. .byte setup_code - header
  100. header:
  101. .byte 'H', 'd', 'r', 'S'
  102. version:
  103. .word 0x0207 /* 2.07 */
  104. realmode_swtch:
  105. .long 0
  106. start_sys:
  107. .word 0
  108. kernel_version:
  109. .word 0
  110. type_of_loader:
  111. .byte 0
  112. loadflags:
  113. .byte 0
  114. setup_move_size:
  115. .word 0
  116. code32_start:
  117. .long 0
  118. ramdisk_image:
  119. .long 0
  120. ramdisk_size:
  121. .long 0
  122. bootsect_kludge:
  123. .long 0
  124. heap_end_ptr:
  125. .word 0
  126. pad1:
  127. .word 0
  128. cmd_line_ptr:
  129. .long 0
  130. initrd_addr_max:
  131. /* We don't use an initrd but some bootloaders (e.g. SYSLINUX) have
  132. * been known to require this field. Set the value to 2 GB. This
  133. * value is also used by the Linux kernel. */
  134. .long 0x7fffffff
  135. kernel_alignment:
  136. .long 0
  137. relocatable_kernel:
  138. .byte 0
  139. pad2:
  140. .byte 0, 0, 0
  141. cmdline_size:
  142. .long 0
  143. hardware_subarch:
  144. .long 0
  145. hardware_subarch_data:
  146. .byte 0, 0, 0, 0, 0, 0, 0, 0
  147. /*
  148. We don't need to do too much setup.
  149. This code gets loaded at SETUPSEG:0. It wants to start
  150. executing the image that's loaded at SYSSEG:0 and
  151. whose entry point is SYSSEG:0.
  152. */
  153. setup_code:
  154. /* We expect to be contiguous in memory once loaded. The Linux image
  155. * boot process requires that setup code is loaded separately from
  156. * "non-real code". Since we don't need any information that's left
  157. * in the prefix, it doesn't matter: we just have to ensure that
  158. * %cs:0000 is where the start of the image *would* be.
  159. */
  160. ljmp $(SYSSEG-(PREFIXSIZE/16)), $run_ipxe
  161. .org PREFIXSIZE
  162. /*
  163. We're now at the beginning of the kernel proper.
  164. */
  165. run_ipxe:
  166. /* Set up stack just below 0x7c00 */
  167. xorw %ax, %ax
  168. movw %ax, %ss
  169. movw $0x7c00, %sp
  170. /* Retrieve command-line pointer */
  171. movl %es:cmd_line_ptr, %edx
  172. /* Install iPXE */
  173. call install
  174. /* Set up real-mode stack */
  175. movw %bx, %ss
  176. movw $_estack16, %sp
  177. /* Jump to .text16 segment */
  178. pushw %ax
  179. pushw $1f
  180. lret
  181. .section ".text16", "awx", @progbits
  182. 1:
  183. /* Set up %ds for access to .data16 */
  184. movw %bx, %ds
  185. /* Store command-line pointer */
  186. movl %edx, cmdline_phys
  187. /* Run iPXE */
  188. pushl $main
  189. pushw %cs
  190. call prot_call
  191. popl %ecx /* discard */
  192. /* Uninstall iPXE */
  193. call uninstall
  194. /* Boot next device */
  195. int $0x18