Currently I am working on a two-sector(VBR1 and VBR2) FAT32 bootloader.
Since LBABegin of the partition is 32, VBR1 is in sector 32 and VBR2 is in sector 34 on the pendrive
(there was some data in sector 33, and writing VBR2 there didn't work). VBR2 is loaded into the memory address right after VBR1 (VBR1+512).
I have some difficulties in calling the functions in VBR2.
Code: Select all
; Simplified code of VBR1:
bits 16
org 0
VBR1_SEG equ 0x07C0
VBR2_OFFSET equ 0x200 ; VBR2 will be loader to 0x7E00
VBR2_SECTNUM equ 32+2 ; partition_lba_begin+2, so the second one of the ReservedSectors
; Function-offsets in VBR2
READFILE_OFFS equ 0
CLUSTER2LBA_OFFS equ 2
jmp main ;(3 bytes)
BPB data
main:
; set segment registers to VBR1_SEG
; ...
; call a function in VBR2
xor ecx, ecx
mov cx, [VBR2_OFFSET+CLUSTER2LBA_OFFS] ; DS:....
call ecx
; ...
Code: Select all
; Simplified code of VBR2
bits 16
org 0x7E00
readfile_offs dw ReadFile ; address of Readfile used from VBR1
cluster2lba_offs dw Cluster2LBA ; address of Cluster2LBA used from VBR1
; IN: EBX(clusternum)
; OUT: EAX
Cluster2LBA:
; code for testing
mov ax, 0
mov ds, ax
mov ax,0xb800
mov gs, ax
mov BYTE [gs:80*24*2], byte 'C'
mov si, txtClus ; 'Clus', 0
call Print2
jmp $
; ...
in the function of VBR2 I have to set the segment registers (e.g. DS) to zero (and restore at the end), otherwise data-referencing won't work in VBR2 (instead of 'Clus', trash will be printed).
It's necessary to get the BPB-data in VBR1 from VBR2, so in that case I have to restore the segment registers.
I have tried to set the origin of VBR2 to 0 and to 0x200 ("org 0" and "org 0x200") and adjust ecx in "call ecx" accordingly, but the call never arrived in the Cluster2LBA in VBR2.
For example with "org 0" I did:
Code: Select all
xor edx, edx
mov dx, VBR1_SEG
shl edx, 4
xor ecx, ecx
mov cx, [VBR2_OFFSET+CLUSTER2LBA_OFFS]
add ecx, edx
call ecx
Of course "org" just adds its value to the address of the symbols.
In the beginning of VBR2, cluster2lba_offs was 0x00ED with "org 0" but 0x7EF3 with "org 0x7E00.
It works as it is now (org 0x7E00), but I believe it can be done in a more elegant way.
Any help appreciated.