Writing an OS Kernel from Scratch – Part 23: Framebuffer — Run Your OS on a Graphical Interface!
“VGA text mode can only display characters; a real graphical interface requires pixel-level control. Today, we implement the Framebuffer driver, enabling your OS to draw points, lines, rectangles, and even display images!”
In previous posts, our output was limited to VGA text mode (0xB8000 video memory): only ASCII characters, no graphics, no color depth control.
Modern operating systems all run in graphics mode, which depends on Framebuffer (frame buffer) — a contiguous block of physical memory, directly mapped to screen pixels.
Today, we will:
✅ Enable VESA graphics mode
✅ Implement Framebuffer driver
✅ Provide basic drawing APIs (draw point, line, clear screen)
✅ Expose via /dev/fb0 to userspace
Give your OS true graphics capabilities!
1. What is Framebuffer?
Framebuffer is a physical memory region whose content directly corresponds to screen pixels:
- Address: Provided by BIOS or UEFI
- Width/Height: Resolution (e.g., 1024×768)
- Color depth: Bytes per pixel (e.g., 32-bit = RGBA)
- Pixel format: RGB order, alpha channel, etc.
Framebuffer advantages:
- Simple: No complex GPU driver needed
- Universal: All x86 PCs support VESA BIOS Extensions (VBE)
- Efficient: Direct memory write = direct display
Linux’s /dev/fb0, Android’s SurfaceFlinger are all based on Framebuffer!
2. Getting Framebuffer Info via Multiboot
GRUB supports Multiboot 2, which can pass Framebuffer information to the kernel.
3. Framebuffer Driver Implementation
- Driver data structure
- Initialization
- Basic drawing API
4. Exposing Framebuffer via devfs
- Register Framebuffer device
- Implement mmap (critical!)
mmap allows user programs to directly access Framebuffer memory, enabling high-performance drawing!
5. Userspace Drawing Program
Running result:
- Screen displays RGB gradient background
- Blue horizontal line in the top-left corner
- User program directly manipulates pixels!
6. Advanced Topics
- Double Buffering — avoids flicker
- Hardware Acceleration — GPU BitBLT, fill instructions
- Multi-monitor Support — /dev/fb0, /dev/fb1
- Mode Switching — dynamic resolution changes
Framebuffer is the lowest layer of the graphics stack. On top of it, you can build a GUI (like MiniGUI, LVGL)!
Comments