AI-Assisted UEFI Bare-Metal Development: From Generated Code to a Working Build

    This article explains why bare-metal UEFI and BIOS environments are a hard test of large language models, and describes a two-stage method for reviving failed generated code: review first, get the program running, then refine it. It covers two common failure modes—stale pointers after mode switches and incorrect graphics protocol GUIDs—and four engineering rules for using AI on low-level firmware work.

    1. Why Bare-Metal Environments Stress-Test AI Coding

    1.1 How Ordinary Development Differs from Bare-Metal Work

    Large language models are already fairly reliable at generating web applications, scripts, and even small or medium server programs. Those domains have abundant training material, and the feedback loop is short: if a page is wrong, a refresh shows the problem immediately.

    UEFI and BIOS bare-metal environments are different:

    • There is no operating system safety net. There is no process management, no standard library, and no exception handling. An out-of-bounds memory access can freeze or exit the machine instead of raising a readable exception.
    • The feedback loop is long. Changing one line often means rebuilding, redeploying a firmware image, and rebooting the target. One iteration can take several minutes.
    • Debugging tools are limited. Full breakpoint debugging is often unavailable. Serial logs are the main way to observe what the code is doing.
    • Training material is sparse and fragmented. Firmware development is a niche field, implementations differ widely, and models see far fewer reliable samples than in general-purpose programming.

    Bare metal is therefore a useful pressure test of a model’s real engineering ability. It is not surprising when a model fails here. One reported case asked a model to write a game under UEFI and spent hours without even getting a build environment working. The more interesting question is this: the leftover code framework was structurally reasonable—what was actually wrong, and could it be salvaged?

    1.2 How This Method Is Organized

    The practice that follows uses a deliberate two-stage strategy:

    • Stage 1 (Round 1): The only goal is to make the program run reliably.
    • Stage 2 (Round 2): Once it runs, improve code quality and polish it.

    That order matters. Many developers ask an AI to “write a complete, elegant, high-performance program” in one shot. The model then tries to solve correctness and style at the same time and does neither well. Correctness problems often hide in environment-interaction details and must be checked one by one against real runs. Style can wait until the program actually works.

    2. First Step: Ask the AI to Review, Not to Rewrite

    After you have leftover code from a previous failed attempt, the right opening move is not an immediate rewrite or patch. Start with a minimal instruction:

    1. Review this code and find issues that could prevent it from running.

    Have the model inspect the entry point, main loop, game logic, rendering, and input handling one by one. This step is useful for three reasons:

    1. It builds a global context. If you ask the model to edit immediately, it often focuses only on the spot you named. A review forces it to read every module and form an overall picture.
    2. It produces a structured issue list. The model may report items such as “initialization order is wrong at X,” “pointer lifetime is suspect at Y,” or “the protocol GUID at Z may be incorrect.” That list is a debugging roadmap.
    3. A human can correct the model’s reading. If the review clearly conflicts with your experience, the model has misunderstood the code. Fix that understanding before changing anything.

    3. Two Typical Failures and How They Were Fixed

    3.1 Crash on Exit: A Stale Pointer After a Mode Change

    After the review issues were fixed, the program built and ran. It started, printed a main-loop startup log, and then exited. That was already progress over “it will not compile”: entry and basic initialization were mostly correct, and the bug was deeper in runtime logic.

    The way to narrow it down is to add debug output and watch execution over the serial port. On bare metal, that is almost the only reliable observation method:

    1. DEBUG((EFI_D_INFO, "Before GOP init\n"));
    2. Status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid,
    3. NULL, (VOID**)&Gop);
    4. DEBUG((EFI_D_INFO, "Gop locate status: %r\n", Status));

    Instrumentation showed that the program failed after a Graphics Output Protocol (GOP) call. The root cause was a classic dangling pointer: the mode-information pointer returned by QueryMode became invalid after SetMode.

    In ordinary programming this might appear to work if the memory has not yet been reused. On bare metal, memory management is coarse and unprotected. Invalid memory is reclaimed and reused immediately, so the next access crashes.

    The fix is direct: after SetMode, fetch framebuffer information again instead of reusing the old pointer.

    1. // After SetMode, query mode information again instead of reusing the old pointer
    2. gBS->Stall(100000); // Wait for the mode switch to settle
    3. Status = Gop->QueryMode(Gop, Gop->Mode->Mode,
    4. &SizeOfInfo, &Info);
    5. FrameBufferBase = Gop->Mode->FrameBufferBase;

    Lesson: After any mode switch or resource reconfiguration, reacquire every derived pointer. Do not keep references from before the switch. A model often cannot infer this rule from static review alone. You need the runtime symptom plus instrumented logs.

    3.2 No Graphics: The Wrong Protocol GUID

    Once the crash was fixed, the program stayed up, but nothing appeared on the screen. Feeding that symptom back to the model pointed to an incorrect GOP protocol GUID.

    This is a classic, easy-to-miss firmware bug. Graphics output is associated with more than one GUID. Different GUIDs correspond to different lookup paths and versions. With the wrong GUID, protocol location returns “not found” or an empty implementation. The program does not crash; graphics simply fail silently. Silent failure is among the hardest bugs to diagnose because there is no crash site.

    The repair approach:

    1. Confirm that the GUID in the code comes from the specification headers of the current toolchain, such as an EDK2-style firmware framework.
    2. Check that the GUID passed to protocol location matches the protocol type used in the later cast.
    3. Log the status returned by LocateProtocol so you can tell “wrong GUID” from “this protocol is not present in the current firmware.”

    4. Four Engineering Rules for AI-Assisted Low-Level Development

    4.1 Two Stages: Make It Run, Then Refine

    Separate correctness from elegance. In the first stage, only require stable execution. Ugly code and verbose logs are acceptable. After it runs, use a second stage for decoupling, naming, and log cleanup. If both happen at once, the model may add extra abstraction for the sake of structure and increase the number of variables in the first successful run.

    4.2 Verify Only One Change at a Time

    Bare-metal iteration is expensive, so control variables. When you ask the AI to edit, state clearly: “This change only fixes the pointer lifetime; leave everything else alone.” If five things change at once and the build crashes, you cannot attribute the failure and must roll back.

    4.3 Feed the Model Symptoms, Not Guesses

    Paste serial logs, the crash timing, and the last line printed before exit. That is far more useful than saying “the program will not run.” A model’s diagnosis depends heavily on the quality of the input. Both bugs above were solved by a complete description of the symptom plus model analysis, not by grinding through the code by hand.

    4.4 Failed AI Code Is Not Necessarily Scrap

    The leftover code from the previous failure still had a modular design, seven files, and clear responsibilities. That suggests the model was not weak at architecture. The failures clustered in environment-interaction details: GUIDs, pointer lifetime, and initialization order. Do not rush to rewrite from scratch. Review first, then fix specific points. That costs less than starting over.

    5. Conclusion

    This practice brought code that had been written off back to life: from an immediate exit to a stable run, and from a black screen to visible graphics. Each step followed the same loop: review and locate, change one thing, verify on the serial port.

    It points to a useful fact: in low-level development, a large model’s weakness is not architecture or coding in general, but the lack of perception of the real runtime environment. The model cannot see your serial output, and it cannot tell that a dangling pointer’s memory has already been reclaimed. The developer’s job is to act as the sensor between the model and the physical environment—bring the symptoms back faithfully and feed precise conclusions in. This human-machine debugging loop is likely to become a standard way to do systems programming with AI. As models get better at using runtime feedback, for example through automated log return, that loop will shorten and AI assistance on bare-metal work should succeed more often.