Project 1: The “Do-Nothing” Bootloader & Kernel

Boot a disk image, write text to VGA memory, and halt in bare metal.

Quick Reference

Attribute Value
Difficulty Advanced
Time Estimate Weekend
Language x86 Assembly + C (Alt: Rust)
Prerequisites Basic assembly, registers, binary layout
Key Topics boot sector, 0x7C00, VGA text mode

1. Learning Objectives

By completing this project, you will:

  1. Build a bootable 512-byte sector with the 0x55AA signature.
  2. Write characters directly to VGA memory at 0xB8000.
  3. Explain BIOS load address and real-mode execution.
  4. Produce a working bootable disk image for QEMU.

2. Theoretical Foundation

2.1 Core Concepts

  • Boot sector: The BIOS loads the first 512 bytes to 0x7C00 and jumps there.
  • Real mode: 16-bit addressing and segmented memory during early boot.
  • Memory-mapped I/O: VGA text buffer is a memory region, not a device API.

2.2 Why This Matters

Everything you run starts here. This project makes the boot sequence tangible and demystifies “before main()”.

2.3 Historical Context / Background

The PC BIOS boot process dates back decades and remains a baseline for understanding low-level bootstrapping.

2.4 Common Misconceptions

  • “The BIOS loads the whole OS”: It loads only 512 bytes.
  • “Printing needs a library”: You can write directly to hardware memory.

3. Project Specification

3.1 What You Will Build

A boot sector that prints a message to the VGA text buffer and halts, runnable in QEMU.

3.2 Functional Requirements

  1. Produce a 512-byte boot sector with signature.
  2. Display “Hello from Bare Metal!” in VGA text mode.
  3. Halt safely (infinite loop or hlt).

3.3 Non-Functional Requirements

  • Reliability: Always boots without undefined execution.
  • Simplicity: Keep code in a single boot sector.

3.4 Example Usage / Output

$ nasm -f bin boot.asm -o boot.bin
$ qemu-system-x86_64 boot.bin

3.5 Real World Outcome

You boot a raw image and see your text on a black screen in QEMU:

$ qemu-system-x86_64 boot.bin
# QEMU window shows: "Hello from Bare Metal!"

4. Solution Architecture

4.1 High-Level Design

BIOS -> load 512 bytes -> jump to 0x7C00 -> write to 0xB8000 -> halt

4.2 Key Components

Component Responsibility Key Decisions
Boot sector Entry point Fit in 512 bytes
VGA writer Display text Direct memory writes
Halt loop Stop CPU HLT loop

4.3 Data Structures

; VGA text cell: [char][attribute]

4.4 Algorithm Overview

Key Algorithm: Write String

  1. Load character from string.
  2. Write char + attribute to VGA memory.
  3. Increment pointer until null terminator.

Complexity Analysis:

  • Time: O(n) for message length
  • Space: O(1)

5. Implementation Guide

5.1 Development Environment Setup

nasm -v
qemu-system-x86_64 --version

5.2 Project Structure

project-root/
├── boot.asm
└── Makefile

5.3 The Core Question You’re Answering

“What happens before main(), and how does the CPU start executing code?”

5.4 Concepts You Must Understand First

Stop and research these before coding:

  1. BIOS boot flow
  2. Real mode addressing
  3. VGA text buffer layout

5.5 Questions to Guide Your Design

Before implementing, think through these:

  1. Where must the signature bytes live in the sector?
  2. How will you pad to 512 bytes exactly?
  3. What happens if execution falls through past your code?

5.6 Thinking Exercise

Trace the boot path

Write a 5-step diagram from power on to executing your first instruction at 0x7C00.

5.7 The Interview Questions They’ll Ask

Prepare to answer these:

  1. “What is the boot signature?”
  2. “What is real mode vs protected mode?”
  3. “What is memory-mapped I/O?”

5.8 Hints in Layers

Hint 1: 0x55AA signature Place 0x55, 0xAA at the last two bytes of the sector.

Hint 2: VGA memory Use segment 0xB800 and offset 0.

Hint 3: Halt loop Use hlt inside a loop to avoid running garbage.

5.9 Books That Will Help

Topic Book Chapter
Booting “Operating Systems: From 0 to 1” Ch. 2-3
Assembly “Programming from the Ground Up” Ch. 1-3

5.10 Implementation Phases

Phase 1: Foundation (Half day)

Goals:

  • Build a minimal boot sector.

Tasks:

  1. Create a 512-byte file with signature.
  2. Boot it in QEMU.

Checkpoint: QEMU boots without error.

Phase 2: Core Functionality (Half day)

Goals:

  • Write to VGA memory.

Tasks:

  1. Add a string.
  2. Loop and write characters.

Checkpoint: Message appears on screen.

Phase 3: Polish & Edge Cases (Half day)

Goals:

  • Clean halt and padding.

Tasks:

  1. Add infinite halt loop.
  2. Verify exact 512-byte size.

Checkpoint: Image always boots.

5.11 Key Implementation Decisions

Decision Options Recommendation Rationale
Print method BIOS int 0x10 vs VGA VGA More direct
Halt jmp $ vs hlt loop hlt loop Lower CPU use

6. Testing Strategy

6.1 Test Categories

Category Purpose Examples
Boot Image boots QEMU run
Output Text visible VGA message
Size Correct length wc -c boot.bin

6.2 Critical Test Cases

  1. Binary is exactly 512 bytes.
  2. Signature bytes are correct.
  3. Message displays on screen.

6.3 Test Data

Message: "Hello from Bare Metal!"

7. Common Pitfalls & Debugging

7.1 Frequent Mistakes

Pitfall Symptom Solution
Missing signature Not bootable Add 0x55AA
Wrong load address Crash Use org 0x7C00
No halt CPU runs garbage Infinite loop

7.2 Debugging Strategies

  • Use QEMU debug logs (-d in_asm).
  • Verify offsets with a hex dump.

7.3 Performance Traps

None for this project.


8. Extensions & Challenges

8.1 Beginner Extensions

  • Print a second line.
  • Change text color attributes.

8.2 Intermediate Extensions

  • Switch to protected mode and write again.
  • Load a second stage from disk.

8.3 Advanced Extensions

  • Build a tiny C kernel entry.
  • Add GDT setup for 32-bit mode.

9. Real-World Connections

9.1 Industry Applications

  • Bootloaders and firmware for embedded systems.
  • OSDev: https://wiki.osdev.org
  • Limine: https://limine-bootloader.org

9.3 Interview Relevance

  • Demonstrates low-level understanding of boot and memory.

10. Resources

10.1 Essential Reading

  • OSDev Bootloader guides

10.2 Video Resources

  • Bootloader tutorials (search “boot sector 0x7C00”)

10.3 Tools & Documentation

  • nasm and qemu manuals

11. Self-Assessment Checklist

11.1 Understanding

  • I can explain the BIOS boot flow.
  • I can explain 0x7C00 and 0x55AA.
  • I can describe VGA text mode.

11.2 Implementation

  • Boot image runs in QEMU.
  • Text displays correctly.
  • Code halts safely.

11.3 Growth

  • I can extend to a second-stage loader.
  • I can explain real vs protected mode.

12. Submission / Completion Criteria

Minimum Viable Completion:

  • Boot sector prints a message and halts.

Full Completion:

  • Clean build process and verified size/signature.

Excellence (Going Above & Beyond):

  • Second-stage loader or protected-mode switch.

This guide was generated from LEARN_LINUX_UNIX_INTERNALS_DEEP_DIVE.md. For the complete learning path, see the parent directory.