[lib][uefi] Migrate to boot memory protocol

Image loading protocol is deprecated, migrate
This commit is contained in:
Kelvin Zhang
2025-09-18 14:28:39 -07:00
committed by Kelvin Zhang
parent 848fbfefe7
commit a1be045514
7 changed files with 86 additions and 59 deletions

View File

@@ -89,6 +89,7 @@ __WEAK EfiStatus open_block_device(EfiHandle handle, void** intf) {
memset(interface, 0, sizeof(EfiBlockIoInterface));
auto dev = bio_open(reinterpret_cast<const char *>(handle));
interface->dev = dev;
interface->protocol.revision = EFI_BLOCK_IO_PROTOCOL_REVISION;
interface->protocol.reset = reset;
interface->protocol.read_blocks = read_blocks;
interface->protocol.write_blocks = write_blocks;

View File

@@ -228,16 +228,7 @@ EfiStatus open_protocol(EfiHandle handle, const EfiGuid *protocol, void **intf,
"%s(EFI_GBL_EFI_IMAGE_LOADING_PROTOCOL_GUID, handle=%p, "
"agent_handle%p, controller_handle=%p, attr=0x%x)\n",
__FUNCTION__, handle, agent_handle, controller_handle, attr);
GblEfiImageLoadingProtocol *image_loading = nullptr;
allocate_pool(EFI_MEMORY_TYPE_BOOT_SERVICES_DATA, sizeof(*image_loading),
reinterpret_cast<void **>(&image_loading));
if (image_loading == nullptr) {
return EFI_STATUS_OUT_OF_RESOURCES;
}
image_loading->revision = GBL_EFI_IMAGE_LOADING_PROTOCOL_REVISION;
image_loading->get_buffer = get_buffer;
*intf = reinterpret_cast<void *>(image_loading);
return EFI_STATUS_SUCCESS;
return EFI_STATUS_UNSUPPORTED;
} else if (guid_eq(protocol, EFI_TIMESTAMP_PROTOCOL_GUID)) {
printf("%s(EFI_TIMESTAMP_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
"controller_handle=%p, attr=0x%x)\n",
@@ -256,6 +247,16 @@ EfiStatus open_protocol(EfiHandle handle, const EfiGuid *protocol, void **intf,
"controller_handle=%p, attr=0x%x)\n",
__FUNCTION__, handle, agent_handle, controller_handle, attr);
return open_efi_erase_block_protocol(handle, intf);
} else if (guid_eq(protocol, EFI_BOOT_MEMORY_PROTOCOL_GUID)) {
printf(
"%s(EFI_BOOT_MEMORY_PROTOCOL_GUID, handle=%p, agent_handle=%p, "
"controller_handle=%p, attr=0x%x)\n",
__FUNCTION__, handle, agent_handle, controller_handle, attr);
*intf = open_boot_memory_protocol();
if (*intf == nullptr) {
return EFI_STATUS_OUT_OF_RESOURCES;
}
return EFI_STATUS_SUCCESS;
}
printf("%s is unsupported 0x%x 0x%x 0x%x 0x%llx\n", __FUNCTION__,
protocol->data1, protocol->data2, protocol->data3,
@@ -334,6 +335,16 @@ EfiStatus locate_handle_buffer(EfiLocateHandleSearchType search_type,
*buf = reinterpret_cast<EfiHandle *>(uefi_malloc(sizeof(buf)));
}
return EFI_STATUS_SUCCESS;
} else if (guid_eq(protocol, EFI_BOOT_MEMORY_PROTOCOL_GUID)) {
printf("%s(0x%x, EFI_BOOT_MEMORY_PROTOCOL_GUID, search_key=%p)\n",
__FUNCTION__, search_type, search_key);
if (num_handles != nullptr) {
*num_handles = 1;
}
if (buf != nullptr) {
*buf = reinterpret_cast<EfiHandle*>(uefi_malloc(sizeof(buf)));
}
return EFI_STATUS_SUCCESS;
}
printf("%s(0x%x, (0x%x 0x%x 0x%x 0x%llx), search_key=%p)\n", __FUNCTION__,
search_type, protocol->data1, protocol->data2, protocol->data3,

View File

@@ -122,6 +122,12 @@ static constexpr auto EFI_TIMESTAMP_PROTOCOL_GUID =
0x4262,
{0xba, 0x65, 0x62, 0xb9, 0x23, 0x6e, 0x54, 0x95}};
static constexpr auto EFI_BOOT_MEMORY_PROTOCOL_GUID =
EfiGuid{0x309f2874,
0xad59,
0x4fd2,
{0xaf, 0x5e, 0xce, 0x0f, 0x4a, 0xb4, 0x01, 0xa6}};
static constexpr auto EFI_ERASE_BLOCK_PROTOCOL_GUID =
EfiGuid{0x95A9A93E,
0xA86E,

View File

@@ -16,7 +16,6 @@
*/
#include "memory_protocols.h"
#include "uefi_platform.h"
#include <arch/defines.h>
#include <arch/mmu.h>
@@ -27,8 +26,11 @@
#include <string.h>
#include <sys/types.h>
#include <uefi/boot_service.h>
#include <uefi/protocols/gbl_efi_boot_memory_protocol.h>
#include <uefi/types.h>
#include "uefi_platform.h"
#define LOCAL_TRACE 0
// MACRO list_for_every_entry cast between int/ptr
@@ -256,4 +258,54 @@ EfiStatus get_physical_memory_map(size_t *memory_map_size,
return EFI_STATUS_SUCCESS;
}
// NOLINTEND(performance-no-int-to-ptr)
// NOLINTEND(performance-no-int-to-ptr)
namespace {
EfiStatus get_partition_buffer(struct GblEfiBootMemoryProtocol* self,
/* in */ const uint8_t* base_name,
/* out */ size_t* size,
/* out */ void** addr,
/* out */ GblEfiPartitionBufferFlag* flag) {
return EFI_STATUS_NOT_FOUND;
}
EfiStatus sync_partition_buffer(struct GblEfiBootMemoryProtocol* self,
/* in */ bool sync_preloaded) {
return EFI_STATUS_SUCCESS;
}
EfiStatus get_boot_buffer(struct GblEfiBootMemoryProtocol* self,
/* in */ GblEfiBootBufferType buf_type,
/* out */ size_t* size,
/* out */ void** addr) {
if (buf_type == GBL_EFI_BOOT_BUFFER_TYPE_GENERAL_LOAD) {
*size = 128ul * 1024 * 1024;
// 2^21 = 2MB alignment, required by linux kernel
*addr = alloc_page(*size, 21);
if (*addr == nullptr) {
return EFI_STATUS_OUT_OF_RESOURCES;
}
return EFI_STATUS_SUCCESS;
} else if (buf_type == GBL_EFI_BOOT_BUFFER_TYPE_PVMFW_DATA) {
*size = 1024ul * 1024;
*addr = alloc_page(*size, PAGE_SIZE_SHIFT);
if (*addr == nullptr) {
return EFI_STATUS_OUT_OF_RESOURCES;
}
return EFI_STATUS_SUCCESS;
}
printf("get_boot_buffer(%d, %zu) unsupported\n", buf_type, *size);
return EFI_STATUS_NOT_FOUND;
}
} // namespace
__WEAK GblEfiBootMemoryProtocol* open_boot_memory_protocol() {
static GblEfiBootMemoryProtocol protocol = {
.revision = GBL_EFI_BOOT_MEMORY_PROTOCOL_REVISION,
.get_partition_buffer = get_partition_buffer,
.sync_partition_buffer = sync_partition_buffer,
.get_boot_buffer = get_boot_buffer,
};
return &protocol;
}

View File

@@ -21,6 +21,7 @@
#include <kernel/vm.h>
#include <stddef.h>
#include <uefi/boot_service.h>
#include <uefi/protocols/gbl_efi_boot_memory_protocol.h>
#include <uefi/types.h>
vmm_aspace_t *set_boot_aspace();

View File

@@ -159,31 +159,6 @@ __WEAK EfiStatus set_watchdog_timer(size_t timeout, uint64_t watchdog_code,
namespace {
const char* GetImageType(const char16_t* ImageType) {
if (memcmp(ImageType, GBL_IMAGE_TYPE_OS_LOAD,
sizeof(GBL_IMAGE_TYPE_OS_LOAD)) == 0) {
return "os_load";
} else if (memcmp(ImageType, GBL_IMAGE_TYPE_FASTBOOT,
sizeof(GBL_IMAGE_TYPE_FASTBOOT)) == 0) {
return "fastboot";
} else if (memcmp(ImageType, GBL_IMAGE_TYPE_PVMFW_DATA,
sizeof(GBL_IMAGE_TYPE_PVMFW_DATA)) == 0) {
return "pvmfw_data";
}
return "unknown";
}
template <typename T>
T clamp(T n, T lower, T upper) {
if (n < lower) {
return lower;
}
if (n > upper) {
return upper;
}
return n;
}
struct EfiEraseBlockInterface {
EfiEraseBlockProtocol protocol;
bdev_t* dev;
@@ -219,28 +194,6 @@ EfiStatus erase_blocks(EfiEraseBlockProtocol* self, uint32_t media_id,
} // namespace
__WEAK EfiStatus get_buffer(struct GblEfiImageLoadingProtocol* self,
const GblEfiImageInfo* ImageInfo,
GblEfiImageBuffer* Buffer) {
printf("%s(%s, %lu)\n", __FUNCTION__, GetImageType(ImageInfo->ImageType),
ImageInfo->SizeBytes);
// Allow maximum of 128MB buffer
const size_t buffer_size =
clamp(Buffer->SizeBytes, PAGE_SIZE, 128ul * 1024 * 1024);
// Bottom line, kernel, ramdisk, device tree must be identity mapped.
// Otherwise linux kernel would crash immediately after entering.
// Other buffers can be allocated from kernel address space, up to
// OEM for customization.
Buffer->Memory = alloc_page(buffer_size);
if (Buffer->Memory == nullptr) {
return EFI_STATUS_OUT_OF_RESOURCES;
}
Buffer->SizeBytes = buffer_size;
return EFI_STATUS_SUCCESS;
}
__WEAK EfiStatus open_efi_erase_block_protocol(EfiHandle handle, void** intf) {
auto* device_name = static_cast<const char*>(handle);
LTRACEF("handle=%p (%s)\n", handle, device_name);

View File

@@ -19,6 +19,7 @@
#define __GBL_OS_CONFIGURATION_
#include <arch/defines.h>
#include <uefi/protocols/gbl_efi_boot_memory_protocol.h>
#include <uefi/protocols/gbl_efi_image_loading_protocol.h>
#include <uefi/protocols/gbl_efi_os_configuration_protocol.h>
#include <uefi/protocols/timestamp.h>
@@ -77,4 +78,6 @@ void reset_heap();
EfiStatus open_efi_erase_block_protocol(EfiHandle handle, void** intf);
GblEfiBootMemoryProtocol* open_boot_memory_protocol();
#endif