vesa_stage.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /* E820 map constants (must match mm.h). */
  16. .equ E820_MAGIC, 0xE820E820
  17. .equ E820_BUF_END, 0x1008 + 32*24 /* 32 entries * 24 bytes */
  18. _vesa_stage:
  19. xorw %ax, %ax
  20. movw %ax, %ds
  21. movw %ax, %es
  22. /* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
  23. * Layout (see mm.h): [magic u32][count u32][24-byte entries...].
  24. * Stored below the kernel (0x7E00) so it survives kexec reloads,
  25. * which re-run kernel_main but not this real-mode stage. */
  26. movl $E820_MAGIC, 0x1000 /* magic */
  27. movl $0, 0x1004 /* count = 0 */
  28. xorl %ebx, %ebx /* continuation value */
  29. movl $0x1008, %edi /* ES:DI -> first entry */
  30. e820_loop:
  31. xorw %ax, %ax
  32. movw %ax, %es /* (re)set ES for the BIOS buffer */
  33. movl $0xE820, %eax
  34. movl $0x534D4150, %edx /* "SMAP" */
  35. movl $24, %ecx
  36. int $0x15
  37. jc e820_done /* CF set -> unsupported / done */
  38. cmpl $0x534D4150, %eax
  39. jne e820_done /* not "SMAP" -> done */
  40. cmpb $20, %cl
  41. jb e820_done /* too short -> stop */
  42. movl $0, 20(%edi) /* zero ACPI attrs (BIOS may write 20 B) */
  43. incl 0x1004 /* count++ */
  44. addl $24, %edi
  45. testl %ebx, %ebx
  46. jz e820_done /* continuation == 0 -> done */
  47. cmpl $E820_BUF_END, %edi
  48. jae e820_done /* buffer full -> stop */
  49. jmp e820_loop
  50. e820_done:
  51. /* ---- VBE Controller Info into 0:0x2000 ---- */
  52. movw $0x2000, %di
  53. movw $0x4256, (%di) /* "VB" */
  54. movw $0x3245, 2(%di) /* "E2" */
  55. movw $0x4F00, %ax
  56. int $0x10
  57. cmpw $0x004F, %ax
  58. jnz vesa_no_vbe
  59. /* Save VRAM size (offset 0x12: TotalMemory in 64KB blocks). */
  60. movw 0x12(%di), %ax
  61. testw %ax, %ax
  62. jnz 1f
  63. movw $0xFFFF, %ax
  64. 1: movw %ax, vram_64k
  65. /* Get mode list pointer (offset 0x0E: far pointer seg:off). */
  66. movw 0x200E, %si /* offset (buffer at 0x2000) */
  67. movw 0x2010, %ax /* segment */
  68. movw %ax, %fs /* FS:SI = mode list */
  69. /* ---- Walk mode list, pick best mode ---- */
  70. movw $0, best_mode
  71. movl $0, best_score
  72. walk_modes:
  73. movw %fs:(%si), %cx
  74. cmpw $0xFFFF, %cx
  75. jz walk_done
  76. addw $2, %si
  77. /* Query mode info: VBE 4F01h into 0:0x2200. */
  78. pushw %si
  79. pushw %cx
  80. xorw %ax, %ax
  81. movw %ax, %es
  82. movw $0x2200, %di
  83. movw $0x4F01, %ax
  84. int $0x10
  85. popw %cx
  86. popw %si
  87. cmpw $0x004F, %ax
  88. jnz walk_modes
  89. /* Check: supported (bit 0) + graphics (bit 4) + LFB (bit 7). */
  90. movw 0x2200, %ax
  91. andw $0x0091, %ax
  92. cmpw $0x0091, %ax
  93. jnz walk_modes
  94. /* Check: BitsPerPixel >= 16. */
  95. cmpb $16, 0x2219
  96. jb walk_modes
  97. /* TODO: Remove this width cap when display scaling is improved. */
  98. cmpw $1280, 0x2212
  99. ja walk_modes
  100. /* Check: fits in VRAM. */
  101. movw 0x2210, %ax
  102. mulw 0x2214
  103. testw %ax, %ax
  104. jz 2f
  105. incw %dx
  106. 2: cmpw vram_64k, %dx
  107. ja walk_modes
  108. /* Score = width * height + bpp. */
  109. movzwl 0x2212, %eax
  110. movzwl 0x2214, %edx
  111. imull %edx, %eax
  112. movzbl 0x2219, %edx
  113. addl %edx, %eax
  114. cmpl best_score, %eax
  115. jbe walk_modes
  116. movl %eax, best_score
  117. movw %cx, best_mode
  118. /* Save mode info block to 0:0x2400. */
  119. pushw %si
  120. pushw %cx
  121. movw $0x2200, %si
  122. movw $0x2400, %di
  123. movw $128, %cx
  124. cld
  125. rep movsw
  126. popw %cx
  127. popw %si
  128. jmp walk_modes
  129. walk_done:
  130. /* Did we find a mode? */
  131. movw best_mode, %cx
  132. testw %cx, %cx
  133. jz vesa_no_vbe
  134. /* ---- Set the mode ---- */
  135. movw %cx, %bx
  136. orw $0x4000, %bx /* LFB */
  137. movw $0x4F02, %ax
  138. int $0x10
  139. cmpw $0x004F, %ax
  140. jnz vesa_no_vbe
  141. /* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
  142. movw $0x600, %bx
  143. movb $1, (%bx)
  144. movw 0x2412, %ax
  145. movw %ax, 1(%bx) /* width */
  146. movw 0x2414, %ax
  147. movw %ax, 3(%bx) /* height */
  148. movw 0x2410, %ax
  149. movw %ax, 5(%bx) /* pitch */
  150. movb 0x2419, %al
  151. movb %al, 7(%bx) /* bpp */
  152. movw 0x2428, %ax
  153. movw %ax, 8(%bx) /* lfb_base lo */
  154. movw 0x242A, %ax
  155. movw %ax, 10(%bx) /* lfb_base hi */
  156. movw vram_64k, %ax
  157. movw %ax, 12(%bx) /* vram_64k */
  158. jmp enter_pm
  159. vesa_no_vbe:
  160. /* No VESA: stay in text mode, mark fb info invalid. */
  161. movw $0x600, %bx
  162. movb $0, (%bx)
  163. enter_pm:
  164. /* Enable A20 gate. */
  165. movw $0x2401, %ax
  166. int $0x15
  167. call a20_wait
  168. movb $0xD1, %al
  169. outb %al, $0x64
  170. call a20_wait
  171. movb $0xDF, %al
  172. outb %al, $0x60
  173. call a20_wait
  174. inb $0x92, %al
  175. testb $0x02, %al
  176. jnz 1f
  177. orb $0x02, %al
  178. andb $0xFE, %al
  179. outb %al, $0x92
  180. 1:
  181. cli
  182. lgdt stage2_gdtr
  183. movl %cr0, %eax
  184. orl $1, %eax
  185. movl %eax, %cr0
  186. .byte 0x66, 0xea
  187. .long stage2_pm
  188. .word 0x08
  189. a20_wait:
  190. inb $0x64, %al
  191. testb $0x02, %al
  192. jnz a20_wait
  193. ret
  194. .code32
  195. stage2_pm:
  196. movw $0x10, %ax
  197. movw %ax, %ds
  198. movw %ax, %es
  199. movw %ax, %fs
  200. movw %ax, %gs
  201. movw %ax, %ss
  202. movl $0x40800, %esp
  203. jmp kernel_main
  204. .align 4
  205. stage2_gdt:
  206. .quad 0
  207. .word 0xffff, 0x0000
  208. .byte 0x00, 0x9a, 0xcf, 0x00
  209. .word 0xffff, 0x0000
  210. .byte 0x00, 0x92, 0xcf, 0x00
  211. stage2_gdt_end:
  212. stage2_gdtr:
  213. .word stage2_gdt_end - stage2_gdt - 1
  214. .long stage2_gdt
  215. .align 2
  216. vram_64k:
  217. .word 0
  218. best_mode:
  219. .word 0
  220. best_score:
  221. .long 0