Using LLMs to Repair Unfinished UEFI Firmware Code
Large language models can generate business logic well, but results are uneven in low-level systems programming. This article walks through a real UEFI graphics-application case: one model produced a working Breakout game, another left an unrunnable but well-structured Tetris codebase, and the stronger model then reviewed and repaired that unfinished code—fixing a dangling GOP mode pointer and a wrong protocol GUID.
In software development today, large language models are already strong at generating business-logic code, but their results in low-level systems programming are often uneven. Low-level work usually involves complex hardware interaction, strict memory management, and limited debugging environments. This article uses a real graphics-application case in the UEFI (Unified Extensible Firmware Interface) environment to examine how a large language model can take over and repair complex unfinished code, and to show the practical value of intelligent programming tools in systems-level development.
1. From code generation to inheriting a broken codebase
We recently tested how several large language models generate code in a low-level development environment. The task was to write a graphical game that runs under UEFI.
In the first round, one model wrote and ran a Breakout game in about 2.5 hours. It compiled on the first attempt and succeeded on the first run. Another model spent nearly six hours on Tetris and still could not set up a correct build environment, so it never produced a runnable program.
Looking at the source left by the failing model, however, we found that it was not worthless. It generated a modular design across seven files, with a fairly clean structure: an entry function, a main loop, game logic, a rendering module, and input handling. Discarding that code outright would have been a waste. To explore how far a large model can go in refactoring and repair, we used a “fight magic with magic” approach: the model that performed well in the first round would take over and fix the unfinished code that would not run.
2. Phase one: intelligent code review and risk assessment
We copied the unrun source into a new development environment unchanged and started the intelligent programming tool to repair it. The test had two rounds. The first goal was simply to get the program running at a basic level.
The first step was to instruct the model to review the code. It analyzed each module in depth, checking not only syntax but also logical consistency with UEFI development conventions. After a few minutes, it produced a detailed issue list covering memory allocation, protocol-interface calling conventions, and data-structure initialization, and it optimistically said the program should run once those issues were fixed.
We applied the suggested changes and tried to compile and run. The program no longer reported errors, but the screen only briefly showed “Starting Game Loop” before the program exited. In low-level development, this kind of flash-and-exit is often very hard to diagnose because there is no modern operating-system crash report. Even so, it was progress compared with a program that would not run at all.
3. Phase two: locating runtime failures and repairing them in depth
We then moved into real debugging of the abnormal exit.
3.1 A flash exit and a dangling-pointer trap
To capture more information, we asked the model to insert extensive serial debug logs so we could watch execution progress in QEMU or on real hardware. The serial output clearly showed that the program failed after calling GOP (Graphics Output Protocol).
From the logs, the model quickly narrowed the problem to the GOP calls QueryMode and SetMode. In UEFI development, GOP is the core protocol for controlling on-screen graphics. QueryMode returns a pointer to EFI_GRAPHICS_OUTPUT_MODE_INFORMATION. After SetMode switches resolution or display mode, however, the underlying framebuffer and mode-information memory may be reallocated or freed. The info pointer obtained earlier from QueryMode then becomes a dangling pointer to memory that is no longer valid. A later access to that pointer triggered a hardware exception and the flash exit.
Fix: The model’s solution was direct and consistent with the UEFI specification: after SetMode, call QueryMode again to obtain the latest framebuffer information, or read the current FrameBufferBase and FrameBufferSize directly from the Mode member of the GOP protocol structure. After that change, the program stayed resident and no longer exited immediately.
3.2 A blank screen and a wrong GUID
The program no longer exited, but the screen stayed black and the expected game graphics never appeared. We reported that behavior to the intelligent programming tool again.
By analyzing the run result and the current code context, the model concluded that the GOP protocol GUID was wrong. In the EDK2 (EFI Development Kit II) environment, UEFI protocols are located by a specific GUID (globally unique identifier). The system defines several GOP-related GUIDs. If gBS->HandleProtocol or LocateProtocol is called with the wrong GUID, the system cannot obtain a valid graphics-output protocol interface pointer.
The model compared the code with header definitions in the EDK2 standard libraries and found that a similar but different GUID had been used. After correcting it to gEfiGraphicsOutputProtocolGuid, the program successfully obtained the GOP interface and rendered the initial Tetris screen.
4. Takeaways
This “AI cleaning up after AI” repair exercise supports a few conclusions.
First, in low-level systems development, large language models can do more than generate code. They can also review and debug in a way that resembles a senior engineer. Given serial logs, protocol rules, and memory-management principles, the model pinpointed hard-to-see bugs such as a dangling pointer and a mismatched GUID.
Second, for complex low-level work, asking an AI to generate everything from scratch can stall on environment setup and similar obstacles. Using its logical analysis to repair and refactor existing unfinished code can produce better results than expected.
Finally, the exercise suggests a practical workflow: in low-level environments that lack mature debugging tools, treat the large model as a pair-programming assistant. Describe the symptom in natural language, provide logs, and let its technical knowledge reason about the cause. That can substantially speed up systems-level bug hunting. As models understand low-level specifications more deeply, their potential in firmware development, driver writing, and related areas will be considerable.
