A while ago I watched Inkbox’s Programming in Assembly without an Operating System, where he builds an arcade game as a standalone UEFI application. That video stuck with me because I had always thought of UEFI as the brief bureaucracy the machine rushes through on its way to GRUB or Linux, not a place where you can actually stop and live for a while.
Once you look closer, pre-boot UEFI is a surprisingly capable little environment. There is no kernel, no window server, and no libc. While your program runs, your loop is the only thing executing on the CPU. Yet by the time the firmware hands control to your entry point, it has already initialized memory, set up a keyboard input queue, and published a framebuffer. You don’t have to write a USB driver to read keystrokes or program a GPU to put pixels on screen. And when main returns, the firmware simply unloads your binary and picks up right where the boot sequence left off, before an operating system ever existed.
I wanted to explore what it actually feels like to write software in that gap, and Snake felt like the right excuse: all you really need is a way to draw rectangles, read arrow keys, and tell time.
Booting into a 10-second pause
To get my bearings, I started with the uefi-rs hello world tutorial. The uefi crate wraps the firmware’s boot and runtime services in safe Rust, and its logger and panic_handler features are enough to get a minimal binary running. Those two features ended up being the entire setup for the finished game too.
Following the application chapter, I created a new crate, added log and uefi, and wrote a src/main.rs that prints one line, pauses for ten seconds, and exits back to the firmware:
#![no_main]
#![no_std]
use core::time::Duration;
use log::info;
use uefi::prelude::*;
#[entry]
fn main() -> Status {
uefi::helpers::init().unwrap();
info!("Hello world!");
boot::stall(Duration::from_secs(10));
Status::SUCCESS
}
Even in a ten-line program, you immediately see how UEFI differs from a normal Rust binary. #![no_std] drops the standard library (leaving core), and #![no_main] disables Rust’s usual runtime entry point so #[entry] can expose the ABI the firmware expects, returning a UEFI Status code at the end. Inside main, helpers::init() wires log up to the firmware’s text console, and boot::stall tells boot services to halt execution for ten seconds so the message doesn’t vanish before you can read it.
Building an .efi binary turned out to be much smoother than I expected. The building chapter adds the UEFI targets to rust-toolchain.toml, after which a standard cargo build --target x86_64-unknown-uefi emits a portable executable ready for the firmware:
[toolchain]
targets = ["aarch64-unknown-uefi", "i686-unknown-uefi", "x86_64-unknown-uefi"]
To actually boot it without rebooting my laptop off a flash drive every two minutes, I followed the VM chapter and ran QEMU with OVMF firmware (edk2-ovmf, which lives under /usr/share/edk2/x64/ on Arch). My favorite trick from that setup is fat:rw:esp: instead of formatting a disk image and mounting it on every build, QEMU can expose a plain local directory as a virtual FAT-formatted EFI system partition. Dropping the binary at esp/efi/boot/bootx64.efi puts it right where UEFI looks for a default removable bootloader:
cargo build --target x86_64-unknown-uefi
sudo pacman -S qemu-full edk2-ovmf
cp /usr/share/edk2/x64/OVMF_CODE.4m.fd .
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd .
mkdir -p esp/efi/boot
cp target/x86_64-unknown-uefi/debug/my-uefi-app.efi esp/efi/boot/bootx64.efi
qemu-system-x86_64 -enable-kvm \
-drive if=pflash,format=raw,readonly=on,file=OVMF_CODE.4m.fd \
-drive if=pflash,format=raw,readonly=on,file=OVMF_VARS.4m.fd \
-drive format=raw,file=fat:rw:esp

