8516588605d159e6b2b35cddbb232d198a1973d2
[refind] / filesystems / fsw_efi.c
1 /**
2 * \file fsw_efi.c
3 * EFI host environment code.
4 */
5
6 /*-
7 * Copyright (c) 2006 Christoph Pfisterer
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the
19 * distribution.
20 *
21 * * Neither the name of Christoph Pfisterer nor the names of the
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 /*
38 * Changes by Roderick Smith are licensed under the preceding terms.
39 */
40
41 #include "fsw_efi.h"
42 #include "fsw_core.h"
43 #ifdef __MAKEWITH_GNUEFI
44 #include "edk2/DriverBinding.h"
45 #include "edk2/ComponentName.h"
46 #endif
47 #include "../include/refit_call_wrapper.h"
48
49 #define DEBUG_LEVEL 0
50
51 #ifndef FSTYPE
52 /** The file system type name to use. */
53 #define FSTYPE ext2
54 #endif
55
56 #ifdef __MAKEWITH_GNUEFI
57
58 #define EFI_DISK_IO_PROTOCOL_GUID \
59 { \
60 0xce345171, 0xba0b, 0x11d2, {0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
61 }
62
63 #define EFI_BLOCK_IO_PROTOCOL_GUID \
64 { \
65 0x964e5b21, 0x6459, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
66 }
67
68 EFI_GUID gEfiDriverBindingProtocolGuid = EFI_DRIVER_BINDING_PROTOCOL_GUID;
69 EFI_GUID gEfiComponentNameProtocolGuid = EFI_COMPONENT_NAME_PROTOCOL_GUID;
70 EFI_GUID gEfiDiskIoProtocolGuid = EFI_DISK_IO_PROTOCOL_GUID;
71 EFI_GUID gEfiBlockIoProtocolGuid = EFI_BLOCK_IO_PROTOCOL_GUID;
72 EFI_GUID gEfiFileInfoGuid = EFI_FILE_INFO_ID;
73 EFI_GUID gEfiFileSystemInfoGuid = EFI_FILE_SYSTEM_INFO_ID;
74 EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid = EFI_FILE_SYSTEM_VOLUME_LABEL_INFO_ID;
75 #define gEfiSimpleFileSystemProtocolGuid FileSystemProtocol
76 #endif
77
78 /** Helper macro for stringification. */
79 #define FSW_EFI_STRINGIFY(x) #x
80 /** Expands to the EFI driver name given the file system type name. */
81 #define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.9.1 " FSW_EFI_STRINGIFY(t) L" File System Driver"
82
83 // function prototypes
84
85 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL *This,
86 IN EFI_HANDLE ControllerHandle,
87 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath);
88 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL *This,
89 IN EFI_HANDLE ControllerHandle,
90 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath);
91 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL *This,
92 IN EFI_HANDLE ControllerHandle,
93 IN UINTN NumberOfChildren,
94 IN EFI_HANDLE *ChildHandleBuffer);
95
96 EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
97 IN CHAR8 *Language,
98 OUT CHAR16 **DriverName);
99 EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
100 IN EFI_HANDLE ControllerHandle,
101 IN EFI_HANDLE ChildHandle OPTIONAL,
102 IN CHAR8 *Language,
103 OUT CHAR16 **ControllerName);
104
105 void fsw_efi_change_blocksize(struct fsw_volume *vol,
106 fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
107 fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize);
108 fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer);
109
110 EFI_STATUS fsw_efi_map_status(fsw_status_t fsw_status, FSW_VOLUME_DATA *Volume);
111
112 EFI_STATUS EFIAPI fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE *This,
113 OUT EFI_FILE **Root);
114 EFI_STATUS fsw_efi_dnode_to_FileHandle(IN struct fsw_dnode *dno,
115 OUT EFI_FILE **NewFileHandle);
116
117 EFI_STATUS fsw_efi_file_read(IN FSW_FILE_DATA *File,
118 IN OUT UINTN *BufferSize,
119 OUT VOID *Buffer);
120 EFI_STATUS fsw_efi_file_getpos(IN FSW_FILE_DATA *File,
121 OUT UINT64 *Position);
122 EFI_STATUS fsw_efi_file_setpos(IN FSW_FILE_DATA *File,
123 IN UINT64 Position);
124
125 EFI_STATUS fsw_efi_dir_open(IN FSW_FILE_DATA *File,
126 OUT EFI_FILE **NewHandle,
127 IN CHAR16 *FileName,
128 IN UINT64 OpenMode,
129 IN UINT64 Attributes);
130 EFI_STATUS fsw_efi_dir_read(IN FSW_FILE_DATA *File,
131 IN OUT UINTN *BufferSize,
132 OUT VOID *Buffer);
133 EFI_STATUS fsw_efi_dir_setpos(IN FSW_FILE_DATA *File,
134 IN UINT64 Position);
135
136 EFI_STATUS fsw_efi_dnode_getinfo(IN FSW_FILE_DATA *File,
137 IN EFI_GUID *InformationType,
138 IN OUT UINTN *BufferSize,
139 OUT VOID *Buffer);
140 EFI_STATUS fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA *Volume,
141 IN struct fsw_dnode *dno,
142 IN OUT UINTN *BufferSize,
143 OUT VOID *Buffer);
144
145 /**
146 * Structure for holding disk cache data.
147 */
148
149 #define CACHE_SIZE 131072 /* 128KiB */
150 struct cache_data {
151 fsw_u8 *Cache;
152 fsw_u64 CacheStart;
153 BOOLEAN CacheValid;
154 FSW_VOLUME_DATA *Volume; // NOTE: Do not deallocate; copied here to ID volume
155 };
156
157 #define NUM_CACHES 2 /* Don't increase without modifying fsw_efi_read_block() */
158 static struct cache_data Caches[NUM_CACHES];
159 static int LastRead = -1;
160
161 /**
162 * Interface structure for the EFI Driver Binding protocol.
163 */
164
165 EFI_DRIVER_BINDING_PROTOCOL fsw_efi_DriverBinding_table = {
166 fsw_efi_DriverBinding_Supported,
167 fsw_efi_DriverBinding_Start,
168 fsw_efi_DriverBinding_Stop,
169 0x10,
170 NULL,
171 NULL
172 };
173
174 /**
175 * Interface structure for the EFI Component Name protocol.
176 */
177
178 EFI_COMPONENT_NAME_PROTOCOL fsw_efi_ComponentName_table = {
179 fsw_efi_ComponentName_GetDriverName,
180 fsw_efi_ComponentName_GetControllerName,
181 (CHAR8*) "eng"
182 };
183
184 /**
185 * Dispatch table for our FSW host driver.
186 */
187
188 struct fsw_host_table fsw_efi_host_table = {
189 FSW_STRING_TYPE_UTF16,
190
191 fsw_efi_change_blocksize,
192 fsw_efi_read_block
193 };
194
195 extern struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(FSTYPE);
196
197
198 static VOID EFIAPI fsw_efi_clear_cache(VOID) {
199 int i;
200
201 // clear the cache
202 for (i = 0; i < NUM_CACHES; i++) {
203 if (Caches[i].Cache != NULL) {
204 FreePool(Caches[i].Cache);
205 Caches[i].Cache = NULL;
206 } // if
207 Caches[i].CacheStart = 0;
208 Caches[i].CacheValid = FALSE;
209 Caches[i].Volume = NULL;
210 }
211 LastRead = -1;
212 } // VOID EFIAPI fsw_efi_clear_cache();
213
214 /**
215 * Image entry point. Installs the Driver Binding and Component Name protocols
216 * on the image's handle. Actually mounting a file system is initiated through
217 * the Driver Binding protocol at the firmware's request.
218 */
219 EFI_STATUS EFIAPI fsw_efi_main(IN EFI_HANDLE ImageHandle,
220 IN EFI_SYSTEM_TABLE *SystemTable)
221 {
222 EFI_STATUS Status;
223
224 #ifndef HOST_EFI_EDK2
225 // Not available in EDK2 toolkit
226 InitializeLib(ImageHandle, SystemTable);
227 #endif
228
229 // complete Driver Binding protocol instance
230 fsw_efi_DriverBinding_table.ImageHandle = ImageHandle;
231 fsw_efi_DriverBinding_table.DriverBindingHandle = ImageHandle;
232 // install Driver Binding protocol
233 Status = refit_call4_wrapper(BS->InstallProtocolInterface, &fsw_efi_DriverBinding_table.DriverBindingHandle,
234 &gEfiDriverBindingProtocolGuid,
235 EFI_NATIVE_INTERFACE,
236 &fsw_efi_DriverBinding_table);
237 if (EFI_ERROR (Status)) {
238 return Status;
239 }
240
241 // install Component Name protocol
242 Status = refit_call4_wrapper(BS->InstallProtocolInterface, &fsw_efi_DriverBinding_table.DriverBindingHandle,
243 &gEfiComponentNameProtocolGuid,
244 EFI_NATIVE_INTERFACE,
245 &fsw_efi_ComponentName_table);
246 if (EFI_ERROR (Status)) {
247 return Status;
248 }
249
250 // OverrideFunctions();
251 // Msg = NULL;
252 // msgCursor = NULL;
253 // Status = BS->LocateProtocol(&gMsgLogProtocolGuid, NULL, (VOID **) &Msg);
254 // if (!EFI_ERROR(Status) && (Msg != NULL)) {
255 // msgCursor = Msg->Cursor;
256 // BootLog("MsgLog installed into VBoxFs\n");
257 // }
258
259 return EFI_SUCCESS;
260 }
261
262 #ifdef __MAKEWITH_GNUEFI
263 EFI_DRIVER_ENTRY_POINT(fsw_efi_main)
264 #endif
265
266 /**
267 * Driver Binding EFI protocol, Supported function. This function is called by EFI
268 * to test if this driver can handle a certain device. Our implementation only checks
269 * if the device is a disk (i.e. that it supports the Block I/O and Disk I/O protocols)
270 * and implicitly checks if the disk is already in use by another driver.
271 */
272
273 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL *This,
274 IN EFI_HANDLE ControllerHandle,
275 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
276 {
277 EFI_STATUS Status;
278 EFI_DISK_IO *DiskIo;
279
280 // we check for both DiskIO and BlockIO protocols
281
282 // first, open DiskIO
283 Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
284 &gEfiDiskIoProtocolGuid,
285 (VOID **) &DiskIo,
286 This->DriverBindingHandle,
287 ControllerHandle,
288 EFI_OPEN_PROTOCOL_BY_DRIVER);
289 if (EFI_ERROR(Status))
290 return Status;
291
292 // we were just checking, close it again
293 refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
294 &gEfiDiskIoProtocolGuid,
295 This->DriverBindingHandle,
296 ControllerHandle);
297
298 // next, check BlockIO without actually opening it
299 Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
300 &gEfiBlockIoProtocolGuid,
301 NULL,
302 This->DriverBindingHandle,
303 ControllerHandle,
304 EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
305 return Status;
306 }
307
308 /**
309 * Driver Binding EFI protocol, Start function. This function is called by EFI
310 * to start driving the given device. It is still possible at this point to
311 * return EFI_UNSUPPORTED, and in fact we will do so if the file system driver
312 * cannot find the superblock signature (or equivalent) that it expects.
313 *
314 * This function allocates memory for a per-volume structure, opens the
315 * required protocols (just Disk I/O in our case, Block I/O is only looked
316 * at to get the MediaId field), and lets the FSW core mount the file system.
317 * If successful, an EFI Simple File System protocol is exported on the
318 * device handle.
319 */
320
321 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL *This,
322 IN EFI_HANDLE ControllerHandle,
323 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
324 {
325 EFI_STATUS Status;
326 EFI_BLOCK_IO *BlockIo;
327 EFI_DISK_IO *DiskIo;
328 FSW_VOLUME_DATA *Volume;
329
330 #if DEBUG_LEVEL
331 Print(L"fsw_efi_DriverBinding_Start\n");
332 #endif
333
334 // open consumed protocols
335 Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
336 &gEfiBlockIoProtocolGuid,
337 (VOID **) &BlockIo,
338 This->DriverBindingHandle,
339 ControllerHandle,
340 EFI_OPEN_PROTOCOL_GET_PROTOCOL); // NOTE: we only want to look at the MediaId
341 if (EFI_ERROR(Status)) {
342 // Print(L"Fsw ERROR: OpenProtocol(BlockIo) returned %x\n", Status);
343 return Status;
344 }
345
346 Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
347 &gEfiDiskIoProtocolGuid,
348 (VOID **) &DiskIo,
349 This->DriverBindingHandle,
350 ControllerHandle,
351 EFI_OPEN_PROTOCOL_BY_DRIVER);
352 if (EFI_ERROR(Status)) {
353 Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %x\n", Status);
354 return Status;
355 }
356
357 // allocate volume structure
358 Volume = AllocateZeroPool(sizeof(FSW_VOLUME_DATA));
359 Volume->Signature = FSW_VOLUME_DATA_SIGNATURE;
360 Volume->Handle = ControllerHandle;
361 Volume->DiskIo = DiskIo;
362 Volume->MediaId = BlockIo->Media->MediaId;
363 Volume->LastIOStatus = EFI_SUCCESS;
364
365 // mount the filesystem
366 Status = fsw_efi_map_status(fsw_mount(Volume, &fsw_efi_host_table,
367 &FSW_FSTYPE_TABLE_NAME(FSTYPE), &Volume->vol),
368 Volume);
369
370 if (!EFI_ERROR(Status)) {
371 // register the SimpleFileSystem protocol
372 Volume->FileSystem.Revision = EFI_FILE_IO_INTERFACE_REVISION;
373 Volume->FileSystem.OpenVolume = fsw_efi_FileSystem_OpenVolume;
374 Status = refit_call4_wrapper(BS->InstallMultipleProtocolInterfaces, &ControllerHandle,
375 &gEfiSimpleFileSystemProtocolGuid,
376 &Volume->FileSystem,
377 NULL);
378 if (EFI_ERROR(Status)) {
379 // Print(L"Fsw ERROR: InstallMultipleProtocolInterfaces returned %x\n", Status);
380 }
381 }
382
383 // on errors, close the opened protocols
384 if (EFI_ERROR(Status)) {
385 if (Volume->vol != NULL)
386 fsw_unmount(Volume->vol);
387 FreePool(Volume);
388
389 refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
390 &gEfiDiskIoProtocolGuid,
391 This->DriverBindingHandle,
392 ControllerHandle);
393 }
394
395 return Status;
396 }
397
398 /**
399 * Driver Binding EFI protocol, Stop function. This function is called by EFI
400 * to stop the driver on the given device. This translates to an unmount
401 * call for the FSW core.
402 *
403 * We assume that all file handles on the volume have been closed before
404 * the driver is stopped. At least with the EFI shell, that is actually the
405 * case; it closes all file handles between commands.
406 */
407
408 EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL *This,
409 IN EFI_HANDLE ControllerHandle,
410 IN UINTN NumberOfChildren,
411 IN EFI_HANDLE *ChildHandleBuffer)
412 {
413 EFI_STATUS Status;
414 EFI_FILE_IO_INTERFACE *FileSystem;
415 FSW_VOLUME_DATA *Volume;
416
417 #if DEBUG_LEVEL
418 Print(L"fsw_efi_DriverBinding_Stop\n");
419 #endif
420
421 // get the installed SimpleFileSystem interface
422 Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
423 &gEfiSimpleFileSystemProtocolGuid,
424 (VOID **) &FileSystem,
425 This->DriverBindingHandle,
426 ControllerHandle,
427 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
428 if (EFI_ERROR(Status))
429 return EFI_UNSUPPORTED;
430
431 // get private data structure
432 Volume = FSW_VOLUME_FROM_FILE_SYSTEM(FileSystem);
433
434 // uninstall Simple File System protocol
435 Status = refit_call4_wrapper(BS->UninstallMultipleProtocolInterfaces, ControllerHandle,
436 &gEfiSimpleFileSystemProtocolGuid, &Volume->FileSystem,
437 NULL);
438 if (EFI_ERROR(Status)) {
439 // Print(L"Fsw ERROR: UninstallMultipleProtocolInterfaces returned %x\n", Status);
440 return Status;
441 }
442 #if DEBUG_LEVEL
443 Print(L"fsw_efi_DriverBinding_Stop: protocol uninstalled successfully\n");
444 #endif
445
446 // release private data structure
447 if (Volume->vol != NULL)
448 fsw_unmount(Volume->vol);
449 FreePool(Volume);
450
451 // close the consumed protocols
452 Status = refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
453 &gEfiDiskIoProtocolGuid,
454 This->DriverBindingHandle,
455 ControllerHandle);
456
457 // clear the cache
458 fsw_efi_clear_cache();
459
460 return Status;
461 }
462
463 /**
464 * Component Name EFI protocol, GetDriverName function. Used by the EFI
465 * environment to inquire the name of this driver. The name returned is
466 * based on the file system type actually used in compilation.
467 */
468
469 EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
470 IN CHAR8 *Language,
471 OUT CHAR16 **DriverName)
472 {
473 if (Language == NULL || DriverName == NULL)
474 return EFI_INVALID_PARAMETER;
475
476 if (Language[0] == 'e' && Language[1] == 'n' && Language[2] == 'g' && Language[3] == 0) {
477 *DriverName = FSW_EFI_DRIVER_NAME(FSTYPE);
478 return EFI_SUCCESS;
479 }
480 return EFI_UNSUPPORTED;
481 }
482
483 /**
484 * Component Name EFI protocol, GetControllerName function. Not implemented
485 * because this is not a "bus" driver in the sense of the EFI Driver Model.
486 */
487
488 EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
489 IN EFI_HANDLE ControllerHandle,
490 IN EFI_HANDLE ChildHandle OPTIONAL,
491 IN CHAR8 *Language,
492 OUT CHAR16 **ControllerName)
493 {
494 return EFI_UNSUPPORTED;
495 }
496
497 /**
498 * FSW interface function for block size changes. This function is called by the FSW core
499 * when the file system driver changes the block sizes for the volume.
500 */
501
502 void fsw_efi_change_blocksize(struct fsw_volume *vol,
503 fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
504 fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize)
505 {
506 // nothing to do
507 }
508
509 /**
510 * FSW interface function to read data blocks. This function is called by the FSW core
511 * to read a block of data from the device. The buffer is allocated by the core code.
512 * Two caches are maintained, so as to improve performance on some systems. (VirtualBox
513 * is particularly susceptible to performance problems with an uncached driver -- the
514 * ext2 driver can take 200 seconds to load a Linux kernel under VirtualBox, whereas
515 * the time is more like 3 seconds with a cache!) Two independent caches are maintained
516 * because the ext2fs driver tends to alternate between accessing two parts of the
517 * disk.
518 */
519
520 fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) {
521 int i, ReadCache = -1;
522 FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)vol->host_data;
523 EFI_STATUS Status = EFI_SUCCESS;
524 BOOLEAN ReadOneBlock = FALSE;
525 fsw_u64 StartRead = phys_bno * vol->phys_blocksize;
526
527 if (buffer == NULL)
528 return (fsw_status_t) EFI_BAD_BUFFER_SIZE;
529
530 // Initialize static data structures, if necessary....
531 if (LastRead < 0) {
532 fsw_efi_clear_cache();
533 } // if
534
535 // Look for a cache hit on the current query....
536 i = 0;
537 do {
538 if ((Caches[i].Volume == Volume) &&
539 (Caches[i].CacheValid == TRUE) &&
540 (StartRead >= Caches[i].CacheStart) &&
541 ((StartRead + vol->phys_blocksize) <= (Caches[i].CacheStart + CACHE_SIZE))) {
542 ReadCache = i;
543 }
544 i++;
545 } while ((i < NUM_CACHES) && (ReadCache < 0));
546
547 // No cache hit found; load new cache and pass it on....
548 if (ReadCache < 0) {
549 if (LastRead == -1)
550 LastRead = 1;
551 ReadCache = 1 - LastRead; // NOTE: If NUM_CACHES > 2, this must become more complex
552 Caches[ReadCache].CacheValid = FALSE;
553 if (Caches[ReadCache].Cache == NULL)
554 Caches[ReadCache].Cache = AllocatePool(CACHE_SIZE);
555 if (Caches[ReadCache].Cache != NULL) {
556 Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
557 StartRead, CACHE_SIZE, Caches[ReadCache].Cache);
558 if (!EFI_ERROR(Status)) {
559 Caches[ReadCache].CacheStart = StartRead;
560 Caches[ReadCache].CacheValid = TRUE;
561 Caches[ReadCache].Volume = Volume;
562 LastRead = ReadCache;
563 } else {
564 ReadOneBlock = TRUE;
565 }
566 } else {
567 ReadOneBlock = TRUE;
568 } // if cache memory allocated
569 } // if (ReadCache < 0)
570
571 if (Caches[ReadCache].Cache != NULL && Caches[ReadCache].CacheValid == TRUE && vol->phys_blocksize > 0) {
572 CopyMem(buffer, &Caches[ReadCache].Cache[StartRead - Caches[ReadCache].CacheStart], vol->phys_blocksize);
573 } else {
574 ReadOneBlock = TRUE;
575 }
576
577 if (ReadOneBlock) { // Something's failed, so try a simple disk read of one block....
578 Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
579 phys_bno * vol->phys_blocksize,
580 vol->phys_blocksize,
581 buffer);
582 }
583 Volume->LastIOStatus = Status;
584
585 return Status;
586 } // fsw_status_t *fsw_efi_read_block()
587
588 /**
589 * Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
590 * by fsw_efi_read_block, so we map it back to the EFI status code remembered from
591 * the last I/O operation.
592 */
593
594 EFI_STATUS fsw_efi_map_status(fsw_status_t fsw_status, FSW_VOLUME_DATA *Volume)
595 {
596 switch (fsw_status) {
597 case FSW_SUCCESS:
598 return EFI_SUCCESS;
599 case FSW_OUT_OF_MEMORY:
600 return EFI_VOLUME_CORRUPTED;
601 case FSW_IO_ERROR:
602 return Volume->LastIOStatus;
603 case FSW_UNSUPPORTED:
604 return EFI_UNSUPPORTED;
605 case FSW_NOT_FOUND:
606 return EFI_NOT_FOUND;
607 case FSW_VOLUME_CORRUPTED:
608 return EFI_VOLUME_CORRUPTED;
609 default:
610 return EFI_DEVICE_ERROR;
611 }
612 }
613
614 /**
615 * File System EFI protocol, OpenVolume function. Creates a file handle for
616 * the root directory and returns it. Note that this function may be called
617 * multiple times and returns a new file handle each time. Each returned
618 * handle is closed by the client using it.
619 */
620
621 EFI_STATUS EFIAPI fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE *This,
622 OUT EFI_FILE **Root)
623 {
624 EFI_STATUS Status;
625 FSW_VOLUME_DATA *Volume = FSW_VOLUME_FROM_FILE_SYSTEM(This);
626
627 #if DEBUG_LEVEL
628 Print(L"fsw_efi_FileSystem_OpenVolume\n");
629 #endif
630
631 fsw_efi_clear_cache();
632 Status = fsw_efi_dnode_to_FileHandle(Volume->vol->root, Root);
633
634 return Status;
635 }
636
637 /**
638 * File Handle EFI protocol, Open function. Dispatches the call
639 * based on the kind of file handle.
640 */
641
642 EFI_STATUS EFIAPI fsw_efi_FileHandle_Open(IN EFI_FILE *This,
643 OUT EFI_FILE **NewHandle,
644 IN CHAR16 *FileName,
645 IN UINT64 OpenMode,
646 IN UINT64 Attributes)
647 {
648 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
649
650 if (File->Type == FSW_EFI_FILE_TYPE_DIR)
651 return fsw_efi_dir_open(File, NewHandle, FileName, OpenMode, Attributes);
652 // not supported for regular files
653 return EFI_UNSUPPORTED;
654 }
655
656 /**
657 * File Handle EFI protocol, Close function. Closes the FSW shandle
658 * and frees the memory used for the structure.
659 */
660
661 EFI_STATUS EFIAPI fsw_efi_FileHandle_Close(IN EFI_FILE *This)
662 {
663 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
664
665 #if DEBUG_LEVEL
666 Print(L"fsw_efi_FileHandle_Close\n");
667 #endif
668
669 fsw_shandle_close(&File->shand);
670 FreePool(File);
671
672 return EFI_SUCCESS;
673 }
674
675 /**
676 * File Handle EFI protocol, Delete function. Calls through to Close
677 * and returns a warning because this driver is read-only.
678 */
679
680 EFI_STATUS EFIAPI fsw_efi_FileHandle_Delete(IN EFI_FILE *This)
681 {
682 EFI_STATUS Status;
683
684 Status = refit_call1_wrapper(This->Close, This);
685 if (Status == EFI_SUCCESS) {
686 // this driver is read-only
687 Status = EFI_WARN_DELETE_FAILURE;
688 }
689
690 return Status;
691 }
692
693 /**
694 * File Handle EFI protocol, Read function. Dispatches the call
695 * based on the kind of file handle.
696 */
697
698 EFI_STATUS EFIAPI fsw_efi_FileHandle_Read(IN EFI_FILE *This,
699 IN OUT UINTN *BufferSize,
700 OUT VOID *Buffer)
701 {
702 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
703
704 if (File->Type == FSW_EFI_FILE_TYPE_FILE)
705 return fsw_efi_file_read(File, BufferSize, Buffer);
706 else if (File->Type == FSW_EFI_FILE_TYPE_DIR)
707 return fsw_efi_dir_read(File, BufferSize, Buffer);
708 return EFI_UNSUPPORTED;
709 }
710
711 /**
712 * File Handle EFI protocol, Write function. Returns unsupported status
713 * because this driver is read-only.
714 */
715
716 EFI_STATUS EFIAPI fsw_efi_FileHandle_Write(IN EFI_FILE *This,
717 IN OUT UINTN *BufferSize,
718 IN VOID *Buffer)
719 {
720 // this driver is read-only
721 return EFI_WRITE_PROTECTED;
722 }
723
724 /**
725 * File Handle EFI protocol, GetPosition function. Dispatches the call
726 * based on the kind of file handle.
727 */
728
729 EFI_STATUS EFIAPI fsw_efi_FileHandle_GetPosition(IN EFI_FILE *This,
730 OUT UINT64 *Position)
731 {
732 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
733
734 if (File->Type == FSW_EFI_FILE_TYPE_FILE)
735 return fsw_efi_file_getpos(File, Position);
736 // not defined for directories
737 return EFI_UNSUPPORTED;
738 }
739
740 /**
741 * File Handle EFI protocol, SetPosition function. Dispatches the call
742 * based on the kind of file handle.
743 */
744
745 EFI_STATUS EFIAPI fsw_efi_FileHandle_SetPosition(IN EFI_FILE *This,
746 IN UINT64 Position)
747 {
748 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
749
750 if (File->Type == FSW_EFI_FILE_TYPE_FILE)
751 return fsw_efi_file_setpos(File, Position);
752 else if (File->Type == FSW_EFI_FILE_TYPE_DIR)
753 return fsw_efi_dir_setpos(File, Position);
754 return EFI_UNSUPPORTED;
755 }
756
757 /**
758 * File Handle EFI protocol, GetInfo function. Dispatches to the common
759 * function implementing this.
760 */
761
762 EFI_STATUS EFIAPI fsw_efi_FileHandle_GetInfo(IN EFI_FILE *This,
763 IN EFI_GUID *InformationType,
764 IN OUT UINTN *BufferSize,
765 OUT VOID *Buffer)
766 {
767 FSW_FILE_DATA *File = FSW_FILE_FROM_FILE_HANDLE(This);
768
769 return fsw_efi_dnode_getinfo(File, InformationType, BufferSize, Buffer);
770 }
771
772 /**
773 * File Handle EFI protocol, SetInfo function. Returns unsupported status
774 * because this driver is read-only.
775 */
776
777 EFI_STATUS EFIAPI fsw_efi_FileHandle_SetInfo(IN EFI_FILE *This,
778 IN EFI_GUID *InformationType,
779 IN UINTN BufferSize,
780 IN VOID *Buffer)
781 {
782 // this driver is read-only
783 return EFI_WRITE_PROTECTED;
784 }
785
786 /**
787 * File Handle EFI protocol, Flush function. Returns unsupported status
788 * because this driver is read-only.
789 */
790
791 EFI_STATUS EFIAPI fsw_efi_FileHandle_Flush(IN EFI_FILE *This)
792 {
793 // this driver is read-only
794 return EFI_WRITE_PROTECTED;
795 }
796
797 /**
798 * Set up a file handle for a dnode. This function allocates a data structure
799 * for a file handle, opens a FSW shandle and populates the EFI_FILE structure
800 * with the interface functions.
801 */
802
803 EFI_STATUS fsw_efi_dnode_to_FileHandle(IN struct fsw_dnode *dno,
804 OUT EFI_FILE **NewFileHandle)
805 {
806 EFI_STATUS Status;
807 FSW_FILE_DATA *File;
808
809 // make sure the dnode has complete info
810 Status = fsw_efi_map_status(fsw_dnode_fill(dno), (FSW_VOLUME_DATA *)dno->vol->host_data);
811 if (EFI_ERROR(Status))
812 return Status;
813
814 // check type
815 if (dno->type != FSW_DNODE_TYPE_FILE && dno->type != FSW_DNODE_TYPE_DIR)
816 return EFI_UNSUPPORTED;
817
818 // allocate file structure
819 File = AllocateZeroPool(sizeof(FSW_FILE_DATA));
820 File->Signature = FSW_FILE_DATA_SIGNATURE;
821 if (dno->type == FSW_DNODE_TYPE_FILE)
822 File->Type = FSW_EFI_FILE_TYPE_FILE;
823 else if (dno->type == FSW_DNODE_TYPE_DIR)
824 File->Type = FSW_EFI_FILE_TYPE_DIR;
825
826 // open shandle
827 Status = fsw_efi_map_status(fsw_shandle_open(dno, &File->shand),
828 (FSW_VOLUME_DATA *)dno->vol->host_data);
829 if (EFI_ERROR(Status)) {
830 FreePool(File);
831 return Status;
832 }
833
834 // populate the file handle
835 File->FileHandle.Revision = EFI_FILE_HANDLE_REVISION;
836 File->FileHandle.Open = fsw_efi_FileHandle_Open;
837 File->FileHandle.Close = fsw_efi_FileHandle_Close;
838 File->FileHandle.Delete = fsw_efi_FileHandle_Delete;
839 File->FileHandle.Read = fsw_efi_FileHandle_Read;
840 File->FileHandle.Write = fsw_efi_FileHandle_Write;
841 File->FileHandle.GetPosition = fsw_efi_FileHandle_GetPosition;
842 File->FileHandle.SetPosition = fsw_efi_FileHandle_SetPosition;
843 File->FileHandle.GetInfo = fsw_efi_FileHandle_GetInfo;
844 File->FileHandle.SetInfo = fsw_efi_FileHandle_SetInfo;
845 File->FileHandle.Flush = fsw_efi_FileHandle_Flush;
846
847 *NewFileHandle = &File->FileHandle;
848 return EFI_SUCCESS;
849 }
850
851 /**
852 * Data read function for regular files. Calls through to fsw_shandle_read.
853 */
854
855 EFI_STATUS fsw_efi_file_read(IN FSW_FILE_DATA *File,
856 IN OUT UINTN *BufferSize,
857 OUT VOID *Buffer)
858 {
859 EFI_STATUS Status;
860 fsw_u32 buffer_size;
861
862 #if DEBUG_LEVEL
863 Print(L"fsw_efi_file_read %d bytes\n", *BufferSize);
864 #endif
865
866 buffer_size = (fsw_u32)*BufferSize;
867 Status = fsw_efi_map_status(fsw_shandle_read(&File->shand, &buffer_size, Buffer),
868 (FSW_VOLUME_DATA *)File->shand.dnode->vol->host_data);
869 *BufferSize = buffer_size;
870
871 return Status;
872 }
873
874 /**
875 * Get file position for regular files.
876 */
877
878 EFI_STATUS fsw_efi_file_getpos(IN FSW_FILE_DATA *File,
879 OUT UINT64 *Position)
880 {
881 *Position = File->shand.pos;
882 return EFI_SUCCESS;
883 }
884
885 /**
886 * Set file position for regular files. EFI specifies the all-ones value
887 * to be a special value for the end of the file.
888 */
889
890 EFI_STATUS fsw_efi_file_setpos(IN FSW_FILE_DATA *File, IN UINT64 Position)
891 {
892 if (Position == 0xFFFFFFFFFFFFFFFFULL)
893 File->shand.pos = File->shand.dnode->size;
894 else
895 File->shand.pos = Position;
896 return EFI_SUCCESS;
897 }
898
899 /**
900 * Open function used to open new file handles relative to a directory.
901 * In EFI, the "open file" function is implemented by directory file handles
902 * and is passed a relative or volume-absolute path to the file or directory
903 * to open. We use fsw_dnode_lookup_path to find the node plus an additional
904 * call to fsw_dnode_resolve because EFI has no concept of symbolic links.
905 */
906
907 EFI_STATUS fsw_efi_dir_open(IN FSW_FILE_DATA *File,
908 OUT EFI_FILE **NewHandle,
909 IN CHAR16 *FileName,
910 IN UINT64 OpenMode,
911 IN UINT64 Attributes)
912 {
913 EFI_STATUS Status;
914 FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)File->shand.dnode->vol->host_data;
915 struct fsw_dnode *dno;
916 struct fsw_dnode *target_dno;
917 struct fsw_string lookup_path;
918
919 #if DEBUG_LEVEL
920 Print(L"fsw_efi_dir_open: '%s'\n", FileName);
921 #endif
922
923 if (OpenMode != EFI_FILE_MODE_READ)
924 return EFI_WRITE_PROTECTED;
925
926 lookup_path.type = FSW_STRING_TYPE_UTF16;
927 lookup_path.len = (int)StrLen(FileName);
928 lookup_path.size = lookup_path.len * sizeof(fsw_u16);
929 lookup_path.data = FileName;
930
931 // resolve the path (symlinks along the way are automatically resolved)
932 Status = fsw_efi_map_status(fsw_dnode_lookup_path(File->shand.dnode, &lookup_path, '\\', &dno), Volume);
933 if (EFI_ERROR(Status))
934 return Status;
935
936 // if the final node is a symlink, also resolve it
937 Status = fsw_efi_map_status(fsw_dnode_resolve(dno, &target_dno), Volume);
938 fsw_dnode_release(dno);
939 if (EFI_ERROR(Status))
940 return Status;
941 dno = target_dno;
942
943 // make a new EFI handle for the target dnode
944 Status = fsw_efi_dnode_to_FileHandle(dno, NewHandle);
945 fsw_dnode_release(dno);
946 return Status;
947 }
948
949 /**
950 * Read function for directories. A file handle read on a directory retrieves
951 * the next directory entry.
952 */
953
954 EFI_STATUS fsw_efi_dir_read(IN FSW_FILE_DATA *File,
955 IN OUT UINTN *BufferSize,
956 OUT VOID *Buffer)
957 {
958 EFI_STATUS Status;
959 FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)File->shand.dnode->vol->host_data;
960 struct fsw_dnode *dno;
961
962 #if DEBUG_LEVEL
963 Print(L"fsw_efi_dir_read...\n");
964 #endif
965
966 // read the next entry
967 Status = fsw_efi_map_status(fsw_dnode_dir_read(&File->shand, &dno), Volume);
968 if (Status == EFI_NOT_FOUND) {
969 // end of directory
970 *BufferSize = 0;
971 #if DEBUG_LEVEL
972 Print(L"...no more entries\n");
973 #endif
974 return EFI_SUCCESS;
975 }
976 if (EFI_ERROR(Status))
977 return Status;
978
979 // get info into buffer
980 Status = fsw_efi_dnode_fill_FileInfo(Volume, dno, BufferSize, Buffer);
981 fsw_dnode_release(dno);
982 return Status;
983 }
984
985 /**
986 * Set file position for directories. The only allowed set position operation
987 * for directories is to rewind the directory completely by setting the
988 * position to zero.
989 */
990
991 EFI_STATUS fsw_efi_dir_setpos(IN FSW_FILE_DATA *File, IN UINT64 Position)
992 {
993 if (Position == 0) {
994 File->shand.pos = 0;
995 return EFI_SUCCESS;
996 } else {
997 // directories can only rewind to the start
998 return EFI_UNSUPPORTED;
999 }
1000 }
1001
1002 /**
1003 * Get file or volume information. This function implements the GetInfo call
1004 * for all file handles. Control is dispatched according to the type of information
1005 * requested by the caller.
1006 */
1007
1008 EFI_STATUS fsw_efi_dnode_getinfo(IN FSW_FILE_DATA *File,
1009 IN EFI_GUID *InformationType,
1010 IN OUT UINTN *BufferSize,
1011 OUT VOID *Buffer)
1012 {
1013 EFI_STATUS Status;
1014 FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)File->shand.dnode->vol->host_data;
1015 EFI_FILE_SYSTEM_INFO *FSInfo;
1016 UINTN RequiredSize;
1017 struct fsw_volume_stat vsb;
1018
1019
1020 if (CompareGuid(InformationType, &gEfiFileInfoGuid)) {
1021 #if DEBUG_LEVEL
1022 Print(L"fsw_efi_dnode_getinfo: FILE_INFO\n");
1023 #endif
1024
1025 Status = fsw_efi_dnode_fill_FileInfo(Volume, File->shand.dnode, BufferSize, Buffer);
1026
1027 } else if (CompareGuid(InformationType, &gEfiFileSystemInfoGuid)) {
1028 #if DEBUG_LEVEL
1029 Print(L"fsw_efi_dnode_getinfo: FILE_SYSTEM_INFO\n");
1030 #endif
1031
1032 // check buffer size
1033 RequiredSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + fsw_efi_strsize(&Volume->vol->label);
1034 if (*BufferSize < RequiredSize) {
1035 *BufferSize = RequiredSize;
1036 return EFI_BUFFER_TOO_SMALL;
1037 }
1038
1039 // fill structure
1040 FSInfo = (EFI_FILE_SYSTEM_INFO *)Buffer;
1041 FSInfo->Size = RequiredSize;
1042 FSInfo->ReadOnly = TRUE;
1043 FSInfo->BlockSize = Volume->vol->log_blocksize;
1044 fsw_efi_strcpy(FSInfo->VolumeLabel, &Volume->vol->label);
1045
1046 // get the missing info from the fs driver
1047 ZeroMem(&vsb, sizeof(struct fsw_volume_stat));
1048 Status = fsw_efi_map_status(fsw_volume_stat(Volume->vol, &vsb), Volume);
1049 if (EFI_ERROR(Status))
1050 return Status;
1051 FSInfo->VolumeSize = vsb.total_bytes;
1052 FSInfo->FreeSpace = vsb.free_bytes;
1053
1054 // prepare for return
1055 *BufferSize = RequiredSize;
1056 Status = EFI_SUCCESS;
1057
1058 } else if (CompareGuid(InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
1059 #if DEBUG_LEVEL
1060 Print(L"fsw_efi_dnode_getinfo: FILE_SYSTEM_VOLUME_LABEL\n");
1061 #endif
1062
1063 // check buffer size
1064 RequiredSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO + fsw_efi_strsize(&Volume->vol->label);
1065 if (*BufferSize < RequiredSize) {
1066 *BufferSize = RequiredSize;
1067 return EFI_BUFFER_TOO_SMALL;
1068 }
1069
1070 // copy volume label
1071 fsw_efi_strcpy(((EFI_FILE_SYSTEM_VOLUME_LABEL_INFO *)Buffer)->VolumeLabel, &Volume->vol->label);
1072
1073 // prepare for return
1074 *BufferSize = RequiredSize;
1075 Status = EFI_SUCCESS;
1076
1077 } else {
1078 Status = EFI_UNSUPPORTED;
1079 }
1080
1081 return Status;
1082 }
1083
1084 /**
1085 * Time mapping callback for the fsw_dnode_stat call. This function converts
1086 * a Posix style timestamp into an EFI_TIME structure and writes it to the
1087 * appropriate member of the EFI_FILE_INFO structure that we're filling.
1088 */
1089
1090 void fsw_store_time_posix(struct fsw_dnode_stat *sb, int which, fsw_u32 posix_time)
1091 {
1092 EFI_FILE_INFO *FileInfo = (EFI_FILE_INFO *)sb->host_data;
1093
1094 if (which == FSW_DNODE_STAT_CTIME)
1095 fsw_efi_decode_time(&FileInfo->CreateTime, posix_time);
1096 else if (which == FSW_DNODE_STAT_MTIME)
1097 fsw_efi_decode_time(&FileInfo->ModificationTime, posix_time);
1098 else if (which == FSW_DNODE_STAT_ATIME)
1099 fsw_efi_decode_time(&FileInfo->LastAccessTime, posix_time);
1100 }
1101
1102 /**
1103 * Mode mapping callback for the fsw_dnode_stat call. This function looks at
1104 * the Posix mode passed by the file system driver and makes appropriate
1105 * adjustments to the EFI_FILE_INFO structure that we're filling.
1106 */
1107
1108 void fsw_store_attr_posix(struct fsw_dnode_stat *sb, fsw_u16 posix_mode)
1109 {
1110 EFI_FILE_INFO *FileInfo = (EFI_FILE_INFO *)sb->host_data;
1111
1112 if ((posix_mode & S_IWUSR) == 0)
1113 FileInfo->Attribute |= EFI_FILE_READ_ONLY;
1114 }
1115
1116 void fsw_store_attr_efi(struct fsw_dnode_stat *sb, fsw_u16 attr)
1117 {
1118 EFI_FILE_INFO *FileInfo = (EFI_FILE_INFO *)sb->host_data;
1119
1120 FileInfo->Attribute |= attr;
1121 }
1122
1123 /**
1124 * Common function to fill an EFI_FILE_INFO with information about a dnode.
1125 */
1126
1127 EFI_STATUS fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA *Volume,
1128 IN struct fsw_dnode *dno,
1129 IN OUT UINTN *BufferSize,
1130 OUT VOID *Buffer)
1131 {
1132 EFI_STATUS Status;
1133 EFI_FILE_INFO *FileInfo;
1134 UINTN RequiredSize;
1135 struct fsw_dnode_stat sb;
1136
1137 // make sure the dnode has complete info
1138 Status = fsw_efi_map_status(fsw_dnode_fill(dno), Volume);
1139 if (EFI_ERROR(Status))
1140 return Status;
1141
1142 // TODO: check/assert that the dno's name is in UTF16
1143
1144 // check buffer size
1145 RequiredSize = SIZE_OF_EFI_FILE_INFO + fsw_efi_strsize(&dno->name);
1146 if (*BufferSize < RequiredSize) {
1147 // TODO: wind back the directory in this case
1148
1149 #if DEBUG_LEVEL
1150 Print(L"...BUFFER TOO SMALL\n");
1151 #endif
1152 *BufferSize = RequiredSize;
1153 return EFI_BUFFER_TOO_SMALL;
1154 }
1155
1156 // fill structure
1157 ZeroMem(Buffer, RequiredSize);
1158 FileInfo = (EFI_FILE_INFO *)Buffer;
1159 FileInfo->Size = RequiredSize;
1160 FileInfo->FileSize = dno->size;
1161 FileInfo->Attribute = 0;
1162 if (dno->type == FSW_DNODE_TYPE_DIR)
1163 FileInfo->Attribute |= EFI_FILE_DIRECTORY;
1164 fsw_efi_strcpy(FileInfo->FileName, &dno->name);
1165
1166 // get the missing info from the fs driver
1167 ZeroMem(&sb, sizeof(struct fsw_dnode_stat));
1168 sb.host_data = FileInfo;
1169 Status = fsw_efi_map_status(fsw_dnode_stat(dno, &sb), Volume);
1170 if (EFI_ERROR(Status))
1171 return Status;
1172 FileInfo->PhysicalSize = sb.used_bytes;
1173
1174 // prepare for return
1175 *BufferSize = RequiredSize;
1176 #if DEBUG_LEVEL
1177 Print(L"...returning '%s'\n", FileInfo->FileName);
1178 #endif
1179 return EFI_SUCCESS;
1180 }
1181
1182 // EOF