assemblyoperating-systemkernelx86-16mbr

How to access the CHS information from the partition table of MBR?


I am creating a basic MBR that reads the Partition Table and loads the first sector of the Bootable Partition. I am confused by the CHS and LBA addressing modes. I read in a tutorial that CHS is more or less not used except for compatibility reasons. So, I opted to use LBA(INT 13h extensions). To load the sector of the drive, I have to read the CHS value from the Partition table of that partition. Only then I could convert it to LBA and store it in "Disk Address Packet".

My doubt is, The CHS in Partition table is 1 byte each. Should I take this value directly and convert it into LBA (or) I have to first convert it into,

Cylinder = 10 bits
Sector   = 6 bits
Head     = 8 bits

and then convert it into LBA?

Note: My MBR must be able to access sectors on both HDD and USB - That is the motive.

OS : Ubuntu

Assembler : Gas


Solution

  • Partition table entries use CHS only for backward compatibility with very old OSes.
    They also have fields for the LBA address:

     
     
                        Format of the MBR partition table

     
     

    Note that this use LBA32, thus there is a limit on the partition size and position. Quoting Wikipedia:

    Since block addresses and sizes are stored in the partition table of an MBR using 32 bits, the maximum size as well as the highest start address of a partition using drives that have 512-byte sectors (actual or emulated) cannot exceed 2 TiB−512 bytes (2,199,023,255,040 bytes or 4,294,967,295 (232−1) sectors × 512 (29) bytes per sector).[2] Alleviating this capacity limitation was one of the prime motivations for the development of the GPT.

    The same is also true for CHS addressing.
    OSes that use the CHS fields have a limitation of about 8 GiB in size.
    So if you want to be compatible with them, you need to stay under that limit.


    The formula for converting LBA <-> CHS can be easily derived or found on the Internet, you can think in term of sector numbers (i.e. LBA) and convert into CHS when creating the partition entry (or use the defaults 1023, 255, 63 tuble for partition too big for CHS).

    Bottom of the line, use the LBA fields.
    Or go for GPT.