vesa_stage.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* TODO: Remove this width cap when display scaling is improved. */
  66. cmpw $1280, 0x2212
  67. ja walk_modes
  68. /* Check: fits in VRAM. */
  69. movw 0x2210, %ax
  70. mulw 0x2214
  71. testw %ax, %ax
  72. jz 2f
  73. incw %dx
  74. 2: cmpw vram_64k, %dx
  75. ja walk_modes
  76. /* Score = width * height + bpp. */
  77. movzwl 0x2212, %eax
  78. movzwl 0x2214, %edx
  79. imull %edx, %eax
  80. movzbl 0x2219, %edx
  81. addl %edx, %eax
  82. cmpl best_score, %eax
  83. jbe walk_modes
  84. movl %eax, best_score
  85. movw %cx, best_mode
  86. /* Save mode info block to 0:0x2400. */
  87. pushw %si
  88. pushw %cx
  89. movw $0x2200, %si
  90. movw $0x2400, %di
  91. movw $128, %cx
  92. cld
  93. rep movsw
  94. popw %cx
  95. popw %si
  96. jmp walk_modes
  97. walk_done:
  98. /* Did we find a mode? */
  99. movw best_mode, %cx
  100. testw %cx, %cx
  101. jz vesa_no_vbe
  102. /* ---- Set the mode ---- */
  103. movw %cx, %bx
  104. orw $0x4000, %bx /* LFB */
  105. movw $0x4F02, %ax
  106. int $0x10
  107. cmpw $0x004F, %ax
  108. jnz vesa_no_vbe
  109. /* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
  110. movw $0x600, %bx
  111. movb $1, (%bx)
  112. movw 0x2412, %ax
  113. movw %ax, 1(%bx) /* width */
  114. movw 0x2414, %ax
  115. movw %ax, 3(%bx) /* height */
  116. movw 0x2410, %ax
  117. movw %ax, 5(%bx) /* pitch */
  118. movb 0x2419, %al
  119. movb %al, 7(%bx) /* bpp */
  120. movw 0x2428, %ax
  121. movw %ax, 8(%bx) /* lfb_base lo */
  122. movw 0x242A, %ax
  123. movw %ax, 10(%bx) /* lfb_base hi */
  124. movw vram_64k, %ax
  125. movw %ax, 12(%bx) /* vram_64k */
  126. jmp enter_pm
  127. vesa_no_vbe:
  128. /* No VESA: stay in text mode, mark fb info invalid. */
  129. movw $0x600, %bx
  130. movb $0, (%bx)
  131. enter_pm:
  132. /* Enable A20 gate. */
  133. movw $0x2401, %ax
  134. int $0x15
  135. call a20_wait
  136. movb $0xD1, %al
  137. outb %al, $0x64
  138. call a20_wait
  139. movb $0xDF, %al
  140. outb %al, $0x60
  141. call a20_wait
  142. inb $0x92, %al
  143. testb $0x02, %al
  144. jnz 1f
  145. orb $0x02, %al
  146. andb $0xFE, %al
  147. outb %al, $0x92
  148. 1:
  149. cli
  150. lgdt stage2_gdtr
  151. movl %cr0, %eax
  152. orl $1, %eax
  153. movl %eax, %cr0
  154. .byte 0x66, 0xea
  155. .long stage2_pm
  156. .word 0x08
  157. a20_wait:
  158. inb $0x64, %al
  159. testb $0x02, %al
  160. jnz a20_wait
  161. ret
  162. .code32
  163. stage2_pm:
  164. movw $0x10, %ax
  165. movw %ax, %ds
  166. movw %ax, %es
  167. movw %ax, %fs
  168. movw %ax, %gs
  169. movw %ax, %ss
  170. movl $0x20800, %esp
  171. jmp kernel_main
  172. .align 4
  173. stage2_gdt:
  174. .quad 0
  175. .word 0xffff, 0x0000
  176. .byte 0x00, 0x9a, 0xcf, 0x00
  177. .word 0xffff, 0x0000
  178. .byte 0x00, 0x92, 0xcf, 0x00
  179. stage2_gdt_end:
  180. stage2_gdtr:
  181. .word stage2_gdt_end - stage2_gdt - 1
  182. .long stage2_gdt
  183. .align 2
  184. vram_64k:
  185. .word 0
  186. best_mode:
  187. .word 0
  188. best_score:
  189. .long 0