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