1

I'm new to OS Development, and I want to format a 1.44MB floppy image with the FAT12 file system. I created the disk using dd if=/dev/zero of=<my image> bs=512 count=2880, so it's just a raw image filled with zeros. Next, I wanted to create the file system. The guy in the tutorial used mkfs.fat12, but I'm working on MacOS.

The only command I've found for formatting disks there is newfs_msdos -f 2880 -F 12 <my image>.

But that commands just prints an error: newfs_msdos: Inappropriate ioctl for device: build/disk2.img: Cannot get partition offset

I've also found diskutil eraseDisk, but that only works for a mounted device, not for disk images, and I can't mount a disk image if it has no working file system.

I'd be thankful if someone knows a command that works for floppy images, or if someone can tell me what triggered this error and how I can avoid it.

How can I tell the command that there is no partitions or offsets, and I just want to format the whole disk?

4

2 Answers 2

2

For a quicker read, I moved the conclusion section to the top of this answer.

Conclusion

The OP should use the commands given in Case 2 to format the image. However, using the commands given in Case 1 will probably correctly format the image when using OS X 10.6.8 through macOS 13.3.1 on an Intel Mac.


Introduction

I cannot reproduce your problem. In other words, I am not able to not format the image.

I test using the following machines

  • Snow Leopard virtual machine with a drive sector size of 512 bytes.
  • Monterey virtual machine with a drive sector size of 512 bytes.
  • Venture 2018 Mac mini with a drive sector size of 4096 bytes.

Case 1

Based on the question, I executed the following commands:

dd if=/dev/zero of=x.img bs=512 count=2880
newfs_msdos -f 2880 -F 12 ./x.img

The output is given below.

davidanderson@Lobster Desktop % dd if=/dev/zero of=x.img bs=512 count=2880
2880+0 records in
2880+0 records out
1474560 bytes transferred in 0.011958 secs (123311591 bytes/sec)
davidanderson@Lobster Desktop % newfs_msdos -f 2880 -F 12 ./x.img
newfs_msdos: warning: ./x.img is not a character device
ioctl(DKIOCGETPHYSICALBLOCKSIZE) not supported
./x.img: 5726 sectors in 2863 FAT12 clusters (1024 bytes/cluster)
bps=512 spc=2 res=1 nft=2 rde=240 sec=5760 mid=0xf0 spf=9 spt=36 hds=2 hid=0 drv=0x00

An error message was generated because the sector (block) size could not be determined from the image file x.img. However, output shows a bps (bytes per sector) value of 512 was used as a default value.

Case 2

I executed the following commands. Here, the command hdiutil attach was used to attach a disk image as a device. The identifier disk7 was determined from the output of this command.

dd if=/dev/zero of=y.img bs=512 count=2880
hdiutil attach -nomount y.img
newfs_msdos -f 2880 -F 12 disk7
hdiutil detach disk7

The output is given below.

davidanderson@Lobster Desktop % dd if=/dev/zero of=y.img bs=512 count=2880
2880+0 records in
2880+0 records out
1474560 bytes transferred in 0.012780 secs (115380282 bytes/sec)
davidanderson@Lobster Desktop % hdiutil attach -nomount y.img
/dev/disk7                                              
davidanderson@Lobster Desktop % newfs_msdos -f 2880 -F 12 disk7
512 bytes per physical sector
/dev/rdisk7: 5726 sectors in 2863 FAT12 clusters (1024 bytes/cluster)
bps=512 spc=2 res=1 nft=2 rde=240 sec=5760 mid=0xf0 spf=9 spt=36 hds=2 hid=0 drv=0x00
davidanderson@Lobster Desktop % hdiutil detach disk7           
"disk7" ejected.

Here no error messages were generated since the bps value could be determined to be 512.

Comparison of Case 1 and Case 2

I entered the following commands to compare the two image files.

hexdump -C x.img >x.lst
hexdump -C y.img >y.lst
diff x.lst y.lst

The output is given below.

davidanderson@Lobster Desktop % hexdump -C x.img >x.lst
davidanderson@Lobster Desktop % hexdump -C y.img >y.lst
davidanderson@Lobster Desktop % diff x.lst y.lst
3c3
< 00000020  00 00 00 00 00 00 29 12  0d c4 37 4e 4f 20 4e 41  |......)...7NO NA|
---
> 00000020  00 00 00 00 00 00 29 15  0d c9 37 4e 4f 20 4e 41  |......)...7NO NA| 

The difference between the two files is at byte offsets 0x27 and 0x29. According to Wikipedia, bytes at offsets 0x27 through 0x2A contain the disk serial number, which is expected to be different between FAT12 formatted images.

4
  • Ok, I tried the first way again, which was also like how I tried to do it, and it still gives the same error. (I'm working on a real Mac with Sonoma 14.0, maybe it depends on the version)Somehow the command doesn't like disk images, but the second way worked for me. I didn't know the way to attach the image without mounting the file system, thank you for that. Oct 30, 2023 at 16:18
  • 1
    axolotlKing0722: Including the Mac model/year and the version of OS X/macOS usually is helpful when posting questions and answers. For example, I still do not know whether your Mac is Intel or Apple Silicon. Oct 30, 2023 at 16:40
  • 1
    Running case 1 on Sonoma 14.1/M1 gives newfs_msdos: Inappropriate ioctl for device: ./x.img: Cannot get partition offset. Case 2 works fine.
    – nohillside
    Oct 30, 2023 at 19:21
  • @nohillside yeah, excatly that was my problem Oct 31, 2023 at 20:01
2

Ok, final solution here that worked for me:

Create an image with a command like that (or whatever you used to create your disk image):

dd if=/dev/zero of=disk.img bs=512 count=2880

Then attach it to:

hdiutil attach -nomount disk.img

That commands print the attached path, like /dev/disk4. Then you can format that path:

newfs_msdos -f 2880 -F 12 disk4

(it's important to write e.g. "disk4", not "/dev/disk4")

And then finally detach the image:

hdiutil detach disk4

That solution worked on my Mac Mini M1, MacOS Somona 14.0

1
  • You can use disk4, rdisk4 or /dev/rdisk4 as the device for the newfs_msdos command. In other words, the newfs_msdos command expects a character special file, which is unbuffered. Specifying the block special file /dev/disk4, which is buffered, will generate a warning message. Nov 1, 2023 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .