- EFI_STATUS Status;
- UINTN HandleIndex, HandleCount = 0, Ejected = 0;
- EFI_HANDLE *Handles, Handle;
- APPLE_REMOVABLE_MEDIA_PROTOCOL *Ejectable;
-
- Status = LibLocateHandle(ByProtocol, &AppleRemovableMediaGuid, NULL, &HandleCount, &Handles);
- if (EFI_ERROR(Status) || HandleCount == 0)
- return (FALSE); // probably not an Apple system
-
- for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
- Handle = Handles[HandleIndex];
- Status = refit_call3_wrapper(BS->HandleProtocol, Handle, &AppleRemovableMediaGuid, (VOID **) &Ejectable);
- if (EFI_ERROR(Status))
- continue;
- Status = refit_call1_wrapper(Ejectable->Eject, Ejectable);
- if (!EFI_ERROR(Status))
- Ejected++;
- }
- MyFreePool(Handles);
- 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.
-CHAR16 * GuidAsString(EFI_GUID *GuidData) {
- CHAR16 *TheString;
-
- TheString = AllocateZeroPool(42 * sizeof(CHAR16));
- if (TheString != 0) {
- SPrint (TheString, 82, L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
- (UINTN)GuidData->Data1, (UINTN)GuidData->Data2, (UINTN)GuidData->Data3,
- (UINTN)GuidData->Data4[0], (UINTN)GuidData->Data4[1], (UINTN)GuidData->Data4[2],
- (UINTN)GuidData->Data4[3], (UINTN)GuidData->Data4[4], (UINTN)GuidData->Data4[5],
- (UINTN)GuidData->Data4[6], (UINTN)GuidData->Data4[7]);
- }
- return TheString;
-} // GuidAsString(EFI_GUID *GuidData)