# Help with the dd program and writing to boot sector of a FAT12 floppy image



## olliea95 (Nov 19, 2009)

I have a problem using the dd program. I have created and formatted a Floppy Image as FAT12, and now I want to write to the boot sector of it. I have accomplished this, but when I do so it appears to overwrite the partition tables (I believe are stored in sectors 2 and 3) so it is no longer mountable.

I have created the image and formatted with:

```
dd if=/dev/zero of=floppy.img bs=512 count=2880
hdiutil attach floppy.img -nomount
diskutil partitionDisk /dev/disk2 2 MBRFormat "MS-DOS FAT12" "FLOPPY" 1.3M Free\ Space N 0B
hdiutil detach /dev/disk2
hdiutil attach floppy.img -nomount
```
But then when I used the command to write to the boot sector, it is no longer mountable due to there not being a file system on the image.

```
dd if=Boot1.bin of=/dev/disk2 bs=512 count=1
```
Using diskutil info before writing to the boot sector, it is definitely formatted as FAT12 correctly, but then doing so after writing to boot sector, there is no file system.

Any help anyone? I believe my problem lies with the final dd command, but I'm not sure.

*EDIT: I can provide my Boot1.bin and Boot1.asm if needed. Boot1.bin is also definitely 512 bytes, and I used nasm to assemble it.*

Thanks,


----------



## Headrush (Feb 9, 2005)

olliea95 said:


> Any help anyone? I believe my problem lies with the final dd command, but I'm not sure.
> 
> *EDIT: I can provide my Boot1.bin and Boot1.asm if needed. Boot1.bin is also definitely 512 bytes, and I used nasm to assemble it.*
> 
> Thanks,


I think your problem probably lies in your Boot1.bin.

As a test, after you create and attach your floppy image, use dd to grab the boot sector

```
dd if=/dev/disk2 of=TestBoot.bin bs=512 count=1
```
Now try your last dd command that you thought was faulty and re-apply it back to your floppy image.
Detach the floppy image.
Now attach the floppy image again and you should see the partition is still valid. (If so, it's not the dd command and your Boot1.bin)


----------



## olliea95 (Nov 19, 2009)

Thank you for your response. I have tried the steps you have outlined and you are correct, there is an issue with my Boot1.bin. When I dd'ed the TestBoot.bin that I recovered from it, I was able to mount it correctly, however with Boot1.bin as soon as I try to remount I get "hdiutil: attach failed - no mountable file systems".

This is very strange. The only thing I can think that would be different is the file size. However they are both exactly 512 bytes, just how they should be. Also, when I write my Boot1.bin, it does still boot correctly in a Bochs emulator, so it can't be a huge problem with it? Anyway, I'll go through the assembly again and check if I have made any errors.

Cheers, and if you have any further input it would be much appreciated.

*UPDATE: I have taken a working boot.asm file, assembled it and then written it to the image and it still doesn't work. I really don't know what could be wrong now. I know there is an issue with the bin file, however its not the contents as I've tried a verified working one, and its not the size either as its 512 bytes. Somehow the Testboot.bin I extracted works but my assembled boot.bin doesn't.

UPDATE 2: Apologies for wasting your time, I have made a simple error in my assembly file. As I'm new to the language it doesn't take much to confuse me, and I just forget a few lines. It now works fine. Thanks for your help Headrush, would've kept on thinking it was my dd command and not got anywhere! The actual cause was my 'bsVolumeLabel' value was only padded to 8 bytes, whereas it needs to be exactly 11 bytes long as all FAT12 filenames do.*


----------



## Headrush (Feb 9, 2005)

Great to hear.

No waste of time. Sometimes the best learning exercises are when we make mistakes and really have to scour into details.


----------

