Where is the OS located on the disk and how does the bootloader find it?

This may be OS specific. I'm not sure (I'm working on Linux). When the BIOS loads the bootloader into memory and starts running it, how does it find the OS? Is it saved in a special disk partition / known sector, etc.? If it is stored on the filesystem, the bootloader must interact with the filesystem code that is stored in the OS. How to solve this chicken egg problem?

+3


source to share


2 answers


The bootloader falls into several parts. First of all, the BIOS extracts some code that follows a very special format called MBR (Master Boot Record). Remember your old computer: you have to tell the BIOS in which order it will look for some device in search of that MBR. After it finds the MBR (MBR is 512 bytes long, ends with a magic number - 0x55aa

), it copies this piece of code at a given offset into physical memory ( 0x7c00

on x86) and sets the Pointer instruction to that address. In most cases, the MBR contains a partition table and loads additional code from the device to help boot the real system: this is called chained loading or multistage loading.

Let's take a look at the last point. Remember that the system is operating in real mode at this point, so you can only access 1MB of physical memory, but on the other hand, you can easily access your hard drive with a BIOS interrupt call. Operating systems currently ask for much more memory than 1MB, and they will want to switch the processor in protected mode to access the full address space (4GB, at least on a 32-bit system). But once the system is in protected mode, the BIOS Interrupt Call is no longer available, and the I / O from the HDD has to go through a complex link like DMA, and this setting is usually not done in the bootloader. The bootloader switches in real mode to protected mode.It fetches a sector from the hard drive into a 1MB address space and then switches to protected mode to copy that sector somewhere in a 4GB address space and finally it switches back to real mode to fetch another sector from HDD. After all sectors have been extracted and copied to physical memory, the bootloader will go to the OS.

Summarizing:



  • The BIOS looks at the list of devices for the MBR located in the first sector of each device. MBR is detected if the last halword of the first sector matches a magic number. BIOS copies MBR to physical memory and switches to it.
  • The MBR embeds the partition table, so it knows from which location (mainly the cylinder sector on the hard disk) it should load more code into physical memory. This step could be, for example, booting GRUB.
  • Let's say in GRUB the spec is multiboot

    used to find the OS. Check out the link below for more information on multiboot

    .

You can take a look at these resources:

+6


source


The BootPrompt HOWTO is relevant to your question. The BIOS contains the code (in FlashROM) to boot the first sector of your boot disk; the first sector contains a boot loader such as GRUB (or LILO). GRUB loads the kernel into RAM (with some initrd).

BIOS settings are stored in a small (battery-powered) dedicated RAM.



GRUB is a rather complex program today . He knows about the structure of disk partitions and shared file systems. It can read its config file and read the kernel (configured in that file) from the section. It can also load initrd (containing the core kernel modules).

+1


source







All Articles