AI Coding Assistants in UEFI Development: From Broken Code to a Running Application

    Testing AI coding assistants in bare-metal UEFI environments reveals significant differences in engineering capabilities. While some models fail to produce runnable code, their well-structured output can still be salvaged. This article explores a two-iteration debugging methodology to fix AI-generated UEFI code, covering typical pitfalls like wild pointers and GUID misconfigurations. It also outlines four reusable principles for effective human-AI collaboration in low-level system development.

    1. Background: When AI Coding Assistants Meet Firmware-Level Development

    Most discussions about whether “AI can write code” remain at the application layer—building web pages, writing scripts, or generating Python snippets. However, the true test of a model’s engineering capabilities often lies in the lower levels: the world before the operating system boots, namely the UEFI environment. There are no standard libraries, no mature debuggers, and no graphical frameworks. You must fetch the frame buffer through protocols yourself and handle input manually. Even getting a simple square to appear on the screen requires a correct understanding of firmware specifications.

    I recently conducted a series of experiments: I had different large language model (LLM) coding assistants implement classic mini-games in a bare-metal UEFI environment—one was tasked with building Breakout, and the other Tetris. The results were starkly different. The former produced a program that compiled on the first try and ran successfully within just over two hours. The latter struggled for nearly six hours, failing to even set up the compilation environment, ultimately leaving behind code that “looked well-structured but couldn’t run.”

    This contrast sparked an interesting follow-up question: Is well-structured but non-running AI-generated code worth saving? And who should save it? Thus, I conducted a second experiment: I handed the broken code, exactly as it was, to another model to see if it could clean up the mess. This article provides a complete review of this process and extracts a reusable methodology.

    2. Experiment Design: The Two-Iteration Method

    Given that expecting an AI to deliver a perfect program in one shot is unrealistic, I broke the task into two iterations:

    • Iteration 1: From broken code to running. The goal was simple—the program should compile, boot, and the main loop shouldn’t crash. We weren’t aiming for a good user experience, just for it to “stay alive.”
    • Iteration 2: Refinement and optimization. Based on a running program, we would improve rendering, input responsiveness, and code quality.

    The logic behind this split is that debugging and optimization are entirely different cognitive tasks. Debugging requires localization skills (reading logs, analyzing crash points, inferring root causes), while optimization requires refactoring skills (understanding architecture, improving implementation). Mixing the two often leads the AI to aggressively rewrite code before the root cause is fully understood, inadvertently introducing new bugs.

    3. Iteration 1 in Practice: Three Typical Issues

    3.1 Code Review: Diagnose Before Acting

    After copying the old code into the new environment, my first instruction was very simple: “Review this code and identify potential issues.”

    This step is crucial. Many developers who receive AI-generated code tend to run it immediately and ask the AI to fix it only when it crashes. In doing so, the only information fed back to the model is “the program crashed,” which provides extremely insufficient context. The correct approach is to have the model perform static analysis first. A few minutes later, the model provided a checklist of issues covering the entry function, main loop, game logic, rendering module, and input handling, claiming it would run once fixed.

    Things didn’t go that smoothly, of course, but this checklist provided a roadmap for subsequent dynamic debugging.

    3.2 Issue 1: Program Crashes—A Classic Wild Pointer Case

    After modifications, the program could boot, print “Starting Game Loop,” and then immediately exit. This was a step forward from “couldn’t compile,” but it was still unusable.

    I asked the model to add debug information and monitor execution progress via serial output. In UEFI development, this is almost the only reliable observation method—before a GUI exists, serial logs are your eyes. The logs showed that the exception occurred after a Graphics Output Protocol (GOP) call.

    The root cause analysis was highly representative: The code fetched a pointer to mode information when calling QueryMode, and then called SetMode to switch the display mode. However, SetMode invalidates the previously acquired frame buffer information—that block of memory essentially becomes a wild pointer, and any subsequent access will inevitably crash.

    The fix is to re-fetch the frame buffer information after SetMode:

    1. // Pseudocode illustration: Must refresh info after switching mode
    2. gop->SetMode(gop, modeNumber);
    3. // Wrong approach: Reuse the info pointer obtained before SetMode
    4. // Correct approach: Re-query the current mode information
    5. gop->QueryMode(gop, gop->Mode->Mode, &infoSize, &info);

    This is a common pitfall in firmware development: The lifecycle of pointers returned by protocols is tied to hardware state. Once the state changes, the old pointer becomes invalid. Application-layer programmers often lack intuition for this kind of “implicit invalidation,” and if the AI’s training data lacks relevant cases, it will easily fall into the same trap.

    3.3 Issue 2: Wrong GUID Configuration—Details Determine Success

    The program no longer crashed, but the graphical interface wouldn’t appear. After feeding this symptom back to the AI, the model deduced that the issue lay in the GOP protocol’s GUID configuration.

    The EDK2 development framework defines multiple GUIDs related to graphics output that look similar but have different semantics. Using the wrong one causes protocol location to fail, which naturally causes all subsequent graphics calls to fall through. The educational value of this type of issue lies in the fact that many failures in UEFI development are not logic errors, but “wrong constants”—similar macro names, incorrect header file inclusion, or protocol version mismatches. AI actually has an advantage here because this type of knowledge exists abundantly in documentation and community code, and the model’s retrieval and matching capabilities are more reliable than human memory.

    After the fix, the program finally ran completely. The goal of Iteration 1 was achieved.

    4. Methodology: Four Principles for AI-Assisted Debugging

    Reviewing the entire process of “one AI cleaning up another AI’s mess,” I summarized a few reusable experiences:

    1. Static review first. When you receive any code (whether written by a human or AI), have the AI perform a systematic review first to build a problem list before entering dynamic debugging. The assumptions generated during the review can significantly narrow the search space.

    2. Enable observation channels. Low-level development lacks the luxury of having printf available everywhere. Setting up serial log infrastructure in advance is the prerequisite for all debugging. AI can help write logging code, but the architectural decision of “whether to leave observation points” must be made by a human.

    3. Feedback loops need phenomena, not emotions. The information fed back to the model in each round should be “phenomenon + excluded possibilities + relevant logs,” not just “it still doesn’t work.” Information density determines the quality of AI’s diagnosis.

    4. Layered iteration, don’t aim for a one-hit wonder. Get it running first, then refine. Solve only one category of problems per round. AI performs far better with a single objective than with multiple objectives.

    5. Conclusion: Where is the Boundary of AI’s Engineering Capability?

    The biggest takeaway from this experiment is that evaluating a coding model shouldn’t just focus on “whether it can write code,” but rather on whether it can locate and solve problems in harsh environments lacking full context and filled with hardware constraints. Many models can write beautifully modular code (the author of the broken code also achieved this), but inferring the invalidation timing of a wild pointer from scrolling serial logs is the true dividing line.

    Another insight is about the value of broken code. Code that is well-structured but doesn’t run is not meaningless—clear module division greatly increases the efficiency of subsequent AI diagnostics. The readability of AI-generated code is an asset in itself, determining the repair cost for the next handler (whether human or AI).

    For developers, the correct way to use AI coding assistants in low-level development is not to “trade a single prompt for a finished product,” but to treat it as a knowledgeable, tireless pair programmer who requires strict acceptance testing. You set the direction, set up observations, and perform validation, while it searches documentation, writes implementations, and checks details. When the division of labor is right, efficiency can truly improve.