Live data from Hacker News

Ask HN: Where can I find a primer on how computers boot?

news.ycombinator.com

51–60 of 96 posts

Re: Ask HN: Where can I find a primer on how computers boot?

#51
I wonder if learning how a more simple microcontroller boots might help you.

I had the same questions as you around a year ago. As other commenters mentioned, modern hardware is incredibly complex and needs to go through many, many steps before it can boot a kernel. Modern CPUs have a bunch of legacy requirements that still exist to keep backward compatibility with older hardware.

One good example of these legacy requirements is the A20 line on x86 [1]. A20 can be considered a boolean flag that determines whether the CPU can access more than 1MB of RAM. The A20 flag was introduced in the Intel 286 because its predecessor, the 8086 had a limit of 1MB of RAM. The 286 needed to access 16MB of RAM, so Intel introduced the A20 flag that determines whether the CPU can access memory past 1MB. The A20 line still exists in x86_64 CPUs today, so it needs to be enabled before the entire RAM address space is used.

There are a TON of legacy requirements like the A20 line on modern CPUs (especially x86_64). It can make it very difficult to figure out what is going on in the boot process. For me, I found it much easier to start learning how simple pieces of hardware works before moving on to x86. The boot process for Raspberry Pi's RP2040 microcontroller is explained in the datasheet [2] and I found it to be a great resource to figure out which hardware needs to be initialized and what that actually means.

Just like x86, microcontrollers have many tasks they need to complete before loading the main program. But these steps are much less ambiguous and are described very well in the RP2040 datasheet. It lists every step that it goes through before the main program starts (including stuff like initializing the clocks to specified speeds). After I felt like I thoroughly understood how the RP2040 boots, it became much easier to understand why the bootloader needs to complete certain tasks.

Not sure if you are familiar with assembly, but you can also checkout part of the RP2040's bootloader here [3]. There are a few different bootloaders for the RP2040, the one I linked is the one referenced in the datasheet. Its purpose is to load the main program from a specified location in the flash memory chip.

Remember that the boot process on x86 processors has been changing for around 40 years, so expect it to take a while before you feel more comfortable with the terms used in bootloading and hardware. I've been learning this stuff for around a year and still feel like I only understand a fraction of the x86 boot process. But I find it so fascinating that I can't help but want to learn more of its complexity.

I love talking about hardware and the boot process, so feel free to let me know if you have any more questions! :)

[1] https://en.wikipedia.org/wiki/A20_line

[2] https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.p...

[3] https://github.com/rp-rs/rp2040-boot2/blob/main/src/boot2_w2...

Re: Ask HN: Where can I find a primer on how computers boot?

#52

I wondered about an adjacent topic recently ("what IS the kernel actually") and found out that a great way to understand it, would be to start with the boot process. Once I felt less lost, I tried to capture my learnings [1] The text is not edited yet, but as I had the benefit of learning about it with fresh eyes, it should be very approachable (and hopefully accurate enough) [1] https://vsupalov.com/demystifying-ker…

> It’s firmware time. Your machine has a motherboard, there is a chip on it which has had BIOS (Basic Input/Output System) flashed on it. I wonder if it's worth it to call out the "processor microcode" firmware? Any cool startup things happen at that low level as well?

Way out of my depth! However, the stuff I saw the folks from Oxide [1] talk about comes to mind. Hardware stuff with Rust. If you want to look at a specific project, Hubris [2] is a kernel for "deeply-embedded" systems.

[1] https://oxide.computer/blog/building-big-systems-with-remote...

[2] https://github.com/oxidecomputer/hubris

Re: Ask HN: Where can I find a primer on how computers boot?

#53

I wondered about an adjacent topic recently ("what IS the kernel actually") and found out that a great way to understand it, would be to start with the boot process. Once I felt less lost, I tried to capture my learnings [1] The text is not edited yet, but as I had the benefit of learning about it with fresh eyes, it should be very approachable (and hopefully accurate enough) [1] https://vsupalov.com/demystifying-ker…

