Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tel : 69589584 同济大学软件学院 UEFI 与固件程序设计.

Similar presentations


Presentation on theme: "Tel : 69589584 同济大学软件学院 UEFI 与固件程序设计."— Presentation transcript:

1 Tel : 同济大学软件学院 UEFI 与固件程序设计

2 Agenda EFI Driver Development EFI Application Development

3 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 EFI Drivers

4 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) EFI Drivers

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

6 Device 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 EFI Drivers Drivers Service Drivers Initializing Drivers Root Bridge Drivers EFI Driver Model Device Drivers Bus Drivers Hybrid Drivers

7 Bus 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 Drivers Service Drivers Initializing Drivers Root Bridge Drivers EFI Driver Model Device Drivers Bus Drivers Hybrid Drivers EFI Drivers

8 Hybrid 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 Drivers Service Drivers Initializing Drivers Root Bridge Drivers EFI Driver Model Device Drivers Bus Drivers Hybrid Drivers EFI Drivers

9 Service 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) EFI Drivers Drivers Service Drivers Initializing Drivers Root Bridge Drivers EFI Driver Model Device Drivers Bus Drivers Hybrid Drivers

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

11 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 EFI Drivers Drivers Service Drivers Initializing Drivers Root Bridge Drivers EFI Driver Model Device Drivers Bus Drivers Hybrid Drivers

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

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

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

15 Driver Design Checklist EFI Drivers

16  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() Running EFI drivers EFI Drivers

17 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 EFI Drivers

18 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 Driver Guidelines

19 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 Driver Guidelines

20 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 Driver Guidelines

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

22 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 Driver Guidelines

23 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 Driver Guidelines

24 Use EFI Driver Library Functions 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 CHILD_DEVICE Child; Status = gBS->AllocatePool ( EfiBootServicesData, sizeof (CHILD_DEVICE), &Child ); if (EFI_ERROR (Status)) { return Status; } gBS->SetMem (Child, sizeof (CHILD_DEVICE), 0); OK GOOD Driver Guidelines

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

26 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 Driver Guidelines

27 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 Driver Guidelines

28 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% Driver Guidelines

29 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 Driver Guidelines

30 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 Driver Writer’s Guide

31 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 Driver Writer’s Guide

32 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 Driver Writer’s Guide

33 Benefits of following EFI Driver 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 Driver Writer’s Guide

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   EFI Web site for information, IDF presentations and EFI Driver Writer ’ s Guide   Website for Unified EFI Forum

36 Back up  Required Materials for IHVs  Optional Materials for OEMs

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 projecthttp://efi-shell.tianocore.org

41 EFI Toolkit Components  Utilities  C Library  Network Stack  Platform Management  Compression  Database Useful tools for EFI application development Useful tools for EFI application development Source Included Open Source located on 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 Hardware EFI Utilities C Library Network Compress Management EFI API Database

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  Port of FreeBSD TCP/IP stack  Supports standard protocols  IPv4, ICMP, ARP, UDP, TCP  Socket library interface  Implemented as an EFI protocol Network Components

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

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

49 #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; } EFI Hello.c

50 #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; } EFI Library Hello.c

51 C Library Hello.c #include EFI_STATUS InitializeHelloLibCApplication ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { InitializeLib(ImageHandle, SystemTable); printf("Hello LibC application started\n\n\n"); printf("Hit C/R to exit this image\n"); return( getchar() ); }

52 C Library Hello.c #include int main (int argc, char **argv ) { printf("Hello LibC application started\n\n\n"); printf("Hit C/R to exit this image\n"); return( getchar() ); }

53  No direct support  No Global constructors and destructors  New and Delete can be mapped to malloc/free C++ Support

54 Portable Embedded Graphics  Portable Embedded Graphics  Portable graphics library for EFI  Similar windowing components (widgets)  Dialog boxes  Progress bars, scroll bars  Text boxes  Window Management  Fonts  Bitmaps, JPEG, …  Contact Swell Software 

55 PEG Components

56 Summary  EFI Applications extend firmware  Provides system independence in the pre-boot space  Hardware  Operating System  Platform  IA-32, Intel ® 64, and IA-64  Large library support  EFI Shell provides convenient launch point

57 Further Information  https://efi-toolkit.tianocore.org/ https://efi-toolkit.tianocore.org/  EFI Web site for EFI Toolkit download  https://www.TianoCore.org https://www.TianoCore.org  Website for EFI open source resources  EFI Developer Kit (EDK) EFI Developer Kit  Nt32 emulation environment  EFI Shell Source EFI Shell Source   Portable Embedded Graphics toolkit   UEFI Specification


Download ppt "Tel : 69589584 同济大学软件学院 UEFI 与固件程序设计."

Similar presentations


Ads by Google