If you’re not a Computer Sciences student or alumni, then probably nobody has told you this – PC architecture is all about the CPU and RAM, and CPU knows only a little about it’s surroundings (devices, drives, slots and cards). There I said it – if you go bare metal, you only have access to CPU and it’s abilities to:
- Do bit-wise operations.
- Do arithmetic operations.
- Do memory (i.e. RAM and registers) operations.
- Do some IO operations via two ways:
- some memory mapped hardware addresses that you can’t take for granted at first – for some of them you’ll have to scan through RAM, for some there are a defined address specified in specifications;
- legacy IO instructions – where you still need to know what device connected to which port.
- Process hardware and software interrupts – again, just an interrupt number and some info in specs about possible values passed on. For hardware interrupts there’s another chip you’ll have to understand – the Programmable Interrupt Controller (or PIC, or APIC for Advanced PIC).
And that’s it. From this point on you’ll need to figure out how to read data from your storage, how to write something on the display and how to read something from your keyboard and mouse. But don’t be worried – I’ll try to guide you through as less painful as possible.
Boot process in a nutshell
As I said in the introductory – we’re going to go the good old BIOS way, as it’s less abstract, yet also less standardized.
- When the PC powers up there is this Basic Input Output System that’s loaded from a special flash chip. This BIOS is built with the knowledge of the motherboard configuration by it’s manufacturer. BIOS will detect the amount of RAM, it will detect and initialize other hardware.
- When BIOS is done with hardware, it will read the first sector (sector at the address 0 and in size of 512 bytes) from user’s selected boot device to RAM starting at location 0x7C00 and pass it on to CPU. That is called Master Boot Record (MBR). From this point BIOS has done it’s job and control is yours.
- This means that these first 512 bytes (actually it’s less – around 440 up to 510 bytes, depending on weather you’ll use partition table) are your starting point. All you can do here is to read more data from the boot device (or other) and pass on to executing it. This process is called bootstrapping.
Some will tell you there’s more to it than that, but that’s actually it – the rest is up to you. Weather you’re going to use standard paradigms, implement standard partition tables, read Volume Boot Records (VBR) or not. You could implement your own storage layout, completely ignoring the standard partition tables (that are only necessary for Linux or Windows systems) or GUID Partition Table layout (used by most of the modern OSes), thus rendering your system incapable of being multi-bootable.
What you could and probably should do is:
- While being in Real Mode (this is where BIOS leaves you), using BIOS interrupts (kind of like OS API functions), read your second stage bootloader from the boot partition (from the location of your own choice).
- Pass on the execution to the second stage bootloader, where you should prepare the basic execution environment:
- enter protected mode
- build a page table
- enter long mode
- prepare interrupt handlers
- enable PIC
- write a simple ATA driver to continiue reading from the boot disk