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

4

9

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.

Andonis answered 1/10, 2015 at 17:40 Comment(6)
Probably i'm ghost-posting somehow or people don't care about me anymore which is sad.Andonis
ANSWER IT. JUST ANSWER IT. YES YOU SHOULD.Andonis
you're asking a very advanced question.. most techies won't have a clue, and the question (like many questions on here) has had 27 views... like 1027 views. Often the most widely searched questions get the most views.. but most people aren't searching this.. it's very niche and old techPoultice
Also i'm intersted in what learning material you are using.. 'cos i'm interested in learning that kind of low level stuff tooPoultice
Thanks! I'll write to the first bytes of my USB and boot it.Andonis
Michael, change that comment to a question.Andonis
T
21

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.

Tetreault answered 5/12, 2015 at 17:46 Comment(4)
You mean conv=notrunc. I assume conv=noconv is a typo, not something that some other dd version accepts.Sibella
A single mistype + copy and pasted in the SO answer multiple times. In DD (Even for Windows) there is no such thing ;) Fixed, thanksTetreault
i replace /dev/fd0 for E:\ on windows but it is saying that the output file cant be foundShick
@Kid8 : Another SO answer of mine has some info on using Chrysocome DD on WIndows. Check the section just above the screenshots https://mcmap.net/q/190644/-enable-the-boot-loader-to-load-the-second-sector-of-a-usbTetreault
T
1

DD for DOS

If you are working in DOS. I recently stumbled upon this post: https://sourceforge.net/p/freedos/mailman/message/21492772/

Which points to this download of gnu tools for DOS: http://www.ibiblio.org/pub/micro/pc-stuff/freedos/gnuish/gnufut21.zip

This command works in MS DOS 6.22 to write your .bin file to the A drive (use at your own risk!)

dd if=c:\wherever\your\bin.bin of=0:

Where bin.bin should be 512 bytes in size and 0: should point to your A drive.

Titrant answered 7/10, 2022 at 20:32 Comment(0)
P
0

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.

Poultice answered 4/12, 2015 at 16:7 Comment(3)
@MichaelPetch I got that from wikipedia en.wikipedia.org/wiki/Master_boot_record "MBRs are not present on non-partitioned media such as floppies, superfloppies or other storage devices configured to behave as such." But from what you've said, it's clear that wikipedia is wrong. What is the difference, if any, between the terms- boot record, master boot record, and boot sector?Poultice
@MichaelPetch Is a floppy disk partitionless? And what's the general category, the name, for MBR and GPT? They are types of partition table right? So, would a floppy disk have a partition table with no partitions? or do they have partitions? lots of websites + wikipedia, say floppy drives don't have partitions.Poultice
@MichaelPetch Your own link does not say that floppies have an MBR. "Execution is then transferred to the freshly loaded boot record. On a floppy disk, all 512 bytes of the boot record (save the last two signature bytes) may contain executable code. On a hard drive, the Master Boot Record (MBR) holds executable code at offset 0x0000 - 0x01bd, followed by table entries for the four primary partitions, using sixteen bytes per entry (0x01be - 0x01fd), and the two-byte signature (0x01fe - 0x01ff)." <-- It says hard drives have a boot record, specifically an MBR, floppies a boot record.Poultice
J
-1

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);
Jeanelle answered 8/6, 2016 at 5:29 Comment(4)
That doesn't check for errors, and writes garbage into floppy.img if it's read+writeable but xx.bin wasn't readable.Sibella
Seeking to 512 in the output before write() will write it as the second sector, not the boot sector. That might be useful as well (for a second-stage bootloader, as MichaelPetch's answer points out), but this question is asking about writing to the first sector.Sibella
You're using POSIX system calls (open(2)/read(2)/write(2)), not ISO C stdio functions (fopen(3)/fread(3)/fwrite(3)). So this will only work if you have a Unix-like environment available, in which case you either have dd(1) already or can install it easily.Sibella
Leave me a reply if you fix these problems, and I'd be happy to upvote. (esp. if you wrap a main() around it so it compiles). However, dangerous/low-quality answers are worse than no answer, so I'm going to have to downvote. I did think about it before voting, though.Sibella

© 2022 - 2024 — McMap. All rights reserved.