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.

hdprefix.S 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #warning "Needs fixing up for gpxe"
  2. #if 0
  3. /****************************************************************\
  4. hdprefix.S Copyright (C) 2005 Per Dalgas Jakobsen
  5. This code has been inspired/derived by the OSLoader by Vladislav Aleksandrov.
  6. http://www.programmersheaven.com/zone5/cat469/40546.htm.
  7. This software may be used and distributed according to the terms
  8. of the GNU Public License (GPL), incorporated herein by reference.
  9. hdprefix.S is loaded at 0x0000:0x7c00 by the bios-startup routines.
  10. Actions performed by hdprefix:
  11. 1) Load the MBR to LOADSEG:0
  12. 2) Check which partition is active (or try first partition if none active)
  13. 3) Check wether LBA is supported.
  14. 3a) LBA
  15. 3a1) Load PAYLOAD_SECTS sectors from chosen partition to LOADSEG:0
  16. 3b) CHS (standard)
  17. 3b1) Load PAYLOAD_SECTS sectors from chosen partition to LOADSEG:0
  18. 4) Check loaded bootsector for BOOTMAGIC code.
  19. 5) Jump to payload LOADSEG:ENTRYPOINT.
  20. Output with failure points (!#):
  21. ---
  22. Loading (!1)partition #
  23. Std. BIOS(!2) | Ext. BIOS(!3)
  24. Booting...(!4)
  25. (!5)
  26. ---
  27. !1: Failed to load MBR with Int13,ah=2.
  28. !2: Failed to load bootrecord+payload with Int13,ah=2.
  29. !3: Failed to load bootrecord+payload with Int13,ah=42.
  30. !4: Invalid BOOTMAGIC in loaded bootrecord.
  31. !5: Jumping to payload.
  32. \*****************************************************************/
  33. .equ BOOTSEG, 0x07c0
  34. .equ LOADSEG, 0x1000
  35. .equ ENTRYPOINT, _start
  36. .equ BOOTMAGIC, 0x0aa55
  37. .equ partition_table, 0x1be
  38. .equ partition_rec_size, 0x10
  39. .equ boot_ind, 0 /* 80h=active */
  40. .equ start_head, 1
  41. .equ start_sector, 2 /* bits 0-5 */
  42. .equ start_cyl, 3 /* bits 8,9 in bits 6,7 of sector */
  43. .equ os_ind, 4 /* os indicator */
  44. .equ end_head, 5
  45. .equ end_sector, 6 /* bits 0-5 */
  46. .equ end_track, 7 /* bits 8,9 in bits 6,7 of sector */
  47. .equ nsect, 8 /* sectors preceding partition */
  48. .equ lenght, 0x0c /* length of partition in sectors */
  49. /-------------------------------------------------------------
  50. .arch i386
  51. .text
  52. .section ".prefix", "ax", @progbits
  53. .code16
  54. bootstart:
  55. jmp $BOOTSEG,$_go /* reload cs:ip */
  56. /****************************************************************/
  57. /* support routines. */
  58. /*--------------------------------------------------------------*/
  59. _failed:
  60. movw $BOOTSEG,%ax
  61. movw %ax,%ds
  62. movw $_failed_msg_end-_failed_msg,%cx
  63. movw $_failed_msg,%si
  64. call _print_str
  65. /* stop execution - should probably have option to auto-reboot after delay. */
  66. _failed_loop:
  67. jmp _failed_loop
  68. /*--------------------------------------------------------------*/
  69. _print_str:
  70. /* cx = count, ds:si = string. */
  71. movw $0x0007,%bx
  72. movb $0x0e,%ah
  73. _print_loop:
  74. lodsb
  75. int $0x10
  76. loop _print_loop
  77. ret
  78. /*--------------------------------------------------------------*/
  79. _print_char:
  80. /* al = char. */
  81. movw $0x0007,%bx
  82. movb $0x0e,%ah
  83. int $0x10
  84. ret
  85. /*--------------------------------------------------------------*/
  86. _print_nl:
  87. /* - */
  88. movb $0x0d,%al
  89. call _print_char
  90. movb $0x0a,%al
  91. call _print_char
  92. ret
  93. /*--------------------------------------------------------------*/
  94. _print_hex:
  95. /* dx = value */
  96. movb $0x0e,%ah /* write char, tty mode */
  97. movw $0x0007,%bx /* page 0, attribute 7 (normal) */
  98. call _print_digit
  99. call _print_digit
  100. call _print_digit
  101. /* fall through */
  102. _print_digit:
  103. rolw $4,%dx /* rotate so that lowest 4 bits are used */
  104. movb $0x0f,%al /* mask for nibble */
  105. andb %dl,%al
  106. addb $0x90,%al /* convert al to ascii hex (four instructions) */
  107. daa
  108. adcb $0x40,%al
  109. daa
  110. int $0x10
  111. ret
  112. /****************************************************************/
  113. _go:
  114. cli
  115. movw $BOOTSEG,%ax
  116. movw %ax,%ds
  117. movw %ax,%ss
  118. movw $0x2000,%sp /* good large stack. */
  119. sti
  120. cld
  121. movw $LOADSEG,%ax
  122. movw %ax,%es
  123. movw $_load_msg_end-_load_msg,%cx
  124. movw $_load_msg,%si
  125. call _print_str
  126. /*--- load MBR so we can use its partition table. ---*/
  127. xorw %bx,%bx
  128. movw $0x0001,%cx /* chs: 0,0,1 */
  129. movb %bh,%dh /* - */
  130. movb $0x80,%dl
  131. movw $0x0201,%ax /* read one sector (MBR) */
  132. int $0x13
  133. jc _failed
  134. /*--- find the active partition ---*/
  135. movw $_part_msg_end-_part_msg,%cx
  136. movw $_part_msg,%si
  137. call _print_str
  138. movw $partition_table,%di
  139. movw $4,%cx
  140. _partition_loop:
  141. cmpb $0x80,%es:(%di) /* active? */
  142. je _partition_found
  143. addw $partition_rec_size,%di
  144. loop _partition_loop
  145. /*- no partitions marked active - use 1. partition. */
  146. movw $partition_table,%di
  147. movw $4,%cx
  148. _partition_found:
  149. movb $'5',%al /* convert to ascii */
  150. subb %cl,%al
  151. call _print_char
  152. call _print_nl
  153. /*--- check for lba support ---*/
  154. movw $0x55aa,%bx
  155. movb $0x80,%dl
  156. movb $0x41,%ah
  157. int $0x13
  158. jc __bios
  159. cmpw $0x0aa55,%bx
  160. jnz __bios
  161. testb $1,%cl
  162. jz __bios
  163. /*--- use lba bios calls to read sectors ---*/
  164. _lba:
  165. movw $_extbios_msg_end-_extbios_msg,%cx
  166. movw $_extbios_msg,%si
  167. call _print_str
  168. movw %es:nsect(%di),%ax
  169. movw %ax,_bios_lba_low
  170. movw %es:nsect+2(%di),%ax
  171. movw %ax,_bios_lba_high
  172. movb $0x80,%dl
  173. movw $_disk_address_packet,%si
  174. movw $0x4200,%ax /* read */
  175. int $0x13
  176. jc _failed
  177. jmp __loaded
  178. /*--- use standard bios calls to read sectors ---*/
  179. __bios:
  180. movw $_stdbios_msg_end-_stdbios_msg,%cx
  181. movw $_stdbios_msg,%si
  182. call _print_str
  183. movw _disk_address_packet+2(,1),%ax /* only low byte is used. */
  184. xorw %bx,%bx
  185. movw %es:start_sector(%di),%cx
  186. movb %es:start_head(%di),%dh
  187. movb $0x80,%dl
  188. movb $0x02,%ah
  189. int $0x13
  190. jc _failed
  191. __loaded:
  192. movw $_boot_msg_end-_boot_msg,%cx
  193. movw $_boot_msg,%si
  194. call _print_str
  195. /* check if it has a valid bootrecord. */
  196. cmpw $BOOTMAGIC,%es:510(,1)
  197. jne _failed
  198. call _print_nl
  199. /* call the payload. */
  200. pushl $0 /* No parameters to preserve for exit path */
  201. pushw $0 /* Use prefix exit path mechanism */
  202. jmp $LOADSEG,$ENTRYPOINT
  203. .section ".text16", "ax", @progbits
  204. .globl prefix_exit
  205. prefix_exit:
  206. int $0x19 /* should try to boot machine */
  207. .globl prefix_exit_end
  208. prefix_exit_end:
  209. .previous
  210. /*--------------------------------------------------------------*/
  211. _load_msg: .ascii "Loading "
  212. _load_msg_end:
  213. _part_msg: .ascii "partition "
  214. _part_msg_end:
  215. _boot_msg: .ascii "Booting..."
  216. _boot_msg_end:
  217. _stdbios_msg: .ascii "Std. BIOS\r\n"
  218. _stdbios_msg_end:
  219. _extbios_msg: .ascii "Ext. BIOS\r\n"
  220. _extbios_msg_end:
  221. _failed_msg: .ascii "FAILED!!!\r\n"
  222. _failed_msg_end:
  223. /*--------------------------------------------------------------*/
  224. _disk_address_packet:
  225. .byte 0x10 /* size of the packet */
  226. .byte 0 /* reserved */
  227. .word _verbatim_size_sct /* number of sectors to read */
  228. .word 0x0000 /* offset */
  229. .word LOADSEG /* segment of buffer */
  230. _bios_lba_low: .word 0
  231. _bios_lba_high: .word 0
  232. .word 0
  233. .word 0
  234. .rept 32
  235. .byte 0
  236. .endr
  237. /*--- Partition table ------------------------------------------*/
  238. .org 446, 0
  239. .rept 64
  240. .byte 0
  241. .endr
  242. /*--- Magic code -----------------------------------------------*/
  243. .org 510, 0
  244. .word BOOTMAGIC
  245. /*** END ********************************************************/
  246. #endif