]> code.delx.au - refind/blob - EfiLib/BmLib.c
TianoCore build support; new use_graphics_for refind.conf token
[refind] / EfiLib / BmLib.c
1 /** @file
2 Utility routines used by boot maintenance modules.
3
4 Copyright (c) 2004 - 2009, 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 "Platform.h"
16 /**
17
18 Find the first instance of this Protocol
19 in the system and return it's interface.
20
21
22 @param ProtocolGuid Provides the protocol to search for
23 @param Interface On return, a pointer to the first interface
24 that matches ProtocolGuid
25
26 @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found
27 @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid
28
29 **/
30 EFI_STATUS
31 EfiLibLocateProtocol (
32 IN EFI_GUID *ProtocolGuid,
33 OUT VOID **Interface
34 )
35 {
36 EFI_STATUS Status;
37
38 Status = gBS->LocateProtocol (
39 ProtocolGuid,
40 NULL,
41 (VOID **) Interface
42 );
43 return Status;
44 }
45
46 /**
47
48 Function opens and returns a file handle to the root directory of a volume.
49
50 @param DeviceHandle A handle for a device
51
52 @return A valid file handle or NULL is returned
53
54 **/
55 EFI_FILE_HANDLE
56 EfiLibOpenRoot (
57 IN EFI_HANDLE DeviceHandle
58 )
59 {
60 EFI_STATUS Status;
61 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
62 EFI_FILE_HANDLE File;
63
64 File = NULL;
65
66 //
67 // File the file system interface to the device
68 //
69 Status = gBS->HandleProtocol (
70 DeviceHandle,
71 &gEfiSimpleFileSystemProtocolGuid,
72 (VOID **) &Volume
73 );
74
75 //
76 // Open the root directory of the volume
77 //
78 if (!EFI_ERROR (Status)) {
79 Status = Volume->OpenVolume (
80 Volume,
81 &File
82 );
83 }
84 //
85 // Done
86 //
87 return EFI_ERROR (Status) ? NULL : File;
88 }
89
90 /**
91
92 Function gets the file system information from an open file descriptor,
93 and stores it in a buffer allocated from pool.
94
95
96 @param FHand The file handle.
97
98 @return A pointer to a buffer with file information.
99 @retval NULL is returned if failed to get Volume Label Info.
100
101 **/
102 EFI_FILE_SYSTEM_VOLUME_LABEL *
103 EfiLibFileSystemVolumeLabelInfo (
104 IN EFI_FILE_HANDLE FHand
105 )
106 {
107 EFI_STATUS Status;
108 EFI_FILE_SYSTEM_VOLUME_LABEL *VolumeInfo = NULL;
109 UINTN Size = 0;
110
111 Status = FHand->GetInfo (FHand, &gEfiFileSystemVolumeLabelInfoIdGuid, &Size, VolumeInfo);
112 if (Status == EFI_BUFFER_TOO_SMALL) {
113 VolumeInfo = AllocateZeroPool (Size);
114 Status = FHand->GetInfo (FHand, &gEfiFileSystemVolumeLabelInfoIdGuid, &Size, VolumeInfo);
115 }
116
117 return EFI_ERROR(Status)?NULL:VolumeInfo;
118 }
119
120 /**
121 Duplicate a string.
122
123 @param Src The source.
124
125 @return A new string which is duplicated copy of the source.
126 @retval NULL If there is not enough memory.
127
128 **/
129 CHAR16 *
130 EfiStrDuplicate (
131 IN CHAR16 *Src
132 )
133 {
134 CHAR16 *Dest;
135 UINTN Size;
136
137 Size = StrSize (Src); //at least 2bytes
138 Dest = AllocateZeroPool (Size);
139 // ASSERT (Dest != NULL);
140 if (Dest != NULL) {
141 CopyMem (Dest, Src, Size);
142 }
143
144 return Dest;
145 }
146 //Compare strings case insensitive
147 INTN
148 EFIAPI
149 StriCmp (
150 IN CONST CHAR16 *FirstString,
151 IN CONST CHAR16 *SecondString
152 )
153 {
154
155 while ((*FirstString != L'\0') && ((*FirstString & ~0x20) == (*SecondString & ~0x20))) {
156 FirstString++;
157 SecondString++;
158 }
159 return *FirstString - *SecondString;
160 }
161
162
163 /**
164
165 Function gets the file information from an open file descriptor, and stores it
166 in a buffer allocated from pool.
167
168 @param FHand File Handle.
169
170 @return A pointer to a buffer with file information or NULL is returned
171
172 **/
173 EFI_FILE_INFO *
174 EfiLibFileInfo (
175 IN EFI_FILE_HANDLE FHand
176 )
177 {
178 EFI_STATUS Status;
179 EFI_FILE_INFO *FileInfo = NULL;
180 UINTN Size = 0;
181
182 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
183 if (Status == EFI_BUFFER_TOO_SMALL) {
184 FileInfo = AllocateZeroPool (Size);
185 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
186 }
187
188 return EFI_ERROR(Status)?NULL:FileInfo;
189 }
190
191 EFI_FILE_SYSTEM_INFO *
192 EfiLibFileSystemInfo (
193 IN EFI_FILE_HANDLE FHand
194 )
195 {
196 EFI_STATUS Status;
197 EFI_FILE_SYSTEM_INFO *FileSystemInfo = NULL;
198 UINTN Size = 0;
199
200 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
201 if (Status == EFI_BUFFER_TOO_SMALL) {
202 FileSystemInfo = AllocateZeroPool (Size);
203 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
204 }
205
206 return EFI_ERROR(Status)?NULL:FileSystemInfo;
207 }
208
209 /**
210 Function is used to determine the number of device path instances
211 that exist in a device path.
212
213
214 @param DevicePath A pointer to a device path data structure.
215
216 @return This function counts and returns the number of device path instances
217 in DevicePath.
218
219 **/
220 UINTN
221 EfiDevicePathInstanceCount (
222 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
223 )
224 {
225 UINTN Count;
226 UINTN Size;
227
228 Count = 0;
229 while (GetNextDevicePathInstance (&DevicePath, &Size) != NULL) {
230 Count += 1;
231 }
232
233 return Count;
234 }
235
236 /**
237 Adjusts the size of a previously allocated buffer.
238
239
240 @param OldPool - A pointer to the buffer whose size is being adjusted.
241 @param OldSize - The size of the current buffer.
242 @param NewSize - The size of the new buffer.
243
244 @return The newly allocated buffer.
245 @retval NULL Allocation failed.
246
247 **/
248 VOID *
249 EfiReallocatePool (
250 IN VOID *OldPool,
251 IN UINTN OldSize,
252 IN UINTN NewSize
253 )
254 {
255 VOID *NewPool;
256
257 NewPool = NULL;
258 if (NewSize != 0) {
259 NewPool = AllocateZeroPool (NewSize);
260 }
261
262 if (OldPool != NULL) {
263 if (NewPool != NULL) {
264 CopyMem (NewPool, OldPool, OldSize < NewSize ? OldSize : NewSize);
265 }
266
267 FreePool (OldPool);
268 }
269
270 return NewPool;
271 }
272
273 /**
274 Compare two EFI_TIME data.
275
276
277 @param FirstTime - A pointer to the first EFI_TIME data.
278 @param SecondTime - A pointer to the second EFI_TIME data.
279
280 @retval TRUE The FirstTime is not later than the SecondTime.
281 @retval FALSE The FirstTime is later than the SecondTime.
282
283 **/
284 BOOLEAN
285 TimeCompare (
286 IN EFI_TIME *FirstTime,
287 IN EFI_TIME *SecondTime
288 )
289 {
290 if (FirstTime->Year != SecondTime->Year) {
291 return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
292 } else if (FirstTime->Month != SecondTime->Month) {
293 return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
294 } else if (FirstTime->Day != SecondTime->Day) {
295 return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
296 } else if (FirstTime->Hour != SecondTime->Hour) {
297 return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
298 } else if (FirstTime->Minute != SecondTime->Minute) {
299 return (BOOLEAN) (FirstTime->Minute < FirstTime->Minute);
300 } else if (FirstTime->Second != SecondTime->Second) {
301 return (BOOLEAN) (FirstTime->Second < SecondTime->Second);
302 }
303
304 return (BOOLEAN) (FirstTime->Nanosecond <= SecondTime->Nanosecond);
305 }
306
307 /**
308 Get a string from the Data Hub record based on
309 a device path.
310
311 @param DevPath The device Path.
312
313 @return A string located from the Data Hub records based on
314 the device path.
315 @retval NULL If failed to get the String from Data Hub.
316
317 **/
318 /*
319 UINT16 *
320 EfiLibStrFromDatahub (
321 IN EFI_DEVICE_PATH_PROTOCOL *DevPath
322 )
323 {
324 return NULL;
325 }*/