1 | /** @file
|
---|
2 | Provides interface to EFI_FILE_HANDLE functionality.
|
---|
3 |
|
---|
4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include <Uefi.h>
|
---|
10 |
|
---|
11 | #include <Protocol/SimpleFileSystem.h>
|
---|
12 | #include <Protocol/UnicodeCollation.h>
|
---|
13 |
|
---|
14 | #include <Guid/FileInfo.h>
|
---|
15 |
|
---|
16 | #include <Library/DebugLib.h>
|
---|
17 | #include <Library/MemoryAllocationLib.h>
|
---|
18 | #include <Library/BaseLib.h>
|
---|
19 | #include <Library/BaseMemoryLib.h>
|
---|
20 | #include <Library/FileHandleLib.h>
|
---|
21 | #include <Library/PcdLib.h>
|
---|
22 | #include <Library/PrintLib.h>
|
---|
23 |
|
---|
24 | CONST UINT16 gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;
|
---|
25 |
|
---|
26 | #define MAX_FILE_NAME_LEN 522// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
|
---|
27 | #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
|
---|
28 |
|
---|
29 | /**
|
---|
30 | This function will retrieve the information about the file for the handle
|
---|
31 | specified and store it in allocated pool memory.
|
---|
32 |
|
---|
33 | This function allocates a buffer to store the file's information. It is the
|
---|
34 | caller's responsibility to free the buffer
|
---|
35 |
|
---|
36 | @param FileHandle The file handle of the file for which information is
|
---|
37 | being requested.
|
---|
38 |
|
---|
39 | @retval NULL information could not be retrieved.
|
---|
40 |
|
---|
41 | @return the information about the file
|
---|
42 | **/
|
---|
43 | EFI_FILE_INFO *
|
---|
44 | EFIAPI
|
---|
45 | FileHandleGetInfo (
|
---|
46 | IN EFI_FILE_HANDLE FileHandle
|
---|
47 | )
|
---|
48 | {
|
---|
49 | EFI_FILE_INFO *FileInfo;
|
---|
50 | UINTN FileInfoSize;
|
---|
51 | EFI_STATUS Status;
|
---|
52 |
|
---|
53 | if (FileHandle == NULL) {
|
---|
54 | return (NULL);
|
---|
55 | }
|
---|
56 |
|
---|
57 | //
|
---|
58 | // Get the required size to allocate
|
---|
59 | //
|
---|
60 | FileInfoSize = 0;
|
---|
61 | FileInfo = NULL;
|
---|
62 | Status = FileHandle->GetInfo (
|
---|
63 | FileHandle,
|
---|
64 | &gEfiFileInfoGuid,
|
---|
65 | &FileInfoSize,
|
---|
66 | NULL
|
---|
67 | );
|
---|
68 | if (Status == EFI_BUFFER_TOO_SMALL) {
|
---|
69 | //
|
---|
70 | // error is expected. getting size to allocate
|
---|
71 | //
|
---|
72 | FileInfo = AllocateZeroPool (FileInfoSize);
|
---|
73 | if (FileInfo != NULL) {
|
---|
74 | //
|
---|
75 | // now get the information
|
---|
76 | //
|
---|
77 | Status = FileHandle->GetInfo (
|
---|
78 | FileHandle,
|
---|
79 | &gEfiFileInfoGuid,
|
---|
80 | &FileInfoSize,
|
---|
81 | FileInfo
|
---|
82 | );
|
---|
83 | //
|
---|
84 | // if we got an error free the memory and return NULL
|
---|
85 | //
|
---|
86 | if (EFI_ERROR (Status)) {
|
---|
87 | FreePool (FileInfo);
|
---|
88 | FileInfo = NULL;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | return (FileInfo);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | This function sets the information about the file for the opened handle
|
---|
98 | specified.
|
---|
99 |
|
---|
100 | @param[in] FileHandle The file handle of the file for which information
|
---|
101 | is being set.
|
---|
102 |
|
---|
103 | @param[in] FileInfo The information to set.
|
---|
104 |
|
---|
105 | @retval EFI_SUCCESS The information was set.
|
---|
106 | @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
|
---|
107 | @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
|
---|
108 | @retval EFI_NO_MEDIA The device has no medium.
|
---|
109 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
110 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
111 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
---|
112 | @retval EFI_ACCESS_DENIED The file was opened read only.
|
---|
113 | @retval EFI_VOLUME_FULL The volume is full.
|
---|
114 | **/
|
---|
115 | EFI_STATUS
|
---|
116 | EFIAPI
|
---|
117 | FileHandleSetInfo (
|
---|
118 | IN EFI_FILE_HANDLE FileHandle,
|
---|
119 | IN CONST EFI_FILE_INFO *FileInfo
|
---|
120 | )
|
---|
121 | {
|
---|
122 | if ((FileHandle == NULL) || (FileInfo == NULL)) {
|
---|
123 | return (EFI_INVALID_PARAMETER);
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // Set the info
|
---|
128 | //
|
---|
129 | return (FileHandle->SetInfo (
|
---|
130 | FileHandle,
|
---|
131 | &gEfiFileInfoGuid,
|
---|
132 | (UINTN)FileInfo->Size,
|
---|
133 | (EFI_FILE_INFO *)FileInfo
|
---|
134 | ));
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | This function reads information from an opened file.
|
---|
139 |
|
---|
140 | If FileHandle is not a directory, the function reads the requested number of
|
---|
141 | bytes from the file at the file's current position and returns them in Buffer.
|
---|
142 | If the read goes beyond the end of the file, the read length is truncated to the
|
---|
143 | end of the file. The file's current position is increased by the number of bytes
|
---|
144 | returned. If FileHandle is a directory, the function reads the directory entry
|
---|
145 | at the file's current position and returns the entry in Buffer. If the Buffer
|
---|
146 | is not large enough to hold the current directory entry, then
|
---|
147 | EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
|
---|
148 | BufferSize is set to be the size of the buffer needed to read the entry. On
|
---|
149 | success, the current position is updated to the next directory entry. If there
|
---|
150 | are no more directory entries, the read returns a zero-length buffer.
|
---|
151 | EFI_FILE_INFO is the structure returned as the directory entry.
|
---|
152 |
|
---|
153 | @param FileHandle the opened file handle
|
---|
154 | @param BufferSize on input the size of buffer in bytes. on return
|
---|
155 | the number of bytes written.
|
---|
156 | @param Buffer the buffer to put read data into.
|
---|
157 |
|
---|
158 | @retval EFI_SUCCESS Data was read.
|
---|
159 | @retval EFI_NO_MEDIA The device has no media.
|
---|
160 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
161 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
162 | @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
|
---|
163 | size.
|
---|
164 |
|
---|
165 | **/
|
---|
166 | EFI_STATUS
|
---|
167 | EFIAPI
|
---|
168 | FileHandleRead (
|
---|
169 | IN EFI_FILE_HANDLE FileHandle,
|
---|
170 | IN OUT UINTN *BufferSize,
|
---|
171 | OUT VOID *Buffer
|
---|
172 | )
|
---|
173 | {
|
---|
174 | if (FileHandle == NULL) {
|
---|
175 | return (EFI_INVALID_PARAMETER);
|
---|
176 | }
|
---|
177 |
|
---|
178 | //
|
---|
179 | // Perform the read based on EFI_FILE_PROTOCOL
|
---|
180 | //
|
---|
181 | return (FileHandle->Read (FileHandle, BufferSize, Buffer));
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | Write data to a file.
|
---|
186 |
|
---|
187 | This function writes the specified number of bytes to the file at the current
|
---|
188 | file position. The current file position is advanced the actual number of bytes
|
---|
189 | written, which is returned in BufferSize. Partial writes only occur when there
|
---|
190 | has been a data error during the write attempt (such as "volume space full").
|
---|
191 | The file is automatically grown to hold the data if required. Direct writes to
|
---|
192 | opened directories are not supported.
|
---|
193 |
|
---|
194 | @param FileHandle The opened file for writing
|
---|
195 | @param BufferSize on input the number of bytes in Buffer. On output
|
---|
196 | the number of bytes written.
|
---|
197 | @param Buffer the buffer containing data to write is stored.
|
---|
198 |
|
---|
199 | @retval EFI_SUCCESS Data was written.
|
---|
200 | @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
|
---|
201 | @retval EFI_NO_MEDIA The device has no media.
|
---|
202 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
203 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
204 | @retval EFI_WRITE_PROTECTED The device is write-protected.
|
---|
205 | @retval EFI_ACCESS_DENIED The file was open for read only.
|
---|
206 | @retval EFI_VOLUME_FULL The volume is full.
|
---|
207 | **/
|
---|
208 | EFI_STATUS
|
---|
209 | EFIAPI
|
---|
210 | FileHandleWrite (
|
---|
211 | IN EFI_FILE_HANDLE FileHandle,
|
---|
212 | IN OUT UINTN *BufferSize,
|
---|
213 | IN VOID *Buffer
|
---|
214 | )
|
---|
215 | {
|
---|
216 | if (FileHandle == NULL) {
|
---|
217 | return (EFI_INVALID_PARAMETER);
|
---|
218 | }
|
---|
219 |
|
---|
220 | //
|
---|
221 | // Perform the write based on EFI_FILE_PROTOCOL
|
---|
222 | //
|
---|
223 | return (FileHandle->Write (FileHandle, BufferSize, Buffer));
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | Close an open file handle.
|
---|
228 |
|
---|
229 | This function closes a specified file handle. All "dirty" cached file data is
|
---|
230 | flushed to the device, and the file is closed. In all cases the handle is
|
---|
231 | closed.
|
---|
232 |
|
---|
233 | @param FileHandle the file handle to close.
|
---|
234 |
|
---|
235 | @retval EFI_SUCCESS the file handle was closed successfully.
|
---|
236 | **/
|
---|
237 | EFI_STATUS
|
---|
238 | EFIAPI
|
---|
239 | FileHandleClose (
|
---|
240 | IN EFI_FILE_HANDLE FileHandle
|
---|
241 | )
|
---|
242 | {
|
---|
243 | EFI_STATUS Status;
|
---|
244 |
|
---|
245 | if (FileHandle == NULL) {
|
---|
246 | return (EFI_INVALID_PARAMETER);
|
---|
247 | }
|
---|
248 |
|
---|
249 | //
|
---|
250 | // Perform the Close based on EFI_FILE_PROTOCOL
|
---|
251 | //
|
---|
252 | Status = FileHandle->Close (FileHandle);
|
---|
253 | return Status;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | Delete a file and close the handle
|
---|
258 |
|
---|
259 | This function closes and deletes a file. In all cases the file handle is closed.
|
---|
260 | If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
|
---|
261 | returned, but the handle is still closed.
|
---|
262 |
|
---|
263 | @param FileHandle the file handle to delete
|
---|
264 |
|
---|
265 | @retval EFI_SUCCESS the file was closed successfully
|
---|
266 | @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
|
---|
267 | deleted
|
---|
268 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
---|
269 | **/
|
---|
270 | EFI_STATUS
|
---|
271 | EFIAPI
|
---|
272 | FileHandleDelete (
|
---|
273 | IN EFI_FILE_HANDLE FileHandle
|
---|
274 | )
|
---|
275 | {
|
---|
276 | EFI_STATUS Status;
|
---|
277 |
|
---|
278 | if (FileHandle == NULL) {
|
---|
279 | return (EFI_INVALID_PARAMETER);
|
---|
280 | }
|
---|
281 |
|
---|
282 | //
|
---|
283 | // Perform the Delete based on EFI_FILE_PROTOCOL
|
---|
284 | //
|
---|
285 | Status = FileHandle->Delete (FileHandle);
|
---|
286 | return Status;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | Set the current position in a file.
|
---|
291 |
|
---|
292 | This function sets the current file position for the handle to the position
|
---|
293 | supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
|
---|
294 | absolute positioning is supported, and seeking past the end of the file is
|
---|
295 | allowed (a subsequent write would grow the file). Seeking to position
|
---|
296 | 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
|
---|
297 | If FileHandle is a directory, the only position that may be set is zero. This
|
---|
298 | has the effect of starting the read process of the directory entries over.
|
---|
299 |
|
---|
300 | @param FileHandle The file handle on which the position is being set
|
---|
301 | @param Position Byte position from beginning of file
|
---|
302 |
|
---|
303 | @retval EFI_SUCCESS Operation completed successfully.
|
---|
304 | @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
|
---|
305 | directories.
|
---|
306 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
---|
307 | **/
|
---|
308 | EFI_STATUS
|
---|
309 | EFIAPI
|
---|
310 | FileHandleSetPosition (
|
---|
311 | IN EFI_FILE_HANDLE FileHandle,
|
---|
312 | IN UINT64 Position
|
---|
313 | )
|
---|
314 | {
|
---|
315 | if (FileHandle == NULL) {
|
---|
316 | return (EFI_INVALID_PARAMETER);
|
---|
317 | }
|
---|
318 |
|
---|
319 | //
|
---|
320 | // Perform the SetPosition based on EFI_FILE_PROTOCOL
|
---|
321 | //
|
---|
322 | return (FileHandle->SetPosition (FileHandle, Position));
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | Gets a file's current position
|
---|
327 |
|
---|
328 | This function retrieves the current file position for the file handle. For
|
---|
329 | directories, the current file position has no meaning outside of the file
|
---|
330 | system driver and as such the operation is not supported. An error is returned
|
---|
331 | if FileHandle is a directory.
|
---|
332 |
|
---|
333 | @param FileHandle The open file handle on which to get the position.
|
---|
334 | @param Position Byte position from beginning of file.
|
---|
335 |
|
---|
336 | @retval EFI_SUCCESS the operation completed successfully.
|
---|
337 | @retval INVALID_PARAMETER One of the parameters has an invalid value.
|
---|
338 | @retval EFI_UNSUPPORTED the request is not valid on directories.
|
---|
339 | **/
|
---|
340 | EFI_STATUS
|
---|
341 | EFIAPI
|
---|
342 | FileHandleGetPosition (
|
---|
343 | IN EFI_FILE_HANDLE FileHandle,
|
---|
344 | OUT UINT64 *Position
|
---|
345 | )
|
---|
346 | {
|
---|
347 | if ((Position == NULL) || (FileHandle == NULL)) {
|
---|
348 | return (EFI_INVALID_PARAMETER);
|
---|
349 | }
|
---|
350 |
|
---|
351 | //
|
---|
352 | // Perform the GetPosition based on EFI_FILE_PROTOCOL
|
---|
353 | //
|
---|
354 | return (FileHandle->GetPosition (FileHandle, Position));
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | Flushes data on a file
|
---|
359 |
|
---|
360 | This function flushes all modified data associated with a file to a device.
|
---|
361 |
|
---|
362 | @param FileHandle The file handle on which to flush data
|
---|
363 |
|
---|
364 | @retval EFI_SUCCESS The data was flushed.
|
---|
365 | @retval EFI_NO_MEDIA The device has no media.
|
---|
366 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
367 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
368 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.
|
---|
369 | @retval EFI_ACCESS_DENIED The file was opened for read only.
|
---|
370 | **/
|
---|
371 | EFI_STATUS
|
---|
372 | EFIAPI
|
---|
373 | FileHandleFlush (
|
---|
374 | IN EFI_FILE_HANDLE FileHandle
|
---|
375 | )
|
---|
376 | {
|
---|
377 | if (FileHandle == NULL) {
|
---|
378 | return (EFI_INVALID_PARAMETER);
|
---|
379 | }
|
---|
380 |
|
---|
381 | //
|
---|
382 | // Perform the Flush based on EFI_FILE_PROTOCOL
|
---|
383 | //
|
---|
384 | return (FileHandle->Flush (FileHandle));
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | Function to determine if a given handle is a directory handle.
|
---|
389 |
|
---|
390 | Open the file information on the DirHandle and verify that the Attribute
|
---|
391 | includes EFI_FILE_DIRECTORY bit set.
|
---|
392 |
|
---|
393 | @param[in] DirHandle Handle to open file.
|
---|
394 |
|
---|
395 | @retval EFI_SUCCESS DirHandle is a directory.
|
---|
396 | @retval EFI_INVALID_PARAMETER DirHandle is NULL.
|
---|
397 | The file information returns from FileHandleGetInfo is NULL.
|
---|
398 | @retval EFI_NOT_FOUND DirHandle is not a directory.
|
---|
399 | **/
|
---|
400 | EFI_STATUS
|
---|
401 | EFIAPI
|
---|
402 | FileHandleIsDirectory (
|
---|
403 | IN EFI_FILE_HANDLE DirHandle
|
---|
404 | )
|
---|
405 | {
|
---|
406 | EFI_FILE_INFO *DirInfo;
|
---|
407 |
|
---|
408 | if (DirHandle == NULL) {
|
---|
409 | return (EFI_INVALID_PARAMETER);
|
---|
410 | }
|
---|
411 |
|
---|
412 | //
|
---|
413 | // get the file information for DirHandle
|
---|
414 | //
|
---|
415 | DirInfo = FileHandleGetInfo (DirHandle);
|
---|
416 |
|
---|
417 | //
|
---|
418 | // Parse DirInfo
|
---|
419 | //
|
---|
420 | if (DirInfo == NULL) {
|
---|
421 | //
|
---|
422 | // We got nothing...
|
---|
423 | //
|
---|
424 | return (EFI_INVALID_PARAMETER);
|
---|
425 | }
|
---|
426 |
|
---|
427 | if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
|
---|
428 | //
|
---|
429 | // Attributes say this is not a directory
|
---|
430 | //
|
---|
431 | FreePool (DirInfo);
|
---|
432 | return (EFI_NOT_FOUND);
|
---|
433 | }
|
---|
434 |
|
---|
435 | //
|
---|
436 | // all good...
|
---|
437 | //
|
---|
438 | FreePool (DirInfo);
|
---|
439 | return (EFI_SUCCESS);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** Retrieve first entry from a directory.
|
---|
443 |
|
---|
444 | This function takes an open directory handle and gets information from the
|
---|
445 | first entry in the directory. A buffer is allocated to contain
|
---|
446 | the information and a pointer to the buffer is returned in *Buffer. The
|
---|
447 | caller can use FileHandleFindNextFile() to get subsequent directory entries.
|
---|
448 |
|
---|
449 | The buffer will be freed by FileHandleFindNextFile() when the last directory
|
---|
450 | entry is read. Otherwise, the caller must free the buffer, using FreePool,
|
---|
451 | when finished with it.
|
---|
452 |
|
---|
453 | @param[in] DirHandle The file handle of the directory to search.
|
---|
454 | @param[out] Buffer The pointer to pointer to buffer for file's information.
|
---|
455 |
|
---|
456 | @retval EFI_SUCCESS Found the first file.
|
---|
457 | @retval EFI_NOT_FOUND Cannot find the directory.
|
---|
458 | @retval EFI_NO_MEDIA The device has no media.
|
---|
459 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
460 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
461 | @return Others status of FileHandleGetInfo, FileHandleSetPosition,
|
---|
462 | or FileHandleRead
|
---|
463 | **/
|
---|
464 | EFI_STATUS
|
---|
465 | EFIAPI
|
---|
466 | FileHandleFindFirstFile (
|
---|
467 | IN EFI_FILE_HANDLE DirHandle,
|
---|
468 | OUT EFI_FILE_INFO **Buffer
|
---|
469 | )
|
---|
470 | {
|
---|
471 | EFI_STATUS Status;
|
---|
472 | UINTN BufferSize;
|
---|
473 |
|
---|
474 | if ((Buffer == NULL) || (DirHandle == NULL)) {
|
---|
475 | return (EFI_INVALID_PARAMETER);
|
---|
476 | }
|
---|
477 |
|
---|
478 | //
|
---|
479 | // verify that DirHandle is a directory
|
---|
480 | //
|
---|
481 | Status = FileHandleIsDirectory (DirHandle);
|
---|
482 | if (EFI_ERROR (Status)) {
|
---|
483 | return (Status);
|
---|
484 | }
|
---|
485 |
|
---|
486 | //
|
---|
487 | // Allocate a buffer sized to struct size + enough for the string at the end
|
---|
488 | //
|
---|
489 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
|
---|
490 | *Buffer = AllocateZeroPool (BufferSize);
|
---|
491 | if (*Buffer == NULL) {
|
---|
492 | return (EFI_OUT_OF_RESOURCES);
|
---|
493 | }
|
---|
494 |
|
---|
495 | //
|
---|
496 | // reset to the beginning of the directory
|
---|
497 | //
|
---|
498 | Status = FileHandleSetPosition (DirHandle, 0);
|
---|
499 | if (EFI_ERROR (Status)) {
|
---|
500 | FreePool (*Buffer);
|
---|
501 | *Buffer = NULL;
|
---|
502 | return (Status);
|
---|
503 | }
|
---|
504 |
|
---|
505 | //
|
---|
506 | // read in the info about the first file
|
---|
507 | //
|
---|
508 | Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
|
---|
509 | ASSERT (Status != EFI_BUFFER_TOO_SMALL);
|
---|
510 | if (EFI_ERROR (Status) || (BufferSize == 0)) {
|
---|
511 | FreePool (*Buffer);
|
---|
512 | *Buffer = NULL;
|
---|
513 | if (BufferSize == 0) {
|
---|
514 | return (EFI_NOT_FOUND);
|
---|
515 | }
|
---|
516 |
|
---|
517 | return (Status);
|
---|
518 | }
|
---|
519 |
|
---|
520 | return (EFI_SUCCESS);
|
---|
521 | }
|
---|
522 |
|
---|
523 | /** Retrieve next entries from a directory.
|
---|
524 |
|
---|
525 | To use this function, the caller must first call the FileHandleFindFirstFile()
|
---|
526 | function to get the first directory entry. Subsequent directory entries are
|
---|
527 | retrieved by using the FileHandleFindNextFile() function. This function can
|
---|
528 | be called several times to get each entry from the directory. If the call of
|
---|
529 | FileHandleFindNextFile() retrieved the last directory entry, the next call of
|
---|
530 | this function will set *NoFile to TRUE and free the buffer.
|
---|
531 |
|
---|
532 | @param[in] DirHandle The file handle of the directory.
|
---|
533 | @param[out] Buffer The pointer to buffer for file's information.
|
---|
534 | @param[out] NoFile The pointer to boolean when last file is found.
|
---|
535 |
|
---|
536 | @retval EFI_SUCCESS Found the next file, or reached last file
|
---|
537 | @retval EFI_NO_MEDIA The device has no media.
|
---|
538 | @retval EFI_DEVICE_ERROR The device reported an error.
|
---|
539 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
|
---|
540 | **/
|
---|
541 | EFI_STATUS
|
---|
542 | EFIAPI
|
---|
543 | FileHandleFindNextFile (
|
---|
544 | IN EFI_FILE_HANDLE DirHandle,
|
---|
545 | OUT EFI_FILE_INFO *Buffer,
|
---|
546 | OUT BOOLEAN *NoFile
|
---|
547 | )
|
---|
548 | {
|
---|
549 | EFI_STATUS Status;
|
---|
550 | UINTN BufferSize;
|
---|
551 |
|
---|
552 | if ((DirHandle == NULL) || (Buffer == NULL) || (NoFile == NULL)) {
|
---|
553 | return (EFI_INVALID_PARAMETER);
|
---|
554 | }
|
---|
555 |
|
---|
556 | //
|
---|
557 | // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
|
---|
558 | //
|
---|
559 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
|
---|
560 |
|
---|
561 | //
|
---|
562 | // read in the info about the next file
|
---|
563 | //
|
---|
564 | Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
|
---|
565 | ASSERT (Status != EFI_BUFFER_TOO_SMALL);
|
---|
566 | if (EFI_ERROR (Status)) {
|
---|
567 | return (Status);
|
---|
568 | }
|
---|
569 |
|
---|
570 | //
|
---|
571 | // If we read 0 bytes (but did not have erros) we already read in the last file.
|
---|
572 | //
|
---|
573 | if (BufferSize == 0) {
|
---|
574 | FreePool (Buffer);
|
---|
575 | *NoFile = TRUE;
|
---|
576 | }
|
---|
577 |
|
---|
578 | return (EFI_SUCCESS);
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | Retrieve the size of a file.
|
---|
583 |
|
---|
584 | This function extracts the file size info from the FileHandle's EFI_FILE_INFO
|
---|
585 | data.
|
---|
586 |
|
---|
587 | @param[in] FileHandle The file handle from which size is retrieved.
|
---|
588 | @param[out] Size The pointer to size.
|
---|
589 |
|
---|
590 | @retval EFI_SUCCESS Operation was completed successfully.
|
---|
591 | @retval EFI_DEVICE_ERROR Cannot access the file.
|
---|
592 | @retval EFI_INVALID_PARAMETER FileHandle is NULL.
|
---|
593 | Size is NULL.
|
---|
594 | **/
|
---|
595 | EFI_STATUS
|
---|
596 | EFIAPI
|
---|
597 | FileHandleGetSize (
|
---|
598 | IN EFI_FILE_HANDLE FileHandle,
|
---|
599 | OUT UINT64 *Size
|
---|
600 | )
|
---|
601 | {
|
---|
602 | EFI_FILE_INFO *FileInfo;
|
---|
603 |
|
---|
604 | if ((FileHandle == NULL) || (Size == NULL)) {
|
---|
605 | return (EFI_INVALID_PARAMETER);
|
---|
606 | }
|
---|
607 |
|
---|
608 | //
|
---|
609 | // get the FileInfo structure
|
---|
610 | //
|
---|
611 | FileInfo = FileHandleGetInfo (FileHandle);
|
---|
612 | if (FileInfo == NULL) {
|
---|
613 | return (EFI_DEVICE_ERROR);
|
---|
614 | }
|
---|
615 |
|
---|
616 | //
|
---|
617 | // Assign the Size pointer to the correct value
|
---|
618 | //
|
---|
619 | *Size = FileInfo->FileSize;
|
---|
620 |
|
---|
621 | //
|
---|
622 | // free the FileInfo memory
|
---|
623 | //
|
---|
624 | FreePool (FileInfo);
|
---|
625 |
|
---|
626 | return (EFI_SUCCESS);
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | Set the size of a file.
|
---|
631 |
|
---|
632 | This function changes the file size info from the FileHandle's EFI_FILE_INFO
|
---|
633 | data.
|
---|
634 |
|
---|
635 | @param[in] FileHandle The file handle whose size is to be changed.
|
---|
636 | @param[in] Size The new size.
|
---|
637 |
|
---|
638 | @retval EFI_SUCCESS The operation completed successfully.
|
---|
639 | @retval EFI_DEVICE_ERROR Cannot access the file.
|
---|
640 | @retval EFI_INVALID_PARAMETER FileHandle is NULL.
|
---|
641 | **/
|
---|
642 | EFI_STATUS
|
---|
643 | EFIAPI
|
---|
644 | FileHandleSetSize (
|
---|
645 | IN EFI_FILE_HANDLE FileHandle,
|
---|
646 | IN UINT64 Size
|
---|
647 | )
|
---|
648 | {
|
---|
649 | EFI_FILE_INFO *FileInfo;
|
---|
650 | EFI_STATUS Status;
|
---|
651 |
|
---|
652 | if (FileHandle == NULL) {
|
---|
653 | return (EFI_INVALID_PARAMETER);
|
---|
654 | }
|
---|
655 |
|
---|
656 | //
|
---|
657 | // get the FileInfo structure
|
---|
658 | //
|
---|
659 | FileInfo = FileHandleGetInfo (FileHandle);
|
---|
660 | if (FileInfo == NULL) {
|
---|
661 | return (EFI_DEVICE_ERROR);
|
---|
662 | }
|
---|
663 |
|
---|
664 | //
|
---|
665 | // Assign the FileSize pointer to the new value
|
---|
666 | //
|
---|
667 | FileInfo->FileSize = Size;
|
---|
668 |
|
---|
669 | Status = FileHandleSetInfo (FileHandle, FileInfo);
|
---|
670 | //
|
---|
671 | // free the FileInfo memory
|
---|
672 | //
|
---|
673 | FreePool (FileInfo);
|
---|
674 |
|
---|
675 | return (Status);
|
---|
676 | }
|
---|
677 |
|
---|
678 | /**
|
---|
679 | Safely append (on the left) with automatic string resizing given length of Destination and
|
---|
680 | desired length of copy from Source.
|
---|
681 |
|
---|
682 | append the first D characters of Source to the end of Destination, where D is
|
---|
683 | the lesser of Count and the StrLen() of Source. If appending those D characters
|
---|
684 | will fit within Destination (whose Size is given as CurrentSize) and
|
---|
685 | still leave room for a NULL terminator, then those characters are appended,
|
---|
686 | starting at the original terminating NULL of Destination, and a new terminating
|
---|
687 | NULL is appended.
|
---|
688 |
|
---|
689 | If appending D characters onto Destination will result in a overflow of the size
|
---|
690 | given in CurrentSize the string will be grown such that the copy can be performed
|
---|
691 | and CurrentSize will be updated to the new size.
|
---|
692 |
|
---|
693 | If Source is NULL, there is nothing to append, just return the current buffer in
|
---|
694 | Destination.
|
---|
695 |
|
---|
696 | if Destination is NULL, then return error
|
---|
697 | if Destination's current length (including NULL terminator) is already more then
|
---|
698 | CurrentSize, then ASSERT()
|
---|
699 |
|
---|
700 | @param[in, out] Destination The String to append onto
|
---|
701 | @param[in, out] CurrentSize on call the number of bytes in Destination. On
|
---|
702 | return possibly the new size (still in bytes). if NULL
|
---|
703 | then allocate whatever is needed.
|
---|
704 | @param[in] Source The String to append from
|
---|
705 | @param[in] Count Maximum number of characters to append. if 0 then
|
---|
706 | all are appended.
|
---|
707 |
|
---|
708 | @return Destination return the resultant string.
|
---|
709 | **/
|
---|
710 | CHAR16 *
|
---|
711 | EFIAPI
|
---|
712 | StrnCatGrowLeft (
|
---|
713 | IN OUT CHAR16 **Destination,
|
---|
714 | IN OUT UINTN *CurrentSize,
|
---|
715 | IN CONST CHAR16 *Source,
|
---|
716 | IN UINTN Count
|
---|
717 | )
|
---|
718 | {
|
---|
719 | UINTN DestinationStartSize;
|
---|
720 | UINTN NewSize;
|
---|
721 | UINTN CopySize;
|
---|
722 |
|
---|
723 | if (Destination == NULL) {
|
---|
724 | return (NULL);
|
---|
725 | }
|
---|
726 |
|
---|
727 | //
|
---|
728 | // If there's nothing to do then just return Destination
|
---|
729 | //
|
---|
730 | if (Source == NULL) {
|
---|
731 | return (*Destination);
|
---|
732 | }
|
---|
733 |
|
---|
734 | //
|
---|
735 | // allow for NULL pointers address as Destination
|
---|
736 | //
|
---|
737 | if (*Destination != NULL) {
|
---|
738 | ASSERT (CurrentSize != 0);
|
---|
739 | DestinationStartSize = StrSize (*Destination);
|
---|
740 | ASSERT (DestinationStartSize <= *CurrentSize);
|
---|
741 | } else {
|
---|
742 | DestinationStartSize = 0;
|
---|
743 | // ASSERT(*CurrentSize == 0);
|
---|
744 | }
|
---|
745 |
|
---|
746 | //
|
---|
747 | // Append all of Source?
|
---|
748 | //
|
---|
749 | if (Count == 0) {
|
---|
750 | Count = StrSize (Source);
|
---|
751 | }
|
---|
752 |
|
---|
753 | //
|
---|
754 | // Test and grow if required
|
---|
755 | //
|
---|
756 | if (CurrentSize != NULL) {
|
---|
757 | NewSize = *CurrentSize;
|
---|
758 | while (NewSize < (DestinationStartSize + Count)) {
|
---|
759 | NewSize += 2 * Count;
|
---|
760 | }
|
---|
761 |
|
---|
762 | *Destination = ReallocatePool (*CurrentSize, NewSize, *Destination);
|
---|
763 | *CurrentSize = NewSize;
|
---|
764 | } else {
|
---|
765 | *Destination = AllocateZeroPool (Count+sizeof (CHAR16));
|
---|
766 | }
|
---|
767 |
|
---|
768 | if (*Destination == NULL) {
|
---|
769 | return NULL;
|
---|
770 | }
|
---|
771 |
|
---|
772 | CopySize = StrSize (*Destination);
|
---|
773 | CopyMem ((*Destination)+((Count-2)/sizeof (CHAR16)), *Destination, CopySize);
|
---|
774 | CopyMem (*Destination, Source, Count-2);
|
---|
775 | return (*Destination);
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
|
---|
780 | directory 'stack'. If the file is a directory, then append the '\' char at the
|
---|
781 | end of name string. If it's not a directory, then the last '\' should not be
|
---|
782 | added.
|
---|
783 |
|
---|
784 | if Handle is NULL, return EFI_INVALID_PARAMETER
|
---|
785 |
|
---|
786 | @param[in] Handle Handle to the Directory or File to create path to.
|
---|
787 | @param[out] FullFileName pointer to pointer to generated full file name. It
|
---|
788 | is the responsibility of the caller to free this memory
|
---|
789 | with a call to FreePool().
|
---|
790 | @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
|
---|
791 | @retval EFI_INVALID_PARAMETER Handle was NULL.
|
---|
792 | @retval EFI_INVALID_PARAMETER FullFileName was NULL.
|
---|
793 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
|
---|
794 | **/
|
---|
795 | EFI_STATUS
|
---|
796 | EFIAPI
|
---|
797 | FileHandleGetFileName (
|
---|
798 | IN CONST EFI_FILE_HANDLE Handle,
|
---|
799 | OUT CHAR16 **FullFileName
|
---|
800 | )
|
---|
801 | {
|
---|
802 | EFI_STATUS Status;
|
---|
803 | UINTN Size;
|
---|
804 | EFI_FILE_HANDLE CurrentHandle;
|
---|
805 | EFI_FILE_HANDLE NextHigherHandle;
|
---|
806 | EFI_FILE_INFO *FileInfo;
|
---|
807 |
|
---|
808 | Size = 0;
|
---|
809 |
|
---|
810 | //
|
---|
811 | // Check our parameters
|
---|
812 | //
|
---|
813 | if ((FullFileName == NULL) || (Handle == NULL)) {
|
---|
814 | return (EFI_INVALID_PARAMETER);
|
---|
815 | }
|
---|
816 |
|
---|
817 | *FullFileName = NULL;
|
---|
818 | CurrentHandle = NULL;
|
---|
819 |
|
---|
820 | Status = Handle->Open (Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
|
---|
821 | if (!EFI_ERROR (Status)) {
|
---|
822 | //
|
---|
823 | // Reverse out the current directory on the device
|
---|
824 | //
|
---|
825 | for ( ; ;) {
|
---|
826 | FileInfo = FileHandleGetInfo (CurrentHandle);
|
---|
827 | if (FileInfo == NULL) {
|
---|
828 | Status = EFI_OUT_OF_RESOURCES;
|
---|
829 | break;
|
---|
830 | } else {
|
---|
831 | //
|
---|
832 | // Prepare to move to the parent directory.
|
---|
833 | // Also determine whether CurrentHandle refers to the Root directory.
|
---|
834 | //
|
---|
835 | Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
|
---|
836 | //
|
---|
837 | // We got info... do we have a name? if yes precede the current path with it...
|
---|
838 | //
|
---|
839 | if ((StrLen (FileInfo->FileName) == 0) || EFI_ERROR (Status)) {
|
---|
840 | //
|
---|
841 | // Both FileInfo->FileName being '\0' and EFI_ERROR() suggest that
|
---|
842 | // CurrentHandle refers to the Root directory. As this loop ensures
|
---|
843 | // FullFileName is starting with '\\' at all times, signal success
|
---|
844 | // and exit the loop.
|
---|
845 | // While FileInfo->FileName could theoretically be a value other than
|
---|
846 | // '\0' or '\\', '\\' is guaranteed to be supported by the
|
---|
847 | // specification and hence its value can safely be ignored.
|
---|
848 | //
|
---|
849 | Status = EFI_SUCCESS;
|
---|
850 | if (*FullFileName == NULL) {
|
---|
851 | ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
|
---|
852 | *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
|
---|
853 | }
|
---|
854 |
|
---|
855 | FreePool (FileInfo);
|
---|
856 | break;
|
---|
857 | } else {
|
---|
858 | if (*FullFileName == NULL) {
|
---|
859 | ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
|
---|
860 | *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
|
---|
861 | }
|
---|
862 |
|
---|
863 | ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
|
---|
864 | *FullFileName = StrnCatGrowLeft (FullFileName, &Size, FileInfo->FileName, 0);
|
---|
865 | *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
|
---|
866 | FreePool (FileInfo);
|
---|
867 | }
|
---|
868 | }
|
---|
869 |
|
---|
870 | FileHandleClose (CurrentHandle);
|
---|
871 | //
|
---|
872 | // Move to the parent directory
|
---|
873 | //
|
---|
874 | CurrentHandle = NextHigherHandle;
|
---|
875 | }
|
---|
876 | } else if (Status == EFI_NOT_FOUND) {
|
---|
877 | Status = EFI_SUCCESS;
|
---|
878 | ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
|
---|
879 | *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
|
---|
880 | }
|
---|
881 |
|
---|
882 | if ((*FullFileName != NULL) &&
|
---|
883 | ((*FullFileName)[StrLen (*FullFileName) - 1] == L'\\') &&
|
---|
884 | (StrLen (*FullFileName) > 1) &&
|
---|
885 | (FileHandleIsDirectory (Handle) == EFI_NOT_FOUND)
|
---|
886 | )
|
---|
887 | {
|
---|
888 | (*FullFileName)[StrLen (*FullFileName) - 1] = CHAR_NULL;
|
---|
889 | }
|
---|
890 |
|
---|
891 | if (CurrentHandle != NULL) {
|
---|
892 | CurrentHandle->Close (CurrentHandle);
|
---|
893 | }
|
---|
894 |
|
---|
895 | if (EFI_ERROR (Status) && (*FullFileName != NULL)) {
|
---|
896 | FreePool (*FullFileName);
|
---|
897 | }
|
---|
898 |
|
---|
899 | return (Status);
|
---|
900 | }
|
---|
901 |
|
---|
902 | /**
|
---|
903 | Function to read a single line from a file. The \n is not included in the returned
|
---|
904 | buffer. The returned buffer must be callee freed.
|
---|
905 |
|
---|
906 | If the position upon start is 0, then the Ascii Boolean will be set. This should be
|
---|
907 | maintained and not changed for all operations with the same file.
|
---|
908 |
|
---|
909 | @param[in] Handle FileHandle to read from.
|
---|
910 | @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
|
---|
911 |
|
---|
912 | @return The line of text from the file.
|
---|
913 |
|
---|
914 | @sa FileHandleReadLine
|
---|
915 | **/
|
---|
916 | CHAR16 *
|
---|
917 | EFIAPI
|
---|
918 | FileHandleReturnLine (
|
---|
919 | IN EFI_FILE_HANDLE Handle,
|
---|
920 | IN OUT BOOLEAN *Ascii
|
---|
921 | )
|
---|
922 | {
|
---|
923 | CHAR16 *RetVal;
|
---|
924 | UINTN Size;
|
---|
925 | EFI_STATUS Status;
|
---|
926 |
|
---|
927 | Size = 0;
|
---|
928 | RetVal = NULL;
|
---|
929 |
|
---|
930 | Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);
|
---|
931 | if (Status == EFI_BUFFER_TOO_SMALL) {
|
---|
932 | RetVal = AllocateZeroPool (Size);
|
---|
933 | Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);
|
---|
934 | }
|
---|
935 |
|
---|
936 | ASSERT_EFI_ERROR (Status);
|
---|
937 | if (EFI_ERROR (Status) && (RetVal != NULL)) {
|
---|
938 | FreePool (RetVal);
|
---|
939 | RetVal = NULL;
|
---|
940 | }
|
---|
941 |
|
---|
942 | return (RetVal);
|
---|
943 | }
|
---|
944 |
|
---|
945 | /**
|
---|
946 | Function to read a single line (up to but not including the \n) from a file.
|
---|
947 |
|
---|
948 | If the position upon start is 0, then the Ascii Boolean will be set. This should be
|
---|
949 | maintained and not changed for all operations with the same file.
|
---|
950 | The function will not return the \r and \n character in buffer. When an empty line is
|
---|
951 | read a CHAR_NULL character will be returned in buffer.
|
---|
952 |
|
---|
953 | @param[in] Handle FileHandle to read from.
|
---|
954 | @param[in, out] Buffer The pointer to buffer to read into.
|
---|
955 | @param[in, out] Size The pointer to number of bytes in Buffer.
|
---|
956 | @param[in] Truncate If the buffer is large enough, this has no effect.
|
---|
957 | If the buffer is is too small and Truncate is TRUE,
|
---|
958 | the line will be truncated.
|
---|
959 | If the buffer is is too small and Truncate is FALSE,
|
---|
960 | then no read will occur.
|
---|
961 |
|
---|
962 | @param[in, out] Ascii Boolean value for indicating whether the file is
|
---|
963 | Ascii (TRUE) or UCS2 (FALSE).
|
---|
964 |
|
---|
965 | @retval EFI_SUCCESS The operation was successful. The line is stored in
|
---|
966 | Buffer.
|
---|
967 | @retval EFI_INVALID_PARAMETER Handle was NULL.
|
---|
968 | @retval EFI_INVALID_PARAMETER Size was NULL.
|
---|
969 | @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
|
---|
970 | Size was updated to the minimum space required.
|
---|
971 | @sa FileHandleRead
|
---|
972 | **/
|
---|
973 | EFI_STATUS
|
---|
974 | EFIAPI
|
---|
975 | FileHandleReadLine (
|
---|
976 | IN EFI_FILE_HANDLE Handle,
|
---|
977 | IN OUT CHAR16 *Buffer,
|
---|
978 | IN OUT UINTN *Size,
|
---|
979 | IN BOOLEAN Truncate,
|
---|
980 | IN OUT BOOLEAN *Ascii
|
---|
981 | )
|
---|
982 | {
|
---|
983 | EFI_STATUS Status;
|
---|
984 | CHAR16 CharBuffer;
|
---|
985 | UINT64 FileSize;
|
---|
986 | UINTN CharSize;
|
---|
987 | UINTN CountSoFar;
|
---|
988 | UINTN CrCount;
|
---|
989 | UINTN OldSize;
|
---|
990 | UINT64 OriginalFilePosition;
|
---|
991 |
|
---|
992 | if ( (Handle == NULL)
|
---|
993 | || (Size == NULL)
|
---|
994 | || ((Buffer == NULL) && (*Size != 0))
|
---|
995 | )
|
---|
996 | {
|
---|
997 | return (EFI_INVALID_PARAMETER);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | if ((Buffer != NULL) && (*Size != 0)) {
|
---|
1001 | *Buffer = CHAR_NULL;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | Status = FileHandleGetSize (Handle, &FileSize);
|
---|
1005 | if (EFI_ERROR (Status)) {
|
---|
1006 | return Status;
|
---|
1007 | } else if (FileSize == 0) {
|
---|
1008 | *Ascii = TRUE;
|
---|
1009 | return EFI_SUCCESS;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | FileHandleGetPosition (Handle, &OriginalFilePosition);
|
---|
1013 | if (OriginalFilePosition == 0) {
|
---|
1014 | CharSize = sizeof (CHAR16);
|
---|
1015 | Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
|
---|
1016 | ASSERT_EFI_ERROR (Status);
|
---|
1017 | if (CharBuffer == gUnicodeFileTag) {
|
---|
1018 | *Ascii = FALSE;
|
---|
1019 | } else {
|
---|
1020 | *Ascii = TRUE;
|
---|
1021 | FileHandleSetPosition (Handle, OriginalFilePosition);
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | CrCount = 0;
|
---|
1026 | for (CountSoFar = 0; ; CountSoFar++) {
|
---|
1027 | CharBuffer = 0;
|
---|
1028 | if (*Ascii) {
|
---|
1029 | CharSize = sizeof (CHAR8);
|
---|
1030 | } else {
|
---|
1031 | CharSize = sizeof (CHAR16);
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
|
---|
1035 | if ( EFI_ERROR (Status)
|
---|
1036 | || (CharSize == 0)
|
---|
1037 | || ((CharBuffer == L'\n') && !(*Ascii))
|
---|
1038 | || ((CharBuffer == '\n') && *Ascii)
|
---|
1039 | )
|
---|
1040 | {
|
---|
1041 | break;
|
---|
1042 | } else if (
|
---|
1043 | ((CharBuffer == L'\r') && !(*Ascii)) ||
|
---|
1044 | ((CharBuffer == '\r') && *Ascii)
|
---|
1045 | )
|
---|
1046 | {
|
---|
1047 | CrCount++;
|
---|
1048 | continue;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | //
|
---|
1052 | // if we have space save it...
|
---|
1053 | //
|
---|
1054 | if ((CountSoFar+1-CrCount)*sizeof (CHAR16) < *Size) {
|
---|
1055 | ASSERT (Buffer != NULL);
|
---|
1056 | ((CHAR16 *)Buffer)[CountSoFar-CrCount] = CharBuffer;
|
---|
1057 | ((CHAR16 *)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | //
|
---|
1062 | // if we ran out of space tell when...
|
---|
1063 | //
|
---|
1064 | if ((CountSoFar+1-CrCount)*sizeof (CHAR16) > *Size) {
|
---|
1065 | OldSize = *Size;
|
---|
1066 | *Size = (CountSoFar+1-CrCount)*sizeof (CHAR16);
|
---|
1067 | if (!Truncate) {
|
---|
1068 | if ((Buffer != NULL) && (OldSize != 0)) {
|
---|
1069 | ZeroMem (Buffer, OldSize);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | FileHandleSetPosition (Handle, OriginalFilePosition);
|
---|
1073 | return (EFI_BUFFER_TOO_SMALL);
|
---|
1074 | } else {
|
---|
1075 | DEBUG ((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));
|
---|
1076 | return (EFI_SUCCESS);
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | return (Status);
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | /**
|
---|
1084 | Function to write a line of text to a file.
|
---|
1085 |
|
---|
1086 | If the file is a Unicode file (with UNICODE file tag) then write the unicode
|
---|
1087 | text.
|
---|
1088 | If the file is an ASCII file then write the ASCII text.
|
---|
1089 | If the size of file is zero (without file tag at the beginning) then write
|
---|
1090 | ASCII text as default.
|
---|
1091 |
|
---|
1092 | @param[in] Handle FileHandle to write to.
|
---|
1093 | @param[in] Buffer Buffer to write, if NULL the function will
|
---|
1094 | take no action and return EFI_SUCCESS.
|
---|
1095 |
|
---|
1096 | @retval EFI_SUCCESS The data was written.
|
---|
1097 | Buffer is NULL.
|
---|
1098 | @retval EFI_INVALID_PARAMETER Handle is NULL.
|
---|
1099 | @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
|
---|
1100 | string due to out of resources.
|
---|
1101 |
|
---|
1102 | @sa FileHandleWrite
|
---|
1103 | **/
|
---|
1104 | EFI_STATUS
|
---|
1105 | EFIAPI
|
---|
1106 | FileHandleWriteLine (
|
---|
1107 | IN EFI_FILE_HANDLE Handle,
|
---|
1108 | IN CHAR16 *Buffer
|
---|
1109 | )
|
---|
1110 | {
|
---|
1111 | EFI_STATUS Status;
|
---|
1112 | CHAR16 CharBuffer;
|
---|
1113 | UINTN Size;
|
---|
1114 | UINTN Index;
|
---|
1115 | UINTN CharSize;
|
---|
1116 | UINT64 FileSize;
|
---|
1117 | UINT64 OriginalFilePosition;
|
---|
1118 | BOOLEAN Ascii;
|
---|
1119 | CHAR8 *AsciiBuffer;
|
---|
1120 |
|
---|
1121 | if (Buffer == NULL) {
|
---|
1122 | return (EFI_SUCCESS);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | if (Handle == NULL) {
|
---|
1126 | return (EFI_INVALID_PARAMETER);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | Ascii = FALSE;
|
---|
1130 | AsciiBuffer = NULL;
|
---|
1131 |
|
---|
1132 | Status = FileHandleGetPosition (Handle, &OriginalFilePosition);
|
---|
1133 | if (EFI_ERROR (Status)) {
|
---|
1134 | return Status;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | Status = FileHandleSetPosition (Handle, 0);
|
---|
1138 | if (EFI_ERROR (Status)) {
|
---|
1139 | return Status;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | Status = FileHandleGetSize (Handle, &FileSize);
|
---|
1143 | if (EFI_ERROR (Status)) {
|
---|
1144 | return Status;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | if (FileSize == 0) {
|
---|
1148 | Ascii = TRUE;
|
---|
1149 | } else {
|
---|
1150 | CharSize = sizeof (CHAR16);
|
---|
1151 | Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
|
---|
1152 | ASSERT_EFI_ERROR (Status);
|
---|
1153 | if (CharBuffer == gUnicodeFileTag) {
|
---|
1154 | Ascii = FALSE;
|
---|
1155 | } else {
|
---|
1156 | Ascii = TRUE;
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | Status = FileHandleSetPosition (Handle, OriginalFilePosition);
|
---|
1161 | if (EFI_ERROR (Status)) {
|
---|
1162 | return Status;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | if (Ascii) {
|
---|
1166 | Size = (StrSize (Buffer) / sizeof (CHAR16)) * sizeof (CHAR8);
|
---|
1167 | AsciiBuffer = (CHAR8 *)AllocateZeroPool (Size);
|
---|
1168 | if (AsciiBuffer == NULL) {
|
---|
1169 | return EFI_OUT_OF_RESOURCES;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size);
|
---|
1173 | for (Index = 0; Index < Size; Index++) {
|
---|
1174 | if ((AsciiBuffer[Index] & BIT7) != 0) {
|
---|
1175 | FreePool (AsciiBuffer);
|
---|
1176 | return EFI_INVALID_PARAMETER;
|
---|
1177 | }
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | Size = AsciiStrSize (AsciiBuffer) - sizeof (CHAR8);
|
---|
1181 | Status = FileHandleWrite (Handle, &Size, AsciiBuffer);
|
---|
1182 | if (EFI_ERROR (Status)) {
|
---|
1183 | FreePool (AsciiBuffer);
|
---|
1184 | return (Status);
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | Size = AsciiStrSize ("\r\n") - sizeof (CHAR8);
|
---|
1188 | Status = FileHandleWrite (Handle, &Size, "\r\n");
|
---|
1189 | } else {
|
---|
1190 | if (OriginalFilePosition == 0) {
|
---|
1191 | Status = FileHandleSetPosition (Handle, sizeof (CHAR16));
|
---|
1192 | if (EFI_ERROR (Status)) {
|
---|
1193 | return Status;
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | Size = StrSize (Buffer) - sizeof (CHAR16);
|
---|
1198 | Status = FileHandleWrite (Handle, &Size, Buffer);
|
---|
1199 | if (EFI_ERROR (Status)) {
|
---|
1200 | return (Status);
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | Size = StrSize (L"\r\n") - sizeof (CHAR16);
|
---|
1204 | Status = FileHandleWrite (Handle, &Size, L"\r\n");
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | if (AsciiBuffer != NULL) {
|
---|
1208 | FreePool (AsciiBuffer);
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | return Status;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | function to take a formatted argument and print it to a file.
|
---|
1216 |
|
---|
1217 | @param[in] Handle the file handle for the file to write to
|
---|
1218 | @param[in] Format the format argument (see printlib for format specifier)
|
---|
1219 | @param[in] ... the variable arguments for the format
|
---|
1220 |
|
---|
1221 | @retval EFI_SUCCESS the operation was successful
|
---|
1222 | @return other a return value from FileHandleWriteLine
|
---|
1223 |
|
---|
1224 | @sa FileHandleWriteLine
|
---|
1225 | **/
|
---|
1226 | EFI_STATUS
|
---|
1227 | EFIAPI
|
---|
1228 | FileHandlePrintLine (
|
---|
1229 | IN EFI_FILE_HANDLE Handle,
|
---|
1230 | IN CONST CHAR16 *Format,
|
---|
1231 | ...
|
---|
1232 | )
|
---|
1233 | {
|
---|
1234 | VA_LIST Marker;
|
---|
1235 | CHAR16 *Buffer;
|
---|
1236 | EFI_STATUS Status;
|
---|
1237 |
|
---|
1238 | //
|
---|
1239 | // Get a buffer to print into
|
---|
1240 | //
|
---|
1241 | Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));
|
---|
1242 | if (Buffer == NULL) {
|
---|
1243 | return (EFI_OUT_OF_RESOURCES);
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | //
|
---|
1247 | // Print into our buffer
|
---|
1248 | //
|
---|
1249 | VA_START (Marker, Format);
|
---|
1250 | UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);
|
---|
1251 | VA_END (Marker);
|
---|
1252 |
|
---|
1253 | //
|
---|
1254 | // Print buffer into file
|
---|
1255 | //
|
---|
1256 | Status = FileHandleWriteLine (Handle, Buffer);
|
---|
1257 |
|
---|
1258 | //
|
---|
1259 | // Cleanup and return
|
---|
1260 | //
|
---|
1261 | FreePool (Buffer);
|
---|
1262 | return (Status);
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | /**
|
---|
1266 | Function to determine if a FILE_HANDLE is at the end of the file.
|
---|
1267 |
|
---|
1268 | This will NOT work on directories.
|
---|
1269 |
|
---|
1270 | If Handle is NULL, then return False.
|
---|
1271 |
|
---|
1272 | @param[in] Handle the file handle
|
---|
1273 |
|
---|
1274 | @retval TRUE the position is at the end of the file
|
---|
1275 | @retval FALSE the position is not at the end of the file
|
---|
1276 | **/
|
---|
1277 | BOOLEAN
|
---|
1278 | EFIAPI
|
---|
1279 | FileHandleEof (
|
---|
1280 | IN EFI_FILE_HANDLE Handle
|
---|
1281 | )
|
---|
1282 | {
|
---|
1283 | EFI_FILE_INFO *Info;
|
---|
1284 | UINT64 Pos;
|
---|
1285 | BOOLEAN RetVal;
|
---|
1286 |
|
---|
1287 | if (Handle == NULL) {
|
---|
1288 | return (FALSE);
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | FileHandleGetPosition (Handle, &Pos);
|
---|
1292 | Info = FileHandleGetInfo (Handle);
|
---|
1293 |
|
---|
1294 | if (Info == NULL) {
|
---|
1295 | return (FALSE);
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | FileHandleSetPosition (Handle, Pos);
|
---|
1299 |
|
---|
1300 | if (Pos == Info->FileSize) {
|
---|
1301 | RetVal = TRUE;
|
---|
1302 | } else {
|
---|
1303 | RetVal = FALSE;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | FreePool (Info);
|
---|
1307 |
|
---|
1308 | return (RetVal);
|
---|
1309 | }
|
---|