Why does sector-number addressing in CHS start at sector 1 and not 0?

13

1

Before LBA was introduced, why does CHS start at 0,0,1 and not 0,0,0 ?

Jordan Davis

Posted 2015-09-18T18:50:33.673

Reputation: 471

1"Why does the sector count start at 1..." -- Actually you're referring to the sector number, not the sector count. Those are two distinct and different parameters used in a disk request. "Sector index" is also poor nomemclature for the sector number, as there is a distinct entity called the index in disk drives. – sawdust – 2015-09-30T01:39:56.873

Answers

8

Unfortunately, this is just how the CHS addressing scheme, popular at the time, was implemented and adopted into use. This was adopted as the official convention for IBM-compatible computers in the BIOS interrupts used for disk access, explaining why this convention is used to this day. From the ECMA-107 Standard, Volume and File Structure of Disk Cartridges for Information Interchange (this is also mentioned in the original ATA-1 specification):

6.1.3 Logical Sector Number

Each sector on a volume shall be identified by a Logical Sector Number. [...] The Logical Sector Numbers shall be assigned in an ascending sequence, beginning with 0, starting at sector 1, track 00, side 0, continuing onto track 00, side 1 (if FDC is recordable on both sides) and then to track 01, side 0, etc.

This issue is addressed in the specifications for some hard disks, where it is noted that a given logical CHS address differs from the physical CHS address location. This is further discussed in the Seagate ATA Interface Reference Manual, which contains this interesting narrative:

5.1 Logical block addressing

[...] the sectors on the drive are assumed to be linearly mapped, with an LBA 0 of cylinder 0 / head 0 / sector 1.

[...] For all translation modes, C=0, H=0, S=1 is equivalent to LBA=0. It is not possible to compute an equivalent CHS for all logical block addresses in all translation modes because this formula only works in one direction. This is because CHS addressing can’t access 1/256th of all of the possible sectors that logical block addressing can access, since there is no sector 0 in CHS.

Thus for logical CHS addressing, although the first cylinder/head indices start from a 0-based offset, and the first sector index starts from 1 (e.g. the minimum possible CHS address is 0/0/1), this does not change anything about the physical location of this sector. Think of it as the first physical sector on the disk being called "sector 1", occupying CHS 0/0/1. Indeed, the "first" element in most programming languages is 0-based, so the logical address of the sector at CHS address 0/0/1 is zero (0x00).

This makes a lot more sense logically (namely, the "zeroth" logical address being the first physical sector), as we can address the disk device as any other memory device (as each sector has a unique linear address to map it to a physical sector), thus why it makes sense for LBA to start from zero. Indeed, if we translate the CHS address 0/0/1 to an LBA, the resulting LBA will be 0x00000000 (this is why 1 is subtracted from the sector index in most CHS to LBA calculations, and why 1 is added to the index for LBA to CHS calculations).

Breakthrough

Posted 2015-09-18T18:50:33.673

Reputation: 32 927

I figure the answer to this question already, but I guess the person I left the my comments displaying the answer deleted it... anyways the reason why I asked this question was due to the fact everyone and everything I read posted your basically your exact words for the most part. This answer of "it's just the way they made it..." really annoyed me because people don't just build something and not use it just because they don't want to use it... that logic doesn't make since at all... – Jordan Davis – 2015-09-23T01:27:10.913

1

Going through the history of CHR/CHS/LBA you find that before CHR which was variable length using MBBCCHHR used the zero sector for error codes, and bad blocks, etc.... transition to CHS and "fixed-block-architecture" you also had the transition of adding a controller (firmware) to the drive and the start of "logical" disk associations... the controller now is invisible but you can assume is uses it the same way... the term is now called "Host Protected Area" https://en.wikipedia.org/wiki/Fixed-block_architecture and https://en.wikipedia.org/wiki/Host_protected_area

– Jordan Davis – 2015-09-23T01:30:29.067

@JordanDavis in most cases, the host-protected area is at the end of the disk, not beginning. Indeed while the nomenclature for the first sector is returned as the value 1, I've still not found any information as to why (although I suspect it may have something to do with sector 0 being reserved as an error flag). I will be sure to update you if I come across anything. – Breakthrough – 2015-09-23T05:47:46.340

The Wikipedia link for FBA section overview, list being the zeroth record as ECC but that sounds good, I'll do the same if either comes across. – Jordan Davis – 2015-09-23T13:20:49.790

@JordanDavis I've added a bounty on this question, as I'd really like to hear some input from others - especially in regards to the why aspect.. Feel free to keep the question open/unanswered for the time being to give a chance for other answers. – Breakthrough – 2015-09-25T04:22:18.757

Okay sounds good – Jordan Davis – 2015-09-25T04:54:18.680

Any idea why it was necessary to tell the BIOS what size hard drive was installed, rather than simply having such information stored on sector 1 of cylinder 0 of head 0 (a location that should exist for drives of any size)? If a disk had never been formatted under MS-DOS, it would be necessary to tell the system how big it was, but is there any advantage to storing such information in CMOS rather than on the disk itself? – supercat – 2015-10-07T20:12:37.543

