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]