Page 1 of 1

understanding bootloaders

Posted: Thu May 13, 2021 6:30 pm
by newosdeveloper2021
How do bootloaders detect devices, peripherals, etc. on boot? I know sometimes it's designed and/or hardcoded for only certain stuff(as in some laptops), but how would you go about finding text mode buffer, refresh/polling rate of monitor/display, etc..

Also, how do the EDK II/gnu-efi libraries work? Like how do they detect and write to devices such as NIC, GPU, CPU..

How do CPUs and proprietary drivers detect cpu temperature, etc.?

The guides and tutorials skim this and tell you to figure it out and/or use a library and dont do anything too low-level

Re: understanding bootloaders

Posted: Thu May 13, 2021 6:57 pm
by neon
Hi,

From the perspective of a boot loader, I only ever obtain information by querying available firmware interfaces: disk drives are scanned using the firmware device ID space int 13h services (BIOS) or EFI boot services, video and EDID information is obtained from VBE (Legacy BIOS) or GOP (EFI). The questions here are very broad: they work by querying the respective firmware service and may attempt a fallback if not available (serial debug out, pci scanning for network device, etc.)

Re: understanding bootloaders

Posted: Fri May 14, 2021 7:30 am
by bzt
newosdeveloper2021 wrote:How do bootloaders detect devices, peripherals, etc. on boot?
Short answer: they don't. Firmware does, and loaders just ask the firmware what devices it had detected.
newosdeveloper2021 wrote:I know sometimes it's designed and/or hardcoded for only certain stuff(as in some laptops), but how would you go about finding text mode buffer,
Under BIOS with VGA cards, that's always 0xB8000. Under UEFI, text mode buffer isn't supported at all.
newosdeveloper2021 wrote:refresh/polling rate of monitor/display, etc..
Bootloaders do not need these. But if they want, they could ask the firmware (BIOS/UEFI) for such an information.
newosdeveloper2021 wrote:Also, how do the EDK II/gnu-efi libraries work? Like how do they detect and write to devices such as NIC, GPU, CPU..
UEFI is designed in a way that everything is communicating through "protocols" (which aren't protocols at all, just interfaces). So if you want to know how many video cards are in the system, you ask the firmware to return handles to GOP interfaces. Likewise, if you want to know how many disks there are, you ask the firmware to return handles to block IO protocol interfaces.
"Writing" to a device is calling a method within a particular interface instance.
newosdeveloper2021 wrote:How do CPUs and proprietary drivers detect cpu temperature, etc.?
Bootloaders do not need these. And kernels have their own driver for the specific sensor device.
newosdeveloper2021 wrote:The guides and tutorials skim this and tell you to figure it out
Our wiki (pages like boot seq, bootloader) is the best source (follow the links like rolling your own bootloader, etc.). For BIOS, see RBIL. For UEFI, see the official spec. For a tutorial, check out the source of an Open Source bootloader, like Limine, BOOTBOOT, or GRUB (however this latter is a "bit" bloated, easy to get lost in its source).
newosdeveloper2021 wrote:use a library and dont do anything too low-level
For a library, take a look at GNU-EFI (gcc only) or POSIX-EFI (works with gcc and Clang too). But don't get high hopes about not doing anything too low-level, because writing an OS is all about being as low level as possible.

Cheers,
bzt