]> code.delx.au - refind/blobdiff - filesystems/fsw_efi.c
Changed names in ComponentName.h, included from TianoCore, and
[refind] / filesystems / fsw_efi.c
index 2f5a4c69c68eab3ae25bb4bdc7c2e918aff6f9d0..5ef49be662b1e8f1026d3d194af1d1cde40f1365 100644 (file)
@@ -1,23 +1,9 @@
-/* $Id: fsw_efi.c 29125 2010-05-06 09:43:05Z vboxsync $ */
-/** @file
- * fsw_efi.c - EFI host environment code.
- */
-
-/*
- * Copyright (C) 2010 Oracle Corporation
- *
- * This file is part of VirtualBox Open Source Edition (OSE), as
- * available from http://www.virtualbox.org. This file is free software;
- * you can redistribute it and/or modify it under the terms of the GNU
- * General Public License (GPL) as published by the Free Software
- * Foundation, in version 2 as it comes in the "COPYING" file of the
- * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
- * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+/**
+ * \file fsw_efi.c
+ * EFI host environment code.
  */
 
 /*-
- * This code is based on:
- *
  * Copyright (c) 2006 Christoph Pfisterer
  *
  * Redistribution and use in source and binary forms, with or without
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+/*
+ * Changes by Roderick Smith are licensed under the preceding terms.
+ */
 
 #include "fsw_efi.h"
+#include "fsw_core.h"
+#ifdef __MAKEWITH_GNUEFI
+#include "edk2/DriverBinding.h"
+#include "edk2/ComponentName.h"
+#else
+#define REFIND_EFI_COMPONENT_NAME_PROTOCOL EFI_COMPONENT_NAME_PROTOCOL
+#endif
+#include "../include/refit_call_wrapper.h"
 
 #define DEBUG_LEVEL 0
 
 #ifndef FSTYPE
-#ifdef VBOX
-#error FSTYPE must be defined!
-#else
+/** The file system type name to use. */
 #define FSTYPE ext2
 #endif
-#endif
-
-#define DEBUG_VBFS 1
-// CHAR8 *msgCursor;
-// MESSAGE_LOG_PROTOCOL *Msg = NULL; 
 
-#if DEBUG_VBFS==2
-#define DBG(x...)  AsciiPrint(x)
-#elif DEBUG_VBFS==1
-#define DBG(x...)  BootLog(x)
-#else
-#define DBG(x...)
+#ifdef __MAKEWITH_GNUEFI
+
+#define EFI_DISK_IO_PROTOCOL_GUID \
+  { \
+    0xce345171, 0xba0b, 0x11d2, {0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+#define EFI_BLOCK_IO_PROTOCOL_GUID \
+  { \
+    0x964e5b21, 0x6459, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+EFI_GUID gEfiDriverBindingProtocolGuid = EFI_DRIVER_BINDING_PROTOCOL_GUID;
+EFI_GUID gEfiComponentNameProtocolGuid = REFIND_EFI_COMPONENT_NAME_PROTOCOL_GUID;
+EFI_GUID gEfiDiskIoProtocolGuid = EFI_DISK_IO_PROTOCOL_GUID;
+EFI_GUID gEfiBlockIoProtocolGuid = EFI_BLOCK_IO_PROTOCOL_GUID;
+EFI_GUID gEfiFileInfoGuid = EFI_FILE_INFO_ID;
+EFI_GUID gEfiFileSystemInfoGuid = EFI_FILE_SYSTEM_INFO_ID;
+EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid = EFI_FILE_SYSTEM_VOLUME_LABEL_INFO_ID;
+#define gEfiSimpleFileSystemProtocolGuid FileSystemProtocol
 #endif
 
-
 /** Helper macro for stringification. */
 #define FSW_EFI_STRINGIFY(x) #x
 /** Expands to the EFI driver name given the file system type name. */
-#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.4.0 " FSW_EFI_STRINGIFY(t) L" File System Driver"
+#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.10.3 " FSW_EFI_STRINGIFY(t) L" File System Driver"
 
 // function prototypes
 
@@ -92,19 +95,19 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
                                              IN  UINTN                        NumberOfChildren,
                                              IN  EFI_HANDLE                   *ChildHandleBuffer);
 
-EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN  REFIND_EFI_COMPONENT_NAME_PROTOCOL  *This,
                                                       IN  CHAR8                        *Language,
                                                       OUT CHAR16                       **DriverName);
-EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  EFI_COMPONENT_NAME_PROTOCOL    *This,
+EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  REFIND_EFI_COMPONENT_NAME_PROTOCOL    *This,
                                                           IN  EFI_HANDLE                     ControllerHandle,
                                                           IN  EFI_HANDLE                     ChildHandle  OPTIONAL,
                                                           IN  CHAR8                          *Language,
                                                           OUT CHAR16                         **ControllerName);
 
