more refactoring
This commit is contained in:
1 parent
909875ec5b
commit
2219fac4e5
30 files changed
+438
-1456
No files matched your search
+112
@@ -0,0 +1,112 @@
|
||||
[bits 16]
|
||||
|
||||
%include "fn.s"
|
||||
%include "ps2.s"
|
||||
|
||||
%macro mov_out 3
|
||||
mov %1, %3
|
||||
out %2, %1
|
||||
%endmacro
|
||||
|
||||
; Check whether the A20 line is enabled. Writes to the boot sector identifier.
|
||||
; Arguments: none
|
||||
; Return:
|
||||
; - ax: 0 if A20 disabled, nonzero if A20 enabled
|
||||
; Clobber: none
|
||||
test_a20:
|
||||
push bp
|
||||
mov bp, sp
|
||||
push gs
|
||||
|
||||
; Restore the boot sector identifier in case it was overwritten by anything.
|
||||
mov word [0x7dfe], 0xaa55
|
||||
|
||||
mov ax, 0xffff
|
||||
mov gs, ax
|
||||
xor ax, ax
|
||||
|
||||
; If the word at 0x107dfe (1 MiB after the boot sector identifier) is different to the boot
|
||||
; sector identifier, than A20 must be enabled.
|
||||
cmp word gs:[0x7e0e], 0xaa55
|
||||
setne al
|
||||
jne .return
|
||||
|
||||
; Even if A20 was enabled, the two words may have been equal by chance, so we temporarily swap
|
||||
; the boot sector identifier bytes and test again.
|
||||
ror word [0x7dfe], 8
|
||||
cmp word gs:[0x7e0e], 0x55aa
|
||||
setne al
|
||||
ror word [0x7dfe], 8
|
||||
jmp .return
|
||||
|
||||
.return:
|
||||
pop gs
|
||||
pop bp
|
||||
ret
|
||||
|
||||
global test_a20
|
||||
|
||||
|
||||
; Try to enable A20 using the Intel 8042 PS/2 keyboard controller.
|
||||
; Arguments: none
|
||||
; Return: none
|
||||
; Clobber: ax, cx, dx
|
||||
enable_a20_intel_8042:
|
||||
; Temporarily disable the keyboard.
|
||||
call intel_8042_wait_write
|
||||
mov_out al, INTEL_8042_OUT_CMD, INTEL_8042_CMD_PS2_1_DISABLE
|
||||
|
||||
; Read the controller output port.
|
||||
call intel_8042_wait_write
|
||||
mov_out al, INTEL_8042_OUT_CMD, INTEL_8042_CMD_CONTROLLER_OUT_PORT_READ
|
||||
call intel_8042_wait_read
|
||||
in al, INTEL_8042_IO_DATA
|
||||
|
||||
; The second bit is "A20 enabled", so set it.
|
||||
mov cl, al
|
||||
or cl, 2
|
||||
|
||||
; Write the modified byte back to the controller output port.
|
||||
call intel_8042_wait_write
|
||||
mov_out al, INTEL_8042_OUT_CMD, INTEL_8042_CMD_CONTROLLER_OUT_PORT_WRITE
|
||||
call intel_8042_wait_write
|
||||
mov_out al, INTEL_8042_IO_DATA, cl
|
||||
|
||||
; Re-enable the keyboard.
|
||||
call intel_8042_wait_write
|
||||
mov_out al, INTEL_8042_OUT_CMD, INTEL_8042_CMD_PS2_1_ENABLE
|
||||
|
||||
; Wait for writes to finish.
|
||||
call intel_8042_wait_write
|
||||
|
||||
ret
|
||||
|
||||
global enable_a20_intel_8042
|
||||
|
||||
|
||||
; Wait for the Intel 8042 input buffer to become empty, so we can write.
|
||||
; Arguments: none
|
||||
; Return: none
|
||||
; Clobber: al
|
||||
intel_8042_wait_write:
|
||||
.loop:
|
||||
; Read the 8042 status register.
|
||||
in al, INTEL_8042_IN_STATUS
|
||||
; Input buffer status flag set means the input buffer is full, so loop in this case.
|
||||
test al, INTEL_8042_STATUS_MASK_IBUF
|
||||
jnz .loop
|
||||
ret
|
||||
|
||||
|
||||
; Wait for the Intel 8042 output buffer to become filled, so we can read.
|
||||
; Arguments: none
|
||||
; Return: none
|
||||
; Clobber: al
|
||||
intel_8042_wait_read:
|
||||
.loop:
|
||||
; Read the 8042 status register.
|
||||
in al, INTEL_8042_IN_STATUS
|
||||
; Output buffer status flag unset means output buffer is empty, so loop in this case.
|
||||
test al, INTEL_8042_STATUS_MASK_OBUF
|
||||
jz .loop
|
||||
ret
|
||||
@@ -0,0 +1,43 @@
|
||||
OUTPUT_FORMAT("binary")
|
||||
|
||||
. = 0x8200;
|
||||
|
||||
SECTIONS {
|
||||
/* Prelude must come first so it's in the single sector loaded by stage 1. */
|
||||
.prelude : {
|
||||
*(.prelude)
|
||||
}
|
||||
|
||||
.text : {
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
}
|
||||
|
||||
.data : {
|
||||
*(.data)
|
||||
*(.data.*)
|
||||
}
|
||||
|
||||
.bss : {
|
||||
*(.bss)
|
||||
*(.bss.*)
|
||||
}
|
||||
|
||||
.rodata : {
|
||||
*(.rodata)
|
||||
*(.rodata.*)
|
||||
}
|
||||
|
||||
.magic : {
|
||||
/* Magic bytes stage 2 uses to make sure it's loaded the subsequent sectors correctly. */
|
||||
LONG(0x544e4150)
|
||||
}
|
||||
|
||||
s2_magic = ADDR(.magic);
|
||||
|
||||
/* Define a symbol for the total length of the binary, so the prelude knows how many blocks to
|
||||
* load from disk.
|
||||
*/
|
||||
s2_bin_len = . - 0x8200;
|
||||
s2_bin_sectors = (s2_bin_len + 511) / 512;
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
[bits 16]
|
||||
|
||||
%include "fn.s"
|
||||
%include "layout.s"
|
||||
%include "s2_fns.s"
|
||||
|
||||
extern test_a20
|
||||
extern enable_a20_intel_8042
|
||||
|
||||
s2_main:
|
||||
call test_a20
|
||||
test al, al
|
||||
jnz .a20_enabled
|
||||
|
||||
; Try to enable A20 using the Intel 8042 PS/2 keyboard controller.
|
||||
call enable_a20_intel_8042
|
||||
call test_a20
|
||||
test al, al
|
||||
jnz .a20_enabled
|
||||
|
||||
; TODO: try other methods first before we panic:
|
||||
; - [ ] BIOS interrupt
|
||||
; - [ ] Fast A20 enable
|
||||
jmp panic_simple
|
||||
|
||||
.a20_enabled:
|
||||
mov ax, 0x0003
|
||||
int 0x10
|
||||
|
||||
; Disable cursor
|
||||
mov ax, 0x0100
|
||||
mov cx, 0x3f00
|
||||
int 0x10
|
||||
|
||||
; Ensure interrupts are definitely disabled.
|
||||
cli
|
||||
|
||||
; Load our flat-address-space GDT.
|
||||
lgdt [gdt_flat_slice]
|
||||
|
||||
; Set the protected-mode bit in cr0.
|
||||
mov eax, cr0
|
||||
or al, 0x01
|
||||
mov cr0, eax
|
||||
|
||||
; Long jump to set the code segment to gdt_flat.segment_code, and to clear the instruction
|
||||
; pipeline.
|
||||
jmp GDT_FLAT_IDX_CODE_32:.protected_mode_32
|
||||
|
||||
[bits 32]
|
||||
.protected_mode_32:
|
||||
|
||||
; Set the data segments to gdt_flat.segment_data.
|
||||
mov eax, GDT_FLAT_IDX_DATA
|
||||
mov ds, eax
|
||||
mov es, eax
|
||||
mov fs, eax
|
||||
mov gs, eax
|
||||
mov ss, eax
|
||||
|
||||
; Reset the stack.
|
||||
; TODO: put the 32-bit stack somewhere else.
|
||||
mov ebp, REAL_STACK_BASE
|
||||
mov esp, ebp
|
||||
|
||||
;jmp _start
|
||||
mov eax, 0xcafebabe
|
||||
|
||||
.halt:
|
||||
hlt
|
||||
jmp .halt
|
||||
|
||||
; panic_simple_32:
|
||||
; mov word [0xb8000], 0x4f21
|
||||
; .halt:
|
||||
; hlt
|
||||
; jmp .halt
|
||||
|
||||
global s2_main
|
||||
|
||||
|
||||
section .s3_data
|
||||
|
||||
gdt_flat_slice:
|
||||
dw GDT_FLAT_LEN
|
||||
dd gdt_flat
|
||||
|
||||
global gdt_flat_slice
|
||||
|
||||
; Segment descriptor layout
|
||||
; | Range (bits) | Field |
|
||||
; |--------------|---------------|
|
||||
; | 0-16 | limit |
|
||||
; | 16-32 | base |
|
||||
; | 32-40 | base cont. |
|
||||
; | 40-48 | access |
|
||||
; | 48-52 | limit cont. |
|
||||
; | 52-56 | flags |
|
||||
; | 56-64 | base cont. |
|
||||
;
|
||||
; Flags
|
||||
; - 0: reserved
|
||||
; - 1: long-mode code segment
|
||||
; - 2: size
|
||||
; - unset: 16-bit
|
||||
; - set: 32-bit
|
||||
; - 3: granularity
|
||||
; - unset: limit is measured in bytes
|
||||
; - set: limit is measured in 4KiB pages
|
||||
;
|
||||
; Access
|
||||
; - 0: accessed
|
||||
; - unset: CPU will set it when the segment is accessed
|
||||
; - 1: readable / writable
|
||||
; - data segments: is segment writable (data segments are always readable)
|
||||
; - code segments: is segment readable (code segments are never writable)
|
||||
; - 2: direction / conforming
|
||||
; - data segments: whether segment grows down
|
||||
; - code segments: whether this can be executed from a lower-privilege ring
|
||||
; - 3: executable
|
||||
; - unset: this is a data segment
|
||||
; - set: this is a code segment
|
||||
; - 4: descriptor type
|
||||
; - unset: this is a task state segment
|
||||
; - set: this is a data or code segment
|
||||
; - 5-6: privilege level (ring number)
|
||||
; - 7: present (must be set)
|
||||
;
|
||||
|
||||
; FIXME: copy this to a fixed memory location
|
||||
align 8
|
||||
gdt_flat:
|
||||
; First GDT entry must be 0.
|
||||
dq 0
|
||||
|
||||
; 32-bit code segment.
|
||||
; Bytes 0x0000 - 0xffff.
|
||||
.segment_code_32:
|
||||
db 0xff, 0xff, \
|
||||
0x00, 0x00, \
|
||||
0x00, \
|
||||
10011011b, \
|
||||
01000000b, \
|
||||
0x00
|
||||
|
||||
; 16-bit code segment, to use if we want to switch back to real mode.
|
||||
; Bytes 0x0000 - 0xffff.
|
||||
.segment_code_16:
|
||||
db 0xff, 0xff, \
|
||||
0x00, 0x00, \
|
||||
0x00, \
|
||||
10011011b, \
|
||||
00000000b, \
|
||||
0x00
|
||||
|
||||
; Data segment.
|
||||
; Pages 0x000000 - 0x0fffff, which covers the entire 32-bit address space (start of 0xfffff-th page
|
||||
; is 0xfffff * 4096 = 0xfffff000, end of page exclusive is 0xfffff000 + 4096 = 0x100000000).
|
||||
.segment_data:
|
||||
db 0xff, 0xff, \
|
||||
0x00, 0x00, \
|
||||
0x00, \
|
||||
10010011b, \
|
||||
11001111b, \
|
||||
0x00
|
||||
|
||||
global gdt_flat
|
||||
|
||||
GDT_FLAT_LEN equ ($ - gdt_flat)
|
||||
|
||||
GDT_FLAT_IDX_CODE_32 equ (gdt_flat.segment_code_32 - gdt_flat)
|
||||
global GDT_FLAT_IDX_CODE_32
|
||||
|
||||
GDT_FLAT_IDX_CODE_16 equ (gdt_flat.segment_code_16 - gdt_flat)
|
||||
global GDT_FLAT_IDX_CODE_16
|
||||
|
||||
GDT_FLAT_IDX_DATA equ (gdt_flat.segment_data - gdt_flat)
|
||||
global GDT_FLAT_IDX_DATA
|
||||
@@ -0,0 +1,227 @@
|
||||
[bits 16]
|
||||
|
||||
%include "fn.s"
|
||||
%include "layout.s"
|
||||
%include "s1_vars.s"
|
||||
|
||||
extern s2_bin_len
|
||||
extern s2_bin_sectors
|
||||
extern s2_magic
|
||||
extern s2_main
|
||||
|
||||
%macro copy_stack_var_to_globals 2
|
||||
mov %1, [bp - %2]
|
||||
mov [REAL_GLOBALS + %2], %1
|
||||
%endmacro
|
||||
|
||||
|
||||
section .prelude
|
||||
|
||||
s2_data:
|
||||
.s3_bin_offset_sectors:
|
||||
dw 0
|
||||
.s3_bin_len_sectors:
|
||||
dw 0
|
||||
.padding:
|
||||
times (S2_DATA_LEN - 4) db 0
|
||||
|
||||
%if ($ - $$) != S2_DATA_LEN
|
||||
%error "incorrect prelude data size"
|
||||
%endif
|
||||
|
||||
; Load the rest of stage 2 into memory.
|
||||
prelude:
|
||||
; Now that we're not doing instruction byte golf like we were in stage 1, we can afford to move
|
||||
; the various stage 1 stack variables to the globals section.
|
||||
copy_stack_var_to_globals ax, BOOT_DRIVE
|
||||
copy_stack_var_to_globals ax, SECTORS_PER_TRACK
|
||||
copy_stack_var_to_globals ax, N_HEADS
|
||||
copy_stack_var_to_globals ax, GPT_ENTRIES_START_LBA
|
||||
copy_stack_var_to_globals ax, GPT_N_ENTRIES_16
|
||||
copy_stack_var_to_globals ax, GPT_SECTOR_STRIDE
|
||||
copy_stack_var_to_globals ax, GPT_BYTE_STRIDE
|
||||
copy_stack_var_to_globals ax, GPT_ENTRIES_PER_SECTOR
|
||||
copy_stack_var_to_globals ax, GPT_CURRENT_ENTRY_IDX
|
||||
copy_stack_var_to_globals ax, GPT_SECTOR_ENTRY_IDX
|
||||
copy_stack_var_to_globals ax, GPT_SECTORS_LOADED
|
||||
copy_stack_var_to_globals ax, GPT_CURRENT_LBA
|
||||
copy_stack_var_to_globals ax, STAGE_2_GPT_ENTRY_ADDR
|
||||
|
||||
; Reset the stack, now we've got everything we need from it.
|
||||
mov sp, bp
|
||||
|
||||
mov si, [REAL_GLOBALS + STAGE_2_GPT_ENTRY_ADDR]
|
||||
mov eax, [si + 0x20] ; Partition / boot1 start LBA lower
|
||||
mov ebx, [si + 0x24] ; Partition / boot1 start LBA upper
|
||||
mov ecx, [si + 0x28] ; Partition end LBA lower
|
||||
mov edx, [si + 0x32] ; Partition LBA upper
|
||||
|
||||
; Panic if the partition / boot1 starting LBA overflows 16 bits.
|
||||
or ebx, ebx
|
||||
jnz panic_simple
|
||||
ror eax, 16
|
||||
or ax, ax
|
||||
jnz panic_simple
|
||||
ror eax, 16
|
||||
|
||||
; Calculate the s2 end LBA and panic if it overflows 16 bits.
|
||||
; n.b. ebx is zero before this so both bx and ebx can be used as the s2 end LBA.
|
||||
mov bx, ax
|
||||
add bx, s2_bin_sectors
|
||||
jc panic_simple
|
||||
|
||||
; Panic if the s2 end LBA is after the partition end LBA.
|
||||
; If the upper 32 bits of the partition end LBA are nonzero, then it must be greater than our
|
||||
; 16-bit s2 end LBA.
|
||||
or edx, edx
|
||||
jnz .end_lba_ok
|
||||
; Compare the s2 end LBA to the lower 32 bits of the partition end LBA.
|
||||
cmp ebx, ecx
|
||||
ja panic_simple
|
||||
|
||||
.end_lba_ok:
|
||||
|
||||
; The first sector has already been loaded (we're running it right now!) so increment the
|
||||
; current LBA.
|
||||
inc ax
|
||||
push ax ; Current LBA
|
||||
push bx ; s2 end LBA
|
||||
mov ebx, S2_LOAD_ADDR + 512 ; Current sector load address
|
||||
|
||||
.load_loop:
|
||||
mov ax, [bp - 0x02] ; Load current LBA
|
||||
cmp word [bp - 0x04], ax ; Compare to s2 end LBA
|
||||
jb .load_done
|
||||
|
||||
mov ecx, ebx
|
||||
call read_sector
|
||||
jc panic_simple
|
||||
|
||||
add ebx, 512
|
||||
inc word [bp - 0x02]
|
||||
jmp .load_loop
|
||||
|
||||
.load_done:
|
||||
|
||||
; Check the magic bytes at the end of s2.
|
||||
push es
|
||||
mov ebx, s2_magic
|
||||
call addr32_to_addr16
|
||||
cmp dword es:[bx], S2_MAGIC
|
||||
pop es
|
||||
jne panic_simple
|
||||
|
||||
jmp s2_main
|
||||
|
||||
|
||||
; Converts a 32-bit address to a 16-bit sector and offset.
|
||||
; Arguments:
|
||||
; - ebx: 32-bit address
|
||||
; Return:
|
||||
; - es: 16-bit address segment (unchanged on failure)
|
||||
; - ebx: 16-bit address offset
|
||||
; - cf: unset on success, set on failure
|
||||
; Clobber: none
|
||||
addr32_to_addr16:
|
||||
fnstart
|
||||
push es
|
||||
push eax
|
||||
|
||||
mov eax, ebx
|
||||
; Divide addr by 16 and saturate to 16 bits to get the segment.
|
||||
shr eax, 4
|
||||
ror eax, 16
|
||||
or ax, ax
|
||||
jz .segment_ok
|
||||
mov eax, 0xffff0000
|
||||
.segment_ok:
|
||||
ror eax, 16
|
||||
mov es, ax
|
||||
|
||||
; Calculate offset = addr - (16 * segment), failing if the offset doesn't fit in 16 bits.
|
||||
shl eax, 4
|
||||
sub ebx, eax
|
||||
ror ebx, 16
|
||||
or bx, bx
|
||||
jnz .fail
|
||||
ror ebx, 16
|
||||
|
||||
pop eax
|
||||
add sp, 2 ; Discard the original es from the stack
|
||||
pop bp
|
||||
clc
|
||||
ret
|
||||
|
||||
.fail:
|
||||
pop eax
|
||||
pop es
|
||||
stc
|
||||
fnret
|
||||
|
||||
global addr32_to_addr16
|
||||
|
||||
|
||||
; Reads a single sector at the given LBA into memory.
|
||||
; Arguments:
|
||||
; - ax: start LBA
|
||||
; - ecx: address to read sector to
|
||||
; Return:
|
||||
; - cf: unset on success, set on failure
|
||||
; Clobber: eax, ecx, edx
|
||||
read_sector:
|
||||
; sector - 1 = LBA % sectors_per_track
|
||||
; temp = LBA / sectors_per_track
|
||||
; head = temp % n_heads
|
||||
; cylinder = temp / n_heads
|
||||
|
||||
fnstart
|
||||
push es
|
||||
push ebx
|
||||
|
||||
mov ebx, ecx
|
||||
call addr32_to_addr16
|
||||
jc .return
|
||||
|
||||
; Calculate sector and temp
|
||||
xor dx, dx
|
||||
; Divide by sectors per track. dx = mod (sector - 1), ax = div (temp)
|
||||
div word [REAL_GLOBALS + SECTORS_PER_TRACK]
|
||||
; Put the sector into cx (the bios call will use cl)
|
||||
mov cx, dx
|
||||
inc cx
|
||||
|
||||
; Calculate head and cylinder
|
||||
xor dx, dx
|
||||
; Divide by number of heads. dx = mod (head), ax = div (cylinder)
|
||||
div word [REAL_GLOBALS + N_HEADS]
|
||||
mov dh, dl
|
||||
mov ch, al
|
||||
|
||||
mov dl, byte [REAL_GLOBALS + BOOT_DRIVE]
|
||||
mov ah, 0x02
|
||||
mov al, 1
|
||||
; Read sector
|
||||
int 0x13
|
||||
|
||||
.return:
|
||||
pop ebx
|
||||
pop es
|
||||
fnret
|
||||
|
||||
global read_sector
|
||||
|
||||
|
||||
panic_simple:
|
||||
mov ax, 0x0003
|
||||
int 0x10
|
||||
mov word fs:[0x0000], 0x4f21
|
||||
.halt:
|
||||
hlt
|
||||
jmp .halt
|
||||
|
||||
global panic_simple
|
||||
|
||||
|
||||
%if ($ - $$) > 512
|
||||
%error "stage 2 exceeded sector size"
|
||||
%endif
|
||||
Reference in new issue
Block a user