COMPLETE BOOT FLOW – From Power to OS

Let’s break down the complete boot flow, start to finish, with clarity, real understanding, and clean diagrams.

we’ll go from power button ➡️ BIOS ➡️ Bootloader ➡️ Kernel ➡️ OS running, no steps skipped, no confusion.


COMPLETE BOOT FLOW – From Power to OS (Explained with Emojis & Diagrams)


🧠 What Is BIOS? (Plain, No Confusion)

🔹 BIOS = Basic Input/Output System

  • It's a tiny program built into your computer’s motherboard.

  • BIOS is the first thing that runs when you turn on the computer—even before your operating system.

  • It prepares the system so it can run more complex software like Windows, Linux, or even your own OS you're about to build .


0. Before Power On – System at Rest

Your computer is sitting there. Nothing is running. But:

  • The BIOS chip is soldered to your motherboard

  • Inside that chip is a small program (the BIOS firmware)

  • The hard drive or USB stick might contain your OS


🔌 1. Power Button Pressed

🧠 Who responds first?
→ The CPU!

But the CPU has no RAM, no disk, no OS, no idea what to do.

So it starts at a hardcoded memory location:

📍 x86 CPUs always begin execution at 0xFFFF0
🧭 This address maps to the BIOS chip.

[Power On]
     ↓
[CPU begins at 0xFFFF0 → jumps into BIOS]

2. BIOS Takes Control

🧪 BIOS runs POST (Power-On Self Test):

  • ✅ Checks if RAM is working

  • ✅ Detects keyboard, display, CPU, mouse

  • ✅ Initializes screen & keyboard (basic I/O)

Then it finds where the OS lives by scanning storage devices.

📦 Example boot order:

  • 1️⃣ USB

  • 2️⃣ Hard Disk

  • 3️⃣ CD/DVD

  • 4️⃣ Network (PXE boot)


3. BIOS Looks for a Bootloader

🔎 BIOS reads sector 0 (first 512 bytes) from each device.

It checks:

Last 2 bytes == 0xAA55? → YES ✅
→ Load it into RAM at 0x7C00
→ Give control to bootloader!

📍 0x7C00 is a fixed location in memory where BIOS places the bootloader.

[BIOS]
     ↓ (loads 512 bytes from disk to 0x7C00)
[Bootloader (MBR)]

4. Bootloader Runs (Stage 1)

💡 The bootloader is a tiny 512-byte program:

  • It’s not the OS

  • Its job is to load the actual OS kernel

🔁 Usually it does:

  • Show message: “Booting...”

  • Switch to 32-bit or 64-bit CPU mode

  • Load second-stage loader or kernel from disk

🧱 The bootloader is like a bridge between BIOS and the real OS.

[Bootloader @ 0x7C00]
     ↓
Load kernel from disk → into RAM

5. Bootloader Loads the Kernel

Now the bootloader:

  • Reads the OS kernel file from disk

  • Copies it into RAM (maybe at 0x100000 or above)

  • Sets up the CPU mode:

    • From real mode (16-bit) ➡️ to protected mode (32-bit)

  • Jumps to the kernel’s entry point

📍 Common kernel start: 0x100000 (1MB mark in RAM)


6. Kernel Takes Over

🧠 The kernel now owns the system.

It:

Now your OS is alive!

[Kernel in memory @ 0x100000]
     ↓
Starts processes, drivers, userspace

7. OS Fully Running

Now you're at a login screen or desktop!

👤 You can open apps, use the terminal, run games, etc.
The OS manages:

  • Memory 💾

  • Files 🗂️

  • Hardware ⚙️

  • Processes 🧵

The BIOS? It's done. It left the stage way back in step 3.


📊 FULL VISUAL FLOW

[Power Button 🔘]
        ↓
[CPU Starts @ 0xFFFF0 🧠]
        ↓
[BIOS Runs 🧪]
  - POST test
  - Device init
  - Look for boot device
        ↓
[Reads Bootloader (512 bytes) 💽]
  - Checks for 0xAA55 signature
  - Loads to RAM at 0x7C00
        ↓
[Bootloader runs ⚙️]
  - Loads kernel
  - Switches to 32-bit mode
        ↓
[Kernel runs 🧠]
  - Memory setup
  - Devices & drivers init
  - Process management
        ↓
