

                        Ŀ
                                         
                            CHAPTER I    
                                         
                        


           Ŀ
                                                        
               STANDARD DISKETTES PHYSICAL STRUCTURE    
                                                        
           



1. GENERALITIES

   Before a new diskette can be used with the computer, it must be
formatted (MS-DOS has an external command FORMAT which will format
diskettes). Formatting is a process that writes organizational infor-
mation on the diskette so that the operating system can read and
write files on it. Details about the "organizational" information
will be given in section 7 of this chapter.

For the time being, let us describe how a standard 5.25-inch,
double side, double density (DS/DD) diskette is organized for data
recording.
Both sides are divided into concentric circles called tracks or
cylinders ; there are 40 tracks, numbered from  0  to  39 on each
side.
Each track is further divided into segments, called sectors ; there
are 9 sectors numbered from  1  to  9  for each track.
So, the total number of sectors on a DS/DD  5.25-inch diskette is
                     2 * 40 * 9 = 720 .
The standard storage capacity of a sector is 512 bytes ( = 0.5 Kbytes)
and, therefore the storage capacity for a standard DS/DD  5.25-inch
diskette is   720 * 512 bytes = 360K bytes.

The disk drive uses two read/write heads (one for each side) to access
data on a diskette. One is called  head 0  and the other one is called
head 1.

To specify a sector one has to give three parameters :

     C :  the cylinder or track of the sector  (0 <= C <= 39) ;
     H :  the side of the disk, or equivalently the disk drive head
          to be used to access the sector  (0  or  1) ;
     R :  the sector number within the track it belongs to
          (1 <= R <= 9).

Now, what about other disk types ?
The following table describes their organization.


Ŀ
  size (inches)    5.25        5.25         3.5         3.5     
Ĵ
  density          double       high       double      high     
                   48 TPI      96 TPI      135 TPI              
Ĵ
  sides              2           2           2           2      
Ĵ
  tracks                                                        
   per  side        40          80          80          80      
Ĵ
  sectors                                                       
   per track         9          15           9          18      
Ĵ
  sectors                                                       
   per disk         720         2400        1440        2880    
Ĵ
  bytes                                                         
   per sector       512         512         512         512     
Ĵ
  bytes                                                         
   per disk         360K        1.2M        720K       1.44M    




From now on, our discussion will be restricted to 360K (5.25-inch /
DS/DD) disks. This is not a real loss of generality because the
adaptation to other disk types is straightforward.


2. LOGICAL SECTOR NUMBERS

   Remember, so far we need three parameters  C  H  R  to specify a
sector. There is another way of doing it : using logical sector
numbers (lsn) in the range  0..719.

   lsn  0  is assigned to sector  C = 0   H = 0   R = 1
   lsn  1  is assigned to sector  C = 0   H = 0   R = 2
   ...  .  .. ........ .. ......  .....   .....   .....
   lsn  8  is assigned to sector  C = 0   H = 0   R = 9
   lsn  9  is assigned to sector  C = 0   H = 1   R = 1
   lsn 10  is assigned to sector  C = 0   H = 1   R = 2
   ... ..  .. ........ .. ......  .....   .....   .....
   lsn 17  is assigned to sector  C = 0   H = 1   R = 9
   lsn 18  is assigned to sector  C = 1   H = 0   R = 1
   ... ..  .. ........ .. ......  .....   .....   .....

More generally :

   lsn = (C * 18) + (H * 9) + R - 1

and conversely :

   C = lsn div 18
   H = (lsn div 9) mod 2
   R = (lsn mod 9) + 1

For readers who are not afraid of elementary mathematics :
lsns correpond to a lexicographic ordering of sectors according to
(C,H,R).



3.  SECTORS "RESERVED" FOR THE OPERATING SYSTEM

    Even when a diskette is formatted without the  /S  option, not all
of the 720 sectors are available to the user for file storage.
The operating system wants its share :

Ŀ
   sector  lsn 0  (C = 0   H = 0   R = 1)  is the boot sector       
