+// Returns FALSE if the specified file/volume matches the GlobalConfig.DontScanDirs
+// or GlobalConfig.DontScanVolumes specification, or if Path points to a volume
+// other than the one specified by Volume, or if the specified path is SelfDir.
+// Returns TRUE if none of these conditions is met -- that is, if the path is
+// eligible for scanning.
+static BOOLEAN ShouldScan(REFIT_VOLUME *Volume, CHAR16 *Path) {
+ CHAR16 *VolName = NULL, *DontScanDir, *PathCopy = NULL;
+ UINTN i = 0, VolNum;
+ BOOLEAN ScanIt = TRUE;
+
+ if (IsIn(Volume->VolName, GlobalConfig.DontScanVolumes))
+ return FALSE;
+
+ if ((StriCmp(Path, SelfDirPath) == 0) && (Volume->DeviceHandle == SelfVolume->DeviceHandle))
+ return FALSE;
+
+ // See if Path includes an explicit volume declaration that's NOT Volume....
+ PathCopy = StrDuplicate(Path);
+ if (SplitVolumeAndFilename(&PathCopy, &VolName)) {
+ if (StriCmp(VolName, Volume->VolName) != 0) {
+ if ((StrLen(VolName) > 2) && (VolName[0] == L'f') && (VolName[1] == L's') && (VolName[2] >= L'0') && (VolName[2] <= L'9')) {
+ VolNum = Atoi(VolName + 2);
+ if (VolNum != Volume->VolNumber) {
+ ScanIt = FALSE;
+ }
+ } else {
+ ScanIt = FALSE;
+ }
+ } // if
+ } // if Path includes volume specification
+ MyFreePool(PathCopy);
+ MyFreePool(VolName);
+ VolName = NULL;
+
+ // See if Volume is in GlobalConfig.DontScanDirs....
+ while ((DontScanDir = FindCommaDelimited(GlobalConfig.DontScanDirs, i++)) && ScanIt) {
+ SplitVolumeAndFilename(&DontScanDir, &VolName);
+ CleanUpPathNameSlashes(DontScanDir);
+ if (VolName != NULL) {
+ if ((StriCmp(VolName, Volume->VolName) == 0) && (StriCmp(DontScanDir, Path) == 0))
+ ScanIt = FALSE;
+ if ((StrLen(VolName) > 2) && (VolName[0] == L'f') && (VolName[1] == L's') && (VolName[2] >= L'0') && (VolName[2] <= L'9')) {
+ VolNum = Atoi(VolName + 2);
+ if ((VolNum == Volume->VolNumber) && (StriCmp(DontScanDir, Path) == 0))
+ ScanIt = FALSE;
+ }
+ } else {
+ if (StriCmp(DontScanDir, Path) == 0)
+ ScanIt = FALSE;
+ }
+ MyFreePool(DontScanDir);
+ MyFreePool(VolName);
+ DontScanDir = NULL;
+ } // while()
+
+ return ScanIt;
+} // BOOLEAN ShouldScan()
+
+// Returns TRUE if the file is byte-for-byte identical with the fallback file
+// on the volume AND if the file is not itself the fallback file; returns
+// FALSE if the file is not identical to the fallback file OR if the file
+// IS the fallback file. Intended for use in excluding the fallback boot
+// loader when it's a duplicate of another boot loader.
+static BOOLEAN DuplicatesFallback(IN REFIT_VOLUME *Volume, IN CHAR16 *FileName) {
+ CHAR8 *FileContents, *FallbackContents;
+ EFI_FILE_HANDLE FileHandle, FallbackHandle;
+ EFI_FILE_INFO *FileInfo, *FallbackInfo;
+ UINTN FileSize = 0, FallbackSize = 0;
+ EFI_STATUS Status;
+ BOOLEAN AreIdentical = FALSE;
+
+ if (!FileExists(Volume->RootDir, FileName) || !FileExists(Volume->RootDir, FALLBACK_FULLNAME))
+ return FALSE;
+
+ CleanUpPathNameSlashes(FileName);
+
+ if (StriCmp(FileName, FALLBACK_FULLNAME) == 0)
+ return FALSE; // identical filenames, so not a duplicate....
+
+ Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
+ if (Status == EFI_SUCCESS) {
+ FileInfo = LibFileInfo(FileHandle);
+ FileSize = FileInfo->FileSize;
+ } else {
+ return FALSE;
+ }
+
+ Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FallbackHandle, FALLBACK_FULLNAME, EFI_FILE_MODE_READ, 0);
+ if (Status == EFI_SUCCESS) {
+ FallbackInfo = LibFileInfo(FallbackHandle);
+ FallbackSize = FallbackInfo->FileSize;
+ } else {
+ refit_call1_wrapper(FileHandle->Close, FileHandle);
+ return FALSE;
+ }
+
+ if (FallbackSize != FileSize) { // not same size, so can't be identical
+ AreIdentical = FALSE;
+ } else { // could be identical; do full check....
+ FileContents = AllocatePool(FileSize);
+ FallbackContents = AllocatePool(FallbackSize);
+ if (FileContents && FallbackContents) {
+ Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &FileSize, FileContents);
+ if (Status == EFI_SUCCESS) {
+ Status = refit_call3_wrapper(FallbackHandle->Read, FallbackHandle, &FallbackSize, FallbackContents);
+ }
+ if (Status == EFI_SUCCESS) {
+ AreIdentical = (CompareMem(FileContents, FallbackContents, FileSize) == 0);
+ } // if
+ } // if
+ MyFreePool(FileContents);
+ MyFreePool(FallbackContents);
+ } // if/else
+
+ // BUG ALERT: Some systems (e.g., DUET, some Macs with large displays) crash if the
+ // following two calls are reversed. Go figure....
+ refit_call1_wrapper(FileHandle->Close, FallbackHandle);
+ refit_call1_wrapper(FileHandle->Close, FileHandle);
+ return AreIdentical;
+} // BOOLEAN DuplicatesFallback()
+
+// Returns FALSE if two measures of file size are identical for a single file,
+// TRUE if not or if the file can't be opened and the other measure is non-0.
+// Despite the function's name, this isn't really a direct test of symbolic
+// link status, since EFI doesn't officially support symlinks. It does seem
+// to be a reliable indicator, though. (OTOH, some disk errors might cause a
+// file to fail to open, which would return a false positive -- but as I use
+// this function to exclude symbolic links from the list of boot loaders,
+// that would be fine, since such boot loaders wouldn't work.)
+static BOOLEAN IsSymbolicLink(REFIT_VOLUME *Volume, CHAR16 *Path, EFI_FILE_INFO *DirEntry) {
+ EFI_FILE_HANDLE FileHandle;
+ EFI_FILE_INFO *FileInfo = NULL;
+ EFI_STATUS Status;
+ UINTN FileSize2 = 0;
+ CHAR16 *FileName;
+
+ FileName = StrDuplicate(Path);
+ MergeStrings(&FileName, DirEntry->FileName, L'\\');
+ CleanUpPathNameSlashes(FileName);
+
+ Status = refit_call5_wrapper(Volume->RootDir->Open, Volume->RootDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
+ if (Status == EFI_SUCCESS) {
+ FileInfo = LibFileInfo(FileHandle);
+ if (FileInfo != NULL)
+ FileSize2 = FileInfo->FileSize;
+ }
+
+ MyFreePool(FileName);
+ MyFreePool(FileInfo);
+
+ return (DirEntry->FileSize != FileSize2);
+} // BOOLEAN IsSymbolicLink()
+