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