LLM-Assisted Code Review and Repair in UEFI Low-Level Programming

    This article examines how a large language model can review and repair leftover UEFI game code without a full rewrite. It covers a static review pass, serial-log-driven debugging, a wild-pointer defect caused by QueryMode/SetMode ordering, and a GOP GUID configuration error that produced a black screen, then summarizes practical lessons for AI-assisted firmware development.

    As large language model technology continues to evolve, AI-assisted programming has expanded from basic script writing to more complex system-level development. In traditional application-layer development, developers have rich standard libraries and mature debugging tools, so AI-generated code often succeeds at a relatively high rate. However, when the development environment moves to UEFI (Unified Extensible Firmware Interface) or BIOS-level system environments, AI code generation and repair face serious challenges because there is no standard C library support, debugging options are limited, and development depends heavily on firmware protocol specifications. This article uses a piece of leftover UEFI game code that failed to run as a starting point to discuss how to use advanced large model technology for code review, defect localization, and repair, demonstrating the practical value of AI in system-level programming.

    1. Legacy Code Background and Test Objectives

    The source code in this practice was a half-finished UEFI Tetris implementation produced by another code generation tool. The code was somewhat structurally standard: it contained seven files and used a modular design covering the entry function, main loop, game logic, rendering module, and input handling. However, because of low-level interface call errors and build environment configuration issues, it ultimately failed to produce an executable program.

    Discarding this code directly would clearly waste resources. Therefore, the test objective was set as follows: have a current mainstream intelligent programming large model take over this problematic codebase and, without rewriting all the logic, use code review and targeted repair to make it compile and run properly in a real UEFI environment. The test was divided into two phases: the first phase focused on getting the program to run at a basic level and eliminating fatal crashes; the second phase focused on code refinement and performance optimization.

    2. AI-Driven Static Code Review

    In the first step, we did not rush to compile. Instead, we fed the complete GopApp source code unchanged into the large model and gave a clear instruction: “Review this UEFI code and identify potential issues that may cause build failure or runtime crashes.”

    The large model quickly entered the analysis state. Based on its understanding of the UEFI specification, it checked the call relationships among the modules one by one:

    1. Entry point and lifecycle: Checked the return value handling of the efi_main entry function and the initialization of EFI_SYSTEM_TABLE.
    2. Main loop mechanism: Analyzed whether the event waiting and key polling mechanism conformed to the UEFI single-threaded, non-preemptive model.
    3. Graphics rendering logic: Reviewed how GOP (Graphics Output Protocol) was obtained and how the framebuffer was written.
    4. Memory management: Checked whether memory allocation and release under Boot Services occurred in pairs and whether memory leaks existed.

    A few minutes later, the large model produced a detailed issue list and optimistically stated that the program should run after these issues were fixed. However, real system-level development is rarely that smooth, and deeper low-level defects soon emerged during practice.

    3. Core Defect Localization and Repair Practice

    Defect 1: Invalid Memory Pointer Causes Program Crash

    After modifying the code according to the AI’s initial suggestions, the program did compile and start running. The screen successfully output the log “Starting Game Loop”, but the program exited immediately afterward without displaying any graphical interface. This indicated that the program encountered a fatal error during the initialization phase.

    To pinpoint the problem, we instructed the AI to add serial debug logs along critical paths and monitor program execution through the serial console. The log feedback showed that the program encountered an exception after calling the GOP protocol to set the screen mode.

    Based on this dynamic feedback, the large model quickly identified the root cause. In UEFI development, obtaining graphics output protocol information usually uses the QueryMode interface, which returns a pointer to EFI_GRAPHICS_OUTPUT_MODE_INFORMATION. However, after calling QueryMode, the code immediately called SetMode to set a new screen resolution. The large model pointed out that SetMode reallocates the underlying video memory and mode information structure, which invalidates the memory region pointed to by the info pointer returned by the earlier QueryMode call, turning it into a wild pointer. If subsequent code continued to access that pointer, it would trigger an out-of-bounds memory access and cause the firmware-level program to terminate directly.

    Fix: After calling SetMode to set the target mode, call QueryMode again to obtain the latest framebuffer information and physical address. The revised code is shown below:

    1. // 1. Set the target graphics mode
    2. Status = Gop->SetMode(Gop, TargetMode);
    3. if (EFI_ERROR(Status)) {
    4. return Status;
    5. }
    6. // 2. Re-query mode information after SetMode to avoid a wild pointer
    7. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
    8. UINTN SizeOfInfo;
    9. Status = Gop->QueryMode(Gop, TargetMode, &SizeOfInfo, &Info);
    10. if (EFI_ERROR(Status)) {
    11. return Status;
    12. }
    13. // 3. Safely use the framebuffer base address for subsequent rendering
    14. UINT8 *FrameBufferBase = (UINT8 *)Gop->Mode->FrameBufferBase;

    After this modification, the program no longer crashed and successfully entered the game main loop.

    Defect 2: Incorrect Graphics Protocol GUID Configuration Causes Black Screen

    Although the program no longer crashed, the screen remained completely black and no game graphics were rendered. We reported this behavior to the large model again and asked it to analyze why the graphical interface could not be displayed.

    The large model performed an in-depth analysis based on the underlying source code logic of EDK2 (EFI Development Kit II) and found that the problem lay in the GOP protocol GUID configuration. In the UEFI specification and EDK2 implementation, there are multiple GUID definitions related to graphics output, such as gEfiGraphicsOutputProtocolGuid and other vendor-specific or legacy-standard GUIDs. If the code references an incorrect GUID, the LocateProtocol function cannot find the correct graphics output protocol instance in the system handle database. As a result, the obtained Gop pointer is null, and subsequent rendering operations cannot work.

    Fix: Standardize the GUID reference and ensure that the EDK2-standard gEfiGraphicsOutputProtocolGuid is used. The revised code is shown below:

    1. EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
    2. EFI_STATUS Status;
    3. // Ensure the correct GOP GUID is used for protocol location
    4. Status = gBS->LocateProtocol(
    5. &gEfiGraphicsOutputProtocolGuid,
    6. NULL,
    7. (VOID **)&Gop
    8. );
    9. if (EFI_ERROR(Status)) {
    10. // Output an error log to the serial console for troubleshooting
    11. Print(L"Error: Failed to locate GOP protocol. Status: %r\n", Status);
    12. return Status;
    13. }

    After correcting the GUID, the code was recompiled and run, and the game graphics finally appeared successfully in the UEFI Shell environment. The basic Tetris logic ran smoothly.

    4. Conclusion and Takeaways

    This practice shows that modern large language models can not only write business code at the application layer, but also demonstrate strong code review and defect repair capabilities in low-level system-level development such as UEFI. By combining static code analysis with dynamic feedback based on serial logs, AI can accurately locate complex system-level bugs such as wild pointers and protocol configuration errors.

    For enterprise R&D teams facing large amounts of legacy system-level firmware code, AI programming tools can be introduced to assist with refactoring and defect investigation. In this process, combining general cloud services, such as object storage for archiving large historical code snapshots or logging and monitoring services for collecting low-level runtime logs, can further improve the efficiency and safety of AI-assisted development. In the future, as models develop a deeper understanding of low-level hardware specifications, AI will play an even more central role in system-level software engineering.