boot.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /* CHS from LBA in SI */
  102. movw %si, %ax
  103. xorw %dx, %dx
  104. movw $18, %di
  105. divw %di
  106. movb %dl, %cl
  107. incb %cl
  108. xorw %dx, %dx
  109. movw $2, %di
  110. divw %di
  111. movb %dl, %dh
  112. movb %al, %ch
  113. movb boot_drive, %dl
  114. movb $0x02, %ah
  115. movb $1, %al
  116. int $0x13
  117. popw %si
  118. popw %cx
  119. jc halt
  120. addw $0x200, %bx
  121. jnc 1f
  122. movw %es, %ax
  123. addw $0x1000, %ax
  124. movw %ax, %es
  125. 1: incw %si
  126. decw %cx
  127. jnz load_loop
  128. load_done:
  129. movw $0x7DFC, %bx
  130. movb boot_drive, %al
  131. movb %al, (%bx)
  132. /* Enable A20 gate (address bit 20). Try three methods. */
  133. movw $0x2401, %ax /* Method 1: BIOS */
  134. int $0x15
  135. call a20_8042_wait /* Method 2: keyboard controller (8042) */
  136. movb $0xD1, %al
  137. outb %al, $0x64
  138. call a20_8042_wait
  139. movb $0xDF, %al
  140. outb %al, $0x60
  141. call a20_8042_wait
  142. inb $0x92, %al /* Method 3: fast A20 gate */
  143. testb $0x02, %al
  144. jnz 5f
  145. orb $0x02, %al
  146. andb $0xFE, %al
  147. outb %al, $0x92
  148. 5:
  149. cli
  150. lgdt gdtr
  151. movl %cr0, %eax
  152. orl $1, %eax
  153. movl %eax, %cr0
  154. .byte 0x66, 0xea
  155. .long pm_start
  156. .word 0x08
  157. .align 4
  158. boot_drive:
  159. .byte 0
  160. .align 8
  161. gdt:
  162. .quad 0
  163. .word 0xffff, 0x0000
  164. .byte 0x00, 0x9a, 0xcf, 0x00
  165. .word 0xffff, 0x0000
  166. .byte 0x00, 0x92, 0xcf, 0x00
  167. gdt_end:
  168. gdtr:
  169. .word gdt_end - gdt - 1
  170. .long gdt
  171. a20_8042_wait:
  172. inb $0x64, %al
  173. testb $0x02, %al
  174. jnz a20_8042_wait
  175. ret
  176. halt:
  177. cli
  178. 1: hlt
  179. jmp 1b
  180. .code32
  181. pm_start:
  182. movw $0x10, %ax
  183. movw %ax, %ds
  184. movw %ax, %es
  185. movw %ax, %fs
  186. movw %ax, %gs
  187. movw %ax, %ss
  188. /* Stack above DMA buffer 0x20000-0x20200 so DMA does not overwrite stack */
  189. movl $0x20800, %esp
  190. movl $0x7e00, %eax
  191. jmp *%eax
  192. .section .bootsig
  193. .byte 0x55, 0xaa