+// 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()
+