redox/bootloader/x86_64/bootsector.asm

189 lines
3.3 KiB
NASM
Raw Normal View History

2016-08-14 02:21:46 +02:00
ORG 0x7C00
SECTION .text
USE16
boot: ; dl comes with disk
; initialize segment registers
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
; initialize stack
mov sp, 0x7C00
; initialize CS
push ax
push word .set_cs
retf
.set_cs:
; save disk number
2016-08-14 02:21:46 +02:00
mov [disk], dl
mov si, name
call print
call print_line
mov bx, (startup_start - boot) / 512
call print_num
call print_line
mov bx, startup_start
call print_num
call print_line
2016-08-14 02:21:46 +02:00
2016-09-30 16:55:29 +02:00
mov eax, (startup_start - boot) / 512
2016-08-14 02:21:46 +02:00
mov bx, startup_start
mov cx, (startup_end - startup_start) / 512
xor dx, dx
call load
mov si, finished
call print
call print_line
jmp startup
; load some sectors from disk to a buffer in memory
; buffer has to be below 1MiB
; IN
; ax: start sector
; bx: offset of buffer
; cx: number of sectors (512 Bytes each)
; dx: segment of buffer
; CLOBBER
; ax, bx, cx, dx, si
; TODO rewrite to (eventually) move larger parts at once
; if that is done increase buffer_size_sectors in startup-common to that (max 0x80000 - startup_end)
load:
cmp cx, 127
2016-08-14 02:21:46 +02:00
jbe .good_size
pusha
mov cx, 127
2016-08-14 02:21:46 +02:00
call load
popa
add ax, 127
add dx, 127 * 512 / 16
sub cx, 127
2016-08-14 02:21:46 +02:00
jmp load
.good_size:
2016-09-30 16:55:29 +02:00
mov [DAPACK.addr], eax
2016-08-14 02:21:46 +02:00
mov [DAPACK.buf], bx
mov [DAPACK.count], cx
mov [DAPACK.seg], dx
call print_dapack
2016-08-14 02:21:46 +02:00
mov dl, [disk]
mov si, DAPACK
mov ah, 0x42
int 0x13
jc error
ret
2016-10-19 01:04:06 +02:00
; store some sectors to disk from a buffer in memory
; buffer has to be below 1MiB
; IN
; ax: start sector
; bx: offset of buffer
; cx: number of sectors (512 Bytes each)
; dx: segment of buffer
; CLOBBER
; ax, bx, cx, dx, si
; TODO rewrite to (eventually) move larger parts at once
; if that is done increase buffer_size_sectors in startup-common to that (max 0x80000 - startup_end)
store:
cmp cx, 127
2016-10-19 01:04:06 +02:00
jbe .good_size
pusha
mov cx, 127
2016-10-19 01:04:06 +02:00
call store
popa
add ax, 127
add dx, 127 * 512 / 16
sub cx, 127
2016-10-19 01:04:06 +02:00
jmp store
.good_size:
mov [DAPACK.addr], eax
mov [DAPACK.buf], bx
mov [DAPACK.count], cx
mov [DAPACK.seg], dx
call print_dapack
mov dl, [disk]
mov si, DAPACK
mov ah, 0x43
int 0x13
jc error
ret
2016-10-19 01:04:06 +02:00
print_dapack:
mov bx, [DAPACK.addr + 2]
call print_num
2016-10-19 01:04:06 +02:00
mov bx, [DAPACK.addr]
call print_num
2016-10-19 01:04:06 +02:00
mov al, '#'
call print_char
2016-10-19 01:04:06 +02:00
mov bx, [DAPACK.count]
call print_num
2016-10-19 01:04:06 +02:00
mov al, ' '
call print_char
2016-10-19 01:04:06 +02:00
mov bx, [DAPACK.seg]
call print_num
2016-10-19 01:04:06 +02:00
mov al, ':'
call print_char
2016-10-19 01:04:06 +02:00
mov bx, [DAPACK.buf]
call print_num
2016-10-19 01:04:06 +02:00
jmp print_line
2016-10-19 01:04:06 +02:00
2016-08-14 02:21:46 +02:00
error:
2016-10-19 16:47:34 +02:00
mov bh, 0
mov bl, ah
call print_num
mov al, ' '
call print_char
2016-08-14 02:21:46 +02:00
mov si, errored
call print
call print_line
.halt:
cli
hlt
jmp .halt
%include "print16.asm"
2016-09-02 04:45:26 +02:00
name: db "Redox Loader - Stage One",0
2016-08-14 02:21:46 +02:00
errored: db "Could not read disk",0
2016-09-02 04:45:26 +02:00
finished: db "Redox Loader - Stage Two",0
2016-08-14 02:21:46 +02:00
disk: db 0
DAPACK:
db 0x10
db 0
.count: dw 0 ; int 13 resets this to # of blocks actually read/written
.buf: dw 0 ; memory buffer destination address (0:7c00)
.seg: dw 0 ; in memory page zero
2016-10-19 16:47:34 +02:00
.addr: dq 0 ; put the lba to read in this spot
2016-08-14 02:21:46 +02:00
times 510-($-$$) db 0
db 0x55
db 0xaa