UEFI Framework - 5 [ EFI Runtime ]

Service Type

  1. Boot Services:開機的Phase到OSLoader 還沒呼叫 ExitBootServices時會存在
  2. Runtime Services:開機的Phase到進入OS都可以使用





在圖中可以看到 EFI Boot Services 只會在 OS Loader還沒進入OS的時後存在
                        而 Runtime Services 則會到Shutdown為止


下面為Boot Services 以及 Runtime Service的 Structure

路徑為 /MdePkg/Include/Library/UefiBootServiceTableLib.h
///
/// Cache pointer to the EFI Boot Services Table
///
extern EFI_BOOT_SERVICES  *gBS;

路徑為 /MdePkg/Include/Library/UefiRuntimeServiceTableLib.h

///
/// Cached copy of the EFI Runtime Services Table
///
extern EFI_RUNTIME_SERVICES  *gRT;

路徑為 /MdePkg/Include/Uefi/UefiSpec.h

///
/// EFI Boot Services Table.
///
typedef struct {
  ///
  /// The table header for the EFI Boot Services Table.
  ///
  EFI_TABLE_HEADER                Hdr;

  //
  // Task Priority Services
  //
  EFI_RAISE_TPL                   RaiseTPL;
  EFI_RESTORE_TPL                 RestoreTPL;

  //
  // Memory Services
  //
  EFI_ALLOCATE_PAGES              AllocatePages;
  EFI_FREE_PAGES                  FreePages;
  EFI_GET_MEMORY_MAP              GetMemoryMap;
  EFI_ALLOCATE_POOL               AllocatePool;
  EFI_FREE_POOL                   FreePool;

  //
  // Event & Timer Services
  //
  EFI_CREATE_EVENT                  CreateEvent;
  EFI_SET_TIMER                     SetTimer;
  EFI_WAIT_FOR_EVENT                WaitForEvent;
  EFI_SIGNAL_EVENT                  SignalEvent;
  EFI_CLOSE_EVENT                   CloseEvent;
  EFI_CHECK_EVENT                   CheckEvent;

  //
  // Protocol Handler Services
  //
  EFI_INSTALL_PROTOCOL_INTERFACE    InstallProtocolInterface;
  EFI_REINSTALL_PROTOCOL_INTERFACE  ReinstallProtocolInterface;
  EFI_UNINSTALL_PROTOCOL_INTERFACE  UninstallProtocolInterface;
  EFI_HANDLE_PROTOCOL               HandleProtocol;
  VOID                              *Reserved;
  EFI_REGISTER_PROTOCOL_NOTIFY      RegisterProtocolNotify;
  EFI_LOCATE_HANDLE                 LocateHandle;
  EFI_LOCATE_DEVICE_PATH            LocateDevicePath;
  EFI_INSTALL_CONFIGURATION_TABLE   InstallConfigurationTable;

  //
  // Image Services
  //
  EFI_IMAGE_LOAD                    LoadImage;
  EFI_IMAGE_START                   StartImage;
  EFI_EXIT                          Exit;
  EFI_IMAGE_UNLOAD                  UnloadImage;
  EFI_EXIT_BOOT_SERVICES            ExitBootServices;

  //
  // Miscellaneous Services
  //
  EFI_GET_NEXT_MONOTONIC_COUNT      GetNextMonotonicCount;
  EFI_STALL                         Stall;
  EFI_SET_WATCHDOG_TIMER            SetWatchdogTimer;

  //
  // DriverSupport Services
  //
  EFI_CONNECT_CONTROLLER            ConnectController;
  EFI_DISCONNECT_CONTROLLER         DisconnectController;

  //
  // Open and Close Protocol Services
  //
  EFI_OPEN_PROTOCOL                 OpenProtocol;
  EFI_CLOSE_PROTOCOL                CloseProtocol;
  EFI_OPEN_PROTOCOL_INFORMATION     OpenProtocolInformation;

  //
  // Library Services
  //
  EFI_PROTOCOLS_PER_HANDLE          ProtocolsPerHandle;
  EFI_LOCATE_HANDLE_BUFFER          LocateHandleBuffer;
  EFI_LOCATE_PROTOCOL               LocateProtocol;
  EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES    InstallMultipleProtocolInterfaces;
  EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES  UninstallMultipleProtocolInterfaces;

  //
  // 32-bit CRC Services
  //
  EFI_CALCULATE_CRC32               CalculateCrc32;

  //
  // Miscellaneous Services
  //
  EFI_COPY_MEM                      CopyMem;
  EFI_SET_MEM                       SetMem;
  EFI_CREATE_EVENT_EX               CreateEventEx;
} EFI_BOOT_SERVICES;


