0

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.

jophab
  • 5,356
  • 14
  • 41
  • 60
Biju Rakhul
  • 115
  • 1
  • 8

1 Answers1

3

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

This is so that once the boot handler has successfully loaded the device’s boot sector into memory at address 0000:7C00h, execution control can be transferred with the following register contents:

  • ES:DI = Pointer to PnP Installation Check Structure
  • DL = Drive number used for the INT 13h (00h, 80h, etc.)

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.

;Simple snippet that load one sector from the booting device

mov ax, LOAD_SEG
mov es, ax
mov bx, LOAD_OFF           ;ES:BX = Where to load the data
mov ax, 0201h              ;AL = How many sectors
mov cx, 0001h              ;CX = Cylinder and sector
xor dh, dh                 ;DH = Head, DL = Driver number (As given by the BIOS)
int 13h
jc _handle_err

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:

  1. It contains the geometry of the driver.
    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.
  2. It contains the information to find the file system.

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

+------------+------------------------------------+
| boot block |              FS block              |
+------------+------------------------------------+

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.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
  • Thank you. What about the values of BPB? they differ from device to device ri8? – Biju Rakhul Dec 21 '16 at 09:13
  • The question belongs to my friend here http://superuser.com/questions/1158589/how-to-get-values-for-bios-parameter-block-in-a-vbr . Please help him if u can. Thank you for your answer – Biju Rakhul Dec 21 '16 at 09:17
  • @BijuRakhul Wikipedia is already doing a grat job at explaining the fields of the BPB. As said, BPB is used to find the FS and define the geometry of the device. The geometry part is relevant only for floppies (HDDs have fixed geometry that you can find by inspecting the BPB on you disk) and the FS part only for NTFS and FAT. Most BPB are fixed, again just format two USB pen drives for example to see what's going on :) – Margaret Bloom Dec 21 '16 at 10:01
  • @BijuRakhul Amend: I forgot about 4KiB HDD. [The BPB generated by Microsoft take that into account](http://writeblocked.org/index.php/19-4096-byte-sector-drives-ntfs-and-forensic-tools.html). HDDs can now have variable geometry. – Margaret Bloom Dec 21 '16 at 10:23
  • At least in the case of Microsoft partition sectors, the boot code relocates itself to 00000h:00600h, scans a table (usually 4) of entries for an "active" partition which could be on any hard drive, then it reads in the boot sector from the "active" partition at 00000h:07c00h and jumps to it The BIOS hard drive device numbering can usually be configured by setting the boot order in a BIOS. The operating system may renumber hard drives after it boots up (for example, Win XP doesn't do this, but Win 7 does). – rcgldr Dec 22 '16 at 17:46