info! on line 11 of src/main.rs.A few seconds after launching QEMU, [ INFO ]: src/main.rs@011: Hello world! appeared on screen, sat there for ten seconds, and exited cleanly back to OVMF. I had code running before an OS, though the text console was only good for printing lines. For Snake, I needed pixels.
Leaving text mode for the Graphics Output Protocol
To draw a grid, I stepped away from the text console and opened UEFI’s Graphics Output Protocol (GOP). In UEFI, hardware capabilities live behind protocols that you query from boot services. Once you open GOP, it tells you the active screen resolution and gives you a block transfer (blt) operation that can paint solid rectangles or copy pixel buffers straight to the screen.
Because I hadn’t enabled a heap allocator (sticking with just the logger and panic_handler crate features from the tutorial), I kept the board dimensions fixed at 40 columns by 30 rows so the entire game state could live in fixed-size arrays. At startup, the game queries current_mode_info().resolution() and scales the cell size to fit and center a 40x30 board on whatever resolution OVMF picked:
let screen = gop.current_mode_info().resolution();
let board = Board::fit(screen.0, screen.1);
fn fit(width: usize, height: usize) -> Self {
let cell = (width / COLS as usize).min(height / ROWS as usize).max(1);
let board_w = cell * COLS as usize;
let board_h = cell * ROWS as usize;
Self {
origin_x: (width - board_w) / 2,
origin_y: (height - board_h) / 2,
cell,
}
}
All of the rendering happens through VideoFill. BltPixel::new takes red, green, and blue values (which the firmware stores under the hood as blue, green, red, and a reserved byte), and VideoFill floods a rectangle with that pixel. On the first frame, the game fills the whole screen with a dark border color and paints the centered board area slightly lighter. When a cell is wider than two pixels, I draw its square one pixel smaller than the grid step so the background shows through as a crisp 1px grid line between cells.
After that initial draw, the renderer only touches the cells that actually changed on each tick: the old head gets repainted in body green, the old tail reverts to the empty board color (unless the snake just ate), the new head lights up in bright green, and the food sits in red.
Polling keys, stalling the CPU, and disarming the watchdog
With a board on screen, the next question was how input and timing work when there are no threads, event loops, or OS timers.
Keyboard input comes through the firmware’s text input queue. Calling read_key doesn’t block; if no key has been pressed since the last check, the firmware returns NOT_READY, which uefi-rs surfaces as Ok(None). At the start of each frame, the loop drains every queued key until read_key returns Ok(None), so any key pressed while the CPU was sleeping between frames still gets picked up on the next tick. Arrow keys and WASD both call turn (which ignores directions that would fold the snake back into itself), and pressing Escape breaks out of the game loop so main can return Status::SUCCESS.
For the frame clock, I reused the same boot::stall call from the hello world tutorial, except instead of stalling once for ten seconds, the game stalls briefly on every tick, starting at 140 ms and speeding up by 3 ms per point down to a floor of 55 ms. In a normal program, busy-stalling the CPU like that would be terrible manners, but in UEFI our binary owns the machine until it exits.
There is one catch with staying inside a UEFI application for more than a few minutes, though: the firmware assumes you are a bootloader. To prevent a hung bootloader from wedging a machine forever, UEFI arms a watchdog timer that resets the system after five minutes if the app hasn’t either exited or called ExitBootServices. Since a good game of Snake can outlast five minutes, the first thing the setup does before starting the loop is disable the watchdog:
boot::set_watchdog_timer(0, 0x10000, None)?;
Fitting Snake into bare boot services
Without a heap, the snake’s body lives in a fixed ring buffer, [Cell; MAX_LEN], where MAX_LEN is 40 * 30 (every cell on the board), so the snake can grow to fill the entire grid without ever asking the firmware to allocate memory. head points to the newest cell in the array, and the live snake is the len cells trailing behind it. Advancing one step just writes the next coordinate to (head + 1) % MAX_LEN. If the snake eats, len and score increment by one; on a normal move, len stays the same, so the oldest cell simply drops out of the window and gets painted over.
That ring buffer also made one classic Snake edge case easy to see: what happens when the head moves into the cell currently occupied by the tail? On a normal move, the tail is about to vacate that cell on the very same tick, so chasing your own tail should be legal. I skip the tail in the self-collision check whenever the snake isn’t eating, and erase the tail pixel before painting the new head so the new head isn’t immediately overwritten:
let eating = next == self.food;
if !in_bounds(next) || self.occupies(next, !eating) {
self.alive = false;
return false;
}
Spawning food meant finding a source of randomness without std or rand. Rather than pulling in a hardware RNG protocol, I reached into UEFI’s runtime services and called runtime::get_time(), mixing the day, hour, minute, second, and nanosecond fields into a seed for a small xorshift generator so even two boots within the same second get different food layouts. To place food, the game tries up to 32 random coordinates and falls back to scanning the board linearly if the snake is so long that all 32 attempts land on its body. Filling all 1,200 cells counts as a win (pausing for two seconds before exiting), while hitting a wall or yourself paints the fatal cell a darker red, pauses two seconds, and exits the same way.
One thing you won’t see on screen is the score counter. Every piece of food increments game.score, and the frame delay uses it to speed the game up, but the number stays in that struct field. In UEFI, switching back to the text console to print digits resets the graphics mode and wipes the board, so rendering a score alongside GOP graphics means bringing your own bitmap font and blitting the glyphs pixel by pixel. Watching the snake speed up was plenty of feedback, so I left the HUD out.
Playing in the firmware
Running the finished game uses the exact same QEMU setup as the hello world binary. The only difference is copying snake_uefi.efi into the virtual EFI system partition’s boot slot:
cp target/x86_64-unknown-uefi/debug/snake_uefi.efi esp/efi/boot/bootx64.efi
Here it is running inside OVMF in QEMU:
Pressing Escape (or winning or crashing) returns Status::SUCCESS and hands the machine right back to the firmware, and the next boot starts a fresh three-cell snake in the center of the board, moving right.
Building a tiny game in UEFI demystified a layer of the boot process I usually never see. Between the firmware’s protocols and uefi-rs, you can go from power-on to drawing frames and polling a keyboard in a couple hundred lines of #![no_std] Rust, without an operating system anywhere in sight. If you want to check out the code, it is on GitHub at snake_uefi.