Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a bin file (512 bytes) to the first sector (sector 0) of a floppy disk?

How do I write a .bin file to be in the first sector of a floppy disk/virtual floppy disk/floppy image?

I'm trying to boot a simple 512-byte bootloader. The size on everywhere says "512 bytes" so I should be good already.

Additional Information:

The bootloader simply displays a string, and I'm learning simple assembly. Some of the work is made in Windows and some in Ubuntu 14.04 (Trusty Tahr) (if this matters).

It doesn't boot even though it has the bootloader sign.

like image 259
Star OS Avatar asked Oct 01 '15 17:10

Star OS


1 Answers

If you are on Linux you can do it with DD utility. There is a version of DD for Microsoft Windows as well.


General DD usage

If you wish to make a zero filled virtual disk image the size of a 720K floppy you can use dd like this:

dd if=/dev/zero of=disk.img bs=1024 count=720

This would create a file called disk.img that is 1024*720 = 737280 bytes in size. A 1.44MB floppy image that is zero filled can be created with:

dd if=/dev/zero of=disk.img bs=1024 count=1440

Writing a binary image to a virtual floppy starting at the beginning of the image can be done like this:

dd if=bootload.bin of=disk.img conv=notrunc 

This example take the file bootload.bin and places it at the beginning of the disk image (called disk.img in this case) without truncation (conv=notrunc) If you don't use conv=notrunc on a virtual disk image it will write bootload.bin and truncate disk image to the size of the bootloader.


DD also has the ability to write to specific parts of a a disk image by jumping to a point other than the beginning of the disk. This is useful if you need to place information (code/data) in a particular sector. This example could be used to place the second stage of a boot loader after the first 512 byte sector of the disk image:

dd if=stage2.bin of=disk.img bs=512 seek=1 conv=notrunc

bs=512 sets the block size to 512 (makes it easier since it is the typical size of most floppy disk sector). seek=1 seeks to the first block (512 bytes) past the beginning of the image and then writes the file stage2.bin . We need conv=notrunc again because we don't want DD to truncate the disk image at the point where stage2.bin ends.

dd if=stage2.bin of=disk.img bs=512 seek=18 conv=notrunc

This example is similar to the last but it skips over 9216 bytes (512*18) before writing stage2.bin


If you have a floppy attached to a Linux system (and root access) you can write the bootloader with something like

dd if=bootload.bin of=/dev/fd0 

where /dev/fd0 is the device for your floppy. /dev/fd0 is generally floppy disk A (if present) and /dev/fd1 is floppy disk B (if present).


DD for Windows

If you are running on Microsoft Windows there is a version of the DD utility available here . The latest download is dd-0.6beta3.zip and is the minimum recommended version. It has some features older ones didn't. Just open the zip file and extract it to a place on your Windows path.

like image 148
Michael Petch Avatar answered Sep 19 '22 04:09

Michael Petch