1

I am trying to write a bootloader to a Virtual Floppy Drive inserted in a VirtualBox VM. Here is the following code that I have:

org     0x7c00              ; We are loaded by BIOS at 0x7C00

bits    16                  ; We are still in 16 bit Real Mode

Start: jmp loader
        
loader:

.Reset:
    mov     ah, 0x0                 ; reset floppy disk function
    mov     dl, 0x0                 ; drive 0 is floppy drive
    int     0x13                    ; call BIOS
    jc      .Reset                  ; If Carry Flag (CF) is set, there was an error. Try resetting again

.Load:
 
    mov     ax, 0x07e0              ; we are going to read sector to into address 0x07e0:0
    mov     es, ax
    xor     bx, bx
 
    mov     ah, 0x2             ; read floppy sector function
    mov     al, 0x1                 ; read 1 sector
    mov     ch, 0x1                 ; we are reading the second sector past us, so its still on track 1
    mov     cl, 0x2                 ; sector to read (The second sector)
    mov     dh, 0x0                 ; head number
    mov     dl, 0x0                 ; drive number. Remember Drive 0 is floppy drive.
    int     0x13                    ; call BIOS - Read the sector
    jc      .Load
    
 
    jmp     0x07e0:0x0000               ; jump to execute the sector!
    
times 510 - ($-$$) db 0             ; We have to be 512 bytes. Clear the rest of the bytes with 0

dw 0xAA55                   ; Boot Signiture

It is supposed to load a small program into memory that prints the letter 'A' on the screen using a BIOS interrupt. This program is located on the second sector of the floppy disk:

org 0x07e0

    xor     bx, bx      
    mov     ah, 0x0e
    mov     al, 'A'
    int     0x10
    cli
    hlt

Can anyone tell me why this is not loading? I have tried loading to another address in memory 0x1000, however, this does not work either. Are there certain areas in the virtual memory that are reserved by VirtualBox?

Thanks!!

H

*Edit:

I build my code using nasm for Windows (nasm -f bin -o bootS1.bin bootS1.asm), and then copy and paste the hex from the binary file onto the VFD image using a program called HxD which writes the raw hex data to the disk. The disk is then inserted into the VM and ran to simulate the boot process.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Harry Stout
  • 145
  • 1
  • 8
  • How do you assemble and run your code? Please provide the exact comments you type to make the disk image. – fuz Jan 15 '21 at 14:19
  • @fuz I've added an edit :) – Harry Stout Jan 15 '21 at 14:26
  • There's two files in your project but you only provided the instructions for one of them. – fuz Jan 15 '21 at 14:27
  • Note that your boot loader reads from floppy drive 0. So it'll only work if the code is loaded as that. Copying it a virtual hard drive won't work. – fuz Jan 15 '21 at 14:29
  • 1
    Oh, I see what you mean. I do the same process for the second file: nasm -f bin -o bootS2.bin bootS2.asm, and then I copy that hex to the second sector of the floppy using HxD. – Harry Stout Jan 15 '21 at 14:30
  • 1
    Also note that `org 0x07e0` is incorrect. The offset at which your payload is loaded is 0 in segment `0x07e0`. So it should be `org 0x0000` (or just no `org` directive at all). Given that you do not refer to any addresses in the payload, this does however not cause the problems you observe. – fuz Jan 15 '21 at 14:31
  • As for the bootloader reading from floppy drive 0: I'm not using a virtual hard drive, I am using a virtual floppy disk. – Harry Stout Jan 15 '21 at 14:33
  • 2
    On a side note I suggest you use Bochs for development, it has an internal debugger that comes pretty handy ;) – m0skit0 Jan 15 '21 at 14:46
  • @m0skit0 Oh cool thanks, I'll look into that! – Harry Stout Jan 15 '21 at 14:51

1 Answers1

6

You are reading from track 1 whereas the payload is on track 0. Remember: heads and tracks count from 0, sectors from 1.

Note that instead of hard-coding a disk number, you should use the number provided by the BIOS in dl.

Also note that org 0x07e0 is incorrect. The offset at which your payload is loaded is 0 in segment 0x07e0. So it should be org 0x0000 (or just no org directive at all). Given that you do not refer to any addresses in the payload, this does however not cause the problems you observe.

fuz
  • 88,405
  • 25
  • 200
  • 352