1 / 57

UEFI 与固件程序设计

UEFI 与固件程序设计. Tel : 69589584 Email:wang.box@163.com. 同济大学软件学院. Agenda. EFI Driver Development EFI Application Development. EFI Drivers . EFI Driver Model. Supports complex bus hierarchies Follows the organization of physical/electrical architecture of the machine

teddy
Télécharger la présentation

UEFI 与固件程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UEFI与固件程序设计 Tel:69589584 Email:wang.box@163.com 同济大学软件学院

  2. Agenda • EFI Driver Development • EFI Application Development

  3. EFI Drivers EFI Driver Model • Supports complex bus hierarchies • Follows the organization of physical/electrical architecture of the machine • Driver Binding Protocol provides flexibility • Function to match drivers to devices • Driver version management • Hot-plug and unload support • Drivers not tied to FLASH • Can be loaded from EFI System Partition • Extensible • Able to extend to future bus and device types

  4. EFI Drivers Driver Design Steps • Determine Driver Type • Identify Consumed I/O Protocols • Identify Produced I/O Protocols • Identify EFI Driver Model Protocols • Identify Additional Driver Features • Identify Target Platforms • x86 • x64 • Itanium Processor Family • EFI Byte Code (EBC)

  5. EFI Drivers Hybrid Drivers Device Drivers Bus Drivers Bus Drivers Device Drivers What Type of Driver is Being Designed? EFI Images Drivers Service Drivers EFI Driver Model Initializing Drivers Root Bridge Drivers EFI 1.02 Drivers Applications OS Loaders

  6. EFI Drivers Drivers Device Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Manages a Controller or Peripheral Device • Start() Does Not Create Any Child Handles • Start() Produces One or More I/O Protocols • Installed onto the Device’s Controller Handle Examples: PCI Video Adapters USB Host Controllers USB Keyboards / USB Mice PS/2 Keyboards / PS/2 Mice

  7. EFI Drivers Drivers Bus Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Manages and Enumerates a Bus Controller • Start() Creates One or More Child Handles • Start() Produces Bus Specific I/O Protocols • Installed onto the Bus’s Child Handles • Examples: • PCI Network Interface Controllers • Serial UART Controllers

  8. EFI Drivers Hybrid Drivers Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Manages and Enumerates a Bus Controller • Start() Creates One or More Child Handles • Start() Produces Bus Specific I/O Protocols • Installed onto the Bus’s Controller Handle • Installed onto Bus’s Child Handles Examples: PCI SCSI Host Controllers PCI Fiber Channel Controllers

  9. EFI Drivers Drivers Service Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Does Not Manage Hardware • Provides Services to other Drivers • Does not support Driver Binding Protocol • Typically installs protocols in driver entry point • Creates One or More Service Handles • Produces Service Specific Protocols • Installed onto Service Handles Examples: EFI Decompress Protocol EFI Byte Code Virtual Machine Boot Integrity Services (BIS)

  10. EFI Drivers Drivers Initializing Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Typically Touches Hardware • Performs One Time Initialization Operations • Does Not Create Any Handles • Does Not Produce Any Protocols • Unloaded When Finished Examples: None

  11. EFI Drivers Drivers Root Bridge Drivers Service Drivers EFI Driver Model Initializing Drivers Hybrid Drivers Device Drivers Bus Drivers Root Bridge Drivers • Typically Manages Part of Core Chipset • Directly Touches Hardware • Creates One or More Root Bridge Handles • Produces Root Bridge I/O Protocols • Installed onto new Root a Bridge Handles Examples: PCI Host Bridge

  12. EFI Drivers FLASH What I/O Protocols are Consumed? • PCI Adapters • PCI I/O Protocol • Device Path Protocol • USB Peripherals • USB I/O Protocol • Device Path Protocol

  13. EFI Drivers FLASH What I/O Protocols are Produced? • SCSI • SCSI RAID • Fiber Channel • SCSI Pass Thru Protocol • and • Block I/O Protocol

  14. EFI Drivers FLASH What I/O Protocols are Produced? • Network Interface • Controller (NIC) • UNDI • and • Network Interface Identifier Protocol

  15. EFI Drivers Driver Design Checklist

  16. EFI Drivers Running EFI drivers • ConnectController() • Called from Boot Manager or during load • Precedence rules are applied • Context override • Platform override • Bus override • Version number • Order of which drivers are installed into handle database is not deterministic • DisconnectController() • Must test and implement Stop()

  17. EFI Drivers Implement, Test & Debug • See Backup Slides for Details • Required for IHVs • Optional for OEM/ODMs • Test Functions with EFI Shell Commands • Check for Leaks with EFI Shell Commands • Install EFI Compliant Operating System • Boot EFI Compliant Operating System • Debug Macros Identify Critical Failures • Use Same Techniques on all CPU Types • x86, x64, Itanium Processor Family, EBC

  18. Driver Guidelines Driver Guidelines • Don’t touch hardware in Driver Entry • Keep Supported() small and simple • Move complex I/O into Start() and Stop() • Start() / Stop() mirror each other • InstallProtocolInterface() UninstallProtocolInterface() • OpenProtocol() CloseProtocol • AllocatePages() FreePages() • AllocatePool() FreePool() • Driver Entry / Unload() mirror each other

  19. Driver Guidelines PCI Device Drivers • Always Call PciIo->Attributes() • Advertises Dual Address Cycle Capability • Save and Enable Attributes in Start() • Disable Attributes in Stop() • DMA – Bus Master Write Operations • Must call PciIo->Flush() • DMA – Setting Up with PciIo->Map() • Do Not Use Returned DeviceAddress • Not all chipsets have 1:1 bus/system mappings

  20. Driver Guidelines PCI Device Drivers – Start() Status = PciIo->Attributes( PciIo, EfiPciIoAttributeOperationGet, 0, &ControllerContext->OriginalPciIoAttributes ); if (EFI_ERROR (Status)) { // Error Handling } Status = PciIo->Attributes( PciIo, EfiPciIoAttributeOperationEnable, (EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY | EFI_PCI_IO_ATTRIBUTE_BUS_MASTER | EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE), 0, NULL ); if (EFI_ERROR (Status)) { // Error Handling } Save Original and Enable

  21. Driver Guidelines PCI Device Drivers – Stop() Status = PciIo->Attributes( PciIo, EfiPciIoAttributeOperationSet, &ControllerContext->OriginalPciIoAttributes NULL ); if (EFI_ERROR (Status)) { // Error Handling } Restore Original

  22. Driver Guidelines Preventing Alignment Faults VOID ScsiDeviceNodeInit ( IN OUT SCSI_DEVICE_PATH *ScsiDeviceNode, IN UINT16 Pun, IN UINT16 Lun ) { ScsiDeviceNode->Scsi.Header.Type = MESSAGING_DEVICE_PATH; ScsiDeviceNode->Scsi.Header.SubType = MSG_SCSI_DP; SetDevicePathNodeLength (&ScsiDeviceNode->Scsi.Header, sizeof(SCSI_DEVICE_PATH)); ScsiDeviceNode->Scsi.Pun = Pun; ScsiDeviceNode->Scsi.Lun = Lun; } BAD ScsiDeviceNode may not be aligned

  23. Driver Guidelines Preventing Alignment Faults VOID ScsiDeviceNodeInit ( IN OUT SCSI_DEVICE_PATH *ScsiDeviceNode, IN UINT16 Pun, IN UINT16 Lun ) { SCSI_DEVICE_PATH MyDeviceNode; MyDeviceNode.Scsi.Header.Type = MESSAGING_DEVICE_PATH; MyDeviceNode.Scsi.Header.SubType = MSG_SCSI_DP; SetDevicePathNodeLength (&MyDeviceNode.Scsi.Header, sizeof(SCSI_DEVICE_PATH)); MyDeviceNode.Scsi.Pun = Pun; MyDeviceNode.Scsi.Lun = Lun; gBS->CopyMem (ScsiDeviceNode, &MyDeviceNode, sizeof(SCSI_DEVICE_PATH)); } GOOD gBS->CopyMem() handles all alignments MyDeviceNode is aligned

  24. Driver Guidelines Use EFI Driver Library Functions OK CHILD_DEVICE Child; Status = gBS->AllocatePool ( EfiBootServicesData, sizeof (CHILD_DEVICE), &Child ); if (EFI_ERROR (Status)) { return Status; } gBS->SetMem (Child, sizeof (CHILD_DEVICE), 0); GOOD CHILD_DEVICE Child; Child = EfiLibAllocateZeroPool (sizeof (CHILD_DEVICE)); if (Child == NULL) { return EFI_OUT_OF_RESOURCES; } Library Functions Simplify Source Code Library Functions May Reduce Size

  25. Driver Guidelines EFI Device Paths Child->DevicePath = EfiAppendDevicePathNode ( ControllerDevicePath, ChildDevicePathNode ); if (Child->DevicePath == NULL) { return(EFI_OUT_OF_RESOURCES); } • EFI_DRIVER_BINDING.Start() • EFI_DRIVER_BINDING.Stop() gBS->FreePool (Child->DevicePath); Parent Device Path is Opaque Not Parsed by Bus Drivers

  26. Driver Guidelines Bus Walk Tips • Use LocateHandleBuffer(Bus I/O Protocol) • Do not scan PCI configuration space • Implement support for RemainingDevicePath • Highly recommended for all bus drivers • (i.e. SCSI, Fibre Channel, etc.) • Allows bus driver to bypass full enumeration. • Reduces boot time

  27. Driver Guidelines Component Name Protocol • Limit Lengths of Names to 40 Unicode Characters • Include Driver Name and Version Number • UNDI Driver (Network Interface Controller) • Typically the Name of the PCI Controller • MAC Node Produced by an UNDI Driver • Identify Location of Physical Connector on NIC • PCI Slots • Identify Physical Location of PCI Slots in the System • SCSI / SCSI RAID / Fiber Channel • Controller - Typically name of the PCI Controller • Channel - Identify Physical Location of the SCSI Channel • Disk - Use Results from INQUIRY Command

  28. Driver Guidelines Option ROM Size Reduction • Use EFI Compression • Compile with EFI Byte Code Compiler • Single Binary for x86, x64 and Itanium • Smaller than Itanium Binaries • Comparable to x86 Binaries • Compresses Well ~ 50%

  29. Driver Guidelines How To Improve Portability • Do Not Assume Max Number of Children • Do Not Use Fixed Memory Addresses • Do Not Use Assembly • Do Not Use Floating Point Arithmetic • Some Minor EBC Porting Considerations • Bus Drivers Should Support Producing 1 Child at a time if possible (improves boot performance) Driver Guidelines Improve Portability

  30. Driver Writer’s Guide EFI Driver Writer’s Guide • Captures Practical Experiences • Use as a Recipe Book • Must Read for all EFI Driver Developers • Living Document • Content Based on Industry Feedback • Updated as Techniques are Refined • Updated as New Technologies are Introduced

  31. Driver Writer’s Guide General Topics • Overview of EFI Concepts • EFI Services • Commonly Used by EFI Drivers • Rarely Used by EFI Drivers • Should Not Be Used by EFI Drivers • General Driver Design Guidelines • Classes of EFI Drivers • Driver Entry Point • Private Context Data Structures • EFI Driver Model Protocols

  32. Driver Writer’s Guide Platform Specific Topics • PCI Driver Guidelines • USB Driver Design Guidelines • SCSI Driver Design Guidelines • Size Optimizations • Speed Optimizations • Itanium Processor Family Considerations • EFI Byte Code Considerations • Building/Testing/Debugging EFI Drivers

  33. Driver Writer’s Guide Benefits of following EFIDriver Guidelines • Following EFI Driver Guidelines • Improves Portability, Quality, and Interoperability • Reduces Implementation Effort • May Increase Performance • May Reduce FLASH Overhead EFI Driver Writer’s Guide Helps Improve EFI Drivers

  34. Summary • Good Designs Save Time and Money • Many Tools Available to Test and Debug • Using Driver Guidelines Improves Portability • Compile in EBC to have one driver image to support x86, x64 and Itanium.

  35. Further Information • http://developer.intel.com/technology/EFI • EFI Web site for information, IDF presentations and EFI Driver Writer’s Guide • http://www.uefi.org • Website for Unified EFI Forum

  36. Back up • Required Materials for IHVs • Optional Materials for OEMs Back up Presentation

  37. Agenda • Introduction • EFI Shell • EFI Toolkit • 3rd Party Libraries

  38. What are EFI Applications? • EFI Applications extend firmware • No hardware dependence • No OS dependence • Portable across platforms • IA-32, Intel® 64, IA-64 • Enables rapid application development

  39. What is an EFI Application? • An EFI Loadable Image • Loaded by EFI loader just like drivers • Does not register protocols like drivers do • Consumes protocols • Typically user driven (exits when task completed) • Same set of interfaces available as drivers have • Can be used for • Platform diagnostics • Factory diagnostics • Utilities • Driver prototyping • ‘Platform’ applications

  40. EFI Shell • An EFI Application • Interactive Console Interface • Application Launch • Load EFI Drivers • Scripting Capability • Automatic execution of startup script file • Console redirection to files • Open Source located on http://efi-shell.tianocore.org project

  41. EFI Toolkit Components Source Included • Utilities • C Library • Network Stack • Platform Management • Compression • Database Useful tools for EFI application development Open Source located on http://efi-toolkit.tianocore.org project

  42. Programming Models • Native EFI Model • Uses only EFI constructs • Access to all EFI constructs • Smaller code size • Portability Model • Familiar programming interfaces • Easier to port ANSI/POSIX based programs • Larger binary image • A single program can use both

  43. EFI Toolkit Integration Database Utilities C Library Network Compress Management EFI API EFI Hardware

  44. C Library • FreeBSD Port • ANSI/POSIX compliant • System I/O - open(), read(), write(), close(), stat() • Standard I/O - fopen(), printf(), gets(), … • String/Char - strcmp(), isascii(), atoi(), … • Memory - malloc(), free(), realloc(), … • Time/Date - time(), asctime(), ctime(), … • Math - sqrt(), pow(), sin(), log(), …

  45. EFI Library • “Lite Weight” C Library like functions • String Functions • Memory Support Functions • CRC Support Functions • Text I/O Functions • Math Functions • Spin Lock Functions • Specific EFI functions • Handle and Protocol Support Functions • Device Path Support Functions

  46. Network Components • Port of FreeBSD TCP/IP stack • Supports standard protocols • IPv4, ICMP, ARP, UDP, TCP • Socket library interface • Implemented as an EFI protocol

  47. Miscellaneous • SMBIOS Library • Library routines for parsing SMBIOS tables • Database • btree • Hashing • Compression • General purpose compression/decompression • Gzip functionality

  48. Utilities • Network utilities • FTP client and server, ping • Text editor • Scripting interpreter (Python) • Sample applications

  49. EFI Hello.c #include "efi.h" EFI_STATUS InitializeHelloApplication ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { UINTN Index; SystemTable->ConOut->OutputString(SystemTable->ConOut, L”Hello application started\n"); SystemTable->ConOut->OutputString(SystemTable->ConOut, L"\n\r\n\r\n\rHit any key to exit this image\n\r"); SystemTable->BootServices->WaitForEvent( 1, &(SystemTable->ConIn->WaitForKey), &Index); SystemTable->ConOut->OutputString(SystemTable->ConOut, L"\n\r\n\r"); return EFI_SUCCESS; }

  50. EFI Library Hello.c #include "efi.h" #include "efilib.h" EFI_STATUS InitializeHelloLibApplication ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { InitializeLib (ImageHandle, SystemTable); Print(L"\n\n\nHelloLib application started\n\n\n"); Print(L"\nHit any key to exit this image\n"); WaitForSingleEvent(ST->ConIn->WaitForKey,0); ST->ConOut->OutputString (ST->ConOut, L"\n\r\n\r"); return EFI_SUCCESS; }

More Related