@supercat FYI that sector is reserved for the master boot record (MBR) in IBM-compatible computers, so it would not be usable for that purpose; furthermore, it would make a lot more sense to store such information in flash memory rather than on the disk itself. There is functionality in the BIOS that can read the parameters directly from the disk (AH=08h: Read Drive Parameters).

– Breakthrough – 2015-10-07T23:46:30.473

Could you be talking about the size of a partition instead? Indeed you need to specify the size of a partition that you want to make on the drive, but the physical characteristics ARE available from the drive itself. Of course in some cases (e.g. using non-standard hardware) the drive might not conform to the IBM/DOS BIOS specifications, and you would thus have to enter the number of heads, cylinders, and sectors per track manually. – Breakthrough – 2015-10-07T23:49:24.623

@Breakthrough: I mean the size of the disk. Since the system is expected to access every hard drive on startup, it could perfectly easily start by reading the first sector of each disk at that time and copy the size information in RAM. That would ensure that if one swaps two different-sized hard drives between two machines, each machine would automatically correctly identify the changed sizes of its drive. On the PC, one could only use specific sizes supported by one's controller (selected via jumper). On the AT, one had to manually enter the drive size. Only on later machines... – supercat – 2015-10-08T14:11:59.790

...could the BIOS be set to auto-configure based on the drive's reported size. – supercat – 2015-10-08T14:14:09.600

@supercat the BIOS basically reads a boot-loader which specifies the drive to find the OS so not much logic their really more like point-and-shoot, it is possible with chain-loading to run a hybrid kind of boot (https://en.wikipedia.org/wiki/Chain_loading) but really the BIOS is a dying breed as we move towards UEFI (http://www.uefi.org/) which is awesome and can do a whole lot more than the BIOS, so if your looking at building something like that definitely build it with UEFI.

– Jordan Davis – 2015-12-31T22:30:06.973

8

I have tried to trace the history of CHS and the "starting with 1" sector numbering fiasco, which has caused many complications for disk-drivers writers and gave rapid rise to LBA.

CHS dates back to the days when personal computers ran on diskettes and when the BIOS was invented. This is what Wikipedia says :

The term BIOS (Basic Input/Output System) was invented by Gary Kildall and first appeared in the CP/M operating system in 1975, describing the machine-specific part of CP/M loaded during boot time that interfaces directly with the hardware.

A research into the CP/M BIOS has found the document CP/M information archive : BDOS system calls, in which sector numbers start with zero. The conclusion is that : earliest CHS schema actually used zero-based sector addresses.

One-based sector addresses were first introduced with the first IBP/PC. The document INT 13 - Diskette BIOS Services specifically says :

Most disk BIOS calls use the following parameter scheme:

    AH = function request number
    AL = number of sectors  (1-128 dec.)
    CH = cylinder number  (0-1023 dec.)
    CL = sector number  (1-17 dec.)    <--------!!!
    DH = head number  (0-15 dec.)
    DL = drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)
    DL = drive number (0=A:, 1=2nd floppy, 80h=C:, 81h=D:)
         Note that some programming references use (0-3) as the
         drive number which represents diskettes only.
    ES:BX = address of user buffer

So it was the IBM/PC that by a de-facto implementation of the BIOS has converted sector-numbering from zero-based to one-based.

Of the twelve IBM engineers assigned to create the IBM Personal Computer (model 5150), David J. Bradley developed the code for its BIOS. So he's the one who, among all its other details, decided on the parameters for the disk interrupts. We also owe to this guy together with Mel Hallerman the famous CTRL+ALT+DEL.

So the answer to the question Why does the sector count start at 1 and not 0 in CHS is :
Because David J. Bradley programmed the BIOS that way.

As to why he did it this way, this is best answered by himself. If I had to guess, I would say that he left sector zero as an addressing sector by which the driver could verify that the head was on the right track.

As disks were actually engineered so as not to require such a mechanism, and the engineers were not ready to waste one sector because of the BIOS, sector zero never came into existence. Thereafter, driver-writers were left with the need to subtract-one and add-one to sector addresses for all BIOS disk calls.

harrymc

Posted 2015-09-18T18:50:33.673

Reputation: 306 093

"I would say that he left sector zero as an addressing sector..." -- Illogical guess. Every sector has an Identification Record that includes the cylinder/head/sector address. The disk controller does not have to wait for the Index to come around to validate that the seek completed on the correct cylinder; the very next sector that rotates under the head is read for verification. – sawdust – 2015-09-30T00:55:10.360

"CH = cylinder number (0-1023 dec.)" -- The original IBM PC BIOS uses CH for the "track" number. CH is an 8-bit register, so the maximum value is only 255. – sawdust – 2015-09-30T01:14:42.067

