| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*
- * VESA real-mode stage: runs at 0x7E00 before protected mode.
- *
- * Called by the boot sector in real mode (CS=0, IP=0x7E00).
- * Walks the VBE mode list from the BIOS, picks the best mode
- * (highest resolution, >= 16 bpp, LFB, fits in VRAM), writes
- * the mode info to 0x600, enables A20, enters PM, jumps to
- * kernel_main.
- *
- * If no VESA mode works, boots in text mode (valid=0 at 0x600).
- */
- .code16
- .section .text.stage2, "ax", @progbits
- .globl _vesa_stage
- /* E820 map constants (must match mm.h). */
- .equ E820_MAGIC, 0xE820E820
- .equ E820_BUF_END, 0x1008 + 32*24 /* 32 entries * 24 bytes */
- _vesa_stage:
- xorw %ax, %ax
- movw %ax, %ds
- movw %ax, %es
- /* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
- * Layout (see mm.h): [magic u32][count u32][24-byte entries...].
- * Stored below the kernel (0x7E00) so it survives kexec reloads,
- * which re-run kernel_main but not this real-mode stage. */
- movl $E820_MAGIC, 0x1000 /* magic */
- movl $0, 0x1004 /* count = 0 */
- xorl %ebx, %ebx /* continuation value */
- movl $0x1008, %edi /* ES:DI -> first entry */
- e820_loop:
- xorw %ax, %ax
- movw %ax, %es /* (re)set ES for the BIOS buffer */
- movl $0xE820, %eax
- movl $0x534D4150, %edx /* "SMAP" */
- movl $24, %ecx
- int $0x15
- jc e820_done /* CF set -> unsupported / done */
- cmpl $0x534D4150, %eax
- jne e820_done /* not "SMAP" -> done */
- cmpb $20, %cl
- jb e820_done /* too short -> stop */
- movl $0, 20(%edi) /* zero ACPI attrs (BIOS may write 20 B) */
- incl 0x1004 /* count++ */
- addl $24, %edi
- testl %ebx, %ebx
- jz e820_done /* continuation == 0 -> done */
- cmpl $E820_BUF_END, %edi
- jae e820_done /* buffer full -> stop */
- jmp e820_loop
- e820_done:
- /* ---- VBE Controller Info into 0:0x2000 ---- */
- movw $0x2000, %di
- movw $0x4256, (%di) /* "VB" */
- movw $0x3245, 2(%di) /* "E2" */
- movw $0x4F00, %ax
- int $0x10
- cmpw $0x004F, %ax
- jnz vesa_no_vbe
- /* Save VRAM size (offset 0x12: TotalMemory in 64KB blocks). */
- movw 0x12(%di), %ax
- testw %ax, %ax
- jnz 1f
- movw $0xFFFF, %ax
- 1: movw %ax, vram_64k
- /* Get mode list pointer (offset 0x0E: far pointer seg:off). */
- movw 0x200E, %si /* offset (buffer at 0x2000) */
- movw 0x2010, %ax /* segment */
- movw %ax, %fs /* FS:SI = mode list */
- /* ---- Walk mode list, pick best mode ---- */
- movw $0, best_mode
- movl $0, best_score
- walk_modes:
- movw %fs:(%si), %cx
- cmpw $0xFFFF, %cx
- jz walk_done
- addw $2, %si
- /* Query mode info: VBE 4F01h into 0:0x2200. */
- pushw %si
- pushw %cx
- xorw %ax, %ax
- movw %ax, %es
- movw $0x2200, %di
- movw $0x4F01, %ax
- int $0x10
- popw %cx
- popw %si
- cmpw $0x004F, %ax
- jnz walk_modes
- /* Check: supported (bit 0) + graphics (bit 4) + LFB (bit 7). */
- movw 0x2200, %ax
- andw $0x0091, %ax
- cmpw $0x0091, %ax
- jnz walk_modes
- /* Check: BitsPerPixel >= 16. */
- cmpb $16, 0x2219
- jb walk_modes
- /* TODO: Remove this width cap when display scaling is improved. */
- cmpw $1280, 0x2212
- ja walk_modes
- /* Check: fits in VRAM. */
- movw 0x2210, %ax
- mulw 0x2214
- testw %ax, %ax
- jz 2f
- incw %dx
- 2: cmpw vram_64k, %dx
- ja walk_modes
- /* Score = width * height + bpp. */
- movzwl 0x2212, %eax
- movzwl 0x2214, %edx
- imull %edx, %eax
- movzbl 0x2219, %edx
- addl %edx, %eax
- cmpl best_score, %eax
- jbe walk_modes
- movl %eax, best_score
- movw %cx, best_mode
- /* Save mode info block to 0:0x2400. */
- pushw %si
- pushw %cx
- movw $0x2200, %si
- movw $0x2400, %di
- movw $128, %cx
- cld
- rep movsw
- popw %cx
- popw %si
- jmp walk_modes
- walk_done:
- /* Did we find a mode? */
- movw best_mode, %cx
- testw %cx, %cx
- jz vesa_no_vbe
- /* ---- Set the mode ---- */
- movw %cx, %bx
- orw $0x4000, %bx /* LFB */
- movw $0x4F02, %ax
- int $0x10
- cmpw $0x004F, %ax
- jnz vesa_no_vbe
- /* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
- movw $0x600, %bx
- movb $1, (%bx)
- movw 0x2412, %ax
- movw %ax, 1(%bx) /* width */
- movw 0x2414, %ax
- movw %ax, 3(%bx) /* height */
- movw 0x2410, %ax
- movw %ax, 5(%bx) /* pitch */
- movb 0x2419, %al
- movb %al, 7(%bx) /* bpp */
- movw 0x2428, %ax
- movw %ax, 8(%bx) /* lfb_base lo */
- movw 0x242A, %ax
- movw %ax, 10(%bx) /* lfb_base hi */
- movw vram_64k, %ax
- movw %ax, 12(%bx) /* vram_64k */
- jmp enter_pm
- vesa_no_vbe:
- /* No VESA: stay in text mode, mark fb info invalid. */
- movw $0x600, %bx
- movb $0, (%bx)
- enter_pm:
- /* Enable A20 gate. */
- movw $0x2401, %ax
- int $0x15
- call a20_wait
- movb $0xD1, %al
- outb %al, $0x64
- call a20_wait
- movb $0xDF, %al
- outb %al, $0x60
- call a20_wait
- inb $0x92, %al
- testb $0x02, %al
- jnz 1f
- orb $0x02, %al
- andb $0xFE, %al
- outb %al, $0x92
- 1:
- cli
- lgdt stage2_gdtr
- movl %cr0, %eax
- orl $1, %eax
- movl %eax, %cr0
- .byte 0x66, 0xea
- .long stage2_pm
- .word 0x08
- a20_wait:
- inb $0x64, %al
- testb $0x02, %al
- jnz a20_wait
- ret
- .code32
- stage2_pm:
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
- movl $0x30800, %esp
- jmp kernel_main
- .align 4
- stage2_gdt:
- .quad 0
- .word 0xffff, 0x0000
- .byte 0x00, 0x9a, 0xcf, 0x00
- .word 0xffff, 0x0000
- .byte 0x00, 0x92, 0xcf, 0x00
- stage2_gdt_end:
- stage2_gdtr:
- .word stage2_gdt_end - stage2_gdt - 1
- .long stage2_gdt
- .align 2
- vram_64k:
- .word 0
- best_mode:
- .word 0
- best_score:
- .long 0
|