在内核中使用 UEFI Runtime Services
来源:OSDev Wiki· 阅读原文 ↗
文章目录
UEFI Runtime Services 是由固件提供的函数,可在任何 CPU 模式下、任何时间使用,即使在退出 UEFI Boot Services 之后也是如此。Runtime Services 可能包含对操作系统有用的函数,例如重置/关闭系统、获取当前时间、设置当前时间等。人们可能希望在引导过程之外使用这些函数。这个过程相对简单。
获取 Runtime Services 指针
引导加载程序
为了获取 Runtime Services 指针,强烈建议使用一个经过修改的、最小化的、仅限 UEFI 的引导加载程序。 以下引导加载程序将无法工作:
- GRUB
- coreboot
- NexBoot
- 任何基于 BIOS 的引导加载程序
获取
为了获取 Runtime Services 指针,你需要一个传递给内核的信息结构。如果你没有,务必创建一个。
你只需向该结构添加一个成员。
typedef struct s_boot_info { // A pointer to the EFI runtime services structure // that contains numerous useful function pointers EFI_RUNTIME_SERVICES *RT; } boot_info;
为了将指针传递给内核,你需要自行定义 EFI_RUNTIME_SERVICES 结构。
你需要定义 Runtime Services 头文件和结构。
typedef struct s_efi_table_header { uint64_t signature; uint32_t rev; uint32_t size; uint32_t crc; uint32_t reserved; } Efi_Table_Header;
// A structure replicating that of the EFI_RUNTIME_SERVICES structure in UEFI typedef struct s_efi_runtime_service_handle { Efi_Table_Header header; Efi_Get_Time GetTime; Efi_Set_Time SetTime; Efi_Get_Wakeup_Time GetWakeupTime; Efi_Set_Wakeup_Time SetWakeupTime; Efi_Set_Virtual_Address_Map SetVirtualAddressMap; Efi_Convert_Pointer ConvertPointer; Efi_Get_Variable GetVariable; Efi_Get_Next_Variable_Name GetNextVariableName; Efi_Set_Variable SetVariable; Efi_Get_Next_High_Mono_Count GetNextHighMonotonicCount; Efi_Reset_System ResetSystem; Efi_Update_Capsule UpdateCapsule; Efi_Query_Capsule_Capabilities QueryCapsuleCapabilities; Efi_Query_Variable_Info QueryVariableInfo; } Efi_Runtime_Services;
你还需要自行声明这些方法以及相关的结构和枚举。
typedef enum { EfiResetCold, EfiResetWarm, EfiResetShutdown } Efi_Reset_Type; typedef uint64_t (EfiApi *Efi_Reset_System) ( Efi_Reset_Type ResetType, uint64_t ResetStatus, uint64_t DataSize, uint16_t *ResetData );
要找到正确的声明/结构,请参考 UEFI 头文件,特别是 GNU-EFI 的 efiapi.h。
EFIAPI
这个定义不是一个类型,而是一个宏,用于指定函数必须使用 UEFI 调用约定。
#define EfiApi __attribute__(ms_abi)
使用 Runtime Services
现在你有了指针,就可以开始调用 UEFI 函数了。
void kmain(boot_info *info) { // Shuts down the system info->RT->ResetSystem(EfiResetShutdown, 0, 0, nullptr); }
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Using UEFI Runtime Services in your Kernel;离线修订号:25396。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论