UEFI Graphics Program Development with AI Assistance: Code Review and Debugging Practice
This article walks through an iterative UEFI graphics program project. It shows how AI-assisted code review helped locate two bugs: a stale pointer after a video mode change, and an incorrect protocol identifier. It also covers serial logging, a repeatable validation process, and the limits of AI in firmware work.
1. Background and Goals
Developing graphics programs in a bare-metal environment (the firmware stage) differs greatly from ordinary application development. The program runs during the UEFI (Unified Extensible Firmware Interface) boot services phase. No operating system provides memory protection, exception handling, or logging. Graphics output depends on the Graphics Output Protocol (GOP) provided by the firmware, and developers must manage the frame buffer directly. A single wrong pointer or identifier can cause the program to exit with no clear error message.
This article follows one real iterative process and divides the work into two rounds. The first round gets the program running at a basic level. The second round refines it. Each round centers on a reproducible problem and explains the reasoning behind the diagnosis and the fix.
2. Code Review: Building a Checklist First
The results of AI-assisted code review depend on how the question is asked. A vague request to “find the problems” usually returns only style suggestions. A more effective approach is to give specific review dimensions, such as:
- Pointer lifetime: Where the memory a pointer refers to was allocated by the firmware, and when it becomes invalid.
- Return value handling: Whether every
EFI_STATUSis checked and whether the error paths are complete. - Protocol lookup: Whether the GUID in use matches the specification and whether the lookup method is correct.
- Resource release: Whether memory allocated by the firmware is freed through the proper interface after use.
- Module boundaries: Whether rendering, input, and game logic depend on implicit global state.
When the code is reviewed against these dimensions, the AI produces a more specific list of issues that is easier to verify one by one. Each conclusion in the list must still be confirmed through code inspection or runtime testing. None of them should be accepted as fact without checking.
3. Issue One: The Program Exits Right After Startup
3.1 Symptom
The program compiles and starts. It prints a message indicating that the loop has begun, then returns immediately. Without debug output, this kind of failure is hard to diagnose.
3.2 Locating the Problem
Printing progress information to a serial port at key points is the most direct technique in firmware development. With segmented logging in place, it becomes clear that the program fails after a graphics protocol call. A closer check shows that the mode information structure returned by QueryMode is allocated by the firmware. It must be freed after use, and its contents are tied to the current mode. After SetMode switches the mode, any previously cached pointers and frame buffer information may no longer be valid.
3.3 Fix
The correct approach is to read the information again from the protocol’s current mode fields after the mode change:
EFI_STATUSInitVideo(IN EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop,IN UINT32 ModeNumber){EFI_STATUS Status;Status = Gop->SetMode(Gop, ModeNumber);if (EFI_ERROR(Status)) {return Status;}// After the mode change, read the current mode parameters again.// Do not reuse pointers cached before the switch.UINT32 Width = Gop->Mode->Info->HorizontalResolution;UINT32 Height = Gop->Mode->Info->VerticalResolution;UINT32 *FrameBuffer = (UINT32 *)(UINTN)Gop->Mode->FrameBufferBase;// Later drawing code uses only the values fetched here.return EFI_SUCCESS;}
After this fix, the program no longer exits during graphics initialization. Repeated boot tests are needed to confirm this result. A single successful run does not prove the problem is solved.
3.4 Lessons
- Information returned by
QueryModemust be freed after use. Keeping it beyond the necessary scope is a common mistake. - Changing the mode alters frame buffer parameters. Data from before and after the switch should be treated as different states.
- Debugging at the firmware stage depends on serial output. Keep logs at every initialization step so that failures can be narrowed down quickly.
4. Issue Two: The Graphical Interface Does Not Display
4.1 Symptom
The program runs, but the screen shows no graphics output. This usually means the program is still executing but has not obtained the correct Graphics Output Protocol.
4.2 Root Cause Analysis
UEFI identifies protocols by GUID. Firmware header files may define several graphics-related GUIDs, such as identifiers for different versions or different purposes. If the lookup uses the wrong GUID, LocateProtocol returns an error. If the caller does not check the return value, the following code runs with a null or uninitialized pointer.
4.3 Correct Usage
The Graphics Output Protocol should be looked up using the macro defined in the specification, and the returned status should be checked:
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop = NULL;EFI_STATUS Status;Status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid,NULL,(VOID **)&Gop);if (EFI_ERROR(Status)) {// Print the error code to the serial port to tell whether the protocol// was not found or a parameter was wrong.return Status;}
During troubleshooting, check that the GUID identifier used in the source matches the Graphics Output Protocol in the specification. Also confirm that the header include paths do not pull in a same-named definition that serves a different purpose.
4.4 Lessons
- Check the return value of every protocol lookup, and print the error code when a lookup fails.
- Verify similar or identical identifiers against the header files and the specification. Do not rely on the name alone.
- Graphics problems often appear as “nothing is drawn,” which is different from a crash. Diagnose the two separately.
5. Validation and Regression Testing
After each fix, use a consistent validation procedure so that a repair in one place does not introduce a problem somewhere else:
- Test a cold boot and a restart to confirm that initialization is stable.
- Check the serial log to confirm that every initialization step produces output and returns success.
- Confirm that input handling, timed refresh, and drawing logic keep running inside the loop.
- Test on different resolutions or different hardware, because the frame buffer format and stride may vary.
Record the validation process, including the environment, the steps, and the results. When the code changes later, these records make it quicker to tell whether a new problem is related to an earlier fix.
6. The Boundaries of AI Collaboration
AI tools are clearly useful for code review, log analysis, and summarizing problems. They also have limits to keep in mind:
- An AI-suggested fix may not match a specific firmware version. Check it against the specification and the actual header files.
- For hardware-dependent behavior, the final conclusion should be based on results from real machines.
- When the AI’s judgment conflicts with logs or the specification, trust the observable evidence first.
- For an unfamiliar interface, asking the AI to explain its reasoning is more reliable than accepting the code as written.
A safe way to work is to treat the AI as a fast first-pass reviewer and rely on human verification for the final decision.
7. Summary
Graphics program development at the firmware stage centers on three types of problems: pointer lifetime, protocol lookup, and state synchronization. The two issues in this article map to two of these categories: a pointer that becomes invalid after a mode change, and a wrong protocol identifier. Both failed silently. They could only be located through segmented logging and comparison with the specification.
A checklist, retained initialization logs, and a consistent validation process can significantly reduce the cost of finding these problems. On this foundation, AI tools can speed up review and analysis, but developers still need to complete the verification step themselves.
