Questions on the legacy loading process (MBR -> VBR ->)
Posted: Tue Feb 20, 2018 5:34 pm
This is more related to the OS bootloader, but it seemed more fitting here than in other forums.
Before I attempt to load my OS via EFI, I wanted to give the "legacy" mode a shot by implementing a realmode loader. I have gotten to the point where I can get execution and read blocks from memory via int13, so the rest is a design chore. From what I understand, the MBR code has the responsibility of reading the MBR table from the first sector of disk, determining which partition is "active" and then loading the VBR from the first sector. Then the VBR loads the next stage from disk, which can be of arbitrary size. This loader may then have logic to support reading from filesystems and parsing its own configuration file (e.g. GRUB).
I think I understand the execution flow, but I am less keen on the details of how to locate these various things on disk. The MBR is easy: it gets loaded from a fixed location. The VBR again, is loaded from the location specified in the active table entry. However, is this a "known" location? Namely, is there are standard for how much memory to reserve after the MBR such that we know where the VBR will be located? Of course, the VBR is not necessarily located in the first partition. I have seen older tools like `fdisk` reserve the first 2048 sectors (1MiB) before the first partition begins. However, I have also read that this is a historical artifact lending itself to old alignment requirements.
In essence, my questions are:
- What regions/how much memory are available at this point? The MBR is loaded at 0x7c00 (as is the VBR), but what about for a stack and other memory needs (like blocks from disk)?
- How does the VBR know where to find the next/final bootloader stage? It is also limited to 512 bytes in size, so it cannot contain logic to parse filesystems.
To test my code I have been creating an MBR with a FAT partition and just dd'ing my realmode loader code into the first sector (making sure to not overwrite the table and signature). I have not quite gotten to the VBR stage yet, but I imagine it would be something similar. However, I am unsure of the format of a partition that also includes a VBR.
Before I attempt to load my OS via EFI, I wanted to give the "legacy" mode a shot by implementing a realmode loader. I have gotten to the point where I can get execution and read blocks from memory via int13, so the rest is a design chore. From what I understand, the MBR code has the responsibility of reading the MBR table from the first sector of disk, determining which partition is "active" and then loading the VBR from the first sector. Then the VBR loads the next stage from disk, which can be of arbitrary size. This loader may then have logic to support reading from filesystems and parsing its own configuration file (e.g. GRUB).
I think I understand the execution flow, but I am less keen on the details of how to locate these various things on disk. The MBR is easy: it gets loaded from a fixed location. The VBR again, is loaded from the location specified in the active table entry. However, is this a "known" location? Namely, is there are standard for how much memory to reserve after the MBR such that we know where the VBR will be located? Of course, the VBR is not necessarily located in the first partition. I have seen older tools like `fdisk` reserve the first 2048 sectors (1MiB) before the first partition begins. However, I have also read that this is a historical artifact lending itself to old alignment requirements.
In essence, my questions are:
- What regions/how much memory are available at this point? The MBR is loaded at 0x7c00 (as is the VBR), but what about for a stack and other memory needs (like blocks from disk)?
- How does the VBR know where to find the next/final bootloader stage? It is also limited to 512 bytes in size, so it cannot contain logic to parse filesystems.
To test my code I have been creating an MBR with a FAT partition and just dd'ing my realmode loader code into the first sector (making sure to not overwrite the table and signature). I have not quite gotten to the VBR stage yet, but I imagine it would be something similar. However, I am unsure of the format of a partition that also includes a VBR.