| 1 |
/* |
| 2 |
* Copyright (C) 2013 David Decotigny <[email protected]> |
| 3 |
* |
| 4 |
* Sample EFI shell session, together with drv0_use.efi: |
| 5 |
* |
| 6 |
* # Loading first instance: |
| 7 |
* |
| 8 |
* fs0:\> load drv0.efi |
| 9 |
* Driver instance loaded successfully. |
| 10 |
* load: Image fs0:\drv0.efi loaded at 2FD7C000 - Success |
| 11 |
* |
| 12 |
* # Testing 1st instance: |
| 13 |
* |
| 14 |
* fs0:\> drv0_use.efi |
| 15 |
* Playing with driver instance 0... |
| 16 |
* Hello Sample UEFI Driver! |
| 17 |
* Hello was called 1 time(s). |
| 18 |
* |
| 19 |
* fs0:\> drv0_use.efi |
| 20 |
* Playing with driver instance 0... |
| 21 |
* Hello Sample UEFI Driver! |
| 22 |
* Hello was called 2 time(s). |
| 23 |
* |
| 24 |
* # Loading another instance: |
| 25 |
* |
| 26 |
* fs0:\> load drv0.efi |
| 27 |
* Driver instance loaded successfully. |
| 28 |
* load: Image fs0:\drv0.efi loaded at 2FD6D000 - Success |
| 29 |
* |
| 30 |
* # Using both instances: |
| 31 |
* |
| 32 |
* fs0:\> drv0_use.efi |
| 33 |
* Playing with driver instance 0... |
| 34 |
* Hello Sample UEFI Driver! |
| 35 |
* Hello was called 3 time(s). |
| 36 |
* Playing with driver instance 1... |
| 37 |
* Hello Sample UEFI Driver! |
| 38 |
* Hello was called 1 time(s). |
| 39 |
* |
| 40 |
* fs0:\> drv0_use.efi |
| 41 |
* Playing with driver instance 0... |
| 42 |
* Hello Sample UEFI Driver! |
| 43 |
* Hello was called 4 time(s). |
| 44 |
* Playing with driver instance 1... |
| 45 |
* Hello Sample UEFI Driver! |
| 46 |
* Hello was called 2 time(s). |
| 47 |
* |
| 48 |
* # Removing 1st instance: |
| 49 |
* |
| 50 |
* fs0:\> dh |
| 51 |
* Handle dump |
| 52 |
* 1: Image(DxeCore) |
| 53 |
* [...] |
| 54 |
* 79: Image(\/drv0.efi) ImageDevPath (..A,0x800,0x17F7DF)/\/drv0.efi) |
| 55 |
* 7A: Image(\/drv0.efi) ImageDevPath (..A,0x800,0x17F7DF)/\/drv0.efi) |
| 56 |
* |
| 57 |
* fs0:\> unload 79 |
| 58 |
* 79: Image(\/drv0.efi) ImageDevPath (..A,0x800,0x17F7DF)/\/drv0.efi) |
| 59 |
* Unload driver image (y/n)? y |
| 60 |
* Driver instance unloaded. |
| 61 |
* unload: Success |
| 62 |
* |
| 63 |
* # Only 2nd instance remaining: |
| 64 |
* |
| 65 |
* fs0:\> drv0_use.efi |
| 66 |
* Playing with driver instance 0... |
| 67 |
* Hello Sample UEFI Driver! |
| 68 |
* Hello was called 3 time(s). |
| 69 |
* |
| 70 |
* # Removing 2nd/last instance: |
| 71 |
* |
| 72 |
* fs0:\> dh |
| 73 |
* Handle dump |
| 74 |
* 1: Image(DxeCore) |
| 75 |
* [...] |
| 76 |
* 79: Image(\/drv0.efi) ImageDevPath (..A,0x800,0x17F7DF)/\/drv0.efi) |
| 77 |
* |
| 78 |
* fs0:\> unload 79 |
| 79 |
* 79: Image(\/drv0.efi) ImageDevPath (..A,0x800,0x17F7DF)/\/drv0.efi) |
| 80 |
* Unload driver image (y/n)? y |
| 81 |
* Driver instance unloaded. |
| 82 |
* unload: Success |
| 83 |
* |
| 84 |
* # Expect error: no other drv0 instance left |
| 85 |
* |
| 86 |
* fs0:\> drv0_use.efi |
| 87 |
* Error looking up handles for proto: 14 |
| 88 |
*/ |
| 89 |
|
| 90 |
#include <efi.h> |
| 91 |
#include <efilib.h> |
| 92 |
#include "drv0.h" |
| 93 |
|
| 94 |
|
| 95 |
static const EFI_GUID GnuEfiAppsDrv0ProtocolGuid |
| 96 |
= GNU_EFI_APPS_DRV0_PROTOCOL_GUID; |
| 97 |
|
| 98 |
static struct { |
| 99 |
GNU_EFI_APPS_DRV0_PROTOCOL Proto; |
| 100 |
UINTN Counter; |
| 101 |
} InternalGnuEfiAppsDrv0ProtocolData; |
| 102 |
|
| 103 |
|
| 104 |
static |
| 105 |
EFI_STATUS |
| 106 |
EFI_FUNCTION |
| 107 |
Drv0SayHello( |
| 108 |
IN const CHAR16 *HelloWho |
| 109 |
) |
| 110 |
{ |
| 111 |
if (! HelloWho) |
| 112 |
return EFI_INVALID_PARAMETER; |
| 113 |
|
| 114 |
Print(L"Hello %s!\n", HelloWho); |
| 115 |
InternalGnuEfiAppsDrv0ProtocolData.Counter ++; |
| 116 |
return EFI_SUCCESS; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
static |
| 121 |
EFI_STATUS |
| 122 |
EFI_FUNCTION |
| 123 |
Drv0GetNumberOfHello( |
| 124 |
OUT UINTN *NumberOfHello |
| 125 |
) |
| 126 |
{ |
| 127 |
if (! NumberOfHello) |
| 128 |
return EFI_INVALID_PARAMETER; |
| 129 |
|
| 130 |
*NumberOfHello = InternalGnuEfiAppsDrv0ProtocolData.Counter; |
| 131 |
return EFI_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
static |
| 136 |
EFI_STATUS |
| 137 |
EFI_FUNCTION |
| 138 |
Drv0Unload(IN EFI_HANDLE ImageHandle) |
| 139 |
{ |
| 140 |
LibUninstallProtocolInterfaces(ImageHandle, |
| 141 |
&GnuEfiAppsDrv0ProtocolGuid, |
| 142 |
&InternalGnuEfiAppsDrv0ProtocolData.Proto, |
| 143 |
NULL); |
| 144 |
Print(L"Driver instance unloaded.\n", ImageHandle); |
| 145 |
return EFI_SUCCESS; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
EFI_STATUS |
| 150 |
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SysTab) |
| 151 |
{ |
| 152 |
EFI_STATUS Status; |
| 153 |
EFI_LOADED_IMAGE *LoadedImage = NULL; |
| 154 |
|
| 155 |
InitializeLib(ImageHandle, SysTab); |
| 156 |
|
| 157 |
/* Initialize global protocol definition + data */ |
| 158 |
InternalGnuEfiAppsDrv0ProtocolData.Proto.SayHello |
| 159 |
= (GNU_EFI_APPS_DRV0_SAY_HELLO) Drv0SayHello; |
| 160 |
InternalGnuEfiAppsDrv0ProtocolData.Proto.GetNumberOfHello |
| 161 |
= (GNU_EFI_APPS_DRV0_GET_NUMBER_OF_HELLO) Drv0GetNumberOfHello; |
| 162 |
InternalGnuEfiAppsDrv0ProtocolData.Counter = 0; |
| 163 |
|
| 164 |
/* Grab handle to this image: we'll attach our proto instance to it */ |
| 165 |
Status = uefi_call_wrapper(BS->OpenProtocol, 6, |
| 166 |
ImageHandle, &LoadedImageProtocol, |
| 167 |
(void**)&LoadedImage, ImageHandle, |
| 168 |
NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 169 |
if (EFI_ERROR(Status)) { |
| 170 |
Print(L"Could not open loaded image protocol: %d\n", Status); |
| 171 |
return Status; |
| 172 |
} |
| 173 |
|
| 174 |
/* Attach our proto to the current driver image */ |
| 175 |
Status = LibInstallProtocolInterfaces( |
| 176 |
&ImageHandle, &GnuEfiAppsDrv0ProtocolGuid, |
| 177 |
&InternalGnuEfiAppsDrv0ProtocolData.Proto, NULL); |
| 178 |
if (EFI_ERROR(Status)) { |
| 179 |
Print(L"Error registering driver instance: %d\n", Status); |
| 180 |
return Status; |
| 181 |
} |
| 182 |
|
| 183 |
/* Register Unload callback, used to unregister current protocol |
| 184 |
* instance from system */ |
| 185 |
LoadedImage->Unload = (EFI_IMAGE_UNLOAD)Drv0Unload; |
| 186 |
|
| 187 |
Print(L"Driver instance loaded successfully.\n"); |
| 188 |
return EFI_SUCCESS; /* at this point, this instance stays resident |
| 189 |
* until image is unloaded, eg. with shell's unload, |
| 190 |
* ExitBootServices() */ |
| 191 |
} |