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.
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.
That sounds fascinating.
I've written to the first 512 bytes of a floppy lots of times back in the day. I'd like to know it on a deeper level.
Roadkil's Sector Editor does it, it letse you open the first 512 bytes on screen, and save it to a file, and open a file with the first 512 bytes, and save that to a floppy.
http://www.roadkil.net/program.php?ProgramID=24
Funnily enough there's a classic website about booting things, by somebody with a similar name to you, starman.. http://starman.vertcomp.com/asm/mbr/ Though floppies are non partitioned media and thus don't have an MBR.
I'm sure i've saved the first 512 bytes from e.g. a Windows 98 floppy, which had said Starting Windows 98 then gone to a C prompt. And it can be changed to e.g. a Windows 95 floppy. You know XP can have a 3 file boot disk if there is a problem with one of 3 core files.. Well, that has a distinct boot sector. IT's not a dos boot disk. I recall that the format command in XP is different from the one in 98. The 98 one was like DOS one it had a format /s to make a system disk. The XP I think couldn't really.. And I notice Win7 format command can't either. Though in XP or 7 I think you can make a dos boot disk from the GUI by ticking a box after right clicking A in 'my computer'. Another thing you could use is the *nix style dd command. or ddrescue(which gives some more info than dd). A similar program is Bart's BBIE, which can take the bootable part of a CD and extract it. Nero was(and perhaps still is), able to take the boot sector of a floppy, 512 bytes, and create a CD based on it. So if you had a bootable DOS disk, you could make a bootable DOS CD. It had an option both to let you browse to the image with that boot record, or to just put the floppy in and let it extract it.
To write a file into another file, you can write a program. Following snippet is in C.
char buf[512];
int floppy_desc, file_desc;
file_desc = open("xx.bin", O_RDONLY);
read(file_desc, buf, 512);
close(file_desc);
floppy_desc = open("floppy.img", O_RDWR);
lseek(floppy_desc, 512, SEEK_SET);
write(floppy_desc, buf, 512);
close(floppy_desc);
来源:https://stackoverflow.com/questions/32893607/how-do-i-write-a-bin-file-512-bytes-to-the-first-sector-sector-0-of-a-floppy