AI-Assisted Development of Pre-Boot Firmware Graphics Programs: A Practical Workflow

    AI coding tools can quickly produce well-structured firmware graphics projects, yet such code often crashes on the first main-loop iteration or keeps running while the screen stays blank in a real pre-boot environment. This article presents a repeatable workflow for taking over a half-finished firmware graphics program and driving it to a runnable state. It explains why pre-boot graphics differ from ordinary applications, why you should review legacy code before rewriting it, and how to fix the two most common first-round failures—exiting right after mode setup and a live process with no visible output. It then shows how to refine rendering, timing, input, state handling, and resource cleanup on a stable baseline, and outlines a fair way to evaluate AI assistance along with reusable collaboration instructions and a delivery checklist.

    The real problem isn’t whether the code looks right, but whether it survives in a live environment

    Pre-boot graphics programs are fundamentally different from ordinary applications. There is no full operating system, memory lifetimes are managed by firmware protocols, switching display modes invalidates the previous frame buffer descriptors, and picking the wrong protocol identifier breaks the entire graphics chain. AI coding tools are good at generating a project with clean module boundaries, consistent naming, and complete comments—but this kind of “looks ready to ship” code often exits the first time it enters the main loop, or keeps running while the screen stays blank.

    So the standard for judging assisted coding should not stop at file counts or how nice the architecture diagram looks. It should be broken into two verifiable outcomes: in the first round, can the program stably reach the game or render loop in the target firmware environment; in the second round, can it fill in input, timing, boundaries, and resource cleanup without breaking the boot path. This article abstracts a real “take over a half-finished project and fix it to a runnable state” process into a repeatable engineering method.

    When you inherit legacy code, review it first instead of rewriting immediately

    Drop the existing source into a clean build environment as-is. The first instruction should be to review, not to rewrite. The review should cover at least five categories of modules:

    • Entry and protocol location: how the program locates the graphics output capability, and whether the identifier matches the current firmware implementation.
    • Main loop: whether the exit condition is too broad, and whether the error path terminates the process directly.
    • Game or business state: whether the initialization order depends on display resources that aren’t ready yet.
    • Rendering: whether it caches frame buffer pointers, pitch, or resolution that may become invalid.
    • Input: whether key events block, and whether polling happens before the graphics mode is established.

    The output of the review should be a list of problems, not a vague “this could be optimized.” Each item should state the trigger condition, the expected symptom, and a minimal verification method. Only then can the fixes be closed out one by one, avoiding a single large rewrite after which you can’t tell which change actually made the difference.

    First round goal: get the program to stop at an observable state

    Symptom 1: exits right after printing startup messages

    A common pattern: the log prints “entering the main loop,” and then the process ends. This usually means the crash happens right after the graphics mode query or set, not in the business logic itself. Don’t keep stacking on features at this point. First, instrument the serial port (or an equivalent debug channel) with the key milestones: locate the protocol, query the mode, set the mode, re-read the frame info, clear the screen for the first time, and enter the loop.

    One frequent root cause: the information pointer returned when you query the display mode points to a descriptor block that the protocol implementation provides only temporarily. After you call set mode, that memory is no longer valid. If you keep using the old pointer to read the width, height, and frame buffer base address, it becomes a wild-pointer access and the process exits. The correct order is not “query once, switch mode, and keep using,” but:

    1. Query the current or target mode, used only to choose the resolution;
    2. Set the mode;
    3. Query again to obtain frame buffer information that is still valid after the mode is set;
    4. Copy the base address, resolution, bytes per pixel, and pitch into your own structure;
    5. From then on, rendering uses only this local copy.

    The logic looks like this; it only expresses the order and isn’t bound to specific interface names:

    1. ModeInfo *info = NULL;
    2. int mode = pick_mode();
    3. if (query_mode(mode, &info) != OK) return ERR;
    4. if (set_mode(mode) != OK) return ERR;
    5. if (query_mode(mode, &info) != OK) return ERR; /* re-query is required */
    6. fb.base = info->frame_base;
    7. fb.width = info->width;
    8. fb.height = info->height;
    9. fb.pitch = info->pixels_per_scanline;

    After this fix, acceptance checks just one thing: the process no longer exits after the mode is set, and the serial port can continuously print the loop heartbeat. The screen can stay black for now, but the control flow must remain alive.

    Symptom 2: the process is alive but nothing appears on screen

    A live process only means it hasn’t hit illegal memory immediately. If the graphics UI still doesn’t appear, the next thing to check is the protocol identifier. The same class of graphics capability often has several similar identifiers in a firmware implementation: some are used to locate the current output, others for legacy compatibility or a specific device path. When the identifier is wrong, the call may return “not found,” or it may hand back a handle that can’t actually draw. The result is a main loop that spins while the frame buffer is never correctly mapped.

    The guiding principle: use the graphics output identifier from the current firmware header or protocol documentation as the source of truth, and delete guessed constants. Once you’ve located it, print whether the handle is null, whether the query succeeded, and whether the mode count is greater than zero. Don’t let “it compiles” stand in for “it binds to a real output device.”

    Second round goal: refine on top of a stable startup

    Do experience polish only after the first round is done; otherwise every animation or input change gets tangled up with startup crashes. For the second round, move from low risk to high risk:

    • Rendering: full-screen clear, rectangle fill for blocks or bricks, and out-of-bounds clipping. Pitch must use the scanline pixel count—don’t assume it equals the width.
    • Timing: use the timing capability provided by the firmware to do fixed-step updates, so the loop speed doesn’t vary with the machine.
    • Input: non-blocking reads that distinguish a key press from key repeat; the exit key must be reachable from any state.
    • State machine: keep ready, running, paused, and ended states separate, and avoid implicitly resetting the game inside the render function.
    • Resources: mode switch, exit, and restart should all go through the same “re-acquire the frame info” path; keeping old pointers is forbidden.

    Keep the last working baseline for every change. If a new version crashes again, you should be able to fall back to the baseline immediately and re-add just one change.

    How to fairly judge “good structure” versus “actually runs”

    Modular design, seven files, separation of responsibilities—all of that shows the generated result is readable, but it can’t replace a running result. A half-finished project is worth keeping precisely because its structure already provides repair entry points: entry, loop, logic, rendering, and input can each be instrumented separately. When comparing different assistance methods fairly, fix the same task, the same firmware environment, the same build method, and the same acceptance script, and record:

    • whether the first build compiles;
    • whether the first run reaches the main loop;
    • how many manual round trips it took to locate the key defect;
    • whether the final result meets a minimal playable or demonstrable standard.

    With different tasks and different environments, you can’t directly compare time spent as a measure of capability. The more valuable conclusion is this: assistance tools are good at quickly laying out modules, while humans still have to supply the protocol constraints, the judgment about invalid memory, and the layered acceptance.

    Reusable collaboration instructions

    When working with an AI coding tool, keep instructions short, verifiable, and focused on closing one fault at a time. A recommended order: first review and list reproducible problems; then fix only the exit path and add debug output; once the heartbeat is stable, separately fix the protocol identifier and the frame buffer re-query; and only then do gameplay and rendering polish. For every round, ask the tool to explain “which points were changed, how to roll back, and which symptom tells you it succeeded.”

    For a development team, the delivery checklist for this kind of pre-boot graphics program can be standardized: the source of the protocol identifier, the re-fetch of information after the mode is set, the local frame descriptor copy, the serial heartbeat, the minimal rendering, the exit path, and the regression record. With these items in place, code that merely looks well-structured finally becomes a program that actually runs.