nightloader/stage_1/start.asm

211 lines
3.3 KiB
NASM

section .text
org 0x7c00
start:
mov ax, 0
mov ds, ax
mov es, ax
mov gs, ax
mov fs, ax
mov ax, 0x7e00
mov ss, ax
mov sp, 256
mov bp, 256
mov si, dx ; save the origin disk number
mov ax, 0x7c00
add ax, 446
; ax now points to the start of the partition table
xor di, di
call initialize_video_mode
.search_partition:
mov bx, ax ; get the offset of the current entry
add bx, 4 ; go to the offset of the partition type
mov cl, [bx]
cmp cl, 0x9d
je .load_partition
inc di
; leave and print debug message if the last partition entry
; did not provide the wanted bootloader - partition
cmp di, 4
je .no_boot_partition
add ax, 16
jmp .search_partition
.load_partition:
; ax is the start of the bootloader's partition entry in the mbr
; read the chs-entry and push it onto the stack
; head
xor dx, dx
mov bx, ax
add bx, 1
mov dl, [bx]
push dx
; cylinder
xor dx, dx
mov bx, ax
add bx, 3
mov dh, [bx]
mov bx, ax
add bx, 2
mov dl, [bx]
shr dx, 6
push dx
; sector
xor dx, dx
mov bx, ax
add bx, 2
mov dl, [bx]
and dl, 0b00111111
push dx
; push the partition's length
mov bx, ax
add bx, 12
mov dx, [bx]
push dx ; lower part
mov bx, ax
add bx, 14
mov dx, [bx]
push dx ; higher part
; load it all and read from disk
mov bx, sp
add bx, 4
mov al, [bx] ; sector-count
add bx, 2
mov cl, [bx] ; start-sector
add bx, 2
mov ch, [bx] ; start-cylinder
mov dx, si ; drive
mov bx, 0x0800
mov es, bx ; output segment
mov bx, sp
add bx, 8
mov dh, [bx] ; start-head
call .write_hex
mov cl, 2
mov ah, 0x02
mov al, 1
mov bx, 0x0000 ; output address in es - segment
int 13h
mov cx, 8
mov si, 0x8000
mov dh, 12
mov dl, 32
call print_string
jmp .keepalive
.no_boot_partition:
mov cx, LEN_TEXT_NO_BOOT_PARTITION
mov si, error_texts.no_boot_partition
mov dh, 12
mov dl, 32
call print_string
.keepalive:
jmp .keepalive
.write_hex:
push bx
push cx
mov bx, error_texts.hextab
and cx, 0x0f
add bx, cx
mov cl, [bx]
mov bx, error_texts.hexbuf
inc bx
mov [bx], cl
mov bx, error_texts.hextab
and cx, 0xf0
shr cx, 4
add bx, cx
mov cl, [bx]
mov bx, error_texts.hexbuf
mov [bx], cl
mov cx, 2
mov si, error_texts.hexbuf
mov dh, 12
mov dl, 32
call print_string
pop cx
pop bx
ret
; %include "exfat.asm"
%include "gdt.asm"
%include "utility.asm"
LEN_TEXT_NO_BOOT_PARTITION equ 17
error_texts:
.no_boot_partition:
;db "NO_BOOT_PARTITION"
.hextab:
db "0123456789abcdef"
.hexbuf:
resb 4
times 446 - ($-$$) db 0
db 0x80
db 0, 2, 0
db 0x9d
db 0, 3, 0
dd 1
dd 1
times 510 - ($-$$) db 0
db 0x55
db 0xAA
db "################"