AI Coding Assistant in UEFI: Using One AI to Repair Another AI’s Broken Code

    A practical review of using an AI coding assistant to diagnose and repair a nonfunctional Tetris game in a UEFI Shell environment. The process highlights how runtime feedback, serial logging, protocol debugging, and structured legacy code affect AI-assisted development in low-level systems.

    1. Background: Asking AI to Clean Up AI-Generated Code

    A popular way to evaluate AI coding assistants is to give them genuinely difficult engineering tasks rather than simple requests such as “write a function” or “fix a bug.” In an earlier comparison, I asked two different coding tools to build games in a firmware environment.

    One tool produced a compilable and runnable Chinese-language Breakout game in about two and a half hours. The other spent nearly six hours without even setting up a working build environment. In the end, it left behind code that looked reasonably well designed but could not run.

    After the article was published, a reader pointed out that the two models had been given different tasks, making the comparison unfair. That criticism was valid. My original goal was not to conduct a rigorous benchmark, but to explore what AI can actually do in real-world engineering work.

    However, the comment raised a more interesting question:

    Was the nonfunctional code worthless, or was it simply unfinished?

    The project contained seven source files with a clear modular structure. The entry point, main loop, game logic, rendering, and input handling were separated cleanly. Throwing it away seemed wasteful.

    So I decided to give the project to another AI coding assistant and ask it to clean up the mess: first make the code run, then refine and optimize it.

    This is actually closer to real software development than generating a program from scratch. Developers frequently need to take over code written by someone else—or by another AI—understand it, diagnose it, and repair it. That kind of maintenance work is common in daily engineering and is a much better test of a tool’s practical capabilities.

    2. Test Environment and Task Definition

    Before looking at the debugging process, it is important to clarify the environment and the difficulty of the task.

    • Target platform: The program ran in a UEFI Shell environment rather than in user space on a conventional operating system. There was no operating system to depend on, no standard library, and no ready-made graphics API. All services had to be accessed through the protocols provided by UEFI.
    • Task: Repair a Tetris game in two rounds. The first round focused on basic functionality: displaying graphics and responding to input. The second round was reserved for refinement and optimization.
    • Workflow: I interacted with the coding assistant through the command line. The AI read the source code, analyzed potential problems, and modified the project. I handled compilation, flashing, execution, and reporting the observed behavior.

    This “AI edits code, human runs and verifies it” loop is currently one of the most typical collaboration patterns for AI coding tools. It is also the pattern that most depends on efficient cooperation between the human and the model.

    3. Round One: From Broken Code to a Running Program

    3.1 Start with a Code Review

    When taking over an existing codebase, the right first step is not to start changing code immediately. It is to build an overall understanding of the project.

    My first instruction to the AI was simple:

    1. Review this code and identify issues that could prevent the program from running.

    The AI examined the major modules one by one, including the entry point, main loop, game logic, rendering, and input handling. A few minutes later, it produced a list of possible problems involving initialization order, pointer usage, and protocol access. It claimed that the program should run after those issues were fixed.

    There is an important methodological point here: an AI-generated issue list is a list of hypotheses, not a set of conclusions.

    The model had made its initial judgments through static code inspection. Each one still needed to be verified by running the program. I asked it to apply the changes, then compiled and executed the result. The real debugging process started at that point.

    3.2 Problem One: A Crash Caused by a Dangling Pointer

    The modified code compiled successfully and launched. The screen displayed the log message Starting Game Loop, and then the process exited immediately.

    That was slightly better than failing to build the environment, but it was still far from playable.

    For this kind of startup crash, my approach was to ask the AI to add diagnostic output and monitor execution through a serial connection. In a UEFI environment, there is no conventional screen-based debugger, so serial logging is one of the most reliable ways to observe program behavior.

    The AI inserted log messages at several points along the critical execution path. After another run, the crash was narrowed down to a call involving the Graphics Output Protocol, or GOP.

    Further analysis revealed a classic C pointer-lifetime bug:

    • The code first called QueryMode to obtain information about the current display mode. This returned a pointer to a mode information structure named info.
    • It then called SetMode to switch the display mode.
    • After SetMode, the firmware rebuilt its internal display state. The memory referenced by the old info pointer was no longer valid, leaving a dangling pointer.
    • The code later dereferenced that pointer, which caused the exception.

    The fix was straightforward: after SetMode completed, the code had to query the display mode information again instead of reusing the pointer obtained before the mode change.

    The relevant logic was updated as follows:

    1. // Incorrect: continue using the old info pointer after SetMode
    2. gop->QueryMode(gop, gop->Mode->Mode, &info_size, &info);
    3. gop->SetMode(gop, target_mode);
    4. // info is no longer valid here; using it may cause a crash
    5. framebuffer = (UINT8*)gop->Mode->FrameBufferBase;
    6. // Correct: retrieve the information again after SetMode
    7. gop->SetMode(gop, target_mode);
    8. gop->QueryMode(gop, gop->Mode->Mode, &info_size, &info);
    9. framebuffer = (UINT8*)gop->Mode->FrameBufferBase;

    After this change, the program stopped crashing.

    This was a useful reminder that problems involving state or memory becoming invalid across an API call can be easy to miss through static inspection alone. Once runtime behavior was available, the AI was able to narrow the search efficiently. Of course, a human still had to be involved: I had to accurately report the crash, and that information guided the AI toward changes in runtime state rather than purely syntactic errors.

    3.3 Problem Two: An Incorrect GUID Prevented Graphics Output

    The program was now running reliably, but a new problem appeared: the graphical interface was still missing.

    The logs showed that execution had reached the rendering stage, yet nothing appeared on the screen.

    After I reported this behavior, the AI identified another common issue in low-level development: the wrong GUID had been used to locate the GOP protocol.

    Some background is useful here. In the UEFI development framework, multiple related GUID definitions may exist for different purposes. A GUID, or globally unique identifier, acts as a key for locating services in UEFI. If even one byte is incorrect, LocateProtocol or HandleProtocol may fail to locate the intended service. The result may be a null pointer or a protocol instance with completely different semantics.

    These errors are particularly difficult to diagnose because they can compile successfully and may not cause an immediate crash. Instead, the feature simply fails silently.

    The AI compared the GUID used in the source code with the definitions in the framework headers. It confirmed that the code was using the wrong variant. After replacing it with the correct Graphics Output Protocol GUID, the display began working and the core rendering path was finally complete.

    4. Four Lessons from the Experiment

    The first round produced several lessons that are relevant to developers using AI assistants for low-level or systems programming.

    1. Treat AI output as a hypothesis, not an answer

    An AI code review can quickly produce a list of candidate problems and significantly reduce the search area. However, every item still needs to be validated through execution.

    A productive collaboration loop looks like this:

    AI proposes a hypothesis → the human runs the program → runtime behavior is reported back → the AI narrows the diagnosis.

    2. Without observability, the AI has only half the map

    In a black-box environment such as firmware, serial logs, crash addresses, and precise descriptions of observed behavior are among the most valuable information a human can provide.

    Investing time in basic diagnostic infrastructure—even if it consists of only a few log statements—can produce a much greater return than expected.

    3. The structure of legacy code determines how effectively AI can repair it

    The previous codebase could not run, but its module boundaries were clear. That allowed the AI to build a global understanding quickly and identify likely problem areas with reasonable precision.

    If the same project had been a single, tangled file containing several hundred lines of code, even an AI assistant would have had a much harder time taking it over efficiently.

    This also suggests that maintaining good structure from the first line of AI-generated code is an investment in future human-AI collaboration.

    4. “One AI repairing another AI’s code” is a representative test scenario

    Compared with generating a program from scratch, taking over defective existing code is much closer to maintaining legacy systems in an enterprise environment.

    It also provides a better way to distinguish models based on their ability to understand code and reason from runtime behavior.

    5. Final Thoughts

    This experiment gave me a more concrete view of the capabilities and limitations of large language model coding assistants.

    The assistant was able to understand code, analyze root causes, and make targeted repairs in a complex low-level environment. Its ability to combine runtime symptoms with code analysis was especially impressive.

    However, it still depended on a human to provide the environment, report observed behavior, and verify the results. The AI acted as an amplifier, not an autonomous system.

    The first-round goal—getting the program to run—was achieved. For the second round, I plan to ask the AI to refine rendering performance, input responsiveness, and the piece-drop algorithm. Those results will be covered separately.

    For developers using AI in low-level or otherwise complex engineering projects, this experience reinforces a practical principle: the quality of the collaboration depends not only on the model, but also on the quality of the feedback loop between the model and the person running the code.