> It’s firmware time. Your machine has a motherboard, there is a chip on it which has had BIOS (Basic Input/Output System) flashed on it. I wonder if it's worth it to call out the "processor microcode" firmware? Any cool startup things happen at that low level as well?

The microcode is mostly irrelevant, and is also opaque. It might be doing something but Intel won't give you more details beyond the programmer's reference model.

"Microcode" generally looks more like expanding a CISC instruction into other instructions, rather than something that looks like a program.

Extremely low level paper: https://www.researchgate.net/publication/295010710_Booting_a... ; good to confirm that Intel systems still boot at 0xFFFFFF0 in real (16-bit!) mode. It also points out that microcode updates are applied after execution has started.

There is also the fun of "cache as RAM"; it's usually quite a time consuming operation to get the DRAM controller up and running and "trained" to the particular signal properties of the motherboard, so the early boot phase has no RAM.

Re: Ask HN: Where can I find a primer on how computers boot?

#54

The following is a pretty decent historical page about the pre-(U)EFI MBR (Master Boot Record) boot process: https://thestarman.pcministry.com/asm/mbr/STDMBR.htm Note that EFI/UEFI -- occurred much later in time than MBR... The MBR boot process is also called "Legacy Boot" -- and is emulated on (U)EFI -- although it may not show as an option on some (U)EFI BIOS'es if the option is turned off... Related: https://en.wi…

I also recommend the Starman for good documentation over the decades.

Basically you're supposed to know BIOS/MBR booting quite well in order to make the best progress on UEFI/GPT.

Originally upon powerup the BIOS would run from ROM and then boot control to a program known as the Master Boot Record which would reside on the first 440 bytes of sector 0 of the HDD. The partition table took up the final bytes of sector 0. The MBR checked which of the 4 possible primary partitions was flagged as active, and then booted control to the Volume Boot Record (Boot Sector) residing at the first sector of that particular partition which would then (usually, but specifically according to BOOT.INI on Windows [BCD today] or GRUB configs for Linux) load the OS contained within that partition. Usually by reading files from the filesystem beyond that point, but capable of being chained directly to other executable sectors.

Starman can be a very good resource on underlying structures:

https://thestarman.pcministry.com/asm/mbr/

He's got lots more pages like this, and references too.

Now, straight to modern UEFI:

The MBR code itself is best not there on sector 0, just an imitation partition table in the final few bytes still exists which is considered "protective" and keeps BIOS PC's from recognizing the HDD since they can not handle partitions layed out by the GPT regime.

Sector 1 designates GPT drive parameters, and sector 2 begins the defining data for each partition, which by default extends to sector 33 so you have room to define many more than 4 partitions, and any of them can be booted to from a single EFI folder on a hidden ESP partition, depending mainly on the config files in the EFI folder. There are backups of these defining sectors (not the EFI folder itself) in the final area of the HDD.

In the ESP partition which is often expected by MB UEFI to carry a specific ESP GUID (but often works just fine after changing to a "regular" GUID so you can unhide it for maintenance), the UEFI expects to find it formatted as FAT32, and there behold a recognizable EFI/BOOT folder containing the elusive BOOTX64.EFI file. That's the only thing normally found in the EFI/BOOT folder except for systems having bootability by the rare remaining 32-bit UEFI firmwares, then you might see BOOTIA32.EFI. Lots of times it will do just fine having an EFI folder on an NTFS partition too.

UEFI 64-bit firmware just runs whatever file is currently named BOOTX64.EFI in the BOOT folder. Or falls back to secondary alternatives if not present.

When you install an OS like Windows it adds an EFI/MICROSOFT folder, but each time you add a different version of Windows to a different partition, you do not add additional Microsoft folders to EFI, instead you add additional boot entries to the default boot menu already present in the Microsoft BCD file there. The OS installation procedure overwrites the existing BOOTX64.EFI file with one directing the boot process to continue from the Microsoft folder after that point, according to the NT6 boot menu you have in place at the time.

