]> code.delx.au - refind/blobdiff - filesystems/fsw_efi.c
Changed install.sh to better support Yosemite.
[refind] / filesystems / fsw_efi.c
index bed6a804e14221c8aa91d50634e5aa0b8a47aa25..f2cb4fe372900871055fc7594711743695e4cb6a 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
@@ -48,6 +34,9 @@
  * (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"
 #define DEBUG_LEVEL 0
 
 #ifndef FSTYPE
-#error FSTYPE must be defined!
-#endif
-
-#define DEBUG_VBFS 1
-
-#if DEBUG_VBFS==2
-#define DBG(x...)  AsciiPrint(x)
-#elif DEBUG_VBFS==1
-#define DBG(x...)  BootLog(x)
-#else
-#define DBG(x...)
+/** The file system type name to use. */
+#define FSTYPE ext2
 #endif
 
 #ifdef __MAKEWITH_GNUEFI
 
 EFI_GUID gEfiDriverBindingProtocolGuid = EFI_DRIVER_BINDING_PROTOCOL_GUID;
 EFI_GUID gEfiComponentNameProtocolGuid = EFI_COMPONENT_NAME_PROTOCOL_GUID;
-EFI_GUID gEfiDiskIoProtocolGuid = EFI_DISK_IO_PROTOCOL_GUID;
-EFI_GUID gEfiBlockIoProtocolGuid = EFI_BLOCK_IO_PROTOCOL_GUID;
+extern EFI_GUID gEfiDiskIoProtocolGuid = EFI_DISK_IO_PROTOCOL_GUID;
+extern 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 SimpleFileSystemProtocol FileSystemProtocol
+#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.7.0 " FSW_EFI_STRINGIFY(t) L" File System Driver"
+#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.8.4 " FSW_EFI_STRINGIFY(t) L" File System Driver"
 
 // function prototypes
 
@@ -176,6 +156,7 @@ struct cache_data {
 
 #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.
@@ -213,7 +194,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
@@ -285,7 +281,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
 
     // first, open DiskIO
     Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
-                              &PROTO_NAME(DiskIoProtocol),
+                              &gEfiDiskIoProtocolGuid,
                               (VOID **) &DiskIo,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -295,13 +291,13 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Supported(IN EFI_DRIVER_BINDING_PROTOCOL
 
     // we were just checking, close it again
     refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
-                      &PROTO_NAME(DiskIoProtocol),
+                      &gEfiDiskIoProtocolGuid,
                       This->DriverBindingHandle,
                       ControllerHandle);
 
     // next, check BlockIO without actually opening it
     Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
-                              &PROTO_NAME(BlockIoProtocol),
+                              &gEfiBlockIoProtocolGuid,
                               NULL,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -337,7 +333,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
 
     // open consumed protocols
     Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
-                              &PROTO_NAME(BlockIoProtocol),
+                              &gEfiBlockIoProtocolGuid,
                               (VOID **) &BlockIo,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -348,14 +344,14 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
     }
 
     Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
-                              &PROTO_NAME(DiskIoProtocol),
+                              &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);
-       return Status;
+        Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %x\n", Status);
+        return Status;
     }
 
     // allocate volume structure
@@ -376,7 +372,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
         Volume->FileSystem.Revision     = EFI_FILE_IO_INTERFACE_REVISION;
         Volume->FileSystem.OpenVolume   = fsw_efi_FileSystem_OpenVolume;
         Status = refit_call4_wrapper(BS->InstallMultipleProtocolInterfaces, &ControllerHandle,
-                                                       &PROTO_NAME(SimpleFileSystemProtocol),
+                                                       &gEfiSimpleFileSystemProtocolGuid,
                                                        &Volume->FileSystem,
                                                        NULL);
         if (EFI_ERROR(Status)) {
@@ -391,7 +387,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL  *T
         FreePool(Volume);
 
         refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
-                          &PROTO_NAME(DiskIoProtocol),
+                          &gEfiDiskIoProtocolGuid,
                           This->DriverBindingHandle,
                           ControllerHandle);
     }
@@ -417,7 +413,6 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
     EFI_STATUS          Status;
     EFI_FILE_IO_INTERFACE *FileSystem;
     FSW_VOLUME_DATA     *Volume;
-    int                 i;
 
 #if DEBUG_LEVEL
     Print(L"fsw_efi_DriverBinding_Stop\n");
@@ -425,7 +420,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
 
     // get the installed SimpleFileSystem interface
     Status = refit_call6_wrapper(BS->OpenProtocol, ControllerHandle,
-                              &PROTO_NAME(SimpleFileSystemProtocol),
+                              &gEfiSimpleFileSystemProtocolGuid,
                               (VOID **) &FileSystem,
                               This->DriverBindingHandle,
                               ControllerHandle,
@@ -438,7 +433,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
 
     // uninstall Simple File System protocol
     Status = refit_call4_wrapper(BS->UninstallMultipleProtocolInterfaces, ControllerHandle,
-                                                     &PROTO_NAME(SimpleFileSystemProtocol), &Volume->FileSystem,
+                                                     &gEfiSimpleFileSystemProtocolGuid, &Volume->FileSystem,
                                                      NULL);
     if (EFI_ERROR(Status)) {
  //       Print(L"Fsw ERROR: UninstallMultipleProtocolInterfaces returned %x\n", Status);
@@ -455,17 +450,13 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN  EFI_DRIVER_BINDING_PROTOCOL  *T
 
     // close the consumed protocols
     Status = refit_call4_wrapper(BS->CloseProtocol, ControllerHandle,
-                               &PROTO_NAME(DiskIoProtocol),
+                               &gEfiDiskIoProtocolGuid,
                                This->DriverBindingHandle,
                                ControllerHandle);
 
     // clear the cache
-    for (i = 0; i < NUM_CACHES; i++) {
-       if (Caches[i].Cache != NULL) {
-          FreePool(Caches[i].Cache);
-          Caches[i].Cache = NULL;
-       } // if
-    }
+    fsw_efi_clear_cache();
+
     return Status;
 }
 
@@ -527,7 +518,6 @@ void fsw_efi_change_blocksize(struct fsw_volume *vol,
  */
 
 fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) {
-   static int       LastRead = -1;
    int              i, ReadCache = -1;
    FSW_VOLUME_DATA  *Volume = (FSW_VOLUME_DATA *)vol->host_data;
    EFI_STATUS       Status = EFI_SUCCESS;
@@ -539,18 +529,14 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *
 
    // Initialize static data structures, if necessary....
    if (LastRead < 0) {
-      for (i = 0; i < NUM_CACHES; i++) {
-         Caches[i].Cache = NULL;
-         Caches[i].CacheStart = 0;
-         Caches[i].CacheValid = FALSE;
-         Caches[i].Volume = NULL;
-      } // for
+      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;
@@ -563,6 +549,7 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *
       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) {
@@ -581,7 +568,7 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *
       } // if cache memory allocated
    } // if (ReadCache < 0)
 
-   if (Caches[ReadCache].Cache != NULL) {
+   if (Caches[ReadCache].Cache != NULL && Caches[ReadCache].CacheValid == TRUE) {
       CopyMem(buffer, &Caches[ReadCache].Cache[StartRead - Caches[ReadCache].CacheStart], vol->phys_blocksize);
    } else {
       ReadOneBlock = TRUE;
@@ -593,6 +580,7 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *
                                    vol->phys_blocksize,
                                    buffer);
    }
+   Volume->LastIOStatus = Status;
 
    return Status;
 } // fsw_status_t *fsw_efi_read_block()
@@ -640,6 +628,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;