Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bootpart.S 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #define BOOT_SEG 0x07c0
  2. #define EXEC_SEG 0x0100
  3. #define STACK_SEG 0x0200
  4. #define STACK_SIZE 0x2000
  5. .text
  6. .arch i386
  7. .section ".prefix", "awx", @progbits
  8. .code16
  9. /*
  10. * Find active partition
  11. *
  12. * Parameters:
  13. * %dl : BIOS drive number
  14. * %bp : Active partition handler routine
  15. */
  16. find_active_partition:
  17. /* Set up stack at STACK_SEG:STACK_SIZE */
  18. movw $STACK_SEG, %ax
  19. movw %ax, %ss
  20. movw $STACK_SIZE, %sp
  21. /* Relocate self to EXEC_SEG */
  22. pushw $BOOT_SEG
  23. popw %ds
  24. pushw $EXEC_SEG
  25. popw %es
  26. xorw %si, %si
  27. xorw %di, %di
  28. movw $0x200, %cx
  29. rep movsb
  30. ljmp $EXEC_SEG, $1f
  31. 1: pushw %ds
  32. popw %es
  33. pushw %cs
  34. popw %ds
  35. /* Check for LBA extensions */
  36. movb $0x41, %ah
  37. movw $0x55aa, %bx
  38. stc
  39. int $0x13
  40. jc 1f
  41. cmpw $0xaa55, %bx
  42. jne 1f
  43. movw $read_lba, read_sectors
  44. 1:
  45. /* Read and process root partition table */
  46. xorb %dh, %dh
  47. movw $0x0001, %cx
  48. xorl %esi, %esi
  49. xorl %edi, %edi
  50. call process_table
  51. /* Print failure message */
  52. movw $10f, %si
  53. jmp boot_error
  54. 10: .asciz "Could not locate active partition\r\n"
  55. /*
  56. * Print failure message and boot next device
  57. *
  58. * Parameters:
  59. * %si : Failure string
  60. */
  61. boot_error:
  62. cld
  63. movw $0x0007, %bx
  64. movb $0x0e, %ah
  65. 1: lodsb
  66. testb %al, %al
  67. je 99f
  68. int $0x10
  69. jmp 1b
  70. 99: /* Boot next device */
  71. int $0x18
  72. /*
  73. * Process partition table
  74. *
  75. * Parameters:
  76. * %dl : BIOS drive number
  77. * %dh : Head
  78. * %cl : Sector (bits 0-5), high two bits of cylinder (bits 6-7)
  79. * %ch : Low eight bits of cylinder
  80. * %esi:%edi : LBA address
  81. * %bp : Active partition handler routine
  82. *
  83. * Returns:
  84. * CF set on error
  85. */
  86. process_table:
  87. pushal
  88. call read_boot_sector
  89. jc 99f
  90. movw $446, %bx
  91. 1: call process_partition
  92. addw $16, %bx
  93. cmpw $510, %bx
  94. jne 1b
  95. 99: popal
  96. ret
  97. /*
  98. * Process partition
  99. *
  100. * Parameters:
  101. * %dl : BIOS drive number
  102. * %dh : Head
  103. * %cl : Sector (bits 0-5), high two bits of cylinder (bits 6-7)
  104. * %ch : Low eight bits of cylinder
  105. * %esi:%edi : LBA address
  106. * %bx : Offset within partition table
  107. * %bp : Active partition handler routine
  108. */
  109. process_partition:
  110. pushal
  111. /* Load C/H/S values from partition entry */
  112. movb %es:1(%bx), %dh
  113. movw %es:2(%bx), %cx
  114. /* Update LBA address from partition entry */
  115. addl %es:8(%bx), %edi
  116. adcl $0, %esi
  117. /* Check active flag */
  118. testb $0x80, %es:(%bx)
  119. jz 1f
  120. call read_boot_sector
  121. jc 99f
  122. jmp *%bp
  123. 1: /* Check for extended partition */
  124. movb %es:4(%bx), %al
  125. cmpb $0x05, %al
  126. je 2f
  127. cmpb $0x0f, %al
  128. je 2f
  129. cmpb $0x85, %al
  130. jne 99f
  131. 2: call process_table
  132. 99: popal
  133. /* Reload original partition table */
  134. call read_boot_sector
  135. ret
  136. /*
  137. * Read single sector to %es:0000 and verify 0x55aa signature
  138. *
  139. * Parameters:
  140. * %dl : BIOS drive number
  141. * %dh : Head
  142. * %cl : Sector (bits 0-5), high two bits of cylinder (bits 6-7)
  143. * %ch : Low eight bits of cylinder
  144. * %esi:%edi : LBA address
  145. *
  146. * Returns:
  147. * CF set on error
  148. */
  149. read_boot_sector:
  150. pushw %ax
  151. movw $1, %ax
  152. call *read_sectors
  153. jc 99f
  154. cmpw $0xaa55, %es:(510)
  155. je 99f
  156. stc
  157. 99: popw %ax
  158. ret
  159. /*
  160. * Read sectors to %es:0000
  161. *
  162. * Parameters:
  163. * %dl : BIOS drive number
  164. * %dh : Head
  165. * %cl : Sector (bits 0-5), high two bits of cylinder (bits 6-7)
  166. * %ch : Low eight bits of cylinder
  167. * %esi:%edi : LBA address
  168. * %ax : Number of sectors (max 127)
  169. *
  170. * Returns:
  171. * CF set on error
  172. */
  173. read_sectors: .word read_chs
  174. read_chs:
  175. /* Read sectors using C/H/S address */
  176. pushal
  177. xorw %bx, %bx
  178. movb $0x02, %ah
  179. stc
  180. int $0x13
  181. sti
  182. popal
  183. ret
  184. read_lba:
  185. /* Read sectors using LBA address */
  186. pushal
  187. movw %ax, (lba_desc + 2)
  188. pushw %es
  189. popw (lba_desc + 6)
  190. movl %edi, (lba_desc + 8)
  191. movl %esi, (lba_desc + 12)
  192. movw $lba_desc, %si
  193. movb $0x42, %ah
  194. int $0x13
  195. popal
  196. ret
  197. lba_desc:
  198. .byte 0x10
  199. .byte 0
  200. .word 1
  201. .word 0x0000
  202. .word 0x0000
  203. .long 0, 0