| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /* Boot sector: load contiguous kernel sectors 1..N to 0x7E00, then PM. No FAT. */
- .code16
- .section .text
- .globl _start
- _start:
- .byte 0xEB, 0x3E, 0x90
- .space 0x3E - 3, 0
- .org 0x3E
- kernel_sectors:
- .word 1
- .org 0x40
- loader_start:
- xorw %ax, %ax
- movw %ax, %ds
- movw %ax, %ss
- movw $0x7c00, %sp
- movb %dl, boot_drive
- movw $0x0003, %ax
- int $0x10
- /* VESA: query VRAM size, then try mode list. Buffer at ES:DI = 0:0x6000. */
- xorw %ax, %ax
- movw %ax, %es
- movw $0x6000, %di
- /* Get VBE controller info (TotalMemory at offset 0x12, in 64KB blocks) */
- movw $0xFFFF, %si /* default: assume unlimited VRAM */
- movw $0x4256, (%di) /* "VB" */
- movw $0x3245, 2(%di) /* "E2" */
- movw $0x4F00, %ax
- int $0x10
- cmpw $0x004F, %ax
- jnz 4f
- movw 0x12(%di), %ax
- testw %ax, %ax
- jz 4f
- movw %ax, %si /* SI = VRAM in 64KB blocks */
- 4:
- /* Try modes: 0x11C..0x110 (descending res, >= 16bpp, must fit in VRAM). */
- movw $0x11C, %cx
- 1: movw $0x4F01, %ax
- int $0x10
- cmpw $0x004F, %ax
- jnz 2f
- cmpb $16, 0x19(%di) /* BitsPerPixel >= 16 */
- jb 2f
- testw $0x80, (%di) /* ModeAttributes bit 7 = LFB */
- jz 2f
- /* Check if pitch * height fits in VRAM */
- movw 0x10(%di), %ax /* BytesPerScanLine */
- mulw 0x14(%di) /* DX:AX = pitch * height */
- testw %ax, %ax
- jz 3f
- incw %dx /* round up partial 64KB block */
- 3: cmpw %si, %dx /* needed_64k > total_64k? */
- ja 2f /* skip: doesn't fit */
- movw %cx, %bx
- orw $0x4000, %bx /* Set mode with LFB */
- movw $0x4F02, %ax
- int $0x10
- cmpw $0x004F, %ax
- jz vesa_ok
- 2: cmpw $0x110, %cx
- jz vesa_fail
- subw $1, %cx /* 0x11C -> 0x11B -> 0x118 -> 0x114 */
- jmp 1b
- vesa_fail:
- jmp halt
- vesa_ok:
- /* Re-query mode info (CX still has mode number). ES:DI = 0:0x6000. */
- xorw %ax, %ax
- movw %ax, %es
- movw $0x6000, %di
- movw $0x4F01, %ax
- int $0x10
- movw $0x600, %bx
- movb $1, (%bx) /* valid = 1 */
- movw 0x12(%di), %ax
- movw %ax, 1(%bx) /* width */
- movw 0x14(%di), %ax
- movw %ax, 3(%bx) /* height */
- movw 0x10(%di), %ax
- movw %ax, 5(%bx) /* pitch */
- movb 0x19(%di), %al
- movb %al, 7(%bx) /* bpp */
- movw 0x28(%di), %ax
- movw %ax, 8(%bx) /* lfb_base lo */
- movw 0x2a(%di), %ax
- movw %ax, 10(%bx) /* lfb_base hi */
- movw %si, 12(%bx) /* vram_64k */
- load_kernel:
- /* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
- movw kernel_sectors, %cx
- movw $1, %si
- movw $0x07e0, %ax
- movw %ax, %es
- xorw %bx, %bx
- load_loop:
- cmpw $0, %cx
- jz load_done
- pushw %cx
- pushw %si
- movb boot_drive, %dl
- cmpb $0x80, %dl
- jb read_chs
- /* LBA read via INT 13h AH=42h. DAP at 0:0x500. */
- xorw %ax, %ax
- movw %ax, 0x50a
- movw %ax, 0x50c
- movw %ax, 0x50e
- movb $0x10, 0x500
- movb %al, 0x501
- incw %ax
- movw %ax, 0x502
- movw %bx, 0x504
- movw %es, 0x506
- movw %si, 0x508
- movw $0x500, %si
- movb $0x42, %ah
- jmp do_int13
- read_chs:
- movw %si, %ax
- xorw %dx, %dx
- movw $18, %di
- divw %di
- movb %dl, %cl
- incb %cl
- xorw %dx, %dx
- movw $2, %di
- divw %di
- movb %dl, %dh
- movb %al, %ch
- movb boot_drive, %dl
- movb $0x02, %ah
- movb $1, %al
- do_int13:
- int $0x13
- popw %si
- popw %cx
- jc halt
- addw $0x200, %bx
- jnc 1f
- movw %es, %ax
- addw $0x1000, %ax
- movw %ax, %es
- 1: incw %si
- decw %cx
- jnz load_loop
- load_done:
- movb boot_drive, %al
- movb %al, 0x600
- /* Enable A20 gate (address bit 20). Try three methods. */
- movw $0x2401, %ax /* Method 1: BIOS */
- int $0x15
- call a20_8042_wait /* Method 2: keyboard controller (8042) */
- movb $0xD1, %al
- outb %al, $0x64
- call a20_8042_wait
- movb $0xDF, %al
- outb %al, $0x60
- call a20_8042_wait
- inb $0x92, %al /* Method 3: fast A20 gate */
- testb $0x02, %al
- jnz 5f
- orb $0x02, %al
- andb $0xFE, %al
- outb %al, $0x92
- 5:
- cli
- lgdt gdtr
- movl %cr0, %eax
- orl $1, %eax
- movl %eax, %cr0
- .byte 0x66, 0xea
- .long pm_start
- .word 0x08
- boot_drive:
- .byte 0
- .align 2
- gdt:
- .quad 0
- .word 0xffff, 0x0000
- .byte 0x00, 0x9a, 0xcf, 0x00
- .word 0xffff, 0x0000
- .byte 0x00, 0x92, 0xcf, 0x00
- gdt_end:
- gdtr:
- .word gdt_end - gdt - 1
- .long gdt
- a20_8042_wait:
- inb $0x64, %al
- testb $0x02, %al
- jnz a20_8042_wait
- ret
- halt:
- cli
- 1: hlt
- jmp 1b
- .code32
- pm_start:
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
- movl $0x20800, %esp
- ljmp $0x08, $0x7e00
- .section .bootsig
- .byte 0x55, 0xaa
|