Using an LLM to Develop a Graphics Program in a Bare-Metal UEFI Environment: Lessons from an AI Coding Experiment

    This hands-on experiment used an AI coding tool to build and debug a Chinese-language Breakout game running directly in a UEFI environment. It examines why UEFI is a meaningful test of AI programming ability, how humans and AI divided the work, and what debugging lessons emerged from issues involving GOP mode changes, stale pointers, and incorrect GUIDs. The experience shows that AI can accelerate low-level development, but only when humans provide precise runtime observations and verify every change in the target environment.

    Why Use UEFI to Test AI Programming?

    For everyday web applications and scripting tools, large language models are operating in their comfort zone. Training data is abundant, error messages are usually informative, and debugging feedback is relatively complete. The real differences between models become more visible in low-level environments where documentation is limited, debugging tools are primitive, and mistakes have a very small margin for recovery.

    UEFI provides exactly this kind of environment:

    1. There is no operating system safety net. The program runs directly at the firmware layer, without process management or memory protection. An invalid pointer may freeze or crash the system instead of producing a readable exception stack.
    2. Debugging tools are primitive. Development depends mainly on serial logs and DebugPrint output rather than conventional IDE breakpoints.
    3. The available training data is relatively limited. EDK2 development is a specialized area, so models generally have less domain knowledge than they do for application-level programming.

    For these reasons, UEFI is a more meaningful test of AI coding ability than asking a model to build yet another to-do application. The goal of this experiment was to drive graphics output directly in a BIOS environment and implement a playable Chinese-language Breakout game. The AI coding tool handled the programming work, while the human operator issued instructions, observed behavior, and reported the results.

    The Overall Workflow: Defining the Human-AI Boundary

    The collaboration model was simple and representative of current AI-assisted development:

    • Human: Describe the task, prepare the environment, run the program, observe its behavior, and report the symptoms to the AI.
    • AI: Read the code, identify potential problems, modify the implementation, and propose verification steps.

    The human does not need to understand every line of code. However, the runtime behavior must be translated into precise feedback. For example, “the program exits after printing Starting Game Loop” is far more useful for debugging than “the program does not work.”

    The work was divided into two rounds:

    • Round 1: Make the program run.
    • Round 2: Refine and improve the implementation.

    This staged approach is important. It prevents the development process from getting stuck in optimization details before the core execution path is working.

    Round 1: Three Debugging Cycles from Broken Code to a Running Program

    3.1 Start with a Code Review

    When working with incomplete or unfamiliar code, the first instruction should not be “fix it.” A better starting point is: review the code and identify possible problems.

    Having the AI perform a static review first provides several benefits:

    • It establishes context. The model needs to understand the entry point, main loop, game logic, rendering module, and input handling before making reliable changes.
    • It produces a problem list. A list of findings makes it easier for the human to evaluate whether the model is looking in the right direction.
    • It reduces unnecessary changes. In low-level code, blind modifications can easily introduce new, difficult-to-diagnose bugs.

    The review did identify several issues, and the model claimed that the changes would make the program runnable. In practice, however, the program still crashed during execution. This is a typical characteristic of low-level development: many bugs only become visible when the code actually runs.

    3.2 Problem One: A Stale Pointer After SetMode

    The program started and printed its initialization logs, then exited immediately. Because UEFI does not provide a conventional crash dump, the investigation had to rely on additional logging.

    The AI was asked to insert debug output at key points, and the execution flow was monitored through the serial console. This gradually narrowed the failure to a call involving the Graphics Output Protocol (GOP).

    Further analysis showed that the graphics information pointer returned by QueryMode referred to temporary data. After changing the display mode, the contents of that memory were no longer valid. The program then used stale information to calculate the frame buffer layout and accessed invalid data.

    The general fix follows a common low-level programming principle: after changing hardware or display state, reacquire any information that depends on that state instead of reusing cached query results.

    1. // After SetMode, query the frame buffer information again
    2. GOP->SetMode(GOP, targetMode);
    3. // Do not rely on the result of QueryMode from before SetMode
    4. GOP->QueryMode(GOP, GOP->Mode->Mode,
    5. &SizeOfInfo, &Info);

    The lesson is that models have limited ability to detect some lifecycle-related bugs through static analysis alone. However, when the human provides an accurate runtime symptom, the model can often locate and fix the problem reliably.

    3.3 Problem Two: The Wrong GUID Prevented Access to the Graphics Protocol

    After the crash was fixed, the program ran consistently, but no graphics interface appeared. Once this symptom was reported, the AI identified a GUID configuration problem.

    This is a classic UEFI development pitfall. EDK2 defines multiple GUIDs related to graphics output. Their names and meanings are similar, but they serve different purposes. If the wrong GUID is passed to LocateProtocol, the call may fail silently: no obvious error is displayed, and the protocol pointer is simply not obtained. If the return value is not checked, later calls may fail or trigger another exception.

    The fix was straightforward: compare the GUID used for protocol lookup with the definitions in the EDK2 documentation and replace it with the correct one.

    This case highlights two broadly applicable engineering practices:

    1. Always check the return value when acquiring a protocol or resource. In firmware environments, failures are often silent.
    2. Constants with similar names deserve special attention. Remembering this kind of specialized detail is one area where an AI can have an advantage over a newcomer.

    3.4 Round 1 Result

    After three cycles of “report the symptom, let the AI diagnose it, apply the change, and rerun the program,” the game ran on bare metal. It rendered graphics, responded to keyboard input, and executed the game logic correctly.

    The human did not write any code during this phase. The human’s role was limited to observing the behavior and accepting or rejecting the results.

    Round 2: From Running to Usable

    The first round addressed basic stability. The second focused on usability and refinement:

    • Rendering performance: Replace full-screen redraws with dirty-region updates to reduce flicker.
    • Input behavior: Add debouncing and state checks to keyboard polling so that holding a key does not cause abnormal acceleration.
    • Chinese text rendering: Extract bitmap glyphs from the built-in font data, render dot-matrix Chinese characters, and handle alignment and line wrapping.
    • Code organization: Separate the rendering and game-logic layers, then have the AI refactor the code by module and add comments.

    This round reinforced an important point: AI performs best when the constraints are clearly defined. A specific request such as “eliminate screen flicker” is generally more effective than an open-ended instruction such as “optimize the program.”

    Lessons for Using AI in Low-Level Development

    Several reusable practices emerged from the experiment:

    1. Review before modifying. Ask the AI to produce a problem list first so that the human can evaluate its diagnosis.
    2. Treat runtime symptoms as input. The human’s most important contribution is an accurate description of what happened, including log output, the point of failure, and reproduction conditions.
    3. Make small changes and verify them individually. Ask the AI to address one issue at a time, then run the program immediately. This avoids combining multiple changes that make it difficult to identify the cause of a new failure.
    4. Static analysis cannot replace runtime verification. Even a confident list of issues may miss lifecycle-related bugs. Actual execution remains the final authority.
    5. Think of the AI as a senior colleague who remembers specialized details but lacks context. It may know GUID conventions and protocol call sequences, but it does not know what your program actually did on the target machine. Supplying that missing context is the key to productive collaboration.

    Conclusion

    This experiment demonstrates that AI assistance can reduce a task that might otherwise take an experienced firmware engineer several days to complete to a working prototype within a few hours. AI can significantly lower the barrier to low-level development, but it does not turn firmware programming into a one-click delivery process.

    Instead, the human role shifts from writing every line of code to defining the problem, observing runtime behavior, and validating the result. The outcome depends largely on whether the human-AI workflow is designed effectively.

    For developers interested in exploring this approach, an EDK2 sample project is a practical starting point. A small graphics program can provide a manageable way to experience this development model firsthand: AI writes and revises the code, while the human runs it, investigates the symptoms, and verifies the result.