vesa_stage.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * VESA real-mode stage: runs at 0x7E00 before protected mode.
  3. *
  4. * Called by the boot sector in real mode (CS=0, IP=0x7E00).
  5. * Walks the VBE mode list from the BIOS, picks the best mode
  6. * (highest resolution, >= 16 bpp, LFB, fits in VRAM), writes
  7. * the mode info to 0x600, enables A20, enters PM, jumps to
  8. * kernel_main.
  9. *
  10. * If no VESA mode works, boots in text mode (valid=0 at 0x600).
  11. */
  12. .code16
  13. .section .text.stage2, "ax", @progbits
  14. .globl _vesa_stage
  15. _vesa_stage:
  16. xorw %ax, %ax
  17. movw %ax, %ds
  18. movw %ax, %es
  19. /* ---- VBE Controller Info into 0:0x2000 ---- */
  20. movw $0x2000, %di
  21. movw $0x4256, (%di) /* "VB" */
  22. movw $0x3245, 2(%di) /* "E2" */
  23. movw $0x4F00, %ax
  24. int $0x10
  25. cmpw $0x004F, %ax
  26. jnz vesa_no_vbe
  27. /* Save VRAM size (offset 0x12: TotalMemory in 64KB blocks). */
  28. movw 0x12(%di), %ax
  29. testw %ax, %ax
  30. jnz 1f
  31. movw $0xFFFF, %ax
  32. 1: movw %ax, vram_64k
  33. /* Get mode list pointer (offset 0x0E: far pointer seg:off). */
  34. movw 0x200E, %si /* offset (buffer at 0x2000) */
  35. movw 0x2010, %ax /* segment */
  36. movw %ax, %fs /* FS:SI = mode list */
  37. /* ---- Walk mode list, pick best mode ---- */
  38. movw $0, best_mode
  39. movl $0, best_score
  40. walk_modes:
  41. movw %fs:(%si), %cx
  42. cmpw $0xFFFF, %cx
  43. jz walk_done
  44. addw $2, %si
  45. /* Query mode info: VBE 4F01h into 0:0x2200. */
  46. pushw %si
  47. pushw %cx
  48. xorw %ax, %ax
  49. movw %ax, %es
  50. movw $0x2200, %di
  51. movw $0x4F01, %ax
  52. int $0x10
  53. popw %cx
  54. popw %si
  55. cmpw $0x004F, %ax
  56. jnz walk_modes
  57. /* Check: supported (bit 0) + graphics (bit 4) + LFB (bit 7). */
  58. movw 0x2200, %ax
  59. andw $0x0091, %ax
  60. cmpw $0x0091, %ax
  61. jnz walk_modes
  62. /* Check: BitsPerPixel >= 16. */
  63. cmpb $16, 0x2219
  64. jb walk_modes
  65. /* Check: fits in VRAM. */
  66. movw 0x2210, %ax
  67. mulw 0x2214
  68. testw %ax, %ax
  69. jz 2f
  70. incw %dx
  71. 2: cmpw vram_64k, %dx
  72. ja walk_modes
  73. /* Score = width * height + bpp. */
  74. movzwl 0x2212, %eax
  75. movzwl 0x2214, %edx
  76. imull %edx, %eax
  77. movzbl 0x2219, %edx
  78. addl %edx, %eax
  79. cmpl best_score, %eax
  80. jbe walk_modes
  81. movl %eax, best_score
  82. movw %cx, best_mode
  83. /* Save mode info block to 0:0x2400. */
  84. pushw %si
  85. pushw %cx
  86. movw $0x2200, %si
  87. movw $0x2400, %di
  88. movw $128, %cx
  89. cld
  90. rep movsw
  91. popw %cx
  92. popw %si
  93. jmp walk_modes
  94. walk_done:
  95. /* Did we find a mode? */
  96. movw best_mode, %cx
  97. testw %cx, %cx
  98. jz vesa_no_vbe
  99. /* ---- Set the mode ---- */
  100. movw %cx, %bx
  101. orw $0x4000, %bx /* LFB */
  102. movw $0x4F02, %ax
  103. int $0x10
  104. cmpw $0x004F, %ax
  105. jnz vesa_no_vbe
  106. /* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
  107. movw $0x600, %bx
  108. movb $1, (%bx)
  109. movw 0x2412, %ax
  110. movw %ax, 1(%bx) /* width */
  111. movw 0x2414, %ax
  112. movw %ax, 3(%bx) /* height */
  113. movw 0x2410, %ax
  114. movw %ax, 5(%bx) /* pitch */
  115. movb 0x2419, %al
  116. movb %al, 7(%bx) /* bpp */
  117. movw 0x2428, %ax
  118. movw %ax, 8(%bx) /* lfb_base lo */
  119. movw 0x242A, %ax
  120. movw %ax, 10(%bx) /* lfb_base hi */
  121. movw vram_64k, %ax
  122. movw %ax, 12(%bx) /* vram_64k */
  123. jmp enter_pm
  124. vesa_no_vbe:
  125. /* No VESA: stay in text mode, mark fb info invalid. */
  126. movw $0x600, %bx
  127. movb $0, (%bx)
  128. enter_pm:
  129. /* Enable A20 gate. */
  130. movw $0x2401, %ax
  131. int $0x15
  132. call a20_wait
  133. movb $0xD1, %al
  134. outb %al, $0x64
  135. call a20_wait
  136. movb $0xDF, %al
  137. outb %al, $0x60
  138. call a20_wait
  139. inb $0x92, %al
  140. testb $0x02, %al
  141. jnz 1f
  142. orb $0x02, %al
  143. andb $0xFE, %al
  144. outb %al, $0x92
  145. 1:
  146. cli
  147. lgdt stage2_gdtr
  148. movl %cr0, %eax
  149. orl $1, %eax
  150. movl %eax, %cr0
  151. .byte 0x66, 0xea
  152. .long stage2_pm
  153. .word 0x08
  154. a20_wait:
  155. inb $0x64, %al
  156. testb $0x02, %al
  157. jnz a20_wait
  158. ret
  159. .code32
  160. stage2_pm:
  161. movw $0x10, %ax
  162. movw %ax, %ds
  163. movw %ax, %es
  164. movw %ax, %fs
  165. movw %ax, %gs
  166. movw %ax, %ss
  167. movl $0x20800, %esp
  168. jmp kernel_main
  169. .align 4
  170. stage2_gdt:
  171. .quad 0
  172. .word 0xffff, 0x0000
  173. .byte 0x00, 0x9a, 0xcf, 0x00
  174. .word 0xffff, 0x0000
  175. .byte 0x00, 0x92, 0xcf, 0x00
  176. stage2_gdt_end:
  177. stage2_gdtr:
  178. .word stage2_gdt_end - stage2_gdt - 1
  179. .long stage2_gdt
  180. .align 2
  181. vram_64k:
  182. .word 0
  183. best_mode:
  184. .word 0
  185. best_score:
  186. .long 0