[OS Up & Running 🎉]
  - You can use apps, UI, etc.

KEY LOCATIONS & TERMS

Term What It Means
0xFFFF0 Start point for CPU after reset (in BIOS chip)
BIOS Built-in firmware that runs first
POST Hardware test done by BIOS
0xAA55 Signature bytes in MBR to mark it as bootable
0x7C00 Where BIOS loads the 512-byte bootloader
MBR Master Boot Record (first sector of a disk)
Bootloader Tiny program that loads the OS kernel
0x100000 Common address for loading the kernel
Kernel Core of the OS that talks to hardware

🤔 Q1: When I press the power button, who responds first—the CPU?

✅ Yes. The CPU is the first thing that gets powered and starts running.

  • As soon as you press the power button:

    • Power goes to the motherboard

    • CPU receives power, resets, and starts execution

    • But the CPU is kinda dumb at first—it doesn’t know what to do yet


🤯 Q2: If CPU is first, how does it know about BIOS? Why not go straight to the bootloader?

👉 The CPU doesn't “know” about BIOS like software knows functions.

What really happens:

  • CPUs have a built-in rule:

    • When powered on, start executing code at a fixed memory address

      • On x86, that address is usually 0xFFFF0

  • This memory address is mapped to a special chip on the motherboard:

    • That chip is the BIOS chip

    • The code sitting there is BIOS firmware

🧠 So: The CPU starts at 0xFFFF0 → jumps into BIOS → BIOS takes control

🔗 This isn't a "choice"—it's a hardware design. The bootloader lives on the disk, but the CPU can’t talk to the disk without some help. That's why BIOS is needed first.


🔁 Q3: If CPU already starts BIOS, why does BIOS check things again (POST)?

Because the CPU alone doesn’t check or initialize anything. It's like a racecar engine without a steering wheel.

🔧 POST (Power-On Self-Test) is BIOS code that:

  • Checks if memory (RAM) is present and working

  • Verifies keyboard, display, CPU itself

  • Initializes basic stuff like screen modes, keyboard input

So:
✅ CPU starts
➡️ BIOS starts
➡️ BIOS runs POST to check that the system is safe to continue

If something critical is broken, BIOS halts the boot process or beeps error codes.


🧩 Q4: Is BIOS part of the OS? If not, why does it look for the OS?

Nope, BIOS is not part of the OS.

BIOS OS
Built into the motherboard             Stored on hard disk/USB
Written in Assembly/C Written in C, Rust, etc.
Only runs at boot time Runs all the time once loaded
Hardware initialization Full-featured user-level services

So, BIOS:

  • Is like the airport runway 🛫

  • It prepares the plane (hardware) for flight

  • Then it hands over control to the pilot (your OS)


🔍 Q5: Where does BIOS look for the OS, and how does it know it found it?

BIOS looks for the OS on bootable storage devices (like USB, SSD, HDD).

Here’s how it knows:

  1. BIOS reads the first 512 bytes of the disk (called the MBR – Master Boot Record)

  2. It checks the last 2 bytes for the magic number: 0xAA55

    • If it’s not there, BIOS says: ❌ Not bootable

  3. If found, BIOS says: ✅ Okay, valid boot sector!

    • It loads it into memory at 0x7C00

    • Then jumps to that address


🚫 Q6: What if BIOS doesn’t find a bootable device?

BIOS will either:

  • Show a message:

    No bootable device found. Insert disk and press a key.
    
  • Or try the next device in boot order (USB → HDD → network)

It never directly loads an OS—it always loads a bootloader, which then loads the OS.


🧠 Q7: Why only 512 bytes in the bootloader?

Because:

  • The MBR is only 512 bytes long

  • It’s a legacy design (from the 1980s) that BIOS still follows

  • BIOS only loads those 512 bytes → it’s up to that small code to load more!

🧱 Think of it like:

BIOS loads a tiny piece of code that acts like a ladder to reach the full OS.


🧮 Q8: Why 0xAA55 and 0x7C00? Why these addresses?

Let’s break it:

🔹 0xAA55 → Boot Signature

  • These are the last 2 bytes of the 512-byte boot sector

  • BIOS checks these to confirm this is a valid bootloader

  • If not found, BIOS ignores it

