Practical Exploration of Large Language Models in UEFI Low-Level Development and Code Refactoring
This article explores the real-world capabilities of large language models (LLMs) in low-level firmware development, using UEFI graphical game development as a test scenario. It covers how one model generated working code from scratch, how another model's unfinished Tetris project was rescued and refactored by a stronger model, and details the debugging of a wild pointer bug caused by calling SetMode after QueryMode under the UEFI Graphics Output Protocol (GOP). Readers will learn about firmware-level code review, runtime defect diagnosis, double buffering, non-blocking input handling, and how AI-assisted review, defect fixing, and refactoring can form a practical workflow for low-level system development.
As large language models (LLMs) continue to advance, they have achieved remarkable results in generating and refactoring high-level business code. However, their performance in low-level system development — such as UEFI (Unified Extensible Firmware Interface) — remains challenging. Low-level firmware development involves direct hardware interaction, video memory management, and protocol invocation, all of which demand exceptional code rigor and system-level understanding. Recently, we conducted a series of tests in this domain to evaluate the real-world capability of LLMs in firmware-level graphical application development, with a particular focus on using LLMs to refactor legacy code and fix defects.
1. Background and Task Breakdown
To comprehensively assess LLM performance in low-level development, we designed a concrete test scenario: asking LLMs to build a graphical game in the UEFI environment. In the first round, we assigned different graphical application tasks to two different AI models.
One model successfully generated a brick-breaker game in just over two hours. The code compiled on the first attempt and ran correctly on the very first launch, demonstrating strong low-level code organization skills. The other model, tasked with building a Tetris game, ran into trouble — after nearly six hours, it had not even set up the compilation environment correctly, and ultimately failed to produce a runnable program.
However, despite its failure to run, a closer look at the generated code revealed a well-structured project: seven separate code files with a clean modular design, properly separating the entry function, main loop, and business logic. Discarding this code outright seemed wasteful, so we launched a second, deeper round of practice: letting the better-performing model take over this “half-finished” code and transform it from abandoned code into a working program. This “AI fixing AI’s mess” approach not only validates the refactoring capabilities of LLMs, but also provides a reference for taking over legacy projects in real engineering work.
2. Phase One: Code Review and Architecture Analysis
We copied the source code left behind by the previous model, unchanged, into a fresh development environment, and launched an intelligent coding tool with stronger code comprehension. The test was divided into two rounds; the core goal of the first round was simply to make the code run.
The first task was a global code review. Our instruction was explicit: read and analyze this UEFI application code, and identify potential issues that could cause compilation or runtime failures. The model quickly got to work, examining each core module one by one:
- UEFI entry function: checked the parameter definitions and return value conventions of
efi_main. - Game main loop: analyzed the event-waiting mechanism and frame-rate control logic.
- Game logic module: evaluated the soundness of collision detection and state machine design.
- Graphics rendering module: checked GOP protocol initialization and framebuffer write logic.
- Input handling: verified the usage of the Simple Text Input Protocol.
Within a few minutes, the model produced a detailed issue list, pointing out a series of risks that could cause runtime failure, including uninitialized pointer references and incorrect protocol invocation order. It indicated that if all items on the list were fixed, the program should start successfully.
3. Phase Two: Runtime Defect Diagnosis and Wild Pointer Fix
After applying the fixes suggested in the review, we attempted to run the program. Initially, the console successfully printed the log message “Starting Game Loop” — but then the program silently crashed. In UEFI development, such anomalies typically stem from out-of-bounds memory access, division by zero, or low-level pointer reference errors.
To pinpoint the issue, we instructed the AI model to inject more detailed debug information at key execution points, monitoring progress via serial port output. The logs showed that the program crashed after calling the UEFI GOP (Graphics Output Protocol) interface to set the display mode.
After deeper analysis, the model identified the root cause. In the original code’s logic, the program first called QueryMode to obtain information about the current or target display mode. This interface returns a pointer info to an EFI_GRAPHICS_OUTPUT_MODE_INFORMATION structure. The program then called SetMode to actually switch the display resolution.
The problem: after SetMode executes, the underlying firmware may reallocate or adjust the memory layout of the framebuffer. At that point, the memory region pointed to by the previously returned info pointer may have become invalid — a wild pointer — and the saved framebuffer base address may also be stale. When the program subsequently attempted to access video memory through these expired pointers and addresses to draw the game screen, it triggered the system’s protection mechanism and crashed.
The fix was precise: the AI model refactored the GOP initialization flow. After the SetMode call, QueryMode is forcibly called again to obtain the latest framebuffer physical address, size, and pixel format information. The pseudocode is as follows:
// Set the target modeStatus = Gop->SetMode(Gop, TargetMode);if (EFI_ERROR(Status)) {return Status;}// Fix: QueryMode must be called again after SetMode to get the latest video memory infoStatus = Gop->QueryMode(Gop, Gop->Mode->Mode, &SizeOfInfo, &Info);if (EFI_ERROR(Status) || Info == NULL) {return Status;}// Update framebuffer base address and sizeFrameBufferBase = Gop->Mode->FrameBufferBase;FrameBufferSize = Gop->Mode->FrameBufferSize;
After this change, the program no longer crashed. It successfully entered the game’s main screen and correctly rendered the basic Tetris background — marking the successful completion of the first round of testing.
4. Phase Three: Rendering Logic Refinement and System-Level Optimization
With the basic “survival” problem solved, the test moved into the second round: refinement and optimization. Although the program ran, the efficiency of low-level graphics rendering and the real-time responsiveness of input handling still needed improvement.
We had the AI model refactor the game main loop. First, to address screen tearing caused by writing directly to the framebuffer, the model introduced the concept of double buffering: allocating an off-screen buffer in memory the same size as the video memory. All game elements are drawn into this back buffer, and once a frame is fully rendered, it is copied to the real video memory in a single memory-copy operation.
Second, the keyboard event polling logic was optimized. The original code used the blocking WaitForEvent, resulting in high CPU usage and sluggish response. The model replaced it with the non-blocking CheckEvent detection mechanism, so that game logic continues to advance when no key event is pending — significantly improving the frame rate and overall smoothness.
In addition, the model automatically fixed several boundary-condition bugs in edge collision detection and mitigated the risk of array out-of-bounds access during piece rotation, making the overall gameplay experience more polished.
5. Summary and Reflections
Through this in-depth practice, we can see that today’s LLMs already possess a meaningful degree of capability in low-level system code writing and complex defect diagnosis. They can not only generate logically rigorous code from scratch, but also take over and refactor legacy code left by other models or human developers, precisely locating deep system-level defects such as wild pointers and incorrect protocol invocation order.
In real-world enterprise development, when combined with stable computing power and efficient development platforms provided by cloud providers such as Baidu AI Cloud, the potential of LLMs to assist in writing low-level drivers and firmware-level applications can be further unlocked. Looking ahead, a workflow combining “AI-assisted code review + AI defect fixing + AI code refactoring” will become an important approach for improving low-level system development efficiency and reducing maintenance costs, offering developers powerful intelligent support when facing complex low-level programming challenges.
