Building UEFI Graphical Applications with AI Coding Tools: A Practical Guide

    Developing graphical applications at the UEFI/firmware layer is far harder than writing graphics code on top of an operating system. There is no OS safety net, graphics come from protocols rather than libraries, and memory and addressing semantics are strict. This guide explains how to use AI coding tools effectively in this environment: it breaks the work into four verifiable stages, analyzes two of the most common failure modes (an invalid framebuffer pointer after SetMode, and GOP addressing errors that leave the screen blank), and shows how to make AI a reliable collaborator. It also proposes a more rigorous way to evaluate AI coding tools and practical engineering practices for turning firmware development into a repeatable team capability.

    1. Why UEFI Graphical Applications Are a Hard Development Scenario

    When you write a graphical program on top of an operating system, the language runtime, graphics libraries, font rendering, event loop, and memory management are all handled by the system, so you mostly deal with business logic. At the UEFI/BIOS layer, the situation is completely different:

    • No OS safety net. Your code runs in a firmware environment. If the main loop blocks or you write to the wrong address, no exception is thrown — it simply shows up as a freeze, a black screen, or an unresponsive machine.
    • Graphics come from protocols, not libraries. Screen output goes through the Graphics Output Protocol (GOP), so you handle mode setting, framebuffer location, and pixel writing yourself.
    • Stricter memory and address semantics. Pointers returned by protocols often point to firmware-managed buffers whose lifetime is tightly tied to the call sequence. Reusing an old pointer easily triggers “wild pointer” failures.
    • A complex build chain. You need a cross-compilation toolchain, module description files, and firmware mapping descriptions. With so many configuration options, even standing up the environment the first time can take a long while.

    Because this kind of work combines a high environment barrier, weak feedback signals, and detailed specification requirements, it is both excellent material for testing the real capabilities of AI coding tools and one of the areas where they fail most easily.

    2. Four Gates for AI-Assisted Development and the Right Pacing

    Instead of asking the model to “write the entire program in one shot,” a more reliable approach is to split the work into verifiable gates and pass each one before moving on.

    Gate 1: The build environment compiles. Aim only for “the build produces a correct executable module.” Let the AI generate the module description files, dependency declarations, and build configuration, while you confirm the toolchain version and directory structure. Failures here are usually configuration or dependency problems, not code problems, and feeding the complete error log back to the model verbatim works far better than letting it guess the cause.

    Gate 2: It loads and prints the first log line. The goal is a minimal runnable skeleton: the entry function acquires the protocols, clears the screen, and outputs a line of text. Write no game logic yet. The value of this step is that it isolates the three failure classes — load failure, protocol-acquisition failure, and display failure — so they do not get tangled up with business-logic errors.

    Gate 3: A closed graphics-and-input loop. Connect the full chain: set the graphics mode → get the framebuffer → draw a color block → poll keyboard events → refresh the position. Start by replacing real assets with a rectangular color block, and only introduce textures or character rendering after confirming that the refresh timing and coordinate system are correct.

    Gate 4: Feature completion and experience tuning. Add collision detection, scoring, levels, pause, sound cues, and exception recovery. This gate has the most code and depends most heavily on the stable baseline built by the previous gates.

    3. A Deep Look at Two Common Failures

    3.1 The framebuffer info pointer goes stale after setting the graphics mode

    This is the most typical hidden defect in UEFI graphics development, and it is often misdiagnosed as a “program crash.”

    The Graphics Output Protocol provides two kinds of interfaces: query the mode and set the mode. A common pattern is: first query the current mode and get a struct describing the framebuffer base address and dimensions, then call the set-mode interface to switch to the target resolution, and then keep using that earlier struct’s pointer to write pixels.

    The problem is that setting the mode triggers a reallocation of graphics resources, so the memory region holding the framebuffer may move, and the struct pointer you obtained earlier now points to invalid memory. Dereferencing it to write pixels produces the symptom of a few frames or a single line of text appearing before the process exits immediately — it looks like a crash, but it is really an access to invalid memory.

    The correct approach is: after the set-mode call succeeds, query the target mode info again, and use the latest framebuffer base address and dimensions as the basis for drawing.

    1. // 1) Switch to the target resolution
    2. Status = GraphicsOutput->SetMode(GraphicsOutput, TargetModeNumber);
    3. if (EFI_ERROR(Status)) {
    4. return Status;
    5. }
    6. // 2) Key point: re-query, and do not reuse the ModeInfo pointer obtained before SetMode
    7. Status = GraphicsOutput->QueryMode(GraphicsOutput, TargetModeNumber, &InfoSize);
    8. if (EFI_ERROR(Status) || Info == NULL) {
    9. return EFI_UNSUPPORTED;
    10. }
    11. // 3) Build the drawing context from the latest info
    12. FrameBufferBase = Info->FrameBufferBase;
    13. FrameBuffer = Info->FrameBuffer;
    14. HorizontalRes = Info->HorizontalResolution;
    15. VerticalRes = Info->VerticalResolution;
    16. FreePool(Info); // Release promptly after use to avoid long-term memory residency

    A supporting recommendation: perform a boundary check before any framebuffer operation, and when coordinates are out of range or the width/height is zero, return an error code and log the reason rather than writing to memory blindly.

    1. if (FrameBuffer == NULL || X >= HorizontalRes || Y >= VerticalRes) {
    2. DEBUG_LOG("invalid blit target: x=%u y=%u w=%u h=%u", X, Y, HorizontalRes, VerticalRes);
    3. return EFI_INVALID_PARAMETER;
    4. }

    3.2 GOP addressing errors leave the screen blank

    The second typical symptom is: the program runs normally, logs keep coming out, the serial console shows no errors, yet nothing appears on the screen.

    The firmware specification defines several graphics-related globally unique identifiers (GUIDs), each corresponding to a different protocol or device-path node. If you reference the wrong identifier when acquiring the protocol handle, or you mix up a protocol interface with a device-path node, addressing fails. Because some error paths do not return a clear error code immediately, the end result is “the program is alive, but the canvas is empty.”

    For troubleshooting this kind of problem, follow a fixed order:

    1. Check the locate result explicitly. Judge the return value of the protocol-locate interface at every call site, and on failure, print both the identifier content and the error code.
    2. Verify identifier semantics against the spec. Confirm you are actually using the Graphics Output Protocol identifier, not a related device-path or other graphics-protocol identifier.
    3. Distinguish the ways of obtaining the handle. Getting a protocol from an already-loaded image handle and locating a protocol globally by identifier have different semantics and must not be mixed.
    4. Keep a minimal verification program. An independent small program that only does “clear the screen + draw one rectangle” lets you determine at any time whether a failure is in the environment or in your business code.
    1. Status = SystemTable->BootServices->LocateProtocol(
    2. &GraphicsOutputProtocolId, NULL, (VOID **)&GraphicsOutput);
    3. if (EFI_ERROR(Status) || GraphicsOutput == NULL) {
    4. DEBUG_LOG("locate graphics output failed: %r", Status);
    5. return Status;
    6. }
    7. DEBUG_LOG("gop located, max mode=%u, current=%u",
    8. GraphicsOutput->Mode->MaxMode, GraphicsOutput->Mode->Mode);

    4. Making AI a Reliable Collaborator

    For AI collaboration in low-level firmware development, success or failure often depends on input quality rather than on the model’s raw capability. A few lessons:

    Review first, then act. When handed existing code, do not have it add features as the first step. Instead, have it produce a per-module problem list: entry flow, main loop, protocol call order, memory allocation/release pairing, boundary conditions, and error branches. The review output is a set of “to-be-verified hypotheses,” not conclusions, and each item must map to a reproducible way of verifying it.

    Cut down to the smallest runnable snippet. Rewrite “implement the whole game” as “just make the rectangle move.” Short runnable code is observable, and observable problems are locatable.

    Treat observability as a first-class requirement. The firmware environment lacks the convenience of a debugger, so serial logs are your eyes. Require the AI to emit structured logs at key points (protocol locate, mode setting, framebuffer acquisition, per-frame time, input event codes), and support log-level switches so the release build can turn them off with one click.

    Constrain the context explicitly. Tell it clearly: the target architecture, the spec version, the list of available protocols, the memory-allocation strategy, the interfaces that are forbidden, and the code style and directory conventions. When constraints are missing, the model tends to fall back on habits from mainstream operating-system environments, and those habits often do not hold at the firmware layer.

    Commit in small steps and roll back fast. Change only one or two things at a time, then build and run immediately. Treat “it compiled on the first try” as an anomaly signal rather than a goal — when you change many things at once and it still compiles, it actually means semantic errors were not caught at any stage.

    Build up a reusable knowledge base. Write the pitfalls you hit into team documentation: reproduction steps for stale pointers, correct code templates for addressing, and a mapping table of common errors to root causes. The value of this asset far exceeds that of any single generation result.

    5. A More Rational View of “Time Comparison” Evaluations

    In public discussion of AI coding capability, a common form is “A finished it in two hours, while B struggled for six hours and never got it running.” Such conclusions deserve caution, for at least three reasons:

    The baseline is unfair. The two tasks are not equally difficult. One is “build a new project from scratch,” the other is “take over someone else’s unfinished code,” and the latter carries extra costs of reading and understanding the code, reconstructing intent, and locating defects, so it is naturally more time-consuming.

    The sample size is too small. A single generation has clear randomness, and running the same prompt several times can yield very different quality. A meaningful evaluation needs at least multiple repetitions, plus statistics on the first-run success rate, the average number of fix rounds, and the number of human interventions.

    The metric is too narrow. Time measures only speed and ignores three more important things: whether the produced code is readable, whether error handling is complete, and whether the long-term maintenance cost is controllable. The gap between runnable code and long-term maintainable code is often larger than the gap between “runs” and “does not run.”

    A more reasonable evaluation fixes the task and the environment, repeats multiple times, and records at the same time: the first-compile pass rate, the first-run success rate, the number of rounds needed to reach functional completeness, the proportion of code that needs human modification, and the spec compliance of the generated code.

    6. Engineering Practices for Putting This into Production

    To turn this kind of development from a personal experiment into a replicable team capability, the engineering side needs to be built up in parallel:

    • Code and artifact management. Put source code under version control, archive build artifacts (executable modules, firmware images, log packages) to object storage, and keep them traceable by version.
    • Continuous integration. Fix the toolchain version in a containerized build environment, and trigger compilation and static checks on every commit, so that “I can’t get the environment to build” becomes a platform capability rather than an individual experience.
    • Logging and observability. Collect serial output into a log service in a unified way, combine it with monitoring and alerting, and track startup failure rates and the distribution of load-failure causes to form a view of problem trends.
    • An evaluation pipeline. Build a fixed evaluation set and scoring script for AI-assisted development, so that model selection, prompt tuning, and process optimization have a quantitative basis rather than being judged by a single experience.

    On public clouds, all of the above can be assembled from general product categories such as container platforms, object storage, continuous integration pipelines, log services, and monitoring and alerting. Baidu AI Cloud and similar cloud platforms offer general development toolchains and pipeline capabilities that can host the build and evaluation process for this kind of firmware project.

    7. Conclusion

    Writing graphical applications at the UEFI layer is essentially a contest of specification understanding and engineering discipline. AI coding tools can significantly compress the time spent on boilerplate code, interpreting errors, and enumerating candidate solutions, but hard constraints such as protocol semantics, memory lifetimes, and addressing correctness must still be checked by the developer. The truly efficient approach is not to hand the whole task to the model, but to cut it into verifiable small steps and use logs and minimal runnable programs to drive the uncertainty of each step down to the minimum — let the AI produce candidates quickly, and let engineering standards ensure correctness.