]> code.delx.au - refind/blob - EfiLib/BmLib.c
Fixed uninitialized-pointer bug that manifested as a crash with
[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 "../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 /**
123
124 Function gets the file information from an open file descriptor, and stores it
125 in a buffer allocated from pool.
126
127 @param FHand File Handle.
128
129 @return A pointer to a buffer with file information or NULL is returned
130
131 **/
132 EFI_FILE_INFO *
133 EfiLibFileInfo (
134 IN EFI_FILE_HANDLE FHand
135 )
136 {
137 EFI_STATUS Status;
138 EFI_FILE_INFO *FileInfo = NULL;
139 UINTN Size = 0;
140
141 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
142 if (Status == EFI_BUFFER_TOO_SMALL) {
143 FileInfo = AllocateZeroPool (Size);
144 Status = FHand->GetInfo (FHand, &gEfiFileInfoGuid, &Size, FileInfo);
145 }
146
147 return EFI_ERROR(Status)?NULL:FileInfo;
148 }
149
150 EFI_FILE_SYSTEM_INFO *
151 EfiLibFileSystemInfo (
152 IN EFI_FILE_HANDLE FHand
153 )
154 {
155 EFI_STATUS Status;
156 EFI_FILE_SYSTEM_INFO *FileSystemInfo = NULL;
157 UINTN Size = 0;
158
159 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
160 if (Status == EFI_BUFFER_TOO_SMALL) {
161 FileSystemInfo = AllocateZeroPool (Size);
162 Status = FHand->GetInfo (FHand, &gEfiFileSystemInfoGuid, &Size, FileSystemInfo);
163 }
164
165 return EFI_ERROR(Status)?NULL:FileSystemInfo;
166 }
167
168 /**
169 Adjusts the size of a previously allocated buffer.
170
171
172 @param OldPool - A pointer to the buffer whose size is being adjusted.
173 @param OldSize - The size of the current buffer.
174 @param NewSize - The size of the new buffer.
175
176 @return The newly allocated buffer.
177 @retval NULL Allocation failed.
178
179 **/
180 VOID *
181 EfiReallocatePool (
182 IN VOID *OldPool,
183 IN UINTN OldSize,
184 IN UINTN NewSize
185 )
186 {
187 VOID *NewPool;
188
189 NewPool = NULL;
190 if (NewSize != 0) {
191 NewPool = AllocateZeroPool (NewSize);
192 }
193
194 if (OldPool != NULL) {
195 if (NewPool != NULL) {
196 CopyMem (NewPool, OldPool, OldSize < NewSize ? OldSize : NewSize);
197 }
198
199 FreePool (OldPool);
200 }
201
202 return NewPool;
203 }