vesa_stage.S 5.5 KB

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