-void fsw_efi_change_blocksize(struct fsw_volume *vol,
+void EFIAPI fsw_efi_change_blocksize(struct fsw_volume *vol,
                               fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
                               fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize);
-fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u32 phys_bno, void *buffer);
+fsw_status_t EFIAPI fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer);
 
 EFI_STATUS fsw_efi_map_status(fsw_status_t fsw_status, FSW_VOLUME_DATA *Volume);
 
@@ -141,6 +144,22 @@ EFI_STATUS fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA *Volume,
                                        IN OUT UINTN *BufferSize,
                                        OUT VOID *Buffer);
 
+/**
+ * Structure for holding disk cache data.
+ */
+
+#define CACHE_SIZE 131072 /* 128KiB */
+struct cache_data {
+   fsw_u8            *Cache;
+   fsw_u64           CacheStart;
+   BOOLEAN           CacheValid;
+   FSW_VOLUME_DATA   *Volume; // NOTE: Do not deallocate; copied here to ID volume
+};
+
+#define NUM_CACHES 2 /* Don't increase without modifying fsw_efi_read_block() */
+static struct cache_data    Caches[NUM_CACHES];
+static int LastRead = -1;
+
 /**
  * Interface structure for the EFI Driver Binding protocol.
  */
@@ -158,10 +177,10 @@ EFI_DRIVER_BINDING_PROTOCOL fsw_efi_DriverBinding_table = {
  * Interface structure for the EFI Component Name protocol.
  */
 
-EFI_COMPONENT_NAME_PROTOCOL fsw_efi_ComponentName_table = {
+REFIND_EFI_COMPONENT_NAME_PROTOCOL fsw_efi_ComponentName_table = {
     fsw_efi_ComponentName_GetDriverName,
     fsw_efi_ComponentName_GetControllerName,
-    "eng"
+    (CHAR8*) "eng"
 };
 
 /**
@@ -177,7 +196,22 @@ struct fsw_host_table   fsw_efi_host_table = {
 
 extern struct fsw_fstype_table   FSW_FSTYPE_TABLE_NAME(FSTYPE);
 
-//#include "OverrideFunctions-kabyl.edk2.c.include"
+
+static VOID EFIAPI fsw_efi_clear_cache(VOID) {
+   int i;
+
+   // clear the cache
+   for (i = 0; i < NUM_CACHES; i++) {
+      if (Caches[i].Cache != NULL) {
+         FreePool(Caches[i].Cache);
+         Caches[i].Cache = NULL;
+      } // if
+            Caches[i].CacheStart = 0;
+            Caches[i].CacheValid = FALSE;
+            Caches[i].Volume = NULL;
+   }
+   LastRead = -1;
+} // VOID EFIAPI fsw_efi_clear_cache();
 
 /**
  * Image entry point. Installs the Driver Binding and Component Name protocols
@@ -189,7 +223,8 @@ EFI_STATUS EFIAPI fsw_efi_main(IN EFI_HANDLE         ImageHandle,
 {
     EFI_STATUS  Status;
 
-#ifndef VBOX
+#ifndef __MAKEWITH_TIANO
+    // Not available in EDK2 toolkit
     InitializeLib(ImageHandle, SystemTable);
 #endif
 
@@ -197,8 +232,8 @@ EFI_STATUS EFIAPI fsw_efi_main(IN EFI_HANDLE         ImageHandle,
     fsw_efi_DriverBinding_table.ImageHandle          = ImageHandle;
     fsw_efi_DriverBinding_table.DriverBindingHandle  = ImageHandle;
     // install Driver Binding protocol
-    Status = BS->InstallProtocolInterface(&fsw_efi_DriverBinding_table.DriverBindingHandle,
-                                          &PROTO_NAME(DriverBindingProtocol),
+    Status = refit_call4_wrapper(BS->InstallProtocolInterface, &fsw_efi_DriverBinding_table.DriverBindingHandle,
+                                          &gEfiDriverBindingProtocolGuid,
                                           EFI_NATIVE_INTERFACE,
                                           &fsw_efi_DriverBinding_table);
     if (EFI_ERROR (Status)) {
@@ -206,8 +241,8 @@ EFI_STATUS EFIAPI fsw_efi_main(IN EFI_HANDLE         ImageHandle,
     }
 
     // install Component Name protocol
-    Status = BS->InstallProtocolInterface(&fsw_efi_DriverBinding_table.DriverBindingHandle,
-                                          &PROTO_NAME(ComponentNameProtocol),
+    Status = refit_call4_wrapper(BS->InstallProtocolInterface, &fsw_efi_DriverBinding_table.DriverBindingHandle,
+                                          &gEfiComponentNameProtocolGuid,
                                           EFI_NATIVE_INTERFACE,
                                           &fsw_efi_ComponentName_table);
     if (EFI_ERROR (Status)) {
@@ -226,6 +261,10 @@ EFI_STATUS EFIAPI fsw_efi_main(IN EFI_HANDLE         ImageHandle,
     return EFI_SUCCESS;
 }
 
+#ifdef __MAKEWITH_GNUEFI
+EFI_DRIVER_ENTRY_POINT(fsw_efi_main)
+#endif
+
 /**
  * Driver Binding EFI protocol, Supported function. This function is called by EFI
  * to test if this driver can handle a certain device. Our implementation only checks
@@ -243,8 +282,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
     // we check for both DiskIO and BlockIO protocols
 
     // first, open DiskIO
-    Status = BS->OpenProtocol(ControllerHandle,
-                              &PROTO_NAME(DiskIoProtocol),
+    Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
+                              &gEfiDiskIoProtocolGuid,
                               (VOID **) &DiskIo,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -253,14 +292,14 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
         return Status;
 
     // we were just checking, close it again
-    BS->CloseProtocol(ControllerHandle,
-                      &PROTO_NAME(DiskIoProtocol),
+    refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
+                      &gEfiDiskIoProtocolGuid,
                       This->DriverBindingHandle,
                       ControllerHandle);
 
     // next, check BlockIO without actually opening it
-    Status = BS->OpenProtocol(ControllerHandle,
-                              &PROTO_NAME(BlockIoProtocol),
+    Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
+                              &gEfiBlockIoProtocolGuid,
                               NULL,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -295,8 +334,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
 #endif
 
     // open consumed protocols
-    Status = BS->OpenProtocol(ControllerHandle,
-                              &PROTO_NAME(BlockIoProtocol),
+    Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
+                              &gEfiBlockIoProtocolGuid,
                               (VOID **) &BlockIo,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -306,14 +345,14 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
         return Status;
     }
 
-    Status = BS->OpenProtocol(ControllerHandle,
-                              &PROTO_NAME(DiskIoProtocol),
+    Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
+                              &gEfiDiskIoProtocolGuid,
                               (VOID **) &DiskIo,
                               This->DriverBindingHandle,
                               ControllerHandle,
                               EFI_OPEN_PROTOCOL_BY_DRIVER);
     if (EFI_ERROR(Status)) {
-        Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %r\n", Status);
+        Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %x\n", Status);
         return Status;
     }
 
@@ -334,8 +373,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
         // register the SimpleFileSystem protocol
         Volume->FileSystem.Revision     = EFI_FILE_IO_INTERFACE_REVISION;
         Volume->FileSystem.OpenVolume   = fsw_efi_FileSystem_OpenVolume;
-        Status = BS->InstallMultipleProtocolInterfaces(&ControllerHandle,
-                                                       &PROTO_NAME(SimpleFileSystemProtocol),
+        Status = refit_call4_wrapper(BS->InstallMultipleProtocolInterfaces, &ControllerHandle,
+                                                       &gEfiSimpleFileSystemProtocolGuid,
                                                        &Volume->FileSystem,
                                                        NULL);
         if (EFI_ERROR(Status)) {
@@ -349,12 +388,11 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
             fsw_unmount(Volume->vol);
         FreePool(Volume);
 
-        BS->CloseProtocol(ControllerHandle,
-                          &PROTO_NAME(DiskIoProtocol),
+        refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
+                          &gEfiDiskIoProtocolGuid,
                           This->DriverBindingHandle,
                           ControllerHandle);
     }
-
     return Status;
 }
 
@@ -382,8 +420,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
 #endif
 
     // get the installed SimpleFileSystem interface
-    Status = BS->OpenProtocol(ControllerHandle,
-                              &PROTO_NAME(SimpleFileSystemProtocol),
+    Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
+                              &gEfiSimpleFileSystemProtocolGuid,
                               (VOID **) &FileSystem,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -395,8 +433,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
     Volume = FSW_VOLUME_FROM_FILE_SYSTEM(FileSystem);
 
     // uninstall Simple File System protocol
-    Status = BS->UninstallMultipleProtocolInterfaces(ControllerHandle,
-                                                     &PROTO_NAME(SimpleFileSystemProtocol), &Volume->FileSystem,
+    Status = refit_call4_wrapper(BS->UninstallMultipleProtocolInterfaces, ControllerHandle,
+                                                     &gEfiSimpleFileSystemProtocolGuid, &Volume->FileSystem,
                                                      NULL);
     if (EFI_ERROR(Status)) {
  //       Print(L"Fsw ERROR: UninstallMultipleProtocolInterfaces returned %x\n", Status);
@@ -412,11 +450,14 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
     FreePool(Volume);
 
     // close the consumed protocols
-    Status = BS->CloseProtocol(ControllerHandle,
-                               &PROTO_NAME(DiskIoProtocol),
+    Status = refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
+                               &gEfiDiskIoProtocolGuid,
                                This->DriverBindingHandle,
                                ControllerHandle);
 
+    // clear the cache
+    fsw_efi_clear_cache();
+
     return Status;
 }
 
@@ -426,7 +467,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
  * based on the file system type actually used in compilation.
  */
 
-EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN  REFIND_EFI_COMPONENT_NAME_PROTOCOL  *This,
                                                       IN  CHAR8                        *Language,
                                                       OUT CHAR16                       **DriverName)
 {
@@ -445,7 +486,7 @@ EFI_STATUS EFIAPI fsw_efi_ComponentName_GetDriverName(IN  EFI_COMPONENT_NAME_PRO
  * because this is not a "bus" driver in the sense of the EFI Driver Model.
  */
 
-EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  EFI_COMPONENT_NAME_PROTOCOL    *This,
+EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  REFIND_EFI_COMPONENT_NAME_PROTOCOL    *This,
                                                           IN  EFI_HANDLE                     ControllerHandle,
                                                           IN  EFI_HANDLE                     ChildHandle  OPTIONAL,
                                                           IN  CHAR8                          *Language,
@@ -459,7 +500,7 @@ EFI_STATUS EFIAPI fsw_efi_ComponentName_GetControllerName(IN  EFI_COMPONENT_NAME
  * when the file system driver changes the block sizes for the volume.
  */
 
-void fsw_efi_change_blocksize(struct fsw_volume *vol,
+void EFIAPI fsw_efi_change_blocksize(struct fsw_volume *vol,
                               fsw_u32 old_phys_blocksize, fsw_u32 old_log_blocksize,
                               fsw_u32 new_phys_blocksize, fsw_u32 new_log_blocksize)
 {
@@ -469,25 +510,91 @@ void fsw_efi_change_blocksize(struct fsw_volume *vol,
 /**
  * FSW interface function to read data blocks. This function is called by the FSW core
  * to read a block of data from the device. The buffer is allocated by the core code.
+ * Two caches are maintained, so as to improve performance on some systems. (VirtualBox
+ * is particularly susceptible to performance problems with an uncached driver -- the
+ * ext2 driver can take 200 seconds to load a Linux kernel under VirtualBox, whereas
+ * the time is more like 3 seconds with a cache!) Two independent caches are maintained
+ * because the ext2fs driver tends to alternate between accessing two parts of the
+ * disk.
  */
 
-fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u32 phys_bno, void *buffer)
-{
-    EFI_STATUS          Status;
-    FSW_VOLUME_DATA     *Volume = (FSW_VOLUME_DATA *)vol->host_data;
-
-//    FSW_MSG_DEBUGV((FSW_MSGSTR("fsw_efi_read_block: %d  (%d)\n"), phys_bno, vol->phys_blocksize));
-
-    // read from disk
-    Status = Volume->DiskIo->ReadDisk(Volume->DiskIo, Volume->MediaId,
-                                      (UINT64)phys_bno * vol->phys_blocksize,
-                                      vol->phys_blocksize,
-                                      buffer);
-    Volume->LastIOStatus = Status;
-    if (EFI_ERROR(Status))
-        return FSW_IO_ERROR;
-    return FSW_SUCCESS;
-}
+fsw_status_t EFIAPI fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) {
+   int              i, ReadCache = -1;
+   FSW_VOLUME_DATA  *Volume = (FSW_VOLUME_DATA *)vol->host_data;
+   EFI_STATUS       Status = EFI_SUCCESS;
+   BOOLEAN          ReadOneBlock = FALSE;
+   UINT64           StartRead = (UINT64) phys_bno * (UINT64) vol->phys_blocksize;
+
+   if (buffer == NULL)
+      return (fsw_status_t) EFI_BAD_BUFFER_SIZE;
+
+   // Initialize static data structures, if necessary....
+   if (LastRead < 0) {
+      fsw_efi_clear_cache();
+   } // if
+
+   // Look for a cache hit on the current query....
+   i = 0;
+   do {
+      if ((Caches[i].Volume == Volume) &&
+          (Caches[i].CacheValid == TRUE) &&
+          (StartRead >= Caches[i].CacheStart) &&
+          ((StartRead + vol->phys_blocksize) <= (Caches[i].CacheStart + CACHE_SIZE))) {
+         ReadCache = i;
+      }
+      i++;
+   } while ((i < NUM_CACHES) && (ReadCache < 0));
+
+   // No cache hit found; load new cache and pass it on....
+   if (ReadCache < 0) {
+      if (LastRead == -1)
+         LastRead = 1;
+      ReadCache = 1 - LastRead; // NOTE: If NUM_CACHES > 2, this must become more complex
+      Caches[ReadCache].CacheValid = FALSE;
+      if (Caches[ReadCache].Cache == NULL)
+         Caches[ReadCache].Cache = AllocatePool(CACHE_SIZE);
+      if (Caches[ReadCache].Cache != NULL) {
+         // TODO: Below call hangs on my 32-bit Mac Mini when compiled with GNU-EFI.
+         // The same binary is fine under VirtualBox, and the same call is fine when
+         // compiled with Tianocore. Further clue: Omitting "Status =" avoids the
+         // hang but produces a failure to mount the filesystem, even when the same
+         // change is made to later similar call. Calling Volume->DiskIo->ReadDisk()
+         // directly (without refit_call5_wrapper()) changes nothing. Placing Print()
+         // statements at the start and end of the function, and before and after the
+         // ReadDisk() call, suggests that when it fails, the program is executing
+         // code starting mid-function, so there seems to be something messed up in
+         // the way the function is being called. FIGURE THIS OUT!
+         Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+                                      StartRead, (UINTN) CACHE_SIZE, (VOID*) Caches[ReadCache].Cache);
+         if (!EFI_ERROR(Status)) {
+            Caches[ReadCache].CacheStart = StartRead;
+            Caches[ReadCache].CacheValid = TRUE;
+            Caches[ReadCache].Volume = Volume;
+            LastRead = ReadCache;
+         } else {
+            ReadOneBlock = TRUE;
+         }
+      } else {
+         ReadOneBlock = TRUE;
+      } // if cache memory allocated
+   } // if (ReadCache < 0)
+
+   if (Caches[ReadCache].Cache != NULL && Caches[ReadCache].CacheValid == TRUE && vol->phys_blocksize > 0) {
+      CopyMem(buffer, &Caches[ReadCache].Cache[StartRead - Caches[ReadCache].CacheStart], vol->phys_blocksize);
+   } else {
+      ReadOneBlock = TRUE;
+   }
+
+   if (ReadOneBlock) { // Something's failed, so try a simple disk read of one block....
+      Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+                                   phys_bno * vol->phys_blocksize,
+                                   (UINTN) vol->phys_blocksize,
+                                   (VOID*) buffer);
+   }
+   Volume->LastIOStatus = Status;
+
+   return Status;
+} // fsw_status_t *fsw_efi_read_block()
 
 /**
  * Map FSW status codes to EFI status codes. The FSW_IO_ERROR code is only produced
@@ -532,6 +639,7 @@ EFI_STATUS EFIAPI fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE *This,
     Print(L"fsw_efi_FileSystem_OpenVolume\n");
 #endif
 
+    fsw_efi_clear_cache();
     Status = fsw_efi_dnode_to_FileHandle(Volume->vol->root, Root);
 
     return Status;
@@ -584,7 +692,7 @@ EFI_STATUS EFIAPI fsw_efi_FileHandle_Delete(IN EFI_FILE *This)
 {
     EFI_STATUS          Status;
 
-    Status = This->Close(This);
+    Status = refit_call1_wrapper(This->Close, This);
     if (Status == EFI_SUCCESS) {
         // this driver is read-only
         Status = EFI_WARN_DELETE_FAILURE;
@@ -790,8 +898,7 @@ EFI_STATUS fsw_efi_file_getpos(IN FSW_FILE_DATA *File,
  * to be a special value for the end of the file.
  */
 
-EFI_STATUS fsw_efi_file_setpos(IN FSW_FILE_DATA *File,
-                               IN UINT64 Position)
+EFI_STATUS fsw_efi_file_setpos(IN FSW_FILE_DATA *File, IN UINT64 Position)
 {
     if (Position == 0xFFFFFFFFFFFFFFFFULL)
         File->shand.pos = File->shand.dnode->size;
@@ -833,14 +940,12 @@ EFI_STATUS fsw_efi_dir_open(IN FSW_FILE_DATA *File,
     lookup_path.data = FileName;
 
     // resolve the path (symlinks along the way are automatically resolved)
-    Status = fsw_efi_map_status(fsw_dnode_lookup_path(File->shand.dnode, &lookup_path, '\\', &dno),
-                                Volume);
+    Status = fsw_efi_map_status(fsw_dnode_lookup_path(File->shand.dnode, &lookup_path, '\\', &dno), Volume);
     if (EFI_ERROR(Status))
         return Status;
 
     // if the final node is a symlink, also resolve it
-    Status = fsw_efi_map_status(fsw_dnode_resolve(dno, &target_dno),
-                                Volume);
+    Status = fsw_efi_map_status(fsw_dnode_resolve(dno, &target_dno), Volume);
     fsw_dnode_release(dno);
     if (EFI_ERROR(Status))
         return Status;
@@ -870,8 +975,7 @@ EFI_STATUS fsw_efi_dir_read(IN FSW_FILE_DATA *File,
 #endif
 
     // read the next entry
-    Status = fsw_efi_map_status(fsw_dnode_dir_read(&File->shand, &dno),
-                                Volume);
+    Status = fsw_efi_map_status(fsw_dnode_dir_read(&File->shand, &dno), Volume);
     if (Status == EFI_NOT_FOUND) {
         // end of directory
         *BufferSize = 0;
@@ -895,8 +999,7 @@ EFI_STATUS fsw_efi_dir_read(IN FSW_FILE_DATA *File,
  * position to zero.
  */
 
-EFI_STATUS fsw_efi_dir_setpos(IN FSW_FILE_DATA *File,
-                              IN UINT64 Position)
+EFI_STATUS fsw_efi_dir_setpos(IN FSW_FILE_DATA *File, IN UINT64 Position)
 {
     if (Position == 0) {
         File->shand.pos = 0;
@@ -995,7 +1098,7 @@ EFI_STATUS fsw_efi_dnode_getinfo(IN FSW_FILE_DATA *File,
  * appropriate member of the EFI_FILE_INFO structure that we're filling.
  */
 
-static void fsw_efi_store_time_posix(struct fsw_dnode_stat *sb, int which, fsw_u32 posix_time)
+void fsw_store_time_posix(struct fsw_dnode_stat *sb, int which, fsw_u32 posix_time)
 {
     EFI_FILE_INFO       *FileInfo = (EFI_FILE_INFO *)sb->host_data;
 
@@ -1013,7 +1116,7 @@ static void fsw_efi_store_time_posix(struct fsw_dnode_stat *sb, int which, fsw_u
  * adjustments to the EFI_FILE_INFO structure that we're filling.
  */
 
-static void fsw_efi_store_attr_posix(struct fsw_dnode_stat *sb, fsw_u16 posix_mode)
+void fsw_store_attr_posix(struct fsw_dnode_stat *sb, fsw_u16 posix_mode)
 {
     EFI_FILE_INFO       *FileInfo = (EFI_FILE_INFO *)sb->host_data;
 
@@ -1021,6 +1124,13 @@ static void fsw_efi_store_attr_posix(struct fsw_dnode_stat *sb, fsw_u16 posix_mo
         FileInfo->Attribute |= EFI_FILE_READ_ONLY;
 }
 
+void fsw_store_attr_efi(struct fsw_dnode_stat *sb, fsw_u16 attr)
+{
+    EFI_FILE_INFO       *FileInfo = (EFI_FILE_INFO *)sb->host_data;
+
+    FileInfo->Attribute |= attr;
+}
+
 /**
  * Common function to fill an EFI_FILE_INFO with information about a dnode.
  */
@@ -1066,8 +1176,6 @@ EFI_STATUS fsw_efi_dnode_fill_FileInfo(IN FSW_VOLUME_DATA *Volume,
 
     // get the missing info from the fs driver
     ZeroMem(&sb, sizeof(struct fsw_dnode_stat));
-    sb.store_time_posix = fsw_efi_store_time_posix;
-    sb.store_attr_posix = fsw_efi_store_attr_posix;
     sb.host_data = FileInfo;
     Status = fsw_efi_map_status(fsw_dnode_stat(dno, &sb), Volume);
     if (EFI_ERROR(Status))