开发者

LBA and cluster

I wonder about LBA and cluster number.

My question is this:

  1. is LBA 0 always cluster 2?

  2. then what does cluster 0 and 1 for?

  3. only difference between cluster and LBA is just where do they start from the disk?

  4. relation among CHS, LBA, cluster nubmer?

  5. and in the flowing code, what does add ax, WORD [datasector] code for?

    ;************************************************;
    ; Convert CHS to LBA
    ; LBA = (cluster - 2) * sectors per cluster
    ;******************************************开发者_Python百科******;
    
    
    ClusterLBA:
              sub     ax, 0x0002                          ; zero base cluster number
              xor     cx, cx
              mov     cl, BYTE [bpbSectorsPerCluster]     ; convert byte to word
              mul     cx
              add     ax, WORD [datasector]               ; base data sector
              ret
    


There are many sector numbering schemes on disk drives. One of the earliest was CHS (Cylinder-Head-Sector). One sector can be selected by specifying the cylinder (track), read/write head and sector per track triplet. This numbering scheme depends on the actual physical characteristics of the disk drive.

The first logical sector resides on cylinder 0, head 0, sector 1. The second is on sector 2, and so on. If there isn't any more sectors on the disk (eg. on a 1.44M floppy disk there's 18 sectors per track), then the next head is applied, starting on sector 1 again, and so on.

You can convert CHS addresses to an absolute (or logical) sector number with a little math:

L = (C * Nh + H) * Ns + S - 1

where C, H ans S are the cylinder, head and sector numbers according to CHS adressing, while Nh and Ns are the number of heads and number of sectors per track (cylinder), respectively. The reverse calculation (to convert LBA to CHS) is as simple as this.

In this numbering scheme, which is called LBA (Logical Block Addressing), each sector can be identified by a single number. The first logical sector is LBA 0, the second is LBA 1, and so on. This scheme is linear and easier to deal with.

Clusters are simply groups of continuous sectors on the disk, which are treated together by the operating system and the file system, in order to reduce disk fragmentation and disk space needed for file system metadata (eg. to describe in which sectors could a specific file found on the disk). A cluster may consist of only 1 sector (512 bytes), up to 128 sectors (64 kilobytes) or more, depending on the capacity of the disk.

Again, the logical sector number of the first sector of a cluster can be easily calculated:

L = ((U - Sc) * Nc) + Sd

where U is the cluster number, Nc is the number of sectors in a cluster, Sc is the first valid cluster number, and Sd is the number of the first logical sector available for generic file data. The latter two parameters (Sc and Sd) are completely filesystem and operating system specific values.

Some filesystems (for example FAT16, and the whole FAT-family) reserve cluster number 0 and 1 for special purposes, that's why the first actual cluster is cluster number two (Sc = 2 in this case). Similarly, there may be some reserved sectors in the beginning of the disk, where no data is allowed to be written to and read from. This reserved area can range from a few sectors (e.g. a boot record) to millions of sectors (e.g. a completely different partition which preceeds our partition on the hard disk).

Huh, this was the long answer. After all, the short answers to your questions can be summarized as follows:

  1. No, LBA 0 is not always cluster 2, it's filesystem specific (in case of FAT, cluster 2 is the first available sector on the disk, but not always LBA 0 - see answer 5).

  2. Interpretation of cluster number 0 and 1 are also filesystem specific (in case of FAT, cluster number 0 represents an empty cluster in the File Allocation Table, and cluster number 1 is reserved).

  3. No, the main difference is that a cluster number addresses a group of continous sectors, while LBA addresses a single sector on the disk.

  4. See the formulas (formulae?), and the accompanying description in the long answer above.

  5. It's hard to tell from such a short assembly code, but my best guess would be the number of reserved sectors in the beginning of the partition (noted by Sd in the formula above).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