OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
ProtocolSupport.c
Go to the documentation of this file.
1
15#include <Uefi.h>
16
17#include <Library/DebugLib.h>
18#include <Library/MemoryAllocationLib.h>
19#include <Library/UefiLib.h>
20#include <Library/UefiBootServicesTableLib.h>
21#include <Protocol/LoadedImage.h>
22#include <Protocol/ShellParameters.h>
23
24EFI_STATUS
26 OUT UINTN *Argc,
27 OUT CHAR16 ***Argv
28 )
29{
30 STATIC CHAR16 *StArgv[2] = { L"Self", NULL };
31
32 EFI_STATUS Status;
33 EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;
34 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
35
36 Status = gBS->HandleProtocol (
39 (VOID **)&ShellParameters
40 );
41 if (!EFI_ERROR (Status)) {
42 *Argc = ShellParameters->Argc;
43 *Argv = ShellParameters->Argv;
44 return EFI_SUCCESS;
45 }
46
47 Status = gBS->HandleProtocol (
50 (VOID **)&LoadedImage
51 );
52
53 if (EFI_ERROR (Status)) {
54 DEBUG ((DEBUG_WARN, "OCM: LoadedImage cannot be located - %r\n", Status));
55 }
56
57 if (EFI_ERROR (Status) || (LoadedImage->LoadOptions == NULL)) {
58 *Argc = 1;
59 *Argv = StArgv;
60 return EFI_SUCCESS;
61 }
62
63 StArgv[1] = LoadedImage->LoadOptions;
64 *Argc = ARRAY_SIZE (StArgv);
65 *Argv = StArgv;
66 return EFI_SUCCESS;
67}
68
69EFI_STATUS
71 EFI_GUID *Protocol
72 )
73{
74 EFI_STATUS Status;
75 EFI_HANDLE *Handles;
76 UINTN Index;
77 UINTN NoHandles;
78 VOID *OriginalProto;
79
80 Status = gBS->LocateHandleBuffer (
81 ByProtocol,
82 Protocol,
83 NULL,
84 &NoHandles,
85 &Handles
86 );
87
88 if (Status == EFI_NOT_FOUND) {
89 return EFI_SUCCESS;
90 }
91
92 if (EFI_ERROR (Status)) {
93 return Status;
94 }
95
96 for (Index = 0; Index < NoHandles; ++Index) {
97 Status = gBS->HandleProtocol (
98 Handles[Index],
99 Protocol,
100 &OriginalProto
101 );
102
103 if (EFI_ERROR (Status)) {
104 break;
105 }
106
107 Status = gBS->UninstallProtocolInterface (
108 Handles[Index],
109 Protocol,
110 OriginalProto
111 );
112
113 if (EFI_ERROR (Status)) {
114 break;
115 }
116 }
117
118 FreePool (Handles);
119
120 return Status;
121}
122
123EFI_STATUS
125 IN EFI_HANDLE Handle,
126 IN EFI_GUID *Protocol,
127 OUT VOID **Interface
128 )
129{
130 EFI_STATUS Status;
131
132 Status = gBS->HandleProtocol (
133 Handle,
134 Protocol,
135 Interface
136 );
137 if (EFI_ERROR (Status)) {
138 Status = gBS->LocateProtocol (
139 Protocol,
140 NULL,
141 Interface
142 );
143 }
144
145 return Status;
146}
147
148UINTN
150 IN EFI_GUID *Protocol
151 )
152{
153 EFI_STATUS Status;
154 UINTN HandleCount;
155 EFI_HANDLE *HandleBuffer;
156
157 HandleCount = 0;
158
159 Status = gBS->LocateHandleBuffer (
160 ByProtocol,
161 Protocol,
162 NULL,
163 &HandleCount,
164 &HandleBuffer
165 );
166 if (EFI_ERROR (Status)) {
167 //
168 // No instance can be found on error.
169 //
170 return 0;
171 }
172
173 FreePool (HandleBuffer);
174
175 return HandleCount;
176}
177
178VOID *
180 IN EFI_GUID *Protocol,
181 IN UINTN ErrorLevel,
182 IN CONST CHAR8 *CallerName OPTIONAL,
183 IN CONST CHAR8 *ProtocolName OPTIONAL
184 )
185{
186 EFI_STATUS Status;
187 VOID *Instance;
188
189 Status = gBS->LocateProtocol (
190 Protocol,
191 NULL,
192 (VOID **)&Instance
193 );
194
195 if (EFI_ERROR (Status)) {
196 Instance = NULL;
197 if (ErrorLevel != 0) {
198 if (ProtocolName != NULL) {
199 DEBUG ((ErrorLevel, "OCM: %a cannot get protocol %s - %r\n", CallerName, ProtocolName, Status));
200 } else {
201 DEBUG ((ErrorLevel, "OCM: %a cannot get protocol %g - %r\n", CallerName, Protocol, Status));
202 }
203 }
204 }
205
206 return Instance;
207}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
APPLE_EVENT_HANDLE Handle
Definition OcTypingLib.h:45
EFI_STATUS OcUninstallAllProtocolInstances(EFI_GUID *Protocol)
EFI_STATUS GetArguments(OUT UINTN *Argc, OUT CHAR16 ***Argv)
VOID * OcGetProtocol(IN EFI_GUID *Protocol, IN UINTN ErrorLevel, IN CONST CHAR8 *CallerName OPTIONAL, IN CONST CHAR8 *ProtocolName OPTIONAL)
EFI_STATUS OcHandleProtocolFallback(IN EFI_HANDLE Handle, IN EFI_GUID *Protocol, OUT VOID **Interface)
UINTN OcCountProtocolInstances(IN EFI_GUID *Protocol)
EFI_GUID gEfiShellParametersProtocolGuid
EFI_GUID gEfiLoadedImageProtocolGuid