"I have tried to trace the history of CHS ... and gave rapid rise to LBA" -- To be clear, all these discussions about "LBA" only pertain to PC disk interfaces, especially the ATAPI specification. Otherwise CHS and LBA can coexist. E.G. I've written a few filesystems that used LBA internally, but converted the addressing to CHS to perform the actual disk I/O per the controller interface. "earliest CHS schema actually used zero-based sector addresses." -- True, but that happened way before CP/M. – sawdust – 2015-09-30T01:30:09.770

@sawdust: All I did was to cite IBM documents, which might even predate the assignment of registers for the PC BIOS interface. It's clear that Bradley was mistaken or misdirected by setting sector 0 as a reserved address, but we cannot be sure what for, since it never happened. It might be that he wasn't aware that the sector address-part also contained the track-number. Or maybe this spare sector was a property of diskettes that was carried over into Bradley's general BIOS interface. We only know that the IBM/PC BIOS was responsible for this change in CHS. – harrymc – 2015-09-30T05:47:40.207

As always in these matters, Ralph Brown's Interrupt List provides invaluable information. While it is true what @sawdust wrote about CH being an eight-bit register, that's not how CX was used. Instead, CX was bit-stuffed with both the cylinder number and sector number (but this extension apparently only applied to hard disks, not floppies; for floppies, CL held the sector number and CH held the cylinder number). Compare for example the specified interface for Int 13/AH=02h, BIOS - DISK - READ SECTOR(S) INTO MEMORY input parameters.

– a CVn – 2015-09-30T07:54:14.740

From user APO69: "CH = cylinder number (0-1023 dec.)" -- The original IBM PC BIOS uses CH for the "track" number. CH is an 8-bit register, so the maximum value is only 255. – sawdust - NO!! CH is 8 bits, yes, but track number use 10 bits. 8 bits from C (CHS) and 2 from S (CHS). That's the reason why Sector uses only 6 bits (2^6=64)

– fixer1234 – 2016-03-06T00:56:54.297

1

The first specification on floppy disks was made by IBM with the appearance of the IBM 3740 and does not mention that there are sectors reserved for the system. The only reservation for the system is track 00, which stores only the "Data Set Labels" that identify the type of information stored in tracks 01 to 76. It clearly defines that the first sector is SECTOR 1. This is not a coincidence but a matter of natural numeration against the numbering of the computer.

We can observe that when a human begins to count anything, it does not begin with zero but one. For example, imagine that there are 135 students in a classroom. Counting the number would be something like that: One, two, three ... one hundred thirty four, one hundred and thirty five.

Its numerical representation would be thus: 1, 2, 3, ... 134, 135 So far we agree, right?

Now let's put the digits that are not represented by being 0. It would look like this: 001, 002, 003, ..., 015, 016, ..., 099, 100, ..., 133, 134, 135

This is what happens with CHS: 0,0,1 - 0,0,2 - 0,0,3...

It is significant that the rows in the Excel sheets or the auto-numeric fields in a database begin by 1 and not by 0 and nobody has said anything about this.

Did David J. Bradley schedule the BIOS that way?

Yes, but he wasn't out side.

Definitely there is no technical reason why SECTOR 0 can not be used unless it is reserved and hidden from users for other reasons. In principle everything points to it is a matter of natural numbering.

GA21-9152-2 File No. 3740-00,15
IBM 3740 Data Entry System

page 12
DISK INITIALIZATION
...Each disk contains two spare tracks to replace any irregular tracks. In addition, the initialization feature provides a means of altering the sequence of sector address on the diskette. Normally, the sequence of sectors is in numeric order (1, 2, 3, ... 25,26)
.

page 24
DATA LABELING ON THE DISKETTE
During initialization, the data set label for a data set is magnetically recorded on the index track (track 00) of the diskette. The primary purpose of this label is to show the location of the data set on the diskette

This was in 1973. It recorded data on an 8" diskette. The IBM PC 5150 was born on August 12, 1981... and we can not forget that they had to maintain some compatibility with the previous devices.

There was definitely no technical reason.

APO69

Posted 2015-09-18T18:50:33.673

Reputation: 141

Its reserved for driver - I/O firmware interface, even in modern day SSD disk or flash have reserved space for I/O operations to read/write/copy/format etc.. – Jordan Davis – 2017-04-11T19:52:00.803

Take a look at This

– yass – 2017-04-11T20:39:51.520

Yes, but not SECTOR 0 but CYLINDER 0. From the first specification (IBM 3740) cylinder 0 is called "the Index cylinder" and stores multiple data specified in the "INDEX CYLINDER LAYOUT".

On modern discs this cylinder is usually assigned as "CYLINDER NUMBER -1", yes "-1", and is only accessible by the controller.

There is nothing to explain why sector 0 has not been used and although we can see it in some operating systems like CP/M, it is virtual since the hardware is still manufactured according to the IBM 3740. In other words, the first sector Is SECTOR 1. Why? "NATURAL NUMBERING" – APO69 – 2017-04-12T18:33:58.180