AI-Assisted Debugging of Legacy UEFI Code: Crashes and GOP Configuration
This case study describes how AI was used to review legacy UEFI graphics code, trace a startup crash through serial logs, and investigate a blank display. It also verifies the model’s initial conclusions against the UEFI specification and EDK2 implementation, showing where AI can accelerate debugging and where firmware-level assumptions must be independently validated.
1. Test Environment and Legacy Code
Debugging graphics programs in a UEFI environment is very different from application development. Firmware often lacks complete online documentation, debugging tools are limited, and programs typically report diagnostics through serial output before exiting immediately after an error.
This article documents the investigation of a nonfunctional UEFI graphics game. The goal was to use AI to take over and review legacy code, diagnose a crash, and investigate why graphics were not displayed. It also highlights parts of the AI’s initial analysis that required verification and provides reproducible coding patterns.
The experiment used the stable branch of EDK2, running under QEMU with OVMF firmware. Serial output was redirected to the terminal with:
-serial stdio
For reproducible testing, record the EDK2 tag, compiler toolchain, and QEMU version used. Header files and firmware implementations may differ between versions.
The project contained seven files covering the entry point, main loop, game logic, rendering, and input handling. The structure was clear, but the program did not run correctly after compilation.
The first step was a global code review. We provided the complete source code to a large language model and asked it to review the code and identify potential problems. The model examined dependencies and call relationships between modules and produced a checklist covering areas such as:
- Whether protocol return values were checked;
- Whether global variables were initialized before use;
- Whether dynamically allocated memory was released;
- Which header defined each GUID and whether the definition matched the standard;
- Whether critical execution paths contained serial logging points for locating failures.
We used two iterations based on this checklist. The first focused only on making the program compile, start, and reach the main loop. The second addressed performance and implementation details. This separation made it easier to isolate changes and verify one category of fixes at a time.
2. Diagnosing the Crash: From Serial Logs to Memory Management
After the initial changes, the program compiled and started successfully. Serial output showed:
Starting Game Loop
The program then exited. We added serial logging around the main loop, GOP initialization, and rendering entry point. The logs indicated that the failure occurred after a GOP-related call.
The AI initially concluded that the Info pointer returned by QueryMode became invalid after SetMode, resulting in a dangling pointer.
We verified this conclusion against the UEFI specification and the GraphicsOutputDxe implementation in EDK2. In that implementation, QueryMode allocates the mode information from a memory pool, and the caller is responsible for releasing it with FreePool. Based on this behavior, SetMode itself does not invalidate an already returned Info pointer. Therefore, “SetMode invalidates Info” should not be treated as a confirmed root cause. The exact behavior depends on the firmware implementation and version, so the relevant source code should be checked in the environment being used.
The more important questions are how the pointer is used:
- Is
Infoaccessed after it has been freed? - Is the old mode information still used after
SetMode? - Are return values checked?
A safer implementation is:
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop = ...; // Obtained through LocateProtocolEFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info = NULL;UINTN SizeOfInfo = 0;EFI_STATUS Status;Status = Gop->QueryMode(Gop, ModeNumber, &SizeOfInfo, &Info);if (EFI_ERROR(Status)) {DEBUG((DEBUG_ERROR, "QueryMode failed: %r\n", Status));return Status;}DEBUG((DEBUG_INFO, "Mode %u: %u x %u\n", ModeNumber,Info->HorizontalResolution, Info->VerticalResolution));FreePool(Info);Info = NULL;Status = Gop->SetMode(Gop, ModeNumber);if (EFI_ERROR(Status)) {DEBUG((DEBUG_ERROR, "SetMode failed: %r\n", Status));return Status;}// After SetMode succeeds, read the current framebuffer information from Gop->ModeFrameBuffer = (UINT32 *)(UINTN)Gop->Mode->FrameBufferBase;FrameBufferSize = Gop->Mode->FrameBufferSize;
In this version, Info is used only before it is released and is set to NULL afterward. Once the mode has been changed, the framebuffer address and size are read from Gop->Mode.
During debugging, print the resolution and PixelsPerScanLine from Gop->Mode->Info to confirm that the new mode is active.
After these changes, the program stopped crashing and entered the main loop. However, there is not enough evidence to conclude that stale-pointer access was the only cause. The change also added return-value checks and corrected memory-management behavior, so the effect cannot be attributed to a single modification.
3. No Graphics on Screen: Correct GOP GUID Usage
After the crash was resolved, the screen was still black. We reported this symptom to the model, which suspected that the GOP GUID was incorrect.
The standard Graphics Output Protocol GUID is defined in:
MdePkg/Include/Protocol/GraphicsOutput.h
The macro is named:
EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
EDK2 also provides the global variable:
gEfiGraphicsOutputProtocolGuid
Using this global variable avoids mistakes when manually copying GUID values.
The initial analysis claimed that an incorrect GUID might cause LocateProtocol to return an invalid pointer without reporting an error. This does not match the UEFI specification. When no matching protocol is found, LocateProtocol returns EFI_NOT_FOUND. For invalid parameters, it returns EFI_INVALID_PARAMETER. An incorrect GUID will generally result in an error status rather than a usable but invalid protocol pointer. The return value should therefore reveal the problem.
A recommended implementation is:
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop = NULL;EFI_STATUS Status;Status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid, NULL, (VOID **)&Gop);if (EFI_ERROR(Status)) {DEBUG((DEBUG_ERROR, "LocateProtocol(GOP) failed: %r\n", Status));return Status;}
If the log reports EFI_NOT_FOUND, check whether the program is running on the expected platform and firmware. For example, verify that QEMU is loading an OVMF build with graphics support and that the virtual machine has a display device configured.
A black screen can have several other causes and should be investigated step by step:
- Whether
SetModereturned successfully and the active mode has the expected resolution; - Whether the pixel format matches
Gop->Mode->Info->PixelFormat, since RGB and BGR formats may use different byte orders; - Whether drawing writes to the memory region pointed to by
FrameBufferBaserather than to a separate temporary buffer; - If the GOP
Bltfunction is used, whether the operation type and region parameters are correct.
The original analysis treated the GUID as the primary cause of the black screen, but this causal relationship cannot be confirmed from the change alone. Each condition should be verified independently, with logs retained at every step.
4. The Role and Limits of AI
In this case, AI was most useful for narrowing the investigation. It could quickly read all seven files, identify suspicious locations, recommend additional serial logging, and provide example changes. Completing these tasks manually would have taken more time.
However, the model’s root-cause analysis still required verification. In this case, the claim that Info became invalid was inconsistent with the relevant EDK2 implementation, while the claim that an incorrect GUID could cause a silent invalid-pointer condition was inconsistent with the specification. Accepting either conclusion without verification could lead to changes in the wrong direction.
To make AI-assisted debugging more reliable:
- Provide the relevant specification sections or header-file contents together with the code;
- Include complete serial logs and error codes rather than describing symptoms alone;
- Compile and run the program after each change, using logs to test the hypothesis;
- Treat the specification, implementation source, and actual runtime results as the final authority for root-cause analysis.
AI can provide a useful starting point for investigating low-level code, but it cannot replace reading the specification or debugging in the actual firmware environment.
