This post is a continuation from the previous post [Writting a Bootloader] which was an attempt to build a Bootloader for the x86 architecture.
Although the executable size is limited to 512 Bytes, there are several important tasks that can be accomplished during booting process. One of them is to read the Geometry from several storage devices.
Interrupt 13h [INT 13h] provides sector-based disk access services using the CHS addressing method (Cylinder/Head/Sectors). This values provide the physical location of the data (although this is not completely true nowadays).
The code below is an example of this interrupt. It reads the CHS from the first Hard Disk using Interrupt 13h.
Briefly, interrupt parameters are set in AX and DX registers. After interrupt is completed, its values can be read from the appropriate registers (CX, DX). The rest of the code includes helper functions to print numbers and strings to present results properly.
After booting our virtual system using the code, we get the following result:
Hard Drive Information
Disk Geometry (C/H/S): 130/16/63
Comparing these results with fdisk on the same drive:
Disk /dev/sdb: 67 MB, 67108864 bytes
255 heads, 63 sectors/track, 8 cylinders, total 131072 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
I am still trying to figure why these values are so different. Anyway, the total size of the drive taking the total oh Cylinders/Heads/Sectors will be close to the drive size described by fdisk.
Finally, notice that 08h service is limited to drives up to 500 MB. If there's required to read properties from bigger drives, service 48h is the appropriate.
As exercise, following improvements can be done:
- Report an error if reading fails
- Read properties from all connected drives
- Determine drive size in LBA format
- Modify code to read bigger drives