Fixing Firmware-Level Graphics Code with LLMs: A Complete Debugging Retrospective from Crash to Stable Run

    This article recounts a hands-on experiment in which a new-generation large language model was tasked with repairing firmware-level graphics code (a Tetris clone) that a previous AI coding model failed to make runnable. It covers the test methodology, two typical defects—a dangling pointer after a display mode switch and a misconfigured graphics protocol GUID—their diagnosis and fixes, and four practical principles for LLM-assisted low-level development.

    Fixing Firmware-Level Graphics Code with LLMs: A Complete Debugging Retrospective from Crash to Stable Run

    1. Background: A Failed AI Generation and a “Relay” Experiment

    Recently, I tried having large language models write classic mini-games (Tetris, Breakout, and the like) in a firmware-level graphics environment. The first attempt, handled by a certain AI coding model, did not go well: after nearly six hours, it couldn’t even set up the build environment, and in the end produced no runnable program.

    I then switched to a combination of “an intelligent coding tool + a new-generation LLM,” and in only about two and a half hours I had a working Chinese-language Breakout game running in a firmware graphics environment. The stark contrast between the two experiences sparked an interesting experimental idea: have the new model clean up the mess left by the previous one—repairing the unrunnable Tetris code. This is essentially “AI cleaning up after AI,” and it also serves as an excellent test of an LLM’s code comprehension and debugging capabilities.

    2. Test Environment and Methodology

    2.1 Environment Migration

    To rule out environmental interference, I copied the source code generated by the first model, untouched, into a brand-new, verified development environment. This ensured that any problems came from the code itself rather than toolchain differences.

    2.2 The Two-Round Testing Method

    The testing followed a common two-stage acceptance standard:

    • Round 1: Get the program to run—eliminate fatal defects such as crashes and hangs, and reach a basic version that enters the main loop and renders.
    • Round 2: Polish and optimize—address quality issues such as image quality, frame rate, input responsiveness, and code maintainability.

    This layered strategy of “make it work first, then make it work well” also applies to everyday development by human engineers. It prevents you from sinking effort into fine-tuning too early.

    3. Step One: Code Review to Build a Full Picture of the Problems

    The first instruction given to the model was simple: “Review this code and identify potential problems.”

    The model then carried out a systematic, module-by-module analysis covering:

    1. Entry function: whether the initialization flow was complete and the protocol-acquisition order was correct;
    2. Main loop: whether there were infinite loops, missing exit conditions, or blocking points;
    3. Game logic: edge cases in collision detection, line-clearing logic, and state machines;
    4. Rendering module: whether the framebuffer address and pixel format were used safely;
    5. Input handling: whether key event reading had error tolerance.

    A few minutes later, the model produced a structured list of issues and judged that “fixing these should make it run.” How credible was that judgment? Time to verify with actual testing.

    4. Testing in Practice: Locating and Fixing Two Typical Defects

    4.1 Defect 1: Crash on Startup—A Classic Dangling Pointer

    The program could initially start, printing “Starting Game Loop” on screen before immediately exiting. Compared to the first model’s version, this was only “slightly better”—still unacceptable.

    Diagnostic approach: I had the model add finer-grained debug messages along the critical path and monitored execution progress via serial output. Serial logs are the most reliable observation tool in firmware-level development, because at this stage there is often no usable console or file system yet.

    Locating the fault: The program crashed after calling the graphics output protocol’s query interface. Deeper analysis revealed a serious problem with the info pointer returned when querying mode information—the memory it pointed to was temporary. Once SetMode was called to switch the display mode, that memory became invalid, and any subsequent access constituted a dangling-pointer dereference, crashing the program.

    The fix: Adjust the ordering so that framebuffer information is re-acquired after SetMode completes, before rendering. Illustrative code:

    1. // Wrong order: query first, then switch mode; the info pointer becomes invalid
    2. QueryMode(..., &info);
    3. SetMode(gop, newMode); // The memory info points to is now invalid!
    4. // Correct order: query after switching modes
    5. SetMode(gop, newMode);
    6. QueryMode(..., &info); // Framebuffer info obtained here is valid
    7. framebuffer = info->FrameBufferBase;

    After the change, the program no longer crashed and entered the main loop smoothly.

    4.2 Defect 2: Misconfigured Graphics Protocol Identifier

    With the crash resolved, the screen still showed no proper output. After I fed the runtime results back to the model, it quickly pinpointed the second problem: the GUID (globally unique identifier) of the graphics output protocol was configured incorrectly.

    In this type of firmware development framework, there is more than one GUID related to graphics output—some identify the protocol itself, while others identify related interfaces or driver instances. If the wrong GUID is referenced, the protocol-locating interface fails silently or returns a null pointer, and the program naturally cannot draw anything.

    The fix: Check the GUID definitions in the framework’s header files, replace the wrong one with the correct graphics output protocol identifier, and add a null check after acquiring the protocol:

    1. status = LocateProtocol(&gCorrectGopGuid, NULL, (void **)&gop);
    2. if (EFI_ERROR(status) || gop == NULL) {
    3. // Fail explicitly and exit safely, rather than continuing with a null pointer
    4. return status;
    5. }

    The lesson from this class of error: in low-level development, the gap between “it works” and “it’s correct” can come down to a single constant—and LLMs that lack knowledge of a specific framework version are especially prone to stumbling on such details.

    5. Lessons Learned: Four Practical Principles for LLM-Assisted Low-Level Development

    1. Review first, modify later. Don’t rush to have the model change code. Have it perform a global review first to build a full picture of the problems; this significantly reduces the chain reaction of “fixing A breaks B.”
    2. Make good use of log-based observation. In an environment without a debugger, serial/console logs are the only source of truth. Place debug messages on the critical path: before and after protocol acquisition, before and after mode switching, and at the main loop entry.
    3. Watch out for lifecycle defects. Dangling pointers, invalidated memory, and stale handles are high-frequency problems in low-level code. Any code sequence that “queries first, then mutates state” requires re-validating previously obtained pointers.
    4. Accept in rounds; control the pace. Round 1 only demands “it runs”; Round 2 is where optimization happens. Clear acceptance criteria are what allow an objective assessment of a model’s actual capability.

    6. Conclusion

    In this experiment, the new-generation LLM demonstrated clearly stronger capability in reviewing, locating, and fixing two typical low-level defects: starting from completely non-running code, it identified the dangling pointer and corrected the protocol identifier configuration, with clear reasoning and a closed feedback loop throughout. That said, it was not a one-shot success—the developer still had to supply runtime results, serial logs, and other on-the-ground information. In human-machine collaboration, the human handles observation and judgment, while the model handles analysis and rewriting. This division of labor has already shown considerable productivity value in high-barrier domains like low-level graphics programming. For developers looking to try similar practices, I recommend first setting up a stable build environment and logging/observation tools, and only then bringing an LLM into the collaboration—you’ll get twice the results for half the effort.