X-Git-Url: https://code.delx.au/refind/blobdiff_plain/8b5b878155046a6da1956a2855f39f364abc0338..e3d2b4a15b8e4a6e7d6e3a6421270fccfb06c4f0:/refind/lib.c diff --git a/refind/lib.c b/refind/lib.c index 0de0cc4..0053c2b 100644 --- a/refind/lib.c +++ b/refind/lib.c @@ -34,8 +34,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* - * Modifications copyright (c) 2012-2013 Roderick W. Smith - * + * Modifications copyright (c) 2012-2014 Roderick W. Smith + * * Modifications distributed under the terms of the GNU General Public * License (GPL) version 3 (GPLv3), a copy of which must be distributed * with this source code or binaries made from it. @@ -507,7 +507,7 @@ static VOID ScanVolumeBootcode(REFIT_VOLUME *Volume, BOOLEAN *Bootable) UINT8 Buffer[SAMPLE_SIZE]; UINTN i; MBR_PARTITION_INFO *MbrTable; - BOOLEAN MbrTableFound; + BOOLEAN MbrTableFound = FALSE; Volume->HasBootCode = FALSE; Volume->OSIconName = NULL; @@ -637,7 +637,6 @@ static VOID ScanVolumeBootcode(REFIT_VOLUME *Volume, BOOLEAN *Bootable) // check for MBR partition table if (*((UINT16 *)(Buffer + 510)) == 0xaa55) { - MbrTableFound = FALSE; MbrTable = (MBR_PARTITION_INFO *)(Buffer + 446); for (i = 0; i < 4; i++) if (MbrTable[i].StartLBA && MbrTable[i].Size) @@ -705,7 +704,7 @@ static CHAR16 *SizeInIEEEUnits(UINT64 SizeInBytes) { SPrint(TheValue, 255, L"%ld%s", SizeInIeee, Units); } // if return TheValue; -} // CHAR16 *SizeInSIUnits() +} // CHAR16 *SizeInIEEEUnits() // Return a name for the volume. Ideally this should be the label for the // filesystem it contains, but this function falls back to describing the @@ -747,7 +746,7 @@ static CHAR16 *GetVolumeName(IN REFIT_VOLUME *Volume) { if (FoundName != NULL) { TypeName = FSTypeName(Volume->FSType); // NOTE: Don't free TypeName; function returns constant if (StrLen(TypeName) > 0) - SPrint(FoundName, 255, L"%s volume", FSTypeName(Volume->FSType)); + SPrint(FoundName, 255, L"%s volume", TypeName); else SPrint(FoundName, 255, L"unknown volume"); } // if allocated memory OK @@ -765,6 +764,19 @@ static CHAR16 *GetVolumeName(IN REFIT_VOLUME *Volume) { return FoundName; } // static CHAR16 *GetVolumeName() +// Determine the unique GUID of the volume and store it. +static VOID SetPartGuid(REFIT_VOLUME *Volume, EFI_DEVICE_PATH_PROTOCOL *DevicePath) { + HARDDRIVE_DEVICE_PATH *HdDevicePath; + + if (Volume == NULL) + return; + + if ((DevicePath->Type == MEDIA_DEVICE_PATH) && (DevicePath->SubType == MEDIA_HARDDRIVE_DP)) { + HdDevicePath = (HARDDRIVE_DEVICE_PATH*) DevicePath; + Volume->PartGuid = *((EFI_GUID*) HdDevicePath->Signature); + } +} // VOID SetPartGuid() + VOID ScanVolume(REFIT_VOLUME *Volume) { EFI_STATUS Status; @@ -806,6 +818,9 @@ VOID ScanVolume(REFIT_VOLUME *Volume) while (DevicePath != NULL && !IsDevicePathEndType(DevicePath)) { NextDevicePath = NextDevicePathNode(DevicePath); + if (DevicePathType(DevicePath) == MEDIA_DEVICE_PATH) { + SetPartGuid(Volume, DevicePath); + } if (DevicePathType(DevicePath) == MESSAGING_DEVICE_PATH && (DevicePathSubType(DevicePath) == MSG_USB_DP || DevicePathSubType(DevicePath) == MSG_USB_CLASS_DP || @@ -967,7 +982,7 @@ VOID ScanVolumes(VOID) UINTN SectorSum, i, VolNumber = 0; UINT8 *SectorBuffer1, *SectorBuffer2; EFI_GUID *UuidList; - EFI_GUID NullUuid = { 00000000, 0000, 0000, {0000, 0000, 0000, 0000} }; + EFI_GUID NullUuid = NULL_GUID_VALUE; MyFreePool(Volumes); Volumes = NULL; @@ -1138,7 +1153,8 @@ VOID ReinitVolumes(VOID) if (!EFI_ERROR(Status)) { // get the BlockIO protocol - Status = refit_call3_wrapper(BS->HandleProtocol, WholeDiskHandle, &BlockIoProtocol, (VOID **) &Volume->WholeDiskBlockIO); + Status = refit_call3_wrapper(BS->HandleProtocol, WholeDiskHandle, &BlockIoProtocol, + (VOID **) &Volume->WholeDiskBlockIO); if (EFI_ERROR(Status)) { Volume->WholeDiskBlockIO = NULL; CheckError(Status, L"from HandleProtocol"); @@ -1842,6 +1858,68 @@ BOOLEAN EjectMedia(VOID) { return (Ejected > 0); } // VOID EjectMedia() +// Converts consecutive characters in the input string into a +// number, interpreting the string as a hexadecimal number, starting +// at the specified position and continuing for the specified number +// of characters or until the end of the string, whichever is first. +// NumChars must be between 1 and 16. Ignores invalid characters. +UINT64 StrToHex(CHAR16 *Input, UINTN Pos, UINTN NumChars) { + UINT64 retval = 0x00; + UINTN NumDone = 0; + CHAR16 a; + + if ((Input == NULL) || (StrLen(Input) < Pos) || (NumChars == 0) || (NumChars > 16)) { + return 0; + } + + while ((StrLen(Input) >= Pos) && (NumDone < NumChars)) { + a = Input[Pos]; + if ((a >= '0') && (a <= '9')) { + retval *= 0x10; + retval += (a - '0'); + NumDone++; + } + if ((a >= 'a') && (a <= 'f')) { + retval *= 0x10; + retval += (a - 'a' + 0x0a); + NumDone++; + } + if ((a >= 'A') && (a <= 'F')) { + retval *= 0x10; + retval += (a - 'A' + 0x0a); + NumDone++; + } + Pos++; + } // while() + return retval; +} // StrToHex() + +// Returns TRUE if UnknownString can be interpreted as a GUID, FALSE otherwise. +// Note that the input string must have no extraneous spaces and must be +// conventionally formatted as a 36-character GUID, complete with dashes in +// appropriate places. +BOOLEAN IsGuid(CHAR16 *UnknownString) { + UINTN Length, i; + BOOLEAN retval = TRUE; + CHAR16 a; + + if (UnknownString == NULL) + return FALSE; + + Length = StrLen(UnknownString); + if (Length != 36) + return FALSE; + + for (i = 0; i < Length; i++) { + a = UnknownString[i]; + if (((i == 8) || (i == 13) || (i == 18) || (i == 23)) && (a != '-')) { + retval = FALSE; + } else if (((a < 'a') || (a > 'f')) && ((a < 'A') || (a > 'F')) && ((a < '0') && (a > '9'))) { + retval = FALSE; + } // if/else + } // for + return retval; +} // BOOLEAN IsGuid() // Return the GUID as a string, suitable for display to the user. Note that the calling // function is responsible for freeing the allocated memory. @@ -1858,3 +1936,34 @@ CHAR16 * GuidAsString(EFI_GUID *GuidData) { } return TheString; } // GuidAsString(EFI_GUID *GuidData) + +EFI_GUID StringAsGuid(CHAR16 * InString) { + EFI_GUID Guid = NULL_GUID_VALUE; + + if (!IsGuid(InString)) { + return Guid; + } + + Guid.Data1 = (UINT32) StrToHex(InString, 0, 8); + Guid.Data2 = (UINT16) StrToHex(InString, 9, 4); + Guid.Data3 = (UINT16) StrToHex(InString, 14, 4); + Guid.Data4[0] = (UINT8) StrToHex(InString, 19, 2); + Guid.Data4[1] = (UINT8) StrToHex(InString, 21, 2); + Guid.Data4[2] = (UINT8) StrToHex(InString, 23, 2); + Guid.Data4[3] = (UINT8) StrToHex(InString, 26, 2); + Guid.Data4[4] = (UINT8) StrToHex(InString, 28, 2); + Guid.Data4[5] = (UINT8) StrToHex(InString, 30, 2); + Guid.Data4[6] = (UINT8) StrToHex(InString, 32, 2); + Guid.Data4[7] = (UINT8) StrToHex(InString, 34, 2); + + return Guid; +} // EFI_GUID StringAsGuid() + +// Returns TRUE if the two GUIDs are equal, FALSE otherwise +BOOLEAN GuidsAreEqual(EFI_GUID *Guid1, EFI_GUID *Guid2) { + return ((Guid1->Data1 == Guid2->Data1) && (Guid1->Data2 == Guid2->Data2) && (Guid1->Data3 == Guid2->Data3) && + (Guid1->Data4[0] == Guid2->Data4[0]) && (Guid1->Data4[1] == Guid2->Data4[1]) && + (Guid1->Data4[2] == Guid2->Data4[2]) && (Guid1->Data4[3] == Guid2->Data4[3]) && + (Guid1->Data4[4] == Guid2->Data4[4]) && (Guid1->Data4[5] == Guid2->Data4[5]) && + (Guid1->Data4[6] == Guid2->Data4[6]) && (Guid1->Data4[7] == Guid2->Data4[7])); +} // BOOLEAN CompareGuids() \ No newline at end of file