blob: 04a2f189eceb0c650c3d632a2949898338c3e5ae [file] [log] [blame]
jcarseyd2b45642009-05-11 18:02:16 +00001/** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
Qiu Shumine84fb6e2015-06-15 07:42:48 +00004 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved. <BR>
jcarseyac255da2010-01-25 20:06:10 +00005 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
jcarseyd2b45642009-05-11 18:02:16 +00009
jcarseyac255da2010-01-25 20:06:10 +000010 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.
jcarseyd2b45642009-05-11 18:02:16 +000012
13**/
14
15#include <Uefi.h>
16
jcarseyb1f95a02009-06-16 00:23:19 +000017#include <Protocol/SimpleFileSystem.h>
jcarsey4eb59be2011-03-25 20:43:54 +000018#include <Protocol/UnicodeCollation.h>
jcarseyb1f95a02009-06-16 00:23:19 +000019
20#include <Guid/FileInfo.h>
21
jcarseyd2b45642009-05-11 18:02:16 +000022#include <Library/DebugLib.h>
23#include <Library/MemoryAllocationLib.h>
jcarseyb1f95a02009-06-16 00:23:19 +000024#include <Library/BaseLib.h>
25#include <Library/BaseMemoryLib.h>
jcarseyb3011f42010-01-11 21:49:04 +000026#include <Library/FileHandleLib.h>
27#include <Library/PcdLib.h>
28#include <Library/PrintLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000029
jcarsey4eb59be2011-03-25 20:43:54 +000030CONST UINT16 gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;
31
jcarseyd2b45642009-05-11 18:02:16 +000032#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
33#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
34
35/**
jcarseya405b862010-09-14 05:18:09 +000036 This function will retrieve the information about the file for the handle
jcarseyd2b45642009-05-11 18:02:16 +000037 specified and store it in allocated pool memory.
38
jcarseya405b862010-09-14 05:18:09 +000039 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +000040 caller's responsibility to free the buffer
jcarseyd2b45642009-05-11 18:02:16 +000041
jcarseya405b862010-09-14 05:18:09 +000042 @param FileHandle The file handle of the file for which information is
jcarseyd2b45642009-05-11 18:02:16 +000043 being requested.
44
45 @retval NULL information could not be retrieved.
46
47 @return the information about the file
48**/
49EFI_FILE_INFO*
50EFIAPI
51FileHandleGetInfo (
52 IN EFI_FILE_HANDLE FileHandle
53 )
54{
jcarseya405b862010-09-14 05:18:09 +000055 EFI_FILE_INFO *FileInfo;
jcarseyd2b45642009-05-11 18:02:16 +000056 UINTN FileInfoSize;
57 EFI_STATUS Status;
58
jcarsey4eb59be2011-03-25 20:43:54 +000059 if (FileHandle == NULL) {
60 return (NULL);
61 }
jcarseyd2b45642009-05-11 18:02:16 +000062
63 //
64 // Get the required size to allocate
65 //
jcarseyd2b45642009-05-11 18:02:16 +000066 FileInfoSize = 0;
jcarseya405b862010-09-14 05:18:09 +000067 FileInfo = NULL;
68 Status = FileHandle->GetInfo(FileHandle,
69 &gEfiFileInfoGuid,
70 &FileInfoSize,
jcarsey4eb59be2011-03-25 20:43:54 +000071 NULL);
72 if (Status == EFI_BUFFER_TOO_SMALL){
73 //
74 // error is expected. getting size to allocate
75 //
76 FileInfo = AllocateZeroPool(FileInfoSize);
77 //
78 // now get the information
79 //
80 Status = FileHandle->GetInfo(FileHandle,
81 &gEfiFileInfoGuid,
82 &FileInfoSize,
83 FileInfo);
84 //
85 // if we got an error free the memory and return NULL
86 //
Sergei Antonov037ca232013-09-20 20:10:17 +000087 if (EFI_ERROR(Status) && (FileInfo != NULL)) {
jcarsey4eb59be2011-03-25 20:43:54 +000088 FreePool(FileInfo);
Sergei Antonov037ca232013-09-20 20:10:17 +000089 FileInfo = NULL;
jcarsey4eb59be2011-03-25 20:43:54 +000090 }
jcarseyd2b45642009-05-11 18:02:16 +000091 }
jcarseya405b862010-09-14 05:18:09 +000092 return (FileInfo);
jcarseyd2b45642009-05-11 18:02:16 +000093}
94
95/**
jcarseya405b862010-09-14 05:18:09 +000096 This function sets the information about the file for the opened handle
jcarseyd2b45642009-05-11 18:02:16 +000097 specified.
98
jcarseya405b862010-09-14 05:18:09 +000099 @param[in] FileHandle The file handle of the file for which information
100 is being set.
jcarseyd2b45642009-05-11 18:02:16 +0000101
jcarseya405b862010-09-14 05:18:09 +0000102 @param[in] FileInfo The information to set.
jcarseyd2b45642009-05-11 18:02:16 +0000103
darylm503b0934ac2011-11-12 00:35:11 +0000104 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000105 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
106 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000107 @retval EFI_NO_MEDIA The device has no medium.
108 @retval EFI_DEVICE_ERROR The device reported an error.
109 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
110 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000111 @retval EFI_ACCESS_DENIED The file was opened read only.
112 @retval EFI_VOLUME_FULL The volume is full.
jcarseyd2b45642009-05-11 18:02:16 +0000113**/
114EFI_STATUS
115EFIAPI
116FileHandleSetInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000117 IN EFI_FILE_HANDLE FileHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000118 IN CONST EFI_FILE_INFO *FileInfo
119 )
120{
jcarseya405b862010-09-14 05:18:09 +0000121
Jaben Carsey183ecff2014-06-18 16:37:16 +0000122 if (FileHandle == NULL || FileInfo == NULL) {
123 return (EFI_INVALID_PARAMETER);
124 }
jcarseyd2b45642009-05-11 18:02:16 +0000125
jcarseyd2b45642009-05-11 18:02:16 +0000126 //
127 // Set the info
128 //
jcarseya405b862010-09-14 05:18:09 +0000129 return (FileHandle->SetInfo(FileHandle,
qhuang83d342022009-11-18 06:01:35 +0000130 &gEfiFileInfoGuid,
jcarseyd2b45642009-05-11 18:02:16 +0000131 (UINTN)FileInfo->Size,
132 (EFI_FILE_INFO*)FileInfo));
jcarseya405b862010-09-14 05:18:09 +0000133}
jcarseyd2b45642009-05-11 18:02:16 +0000134
135/**
136 This function reads information from an opened file.
137
jcarseya405b862010-09-14 05:18:09 +0000138 If FileHandle is not a directory, the function reads the requested number of
139 bytes from the file at the file's current position and returns them in Buffer.
jcarseyd2b45642009-05-11 18:02:16 +0000140 If the read goes beyond the end of the file, the read length is truncated to the
jcarseya405b862010-09-14 05:18:09 +0000141 end of the file. The file's current position is increased by the number of bytes
142 returned. If FileHandle is a directory, the function reads the directory entry
143 at the file's current position and returns the entry in Buffer. If the Buffer
144 is not large enough to hold the current directory entry, then
145 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
146 BufferSize is set to be the size of the buffer needed to read the entry. On
147 success, the current position is updated to the next directory entry. If there
148 are no more directory entries, the read returns a zero-length buffer.
jcarseyd2b45642009-05-11 18:02:16 +0000149 EFI_FILE_INFO is the structure returned as the directory entry.
150
151 @param FileHandle the opened file handle
jcarseya405b862010-09-14 05:18:09 +0000152 @param BufferSize on input the size of buffer in bytes. on return
jcarseyd2b45642009-05-11 18:02:16 +0000153 the number of bytes written.
154 @param Buffer the buffer to put read data into.
155
jcarseya405b862010-09-14 05:18:09 +0000156 @retval EFI_SUCCESS Data was read.
157 @retval EFI_NO_MEDIA The device has no media.
158 @retval EFI_DEVICE_ERROR The device reported an error.
159 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
160 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarseyd2b45642009-05-11 18:02:16 +0000161 size.
162
163**/
164EFI_STATUS
165EFIAPI
166FileHandleRead(
167 IN EFI_FILE_HANDLE FileHandle,
168 IN OUT UINTN *BufferSize,
169 OUT VOID *Buffer
170 )
171{
Jaben Carsey183ecff2014-06-18 16:37:16 +0000172 if (FileHandle == NULL) {
173 return (EFI_INVALID_PARAMETER);
174 }
jcarseyd2b45642009-05-11 18:02:16 +0000175
176 //
177 // Perform the read based on EFI_FILE_PROTOCOL
178 //
179 return (FileHandle->Read(FileHandle, BufferSize, Buffer));
180}
181
182
183/**
184 Write data to a file.
185
jcarseya405b862010-09-14 05:18:09 +0000186 This function writes the specified number of bytes to the file at the current
187 file position. The current file position is advanced the actual number of bytes
188 written, which is returned in BufferSize. Partial writes only occur when there
189 has been a data error during the write attempt (such as "volume space full").
190 The file is automatically grown to hold the data if required. Direct writes to
jcarseyd2b45642009-05-11 18:02:16 +0000191 opened directories are not supported.
192
193 @param FileHandle The opened file for writing
194 @param BufferSize on input the number of bytes in Buffer. On output
195 the number of bytes written.
196 @param Buffer the buffer containing data to write is stored.
197
darylm503b0934ac2011-11-12 00:35:11 +0000198 @retval EFI_SUCCESS Data was written.
199 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
200 @retval EFI_NO_MEDIA The device has no media.
201 @retval EFI_DEVICE_ERROR The device reported an error.
202 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
203 @retval EFI_WRITE_PROTECTED The device is write-protected.
204 @retval EFI_ACCESS_DENIED The file was open for read only.
205 @retval EFI_VOLUME_FULL The volume is full.
jcarseyd2b45642009-05-11 18:02:16 +0000206**/
207EFI_STATUS
208EFIAPI
209FileHandleWrite(
210 IN EFI_FILE_HANDLE FileHandle,
211 IN OUT UINTN *BufferSize,
212 IN VOID *Buffer
213 )
214{
Jaben Carsey183ecff2014-06-18 16:37:16 +0000215 if (FileHandle == NULL) {
216 return (EFI_INVALID_PARAMETER);
217 }
218
jcarseyd2b45642009-05-11 18:02:16 +0000219 //
220 // Perform the write based on EFI_FILE_PROTOCOL
221 //
222 return (FileHandle->Write(FileHandle, BufferSize, Buffer));
223}
224
jcarseya405b862010-09-14 05:18:09 +0000225/**
jcarseyd2b45642009-05-11 18:02:16 +0000226 Close an open file handle.
227
jcarseya405b862010-09-14 05:18:09 +0000228 This function closes a specified file handle. All "dirty" cached file data is
229 flushed to the device, and the file is closed. In all cases the handle is
jcarseyd2b45642009-05-11 18:02:16 +0000230 closed.
231
232@param FileHandle the file handle to close.
233
234@retval EFI_SUCCESS the file handle was closed sucessfully.
235**/
236EFI_STATUS
237EFIAPI
238FileHandleClose (
239 IN EFI_FILE_HANDLE FileHandle
240 )
241{
242 EFI_STATUS Status;
Jaben Carsey183ecff2014-06-18 16:37:16 +0000243
244 if (FileHandle == NULL) {
245 return (EFI_INVALID_PARAMETER);
246 }
247
jcarseyd2b45642009-05-11 18:02:16 +0000248 //
249 // Perform the Close based on EFI_FILE_PROTOCOL
250 //
251 Status = FileHandle->Close(FileHandle);
252 return Status;
253}
254
255/**
256 Delete a file and close the handle
257
258 This function closes and deletes a file. In all cases the file handle is closed.
jcarseya405b862010-09-14 05:18:09 +0000259 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarseyd2b45642009-05-11 18:02:16 +0000260 returned, but the handle is still closed.
261
262 @param FileHandle the file handle to delete
263
264 @retval EFI_SUCCESS the file was closed sucessfully
jcarseya405b862010-09-14 05:18:09 +0000265 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarseyd2b45642009-05-11 18:02:16 +0000266 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000267 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarseyd2b45642009-05-11 18:02:16 +0000268**/
269EFI_STATUS
270EFIAPI
271FileHandleDelete (
darylm503b0934ac2011-11-12 00:35:11 +0000272 IN EFI_FILE_HANDLE FileHandle
jcarseyd2b45642009-05-11 18:02:16 +0000273 )
274{
275 EFI_STATUS Status;
Jaben Carsey183ecff2014-06-18 16:37:16 +0000276
277 if (FileHandle == NULL) {
278 return (EFI_INVALID_PARAMETER);
279 }
280
jcarseyd2b45642009-05-11 18:02:16 +0000281 //
282 // Perform the Delete based on EFI_FILE_PROTOCOL
283 //
284 Status = FileHandle->Delete(FileHandle);
285 return Status;
286}
287
288/**
289 Set the current position in a file.
290
jcarseya405b862010-09-14 05:18:09 +0000291 This function sets the current file position for the handle to the position
jcarseyd2b45642009-05-11 18:02:16 +0000292 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarseya405b862010-09-14 05:18:09 +0000293 absolute positioning is supported, and seeking past the end of the file is
294 allowed (a subsequent write would grow the file). Seeking to position
jcarseyd2b45642009-05-11 18:02:16 +0000295 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarseya405b862010-09-14 05:18:09 +0000296 If FileHandle is a directory, the only position that may be set is zero. This
jcarseyd2b45642009-05-11 18:02:16 +0000297 has the effect of starting the read process of the directory entries over.
298
299 @param FileHandle The file handle on which the position is being set
300 @param Position Byte position from begining of file
301
302 @retval EFI_SUCCESS Operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +0000303 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarseyd2b45642009-05-11 18:02:16 +0000304 directories.
305 @retval INVALID_PARAMETER One of the parameters has an invalid value.
306**/
307EFI_STATUS
308EFIAPI
309FileHandleSetPosition (
darylm503b0934ac2011-11-12 00:35:11 +0000310 IN EFI_FILE_HANDLE FileHandle,
311 IN UINT64 Position
jcarseyd2b45642009-05-11 18:02:16 +0000312 )
313{
Jaben Carsey183ecff2014-06-18 16:37:16 +0000314 if (FileHandle == NULL) {
315 return (EFI_INVALID_PARAMETER);
316 }
317
jcarseyd2b45642009-05-11 18:02:16 +0000318 //
319 // Perform the SetPosition based on EFI_FILE_PROTOCOL
320 //
321 return (FileHandle->SetPosition(FileHandle, Position));
322}
323
jcarseya405b862010-09-14 05:18:09 +0000324/**
jcarseyd2b45642009-05-11 18:02:16 +0000325 Gets a file's current position
326
jcarseya405b862010-09-14 05:18:09 +0000327 This function retrieves the current file position for the file handle. For
328 directories, the current file position has no meaning outside of the file
jcarseyd2b45642009-05-11 18:02:16 +0000329 system driver and as such the operation is not supported. An error is returned
330 if FileHandle is a directory.
331
332 @param FileHandle The open file handle on which to get the position.
333 @param Position Byte position from begining of file.
334
335 @retval EFI_SUCCESS the operation completed sucessfully.
336 @retval INVALID_PARAMETER One of the parameters has an invalid value.
337 @retval EFI_UNSUPPORTED the request is not valid on directories.
338**/
339EFI_STATUS
340EFIAPI
341FileHandleGetPosition (
342 IN EFI_FILE_HANDLE FileHandle,
343 OUT UINT64 *Position
344 )
345{
Jaben Carsey183ecff2014-06-18 16:37:16 +0000346 if (Position == NULL || FileHandle == NULL) {
jcarseya405b862010-09-14 05:18:09 +0000347 return (EFI_INVALID_PARAMETER);
348 }
Jaben Carsey183ecff2014-06-18 16:37:16 +0000349
jcarseyd2b45642009-05-11 18:02:16 +0000350 //
351 // Perform the GetPosition based on EFI_FILE_PROTOCOL
352 //
353 return (FileHandle->GetPosition(FileHandle, Position));
354}
355/**
356 Flushes data on a file
jcarseya405b862010-09-14 05:18:09 +0000357
jcarseyd2b45642009-05-11 18:02:16 +0000358 This function flushes all modified data associated with a file to a device.
359
360 @param FileHandle The file handle on which to flush data
361
362 @retval EFI_SUCCESS The data was flushed.
363 @retval EFI_NO_MEDIA The device has no media.
364 @retval EFI_DEVICE_ERROR The device reported an error.
365 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
366 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
367 @retval EFI_ACCESS_DENIED The file was opened for read only.
368**/
369EFI_STATUS
370EFIAPI
371FileHandleFlush (
372 IN EFI_FILE_HANDLE FileHandle
373 )
374{
Jaben Carsey183ecff2014-06-18 16:37:16 +0000375 if (FileHandle == NULL) {
376 return (EFI_INVALID_PARAMETER);
377 }
378
jcarseyd2b45642009-05-11 18:02:16 +0000379 //
380 // Perform the Flush based on EFI_FILE_PROTOCOL
381 //
382 return (FileHandle->Flush(FileHandle));
383}
384
385/**
Qiu Shumine84fb6e2015-06-15 07:42:48 +0000386 Function to determine if a given handle is a directory handle.
jcarseyd2b45642009-05-11 18:02:16 +0000387
Qiu Shumine84fb6e2015-06-15 07:42:48 +0000388 Open the file information on the DirHandle and verify that the Attribute
jcarseyd2b45642009-05-11 18:02:16 +0000389 includes EFI_FILE_DIRECTORY bit set.
390
Qiu Shumine84fb6e2015-06-15 07:42:48 +0000391 @param[in] DirHandle Handle to open file.
jcarseyd2b45642009-05-11 18:02:16 +0000392
Qiu Shumine84fb6e2015-06-15 07:42:48 +0000393 @retval EFI_SUCCESS DirHandle is a directory.
394 @retval EFI_INVALID_PARAMETER DirHandle is NULL.
395 The file information returns from FileHandleGetInfo is NULL.
396 @retval EFI_NOT_FOUND DirHandle is not a directory.
jcarseyd2b45642009-05-11 18:02:16 +0000397**/
398EFI_STATUS
399EFIAPI
400FileHandleIsDirectory (
401 IN EFI_FILE_HANDLE DirHandle
402 )
403{
404 EFI_FILE_INFO *DirInfo;
405
Jaben Carsey183ecff2014-06-18 16:37:16 +0000406 if (DirHandle == NULL) {
407 return (EFI_INVALID_PARAMETER);
408 }
jcarseya405b862010-09-14 05:18:09 +0000409
jcarseyd2b45642009-05-11 18:02:16 +0000410 //
411 // get the file information for DirHandle
412 //
413 DirInfo = FileHandleGetInfo (DirHandle);
jcarseya405b862010-09-14 05:18:09 +0000414
jcarseyd2b45642009-05-11 18:02:16 +0000415 //
416 // Parse DirInfo
417 //
418 if (DirInfo == NULL) {
419 //
420 // We got nothing...
421 //
422 return (EFI_INVALID_PARAMETER);
jcarseya405b862010-09-14 05:18:09 +0000423 }
jcarseyd2b45642009-05-11 18:02:16 +0000424 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
425 //
426 // Attributes say this is not a directory
427 //
428 FreePool (DirInfo);
429 return (EFI_NOT_FOUND);
430 }
431 //
432 // all good...
433 //
434 FreePool (DirInfo);
435 return (EFI_SUCCESS);
436}
437
darylm503b0934ac2011-11-12 00:35:11 +0000438/** Retrieve first entry from a directory.
jcarseyd2b45642009-05-11 18:02:16 +0000439
darylm503b0934ac2011-11-12 00:35:11 +0000440 This function takes an open directory handle and gets information from the
441 first entry in the directory. A buffer is allocated to contain
442 the information and a pointer to the buffer is returned in *Buffer. The
443 caller can use FileHandleFindNextFile() to get subsequent directory entries.
jcarseyd2b45642009-05-11 18:02:16 +0000444
darylm503b0934ac2011-11-12 00:35:11 +0000445 The buffer will be freed by FileHandleFindNextFile() when the last directory
446 entry is read. Otherwise, the caller must free the buffer, using FreePool,
447 when finished with it.
448
449 @param[in] DirHandle The file handle of the directory to search.
450 @param[out] Buffer The pointer to pointer to buffer for file's information.
jcarseyd2b45642009-05-11 18:02:16 +0000451
452 @retval EFI_SUCCESS Found the first file.
453 @retval EFI_NOT_FOUND Cannot find the directory.
454 @retval EFI_NO_MEDIA The device has no media.
455 @retval EFI_DEVICE_ERROR The device reported an error.
456 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
457 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
458 or FileHandleRead
459**/
460EFI_STATUS
461EFIAPI
462FileHandleFindFirstFile (
463 IN EFI_FILE_HANDLE DirHandle,
464 OUT EFI_FILE_INFO **Buffer
465 )
466{
467 EFI_STATUS Status;
468 UINTN BufferSize;
469
jcarsey4eb59be2011-03-25 20:43:54 +0000470 if (Buffer == NULL || DirHandle == NULL) {
471 return (EFI_INVALID_PARAMETER);
472 }
jcarseyd2b45642009-05-11 18:02:16 +0000473
474 //
475 // verify that DirHandle is a directory
476 //
477 Status = FileHandleIsDirectory(DirHandle);
478 if (EFI_ERROR(Status)) {
479 return (Status);
jcarseya405b862010-09-14 05:18:09 +0000480 }
jcarseyd2b45642009-05-11 18:02:16 +0000481
482 //
jcarseyd2b45642009-05-11 18:02:16 +0000483 // Allocate a buffer sized to struct size + enough for the string at the end
484 //
485 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
486 *Buffer = AllocateZeroPool(BufferSize);
jcarsey4eb59be2011-03-25 20:43:54 +0000487 if (*Buffer == NULL){
488 return (EFI_OUT_OF_RESOURCES);
489 }
490
491 //
492 // reset to the begining of the directory
493 //
494 Status = FileHandleSetPosition(DirHandle, 0);
495 if (EFI_ERROR(Status)) {
496 FreePool(*Buffer);
497 *Buffer = NULL;
498 return (Status);
499 }
jcarseyd2b45642009-05-11 18:02:16 +0000500
501 //
502 // read in the info about the first file
503 //
504 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
505 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
jcarsey74fa83f2012-02-02 16:55:30 +0000506 if (EFI_ERROR(Status) || BufferSize == 0) {
jcarseyd2b45642009-05-11 18:02:16 +0000507 FreePool(*Buffer);
508 *Buffer = NULL;
jcarsey74fa83f2012-02-02 16:55:30 +0000509 if (BufferSize == 0) {
510 return (EFI_NOT_FOUND);
511 }
jcarseyd2b45642009-05-11 18:02:16 +0000512 return (Status);
513 }
514 return (EFI_SUCCESS);
515}
jcarseyd2b45642009-05-11 18:02:16 +0000516
darylm503b0934ac2011-11-12 00:35:11 +0000517/** Retrieve next entries from a directory.
jcarseyd2b45642009-05-11 18:02:16 +0000518
darylm503b0934ac2011-11-12 00:35:11 +0000519 To use this function, the caller must first call the FileHandleFindFirstFile()
520 function to get the first directory entry. Subsequent directory entries are
521 retrieved by using the FileHandleFindNextFile() function. This function can
522 be called several times to get each entry from the directory. If the call of
523 FileHandleFindNextFile() retrieved the last directory entry, the next call of
524 this function will set *NoFile to TRUE and free the buffer.
525
526 @param[in] DirHandle The file handle of the directory.
527 @param[out] Buffer The pointer to buffer for file's information.
528 @param[out] NoFile The pointer to boolean when last file is found.
jcarseyd2b45642009-05-11 18:02:16 +0000529
530 @retval EFI_SUCCESS Found the next file, or reached last file
531 @retval EFI_NO_MEDIA The device has no media.
532 @retval EFI_DEVICE_ERROR The device reported an error.
533 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
534**/
535EFI_STATUS
536EFIAPI
537FileHandleFindNextFile(
darylm503b0934ac2011-11-12 00:35:11 +0000538 IN EFI_FILE_HANDLE DirHandle,
539 OUT EFI_FILE_INFO *Buffer,
540 OUT BOOLEAN *NoFile
jcarseyd2b45642009-05-11 18:02:16 +0000541 )
542{
543 EFI_STATUS Status;
544 UINTN BufferSize;
545
Jaben Carsey183ecff2014-06-18 16:37:16 +0000546 if (DirHandle == NULL || Buffer == NULL || NoFile == NULL) {
547 return (EFI_INVALID_PARAMETER);
548 }
jcarseyd2b45642009-05-11 18:02:16 +0000549
550 //
551 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
552 //
553 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
554
555 //
556 // read in the info about the next file
557 //
558 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
559 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
560 if (EFI_ERROR(Status)) {
561 return (Status);
562 }
563
564 //
565 // If we read 0 bytes (but did not have erros) we already read in the last file.
566 //
567 if (BufferSize == 0) {
568 FreePool(Buffer);
569 *NoFile = TRUE;
570 }
571
572 return (EFI_SUCCESS);
573}
jcarseya405b862010-09-14 05:18:09 +0000574
jcarseyd2b45642009-05-11 18:02:16 +0000575/**
576 Retrieve the size of a file.
577
jcarseya405b862010-09-14 05:18:09 +0000578 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarseyd2b45642009-05-11 18:02:16 +0000579 data.
580
Qiu Shuminb269f892015-06-17 04:49:47 +0000581 @param[in] FileHandle The file handle from which size is retrieved.
582 @param[out] Size The pointer to size.
jcarseyd2b45642009-05-11 18:02:16 +0000583
Qiu Shuminb269f892015-06-17 04:49:47 +0000584 @retval EFI_SUCCESS Operation was completed sucessfully.
585 @retval EFI_DEVICE_ERROR Cannot access the file.
586 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
587 Size is NULL.
jcarseyd2b45642009-05-11 18:02:16 +0000588**/
589EFI_STATUS
590EFIAPI
591FileHandleGetSize (
592 IN EFI_FILE_HANDLE FileHandle,
593 OUT UINT64 *Size
594 )
595{
596 EFI_FILE_INFO *FileInfo;
597
Jaben Carsey183ecff2014-06-18 16:37:16 +0000598 if (FileHandle == NULL || Size == NULL) {
599 return (EFI_INVALID_PARAMETER);
600 }
jcarseya405b862010-09-14 05:18:09 +0000601
jcarseyd2b45642009-05-11 18:02:16 +0000602 //
603 // get the FileInfo structure
604 //
605 FileInfo = FileHandleGetInfo(FileHandle);
606 if (FileInfo == NULL) {
607 return (EFI_DEVICE_ERROR);
608 }
609
610 //
611 // Assign the Size pointer to the correct value
612 //
613 *Size = FileInfo->FileSize;
jcarseya405b862010-09-14 05:18:09 +0000614
jcarseyd2b45642009-05-11 18:02:16 +0000615 //
616 // free the FileInfo memory
617 //
618 FreePool(FileInfo);
619
620 return (EFI_SUCCESS);
jcarseyb1f95a02009-06-16 00:23:19 +0000621}
622
jcarseya405b862010-09-14 05:18:09 +0000623/**
624 Set the size of a file.
625
jcarseya405b862010-09-14 05:18:09 +0000626 This function changes the file size info from the FileHandle's EFI_FILE_INFO
627 data.
628
Qiu Shuminb269f892015-06-17 04:49:47 +0000629 @param[in] FileHandle The file handle whose size is to be changed.
630 @param[in] Size The new size.
jcarseya405b862010-09-14 05:18:09 +0000631
Qiu Shuminb269f892015-06-17 04:49:47 +0000632 @retval EFI_SUCCESS The operation completed successfully.
633 @retval EFI_DEVICE_ERROR Cannot access the file.
634 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
jcarseya405b862010-09-14 05:18:09 +0000635**/
636EFI_STATUS
637EFIAPI
638FileHandleSetSize (
639 IN EFI_FILE_HANDLE FileHandle,
640 IN UINT64 Size
641 )
642{
643 EFI_FILE_INFO *FileInfo;
644 EFI_STATUS Status;
645
Jaben Carsey183ecff2014-06-18 16:37:16 +0000646 if (FileHandle == NULL) {
647 return (EFI_INVALID_PARAMETER);
648 }
jcarseya405b862010-09-14 05:18:09 +0000649
650 //
651 // get the FileInfo structure
652 //
653 FileInfo = FileHandleGetInfo(FileHandle);
654 if (FileInfo == NULL) {
655 return (EFI_DEVICE_ERROR);
656 }
657
658 //
659 // Assign the FileSize pointer to the new value
660 //
661 FileInfo->FileSize = Size;
662
663 Status = FileHandleSetInfo(FileHandle, FileInfo);
664 //
665 // free the FileInfo memory
666 //
667 FreePool(FileInfo);
668
669 return (Status);
670}
jcarseyb1f95a02009-06-16 00:23:19 +0000671
672/**
jcarseya405b862010-09-14 05:18:09 +0000673 Safely append (on the left) with automatic string resizing given length of Destination and
jcarseyb1f95a02009-06-16 00:23:19 +0000674 desired length of copy from Source.
675
jcarseya405b862010-09-14 05:18:09 +0000676 append the first D characters of Source to the end of Destination, where D is
677 the lesser of Count and the StrLen() of Source. If appending those D characters
678 will fit within Destination (whose Size is given as CurrentSize) and
679 still leave room for a NULL terminator, then those characters are appended,
680 starting at the original terminating NULL of Destination, and a new terminating
jcarseyac255da2010-01-25 20:06:10 +0000681 NULL is appended.
jcarseyb1f95a02009-06-16 00:23:19 +0000682
683 If appending D characters onto Destination will result in a overflow of the size
684 given in CurrentSize the string will be grown such that the copy can be performed
685 and CurrentSize will be updated to the new size.
686
jcarseya405b862010-09-14 05:18:09 +0000687 If Source is NULL, there is nothing to append, just return the current buffer in
jcarseyb1f95a02009-06-16 00:23:19 +0000688 Destination.
689
Jaben Carsey183ecff2014-06-18 16:37:16 +0000690 if Destination is NULL, then return error
jcarseya405b862010-09-14 05:18:09 +0000691 if Destination's current length (including NULL terminator) is already more then
jcarseyb1f95a02009-06-16 00:23:19 +0000692 CurrentSize, then ASSERT()
693
ydong104ff7e372011-09-02 08:05:34 +0000694 @param[in, out] Destination The String to append onto
695 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarseyb1f95a02009-06-16 00:23:19 +0000696 return possibly the new size (still in bytes). if NULL
697 then allocate whatever is needed.
698 @param[in] Source The String to append from
jcarseya405b862010-09-14 05:18:09 +0000699 @param[in] Count Maximum number of characters to append. if 0 then
jcarseyb1f95a02009-06-16 00:23:19 +0000700 all are appended.
701
702 @return Destination return the resultant string.
703**/
jcarseya405b862010-09-14 05:18:09 +0000704CHAR16*
jcarseyb1f95a02009-06-16 00:23:19 +0000705EFIAPI
706StrnCatGrowLeft (
707 IN OUT CHAR16 **Destination,
708 IN OUT UINTN *CurrentSize,
709 IN CONST CHAR16 *Source,
710 IN UINTN Count
jcarseya405b862010-09-14 05:18:09 +0000711 )
712{
jcarseyb1f95a02009-06-16 00:23:19 +0000713 UINTN DestinationStartSize;
714 UINTN NewSize;
jcarsey727a4c72009-07-09 17:36:06 +0000715 UINTN CopySize;
jcarseyb1f95a02009-06-16 00:23:19 +0000716
Jaben Carsey183ecff2014-06-18 16:37:16 +0000717 if (Destination == NULL) {
718 return (NULL);
719 }
jcarseyb1f95a02009-06-16 00:23:19 +0000720
721 //
722 // If there's nothing to do then just return Destination
723 //
724 if (Source == NULL) {
725 return (*Destination);
726 }
727
728 //
729 // allow for NULL pointers address as Destination
730 //
731 if (*Destination != NULL) {
732 ASSERT(CurrentSize != 0);
733 DestinationStartSize = StrSize(*Destination);
734 ASSERT(DestinationStartSize <= *CurrentSize);
735 } else {
736 DestinationStartSize = 0;
737// ASSERT(*CurrentSize == 0);
738 }
739
740 //
741 // Append all of Source?
742 //
743 if (Count == 0) {
jcarseyd595d4b2009-07-16 17:24:16 +0000744 Count = StrSize(Source);
jcarseyb1f95a02009-06-16 00:23:19 +0000745 }
746
747 //
748 // Test and grow if required
749 //
750 if (CurrentSize != NULL) {
751 NewSize = *CurrentSize;
jcarseyd595d4b2009-07-16 17:24:16 +0000752 while (NewSize < (DestinationStartSize + Count)) {
753 NewSize += 2 * Count;
jcarseyb1f95a02009-06-16 00:23:19 +0000754 }
755 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
756 *CurrentSize = NewSize;
757 } else {
jcarseyd595d4b2009-07-16 17:24:16 +0000758 *Destination = AllocateZeroPool(Count+sizeof(CHAR16));
jcarseyb1f95a02009-06-16 00:23:19 +0000759 }
sfu502a758c2011-10-08 02:55:30 +0000760 if (*Destination == NULL) {
761 return NULL;
762 }
jcarseyb1f95a02009-06-16 00:23:19 +0000763
jcarsey727a4c72009-07-09 17:36:06 +0000764 CopySize = StrSize(*Destination);
jcarsey46f43bc2009-07-24 21:32:00 +0000765 CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);
766 CopyMem(*Destination, Source, Count-2);
jcarseyb1f95a02009-06-16 00:23:19 +0000767 return (*Destination);
768}
769
770/**
jcarseya405b862010-09-14 05:18:09 +0000771 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
Qiu Shuminad6f6f82015-08-26 02:25:42 +0000772 directory 'stack'. If the file is a directory, then append the '\' char at the
773 end of name string. If it's not a directory, then the last '\' should not be
774 added.
jcarseyb1f95a02009-06-16 00:23:19 +0000775
776 if Handle is NULL, return EFI_INVALID_PARAMETER
777
778 @param[in] Handle Handle to the Directory or File to create path to.
jcarseya405b862010-09-14 05:18:09 +0000779 @param[out] FullFileName pointer to pointer to generated full file name. It
jcarseyb1f95a02009-06-16 00:23:19 +0000780 is the responsibility of the caller to free this memory
781 with a call to FreePool().
782 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
783 @retval EFI_INVALID_PARAMETER Handle was NULL.
784 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
785 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
786**/
787EFI_STATUS
788EFIAPI
789FileHandleGetFileName (
790 IN CONST EFI_FILE_HANDLE Handle,
791 OUT CHAR16 **FullFileName
jcarseya405b862010-09-14 05:18:09 +0000792 )
793{
jcarseyb1f95a02009-06-16 00:23:19 +0000794 EFI_STATUS Status;
795 UINTN Size;
796 EFI_FILE_HANDLE CurrentHandle;
797 EFI_FILE_HANDLE NextHigherHandle;
798 EFI_FILE_INFO *FileInfo;
799
800 Size = 0;
jcarseyb1f95a02009-06-16 00:23:19 +0000801
802 //
803 // Check our parameters
804 //
805 if (FullFileName == NULL || Handle == NULL) {
806 return (EFI_INVALID_PARAMETER);
807 }
808
jcarseyd595d4b2009-07-16 17:24:16 +0000809 *FullFileName = NULL;
jcarseya405b862010-09-14 05:18:09 +0000810 CurrentHandle = NULL;
jcarseyd595d4b2009-07-16 17:24:16 +0000811
jcarseyb1f95a02009-06-16 00:23:19 +0000812 Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
813 if (!EFI_ERROR(Status)) {
814 //
815 // Reverse out the current directory on the device
816 //
817 for (;;) {
818 FileInfo = FileHandleGetInfo(CurrentHandle);
819 if (FileInfo == NULL) {
820 Status = EFI_OUT_OF_RESOURCES;
821 break;
822 } else {
823 //
824 // We got info... do we have a name? if yes preceed the current path with it...
825 //
826 if (StrLen (FileInfo->FileName) == 0) {
jcarsey46f43bc2009-07-24 21:32:00 +0000827 if (*FullFileName == NULL) {
jcarseya405b862010-09-14 05:18:09 +0000828 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
jcarsey46f43bc2009-07-24 21:32:00 +0000829 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
830 }
jcarseyb1f95a02009-06-16 00:23:19 +0000831 FreePool(FileInfo);
832 break;
833 } else {
jcarsey46f43bc2009-07-24 21:32:00 +0000834 if (*FullFileName == NULL) {
jcarseya405b862010-09-14 05:18:09 +0000835 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
jcarsey46f43bc2009-07-24 21:32:00 +0000836 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
837 }
jcarseya405b862010-09-14 05:18:09 +0000838 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
jcarseyb1f95a02009-06-16 00:23:19 +0000839 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);
jcarsey46f43bc2009-07-24 21:32:00 +0000840 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
jcarseyb1f95a02009-06-16 00:23:19 +0000841 FreePool(FileInfo);
842 }
843 }
844 //
845 // Move to the parent directory
846 //
847 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
848 if (EFI_ERROR (Status)) {
849 break;
850 }
851
852 FileHandleClose(CurrentHandle);
853 CurrentHandle = NextHigherHandle;
854 }
jcarseya405b862010-09-14 05:18:09 +0000855 } else if (Status == EFI_NOT_FOUND) {
856 Status = EFI_SUCCESS;
857 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
858 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
jcarseyb1f95a02009-06-16 00:23:19 +0000859 }
860
Qiu Shuminad6f6f82015-08-26 02:25:42 +0000861 if (*FullFileName != NULL &&
862 (*FullFileName)[StrLen(*FullFileName) - 1] == L'\\' &&
863 StrLen(*FullFileName) > 1 &&
864 FileHandleIsDirectory(Handle) == EFI_NOT_FOUND
865 ) {
866 (*FullFileName)[StrLen(*FullFileName) - 1] = CHAR_NULL;
867 }
868
jcarseyb1f95a02009-06-16 00:23:19 +0000869 if (CurrentHandle != NULL) {
870 CurrentHandle->Close (CurrentHandle);
871 }
872
873 if (EFI_ERROR(Status) && *FullFileName != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +0000874 FreePool(*FullFileName);
jcarseyb1f95a02009-06-16 00:23:19 +0000875 }
876
877 return (Status);
878}
879
880/**
jcarseya405b862010-09-14 05:18:09 +0000881 Function to read a single line from a file. The \n is not included in the returned
jcarseyb3011f42010-01-11 21:49:04 +0000882 buffer. The returned buffer must be callee freed.
883
jcarseya405b862010-09-14 05:18:09 +0000884 If the position upon start is 0, then the Ascii Boolean will be set. This should be
jcarseyb3011f42010-01-11 21:49:04 +0000885 maintained and not changed for all operations with the same file.
886
ydong104ff7e372011-09-02 08:05:34 +0000887 @param[in] Handle FileHandle to read from.
888 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
jcarseyb3011f42010-01-11 21:49:04 +0000889
890 @return The line of text from the file.
891
892 @sa FileHandleReadLine
893**/
894CHAR16*
895EFIAPI
896FileHandleReturnLine(
897 IN EFI_FILE_HANDLE Handle,
898 IN OUT BOOLEAN *Ascii
899 )
900{
901 CHAR16 *RetVal;
902 UINTN Size;
903 EFI_STATUS Status;
904
905 Size = 0;
906 RetVal = NULL;
907
908 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
909 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey4eb59be2011-03-25 20:43:54 +0000910 RetVal = AllocateZeroPool(Size);
jcarseyb3011f42010-01-11 21:49:04 +0000911 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
912 }
913 ASSERT_EFI_ERROR(Status);
914 if (EFI_ERROR(Status) && (RetVal != NULL)) {
915 FreePool(RetVal);
916 RetVal = NULL;
917 }
918 return (RetVal);
919}
920
921/**
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000922 Function to read a single line (up to but not including the \n) from a file.
jcarseyb1f95a02009-06-16 00:23:19 +0000923
jcarseya405b862010-09-14 05:18:09 +0000924 If the position upon start is 0, then the Ascii Boolean will be set. This should be
jcarseyb3011f42010-01-11 21:49:04 +0000925 maintained and not changed for all operations with the same file.
Qiu Shuminba180622015-08-24 07:42:27 +0000926 The function will not return the \r and \n character in buffer. When an empty line is
927 read a CHAR_NULL character will be returned in buffer.
jcarseyb3011f42010-01-11 21:49:04 +0000928
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000929 @param[in] Handle FileHandle to read from.
930 @param[in, out] Buffer The pointer to buffer to read into.
931 @param[in, out] Size The pointer to number of bytes in Buffer.
932 @param[in] Truncate If the buffer is large enough, this has no effect.
933 If the buffer is is too small and Truncate is TRUE,
934 the line will be truncated.
935 If the buffer is is too small and Truncate is FALSE,
936 then no read will occur.
jcarseyb1f95a02009-06-16 00:23:19 +0000937
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000938 @param[in, out] Ascii Boolean value for indicating whether the file is
939 Ascii (TRUE) or UCS2 (FALSE).
940
941 @retval EFI_SUCCESS The operation was successful. The line is stored in
jcarseyb1f95a02009-06-16 00:23:19 +0000942 Buffer.
943 @retval EFI_INVALID_PARAMETER Handle was NULL.
jcarseyb1f95a02009-06-16 00:23:19 +0000944 @retval EFI_INVALID_PARAMETER Size was NULL.
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000945 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
946 Size was updated to the minimum space required.
jcarseyb1f95a02009-06-16 00:23:19 +0000947 @sa FileHandleRead
948**/
949EFI_STATUS
950EFIAPI
951FileHandleReadLine(
952 IN EFI_FILE_HANDLE Handle,
jcarseyb3011f42010-01-11 21:49:04 +0000953 IN OUT CHAR16 *Buffer,
jcarseyb1f95a02009-06-16 00:23:19 +0000954 IN OUT UINTN *Size,
jcarseyb3011f42010-01-11 21:49:04 +0000955 IN BOOLEAN Truncate,
956 IN OUT BOOLEAN *Ascii
jcarseya405b862010-09-14 05:18:09 +0000957 )
958{
jcarseyb1f95a02009-06-16 00:23:19 +0000959 EFI_STATUS Status;
960 CHAR16 CharBuffer;
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000961 UINT64 FileSize;
jcarseyb1f95a02009-06-16 00:23:19 +0000962 UINTN CharSize;
963 UINTN CountSoFar;
Qiu Shuminba180622015-08-24 07:42:27 +0000964 UINTN CrCount;
jcarseyb3011f42010-01-11 21:49:04 +0000965 UINT64 OriginalFilePosition;
jcarseyb1f95a02009-06-16 00:23:19 +0000966
jcarseyb1f95a02009-06-16 00:23:19 +0000967 if (Handle == NULL
jcarseyb1f95a02009-06-16 00:23:19 +0000968 ||Size == NULL
Jaben Carsey183ecff2014-06-18 16:37:16 +0000969 ||(Buffer==NULL&&*Size!=0)
jcarseya405b862010-09-14 05:18:09 +0000970 ){
971 return (EFI_INVALID_PARAMETER);
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000972 }
973
Qiu Shuminba180622015-08-24 07:42:27 +0000974 if (Buffer != NULL && *Size != 0) {
jcarseya405b862010-09-14 05:18:09 +0000975 *Buffer = CHAR_NULL;
Qiu Shuminba180622015-08-24 07:42:27 +0000976 }
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000977
978 Status = FileHandleGetSize (Handle, &FileSize);
979 if (EFI_ERROR (Status)) {
980 return Status;
981 } else if (FileSize == 0) {
Qiu Shuminba180622015-08-24 07:42:27 +0000982 *Ascii = TRUE;
Qiu Shuminbfc2fc92015-06-19 02:05:10 +0000983 return EFI_SUCCESS;
984 }
985
jcarseyb3011f42010-01-11 21:49:04 +0000986 FileHandleGetPosition(Handle, &OriginalFilePosition);
987 if (OriginalFilePosition == 0) {
988 CharSize = sizeof(CHAR16);
989 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
990 ASSERT_EFI_ERROR(Status);
jcarsey4eb59be2011-03-25 20:43:54 +0000991 if (CharBuffer == gUnicodeFileTag) {
jcarseyb3011f42010-01-11 21:49:04 +0000992 *Ascii = FALSE;
993 } else {
994 *Ascii = TRUE;
995 FileHandleSetPosition(Handle, OriginalFilePosition);
996 }
997 }
jcarseyb1f95a02009-06-16 00:23:19 +0000998
Qiu Shuminba180622015-08-24 07:42:27 +0000999 CrCount = 0;
jcarseyb1f95a02009-06-16 00:23:19 +00001000 for (CountSoFar = 0;;CountSoFar++){
jcarseyb3011f42010-01-11 21:49:04 +00001001 CharBuffer = 0;
1002 if (*Ascii) {
1003 CharSize = sizeof(CHAR8);
1004 } else {
1005 CharSize = sizeof(CHAR16);
1006 }
jcarseyb1f95a02009-06-16 00:23:19 +00001007 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
jcarseya405b862010-09-14 05:18:09 +00001008 if ( EFI_ERROR(Status)
1009 || CharSize == 0
1010 || (CharBuffer == L'\n' && !(*Ascii))
1011 || (CharBuffer == '\n' && *Ascii)
1012 ){
jcarseyb1f95a02009-06-16 00:23:19 +00001013 break;
Qiu Shuminba180622015-08-24 07:42:27 +00001014 } else if (
1015 (CharBuffer == L'\r' && !(*Ascii)) ||
1016 (CharBuffer == '\r' && *Ascii)
1017 ) {
1018 CrCount++;
1019 continue;
jcarseyb1f95a02009-06-16 00:23:19 +00001020 }
1021 //
1022 // if we have space save it...
1023 //
Qiu Shuminba180622015-08-24 07:42:27 +00001024 if ((CountSoFar+1-CrCount)*sizeof(CHAR16) < *Size){
jcarseyb3011f42010-01-11 21:49:04 +00001025 ASSERT(Buffer != NULL);
Qiu Shuminba180622015-08-24 07:42:27 +00001026 ((CHAR16*)Buffer)[CountSoFar-CrCount] = CharBuffer;
1027 ((CHAR16*)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;
jcarseyb1f95a02009-06-16 00:23:19 +00001028 }
1029 }
1030
1031 //
1032 // if we ran out of space tell when...
1033 //
Qiu Shuminba180622015-08-24 07:42:27 +00001034 if ((CountSoFar+1-CrCount)*sizeof(CHAR16) > *Size){
1035 *Size = (CountSoFar+1-CrCount)*sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00001036 if (!Truncate) {
Qiu Shuminba180622015-08-24 07:42:27 +00001037 if (Buffer != NULL && *Size != 0) {
1038 ZeroMem(Buffer, *Size);
1039 }
jcarseyb3011f42010-01-11 21:49:04 +00001040 FileHandleSetPosition(Handle, OriginalFilePosition);
Qiu Shuminba180622015-08-24 07:42:27 +00001041 return (EFI_BUFFER_TOO_SMALL);
jcarseyb1f95a02009-06-16 00:23:19 +00001042 } else {
jcarseyb3011f42010-01-11 21:49:04 +00001043 DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));
Qiu Shuminba180622015-08-24 07:42:27 +00001044 return (EFI_SUCCESS);
jcarseyb1f95a02009-06-16 00:23:19 +00001045 }
jcarseyb3011f42010-01-11 21:49:04 +00001046 }
1047
jcarseyb1f95a02009-06-16 00:23:19 +00001048 return (Status);
1049}
1050
1051/**
Qiu Shumina86c73c2015-06-24 08:09:05 +00001052 Function to write a line of text to a file.
1053
1054 If the file is a Unicode file (with UNICODE file tag) then write the unicode
1055 text.
1056 If the file is an ASCII file then write the ASCII text.
1057 If the size of file is zero (without file tag at the beginning) then write
1058 ASCII text as default.
jcarseyb1f95a02009-06-16 00:23:19 +00001059
Qiu Shuminb269f892015-06-17 04:49:47 +00001060 @param[in] Handle FileHandle to write to.
1061 @param[in] Buffer Buffer to write, if NULL the function will
1062 take no action and return EFI_SUCCESS.
jcarseyb1f95a02009-06-16 00:23:19 +00001063
Qiu Shuminb269f892015-06-17 04:49:47 +00001064 @retval EFI_SUCCESS The data was written.
1065 Buffer is NULL.
1066 @retval EFI_INVALID_PARAMETER Handle is NULL.
Qiu Shumina86c73c2015-06-24 08:09:05 +00001067 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
1068 string due to out of resources.
jcarseyb1f95a02009-06-16 00:23:19 +00001069
1070 @sa FileHandleWrite
1071**/
1072EFI_STATUS
1073EFIAPI
1074FileHandleWriteLine(
1075 IN EFI_FILE_HANDLE Handle,
1076 IN CHAR16 *Buffer
jcarseya405b862010-09-14 05:18:09 +00001077 )
1078{
Qiu Shumina86c73c2015-06-24 08:09:05 +00001079 EFI_STATUS Status;
1080 CHAR16 CharBuffer;
1081 UINTN Size;
1082 UINTN CharSize;
1083 UINT64 FileSize;
1084 UINT64 OriginalFilePosition;
1085 BOOLEAN Ascii;
1086 CHAR8 *AsciiBuffer;
jcarseyb1f95a02009-06-16 00:23:19 +00001087
jcarseyb1f95a02009-06-16 00:23:19 +00001088 if (Buffer == NULL) {
1089 return (EFI_SUCCESS);
1090 }
1091
Jaben Carsey183ecff2014-06-18 16:37:16 +00001092 if (Handle == NULL) {
1093 return (EFI_INVALID_PARAMETER);
1094 }
Qiu Shumina86c73c2015-06-24 08:09:05 +00001095
1096 Ascii = FALSE;
1097 AsciiBuffer = NULL;
1098
1099 Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
jcarseyb1f95a02009-06-16 00:23:19 +00001100 if (EFI_ERROR(Status)) {
Qiu Shumina86c73c2015-06-24 08:09:05 +00001101 return Status;
jcarseyb1f95a02009-06-16 00:23:19 +00001102 }
Qiu Shumina86c73c2015-06-24 08:09:05 +00001103
1104 Status = FileHandleSetPosition(Handle, 0);
1105 if (EFI_ERROR(Status)) {
1106 return Status;
1107 }
1108
1109 Status = FileHandleGetSize(Handle, &FileSize);
1110 if (EFI_ERROR(Status)) {
1111 return Status;
1112 }
1113
1114 if (FileSize == 0) {
1115 Ascii = TRUE;
1116 } else {
1117 CharSize = sizeof (CHAR16);
1118 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
1119 ASSERT_EFI_ERROR (Status);
1120 if (CharBuffer == gUnicodeFileTag) {
1121 Ascii = FALSE;
1122 } else {
1123 Ascii = TRUE;
1124 }
1125 }
1126
1127 Status = FileHandleSetPosition(Handle, OriginalFilePosition);
1128 if (EFI_ERROR(Status)) {
1129 return Status;
1130 }
1131
1132 if (Ascii) {
1133 Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
1134 AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
1135 if (AsciiBuffer == NULL) {
1136 return EFI_OUT_OF_RESOURCES;
1137 }
1138 UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
1139
1140 Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
1141 Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
1142 if (EFI_ERROR(Status)) {
1143 FreePool (AsciiBuffer);
1144 return (Status);
1145 }
1146 Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
1147 Status = FileHandleWrite(Handle, &Size, "\r\n");
1148 } else {
1149 if (OriginalFilePosition == 0) {
1150 Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
1151 if (EFI_ERROR(Status)) {
1152 return Status;
1153 }
1154 }
1155 Size = StrSize(Buffer) - sizeof(CHAR16);
1156 Status = FileHandleWrite(Handle, &Size, Buffer);
1157 if (EFI_ERROR(Status)) {
1158 return (Status);
1159 }
1160 Size = StrSize(L"\r\n") - sizeof(CHAR16);
1161 Status = FileHandleWrite(Handle, &Size, L"\r\n");
1162 }
1163
1164 if (AsciiBuffer != NULL) {
1165 FreePool (AsciiBuffer);
1166 }
1167 return Status;
jcarseyb1f95a02009-06-16 00:23:19 +00001168}
jcarseyb3011f42010-01-11 21:49:04 +00001169
1170/**
1171 function to take a formatted argument and print it to a file.
1172
1173 @param[in] Handle the file handle for the file to write to
1174 @param[in] Format the format argument (see printlib for format specifier)
1175 @param[in] ... the variable arguments for the format
1176
1177 @retval EFI_SUCCESS the operation was sucessful
1178 @return other a return value from FileHandleWriteLine
1179
1180 @sa FileHandleWriteLine
1181**/
1182EFI_STATUS
1183EFIAPI
1184FileHandlePrintLine(
1185 IN EFI_FILE_HANDLE Handle,
1186 IN CONST CHAR16 *Format,
1187 ...
1188 )
1189{
1190 VA_LIST Marker;
1191 CHAR16 *Buffer;
1192 EFI_STATUS Status;
1193
jcarseyb3011f42010-01-11 21:49:04 +00001194 //
1195 // Get a buffer to print into
1196 //
Daryl McDanielae591c12015-01-13 01:04:07 +00001197 Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));
Jaben Carsey183ecff2014-06-18 16:37:16 +00001198 if (Buffer == NULL) {
1199 return (EFI_OUT_OF_RESOURCES);
1200 }
jcarseyb3011f42010-01-11 21:49:04 +00001201
1202 //
1203 // Print into our buffer
1204 //
rsun33bbe68a2012-02-01 06:06:08 +00001205 VA_START (Marker, Format);
Daryl McDanielae591c12015-01-13 01:04:07 +00001206 UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);
rsun33bbe68a2012-02-01 06:06:08 +00001207 VA_END (Marker);
jcarseyb3011f42010-01-11 21:49:04 +00001208
1209 //
1210 // Print buffer into file
1211 //
1212 Status = FileHandleWriteLine(Handle, Buffer);
1213
1214 //
jcarseya405b862010-09-14 05:18:09 +00001215 // Cleanup and return
jcarseyb3011f42010-01-11 21:49:04 +00001216 //
1217 FreePool(Buffer);
1218 return (Status);
1219}
1220
1221/**
1222 Function to determine if a FILE_HANDLE is at the end of the file.
1223
1224 This will NOT work on directories.
1225
Jaben Carsey183ecff2014-06-18 16:37:16 +00001226 If Handle is NULL, then return False.
jcarseyb3011f42010-01-11 21:49:04 +00001227
1228 @param[in] Handle the file handle
1229
1230 @retval TRUE the position is at the end of the file
1231 @retval FALSE the position is not at the end of the file
1232**/
1233BOOLEAN
1234EFIAPI
1235FileHandleEof(
1236 IN EFI_FILE_HANDLE Handle
1237 )
1238{
1239 EFI_FILE_INFO *Info;
1240 UINT64 Pos;
1241 BOOLEAN RetVal;
1242
Jaben Carsey183ecff2014-06-18 16:37:16 +00001243 if (Handle == NULL) {
1244 return (FALSE);
1245 }
jcarseya405b862010-09-14 05:18:09 +00001246
jcarseyb3011f42010-01-11 21:49:04 +00001247 FileHandleGetPosition(Handle, &Pos);
1248 Info = FileHandleGetInfo (Handle);
jcarseya405b862010-09-14 05:18:09 +00001249
jcarseyb3011f42010-01-11 21:49:04 +00001250 if (Info == NULL) {
1251 return (FALSE);
jcarseya405b862010-09-14 05:18:09 +00001252 }
jcarseyb3011f42010-01-11 21:49:04 +00001253
Jaben Carsey183ecff2014-06-18 16:37:16 +00001254 FileHandleSetPosition(Handle, Pos);
1255
jcarseyd54744c2010-01-12 19:04:06 +00001256 if (Pos == Info->FileSize) {
1257 RetVal = TRUE;
1258 } else {
1259 RetVal = FALSE;
1260 }
jcarseyb3011f42010-01-11 21:49:04 +00001261
1262 FreePool (Info);
1263
1264 return (RetVal);
jcarsey22d11952010-01-13 19:07:58 +00001265}