Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

assembly - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...