I am studying operating system development. I recently read that the Hard Disk is denoted by 80h and so on. But in an MBR, the MBR must detect multiple HDD to choose booting from.
How is this done? How to detect the availability of multiple Hard Disk Drive and switch to and from it?
I'm using:
Ubuntu
Gas assembler
Note : I would like the answer in Assembly language during Real mode of a system.
Thanks.
The title and the body of your question ask two different things.
Regarding how a boot loader program, or in better terms an IPL (Initial Program Loader), detects which device to boot, you can check this specification to see that in section 6.5.1 it reads
So the IPL is given the driver number of the device is was loaded from.
The IPL can then use it with INT13h to load a second stage bootloader.
Simply put, if you are writing an MBR bootloader, you can find the driver number in
dl
, don't overwrite it before saving it.You are better off using the INT13h extensions rather than the INT13h itself, though for educational purpose the latter is simpler.
If you are writing a VBR bootloader then technically you are on your own, there is no standard interface between the VBR and the MBR code.
However, every MBR bootloader gives at least the driver number to the VBR bootloader (still in
dl
)More metadata is usually passed, but such data is proprietary.
What to do once the MBR/VBR code is loaded is up to the programmer, for example GRUB has an hardcoded LBA (written during the installation).
This LBA is used to load the second stage, using the driver number from the BIOS. The second stage has a small file system driver that is used to read, by their paths, the config file the kernel.
The bottom of the line is that the bootloader has to know, somehow, where to find the rest of the code to load.
This is usually accomplished by writing some metadata in the binary image of the bootloader before writing it to the disk. Another strategy is relying on the MBR code to pass the partition number/start so that, if the second stage is put a fixed number of sector ahead of the first stage, the bootloader knows where it is located on the disk and hence where the second stage is.
Speaking of metadata leads us to the BIOS Parameter Block.
Despite the name, it is not created by the BIOS, but it is the result of the installation/format process of an OS.
It is not strictly necessary for custom loaders, it was used for two reasons:
For floppies this was important, as the software had to tell the FDC the geometry to use (i.e. how big a sector was).
Now this is getting relevant again due to the presence of 4KiB sector HDDs.
Some BIOS assume the presence of a BPB and may attempt to patch same values, especially when booting from USB, so it is a good idea to include at least a DOS 3.31 BPB like NT bootloaders do.
In general, the first thing to do is decide the layout of a partition, this includes the FS type and the VBR format.
The general format is
Some space is reserved at the beginning of the partition for the bootloader code, the VBR contains a pointer to the FS block.
The bootloader can load the kernel directly if the FS is simple enough (this may be the case for toy OSes)or has special support, thus shrinking the boot block to the first sector only.
As an alternative, the bootloader loads a second stage that will have minimal, yet general, FS driver to locate the kernel by name.
Still speaking generally, the VBR code is not completely independent of the FS type of the volume.
For example, to install GRUB on an NTFS partition, you need to use a special version of it, GRUB4DOS that doesn't use the classic second stage.