AI-Assisted UEFI Graphics Programming: Lessons from Repairing AI-Written Firmware Code

    This article describes a two-round experiment in UEFI graphics development using the Graphics Output Protocol (GOP). Model A completed a graphical Breakout game quickly, while Model B’s Tetris code could not even get a build environment working after nearly six hours. Model A was then asked to review and repair Model B’s legacy code, moving it from a state that would not run to stable execution and refinement. The article covers static code review, a dangling-pointer bug, a GOP GUID configuration error, rendering optimization, and practical lessons for AI-assisted firmware development.

    1. Background: Why Run This Experiment

    I recently ran a set of comparison tests on “the practical usability of AI-assisted programming in low-level firmware development.” The test environment involved writing a small game in UEFI using the Graphics Output Protocol (GOP). This is a good scenario for testing AI programming capability: it involves C, firmware-level APIs, graphics rendering, input handling, and other layers, with little room for error. Any oversight can prevent the program from starting or cause it to crash.

    In the first round, Model A completed a graphical Breakout game in two and a half hours. It compiled on the first try and ran successfully on the first run. Model B’s Tetris, by contrast, took nearly six hours and still could not get the build environment set up. What remained was a set of code that “looked plausible”: seven source files, a modular design, and a structured layout, but it could not run at all.

    Some readers pointed out that having the two models perform different tasks made the comparison unfair. That is a reasonable point. The “discarded code” was also structurally decent, so throwing it away seemed wasteful. I therefore decided to have Model A take over this legacy code and complete the full process from “can compile and run” to “refined and optimized”—in other words, “using AI to repair the mess left by AI.”

    2. Test Design

    The test was conducted in two rounds, with progressively higher goals:

    • Round 1: Get the program running. Focus on blocking issues such as crashes, black screens, and protocol initialization failures.
    • Round 2: Refinement and optimization. Improve code structure, rendering efficiency, and interaction experience.

    The specific approach was to copy Model B’s source code unchanged into a clean development environment, then give Model A its first instruction: “Review this code and identify possible issues.”

    3. Round 1: From Discarded Code to a Running Program

    3.1 Static Code Review

    Model A examined the entry function, main loop, game logic, rendering module, and input-handling module one by one. A few minutes later, it produced a list of issues and judged that “after these fixes, it should run.” I asked it to modify the code and compile it directly. The result: the program did compile and run, printed “Starting Game Loop” on the screen, and then the process exited immediately.

    This was slightly better than Model B’s result, but still far from usable. Static review can find syntax and logic errors, but it cannot find memory and protocol issues that only appear at runtime.

    3.2 Issue 1: Program Crash Caused by a Dangling Pointer

    I asked Model A to add more detailed debug logs on critical paths and monitor execution progress through the serial port. It quickly located the problem: the program exited abnormally after calling the graphics output protocol.

    Further analysis revealed a classic error: after calling SetMode(), the mode information pointer previously obtained through QueryMode() had become invalid, but the code was still using it. This kind of “dangling pointer” problem is very typical in firmware development. The memory ownership and lifetime of protocol interfaces are often very different from application-level development.

    Fix: After SetMode(), call QueryMode() again to obtain the new framebuffer information, ensuring that the pointer always points to valid memory. After the change, the program no longer crashed.

    The lesson is this: when programming in a boot environment, for any pointer passed across interfaces, ask: “Who manages this memory, and when does it become invalid?” When asking AI to repair code, you should also explicitly prompt it to pay attention to resource lifetimes.

    3.3 Issue 2: No Graphics Output Because the Wrong GUID Was Used

    The program no longer crashed, but nothing appeared on the screen. After I reported the symptom to Model A, it reviewed the code again and found that the GUID configuration for the graphics output protocol was wrong.

    In UEFI, there are multiple graphics-output-related GUID definitions, each with a different purpose. If the wrong GUID is referenced, LocateProtocol() cannot correctly find the target protocol instance, and subsequent framebuffer writes naturally all fail. The symptom is “the program is running, but the screen is completely black.”

    What makes this kind of problem subtle is that the compiler does not report an error, and a failed protocol lookup may only return a status code. If the code does not check the return value, the problem is swallowed. The proper approach is:

    1. status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid,
    2. NULL, (VOID **)&gop);
    3. if (EFI_ERROR(status)) {
    4. Print(L"Locate GOP failed: %r\n", status);
    5. return status;
    6. }

    Checking the return value and printing the status for every protocol call is the most cost-effective defensive programming habit in firmware development, and it is also the first method for troubleshooting “black screen” issues.

    4. Round 2: Refinement and Optimization

    Once the program could run, it entered the refinement stage. Three main things were done:

    1. Module decoupling: Split game logic, rendering, and input handling into independent modules, each interacting through clear data structures, avoiding global variables scattered everywhere.
    2. Rendering optimization: Cache the framebuffer base address and pitch, use inline row calculations instead of per-pixel lookups, and reduce the call overhead of drawing functions.
    3. Error path convergence: Route all protocol calls through a unified error-handling path with logging, so that the root cause can be seen immediately through the serial port instead of failing silently.

    5. Methodology Summary

    This “AI repairing AI” experiment was more valuable than simply comparing which model is stronger. It simulates the most common scenario in real engineering: taking over problematic legacy code that you did not write yourself. A few observations:

    • Have AI review the code before making changes. This works much better than directly saying “fix it for me.” The issue list produced by the review is itself a repair roadmap.
    • Give AI runtime symptoms and logs. Static review cannot find runtime problems. Feedback such as “the program crashes and the serial port has no output” can significantly narrow its search space.
    • A human in the loop is indispensable. Judging which repair suggestion is reasonable and how to design verification steps still requires the developer to have low-level knowledge.
    • Modular code has value even when it is “discarded.” Clearly structured code allows the person taking over—whether human or AI—to quickly identify the boundaries of the problem.

    From “six hours and still unable to set up the environment” to “taking over discarded code and repairing it to run,” AI-assisted programming has already shown practical productivity in low-level firmware scenarios. But its ceiling depends heavily on the user’s engineering literacy in asking questions, providing feedback, and verifying results.