From : Inbar Raz                                            2:403/100.42
Subj : Formatting floppies.                                                  

 >   2. What's the relationship between physical sector numbers and cluster
 >      numbers? If a sector is bad how do I determine which cluster it's
 >      going to belong to? (for updating the FAT)

You have to take into account the following facts:

1. The first cluster number is ALWAYS ___2___. There is no cluster 1 and 0.
2. Clusters refer to _file data_ only. The clusters only point file data, and
   do not include boot sector, FAT and root directory.

   Therefore:

   ClusterNumber:=

((current_track * sectors_per_track) + sector_Number) - Reserved sectors
-------------------------------------------------------------------------
                          sectors_per_cluster

Where reserved sectors contains, according to the Boot Parameter Block:

1. Boot Sector
2. Reserved Sectors (almost always 0)
3. FAT sectors * copies of FAT
4. Root Directory sectors

 >   3. What's the formula to determine the offset of a cluster in
 >      a 12bit FAT?

 >      This is what I got:     fatoffset=(cluster+(cluster>>1));
 >      And to mark it bad:     if (cluster&1)     /* odd cluster number */
 >                                {
 >                                fat1[fatoffset]  |= 0xF;
 >                                fat1[fatoffset+1] = 0xF7;
 >                                }
 >                              else
 >                                {
 >                                fat1[fatoffset]   = 0xFF;
 >                                fat1[fatoffset+1] = 0x70;
 >                                }

This is what I know:

[Quotes from TechHelp!]
---------------------------------= cut here =---------------------------------
Ŀ
 Reading the FAT  To read the value of any entry in a FAT (as when following
 a FAT chain), first read the entire FAT into memory and
 obtain a starting cluster number from a directory.  Then, for 12-bit entries:
                                                               
 Multiply the cluster number by 3 ͻ
 Divide the result by 2    (each entry is 1.5 (3/2) bytes long)
 Read the WORD at the resulting address (as offset from the start of the FAT)
 If the cluster was even, mask the value by 0fffH (keep the low 12 bits)
  If the cluster number was odd, shift the value right by 4 bits (keep the
  upper 12 bits)
 The result is the entry for the next cluster in the chain (0fffH=the end).

Note: A 12-bit entry can cross over a sector boundary, so be careful with
      1-sector FAT buffering schemes.

 16-bit entries are simplereach entry contains the 16-bit offset (from the
 start of the FAT) of the next entry in the chain (0ffffH indicates the end).

Ŀ
 FAT Content  The first byte of the FAT is called the Media Descriptor or
 FAT ID byte.  The next 5 bytes (12-bit FATs) or 7 bytes
 (16-bit FATs) are 0ffH.  The rest of the FAT is composed of 12-bit or 16-bit
 cells that each represent one disk cluster.  These cells will contain one of
 the following values:

     (0)000H ................... an available cluster
     (f)ff0H through (f)ff7H ... a reserved cluster
     (f)ff7H ................... a bad cluster
     (f)ff8H through (f)fffH ... the end of an allocation chain
     (0)002H through (f)fefH ... the number of the next cluster in a chain

---------------------------------= cut here =---------------------------------

Hope this helps.

Inbar Raz