--------------------------------------------------------------------
   sector  lsn 1 and lsn 2  (C = 0   H = 0   R = 2,3) are for       
           the first File Allocation Table  (FAT1)                  
--------------------------------------------------------------------
   sector  lsn 3 and lsn 4  (C = 0   H = 0   R = 4,5) are for       
           the second File Allocation Table  (FAT2)                 
--------------------------------------------------------------------
   sector  lsn 5 to lsn 11   (C = 0   H = 0   R = 6)    to          
                             (C = 0   H = 1   R = 3)    are         
           for the root directory                                   
Ĵ
      see sections 4 - 6  for information about these sectors       



The operating system is not too greedy ; it wants a share consisting
of 12 sectors (6 Kbytes) and therefore  360Kbytes - 6 Kbytes =
354 Kbytes are available to the user (starting from lsn 12).

A cluster is a group of two consecutive sectors ("consecutive sectors"
means "sectors whose lsns are consecutive numbers"). They are numbered
from  2  to  355.

cluster  2  consists of sectors  lsn 12  and  lsn 13
cluster  3  consists of sectors  lsn 14  and  lsn 15
....................................................

More generally :

cluster  k  consists of sectors  lsn  (k - 2) * 2 + 12
                        and      lsn  (k - 2) * 2 + 13  .

Clusters are allocation units used to assign disk space to files.
The number of clusters needed to write a file of size  s  (bytes)
is  (s + 1023) div 1024 .



4. THE BOOT SECTOR

   Sector  lsn 0  (C = 0   H = 0   R = 1)  is the boot sector.
It contains :

(a)  a bootstrap program which reads the first root directory sector
     to see if the (hidden files)  IO.SYS  and  MSDOS.SYS  are present
     (if the system uses PC-DOS these files are named  IBMBIO.COM
and  IBMDOS.COM). If they are present  IO.SYS  is loaded into memory
and executed. Otherwise the bootstrap displays the (well known)
message :
           Non-System disk or disk error
           Replace and strike any key when ready  .

(b)  a parameter table starting at the offset 0Bh :


Ŀ
 offset  length             description                 standard 
 (hex)                                                   value   
Ĵ
  0Bh     word    number of bytes per sector            200h=512 
Ĵ
  0Dh     byte    number of sectors per cluster            2     
Ĵ
  0Eh     word    number of reserved (boot) sectors        1     
Ĵ
  10h     byte    number of FATs                           2     
Ĵ
  11h     word    number of root directory entries       70h=112 
Ĵ
  13h     word    total number of sectors on disk       2D0h=720 
Ĵ
  15h     byte    media descriptor or disk ID byte (*)    FDh    
Ĵ
  16h     word    number of sectors per FAT                2     
Ĵ
  18h     word    number of sectors per track              9     
Ĵ
  1Ah     word    number of heads                          1     
Ĵ
  1Ch     word    number of hidden sectors                 0     


    (*)  see  Hogan's Sourcebook (2.37) for non 360K disks ID byte


Remark : I think that it is instructive to spend a couple of hours to
         disassemble the contents of the boot sector and I advice the
         reader to do it.



5. THE ROOT DIRECTORY

   The root directory occupies 7 sectors. It contains 112 entries ;
each of them is composed of 32 bytes (see Hogan's Sourcebook (2.33)
for information about directory entries layout).

If the disk is bootable, the first directory entry describes the file
IO.SYS  and the second directory entry describes the file  MSDOS.SYS.

Bytes  1Ah  and  1Bh  of a directory entry indicate the first cluster
assigned to the file.
What about the other clusters (if any) ?
The File Allocation Table (to be discussed in the next section) is to
be used to locate them.

When a file is erased its directory entry is left intact except for
the first byte (the first character of the file name) which is
changed into  E5h . Of course, such a directory entry can be
overwritten if additional files are stored to the diskette.

Note that there are no sectors reserved for subdirectories and that
subdirectoy entries have the same format as root directory entries.



6. THE FILE ALLOCATION TABLES (FATs)

   There are two File Allocation Tables occupying two sectors each.

Under normal circumstances, both are identical.
If they are not, the situation is quite often a nightmare (except if
it is known for sure that only one is corrupted) because the FATs
contain vital information about cluster allocation to files.

For the sake of keeping explanations as simple as possible (I hope!)
we need an example. Here is a hexadecimal dump of the (first) FAT of
a disk (1024 = 400h bytes) :


                    0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F

           0000    FD FF FF 03 40 00 05 60 00 07 80 00 09 A0 00 0B
           0010    C0 00 0D E0 00 0F 00 01 11 F0 FF 13 40 01 15 60
           0020    01 17 80 01 19 A0 01 1B C0 01 1D E0 01 1F 00 02
           0030    21 20 02 23 40 02 25 60 02 27 80 02 29 A0 02 2B
           0040    C0 02 2D F0 FF 2F 00 03 31 20 03 33 40 03 35 60
           0050    03 37 80 03 39 A0 03 3B C0 03 3D E0 03 3F 00 04
           0060    41 20 04 43 40 04 45 F0 FF FF 8F 04 49 F0 FF 4B
           0070    C0 04 4D E0 04 4F 00 05 51 20 05 53 F0 FF 55 60
           0080    05 57 F0 FF 59 A0 05 5B F0 FF 5D E0 05 5F 00 06
           0090    61 20 06 63 F0 FF 65 60 06 67 80 06 69 A0 06 6B
           00A0    C0 06 6D E0 06 6F 00 07 71 20 07 FF 4F 07 75 60
           00B0    07 77 80 07 79 A0 07 7B C0 07 7D F0 FF 7F 00 08
           00C0    FF 2F 08 83 40 08 85 60 08 87 80 08 89 A0 08 8B
           00D0    C0 08 8D E0 08 FF 0F 09 91 20 09 93 40 09 95 60
           00E0    09 97 F0 FF 99 A0 09 9B C0 09 9D E0 09 9F 00 0A
           00F0    FF FF FF A3 40 0A A5 60 0A A7 80 0A FF AF 0A FF
           0100    CF 0A AD E0 0A AF 00 0B B1 20 0B B3 40 0B B5 60
           0110    0B B7 80 0B B9 A0 0B FF CF 0B BD E0 0B BF 00 0C
           0120    C1 20 0C C3 40 0C C5 60 0C C7 80 0C C9 A0 0C CB
           0130    C0 0C CD E0 0C CF 00 0D D1 20 0D D3 40 0D D5 60
           0140    0D D7 80 0D D9 A0 0D DB C0 0D DD E0 0D DF 00 0E
           0150    E1 20 0E E3 40 0E E5 F0 FF E7 80 0E E9 A0 0E EB
           0160    C0 0E ED E0 0E FF 0F 0F F1 20 0F F3 40 0F F5 60
           0170    0F F7 F0 FF F9 A0 0F FB C0 0F FD F0 FF FF F0 FF
           0180    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
           0190    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

           ....    .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..

           03F0    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00



The first three bytes of the FAT do not seem to be of any use.

Remember that there are  354  clusters (numbered from  2  to  163h).
The FAT provides a 12-bit entry for each cluster (12-bit numbers can
be written as 3-digit hexadecimal numbers).

The following algorithm may be used to get the entry for cluster k :

Ŀ
  (i)  get the word w at the offset   (k * 3) div 2                 
           (keeping in mind that words are stored byte swapped,     
           i.e. least significant byte first)                       
                                                                    
  (ii) if k is even then get rid of the first hex digit of w        
       if k is odd  then get rid of the last  hex digit of w        



Using the algorithm and the sample FAT above, one get the results :


                  cluster no      12-bit entry
               
                     002h            003h
                     003h            004h
                     004h            005h
                     005h            006h
                     006h            007h
                     007h            008h
                     008h            009h
                     009h            00Ah
                     00Ah            00Bh
                     00Bh            00Ch
                     00Ch            00Dh
                     00Dh            00Eh
                     00Eh            00Fh
                     00Fh            010h
                     010h            011h
                     011h            FFFh
                     ....      .      ....
                     100h            000h
                     ....      .      ....


