How to write bootloader to MBR of HDD ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
dnyaneshgate
Posts: 5
Joined: Fri Sep 23, 2011 3:15 pm

How to write bootloader to MBR of HDD ?

Post by dnyaneshgate »

Hello All,
Recently I have started with writting bootloader from scratch. I am following osdev.wiki to write bootloader. But I donnt want to use floppy any more for running and testing my bootloader. I want to install it on hdd image.

So that I am following this tutorial http://www.adayinthelifeof.nl/2011/10/1 ... sk-images/ to create a hdd image, but I dont know how can I write my bootloader on MBR of hdd.

This is my hdd image configuration:
Disk gboot.img: 33 MB, 33546240 bytes
16 heads, 63 sectors/track, 65 cylinders, total 65520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5ace641b

Device Boot Start End Blocks Id System
gboot.img1 * 63 12287 6112+ 83 Linux
gboot.img2 * 12288 16383 2048 6 FAT16
gboot.img3 16384 65519 24568 5 Extended
gboot.img5 18432 59391 20480 83 Linux
gboot.img6 61440 65519 2040 b W95 FAT32
Now I wanted to know that how can I install my bootloader on MBR of HDD? or can I install bootloader on any one of the prtition partition and how?

Any help will be appreciated.

Thank You.
AndyB
Posts: 15
Joined: Fri Dec 17, 2010 5:43 pm

Re: How to write bootloader to MBR of HDD ?

Post by AndyB »

Its very similar to writing to a floppy disk image, I tend to use a HDD image rather than a physical HDD but either way you can just replace any references to an image file to a physical disk.

First step is to create the disk image (approx 500MB in my case):

Code: Select all

dd if=/dev/zero of=./hdd.img bs=516096c count=1000
Next step is to partition the image. I just use a single Fat32 (0c) partition. The echos just automate the input for fdisk.

Code: Select all

(echo o; echo n; echo p; echo 1; echo ; echo; echo t; echo 0c; echo a; echo 1; echo p; echo w) | fdisk -u -C1000 -S63 -H16 ./hdd.img
Then you can write your MBR to the image.

Code: Select all

dd if=./mbr of=./hdd.img bs=446 count=1 conv=notrunc
Note that you are limited to only 446 bytes due to the partition table. (No need to worry about the boot signature as fdisk takes care of that during partition.

The MBR on a HDD typically just detects the active partition and loads the first sector (VBR) and jumps to it.
To write your VBR to the first sector of the fat32 partition is a bit more of a pain because you must create a loopback for the partition.

Once its on a loopback you have to format as fat32 and write the VBR. I write the VBR in two stages, the first three bytes (jmp) and then I skip the size of the BPB (90) and then I continue writing. That way I dont trash the BPB and I have labels in my VBR so that I can reference the values.

Code: Select all

sudo losetup -o1048576 /dev/loop0 ./hdd.img
sudo mkfs.vfat -F 32 /dev/loop0
sudo dd if=./vbr of=/dev/loop0 bs=1 count=3 conv=notrunc
sudo dd if=./vbr of=/dev/loop0 bs=1 skip=90 seek=90 count=934 conv=notrunc
sudo losetup -d /dev/loop0
The 1048576 is an offset into the image file to where the partition starts.
You may need to change the count for the second write to 422. I use 934 as my VBR is two sectors in size.

Hope this is useful.

AndyB
dnyaneshgate
Posts: 5
Joined: Fri Sep 23, 2011 3:15 pm

Re: How to write bootloader to MBR of HDD ?

Post by dnyaneshgate »

Hi,

Thank you for your reply.

Code: Select all

dd if=./mbr of=./hdd.img bs=446 count=1 conv=notrunc
This line helps me. I was using bs=512, so that is why it was creating a problem for me.

Thank You so much.
AndyB
Posts: 15
Joined: Fri Dec 17, 2010 5:43 pm

Re: How to write bootloader to MBR of HDD ?

Post by AndyB »

Ah yes, you would have been over writing the partition table.

Glad the information was useful!

AndyB
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How to write bootloader to MBR of HDD ?

Post by BMW »

dnyaneshgate wrote:Hello All,
Recently I have started with writting bootloader from scratch. I am following osdev.wiki to write bootloader. But I donnt want to use floppy any more for running and testing my bootloader. I want to install it on hdd image.

So that I am following this tutorial http://www.adayinthelifeof.nl/2011/10/1 ... sk-images/ to create a hdd image, but I dont know how can I write my bootloader on MBR of hdd.

This is my hdd image configuration:
Disk gboot.img: 33 MB, 33546240 bytes
16 heads, 63 sectors/track, 65 cylinders, total 65520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5ace641b

Device Boot Start End Blocks Id System
gboot.img1 * 63 12287 6112+ 83 Linux
gboot.img2 * 12288 16383 2048 6 FAT16
gboot.img3 16384 65519 24568 5 Extended
gboot.img5 18432 59391 20480 83 Linux
gboot.img6 61440 65519 2040 b W95 FAT32
Now I wanted to know that how can I install my bootloader on MBR of HDD? or can I install bootloader on any one of the prtition partition and how?

Any help will be appreciated.

Thank You.
one way is to get HxD hex editor, you can open the drive and paste your bootloader into MBR
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Post Reply