.PHONY: disasm
disasm: $(bin)
- ndisasm -o 0x7c00 $< >dis
+ ndisasm -o 0x7c00 $< >dis1
+ ndisasm -o 0x80000 -e 512 $< >dis2
.bootsect : {
* (.bootsect);
}
+ _bootsect_end = .;
- . = 0x7e00;
+ /* load high out of the way, to allow stage2 to load the kernel low */
+ . = 0x80000;
_stage2_start = .;
- .text : {
+ _stage2_start_seg = _stage2_start >> 4;
+ .text : AT(_bootsect_end) {
* (.startup);
* (.text*);
}
}
.bss ALIGN(4) (NOLOAD): {
_bss_start = .;
+ _bss_start_off = _bss_start - _stage2_start;
* (.bss*);
* (COMMON);
. = ALIGN(4);
_bss_size = SIZEOF(.bss);
. = ALIGN(4);
_stage2_end = .;
+ _stage2_size = _stage2_end - _stage2_start;
};
bits 16
section .bootsect
-extern _stage2_start
-extern _stage2_end
+extern _stage2_start_seg
+extern _stage2_size
-stacktop equ 7b00h
boot_driveno equ 7b00h
num_read_tries equ 7b06h ; 2 bytes
sect_pending equ 7b08h ; 2 bytes
xor ax, ax
mov ds, ax
mov es, ax
- mov ss, ax
- mov gs, ax
- mov fs, ax
jmp 00:.setcs
.setcs:
- mov sp, stacktop
+ ; put the stack high
+ mov ax, 0x7f00
+ mov ss, ax
+ xor sp, sp
mov [boot_driveno], dl
; query sectors per track
mov word [num_heads], 2
.querydone:
-; load the rest of the code at 7e00h
- mov ax, _stage2_end
- sub ax, _stage2_start
+ ; load the rest of the code high
+ mov ax, _stage2_size
add ax, 511
mov cl, 9
shr ax, cl
inc ax
mov [sect_pending], ax
- mov ax, _stage2_start
- shr ax, 1
- shr ax, 1
- shr ax, 1
- shr ax, 1
- mov es, ax ; destination segment 7e0h to allow loading up to 64k
+ mov ax, _stage2_start_seg
+ mov es, ax ; destination segment
mov word [destptr], 0
mov word [start_sect], 1 ; start from sector 1 to skip boot sector
mov word [cur_cyl], 0
add [destptr], ax
jnz .rdloop
- ; loaded sucessfully, reset es back to 0 and jump
-.done: xor ax, ax
+ ; loaded sucessfully, load segment registers and jump
+.done: mov ax, _stage2_start_seg
+ mov ds, ax
mov es, ax
- jmp _stage2_start
+ push ax
+ xor ax, ax
+ push ax
+ retf
+
.fail: add sp, 2 ; clear num_sect off the stack
dec word [num_read_tries]
section .startup
extern bootmain
+extern _stage2_start_seg
+extern _bss_start_off
+extern _bss_size
global _start
_start:
; TODO floppy off if necessary
- ; TODO zero .bss
+
+ ; zero .bss
+ mov di, _bss_start_off
+ mov cx, _bss_size
+ shr cx, 1
+ rep stosw
+ xor ax, ax
+ mov es, ax
+
call bootmain
hang: hlt
jmp hang
-
; vi:set ts=8 sts=8 sw=8 ft=nasm: