boot.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Boot sector: load contiguous kernel sectors 1..N to 0x7E00, then PM. No FAT. */
  2. .code16
  3. .section .text
  4. .globl _start
  5. _start:
  6. .byte 0xEB, 0x3E, 0x90
  7. .space 0x3E - 3, 0
  8. .org 0x3E
  9. kernel_sectors:
  10. .word 1
  11. .org 0x40
  12. loader_start:
  13. xorw %ax, %ax
  14. movw %ax, %ds
  15. movw %ax, %ss
  16. movw $0x7c00, %sp
  17. movb %dl, boot_drive
  18. movw $0x0003, %ax
  19. int $0x10
  20. /* VESA: query VRAM size, then try mode list. Buffer at ES:DI = 0:0x6000. */
  21. xorw %ax, %ax
  22. movw %ax, %es
  23. movw $0x6000, %di
  24. /* Get VBE controller info (TotalMemory at offset 0x12, in 64KB blocks) */
  25. movw $0xFFFF, %si /* default: assume unlimited VRAM */
  26. movw $0x4256, (%di) /* "VB" */
  27. movw $0x3245, 2(%di) /* "E2" */
  28. movw $0x4F00, %ax
  29. int $0x10
  30. cmpw $0x004F, %ax
  31. jnz 4f
  32. movw 0x12(%di), %ax
  33. testw %ax, %ax
  34. jz 4f
  35. movw %ax, %si /* SI = VRAM in 64KB blocks */
  36. 4:
  37. /* Try modes: 0x11C..0x110 (descending res, >= 16bpp, must fit in VRAM). */
  38. movw $0x11C, %cx
  39. 1: movw $0x4F01, %ax
  40. int $0x10
  41. cmpw $0x004F, %ax
  42. jnz 2f
  43. cmpb $16, 0x19(%di) /* BitsPerPixel >= 16 */
  44. jb 2f
  45. testw $0x80, (%di) /* ModeAttributes bit 7 = LFB */
  46. jz 2f
  47. /* Check if pitch * height fits in VRAM */
  48. movw 0x10(%di), %ax /* BytesPerScanLine */
  49. mulw 0x14(%di) /* DX:AX = pitch * height */
  50. testw %ax, %ax
  51. jz 3f
  52. incw %dx /* round up partial 64KB block */
  53. 3: cmpw %si, %dx /* needed_64k > total_64k? */
  54. ja 2f /* skip: doesn't fit */
  55. movw %cx, %bx
  56. orw $0x4000, %bx /* Set mode with LFB */
  57. movw $0x4F02, %ax
  58. int $0x10
  59. cmpw $0x004F, %ax
  60. jz vesa_ok
  61. 2: cmpw $0x110, %cx
  62. jz vesa_fail
  63. subw $1, %cx /* 0x11C -> 0x11B -> 0x118 -> 0x114 */
  64. jmp 1b
  65. vesa_fail:
  66. jmp halt
  67. vesa_ok:
  68. /* Re-query mode info (CX still has mode number). ES:DI = 0:0x6000. */
  69. xorw %ax, %ax
  70. movw %ax, %es
  71. movw $0x6000, %di
  72. movw $0x4F01, %ax
  73. int $0x10
  74. movw $0x600, %bx
  75. movb $1, (%bx) /* valid = 1 */
  76. movw 0x12(%di), %ax
  77. movw %ax, 1(%bx) /* width */
  78. movw 0x14(%di), %ax
  79. movw %ax, 3(%bx) /* height */
  80. movw 0x10(%di), %ax
  81. movw %ax, 5(%bx) /* pitch */
  82. movb 0x19(%di), %al
  83. movb %al, 7(%bx) /* bpp */
  84. movw 0x28(%di), %ax
  85. movw %ax, 8(%bx) /* lfb_base lo */
  86. movw 0x2a(%di), %ax
  87. movw %ax, 10(%bx) /* lfb_base hi */
  88. movw %si, 12(%bx) /* vram_64k */
  89. load_kernel:
  90. /* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
  91. movw kernel_sectors, %cx
  92. movw $1, %si
  93. movw $0x07e0, %ax
  94. movw %ax, %es
  95. xorw %bx, %bx
  96. load_loop:
  97. cmpw $0, %cx
  98. jz load_done
  99. pushw %cx
  100. pushw %si
  101. movb boot_drive, %dl
  102. cmpb $0x80, %dl
  103. jb read_chs
  104. /* LBA read via INT 13h AH=42h. DAP at 0:0x500. */
  105. xorw %ax, %ax
  106. movw %ax, 0x50a
  107. movw %ax, 0x50c
  108. movw %ax, 0x50e
  109. movb $0x10, 0x500
  110. movb %al, 0x501
  111. incw %ax
  112. movw %ax, 0x502
  113. movw %bx, 0x504
  114. movw %es, 0x506
  115. movw %si, 0x508
  116. movw $0x500, %si
  117. movb $0x42, %ah
  118. jmp do_int13
  119. read_chs:
  120. movw %si, %ax
  121. xorw %dx, %dx
  122. movw $18, %di
  123. divw %di
  124. movb %dl, %cl
  125. incb %cl
  126. xorw %dx, %dx
  127. movw $2, %di
  128. divw %di
  129. movb %dl, %dh
  130. movb %al, %ch
  131. movb boot_drive, %dl
  132. movb $0x02, %ah
  133. movb $1, %al
  134. do_int13:
  135. int $0x13
  136. popw %si
  137. popw %cx
  138. jc halt
  139. addw $0x200, %bx
  140. jnc 1f
  141. movw %es, %ax
  142. addw $0x1000, %ax
  143. movw %ax, %es
  144. 1: incw %si
  145. decw %cx
  146. jnz load_loop
  147. load_done:
  148. movb boot_drive, %al
  149. movb %al, 0x600
  150. /* Enable A20 gate (address bit 20). Try three methods. */
  151. movw $0x2401, %ax /* Method 1: BIOS */
  152. int $0x15
  153. call a20_8042_wait /* Method 2: keyboard controller (8042) */
  154. movb $0xD1, %al
  155. outb %al, $0x64
  156. call a20_8042_wait
  157. movb $0xDF, %al
  158. outb %al, $0x60
  159. call a20_8042_wait
  160. inb $0x92, %al /* Method 3: fast A20 gate */
  161. testb $0x02, %al
  162. jnz 5f
  163. orb $0x02, %al
  164. andb $0xFE, %al
  165. outb %al, $0x92
  166. 5:
  167. cli
  168. lgdt gdtr
  169. movl %cr0, %eax
  170. orl $1, %eax
  171. movl %eax, %cr0
  172. .byte 0x66, 0xea
  173. .long pm_start
  174. .word 0x08
  175. boot_drive:
  176. .byte 0
  177. .align 2
  178. gdt:
  179. .quad 0
  180. .word 0xffff, 0x0000
  181. .byte 0x00, 0x9a, 0xcf, 0x00
  182. .word 0xffff, 0x0000
  183. .byte 0x00, 0x92, 0xcf, 0x00
  184. gdt_end:
  185. gdtr:
  186. .word gdt_end - gdt - 1
  187. .long gdt
  188. a20_8042_wait:
  189. inb $0x64, %al
  190. testb $0x02, %al
  191. jnz a20_8042_wait
  192. ret
  193. halt:
  194. cli
  195. 1: hlt
  196. jmp 1b
  197. .code32
  198. pm_start:
  199. movw $0x10, %ax
  200. movw %ax, %ds
  201. movw %ax, %es
  202. movw %ax, %fs
  203. movw %ax, %gs
  204. movw %ax, %ss
  205. movl $0x20800, %esp
  206. ljmp $0x08, $0x7e00
  207. .section .bootsig
  208. .byte 0x55, 0xaa