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.8KB

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