Large Language Models Taking Over Legacy Code: A Practical Guide to UEFI Graphics Programming
This article presents a reusable methodology for using large language models to debug and maintain AI-generated legacy code in UEFI environments. It covers a two-round iteration strategy—runnability first, then refinement—along with code review priorities, two common graphics failures (a dangling pointer after SetMode and incorrect protocol identifiers), their fixes, and five lessons for human-AI collaboration in firmware development.
1. Background: When AI-Generated Code Needs Another AI to Take Over
In real development workflows, “legacy code” is not always written by humans. As intelligent coding tools become part of daily development, more and more projects start with a model-generated first version and are then maintained by humans or other models. A representative scenario from the original practice: one model produced a graphics program skeleton with acceptable modularity across seven files, but the code could not be built or run in the target environment. Another model was then brought in to take over, with the goal of getting the program running first and refining it afterward.
The technical difficulty here is not writing more code, but three specific challenges. First, the target environment is the bare-metal firmware layer, which lacks mature operating system abstractions. Second, graphics output depends on firmware protocols, where pointer lifetimes and protocol identifiers are easy to get wrong. Third, whoever takes over lacks the original design intent and must infer it from the code and runtime behavior. The following sections reconstruct the entire process into a reusable methodology.
2. Overall Approach: Two Rounds of Iteration, Diagnose Before Modifying
The rhythm that proved effective in practice is a two-round approach:
- Round one prioritizes runnability. Make only the minimal fixes needed to start the program, enter the main loop, and display something on screen. Do not pursue architectural cleanup.
- Round two focuses on refinement and optimization. Address input handling, frame rate, drawing logic, resource release, and code structure.
One principle remains firm throughout: every change must be driven by runtime results. Have the model read through the code and produce a list of issues first, then verify them one by one, rather than rewriting everything at once. The reason is that debugging at the firmware layer is expensive, and too many changes at once make it hard to attribute failures.
A typical prompt structure could look like this:
Role: Senior engineer experienced in firmware graphics programmingInput: Full source directory + build instructions + runtime logsTask: Code review only; output an issue list graded as "will certainly cause failure / may cause failure / style issue"Constraints: Do not modify code this round; do not introduce new third-party dependencies
3. Round One: From Not Running to Running Stably
3.1 Code Review Stage
The first step for whoever takes over is a static walkthrough, checking modules in order: entry function, main loop, game logic, rendering module, and input handling. The review usually focuses on four categories of risk:
- Whether handles and protocols are acquired in the correct order;
- Whether pointers passed across functions remain valid after the call returns;
- Whether protocol identifier constants match the firmware specification;
- Whether error codes are ignored, causing failures to be silently swallowed.
Review conclusions should be actionable, for example: “re-query frame buffer information after setting the display mode” or “add return value checks to every protocol call.”
3.2 Failure One: The Main Loop Exits Immediately After Starting
The symptom is that the program enters, prints the main loop startup message, and then returns immediately. The way to locate the problem is to add serial debug output, place checkpoints at key points, and observe where execution stops. The conclusion points to right after the display protocol call: the information structure returned by the mode query becomes invalid after the mode is set. The root cause is a reference to memory that has been overwritten or freed—the classic dangling pointer problem.
The fix follows the principle of “whoever uses it, acquires it”: query the frame buffer address and dimensions again after the mode is set, and check the return value.
// Example: re-query the frame buffer after setting the mode to avoid holding an invalid pointerif (EFI_SUCCESS != gop->SetMode(gop, 0)) {return EFI_UNSUPPORTED;}UINT32 w = 0, h = 0, stride = 0;UINT8 *fb = NULL;if (EFI_SUCCESS != gop->QueryMode(gop, 0, &w, &h, &stride, &fb)) {return EFI_DEVICE_ERROR;}// All subsequent drawing is based on fb / stride; the old pointer is no longer reused
In engineering terms, this can be distilled into three general rules: do not cache volatile pointers across calls; always check return values from firmware interfaces; use serial logs instead of a graphical interface, which is unavailable during debugging.
3.3 Failure Two: The Program Runs but Shows Nothing
The second type of failure is an incorrect protocol identifier. The firmware specification defines more than one graphics-related identifier; picking the wrong one prevents binding to the correct output protocol, which shows up as “no exception, no output.” This kind of problem is hard to catch during static review and must be judged using runtime logs and the protocols actually supported.
The way to handle it is to enumerate the currently installed protocols, print the match results, and then switch to the correct identifier. At the same time, make “protocol bound successfully” a hard assertion during startup: on failure, output a clear error code instead of continuing execution.
4. Round Two: Refinement and Experience Optimization
Getting the program running is only the starting point. Round two typically includes:
- Input handling: debounce key scanning so that a single press is not recognized multiple times;
- Rendering optimization: draw by row stride to avoid screen tearing caused by per-pixel computation;
- Decoupling logic from display: update with a fixed time step, and let rendering only read the current state;
- Resource and state management: separate initialization and reset functions to make restarting a round easier;
- Observability: keep a leveled logging switch and turn off redundant output in production runs.
Acceptance criteria should be quantified, for example: “runs for ten minutes continuously without exiting,” “key response latency within a perceptible range,” and “state fully reset after restart.”
5. Methodology: Five Lessons for AI-Assisted Development
- Context matters more than prompt tricks. Providing the directory structure, build instructions, and runtime logs is more effective than repeatedly telling the model to “be careful.”
- Having the model produce a diagnostic report before touching code significantly reduces regression risk.
- Verify in small steps. Change only one variable at a time and keep a version you can roll back to.
- Make invisible problems visible. Serial logs, assertions, and error codes are the three essentials for firmware-layer debugging.
- Divide labor clearly between humans and models: models are good at pattern-based investigation and boilerplate code, while humans handle the environment, permissions, and final acceptance.
On the compute and model serving side, this type of task typically requires stable inference services, reusable images and container environments, centralized logging services, and monitoring with alerting. The relevant general capabilities of Baidu AI Cloud can serve as a reference direction for engineering support.
6. Conclusion
The value of “having AI clean up after AI” is not about comparing which one is stronger, but about validating a transferable engineering process: review first, drive with logs, apply minimal fixes, and accept against quantified criteria. Once this process is in place, regardless of who generated the code, a team can turn uncontrollable “generated results” into maintainable, deliverable engineering assets.