///
/// EFI Runtime Services Table.
///
typedef struct {
  ///
  /// The table header for the EFI Runtime Services Table.
  ///
  EFI_TABLE_HEADER                Hdr;

  //
  // Time Services
  //
  EFI_GET_TIME                    GetTime;
  EFI_SET_TIME                    SetTime;
  EFI_GET_WAKEUP_TIME             GetWakeupTime;
  EFI_SET_WAKEUP_TIME             SetWakeupTime;

  //
  // Virtual Memory Services
  //
  EFI_SET_VIRTUAL_ADDRESS_MAP     SetVirtualAddressMap;
  EFI_CONVERT_POINTER             ConvertPointer;

  //
  // Variable Services
  //
  EFI_GET_VARIABLE                GetVariable;
  EFI_GET_NEXT_VARIABLE_NAME      GetNextVariableName;
  EFI_SET_VARIABLE                SetVariable;

  //
  // Miscellaneous Services
  //
  EFI_GET_NEXT_HIGH_MONO_COUNT    GetNextHighMonotonicCount;
  EFI_RESET_SYSTEM                ResetSystem;

  //
  // UEFI 2.0 Capsule Services
  //
  EFI_UPDATE_CAPSULE              UpdateCapsule;
  EFI_QUERY_CAPSULE_CAPABILITIES  QueryCapsuleCapabilities;

  //
  // Miscellaneous UEFI 2.0 Service
  //
  EFI_QUERY_VARIABLE_INFO         QueryVariableInfo;
} EFI_RUNTIME_SERVICES;


EFI Memory Type



typedef enum {
  EfiReservedMemoryType,
  EfiLoaderCode,
  EfiLoaderData,
  EfiBootServicesCode,
  EfiBootServicesData,
  EfiRuntimeServicesCode,
  EfiRuntimeServicesData,
  EfiConventionalMemory,
  EfiUnusableMemory,
  EfiACPIReclaimMemory,
  EfiACPIMemoryNVS,
  EfiMemoryMappedIO,
  EfiMemoryMappedIOPortSpace,
  EfiPalCode,
  EfiMaxMemoryType
} EFI_MEMORY_TYPE;

