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: http://viralpatel.ne..._bootloader.php
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.
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)
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:

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:

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.
Peace
~Bodom
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: http://viralpatel.ne..._bootloader.php
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] start: 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 .end: retn print_char: 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 retn ;data 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:

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:

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.
Peace
~Bodom
3 Comments On This Entry
Page 1 of 1

BerryBoy
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 ?
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 ?

8byte
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
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

Page 1 of 1
← April 2018 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Adminsitration
- Apple
- assembly
- atmel
- AVR
- avr-libc
- Bada
- beginner
- Blackberry
- boot
- C
- C#, .NET, and XNA
- C++
- ccleaner
- code
- defragmentation
- Desktop
- dynamic
- embedded
- FooBada
- foobar
- foobar2000
- Gnome
- high
- interrupts
- Java
- level
- Linux
- Math
- Matricies
- Matrix
- microcontroller
- nasm
- New
- Open Source
- Operating System
- Oracle
- oscilloscope
- Programming
- Python
- Random Computer Stuff
- Random Rants
- Repair
- Samsung
- sector
- serial
- speed
- Square
- static
- timer
- UART
- Ubuntu
- unix
- USART
- virtualbox
- Visual
- VOIP
- Windows
- Windows 7
- x86
My Blog Links
Recent Entries
-
-
-
Using Virtualbox as a bootloader testing environment
on Nov 13 2010 11:00 PM
-
-
Recent Comments
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)