]> code.delx.au - refind/blob - EfiLib/BmLib.c
BIOS-mode boot support now works when compiled with GNU-EFI
[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 #ifdef __MAKEWITH_TIANO
16 #include "Platform.h"
17 #else
18 #include "gnuefi-helper.h"
19 #endif
20 #include "refit_call_wrapper.h"
21
22 /**
23
24 Find the first instance of this Protocol
25 in the system and return it's interface.
26
27
28 @param ProtocolGuid Provides the protocol to search for
29 @param Interface On return, a pointer to the first interface
30 that matches ProtocolGuid
31
32 @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found
33 @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid
34
35 **/
36 EFI_STATUS
37 EfiLibLocateProtocol (
38 IN EFI_GUID *ProtocolGuid,
39 OUT VOID **Interface
40 )
41 {
42 EFI_STATUS Status;
43
44 Status = refit_call3_wrapper(gBS->LocateProtocol,
45 ProtocolGuid,
46 NULL,
47 (VOID **) Interface
48 );
49 return Status;
50 }
51
52 /**
53
54 Function opens and returns a file handle to the root directory of a volume.
55
56 @param DeviceHandle A handle for a device
57
58 @return A valid file handle or NULL is returned
59
60 **/
61 EFI_FILE_HANDLE
62 EfiLibOpenRoot (
63 IN EFI_HANDLE DeviceHandle
64 )
65 {
66 EFI_STATUS Status;
67 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
68 EFI_FILE_HANDLE File;
69
70 File = NULL;
71
72 //
73 // File the file system interface to the device
74 //
75 Status = refit_call3_wrapper(gBS->HandleProtocol,
76 DeviceHandle,
77 &gEfiSimpleFileSystemProtocolGuid,
78 (VOID **) &Volume
79 );
80
81 //
82 // Open the root directory of the volume
83 //
84 if (!EFI_ERROR (Status)) {
85 Status = Volume->OpenVolume (
86 Volume,
87 &File
88 );
89 }
90 //
91 // Done
92 //
93 return EFI_ERROR (Status) ? NULL : File;
94 }
95
96 /**
97 Duplicate a string.
98
99 @param Src The source.
100
101 @return A new string which is duplicated copy of the source.
102 @retval NULL If there is not enough memory.
103
104 **/
105 CHAR16 *
106 EfiStrDuplicate (
107 IN CHAR16 *Src
108 )
109 {
110 CHAR16 *Dest;
111 UINTN Size;
112
113 Size = StrSize (Src); //at least 2bytes
114 Dest = AllocateZeroPool (Size);
115 if (Dest != NULL) {
116 CopyMem (Dest, Src, Size);
117 }
118
119 return Dest;
120 }
121
122 //Compare strings case insensitive
123 INTN
124 EFIAPI
125 StriCmp (
126 IN CONST CHAR16 *FirstString,
127 IN CONST CHAR16 *SecondString
128 )
129 {
130
131 while ((*FirstString != L'\0') && ((*FirstString & ~0x20) == (*SecondString & ~0x20))) {
132 FirstString++;
133 SecondString++;
134 }
135 return *FirstString - *SecondString;
136 }
137
138 /**
139
140 Function gets the file information from an open file descriptor, and stores it
141 in a buffer allocated from pool.
142
143 @param FHand File Handle.
144
145 @return A pointer to a buffer with file information or NULL is returned
146
147 **/
148 EFI_FILE_INFO *
149 EfiLibFileInfo (
150 IN EFI_FILE_HANDLE FHand
151 )
152 {
153 EFI_STATUS Status;
154 EFI_FILE_INFO *FileInfo = NULL;
155 UINTN Size = 0;
156
157 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
158 if (Status == EFI_BUFFER_TOO_SMALL) {
159 FileInfo = AllocateZeroPool (Size);
160 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
161 }
162
163 return EFI_ERROR(Status)?NULL:FileInfo;
164 }
165
166 EFI_FILE_SYSTEM_INFO *
167 EfiLibFileSystemInfo (
168 IN EFI_FILE_HANDLE FHand
169 )
170 {
171 EFI_STATUS Status;
172 EFI_FILE_SYSTEM_INFO *FileSystemInfo = NULL;
173 UINTN Size = 0;
174
175 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
176 if (Status == EFI_BUFFER_TOO_SMALL) {
177 FileSystemInfo = AllocateZeroPool (Size);
178 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
179 }
180
181 return EFI_ERROR(Status)?NULL:FileSystemInfo;
182 }
183
184 /**
185 Adjusts the size of a previously allocated buffer.
186
187
188 @param OldPool - A pointer to the buffer whose size is being adjusted.
189 @param OldSize - The size of the current buffer.
190 @param NewSize - The size of the new buffer.
191
192 @return The newly allocated buffer.
193 @retval NULL Allocation failed.
194
195 **/
196 VOID *
197 EfiReallocatePool (
198 IN VOID *OldPool,
199 IN UINTN OldSize,
200 IN UINTN NewSize
201 )
202 {
203 VOID *NewPool;
204
205 NewPool = NULL;
206 if (NewSize != 0) {
207 NewPool = AllocateZeroPool (NewSize);
208 }
209
210 if (OldPool != NULL) {
211 if (NewPool != NULL) {
212 CopyMem (NewPool, OldPool, OldSize < NewSize ? OldSize : NewSize);
213 }
214
215 FreePool (OldPool);
216 }
217
218 return NewPool;
219 }