The values of the FAT  12-bit entries are interpreted as follows :


   Ŀ
        value                       meaning                    
   Ĵ
        000h         cluster available (i.e. unused cluster)   
   Ĵ
      FF0h..FF6h     reserved cluster                          
   Ĵ
        FF7h         cluster marked bad                        
   Ĵ
      FF8h..FFFh     last cluster of file                      
   Ĵ
      002h..163h     next cluster in file                      
   


Now, using the directory (or subdirectory) entry for a file and the
FAT, it is quite easy to trace the chain of cluster used by that file.

For example, the first root directory entry of the disk from which the
sample FAT above comes from is for the (standard system) file IO.SYS.
The first directory entry shows that the starting cluster for IO.SYS
is cluster  2. Tracing through the FAT, one realizes that

   the 2nd cluster for IO.SYS is cluster  3  (because the 12-bit entry
                                             for cluster  2  is  3)
   the 3rd cluster for IO.SYS is cluster  4  (because the 12-bit entry
                                             for cluster  3  is  4)
   ...................................................................
   the last cluster for IO.SYS is cluster  11h
                                             (because the 12-bit entry
                                             for cluster  11h  is 0) .


So, IO.SYS occupies contiguous clusters (as required by the bootstrap
program). For other files there is no contiguity requirement.


When a file is erased, all its clusters are freed (i.e. the correspon-
ding FAT  12-bit entries are changed into 000).



7. THE FINE STRUCTURE OF TRACKS

   When access is given to a disk, it rotates quite fast ; its speed
is 300 rpm (in other words, only  60/300 = 0.2 second is required to
perform a complete revolution).
The data transfer rate is  250Kbits/second (therefore  8/250000 =
32 microseconds are required to read/write a byte).

The sector (512 data bytes) is the fundamental unit of disk informa-
tion.
When a sector read/write operation is performed, the read/write head
is mechanically positioned to the proper track. Now, the disk
controller has to find the location of the sector to be used.
This can be done because, in addition to its 512 data bytes, each
sector includes a certain amount of overhead information which
provides sector identification bytes.

The analysis of the fine structure of sectors (to be described below)
also reveals the presence of a gap. This gap provides both an interval
for switching the drive electronics from reading or writing and
compensation for rotational speed.

It turns out that each sector actually uses  574  bytes!

                   The fine structure of a sector

Ŀ
   00                                                             
   ..               12 bytes of 00                                
   00                                                             
Ĵ
   A1h                                                            
   A1h              3 bytes of A1h                                
   A1h                                                            
Ĵ
   FEh              1 byte of FEh                                 
Ĵ
    C             Cylinder No          (1 byte)                   
    H             Head No              (1 byte)                   
    R             Sector No            (1 byte)                   
    N             data size byte       (1 byte)      (*)          
Ĵ
   CRC1           2 Cyclic Redundancy Check bytes                 
   CRC2                                              (**)         
Ĵ
   4Eh      G                                                G    
   ..       A             22 bytes of 4Eh                    A    
   4Eh      P                                                P    
Ĵ
   00                                                             
   ..               12 bytes of 00                                
   00                                                             
Ĵ
   A1h                                                            
   A1h              3 bytes of A1h                                
   A1h                                                            
Ĵ
   FBh              1 byte of FBh                                 
Ĵ
   data             512 bytes                                     
  bytes                                                           
Ĵ
   CRC3           2 Cyclic Redundancy Check bytes                 
   CRC4                                              (**)         


(*)   the standard value of N is 2, meaning that there are
      128*2^N = 512 data bytes ;

(**)  CRCs are used to detect errors ; as some mathematics is involved,
      they will be discussed in Appendix A.

Although tracks are circles they all have a starting point. This fact
needs some explanation :
There is a hole in a diskette near the central hub (index hole). While
the diskette rotates a photo-sensor detects its passage and sends a
signal to the controller defining the start of track information.

Whe are now ready to describe the fine structure of a track.


                   The fine structure of a track

