Page 1 of 2

get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 4:52 am
by LGabci
Hello!

How can I get the floppy disk CHS geometry?
BIOS knows only the drive properties, but there could be a DD or HD disk in the drive, eg. 360KB floppy disk in a 1.2 MB drive (and exists a lot of other formats, like 320KB, when there are 8 sectors per track instead of 9; or 180KB, 160KB same as 360/320 KB but with only 1 head).

FAT boot sector have the "sectors per track" and "heads" values at least, but there is no similar in ext2/3/4 filesystem.
I tried to figure out how Linux does this in floppy.c, but I could not manage it yet. I think it maybe some trial and fail technique because there is a flag named "probing" and it supresses errors like reading non existent sectors.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 8:29 am
by rdos
It was a long time ago since I implemented my floppy disc driver, but I remember several things were tricky on real hardware, mostly due to timing. Some of the parameters are detected by sending commands to the controller. The driver also uses spinlocks frequently (originally it turned off interrupts, but this doesn't work on multicore).

You don't need the CHS geometry to read the boot sector, and my driver uses the information from the boot sector to calculate CHS. I think Linux will still need to read this information somehow. I also don't see why they would waste space on a floppy for ext. Floppies are typically formatted with FAT12, which is a problem in itself.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 8:44 am
by iansjack
Don’t forget that the boot sector is not part of the ext file system. Are you sure you are looking at the boot sector, not the superblock?

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 2:03 pm
by LGabci
rdos wrote: Sat Nov 23, 2024 8:29 am why they would waste space on a floppy for ext
It is not a big waste, a few KBs on a 1.44 MB disk.

Linux doesn't rely on filesystem information when detects floppy geometry, it can use ext2 floppies.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 2:11 pm
by LGabci
iansjack wrote: Sat Nov 23, 2024 8:44 am Don’t forget that the boot sector is not part of the ext file system. Are you sure you are looking at the boot sector, not the superblock?
There is no boot sector. I just want to know the CHS geometry.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 2:44 pm
by nullplan
rdos wrote: Sat Nov 23, 2024 8:29 am I think Linux will still need to read this information somehow.
If I understand the source code in drivers/block/floppy.c correctly, Linux will indeed read sector 0 into memory. However, it will discard the data unexamined. The only thing that matters to Linux is the ability to read sector 0, which simultaneously verifies that there is a formatted medium in the drive, and that it is readable, and what the format is.

Linux has a list of standard formats. These include the geometry, but also the gap values and whether double stepping is needed (which mean it contains all formats with 40 tracks per side twice). There is also a list of drive types, and each drive type specifies up to 8 formats that are tried. When a disk change interrupt occurs (or a medium change is noticed another way), if the user has not set up their own special geometry, the first autodetect format is set and a read of sector 0 is attempted. If that fails, the next autodetect format is set up and the command is retried. This goes until it runs out of autodetect formats.

This means that the OP's 8 SPT formats are not recognized, but you can make the driver force-set that format. The given rationale is that the drive will not be able to lock on to track 0 if the format is set wrong.
rdos wrote: Sat Nov 23, 2024 8:29 am I also don't see why they would waste space on a floppy for ext.
I doubt ext2 wastes that much space over FAT12 with VFAT.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 2:47 pm
by iansjack
It should have a boot sector. The msdos format command, or fdformat, will write a boot sector to the floppy. The ext2 file system doesn’t touch that sector.

That doesn’t mean the disk is bootable, so boot sector is something of a misnomer.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 3:48 pm
by LGabci
nullplan wrote: Sat Nov 23, 2024 2:44 pm If I understand the source code in drivers/block/floppy.c correctly, Linux will indeed read sector 0 into memory. However, it will discard the data unexamined. The only thing that matters to Linux is the ability to read sector 0, which simultaneously verifies that there is a formatted medium in the drive, and that it is readable, and what the format is.

Linux has a list of standard formats. These include the geometry, but also the gap values and whether double stepping is needed (which mean it contains all formats with 40 tracks per side twice). There is also a list of drive types, and each drive type specifies up to 8 formats that are tried. When a disk change interrupt occurs (or a medium change is noticed another way), if the user has not set up their own special geometry, the first autodetect format is set and a read of sector 0 is attempted. If that fails, the next autodetect format is set up and the command is retried. This goes until it runs out of autodetect formats.

This means that the OP's 8 SPT formats are not recognized, but you can make the driver force-set that format. The given rationale is that the drive will not be able to lock on to track 0 if the format is set wrong.
Thank you!
I found the 31 floppy types (and a NULL type), and really there is no an 8 sector / track type.
And it reads sector 0. But how can it distinguish between 1 and 2 head types?
Eg. the 3. (D360) and 4. (D720) type has only difference in the number of heads:

Code: Select all

	    Size
	     |  Sectors per track
	     |  | Head
	     |  | |  Tracks
	     |  | |  | Stretch
	     |  | |  | |  Gap 1 size
	     |  | |  | |    |  Data rate, | 0x40 for perp
	     |  | |  | |    |    |  Spec1 (stepping rate, head unload
	     |  | |  | |    |    |    |    /fmt gap (gap2) */
static struct floppy_struct floppy_type[32] = {
	...
	{  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },	/*  3 360KB SS 3.5" */
	{ 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },	/*  4 720KB 3.5"    */
	...
And the 3.5" DD type FDD tries to autodetect the 4. and the 3. types: "{ 4,22,21,30, 3, 0, 0, 0}"

Code: Select all

/* NOTE: the time values in jiffies should be in msec!
 CMOS drive type
  |     Maximum data rate supported by drive type
  |     |   Head load time, msec
  |     |   |   Head unload time, msec (not used)
  |     |   |   |     Step rate interval, usec
  |     |   |   |     |       Time needed for spinup time (jiffies)
  |     |   |   |     |       |      Timeout for spinning down (jiffies)
  |     |   |   |     |       |      |   Spindown offset (where disk stops)
  |     |   |   |     |       |      |   |     Select delay
  |     |   |   |     |       |      |   |     |     RPS
  |     |   |   |     |       |      |   |     |     |    Max number of tracks
  |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
  |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
  |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
...
{{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
      0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
...
/*    |  --autodetected formats---    |      |      |
 *    read_track                      |      |    Name printed when booting
 *				      |     Native format
 *	            Frequency of disk change checks */
};
If it doesn't read the other side of the disk, how it knows which floppy type it have to use???
How the first attempt on type 4 does not stop the autodetection?

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 4:01 pm
by LGabci
iansjack wrote: Sat Nov 23, 2024 2:47 pm It should have a boot sector. The msdos format command, or fdformat, will write a boot sector to the floppy. The ext2 file system doesn’t touch that sector.

That doesn’t mean the disk is bootable, so boot sector is something of a misnomer.
1st: I don't format it to an MS-DOS diskette and I can't tell everybody who will use this software to create an MS-DOS boot sector on his/her floppy. Like you wrote: ext2 doesn't touch that sector, mkfs.ext2 leaves empty the first 1KB.

2nd: I don't want to tie the detection of the floppy diskette geometry to a file system, because it is independent of the file system.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 8:39 pm
by Octocontrabass
There's no 100% reliable way to find a floppy disk's geometry. That's why the BPB exists in the first place.

If you still want autodetection, you could try reading the first track with different data rates until you find one that works, and assume the most popular geometry for the data rate that works is the correct one. You'll need to provide a way to override the guess when you've guessed wrong. Linux does something like this.

Floppy disk formats can get complicated. Some formats don't really fit the concept of CHS geometry.

Re: get floppy disk CHS geometry

Posted: Sat Nov 23, 2024 11:10 pm
by nullplan
LGabci wrote: Sat Nov 23, 2024 3:48 pm And it reads sector 0. But how can it distinguish between 1 and 2 head types?
I'm guessing single-sided is not really supported. The type 3 you identified is the only single-sided type in the entire list, and it is only on the detection list for unknown drives, and 3 1/2" DD drives, which ought to be reasonably rare. The most common drive type by far is going to be the 3 1/2" HD drive, which doesn't have type 3 in its list. And single-sided 3 1/2" floppies should also be pretty rare. By the time the 3 1/2" floppy rolled around, nobody would accept a downgrade in storage size, so a format of less than 720 kB was just never going to fly. I have honestly never heard of single-sided 3 1/2" disks. I did hear of single-sided 5 1/4" disks, though, mostly from the early days.

I know that the entire topic is a rabbit hole, but I highly doubt that you will, in reality, ever be faced with a floppy that doesn't have the most common formats. This is also why the method Linux uses (autodetection that might be wrong, but works for the most popular formats, and allowing the user to override) is probably going to be good enough. If a Commodore enthusiast shoves one of their floppies into a PC with your OS, hoping to archive something, then they know what they are doing and can provide the format themselves.

Re: get floppy disk CHS geometry

Posted: Sun Nov 24, 2024 12:40 am
by iansjack
A floppy disk must be formatted before it can be used. Otherwise it is just a blank sheet of iron oxide with no concept of tracks or sectors. This is different from making a file system on the disk. Msdos combines the formatting and file system creation into a single program - format. Linux and the BSDs have the program fdformat to format the disk (and create a boot sector) and prepare it for a file system. Then you use mkfs (or some other tool) to create a file system on the formatted disk.

Most floppy disks that you find nowadays will have already been formatted - either they’ve been used before or they were pre-formatted by the manufacturer. The boot sector details the physical layout of the disk; the file system details its logical layout. CHS geometry is a physical parameter.

Formatting and the creation of a file system are not the same operation.

Re: get floppy disk CHS geometry

Posted: Sun Nov 24, 2024 7:57 am
by LGabci
Octocontrabass wrote: Sat Nov 23, 2024 8:39 pm If you still want autodetection, you could try reading the first track with different data rates until you find one that works, and assume the most popular geometry for the data rate that works is the correct one. You'll need to provide a way to override the guess when you've guessed wrong. Linux does something like this.
I don't want to detect all types of floppy formats, but just some fundamental ones: 360KB, 1.2MB, 720KB, 1.44MB.
Linux supports 31 floppy disk types, and it can autodetect this types (by not only data rates, but eg. gap sizes between sectors), if you try to use another format, they say we don't support that.

Re: get floppy disk CHS geometry

Posted: Sun Nov 24, 2024 8:16 am
by LGabci
nullplan wrote: Sat Nov 23, 2024 11:10 pm I know that the entire topic is a rabbit hole, but I highly doubt that you will, in reality, ever be faced with a floppy that doesn't have the most common formats.
Yes, I think so, you are right.
30 years ago I used the 5 1/4" 360KB and 1.2MB floppy types, and a few years later 3 1/2" 1.44MB.
I didn't use the 3 1/2" 720KB media (in the highschool there were a few guys who had this type of floppy drive at home, and they shared a program among each others named 800.COM, that can format this 3 1/2" 720KB floppies to 800KB in DOS, this was the only exotic floppy type I have ever seen).

And there are no floppies around many years ago.

Re: get floppy disk CHS geometry

Posted: Sun Nov 24, 2024 12:12 pm
by nullplan
LGabci wrote: Sun Nov 24, 2024 7:57 am if you try to use another format, they say we don't support that.
Actually, you can use the FDSETPRM ioctl to set any geometry you should want, so long as it can be formulated in that structure (struct floppy_struct). As I said, if you actually have a non-standard format, and you know it, you can set it. So long as the PC floppy drive can read it, you can do so in Linux as well.
LGabci wrote: Sun Nov 24, 2024 8:16 am I didn't use the 3 1/2" 720KB media (in the highschool there were a few guys who had this type of floppy drive at home, and they shared a program among each others named 800.COM, that can format this 3 1/2" 720KB floppies to 800KB in DOS, this was the only exotic floppy type I have ever seen).
My, this is a rabbit hole. According to https://en.wikipedia.org/wiki/List_of_f ... sk_formats, there are multiple 800 kB formats for the 3 1/4" floppy. The most likely candidate here being the Commodore 2/80/10 format with 512 byte sectors. That keeps everything as normal, only ups the number of sectors per track from 9 to 10.
LGabci wrote: Sun Nov 24, 2024 8:16 am And there are no floppies around many years ago.
Well, with USB thumb drives, portable SSDs and tiny hard disks, a storage solution that can, in the main case, save 1.14 MB and take half an era to do so is just not worth it. And it can be taken out by a fridge magnet, which the other things aren't (that) susceptible to. It is simply obsoleted by the march of technology.