从内存开始图像

Starting image from memory

我在从内存加载和启动图像时遇到问题。 这是代码:

    UINT8 *Buffer;
    
    typedef struct
    {
        MEMMAP_DEVICE_PATH Node1;
        EFI_DEVICE_PATH_PROTOCOL End;
    } MEMORY_DEVICE_PATH;
    
    STATIC CONST MEMORY_DEVICE_PATH mMemoryDevicePathTemplate =
        {
            {
                {
                    HARDWARE_DEVICE_PATH,
                    HW_MEMMAP_DP,
                    {
                        (UINT8)(sizeof(MEMMAP_DEVICE_PATH)),
                        (UINT8)((sizeof(MEMMAP_DEVICE_PATH)) >> 8),
                    },
                }, // Header
                0, // StartingAddress (set at runtime)
                0  // EndingAddress   (set at runtime)
            },     // Node1
            {
                END_DEVICE_PATH_TYPE,
                END_ENTIRE_DEVICE_PATH_SUBTYPE,
                {sizeof(EFI_DEVICE_PATH_PROTOCOL), 0}} // End
    };
    
    MEMORY_DEVICE_PATH BufferDevicePath = mMemoryDevicePathTemplate;

    BufferDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
    BufferDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer + BufferSize;

    Prnt("Loading Image...");
    Status = gBS->LoadImage(
        TRUE,
        gImageHandle,
        (EFI_DEVICE_PATH *)&BufferDevicePath,
        Buffer,
        BufferSize,
        &ImageHandle);
    if (!EFI_ERROR(Status))
    {
            Status = gBS->StartImage (ImageHandle, NULL, NULL);
            if (EFI_ERROR(Status))
            {
                Prnt("Error Starting image ... Status=%08x", Status);
            }
    } 
    else {
          Prnt("Error Loading image ... Status=%08x", Status);
    }

使用 load 使用 UEFI Shell 加载图像(而不是从内存中)验证图像正常。 不过这种情况下没有报错,图像好像启动了但是机器卡住了,屏幕左上角出现黑屏白光标。

您忘记了 MEMMAP_DEVICE_PATH 结构中的 MemoryType 字段。

///
/// Memory Mapped Device Path.
///
typedef struct {
  EFI_DEVICE_PATH_PROTOCOL        Header;
  ///
  /// EFI_MEMORY_TYPE
  ///
  UINT32                          MemoryType;
  ///
  /// Starting Memory Address.
  ///
  EFI_PHYSICAL_ADDRESS            StartingAddress;
  ///
  /// Ending Memory Address.
  ///
  EFI_PHYSICAL_ADDRESS            EndingAddress;
} MEMMAP_DEVICE_PATH;

使用您的代码将其设置为 0 (EfiReservedMemoryType),将其设置为 1 (EfiLoaderCode)。

顺便说一句 ,也许那里的解决方案也能帮到你。