Ŀ
   4Eh                                                            
   ..               80 bytes of 4Eh                               
   4Eh                                                            
Ĵ
   00                                                             
   ..               12 bytes of 00                                
   00                                                             
Ĵ
   C2h                                                            
   C2h              3 bytes of C2h                                
   C2h                                                            
Ĵ
   FCh              1 byte of FCh                                 
Ĵ
   4Eh                                                            
   ..               50 bytes of 4Eh                               
   4Eh                                                            
Ĵ
 sector                                                           
    1               574 bytes                                     
Ĵ
   4Eh      G             80 bytes of 4Eh                    G    
   ..       A                                                A    
   4Eh      P       gap between sectors 1 and 2              P    
Ĵ
 sector                                                           
    2               574 bytes                                     
Ĵ
   4Eh      G             80 bytes of 4Eh                    G    
   ..       A                                                A    
   4Eh      P       gap between sectors 2 and 3              P    
Ĵ
/         /                                                         /
/         /                                                         /
/         /                                                         /
Ĵ
 sector                                                           
    9               574 bytes                                     
Ĵ
   4eh              bytes of 4Eh                                  
   ..                                                             
   4Eh              to end of track                               




8. ANGULAR POSITIONS

   We already know that it takes  0.2 second for a diskette to perform
a complete revolution and that the transfer rate is  250Kbits/second.
Therefore a track holds  * about *
            250000 * 0.2 / 8 = 6250 bytes
(9 * 512  data bytes and organizational bytes). We said  * about *
because the disk speed can vary (the drive electronics can cope with
small variations of the rotational speed).

So, a track can be regarded as an   array[0..6249] of byte .
Looking at the above table one realizes that

      sector 1  starts at the offset  146
      sector 2  starts at the offset  146 + 654

and, more generally :

      sector R  starts at the offset  146 + (R - 1) * 654 .

Let us define the angular position (expressed in degrees) of the byte
at offset  i  by the formula :

                    i * 360 / 6250 .

For programming reasons (to be discussed later on), it is easier (and
more reliable) to locate the  CHRN  field of a sector (rather than
its starting point).

The  CHRN  field of sector 1 starts at the offset 162 and, more
generally the  CHRN  field of sector  R  starts at the offset :

                 162 + (R - 1) * 654 .

Thus we have :

              sector            angular position of CHRN
                                field (degrees)
           
                1              about      9
                2              about     47
                3              about     85
                4              about    122
                5              about    160
                6              about    198
                7              about    235
                8              about    273
                9              about    311


We have devoted a section to angular positions because some disk
utilities (e.g. Disk Explorer - Quaid Software Ltd) provide the user
with such information.


9. CLOCK BITS

   Of course, any write operation turns out to write bits to the
diskette. For example, writing two (consecutive) bytes of  CBh
amounts to writing the sequence of (standard) bits :

                1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1

For technical reasons (aimed at storage efficiency) the disk drive
technology adds extra bits (clock bits) between each pair of standard
bits.
The clock bit is  1  between two  0  bits and  0  between any other
pair of bits.
So, in the example given above

standard bits        1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1
clock bits            0 0 1 0 0 0 0 0 0 0 1 0 0 0 0

the actual recording is    1 0 1 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 1  ...

There are exceptions to the rule on clock bits :

  the 6 bytes of  A1h  in the fine structure of a sector
                          (see section 7)

  the 3 bytes of  C2h  in the fine structure of a track
                          (see section 7)

contain some "wrong" clock bits (some  1  clock bits are changed to 0).


It is (of course) quite easy to read the (512 * 9) data bytes on a
track.


What about reading  * all *  the bytes on a track ?
Although the disk controller is able to read organizational bytes, it
does not mean that the programmer can easily have access to non data
bytes. The disk controller acts like a black box within the computer,
executing procedures of its own and revealing (through ports)  * only
some *  of its results to the computer (just as local variables are
not accessible from the main program in Pascal).
If one turns to non standard (tricky) reading methods, the results are
partially unreliable. This is because shifts can occur and clock bits
rather than normal bits are read.