Page 1 of 1

Insert boot loading code into MBR of virtual disk

Posted: Mon Apr 27, 2009 6:15 am
by prayeriz
Hi all, I have some problem with performing this operation.

I've done the following things:

dd if=/dev/zero of=disk.img bs=1024 count=100000
parted disk.img mklabel msdos
parted disk.img mkpartf primary fat32 0 70
parted disk.img set 1 boot on
fdisk -ul disk.img give me a correct output

So I have a virtual disk well formated (I think). And I want to put a code (a bootloader) compile with nasm -f bin stage1.S -o stage1 that fit in 446
bytes (with times 446-($-$$) db 144).

I put the stage1 file binary on the MBR code area with this command:
dd if=stage1 of=disk.img bs=446 count=1

And just after an fidsk -ul disk.img give me nothing, like if I broke the partition table.

The bootloader only works when stage1 is 512B with the aa55 signature:

dd if=/dev/zero of=disk.img bs=1024 count=100000
parted disk.img mklabel msdos
parted disk.img mkpartf primary fat32 0 70
parted disk.img set 1 boot on
dd if=stage1 of=disk.img bs=512 count=1

And the bootloader works, but I erase the partition table of the disk :(.
Am I doing something wrong.

Re: Insert boot loading code into MBR of virtual disk

Posted: Mon Apr 27, 2009 10:57 am
by quok
Parted won't set the boot signature if it isn't there. In fact, it shouldn't touch any bytes in the boot sector other than the partition table itself.

My MBR is the full 512 bytes, with boot signature, and a bunch of zeros for the partition table. I copy the full thing to a new disk image, then I partition the image. After that, if I have to copy a new MBR to an existing disk image, I copy just the first 446 bytes. That way the partition table does not get overridden. Also specify 'conv=notrunc' to make sure DD does not truncate your disk image.

So try changing your procedure to this:
dd if=/dev/zero of=disk.img bs=1024 count=100000
dd if=stage1 of=disk.img bs=512 count=1 conv=notrunc

parted disk.img mklabel msdos
parted disk.img mkpartf primary fat32 0 70
parted disk.img set 1 boot on

dd if=stage1 of=disk.img bs=446 count=1 conv=notrunc

Re: Insert boot loading code into MBR of virtual disk

Posted: Mon Apr 27, 2009 5:07 pm
by JohnnyTheDon
You could also try doing 'fdisk disk.img' and then running 'o'. That should setup a valid DOS MBR (including the signature), after which you can use 'dd if=stage1 of=disk.img bs=446 count=1 conv=notrunc' without messing stuff up.

Re: Insert boot loading code into MBR of virtual disk

Posted: Tue Apr 28, 2009 5:18 am
by prayeriz
quok wrote:Parted won't set the boot signature if it isn't there. In fact, it shouldn't touch any bytes in the boot sector other than the partition table itself.
I think it is the contrary, try:

dd if=/dev/zero of=disk.img bs=1024 count=100000
parted disk.img mklabel msdos
parted disk.img mkpartf primary fat32 0 70
parted disk.img set 1 boot on

and after extract the MBR by performing this:
dd if=disk.img of=dump.bin bs=512 count=1
ndisasm -b16 -o0x7C00 dump.bin > dump.S

See dump.S and you will see the 0xaa55 signature.
quok wrote: My MBR is the full 512 bytes, with boot signature, and a bunch of zeros for the partition table. I copy the full thing to a new disk image, then I partition the image. After that, if I have to copy a new MBR to an existing disk image, I copy just the first 446 bytes. That way the partition table does not get overridden. Also specify 'conv=notrunc' to make sure DD does not truncate your disk image.

So try changing your procedure to this:
dd if=/dev/zero of=disk.img bs=1024 count=100000
dd if=stage1 of=disk.img bs=512 count=1 conv=notrunc

parted disk.img mklabel msdos
parted disk.img mkpartf primary fat32 0 70
parted disk.img set 1 boot on

dd if=stage1 of=disk.img bs=446 count=1 conv=notrunc
With adding conv=notrunc, it works perfectly thanks ;). Before, dd was trunking the file when I try to add
446 bytes, so what was following (partition table + boot signature) was erase.

So there is my new script:
rm -rf disk.img
dd if=/dev/zero of=disk.img bs=1024 count=100000
dd if=stage1 of=disk.img bs=446 count=1 conv=notrunc
parted disk.img mklabel msdos
parted disk.img mkpartfs primary fat32 0 70
parted disk.img set 1 boot on

I have a stage2 that I want to insert in the reserved sectors, is there a tool that exist to perform it, or do
I have to write my own tool.

Re: Insert boot loading code into MBR of virtual disk

Posted: Wed Apr 29, 2009 8:35 am
by prayeriz
I'm facing now a new problem, I have now a virtual disk with a correct MBR (bootloader code & partition table), and I want to format
the first partition in FAT32. How could I do it ?

I tried to do this:
losetup -o 16384 /dev/loop0 disk.img
mkfs.vfat -F 32 /dev/loop0

mkfs.vfat 2.11 (12 Mar 2005)
Loop device does not match a floppy size, using default hd params

But without success :(

EDIT:
It works with a minimum disk size of 32 MB. I've succeed to mount /dev/loop0 in /mnt, and write and read.
losetup -o 16384 /dev/loop0 disk.img
mkfs.vfat -F 32 /dev/loop0
mkfs.vfat 2.11 (12 Mar 2005)
Loop device does not match a floppy size, using default hd params
mount /dev/loop0 /mnt
cd /mnt + write & read files
umount /mnt
losetup -d /dev/loop0