Memory Type Usage before ExitBootServices()
MnemonicDescription
EfiReservedMemoryTypeNot used.
EfiLoaderCodeThe code portions of a loaded application. (Note that UEFI OS loaders are UEFI applications.)
EfiLoaderDataThe data portions of a loaded application and the default data allocation type used by an application to allocate pool memory.
EfiBootServicesCodeThe code portions of a loaded Boot Services Driver.
EfiBootServicesDataThe data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot Services Driver to allocate pool memory.
EfiRuntimeServicesCodeThe code portions of a loaded Runtime Services Driver.
EfiRuntimeServicesDataThe data portions of a loaded Runtime Services Driver and the default data allocation type used by a Runtime Services Driver to allocate pool memory.
EfiConventionalMemoryFree (unallocated) memory.
EfiUnusableMemoryMemory in which errors have been detected.
EfiACPIReclaimMemoryMemory that holds the ACPI tables.
EfiACPIMemoryNVSAddress space reserved for use by the firmware.
EfiMemoryMappedIOUsed by system firmware to request that a memory-mapped IO region be mapped by the OS to a virtual address so it can be accessed by EFI runtime services.
EfiMemoryMappedIOPortSpaceSystem memory-mapped IO region that is used to translate memory cycles to IO cycles by the processor. Note: There is only one region of type EfiMemoryMappedIoPortSpace defined in the architecture for Itanium-based platforms. As a result, there should be one and only one region of type EfiMemoryMappedIoPortSpace in the EFI memory map of an Itanium-based platform.
EfiPalCodeAddress space reserved by the firmware for code that is part of the processor.
Memory Type Usage after ExitBootServices()
MnemonicDescription
EfiReservedMemoryTypeNot used.
EfiLoaderCodeThe Loader and/or OS may use this memory as they see fit. Note: the OS loader that called ExitBootServices() is utilizing one or
more EfiLoaderCode ranges.
EfiLoaderDataThe Loader and/or OS may use this memory as they see fit. Note: the OS loader that called ExitBootServices() is utilizing one or
more EfiLoaderData ranges.
EfiBootServicesCodeMemory available for general use.
EfiBootServicesDataMemory available for general use.
EfiRuntimeServicesCodeThe memory in this range is to be preserved by the loader and OS in the working and ACPI S1–S3 states.
EfiRuntimeServicesDataThe memory in this range is to be preserved by the loader and OS in the working and ACPI S1–S3 states.
EfiConventionalMemoryMemory available for general use.
EfiUnusableMemoryMemory that contains errors and is not to be used.
EfiACPIReclaimMemoryThis memory is to be preserved by the loader and OS until ACPI is enabled. Once ACPI is enabled, the memory in this range is available for general use.
EfiACPIMemoryNVSThis memory is to be preserved by the loader and OS in the working and ACPI S1–S3 states.
EfiMemoryMappedIOThis memory is not used by the OS. All system memory-mapped IO information should come from ACPI tables.
EfiMemoryMappedIOPortSpaceThis memory is not used by the OS. All system memory-mapped IO port space information should come from ACPI tables.
EfiPalCodeThis memory is to be preserved by the loader and OS in the working and ACPI S1–S3 states. This memory may also have other attributes that are defined by the processor implementation




在開機後有兩個保留下且可使用的資料為

  1. Runtime Service Table:裡面保留了所有Runtime Service的Pointer,這些Service是在開機後可以被用的。
  2. EFI Configuration Table:裡面包含了GUID跟Pointer的pair。


這張圖說明了 EFI Configuration Table 跟 Function prototype 之間的關係





下面所介紹的Runtime Service 可以從這張圖右上角看到




Table 5.3說明了一些關於時間的api使用,也就是能透過這些api跟RTC 溝通,如果自己去寫一些與RTC溝通的function有可能會造成無法預期的錯誤。



Table 5.4介紹了能將 Runtime Service的address 從實體轉虛擬



在Table 5.5中介紹如何取得Variable值的API
gRT->SetVariable(L"BootCurrent", &gEfiGlobalVariableGuid, 0, 0, NULL);
gRT->GetVariable(BootVar, &gEfiGlobalVariableGuid, &VarAttributes, &VarDataSize, CurrentLoadOption);


Table 5.6中有列出一些基本的Global Variable 
NV(Nonvolatile) :value 一直到reboot 都是有效的
BS(Boot Service): 只在preboot environment的時後有效
RT(Runtime Service):在 Preboot 跟 OS時後都有效

Misscellaneous Services



  1. ResetSystem:提供了能夠重開 Processor 跟 Device 的功能
  2. gRT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
    gRT->ResetSystem (EfiResetShutdown, Status, 0, NULL);
    gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
    
    1. Cold Reset:呼叫 ResetSystem() Service 進行整個系統的reset。
    2. Warm Reset: 呼叫 ResetSystem() Service 進行整個系統的initialize,不同點在於Memory不會被initial,透過memory裡面的資料rebooting到先前工作的狀態
    3. Reset Shutdown: 呼叫 ResetSystem() Service 進入 ACPI G2/S5 or G3 (參考下表)
  3. GetNext High Monotonic Count:這個Service只在Boot Service time有效 長度為64bit 每次取得都會比上次大(+1)


留言

張貼留言