tabs/bootloader/main.asm

352 lines
5.1 KiB
NASM
Raw Normal View History

; DISK_ID EQU 0x80
2019-12-27 17:57:36 +01:00
2019-12-30 13:59:02 +01:00
KERNEL_START EQU 0x100000
ELF_START EQU 0x8000
2020-01-02 12:08:29 +01:00
NUM_SECTORS EQU 65
2019-12-30 16:02:43 +01:00
2019-12-28 11:46:56 +01:00
org 0x7C00
bits 16
jmp .start
2019-12-30 16:02:43 +01:00
2019-12-28 15:19:48 +01:00
[bits 16]
; Function: check_a20
;
; Returns: 0 in ax if the a20 line is disabled (memory wraps around)
; 1 in ax if the a20 line is enabled (memory does not wrap around)
check_a20:
cli
xor ax, ax ; ax = 0
mov es, ax
not ax ; ax = 0xFFFF
mov ds, ax
mov di, 0x0500
mov si, 0x0510
mov al, byte [es:di]
2019-12-30 16:46:57 +01:00
mov bx, ax
2019-12-28 15:19:48 +01:00
mov al, byte [ds:si]
mov byte [es:di], 0x00
mov byte [ds:si], 0xFF
cmp byte [es:di], 0xFF
mov byte [ds:si], al
2019-12-30 16:46:57 +01:00
mov ax, bx
2019-12-28 15:19:48 +01:00
mov byte [es:di], al
mov ax, 0
2019-12-30 16:46:57 +01:00
je .check_a20__exit
2019-12-28 15:19:48 +01:00
mov ax, 1
2019-12-30 16:46:57 +01:00
.check_a20__exit:
2019-12-28 15:19:48 +01:00
ret
;;; Print string at bx
2019-12-28 15:19:48 +01:00
puts:
.puts_loop:
mov al, [bx] ; load the character
cmp al, 0 ; check for null byte
je .end_puts_loop
mov ah, 0x0e
int 0x10 ; print charachter
inc bx
jmp .puts_loop
.end_puts_loop:
ret
;;; Print bl as hex
2019-12-28 15:19:48 +01:00
puthex:
; Print high bits
mov cl, bl
shr cl, 4
cmp cl, 9
jg .puthex_high_alpha
; It's < 10, print as digit
mov al, cl
add al, 0x30
mov ah, 0x0e
int 0x10
jmp .puthex_low
.puthex_high_alpha:
; It's >= 10, print as letter
mov al, cl
add al, 0x57 ; 0x57 = (0x61 - 10)
mov ah, 0x0e
int 0x10
.puthex_low:
; Print low bits
mov cl, bl
and cl, 0b1111
cmp cl, 9
jg .puthex_low_alpha
; It's < 10, print as digit
mov al, cl
add al, 0x30
mov ah, 0x0e
int 0x10
jmp .puthex_end
.puthex_low_alpha:
; It's >= 10, print as letter
mov al, cl
add al, 0x57 ; 0x57 = (0x61 - 10)
mov ah, 0x0e
int 0x10
.puthex_end:
ret
.start:
2020-01-02 12:00:50 +01:00
; set up stack
mov ax, 0
mov ss, ax
mov sp, 0x7C00
mov ds, ax
mov es, ax
2019-12-30 14:45:55 +01:00
; disable vga cursor
mov ah, 0x01
mov ch, 0b00100000
mov cl, 0b00000000
int 0x10
; reset disk system
mov ah, 0x00
; mov dl, DISK_ID
int 0x13
; print result on error
jnc .reset_disk_no_error
mov bl, ah
2019-12-28 15:19:48 +01:00
call puthex
.reset_disk_no_error:
2019-12-30 16:02:43 +01:00
mov cx, 2 ; start at sector 2 (skip bootloader)
2019-12-30 13:59:02 +01:00
mov bx, ELF_START ; write to ELF_START
2019-12-28 14:41:19 +01:00
.read_disk_loop:
2019-12-27 17:57:36 +01:00
; Read sector dl into memory
mov ah, 0x02
mov al, 0x01 ; number of sectors
mov ch, 0x00 ; cylinder number, low 8 bits
2019-12-27 17:57:36 +01:00
; mov cl, 0x01 ; cylinder number, high 2 bits + sector number (6 bits)
mov dh, 0x00 ; head number
; mov dl, DISK_ID ; drive number
int 0x13
2019-12-30 16:02:43 +01:00
; exit on error
2019-12-27 17:57:36 +01:00
jc .read_disk_error
2019-12-30 16:02:43 +01:00
cmp cx, NUM_SECTORS
jge .read_disk_end ; we have reached the sector limit, time to boot
2019-12-28 14:41:19 +01:00
inc cl ; next sector
add bx, 512 ; add 512 to data start so we don't overwrite sectors
2019-12-27 17:57:36 +01:00
jmp .read_disk_loop
2019-12-30 16:02:43 +01:00
2019-12-27 17:57:36 +01:00
.read_disk_error:
2019-12-30 16:02:43 +01:00
mov bl, ah
call puthex
.read_disk_end:
2019-12-28 15:19:48 +01:00
call check_a20
cmp ax, 1
je .a20_enabled
2020-01-02 12:00:50 +01:00
mov ah, 0x24 ; set a20 gate
mov al, 0x01 ; turn it on
int 0x15
jnc .a20_enabled
mov bl, ah
call puthex
2019-12-28 15:19:48 +01:00
jmp .end
.a20_enabled:
2019-12-28 16:44:11 +01:00
; disable interrupts
cli
2019-12-28 15:19:48 +01:00
2019-12-28 16:44:11 +01:00
; reset ds
xor ax, ax
mov ds, ax
; load the GDT
lgdt [.gdt_desc]
; switch to protected mode
mov eax, cr0
or eax, 1
mov cr0, eax
; jump to 32 bit code
jmp 0x08:.clear_cs
2019-12-28 16:44:11 +01:00
[BITS 32]
.clear_cs:
2019-12-28 16:44:11 +01:00
; set Data Segment, Stack Segment and other segments
mov ax, 0x10
2019-12-28 16:44:11 +01:00
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
2019-12-28 16:44:11 +01:00
; set up stack
mov esp, 0x090000
2019-12-30 13:59:02 +01:00
; parse elf file at ELF_START to KERNEL_START
2020-01-10 09:28:31 +01:00
; verify ELF header (scrapped for memory)
2019-12-30 13:59:02 +01:00
mov esi, ELF_START
2020-01-10 09:28:31 +01:00
; cmp dword [esi], 464C457Fh ; ELF magic
; jne .invalid_elf
; cmp word [esi+4], 0101h ; lsb 32 bit, little endian
; jne .invalid_elf
; cmp word [esi+18], 03 ; x86 architecture
; jne .invalid_elf
2019-12-30 13:59:02 +01:00
; read the entrypoint and store it
mov eax, dword [esi+0x18] ; program entry position
mov dword [.entrypoint], eax
2020-01-10 09:28:31 +01:00
mov ax, word [esi+0x2C] ; read phnum (number of program headers)
2019-12-30 13:59:02 +01:00
2020-01-10 09:28:31 +01:00
; move esi to the start of the program header
add esi, dword [esi+0x1C]
2019-12-30 13:59:02 +01:00
; set up for loop
sub esi, 0x20
2020-01-10 09:28:31 +01:00
inc ax
mov dword [.edi_backup], esi
2019-12-30 13:59:02 +01:00
.elf_ph_loop:
2020-01-10 09:28:31 +01:00
mov ebx, ELF_START
mov esi, dword [.edi_backup]
2019-12-30 13:59:02 +01:00
add esi, 0x20
2020-01-10 09:28:31 +01:00
mov dword [.edi_backup], esi
dec ax
jz .start_kernel ; there is no valid code block
2019-12-30 13:59:02 +01:00
cmp word [esi], 1 ; check if p_type is loadable
jne .elf_ph_loop
2020-01-10 09:28:31 +01:00
; set destination
mov edi, dword [esi+0x08]
2019-12-30 13:59:02 +01:00
; add offset to ebx (ebx = pointer to code)
add ebx, dword [esi+0x04]
; store p_filesz (ecx = size of the segment)
mov ecx, dword [esi+0x10]
2019-12-28 21:05:22 +01:00
2019-12-30 13:59:02 +01:00
; calculate size of bss (edx = size of bss)
mov edx, dword [esi+0x14]
sub edx, ecx
2019-12-28 17:25:29 +01:00
2019-12-30 13:59:02 +01:00
; check if ecx is zero
or ecx, ecx
jz .invalid_elf
; set source
mov esi, ebx
; repeat ecx/4 times (because it moves 4 bytes at a time)
shr ecx, 2
; copy
repnz movsd
2020-01-10 09:28:31 +01:00
or ax, ax
jnz .elf_ph_loop
.start_kernel
cmp edi, KERNEL_START
je .invalid_elf
2019-12-30 13:59:02 +01:00
; jump to start of kernel
jmp [.entrypoint]
2019-12-28 15:19:48 +01:00
.end:
cli
hlt
2019-12-28 15:19:48 +01:00
jmp .end
2019-12-30 13:59:02 +01:00
.invalid_elf:
mov byte [ds:0x0B8000], 'E'
mov byte [ds:0x0B8002], 'L'
mov byte [ds:0x0B8004], 'F'
jmp .end
2020-01-03 13:02:18 +01:00
; padding for bootloader - SFS superblock
times 404 - ($ - $$) db 0
; room for SFS superblock
times 42 db 0
; padding for SFS superblock - data
times 66 - (.file_end - .data) db 0
2019-12-28 15:19:48 +01:00
.data:
2019-12-28 16:44:11 +01:00
.gdt:
dd 0x00000000
dd 0x00000000
dw 0xffff
dw 0x0000
db 0x00
2020-01-10 10:05:19 +01:00
db 0b10011110
db 0b11001111
db 0x00
dw 0xffff
dw 0x0000
db 0x00
2020-01-10 10:05:19 +01:00
db 0b10010110
db 0b11001111
db 0x00
2019-12-28 16:44:11 +01:00
.gdt_end:
.gdt_desc:
dw .gdt_end - .gdt - 1
dd .gdt
2019-12-30 13:59:02 +01:00
.entrypoint: dd 0
2020-01-10 09:28:31 +01:00
.edi_backup: dd 0
2019-12-30 13:59:02 +01:00
2020-01-03 13:02:18 +01:00
; magic string
dw 0xAA55
2019-12-27 17:57:36 +01:00
2020-01-03 13:02:18 +01:00
.file_end: