nightloader/stage_1/start.asm

211 lines
3.3 KiB
NASM
Raw Normal View History

2023-04-24 20:00:51 +00:00
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
2023-04-25 23:09:46 +00:00
mov si, dx ; save the origin disk number
2023-04-24 20:00:51 +00:00
mov ax, 0x7c00
add ax, 446
; ax now points to the start of the partition table
2023-04-25 23:09:46 +00:00
xor di, di
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
call initialize_video_mode
2023-04-24 20:00:51 +00:00
.search_partition:
2023-04-25 23:09:46 +00:00
mov bx, ax ; get the offset of the current entry
add bx, 4 ; go to the offset of the partition type
2023-04-24 20:00:51 +00:00
mov cl, [bx]
cmp cl, 0x9d
2023-04-25 23:09:46 +00:00
je .load_partition
2023-04-24 20:00:51 +00:00
inc di
2023-04-25 23:09:46 +00:00
; leave and print debug message if the last partition entry
; did not provide the wanted bootloader - partition
2023-04-24 20:00:51 +00:00
cmp di, 4
je .no_boot_partition
2023-04-25 23:09:46 +00:00
2023-04-24 20:00:51 +00:00
add ax, 16
2023-04-25 23:09:46 +00:00
jmp .search_partition
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
.load_partition:
; ax is the start of the bootloader's partition entry in the mbr
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
; read the chs-entry and push it onto the stack
; head
xor dx, dx
2023-04-24 20:00:51 +00:00
mov bx, ax
2023-04-25 23:09:46 +00:00
add bx, 1
mov dl, [bx]
push dx
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
; 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
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
; sector
xor dx, dx
2023-04-24 20:00:51 +00:00
mov bx, ax
2023-04-25 23:09:46 +00:00
add bx, 2
mov dl, [bx]
and dl, 0b00111111
push dx
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
; push the partition's length
mov bx, ax
add bx, 12
2023-04-24 20:00:51 +00:00
mov dx, [bx]
2023-04-25 23:09:46 +00:00
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
2023-04-24 20:00:51 +00:00
add bx, 2
2023-04-25 23:09:46 +00:00
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
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
mov cl, 2
mov ah, 0x02
mov al, 1
mov bx, 0x0000 ; output address in es - segment
int 13h
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
mov cx, 8
mov si, 0x8000
mov dh, 12
mov dl, 32
call print_string
jmp .keepalive
2023-04-24 20:00:51 +00:00
.no_boot_partition:
2023-04-25 23:09:46 +00:00
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
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
; %include "exfat.asm"
2023-04-24 20:00:51 +00:00
%include "gdt.asm"
%include "utility.asm"
2023-04-25 23:09:46 +00:00
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 "################"