]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/BdsDxe/BootMaint/BmLib.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / BootMaint / BmLib.c
CommitLineData
5c08e117 1/** @file\r
2 Utility routines used by boot maintenance modules.\r
3\r
0a6f4824 4Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
c0a00b14 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
5c08e117 6\r
7**/\r
8\r
9#include "BootMaint.h"\r
10\r
5c08e117 11/**\r
12\r
13 Function opens and returns a file handle to the root directory of a volume.\r
14\r
15 @param DeviceHandle A handle for a device\r
16\r
17 @return A valid file handle or NULL is returned\r
18\r
19**/\r
20EFI_FILE_HANDLE\r
21EfiLibOpenRoot (\r
22 IN EFI_HANDLE DeviceHandle\r
23 )\r
24{\r
25 EFI_STATUS Status;\r
26 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
27 EFI_FILE_HANDLE File;\r
28\r
29 File = NULL;\r
30\r
31 //\r
32 // File the file system interface to the device\r
33 //\r
34 Status = gBS->HandleProtocol (\r
35 DeviceHandle,\r
36 &gEfiSimpleFileSystemProtocolGuid,\r
37 (VOID *) &Volume\r
38 );\r
39\r
40 //\r
41 // Open the root directory of the volume\r
42 //\r
43 if (!EFI_ERROR (Status)) {\r
44 Status = Volume->OpenVolume (\r
45 Volume,\r
46 &File\r
47 );\r
48 }\r
49 //\r
50 // Done\r
51 //\r
52 return EFI_ERROR (Status) ? NULL : File;\r
53}\r
54\r
55/**\r
56\r
57 Helper function called as part of the code needed\r
58 to allocate the proper sized buffer for various\r
59 EFI interfaces.\r
60\r
61\r
62 @param Status Current status\r
63 @param Buffer Current allocated buffer, or NULL\r
64 @param BufferSize Current buffer size needed\r
65\r
66 @retval TRUE if the buffer was reallocated and the caller\r
67 should try the API again.\r
68 @retval FALSE The caller should not call this function again.\r
69\r
70**/\r
71BOOLEAN\r
72EfiGrowBuffer (\r
73 IN OUT EFI_STATUS *Status,\r
74 IN OUT VOID **Buffer,\r
75 IN UINTN BufferSize\r
76 )\r
77{\r
78 BOOLEAN TryAgain;\r
79\r
80 //\r
81 // If this is an initial request, buffer will be null with a new buffer size\r
82 //\r
83 if ((*Buffer == NULL) && (BufferSize != 0)) {\r
84 *Status = EFI_BUFFER_TOO_SMALL;\r
85 }\r
86 //\r
87 // If the status code is "buffer too small", resize the buffer\r
88 //\r
89 TryAgain = FALSE;\r
90 if (*Status == EFI_BUFFER_TOO_SMALL) {\r
91\r
92 if (*Buffer != NULL) {\r
93 FreePool (*Buffer);\r
94 }\r
95\r
96 *Buffer = AllocateZeroPool (BufferSize);\r
97\r
98 if (*Buffer != NULL) {\r
99 TryAgain = TRUE;\r
100 } else {\r
101 *Status = EFI_OUT_OF_RESOURCES;\r
102 }\r
103 }\r
104 //\r
105 // If there's an error, free the buffer\r
106 //\r
107 if (!TryAgain && EFI_ERROR (*Status) && (*Buffer != NULL)) {\r
108 FreePool (*Buffer);\r
109 *Buffer = NULL;\r
110 }\r
111\r
112 return TryAgain;\r
113}\r
114\r
115/**\r
116 Function returns the value of the specified variable.\r
117\r
118\r
119 @param Name A Null-terminated Unicode string that is\r
120 the name of the vendor's variable.\r
121 @param VendorGuid A unique identifier for the vendor.\r
122\r
123 @return The payload of the variable.\r
124 @retval NULL If the variable can't be read.\r
125\r
126**/\r
127VOID *\r
128EfiLibGetVariable (\r
129 IN CHAR16 *Name,\r
130 IN EFI_GUID *VendorGuid\r
131 )\r
132{\r
133 UINTN VarSize;\r
134\r
135 return BdsLibGetVariableAndSize (Name, VendorGuid, &VarSize);\r
136}\r
137\r
138/**\r
139 Function deletes the variable specified by VarName and VarGuid.\r
140\r
141 @param VarName A Null-terminated Unicode string that is\r
142 the name of the vendor's variable.\r
0a6f4824 143\r
5c08e117 144 @param VarGuid A unique identifier for the vendor.\r
145\r
146 @retval EFI_SUCCESS The variable was found and removed\r
147 @retval EFI_UNSUPPORTED The variable store was inaccessible\r
148 @retval EFI_OUT_OF_RESOURCES The temporary buffer was not available\r
149 @retval EFI_NOT_FOUND The variable was not found\r
150\r
151**/\r
152EFI_STATUS\r
153EfiLibDeleteVariable (\r
154 IN CHAR16 *VarName,\r
155 IN EFI_GUID *VarGuid\r
156 )\r
157{\r
158 VOID *VarBuf;\r
159 EFI_STATUS Status;\r
160\r
161 VarBuf = EfiLibGetVariable (VarName, VarGuid);\r
162 Status = EFI_NOT_FOUND;\r
163\r
164 if (VarBuf != NULL) {\r
165 //\r
166 // Delete variable from Storage\r
167 //\r
16e5944a
RN
168 Status = gRT->SetVariable (\r
169 VarName,\r
170 VarGuid,\r
171 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
172 0,\r
173 NULL\r
174 );\r
69fc8f08
RN
175 //\r
176 // Deleting variable with current variable implementation shouldn't fail.\r
177 //\r
178 ASSERT_EFI_ERROR (Status);\r
5c08e117 179 FreePool (VarBuf);\r
180 }\r
181\r
182 return Status;\r
183}\r
184\r
185/**\r
186\r
187 Function gets the file system information from an open file descriptor,\r
188 and stores it in a buffer allocated from pool.\r
189\r
190\r
191 @param FHand The file handle.\r
192\r
193 @return A pointer to a buffer with file information.\r
194 @retval NULL is returned if failed to get Vaolume Label Info.\r
195\r
196**/\r
197EFI_FILE_SYSTEM_VOLUME_LABEL *\r
198EfiLibFileSystemVolumeLabelInfo (\r
199 IN EFI_FILE_HANDLE FHand\r
200 )\r
201{\r
202 EFI_STATUS Status;\r
203 EFI_FILE_SYSTEM_VOLUME_LABEL *Buffer;\r
204 UINTN BufferSize;\r
205 //\r
206 // Initialize for GrowBuffer loop\r
207 //\r
208 Buffer = NULL;\r
209 BufferSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 200;\r
210\r
211 //\r
212 // Call the real function\r
213 //\r
214 while (EfiGrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
215 Status = FHand->GetInfo (\r
216 FHand,\r
217 &gEfiFileSystemVolumeLabelInfoIdGuid,\r
218 &BufferSize,\r
219 Buffer\r
220 );\r
221 }\r
222\r
223 return Buffer;\r
224}\r
225\r
226/**\r
227 Duplicate a string.\r
228\r
229 @param Src The source.\r
230\r
231 @return A new string which is duplicated copy of the source.\r
232 @retval NULL If there is not enough memory.\r
233\r
234**/\r
235CHAR16 *\r
236EfiStrDuplicate (\r
237 IN CHAR16 *Src\r
238 )\r
239{\r
240 CHAR16 *Dest;\r
241 UINTN Size;\r
242\r
243 Size = StrSize (Src);\r
244 Dest = AllocateZeroPool (Size);\r
245 ASSERT (Dest != NULL);\r
246 if (Dest != NULL) {\r
247 CopyMem (Dest, Src, Size);\r
248 }\r
249\r
250 return Dest;\r
251}\r
252\r
253/**\r
254\r
255 Function gets the file information from an open file descriptor, and stores it\r
256 in a buffer allocated from pool.\r
257\r
258 @param FHand File Handle.\r
259\r
260 @return A pointer to a buffer with file information or NULL is returned\r
261\r
262**/\r
263EFI_FILE_INFO *\r
264EfiLibFileInfo (\r
265 IN EFI_FILE_HANDLE FHand\r
266 )\r
267{\r
268 EFI_STATUS Status;\r
269 EFI_FILE_INFO *Buffer;\r
270 UINTN BufferSize;\r
271\r
272 //\r
273 // Initialize for GrowBuffer loop\r
274 //\r
275 Buffer = NULL;\r
276 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
277\r
278 //\r
279 // Call the real function\r
280 //\r
281 while (EfiGrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
282 Status = FHand->GetInfo (\r
283 FHand,\r
284 &gEfiFileInfoGuid,\r
285 &BufferSize,\r
286 Buffer\r
287 );\r
288 }\r
289\r
290 return Buffer;\r
291}\r
292\r
293/**\r
294 Function is used to determine the number of device path instances\r
295 that exist in a device path.\r
296\r
297\r
298 @param DevicePath A pointer to a device path data structure.\r
299\r
300 @return This function counts and returns the number of device path instances\r
301 in DevicePath.\r
302\r
303**/\r
304UINTN\r
305EfiDevicePathInstanceCount (\r
306 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
307 )\r
308{\r
309 UINTN Count;\r
310 UINTN Size;\r
311\r
312 Count = 0;\r
7ec9caab 313 while (GetNextDevicePathInstance (&DevicePath, &Size) != NULL) {\r
5c08e117 314 Count += 1;\r
315 }\r
316\r
317 return Count;\r
318}\r
319\r
5c08e117 320\r
5c08e117 321/**\r
0a6f4824 322 Get a string from the Data Hub record based on\r
5c08e117 323 a device path.\r
324\r
325 @param DevPath The device Path.\r
326\r
327 @return A string located from the Data Hub records based on\r
328 the device path.\r
329 @retval NULL If failed to get the String from Data Hub.\r
330\r
331**/\r
332UINT16 *\r
333EfiLibStrFromDatahub (\r
334 IN EFI_DEVICE_PATH_PROTOCOL *DevPath\r
335 )\r
336{\r
5c08e117 337 return NULL;\r
338}\r
be9304f3
ED
339\r
340/**\r
341\r
342 Find the first instance of this Protocol\r
343 in the system and return it's interface.\r
344\r
345\r
346 @param ProtocolGuid Provides the protocol to search for\r
347 @param Interface On return, a pointer to the first interface\r
348 that matches ProtocolGuid\r
349\r
350 @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found\r
351 @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid\r
352\r
353**/\r
354EFI_STATUS\r
355EfiLibLocateProtocol (\r
356 IN EFI_GUID *ProtocolGuid,\r
357 OUT VOID **Interface\r
358 )\r
359{\r
360 EFI_STATUS Status;\r
361\r
362 Status = gBS->LocateProtocol (\r
363 ProtocolGuid,\r
364 NULL,\r
365 (VOID **) Interface\r
366 );\r
367 return Status;\r
368}\r
369\r