Installing Linux creates an EFI/ubuntu folder for example, containing most necessary GRUB elements, while overwriting BOOTX64.EFI to direct the boot process to the ubuntu folder and use the GRUB bootmenu accessible from there. Each distinct distro will add its own folder like EFI/debian/, EFI/fedora, with the most recently added claiming the most coveted BOOTX64.EFI prize.

Ideally, you install all the Windows versions you want into their designated NTFS volumes, which naturally builds up your NT6 bootmenu as you go. You will best do some BCDEDITing as you go just to keep the Descriptions in the bootmenu straight as to which entry goes to which Windows version on which partition.

Ideally again, your final Linux install will be a distro that automatically detects all other Linux installations residing on their EXT-formatted partitions, while also correctly detecting & adding a bootentry for Windows, so GRUB can take you to your already-established NT6 bootmenu when you want.

In the EFI/Microsoft folder you will find an os-specific file in EFI/Microsoft/BOOT named BOOTMGFW.EFI. Not to be confused with the basic EFI/BOOT/BOOTX64.EFI.

These are EFI executables, either one will boot the PC from UEFI directly if you drop down to the UEFI Shell preboot using your MainBoard tools. Navigate to fsx:EFI/Microsoft/BOOT and run bootmgfw.efi to boot Windows (even without an EFI/BOOT folder present), or navigate to the main EFI/BOOT folder and run the latest BOOTX64.EFI that your final OS install has overwritten for you accordingly. Where "fsx:" is the filesystem which contains the EFI folder as detected by the UEFI firmware, often fs0. You can even find grubx64.efi files for Linux.

Not only OS installs, but things like GRUB updates and BCDBOOT commands can overwrite BOOTX64.EFI.

Finally, once different OS's are properly installed in a traditional self-contained way on each of their own partitions, you should be able to craft any number of alternative EFI folders or contents such that any or all of the OS choices are presented to you on a bootmenu upon powerup, booting to your established choice of default (or fallback) in case of unattended powerup. GRUB can still boot Windows but Windows NT6 bootloader can no longer boot Linux when using GPT.

But you can even still make a boot floppy you can use to load OS's present on the HDD without having any boot files on the HDD at all.

UEFI can also sometimes do well when it finds a good recognizable EFI folder on an MBR-layout HDD.

Re: Ask HN: Where can I find a primer on how computers boot?

#55

I wondered about an adjacent topic recently ("what IS the kernel actually") and found out that a great way to understand it, would be to start with the boot process. Once I felt less lost, I tried to capture my learnings [1] The text is not edited yet, but as I had the benefit of learning about it with fresh eyes, it should be very approachable (and hopefully accurate enough) [1] https://vsupalov.com/demystifying-ker…

> It’s firmware time. Your machine has a motherboard, there is a chip on it which has had BIOS (Basic Input/Output System) flashed on it. I wonder if it's worth it to call out the "processor microcode" firmware? Any cool startup things happen at that low level as well?

There's indeed a lot of firmware (not just microcode). A bunch of code is signature-checked and executed from the boot ROM and firmware flash in order to bring up the CPU and its various auxiliary cores (starting with the security processor/management engine nowadays), the PCH and the DRAM controller - all before the CPU can start executing any bootloader code from RAM.

Re: Ask HN: Where can I find a primer on how computers boot?

#57
When you turn on the power, a capacitor keeps the 6502's reset line low for several milliseconds. This causes the processor to fetch the instruction pointer from $FFFE, and execution proceeds from there.

Modern PCs are a little more complicated. As a users, it seems to me like 20% of that complexity is necessary, such as to configure the DRAM (which definitely has to be done before running any user code.) The rest is all accidental complexity and compatibility with various legacy mistakes.

Re: Ask HN: Where can I find a primer on how computers boot?

#60
I wrote an article on writing a tiny pseudo-bootloader a while back[0] that got a bit of traction on HN at the time; you may enjoy it as an accesible and fun stepping stone into the space. There’s an accompanying GH repo[1] with all the resources you need to run it yourself :)

[0]: https://www.joe-bergeron.com/posts/Writing%20a%20Tiny%20x86%...

[1]: https://github.com/Jophish/tiny-bootstrap

Post reply on HN