Large Language Models in Firmware Development: Practical Lessons and Pitfalls to Avoid

    This article shares a hands-on case study of using a large language model (LLM) to debug legacy firmware, where compilation alone does not guarantee a running program. It explains why bare-metal firmware is a stress test for AI coding, walks through a two-round workflow that takes code from non-functional to runnable and then to polished, and details two real runtime failures: a dangling pointer caused by an invalid call sequence after a mode switch, and a blank screen caused by mixing up protocol identifiers. It also summarizes three key thresholds for getting low-level graphics code to run—timing, identifier semantics, and observability—along with practical collaboration tips for developers working with AI.

    1. Why Firmware Development Stress-Tests Large Language Models

    When people evaluate an LLM’s coding ability, application-layer code is usually the first proving ground: wiring up APIs, form logic, and data processing. These tasks follow well-established patterns with a closed context, so models tend to perform reliably. Move the same model down to the firmware layer, however, and the difficulty rises exponentially.

    There are three reasons for this. First, the environment is unfamiliar. Firmware runs on bare metal, with no process isolation, memory protection, or standard library safety net from an operating system. A single invalid memory access can trigger an unrecoverable fault. Second, the feedback loop is long. Developers cannot rely on a familiar debugger to step through code; often the only option is to print the execution trace over a serial port and work backward to locate the problem. Third, specification coupling is deep. Firmware development depends heavily on established boot specifications and protocol definitions—pick the wrong identifier constant, and an entire graphics initialization chain can fail silently.

    For exactly these reasons, handing a model a piece of legacy firmware that “compiles but does not run” is an excellent test of its engineering ability. It requires the model not only to write code, but to read code, diagnose runtime faults, and form and verify hypotheses without visual debugging tools.

    2. Task Setup: Taking Over Structured but Non-Functional Legacy Code

    The subject of this exercise was a piece of graphical mini-game firmware generated by an AI coding tool. Its defining trait was that it “looked solid”: the code was split by responsibility into seven files, with clear boundaries for the entry point, main loop, game logic, rendering, and input handling—a reasonably modular design. In practice, however, the build environment was hard to set up, and the program produced no executable result.

    Throwing it away would have been a waste, so the task was structured as two rounds:

    • Round one: take the code from “not runnable” to “basically runnable.”
    • Round two: refine the experience and polish it on top of a working baseline.

    This relay-style repair reflects real engineering better than generating from scratch. Most maintenance of existing systems is not a rewrite; it is incremental repair within constraints left by someone else.

    3. Round One: From Static Review to Dynamic Diagnosis

    3.1 Review the Code First, Don’t Change It Right Away

    The first instruction after starting the model was not “make it run.” Instead, the model was asked to perform a systematic review of the existing implementation and output a list of likely problems. The value of this step is building a global picture: the model has to trace the call chain top-down and map the dependencies between entry initialization, mode setup, frame buffer acquisition, and the event loop.

    The resulting problem list usually clusters into a few categories: unchecked return values, pointers with mismatched lifetimes, incorrect protocol identifier references, and missing timeout and failure branches. This list gives subsequent fixes a priority order and avoids the blind loop of “fix whatever throws an error.”

    3.2 Problem One: A Dangling Pointer After Setting the Mode Causes a Crash

    On the first run, the program printed a startup banner and then exited immediately—a classic symptom of a crash during initialization.

    After adding serial debug output, the trace showed the fault occurred right after a graphics output protocol call. Further analysis revealed that the display information pointer obtained when querying the mode was no longer valid after the new mode was set: switching modes reconfigures the display device, and the memory describing the original frame buffer is released or rewritten. Continuing to hold that pointer means accessing a dangling pointer.

    The fix was to adjust the order of operations: re-query the display information after the mode is set successfully, so the pointer references the currently valid frame buffer description. The logic looks like this:

    1. // Wrong order: query first, then switch mode; the pointer becomes invalid
    2. ModeInfo = GetModeInfo(gop, mode);
    3. Status = SetMode(gop, mode);
    4. UseFrameBuffer(ModeInfo); // dangling pointer access
    5. // Corrected order: re-query after the mode is set successfully
    6. Status = SetMode(gop, mode);
    7. if (IsError(Status)) return Status;
    8. ModeInfo = GetModeInfo(gop, mode); // the description is valid only now
    9. UseFrameBuffer(ModeInfo);

    The general lesson here: in any system where resource state can change, every “handle-style return value” must be confirmed to belong to a state that has not been replaced before it is used.

    3.3 Problem Two: An Incorrect Protocol Identifier Leaves the Screen Blank

    Once the crash was resolved, the program entered its loop but produced no output on screen. Based on the runtime log, the model traced the problem to the identifier configuration of the graphics output protocol: the boot specification defines several similar protocol identifiers—some for loading an already-installed protocol, others for creating a new protocol instance. Mixing them up means no valid graphics output interface is obtained.

    The fix was to standardize on the identifier for loading an installed protocol, then verify after acquisition that the interface pointer is non-null and the mode count is greater than zero before entering the rendering flow. This class of problem is just as common in manual development, and its stealth comes from being completely invisible at compile time—it surfaces only at runtime.

    3.4 Round One Summary: Three Thresholds to “Runnable”

    Abstracted from the process above, a low-level graphics program typically has to clear three thresholds to go from “not runnable” to “runnable”:

    1. The timing threshold: pointer and handle validity before and after a resource state change.
    2. The identifier threshold: distinguishing the semantics of similar constants in a specification.
    3. The observability threshold: whether there is runtime trace output at the serial-port or log level.

    The third is the most easily overlooked, yet it determines repair efficiency. Without observability, both the model and the developer can only guess; once logging is in place, diagnosis time often drops by an order of magnitude.

    4. Round Two: From Running to Usable

    After the code runs, the refinement stage usually centers on four areas:

    • Stability: add return-value checks and fallback paths for every external call to avoid abnormal interruptions.
    • Input responsiveness: eliminate busy-waiting in event polling to reduce input latency and jitter.
    • Rendering efficiency: reduce full-screen redraws in favor of partial refreshes to ease flickering at low resolutions.
    • Maintainability: extract magic numbers into named constants, and add comments and assertions to critical modules.

    5. Collaboration Tips for Developers

    1. Make the model read before it writes. Asking for a problem list and a repair plan before touching the code significantly reduces the cost of trial and error.
    2. Put observability first. Add logging and assertions before discussing feature implementation.
    3. Verify in small steps. Change one variable at a time and run immediately after each change; changing several things at once makes attribution difficult.
    4. Define the constraint boundaries. State the target specification version, the available API surface, and prohibited actions in the prompt to reduce deviations from the model improvising.
    5. Keep humans in charge of key decisions. High-risk changes involving memory lifetimes or mode switching must be reviewed by the developer.

    6. Conclusion

    This exercise shows that the value of large language models in low-level development is not “generating perfect code in one shot,” but compressing the review–hypothesize–verify loop into a shorter cycle. Without visual debugging, the model can use logs to quickly narrow the suspicious area and propose fixes that match the specification’s semantics. For developers, the real capability upgrade is designing the task breakdown, the constraint description, and the verification approach well—get those three solid, and AI can become a reliable link in the engineering chain.