UEFI与固件程序设计 Tel:69589584 Email:wang.box@163.com 同济大学软件学院 Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers Boot Flow DXE IPL DXE MAIN Exposed Platform Interface verify Pre Verifier CPU Init Chipset Init Transient OS Environment Device, Bus, or Service Driver Board Init EFI Driver Dispatcher Transient OS Boot Loader Power on Pre EFI Initialization (PEI) Driver Execution Environment (DXE) [ . . Platform initialization . . ] OS-Present App Boot Manager Intrinsic Services security Security (SEC) OS-Absent App Boot Dev Select (BDS) Final OS Boot Loader Final OS Environment ? Transient System Load (TSL) Run Time (RT) After Life (AL) [ . . . . OS boot . . . . ] Shutdown Why DXE No DXE – BIOS features coded in proprietary fashion ODM has to port features from IBV to IBV Third parties can not provide value added pre OS features – Single source file controls boot flow With DXE – Works like OS, large group of companies can write drivers – Modular systems enabled FLASH on a plug in module to support module Properties of DXE Foundation Depends only on HOB list – State initialization passed in from PEI No hard coded addresses in DXE – Foundation code can be loaded anywhere No hardware specifics in DXE Foundation – Access to hardware abstracted by a set of architectural protocols (APs) – APs implemented as drivers – Only DXE Foundation may call APs – APs encapsulate CPU, chipset, board specifics DXE Components DXE Core – The main DXE executable binary responsible for dispatching drivers and provide basic services. DXE Driver – code loaded by the core to do various initializations, produce protocols and other services. DXE Dispatcher – The part of the DXE core that searches for and executes the drivers in the correct order. DXE Architectural Protocols – Produced by DXE drivers to abstract DXE from hardware. UEFI System Table – Contains pointers to all the EFI service tables, configuration tables, handle database, and console device. Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers DXE Core No hard coded addresses allowed Single Binary for each CPU Architecture Well specified and validated Written in C. No assembly allowed Consumes HOB List Architectural Protocols Produces Dispatcher EFI Boot Services EFI Runtime Services DXE Core Block Diagram System System Memory Memory System I/O Memory Resources System MMIO Memory Resources System Firmware Memory Devices System Firmware Memory Volumes System DXE Memory Drivers HOB List PHIT HOB HOB HOB EFI Boot Services Table HOB HOB DXE Services ... System DXE Memory Drivers HOB EFI System Table EFI Runtime Services Table DXE Core / DXE Dispatcher Security Architectural Protocol CPU Architectural Protocol Metronome Architectural Protocol Timer Architectural Protocol BDS Architectural Protocol Runtime Architectural Protocol Watchdog Timer Architectural Protocol Hardware Variable Architectural Protocol Monotonic Counter Architectural Protocol Reset Architectural Protocol Status Code Architectural Protocol Real Time Clock Architectural Protocol DXE Core Functional Blocks DXE Core EFI Boot Services DXE Services Task Priority Services Event and Timer Services Image Services Global Coherency Domain Services Memory Services Protocol Handler Services Driver Support Services Dispatcher Services Firmware Volume Driver PE/COFF HOB Loader Parser DXE Dispatcher Firmware Volume Block Driver Section Extraction Protocol Driver Flush Instruction Cache (Read-Only) (Memory Mapped) Decompress Driver Switch SetJump Stacks LongJump Dependency Expression Evaluator DXE Core Data Structures Active Consoles Input Console UEFI System Table UEFI Runtime Services Table Variable Services Output Console Real Time Clock Services Standard Error Console Reset Services Status Code Services UEFI Boot Services Table Virtual Memory Services Task Priority Level Services Memory Services Version Information Event and Timer Services UEFI Specification Version Protocol Handler Services Firmware Vendor Image Services Firmware Revision Driver Support Services System Configuration Table DXE Services Table Global Coherency Domain Services Dispatcher Services DXE Services Table HOB List ACPI Table SMBIOS Table Handle Database Protocol Interface Protocol Interface Protocol Interface Protocol Interface Protocol Interface Protocol Interface … SAL System Table Boot Services and Structures Runtime Services and Structures Only available prior to OS runtime Available before and during OS runtime DXE Main Responsible for Initializing DXE Core Consumes HOB List Builds EFI System Table Builds EFI Boot Services Table Builds EFI Runtime Services Table Builds DXE Services Table Makes Memory-Only Boot Services Available Hands off control to the DXE Dispatcher Requires access to Firmware Volumes Requires LoadImage(), StartImage(), Exit() May require decompression service DXE Core Initialization Initialize EFI Boot Services Table Global Variable in DXE Core All services return EFI_NOT_AVAILABLE_YET Initialize DXE Services Table Global Variable in DXE Core All services return EFI_NOT_AVAILABLE_YET Initialize Memory-Only TPL Services RaiseTPL(), RestoreTPL() Initialize Memory Services (Parses HOB List) AllocatePages(), AllocatePool(), FreePages(), FreePool() Allocate EFI System Table Allocated from EfiRuntimeServicesData Allocate EFI Runtime Service Table Allocated from EfiRuntimeServicesData All services return EFI_NOT_AVAILABLE_YET DXE Core Initialization Initialize GCD Services (Parses HOB List) Initialize Driver Support Services Initialize Event Services Initialize Protocol Services Initialize Miscellaneous Services Add HOB List to System Configuration Table Part of EFI System Table Provides other component access to HOB List Initialize Image Services Creates Image Handle for DXE Core itself DXE Core Initialization Create event for each Architectural Protocol Informs DXE Core when AP is installed Used to complete EFI Boot Services Used to complete EFI Runtime Services Initialize Firmware Volume Drivers Provides file access to FVs discovered by PEI Initialize File Decompression (Optional) Hand control to DXE Dispatcher Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers Dispatcher Goals Resolve Execution Ordering Among DXE Drivers Drivers may be written by different organizations Different divisions Different companies Support Known Business Issues Emergency patches “Control of Destiny” for System developer Add-in card developer Driver developer Expansion Hooks for e.g. Security DXE Dispatcher Model Some of each A Priori list of drivers to be run first Defined by system developers May be null Generally implemented as pre-load dispatch queue Requirements based dispatching for the rest General Concept: Driver provides a list of requirements that must be met before it can be dispatched. A syntax that gets the ordering to about 90% is ok EFI primitives exist to do finer granularity ordering Requirements Protocols are the “Requirements” Represented by their GUIDs The fundamental primitive is “Does this protocol exist?” Hits the 90% target Driver may have to do RequestProtocolNotify for finer granularity “List of Requirements”: Boolean Expressions Operators: And, Or, Not (&, |, !) Special operators: Before, After Run this driver just before or just after that one Used for patching Internally, stored in driver header as Postfix (RPN) for quicker processing Tool support planned Driver Execution Environment DXE Foundation Dispatcher Handle Database 1 Loaded Image Protocol Main Firmware Volume DXE Dispatcher Driver C Dependent: A B C Scheduled: Driver B Initializing: (CPU_AP) Initialized: Driver A Dependency Expression: CPU_AP Evaluates To: FALSE (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database 1 Loaded Image Protocol Main Firmware Volume DXE Dispatcher Driver C Dependent: A B C Scheduled: Driver B Initializing: (CPU_AP) Initialized: Driver A Dependency Expression: CPU_AP TRUE Evaluates To: FALSE TRUE (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database Main Firmware Volume DXE Dispatcher ` 1 Loaded Image Protocol Driver C Dependent: A Scheduled: B B C C Driver B Initializing: (CPU_AP) Initialized: Driver A Dependency Expression: CPU_AP AND TIMER_AP TRUE FALSE TRUE Evaluates To: FALSE (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Main Firmware Volume Handle Database DXE Dispatcher ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: A Scheduled: B B C C Driver B Initializes Initializing: Driver B (CPU_AP) B Driver A Dependency Expression: CPU_AP AND TIMER_AP TRUE (Timer_AP) Initialized: Evaluates To: FALSE TRUE DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database Main Firmware Volume DXE Dispatcher ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: A Scheduled: B B C C Driver B Initializes Initializing: Driver B (CPU_AP) B Driver A Dependency Expression: CPU_AP AND TIMER_AP TRUE TRUE Evaluates To: FALSE (Timer_AP) Initialized: DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database Main Firmware Volume DXE Dispatcher ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: A C Scheduled: B A B C C (CPU_AP) Initializing: Initialized: Driver B B Dependency Expression: CPU_AP TRUE CPU_AP AND AND TIMER_AP TIMER_AP TRUE Evaluates To: FALSE FALSE Driver A (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database DXE Dispatcher Main Firmware Volume ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: 3 Driver A Initializes Scheduled: Driver B (CPU_AP) Initializing: Initialized: Timer Architectural Protocol A C B A Dependency Expression: CPU_AP TRUE CPU_AP AND AND TIMER_AP TIMER_AP TRUE Evaluates To: FALSE FALSE Driver A (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database DXE Dispatcher Main Firmware Volume ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: 3 Driver A Initializes Scheduled: Driver B (CPU_AP) Initializing: Initialized: Timer Architectural Protocol A C B A Dependency Expression: CPU_AP AND TIMER_AP Evaluates To: TRUE Driver A (Timer_AP) DXE Foundation Driver Execution Environment DXE Foundation Dispatcher Handle Database DXE Dispatcher Main Firmware Volume ` 1 2 Loaded Image Protocol CPU Architectural Protocol Driver C Dependent: A C Scheduled: B 3 Driver B (CPU_AP) Initializing: Initialized: Timer Architectural Protocol Driver C Initializes B A C Dependency Expression: CPU_AP AND TIMER_AP Evaluates To: TRUE Execution Order Determined at Runtime Based on Dependencies Driver A (Timer_AP) DXE Foundation Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers DXE – Architectural Protocols What are Architectural Protocols? Low level protocols that support DXE APIs Provide support for boot services and runtime services Typically touch hardware Only called by DXE core (directly) DXE Core System System Memory Memory System I/O Memory Resources System MMIO Memory Resources System Firmware Memory Devices System Firmware Memory Volumes System DXE Memory Drivers HOB List PHIT HOB HOB HOB EFI Boot Services Table HOB HOB DXE Services ... System DXE Memory Drivers HOB EFI System Table EFI Runtime Services Table DXE Core / DXE Dispatcher Security Architectural Protocol CPU Architectural Protocol Metronome Architectural Protocol Timer Architectural Protocol BDS Architectural Protocol Runtime Architectural Protocol Watchdog Timer Architectural Protocol Hardware Variable Architectural Protocol Monotonic Counter Architectural Protocol Reset Architectural Protocol Status Code Architectural Protocol Real Time Clock Architectural Protocol DXE – Architectural Protocols Boot Service APs CPU – cache, interrupts, reset, timers Metronome – periodic signals Timer – event services Boot Device Selection (BDS) Watchdog Timer Security Runtime Service APs Runtime – virtual addressing Real Time Clock Reset Variable Services – nonvolatile storage access Monotonic Counter Status Code DXE – Architectural Protocols Some APs have dependencies on others. Timer requires interrupts (CPU) and IO access Watchdog timer requires timer and IO access Reset requires CPU and IO access Dependencies can be satisfied by using one or more of the following methods to control load order Dependency grammar to have DXE load in the correct order RegisterProtocolNotify() to be notified when required AP gets loaded Apriori list – file in the file system containing list of filename GUIDs DXE – Architectural Protocols BDS Security Status Code Runtime Services Variable Services Watchdog Timer Realtime Clock Timer Metronome CPU PCI I/O Abstraction DXE Core FDVP Load Order Reset DXE – Architectural Protocols CPU APs FlushDataCache() EnableInterrupt()/DisableInterrupt()/GetInterruptState() RaiseTPL() and RestoreTPL() Init() reset control RegisterInterruptHandler() Used by timer initialization GetTimerValue() SetMemoryAttributes() DXE – Architectural Protocols Watchdog Timer RegisterHandler() SetTimer() GetTimer() Boot Device Select (BDS) Entry() to hand off system to OS loader or system utility Security FileAuthenticationState() DXE – Architectural Protocols Real Time Clock APs RT->GetTime() RT->SetTime() RT->GetWakeupTime() RT->SetWakeupTime() Reset APs RT->ResetSystem() – for cold or warm system reset Status Code RT->ReportStatusCode() Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers DXE Services Dispatcher Services Dispatch() Schedule() Trust() Global Coherency Domain Services Used to manage CPU visible memory resources Used to manage CPU visible I/O resources DXE GCD Services GCD Memory Services AddMemorySpace() AllocateMemorySpace() FreeMemorySpace() RemoveMemorySpace() GetMemorySpaceDescriptor() SetMemorySpaceAttributes() GetMemorySpaceMap() GCD I/O Services AddIoSpace() AllocateIoSpace() FreeIoSpace() RemoveIoSpace() GetIoSpaceDescriptor() GetIoSpaceMap() Agenda Overview DXE Core DXE Dispatcher DXE Architectural Protocol DXE Services DXE Drivers DXE Drivers DXE drivers are DXE Components that Initialize the platform Provide the services required to boot an UEFI-compliant operating system Or a set of UEFI-compliant system utilities. DXE drivers may consume UEFI Boot Services UEFI Runtime Services DXE Services Classification of DXE Drivers According to Functionality Early DXE Drivers DXE Drivers that follow the UEFI Driver Model According to Life Cycle Boot service drivers Runtime drivers Early DXE Drivers Early DXE Drivers Basic services Processor initialization code Chipset initialization code Platform initialization code DXE Architectural Protocols Not all services are available Execution order depends on dependency expressions As much initialization should be deferred to DXE drivers that follow UEFI Driver Model DXE Drivers that Follow the UEFI Driver Model DXE Drivers that follow the UEFI Driver Model Do not touch any hardware resources when they initialize Register a Driver Binding Protocol interface Ultimately provide software abstraction for console devices and boot devices Do not need to be concerned with dependency expressions Selected by BDS