nightloader/stage_1/start.asm

124 lines
2.8 KiB
NASM
Raw Normal View History

2023-04-24 20:00:51 +00:00
STAGE_1_CODE_MEMORY_ADDRESS equ 0x7c00
STAGE_1_STACK_MEMORY_ADDRESS equ 0x07e0
NIGHTLOADER_PARTITION_TYPE equ 0x9d
ORIGINAL_PARTITION_TABLE_OFFSET equ 446
ORIGINAL_NUM_PARTITIONS_IN_TABLE equ 4
ORIGINAL_PARTITION_ENTRY_SIZE equ 16
BOOTABLE_BYTE_OFFSET equ 0
CHS_START_SECTOR_ADDRESS_OFFSET equ 1
PARTITION_TYPE_OFFSET equ 4
CHS_END_SECTOR_ADDRESS equ 5
FIRST_LBA_SECTOR_ADDR_OFFSET equ 8
LAST_LBA_SECTOR_ADDR_OFFSET equ 12
org STAGE_1_CODE_MEMORY_ADDRESS
bits 16
stage_0:
2023-04-24 20:00:51 +00:00
mov bx, STAGE_1_STACK_MEMORY_ADDRESS
mov ss, bx
mov bx, 0x0800
2023-04-24 20:00:51 +00:00
mov bp, 256
mov sp, 256
2023-04-24 20:00:51 +00:00
mov bx, 0x0000
mov ds, bx
mov es, bx
2023-04-25 23:09:46 +00:00
mov di, dx
2023-04-25 23:09:46 +00:00
; set the video mode correctly
xor ah, ah ; bios-function (set video mode)
mov al, 3 ; video mode with 80x25 characters
int 0x10
2023-04-25 23:09:46 +00:00
.search_partition:
2023-04-25 23:09:46 +00:00
mov si, STAGE_1_CODE_MEMORY_ADDRESS
add si, ORIGINAL_PARTITION_TABLE_OFFSET
mov cx, 0 ; offset of the current entry from the partition table start
2023-04-25 23:09:46 +00:00
2023-04-24 20:00:51 +00:00
.search_loop:
2023-04-25 23:09:46 +00:00
mov bx, si
add bx, cx
add bx, PARTITION_TYPE_OFFSET
2023-04-25 23:09:46 +00:00
mov al, [bx]
2023-04-25 23:09:46 +00:00
cmp al, NIGHTLOADER_PARTITION_TYPE
je .load_partition;
2023-04-25 23:09:46 +00:00
; go to the next entry
add cx, ORIGINAL_PARTITION_ENTRY_SIZE
2023-04-25 23:09:46 +00:00
; check if there are more entries
cmp dx, ORIGINAL_NUM_PARTITIONS_IN_TABLE * ORIGINAL_PARTITION_ENTRY_SIZE
jb .search_loop
jmp .no_boot_partition
2023-04-24 20:00:51 +00:00
.load_partition:
2023-04-24 20:00:51 +00:00
mov si, cx ; address of the partition table entry of the nightloader-partition
2023-04-24 20:00:51 +00:00
; TODO: Make this more flexible; that's urgent.
mov ah, 0x02 ; action (read from disk)
mov al, 8 ; num_sectors
mov ch, 0 ; cylinder
mov cl, 2 ; sector (starts at 1, max.: 63)
mov dx, di ; storage device id
mov dh, 0 ; head
mov bx, 0x0800
mov es, bx ; read destination segment
mov bx, 0x0000 ; read destination address
int 0x13
2023-04-25 23:09:46 +00:00
mov si, 0x8000
mov cx, 8
2023-04-25 23:09:46 +00:00
mov dh, 12
mov dl, 36
2023-04-25 23:09:46 +00:00
call print_string
; jmp .wait
2023-04-25 23:09:46 +00:00
jmp 0x0800:0x0000
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
.no_boot_partition:
2023-04-25 23:09:46 +00:00
mov si, errors.no_boot_partition
mov cx, 30
mov dl, 25
2023-04-25 23:09:46 +00:00
mov dh, 12
call print_string
.wait:
cli
hlt
2023-04-24 20:00:51 +00:00
2023-04-25 23:09:46 +00:00
MESSAGE_LENGTH_NO_BOOT_PARTITION equ 30
2023-04-25 23:09:46 +00:00
errors:
2023-04-25 23:09:46 +00:00
.no_boot_partition:
db "NO NIGHTLOADER-PARTITION FOUND"
.hex:
2023-04-25 23:09:46 +00:00
db "0123456789abcdef"
%include "utility.asm"
%include "fat32.asm"
%include "partition_table.asm"