124 lines
2.8 KiB
NASM
124 lines
2.8 KiB
NASM
|
|
|
|
|
|
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:
|
|
|
|
mov bx, STAGE_1_STACK_MEMORY_ADDRESS
|
|
mov ss, bx
|
|
mov bx, 0x0800
|
|
mov bp, 256
|
|
mov sp, 256
|
|
|
|
mov bx, 0x0000
|
|
mov ds, bx
|
|
mov es, bx
|
|
|
|
mov di, dx
|
|
|
|
; set the video mode correctly
|
|
xor ah, ah ; bios-function (set video mode)
|
|
mov al, 3 ; video mode with 80x25 characters
|
|
int 0x10
|
|
|
|
|
|
|
|
.search_partition:
|
|
|
|
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
|
|
|
|
|
|
|
|
.search_loop:
|
|
|
|
mov bx, si
|
|
add bx, cx
|
|
add bx, PARTITION_TYPE_OFFSET
|
|
|
|
mov al, [bx]
|
|
|
|
cmp al, NIGHTLOADER_PARTITION_TYPE
|
|
je .load_partition;
|
|
|
|
; go to the next entry
|
|
add cx, ORIGINAL_PARTITION_ENTRY_SIZE
|
|
|
|
; check if there are more entries
|
|
cmp dx, ORIGINAL_NUM_PARTITIONS_IN_TABLE * ORIGINAL_PARTITION_ENTRY_SIZE
|
|
jb .search_loop
|
|
jmp .no_boot_partition
|
|
|
|
|
|
|
|
.load_partition:
|
|
|
|
mov si, cx ; address of the partition table entry of the nightloader-partition
|
|
|
|
; 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
|
|
|
|
mov si, 0x8000
|
|
mov cx, 8
|
|
mov dh, 12
|
|
mov dl, 36
|
|
call print_string
|
|
; jmp .wait
|
|
|
|
jmp 0x0800:0x0000
|
|
|
|
|
|
|
|
|
|
.no_boot_partition:
|
|
|
|
mov si, errors.no_boot_partition
|
|
mov cx, 30
|
|
mov dl, 25
|
|
mov dh, 12
|
|
call print_string
|
|
|
|
.wait:
|
|
cli
|
|
hlt
|
|
|
|
|
|
MESSAGE_LENGTH_NO_BOOT_PARTITION equ 30
|
|
|
|
errors:
|
|
.no_boot_partition:
|
|
db "NO NIGHTLOADER-PARTITION FOUND"
|
|
.hex:
|
|
db "0123456789abcdef"
|
|
|
|
%include "utility.asm"
|
|
%include "fat32.asm"
|
|
%include "partition_table.asm"
|