LBA

From OSDev.wiki
Jump to navigation Jump to search

LBA stands for Linear Block Address. It is a way of determining the position of a block (usually on a storage medium). Most storage devices support specifying LBA's in some way or another.

Uses

LBA's are used to access certain blocks on a storage medium. The LBA is called 'linear' because if you try to access LBA + 1, you will in fact access the block after the block at LBA. Devices such as hard drives usually support LBA's by specifying them directly in I/O ports in some way or another.

LBA's can be stored in several different formats. Sometimes they are stored relative to the start of a partition or filesystem. They may also be stored in an absolute way (from the beginning of the storage device). LBA's are almost always dependant on the block size for the device in question. Suppose we have a hard drive with 512 bytes per sector (or bytes per block), then an LBA of 50 would specify the sector containing bytes (50 * 512) to (51 * 512 - 1).

Comparison with CHS

CHS (Cylinder Head Sector) is an older way of addressing a storage device. Devices such as floppies usually have no direct way of being accessed with LBA's. Instead, they require you to specify the cylinder, the head, and the sector you want to access individually. However, since LBA's are easy to use, it is not uncommon to provide a conversion between the two.

For example, one could convert an LBA for a floppy drive to the CHS with the following code snippet (based on Brokenthorn's Floppy tutorial, see the link below):

void ToCHS(int lba, int *head, int *track, int *sector)
{
	(*head) = (lba % (SECTORS_PER_TRACK * 2)) / SECTORS_PER_TRACK;
	(*track) = (lba / (SECTORS_PER_TRACK * 2));
	(*sector) = (lba % SECTORS_PER_TRACK + 1);
}

See Also

External