Using AI Coding Tools to Develop UEFI Firmware Applications: Practices and Pitfalls

    This article presents a hands-on experiment in using AI coding tools to develop a UEFI Shell application with GOP-based graphics and a Chinese-language interface. It explains how to make constraints explicit, split work into verifiable milestones, avoid common failures such as directory drift and unusable validation environments, and evaluate model behavior across practical dimensions. The article also provides a reusable checklist for firmware-level development with low feedback and strong specification constraints.

    Most discussions about AI coding tools focus on web and business backend development. These scenarios benefit from mature frameworks, abundant example code, and immediate feedback. Firmware development is different. Firmware applications run before the operating system, have no system calls, no standard library, and no ready-made graphics stack. The available resources are mainly the protocols and device handles exposed by the firmware specification.

    Giving this kind of task to an AI coding tool is therefore a test of engineering reliability under three difficult conditions: low corpus density, strong specification constraints, and a long feedback loop.

    The goal of this practice was deliberately narrowed: run a snake game without audio in the UEFI Shell, draw graphics by directly accessing the framebuffer through the firmware-provided Graphics Output Protocol (GOP), and use Chinese for all interface text.

    This topic was chosen for three reasons:

    1. The logic is simple, making it easier to determine whether a failure comes from model capability or environment setup.
    2. It covers four basic capabilities: input, rendering, timing, and state management.
    3. Chinese rendering exposes hidden assumptions related to encoding and fonts.

    Making Constraints Explicit

    The first major source of failure in low-level development is not incorrect code, but unclear constraints.

    The first step was to create a project context file in an empty directory. The file explicitly defined the following constraints in Chinese:

    1. # Project Constraints
    2. - Goal: A graphical snake application running in the UEFI Shell, with no audio
    3. - Runtime: Firmware command line; graphics output must use only the GOP protocol
    4. - Build: Use an EDK-style project structure from an open-source firmware development kit
    5. - Directory: All source code, build artifacts, and toolchains must remain inside the current working directory
    6. - Validation: Boot and verify in an open-source instruction set emulator with an open-source firmware image
    7. - UI: All menus, status bars, and prompts must be in Chinese

    The purpose of this file was to convert the default knowledge of an experienced firmware engineer into machine-readable hard constraints.

    Experience shows that any constraint not written into the context will be filled in by the tool using the most mainstream approach found on the internet. In firmware scenarios, that mainstream approach is often wrong.

    Three Iterations: Breaking a Large Task into Verifiable Steps

    The development work was divided into three rounds with more than twenty prompts in total. The whole process took about three hours.

    The first round produced a minimal runnable version:

    • Obtain the GOP handle.
    • Set the graphics mode.
    • Map the framebuffer.
    • Draw a monochrome block.
    • Move the snake with the arrow keys.
    • Detect collisions.
    • Show an end prompt.

    The second round added:

    • Double-buffered rendering.
    • Increasing difficulty levels.
    • Obstacles.
    • A particle effect when food is eaten.

    The third round completed:

    • Chinese localization.
    • Pause functionality.

    The value of this split was that each round had clear acceptance criteria and failures could be localized.

    If the first round produced a black screen, the problem was almost certainly related to the protocol handle or pixel format. If the second round showed tearing, the problem was likely in the buffer-switching timing. If the third round produced garbled text, the problem was likely in source encoding or glyph sources.

    Mixing these three types of problems into one round would multiply debugging costs.

    Pitfall 1: Permission Boundary Failure and Directory Drift

    After the first instruction was issued, the tool began downloading and configuring a firmware development kit. However, the project was created in the parent directory of the working directory. During compilation, the tool then generated multiple directories, such as tools, build, output, and configuration, in that parent directory.

    Writing outside the specified working directory is a common problem for this kind of tool when operating with high privileges. The consequences are not limited to messy directories:

    • Build scripts may reference incorrect paths.
    • Build artifacts may become separated from source code.
    • Cleanup becomes difficult.
    • Shared environments may be polluted with files belonging to other users.

    There are three practical ways to reduce this risk.

    The first layer is least privilege. High-risk options that skip confirmation should be disabled by default. Writing files, executing commands, and accessing the network should require confirmation.

    The second layer is environment isolation. Provide the working directory inside a container or an independent volume, and make it the only writable location outside of read-only mounts.

    The third layer is explicit validation. Add checkable acceptance conditions to the prompt:

    1. Before each write or command execution, first output pwd and git status.
    2. If the target path is not within ./, stop and report the issue. Do not continue.

    This kind of “self-check before action” instruction significantly reduces the probability of unauthorized operations because it turns path correctness into a question the model must explicitly answer rather than an implicit assumption.

    Pitfall 2: Distorted Validation Environment

    The second typical problem is an unusable validation environment.

    To save compilation time, the tool downloaded a prebuilt emulator and firmware image. However, the resulting firmware image had no firmware command-line entry, and the boot media did not contain the application to be validated. The result was “compilation succeeded, but the application cannot run.”

    This type of failure is especially dangerous because it does not produce a clear error. It only appears as a stalled workflow. The model may then repeatedly adjust unrelated code.

    The correct approach is to define the validation environment as part of the deliverable:

    • The firmware image must include a command-line shell.
    • The boot media must be generated as a bootable image according to the specification, with the application placed in the boot path.
    • A minimal validation script should define how to verify the process: boot, enter the command line, load the application, and exit.

    Writing clear acceptance commands into the prompt is much more effective than repeatedly saying “please run it in the emulator.”

    Other Common Hidden Pitfalls

    Chinese Rendering

    The firmware environment has no system fonts. It is necessary to explicitly embed bitmap glyphs or provide glyph data. UTF-8 decoding and wide-character width calculation must also be handled. Otherwise, garbled text or misalignment will occur.

    Input Handling

    The implementation must distinguish between extended scan codes and character codes. Arrow keys are extended codes and cannot be parsed as ordinary characters. Key debouncing and repeat-rate control are also required.

    Pixel Format and Resolution

    GOP supports multiple pixel formats and resolutions. The row stride must be calculated from the actual mode parameters. Assuming a fixed width when addressing pixels will cause diagonal streaks.

    Timing

    Firmware environments usually do not provide high-precision sleep interfaces. Frame-rate control must rely on timing protocols or busy waiting. Long blocking periods should be avoided so that the interface remains responsive.

    Evaluate Dimensions, Not Just Conclusions

    Differences between models on the same task should be recorded across five dimensions rather than reduced to a vague ranking:

    1. Constraint adherence: whether the tool exceeds permissions or deviates from the specified directory.
    2. Specification understanding: whether protocol fields and call sequences are correct.
    3. Self-correction rate: whether the tool can independently locate the cause after an error.
    4. Validation-loop capability: whether the tool proactively builds a runnable environment.
    5. Long-task consistency: whether later iterations introduce regressions.

    These dimensions can be reused for other low-level development topics and can help teams build their own evaluation sets.

    Reusable Practice Checklist

    1. Write the constraint file before writing code. Constraints must be machine-checkable.
    2. Apply least privilege. Make the working directory the only writable location, and use container isolation when necessary.
    3. Include the validation environment in the acceptance criteria. Do not allow “compile only, never run.”
    4. Split large tasks into verifiable milestones. Introduce only one new type of risk per round.
    5. Provide explicit specification fragments for low-level details such as pixel formats, scan codes, and encoding instead of vague natural-language hints.
    6. Keep a runnable version after each round for regression comparison.

    Conclusion

    AI coding tools are not incapable of handling low-feedback scenarios such as firmware development. However, they need to be managed like a new team member with high privileges, high confidence, and limited domain common sense.

    Use constraints instead of implicit understanding. Use executable acceptance checks instead of verbal descriptions. Use isolation instead of blind trust.

    With these three practices, completing a firmware-level graphical application in three hours is achievable. Without them, time will be consumed by cleaning up directories and debugging the “compilation succeeded but startup failed” problem.