🔹 0x7C00 → Memory Address

  • BIOS always copies the MBR into memory at 0x7C00 (31 KB mark in RAM)

  • Why? It’s a convention set by IBM in the original PC

  • All bootloaders are written assuming they’ll start from 0x7C00

👀 That’s why we use this line in our bootloader:

[ORG 0x7C00]  ; Tell assembler this code will run at 0x7C00

🧩 Why copy again / load more code later?

Because:

  • The first 512 bytes can’t do much—it’s super tiny

  • It usually contains just enough code to load a second stage

    • From disk or file system

    • Which then loads the full kernel (written in C)

Think of it like:

BIOS → Bootloader (512 bytes) → Stage 2 loader → Kernel → OS


🧠 WHY Switch to Protected Mode?

❓ What’s “real mode” anyway?

When the CPU first starts (because that’s how BIOS works), it’s in Real Mode, which:

  • Can only address 1 MB of memory

  • Only understands 16-bit code

  • Uses segmented memory (annoying and limited)

  • Can use BIOS interrupts (like INT 10h for printing)

🧓 Real Mode is an ancient leftover from 1981 IBM PCs.

❌ Problem:

You can't build a serious OS in real mode.

  • You can’t access modern memory (>1 MB)

  • You can’t use paging, process isolation, multitasking, etc.

✅ Solution:

Switch the CPU to Protected Mode:

  • Enables 32-bit instructions

  • Gives access to up to 4 GB RAM

  • Allows use of virtual memory

  • Lets you run modern C code and kernel features

So we must make this switch in our bootloader before loading the real kernel.

---------------------------------------------------------------------------------------------------------

Even if your PC is 64-bit, it always starts in 16-bit Real Mode.

😳 sounds weird, right? like, “bro I bought a 64-bit beast, why’s it acting like it’s 1981?”

Let’s break it down.


🧓 Why 16-bit Real Mode on a 64-bit CPU?

🧬 It’s by design, for backward compatibility.

  • Intel and AMD wanted modern CPUs to still run old software (like DOS, BIOS tools, bootloaders from 80s/90s)

  • So they kept the startup process exactly like older x86 CPUs

  • That means:

    • CPU starts in Real Mode (16-bit)

    • Can only access first 1 MB of memory

    • Uses segment:offset memory addressing

    • Runs BIOS code


🧠 CPU Boot Modes Overview

ModeBitsWho uses it?When used
Real Mode    16        BIOS, legacy codeStartup (always!)
Protected Mode    32OS kernels, modern OS features    Bootloader → Kernel
Long Mode    6464-bit operating systemsKernel (like Linux/Win64)

🔁 Typical Boot Flow on a 64-bit PC

Even with a 64-bit CPU (x86-64), it goes like this:

🔘 Power ON ↓ 🧠 CPU starts in 16-bit Real Mode ↓ 🧪 BIOS runs (also 16-bit) ↓ 💽 Loads Bootloader (still 16-bit) ↓ ⚙️ Bootloader switches to 32-bit Protected Mode ↓ 🚀 Bootloader or Kernel switches to 64-bit Long Mode ↓ 🎯 64-bit Kernel runs, OS is now fully 64-bit

💡 So: The switch to 64-bit doesn’t happen automatically.

You (as the OS or bootloader dev) must:

  1. Go from Real Mode → Protected Mode (32-bit)

  2. THEN from Protected Mode → Long Mode (64-bit)


📦 What is "Long Mode"?

"Long Mode" is just the official name for 64-bit mode in x86-64 CPUs.
To enter Long Mode:

  • You must be in Protected Mode first

  • You enable a special bit in a control register (EFER)

  • You must also enable paging

  • You load a 64-bit GDT and jump to 64-bit code

So yeah... it's not automatic. You earn your 64-bit status 😎


QuestionAnswer
Do 64-bit CPUs start in 64-bit?    ❌ No. They always start in 16-bit Real Mode
Why?        For compatibility with legacy BIOS and boot code
When does 64-bit start?    Only after your bootloader sets up Long Mode
Who is responsible?    Your bootloader or kernel must enable it manually

Cold Boot (Hard Boot): Starting the system from a completely off state (BIOS/POST loads OS).

Warm Boot (Soft Boot): Restarting the system without cutting power (OS reloads, skipping hardware checks).

Comments