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