]> code.delx.au - refind/blobdiff - refind/lib.c
Improvements to handling of case-insensitive string comparisons, which
[refind] / refind / lib.c
index 980b51a7228d527f5940eb4948bf7196d446bf8c..92abfd04cc46f9e2f84e458696d1e5a875ac52b4 100644 (file)
@@ -50,6 +50,7 @@
 #include "../include/RemovableMedia.h"
 #include "gpt.h"
 #include "config.h"
+#include "../EfiLib/LegacyBios.h"
 
 #ifdef __MAKEWITH_GNUEFI
 #define EfiReallocatePool ReallocatePool
@@ -75,6 +76,7 @@ EFI_DEVICE_PATH EndDevicePath[] = {
 #define REISER2FS_SUPER_MAGIC_STRING     "ReIsEr2Fs"
 #define REISER2FS_JR_SUPER_MAGIC_STRING  "ReIsEr3Fs"
 #define BTRFS_SIGNATURE                  "_BHRfS_M"
+#define XFS_SIGNATURE                    "XFSB"
 #define NTFS_SIGNATURE                   "NTFS    "
 
 // variables
@@ -308,25 +310,12 @@ EFI_STATUS EfivarSetRaw(EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size,
 // list functions
 //
 
-VOID CreateList(OUT VOID ***ListPtr, OUT UINTN *ElementCount, IN UINTN InitialElementCount)
-{
-    UINTN AllocateCount;
-
-    *ElementCount = InitialElementCount;
-    if (*ElementCount > 0) {
-        AllocateCount = (*ElementCount + 7) & ~7;   // next multiple of 8
-        *ListPtr = AllocatePool(sizeof(VOID *) * AllocateCount);
-    } else {
-        *ListPtr = NULL;
-    }
-}
-
 VOID AddListElement(IN OUT VOID ***ListPtr, IN OUT UINTN *ElementCount, IN VOID *NewElement)
 {
     UINTN AllocateCount;
 
-    if ((*ElementCount & 7) == 0) {
-        AllocateCount = *ElementCount + 8;
+    if ((*ElementCount & 15) == 0) {
+        AllocateCount = *ElementCount + 16;
         if (*ElementCount == 0)
             *ListPtr = AllocatePool(sizeof(VOID *) * AllocateCount);
         else
@@ -460,6 +449,9 @@ static CHAR16 *FSTypeName(IN UINT32 TypeCode) {
       case FS_TYPE_BTRFS:
          retval = L" Btrfs";
          break;
+      case FS_TYPE_XFS:
+         retval = L" XFS";
+         break;
       case FS_TYPE_ISO9660:
          retval = L" ISO-9660";
          break;
@@ -494,33 +486,6 @@ static VOID SetFilesystemData(IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT REFI
       SetMem(&(Volume->VolUuid), sizeof(EFI_GUID), 0);
       Volume->FSType = FS_TYPE_UNKNOWN;
 
-      if (BufferSize >= 512) {
-
-         // Search for NTFS, FAT, and MBR/EBR.
-         // These all have 0xAA55 at the end of the first sector, but FAT and
-         // MBR/EBR are not easily distinguished. Thus, we first check to see
-         // if the "volume" is in fact a disk device; then look for NTFS
-         // "magic"; and then check to see if the volume can be mounted, thus
-         // relying on the EFI's built-in FAT driver to identify FAT.
-         Magic16 = (UINT16*) (Buffer + 510);
-         if (*Magic16 == FAT_MAGIC) {
-            MagicString = (char*) (Buffer + 3);
-            // Confusingly, "LogicalPartition" refers to the presence of a
-            // partition table, not an MBR logical partition.
-            if (Volume->BlockIO->Media->LogicalPartition) {
-               Volume->FSType = FS_TYPE_WHOLEDISK;
-            } else if (CompareMem(MagicString, NTFS_SIGNATURE, 8) == 0) {
-               Volume->FSType = FS_TYPE_NTFS;
-               CopyMem(&(Volume->VolUuid), Buffer + 0x48, sizeof(UINT64));
-            } else {
-               RootDir = LibOpenRoot(Volume->DeviceHandle);
-               if (RootDir != NULL)
-                  Volume->FSType = FS_TYPE_FAT;
-            } // if/elseif/else
-            return;
-         } // if
-      } // search for FAT and NTFS magic
-
       if (BufferSize >= (1024 + 100)) {
          Magic16 = (UINT16*) (Buffer + 1024 + 56);
          if (*Magic16 == EXT2_SUPER_MAGIC) { // ext2/3/4
@@ -557,6 +522,14 @@ static VOID SetFilesystemData(IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT REFI
          } // if
       } // search for Btrfs magic
 
+      if (BufferSize >= 512) {
+         MagicString = (char*) Buffer;
+         if (CompareMem(MagicString, XFS_SIGNATURE, 4) == 0) {
+            Volume->FSType = FS_TYPE_XFS;
+            return;
+         }
+      } // search for XFS magic
+
       if (BufferSize >= (1024 + 2)) {
          Magic16 = (UINT16*) (Buffer + 1024);
          if ((*Magic16 == HFSPLUS_MAGIC1) || (*Magic16 == HFSPLUS_MAGIC2)) {
@@ -565,7 +538,39 @@ static VOID SetFilesystemData(IN UINT8 *Buffer, IN UINTN BufferSize, IN OUT REFI
          }
       } // search for HFS+ magic
 
-   } // if (Buffer != NULL)
+      if (BufferSize >= 512) {
+         // Search for NTFS, FAT, and MBR/EBR.
+         // These all have 0xAA55 at the end of the first sector, but FAT and
+         // MBR/EBR are not easily distinguished. Thus, we first look for NTFS
+         // "magic"; then check to see if the volume can be mounted, thus
+         // relying on the EFI's built-in FAT driver to identify FAT; and then
+         // check to see if the "volume" is in fact a whole-disk device.
+         Magic16 = (UINT16*) (Buffer + 510);
+         if (*Magic16 == FAT_MAGIC) {
+            MagicString = (char*) (Buffer + 3);
+            if (CompareMem(MagicString, NTFS_SIGNATURE, 8) == 0) {
+               Volume->FSType = FS_TYPE_NTFS;
+               CopyMem(&(Volume->VolUuid), Buffer + 0x48, sizeof(UINT64));
+            } else {
+               RootDir = LibOpenRoot(Volume->DeviceHandle);
+               if (RootDir != NULL) {
+                  Volume->FSType = FS_TYPE_FAT;
+               } else if (!Volume->BlockIO->Media->LogicalPartition) {
+                  Volume->FSType = FS_TYPE_WHOLEDISK;
+               } // if/elseif/else
+            } // if/else
+            return;
+         } // if
+      } // search for FAT and NTFS magic
+
+      // If no other filesystem is identified and block size is right, assume
+      // it's ISO-9660....
+      if (Volume->BlockIO->Media->BlockSize == 2048) {
+          Volume->FSType = FS_TYPE_ISO9660;
+          return;
+      }
+
+   } // if ((Buffer != NULL) && (Volume != NULL))
 
 } // UINT32 SetFilesystemData()
 
@@ -592,12 +597,9 @@ static VOID ScanVolumeBootcode(REFIT_VOLUME *Volume, BOOLEAN *Bootable)
                                  Volume->BlockIO, Volume->BlockIO->Media->MediaId,
                                  Volume->BlockIOOffset, SAMPLE_SIZE, Buffer);
     if (!EFI_ERROR(Status)) {
-
-//         if (Volume->BlockIO->Media->LogicalPartition)
-//            Print(L"Skipping; whole disk!\n");
-//         else
-           SetFilesystemData(Buffer, SAMPLE_SIZE, Volume);
-//        PauseForKey();
+        SetFilesystemData(Buffer, SAMPLE_SIZE, Volume);
+    }
+    if ((Status == EFI_SUCCESS) && (GlobalConfig.LegacyType == LEGACY_TYPE_MAC)) {
         if ((*((UINT16 *)(Buffer + 510)) == 0xaa55 && Buffer[0] != 0) && (FindMem(Buffer, 512, "EXFAT", 5) == -1)) {
             *Bootable = TRUE;
             Volume->HasBootCode = TRUE;
@@ -617,15 +619,6 @@ static VOID ScanVolumeBootcode(REFIT_VOLUME *Volume, BOOLEAN *Bootable)
             Volume->OSIconName = L"grub,linux";
             Volume->OSName = L"Linux";
 
-//         // Below doesn't produce a bootable entry, so commented out for the moment....
-//         // GRUB in BIOS boot partition:
-//         } else if (FindMem(Buffer, 512, "Geom\0Read\0 Error", 16) >= 0) {
-//             Volume->HasBootCode = TRUE;
-//             Volume->OSIconName = L"grub,linux";
-//             Volume->OSName = L"Linux";
-//             Volume->VolName = L"BIOS Boot Partition";
-//             *Bootable = TRUE;
-
         } else if ((*((UINT32 *)(Buffer + 502)) == 0 &&
                     *((UINT32 *)(Buffer + 506)) == 50000 &&
                     *((UINT16 *)(Buffer + 510)) == 0xaa55) ||
@@ -634,6 +627,15 @@ static VOID ScanVolumeBootcode(REFIT_VOLUME *Volume, BOOLEAN *Bootable)
             Volume->OSIconName = L"freebsd";
             Volume->OSName = L"FreeBSD";
 
+        // If more differentiation needed, also search for
+        // "Invalid partition table" &/or "Missing boot loader".
+        } else if ((*((UINT16 *)(Buffer + 510)) == 0xaa55) &&
+                   (FindMem(Buffer, SECTOR_SIZE, "Boot loader too large", 21) >= 0) &&
+                   (FindMem(Buffer, SECTOR_SIZE, "I/O error loading boot loader", 29) >= 0))  {
+            Volume->HasBootCode = TRUE;
+            Volume->OSIconName = L"freebsd";
+            Volume->OSName = L"FreeBSD";
+
         } else if (FindMem(Buffer, 512, "!Loading", 8) >= 0 ||
                    FindMem(Buffer, SECTOR_SIZE, "/cdboot\0/CDBOOT\0", 16) >= 0) {
             Volume->HasBootCode = TRUE;
@@ -846,20 +848,28 @@ static CHAR16 *GetVolumeName(REFIT_VOLUME *Volume) {
    return FoundName;
 } // static CHAR16 *GetVolumeName()
 
-// Determine the unique GUID and name of the volume and store them.
+// Determine the unique GUID, type code GUID, and name of the volume and store them.
 static VOID SetPartGuidAndName(REFIT_VOLUME *Volume, EFI_DEVICE_PATH_PROTOCOL *DevicePath) {
    HARDDRIVE_DEVICE_PATH    *HdDevicePath;
+   GPT_ENTRY                *PartInfo;
 
-   if (Volume == NULL)
+   if ((Volume == NULL) || (DevicePath == NULL))
       return;
 
    if ((DevicePath->Type == MEDIA_DEVICE_PATH) && (DevicePath->SubType == MEDIA_HARDDRIVE_DP)) {
       HdDevicePath = (HARDDRIVE_DEVICE_PATH*) DevicePath;
       if (HdDevicePath->SignatureType == SIGNATURE_TYPE_GUID) {
          Volume->PartGuid = *((EFI_GUID*) HdDevicePath->Signature);
-         Volume->PartName = PartNameFromGuid(&(Volume->PartGuid));
-      } // if
-   } // if
+         PartInfo = FindPartWithGuid(&(Volume->PartGuid));
+         if (PartInfo) {
+             Volume->PartName = StrDuplicate(PartInfo->name);
+             CopyMem(&(Volume->PartTypeGuid), PartInfo->type_guid, sizeof(EFI_GUID));
+             if (GuidsAreEqual (&(Volume->PartTypeGuid), &gFreedesktopRootGuid)) {
+                GlobalConfig.DiscoveredRoot = Volume;
+             } // if (GUIDs match)
+         } // if (PartInfo exists)
+      } // if (GPT disk)
+   } // if (disk device)
 } // VOID SetPartGuid()
 
 // Return TRUE if NTFS boot files are found or if Volume is unreadable,
@@ -1279,15 +1289,17 @@ VOID ReinitVolumes(VOID)
 
 BOOLEAN FileExists(IN EFI_FILE *BaseDir, IN CHAR16 *RelativePath)
 {
-    EFI_STATUS         Status;
-    EFI_FILE_HANDLE    TestFile;
-
-    Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &TestFile, RelativePath, EFI_FILE_MODE_READ, 0);
-    if (Status == EFI_SUCCESS) {
-        refit_call1_wrapper(TestFile->Close, TestFile);
-        return TRUE;
-    }
-    return FALSE;
+   EFI_STATUS         Status;
+   EFI_FILE_HANDLE    TestFile;
+
+   if (BaseDir != NULL) {
+      Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &TestFile, RelativePath, EFI_FILE_MODE_READ, 0);
+      if (Status == EFI_SUCCESS) {
+         refit_call1_wrapper(TestFile->Close, TestFile);
+         return TRUE;
+      }
+   }
+   return FALSE;
 }
 
 EFI_STATUS DirNextEntry(IN EFI_FILE *Directory, IN OUT EFI_FILE_INFO **DirEntry, IN UINTN FilterMode)
@@ -1410,14 +1422,6 @@ MetaiMatch (IN CHAR16 *String, IN CHAR16 *Pattern)
    return FALSE; // Shouldn't happen
 }
 
-static VOID StrLwr (IN OUT CHAR16 *Str) {
-   if (!mUnicodeCollation) {
-      InitializeUnicodeCollationProtocol();
-   }
-   if (mUnicodeCollation)
-      mUnicodeCollation->StrLwr (mUnicodeCollation, Str);
-}
-
 #endif
 
 BOOLEAN DirIterNext(IN OUT REFIT_DIR_ITER *DirIter, IN UINTN FilterMode, IN CHAR16 *FilePattern OPTIONAL,
@@ -1503,8 +1507,7 @@ CHAR16 * StripEfiExtension(CHAR16 *FileName) {
 
    if ((FileName != NULL) && ((Copy = StrDuplicate(FileName)) != NULL)) {
       Length = StrLen(Copy);
-      // Note: Do StriCmp() twice to work around Gigabyte Hybrid EFI case-sensitivity bug....
-      if ((Length >= 4) && ((StriCmp(&Copy[Length - 4], L".efi") == 0) || (StriCmp(&Copy[Length - 4], L".EFI") == 0))) {
+      if ((Length >= 4) && MyStriCmp(&Copy[Length - 4], L".efi")) {
          Copy[Length - 4] = 0;
       } // if
    } // if
@@ -1530,30 +1533,62 @@ INTN FindMem(IN VOID *Buffer, IN UINTN BufferLength, IN VOID *SearchString, IN U
     return -1;
 }
 
-// Performs a case-insensitive search of BigStr for SmallStr.
-// Returns TRUE if found, FALSE if not.
 BOOLEAN StriSubCmp(IN CHAR16 *SmallStr, IN CHAR16 *BigStr) {
-   CHAR16 *SmallCopy, *BigCopy;
-   BOOLEAN Found = FALSE;
-   UINTN StartPoint = 0, NumCompares = 0, SmallLen = 0;
-
-   if ((SmallStr != NULL) && (BigStr != NULL) && (StrLen(BigStr) >= StrLen(SmallStr))) {
-      SmallCopy = StrDuplicate(SmallStr);
-      BigCopy = StrDuplicate(BigStr);
-      StrLwr(SmallCopy);
-      StrLwr(BigCopy);
-      SmallLen = StrLen(SmallCopy);
-      NumCompares = StrLen(BigCopy) - SmallLen + 1;
-      while ((!Found) && (StartPoint < NumCompares)) {
-         Found = (StrnCmp(SmallCopy, &BigCopy[StartPoint++], SmallLen) == 0);
-      } // while
-      MyFreePool(SmallCopy);
-      MyFreePool(BigCopy);
-   } // if
+    BOOLEAN Found = 0, Terminate = 0;
+    UINTN BigIndex = 0, SmallIndex = 0, BigStart = 0;
 
-   return (Found);
+    if (SmallStr && BigStr) {
+        while (!Terminate) {
+            if (BigStr[BigIndex] == '\0') {
+                Terminate = 1;
+            }
+            if (SmallStr[SmallIndex] == '\0') {
+                Found = 1;
+                Terminate = 1;
+            }
+            if ((SmallStr[SmallIndex] & ~0x20) == (BigStr[BigIndex] & ~0x20)) {
+                SmallIndex++;
+                BigIndex++;
+            } else {
+                SmallIndex = 0;
+                BigStart++;
+                BigIndex = BigStart;
+            }
+        } // while
+    } // if
+    return Found;
 } // BOOLEAN StriSubCmp()
 
+// Performs a case-insensitive string comparison. This function is necesary
+// because some EFIs have buggy StriCmp() functions that actually perform
+// case-sensitive comparisons.
+// Returns TRUE if strings are identical, FALSE otherwise.
+BOOLEAN MyStriCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString) {
+    if (FirstString && SecondString) {
+        while ((*FirstString != L'\0') && ((*FirstString & ~0x20) == (*SecondString & ~0x20))) {
+                FirstString++;
+                SecondString++;
+        }
+        return (*FirstString == *SecondString);
+    } else {
+        return FALSE;
+    }
+} // BOOLEAN MyStriCmp()
+
+// Convert input string to all-lowercase.
+// DO NOT USE the standard StrLwr() function, since it's broken on some EFIs!
+VOID ToLower(CHAR16 * MyString) {
+    UINTN i = 0;
+
+    if (MyString) {
+        while (MyString[i] != L'\0') {
+            if ((MyString[i] >= L'A') && (MyString[i] <= L'Z'))
+                MyString[i] = MyString[i] - L'A' + L'a';
+            i++;
+        } // while
+    } // if
+} // VOID ToLower()
+
 // Merges two strings, creating a new one and returning a pointer to it.
 // If AddChar != 0, the specified character is placed between the two original
 // strings (unless the first string is NULL or empty). The original input
@@ -1573,7 +1608,7 @@ VOID MergeStrings(IN OUT CHAR16 **First, IN CHAR16 *Second, CHAR16 AddChar) {
       Length2 = StrLen(Second);
    NewString = AllocatePool(sizeof(CHAR16) * (Length1 + Length2 + 2));
    if (NewString != NULL) {
-      if ((*First != NULL) && (StrLen(*First) == 0)) {
+      if ((*First != NULL) && (Length1 == 0)) {
          MyFreePool(*First);
          *First = NULL;
       }
@@ -1592,7 +1627,35 @@ VOID MergeStrings(IN OUT CHAR16 **First, IN CHAR16 *Second, CHAR16 AddChar) {
    } else {
       Print(L"Error! Unable to allocate memory in MergeStrings()!\n");
    } // if/else
-} // static CHAR16* MergeStrings()
+} // VOID MergeStrings()
+
+// Similar to MergeStrings, but breaks the input string into word chunks and
+// merges each word separately. Words are defined as string fragments separated
+// by ' ', '_', or '-'.
+VOID MergeWords(CHAR16 **MergeTo, CHAR16 *SourceString, CHAR16 AddChar) {
+    CHAR16 *Temp, *Word, *p;
+    BOOLEAN LineFinished = FALSE;
+
+    if (SourceString) {
+        Temp = Word = p = StrDuplicate(SourceString);
+        if (Temp) {
+            while (!LineFinished) {
+                if ((*p == L' ') || (*p == L'_') || (*p == L'-') || (*p == L'\0')) {
+                    if (*p == L'\0')
+                        LineFinished = TRUE;
+                    *p = L'\0';
+                    if (*Word != L'\0')
+                        MergeStrings(MergeTo, Word, AddChar);
+                    Word = p + 1;
+                } // if
+                p++;
+            } // while
+            MyFreePool(Temp);
+        } else {
+            Print(L"Error! Unable to allocate memory in MergeWords()!\n");
+        } // if/else
+    } // if
+} // VOID MergeWords()
 
 // Takes an input pathname (*Path) and returns the part of the filename from
 // the final dot onwards, converted to lowercase. If the filename includes
@@ -1617,7 +1680,7 @@ CHAR16 *FindExtension(IN CHAR16 *Path) {
       } // while
       if (Found) {
          MergeStrings(&Extension, &Path[i], 0);
-         StrLwr(Extension);
+         ToLower(Extension);
       } // if (Found)
    } // if
    return (Extension);
@@ -1777,7 +1840,7 @@ VOID FindVolumeAndFilename(IN EFI_DEVICE_PATH *loadpath, OUT REFIT_VOLUME **Devi
    while ((i < VolumesCount) && (!Found)) {
       VolumeDeviceString = DevicePathToStr(Volumes[i]->DevicePath);
       Temp = SplitDeviceString(VolumeDeviceString);
-      if (StriCmp(DeviceString, VolumeDeviceString) == 0) {
+      if (MyStriCmp(DeviceString, VolumeDeviceString)) {
          Found = TRUE;
          *DeviceVolume = Volumes[i];
       }
@@ -1945,8 +2008,7 @@ VOID SplitPathName(CHAR16 *InPath, CHAR16 **VolName, CHAR16 **Path, CHAR16 **Fil
 } // VOID SplitPathName
 
 // Returns TRUE if SmallString is an element in the comma-delimited List,
-// FALSE otherwise. Performs comparison case-insensitively (except on
-// buggy EFIs with case-sensitive StriCmp() functions).
+// FALSE otherwise. Performs comparison case-insensitively.
 BOOLEAN IsIn(IN CHAR16 *SmallString, IN CHAR16 *List) {
    UINTN     i = 0;
    BOOLEAN   Found = FALSE;
@@ -1954,7 +2016,7 @@ BOOLEAN IsIn(IN CHAR16 *SmallString, IN CHAR16 *List) {
 
    if (SmallString && List) {
       while (!Found && (OneElement = FindCommaDelimited(List, i++))) {
-         if (StriCmp(OneElement, SmallString) == 0)
+         if (MyStriCmp(OneElement, SmallString))
             Found = TRUE;
       } // while
    } // if
@@ -1982,8 +2044,7 @@ BOOLEAN IsInSubstring(IN CHAR16 *BigString, IN CHAR16 *List) {
 // element in the comma-delimited List, FALSE otherwise. Note that Directory and
 // Filename must *NOT* include a volume or path specification (that's part of
 // the Volume variable), but the List elements may. Performs comparison
-// case-insensitively (except on buggy EFIs with case-sensitive StriCmp()
-// functions).
+// case-insensitively.
 BOOLEAN FilenameIn(REFIT_VOLUME *Volume, CHAR16 *Directory, CHAR16 *Filename, CHAR16 *List) {
    UINTN     i = 0;
    BOOLEAN   Found = FALSE;
@@ -1995,9 +2056,9 @@ BOOLEAN FilenameIn(REFIT_VOLUME *Volume, CHAR16 *Directory, CHAR16 *Filename, CH
          Found = TRUE;
          SplitPathName(OneElement, &TargetVolName, &TargetPath, &TargetFilename);
          VolumeNumberToName(Volume, &TargetVolName);
-         if (((TargetVolName != NULL) && ((Volume == NULL) || (StriCmp(TargetVolName, Volume->VolName) != 0))) ||
-             ((TargetPath != NULL) && (StriCmp(TargetPath, Directory) != 0)) ||
-             ((TargetFilename != NULL) && (StriCmp(TargetFilename, Filename) != 0))) {
+         if (((TargetVolName != NULL) && ((Volume == NULL) || (!MyStriCmp(TargetVolName, Volume->VolName)))) ||
+             ((TargetPath != NULL) && (!MyStriCmp(TargetPath, Directory))) ||
+             ((TargetFilename != NULL) && (!MyStriCmp(TargetFilename, Filename)))) {
             Found = FALSE;
          } // if
          MyFreePool(OneElement);
@@ -2171,5 +2232,5 @@ EFI_GUID StringAsGuid(CHAR16 * InString) {
 // Returns TRUE if the two GUIDs are equal, FALSE otherwise
 BOOLEAN GuidsAreEqual(EFI_GUID *Guid1, EFI_GUID *Guid2) {
    return (CompareMem(Guid1, Guid2, 16) == 0);
-} // BOOLEAN CompareGuids()
+} // BOOLEAN GuidsAreEqual()