Subscribe to Bodom's Universe        RSS Feed

Using Virtualbox as a bootloader testing environment

Icon 3 Comments
I really need to start blogging about consistent topics... Oh well.

Over the past few weeks, I've been slowly starting to get back into programming in assembly, and today, I got to experience an actual required use for such a language. This would be the creation of a master boot record [MBR], or Boot Sector. This is a 512 byte long assembly program which is run when your device is accessed by the bios during system startup. (Other uses exist, this is just one example, but is the one I'll be concentrating on).

I'm going to make it clear now that this isn't any kind of programming tutorial. To learn all about the boot process, and how to write a boot record, take a look at this tutorial:

There are tons of other tutorials on the site as well. What I am going to give you is an alternative to the writer's approach to using a floppy disk to load your created boot loader.

This, if you haven't figured it out yet, is using virtualbox. There are a few things you will need to get started.

  • An Assembler, such as NASM (my preference) or MASM
  • Oracle Virtualbox
  • A Hex editor (like HxD, which is just amazing)

I'm going to assume that you have at least a basic idea of what hex is. Also, I'll be doing everything from a windows perspective. If you get confused through any of this, let me know, and I'll try to help as best I can. I do have a linux partition on this computer, so I can try troubleshooting this procedure for linux, but at the time of writing, this has only been done by me on windows.

After installing necessary software, boot up virtualbox, and create a new VM (just choose unknown or other for everything...)
Be sure to create a new FIXED SIZE hard drive. This is important. I have not tested this method with a dynamically growing hard drive, so just to be safe, you know?

You don't need a big one anyway. I made mine 30 MB, but 4 or 5 MB should be more than enough. (You're only using 512 bytes for a bootloader!)

Done that? great!

Now create the bootloader specified in the tutorial above, or use the code presented here, and assemble it into a bin file ( nasm -f bin -o boot.bin boot.asm is what I used)

[BITS 16]
[org 0x7C00]

    mov si,MSG
    call print_string
    jmp $

print_string:           ; Expects null terminated message in si
    mov al,[si]
    or al,al
    jz  .end
    inc si
    call print_char
    jmp print_string

    mov ah,0x0E         ; Specifies that we want to write a character to the screen
    mov bl,0x07         ; Specifies output text color.  Not required, but useful to know
    mov bh,0x00         ; Page number.  Leave this alone.
    int 0x10            ; Signal video interrupt to BIOS

    MSG db 'Hello World!',0x0A,0

TIMES 510 - ($ - $$) db 0
DW 0xAA55

Now, check the file size and make sure it's 512 bytes. If it isn't something's wrong, so try the other bootloader code (if you used mine, use the tutorial, and vice versa).

If you're all good, then pop open your hex editor.

Open up your harddrive *.vdi file that you made when you created your VM. This should be in C:\Users\<your user name>\.VirtualBox\HardDisks or something to that extent. Now go to the line starting at 0x150. Looks something like:

Posted Image

If you notice, I've circled the 4 bytes your interested in. This is going to tell you the Virtualbox header offset, and is also the starting position for your hard drive, and is thus the starting position for your bootloader.

So where is this? Well, this is in little endian, meaning that if you read 00 04 00 00, your location is 00 00 04 00, 0x0400. So, scroll on down to that line. Now, in a separate tab or window (I'm assuming you are using HxD) open up your boot.bin file, and select and copy it all. Then go back into the first file (the *.vdi) and select 0x400 to 0x5FF (either with the cursor or by hitting CTRL+E and entering the values in.) Then hit paste, either via the context menu, CTRL+P, whatever. You're a programmer. Figure it out.

When you paste it in, if you get a warning about spacing issues, you did it wrong, so hit no, and reselect the area.

Save the file and close it, then load up the virtual box VM. If all is well, you should see the words 'Hello World' printed out on your VM terminal:

Posted Image

Now, you can just modify your source, repeat the process, and create your own master boot records with no problem!

Possible steps from here is developing a better MBR with a full interface, or developing an application to do all this hex work for you. Or both.


3 Comments On This Entry

Page 1 of 1


14 December 2010 - 03:44 AM
Cool !! It's very interesting.

Can you tell me more...

- Where did you get information about Virtualbox .vdi file structure ? ( I want to know more about .vdi file )

- Is there any way to write the bootloader in higher level language such as C ?


14 December 2010 - 07:46 AM
I found the vdi file structure after a lot of looking around on Google. Check this out for more: http://forums.virtua...opic.php?t=8046

Writing a bootloader is definately possible in c (
I prefer to do it in assembly just because I can see what's going on better and how much of my 512 bytes of hard disk space I've used, but by all means, go to!


25 September 2014 - 01:53 AM
Hi there, nice blog, just an update the vdi has changed.

I found that on line 150 it now points to 200000, so if you want to copy
a bootloader into a vdi you have to paste between 200000 and 2001FF.

Hope this help for those playing with OS dev.. so much fun, now to read a file :whistling:
Page 1 of 1