2019-09-03 11:58:42 +02:00
|
|
|
/**
|
|
|
|
* Module for work with runtime (RT, NVRAM) vars,
|
|
|
|
* determining default boot volume (Startup disk)
|
|
|
|
* and (kid of) persistent RT support with nvram.plist on CloverEFI.
|
|
|
|
* dmazar, 2012
|
|
|
|
*/
|
|
|
|
|
2020-08-17 21:40:52 +02:00
|
|
|
#include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
|
2021-02-06 18:16:46 +01:00
|
|
|
#include "../include/OSTypes.h"
|
2020-04-16 09:15:26 +02:00
|
|
|
#include "Nvram.h"
|
|
|
|
#include "BootOptions.h"
|
2020-06-04 12:53:53 +02:00
|
|
|
#include "guid.h"
|
2020-08-09 17:55:30 +02:00
|
|
|
#include "../gui/REFIT_MENU_SCREEN.h"
|
2021-02-04 15:04:31 +01:00
|
|
|
#include "../Platform/Volumes.h"
|
2021-02-06 18:16:46 +01:00
|
|
|
#include "../Platform/Settings.h"
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
#ifndef DEBUG_ALL
|
|
|
|
#define DEBUG_SET 1
|
|
|
|
#else
|
|
|
|
#define DEBUG_SET DEBUG_ALL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG_SET == 0
|
|
|
|
#define DBG(...)
|
|
|
|
#else
|
|
|
|
#define DBG(...) DebugLog (DEBUG_SET, __VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for saving nvram.plist and it's data
|
2020-08-25 17:35:19 +02:00
|
|
|
TagDict* gNvramDict;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// vars filled after call to GetEfiBootDeviceFromNvram ()
|
|
|
|
//
|
|
|
|
|
|
|
|
// always contains original efi-boot-device-data
|
2020-06-04 12:53:53 +02:00
|
|
|
EFI_DEVICE_PATH *gEfiBootDeviceData;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// if gEfiBootDeviceData starts with MemoryMapped node, then gBootCampHD = "BootCampHD" var, otherwise == NULL
|
2020-06-04 12:53:53 +02:00
|
|
|
EFI_DEVICE_PATH *gBootCampHD;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// contains only volume dev path from gEfiBootDeviceData or gBootCampHD
|
2020-06-04 12:53:53 +02:00
|
|
|
EFI_DEVICE_PATH *gEfiBootVolume;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// contains file path from gEfiBootDeviceData or gBootCampHD (if exists)
|
2020-06-04 12:53:53 +02:00
|
|
|
CHAR16 *gEfiBootLoaderPath;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// contains GPT GUID from gEfiBootDeviceData or gBootCampHD (if exists)
|
2020-06-04 12:53:53 +02:00
|
|
|
EFI_GUID *gEfiBootDeviceGuid;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Lilu / OpenCore
|
2020-09-07 00:19:48 +02:00
|
|
|
//EFI_GUID gOcVendorVariableGuid = { 0x4D1FDA02, 0x38C7, 0x4A6A, { 0x9C, 0xC6, 0x4B, 0xCC, 0xA8, 0xB3, 0x01, 0x02 } };
|
|
|
|
//EFI_GUID gOcReadOnlyVariableGuid = { 0xE09B9297, 0x7928, 0x4440, { 0x9A, 0xAB, 0xD1, 0xF8, 0x53, 0x6F, 0xBF, 0x0A } };
|
|
|
|
//EFI_GUID gOcWriteOnlyVariableGuid = { 0xF0B9AF8F, 0x2222, 0x4840, { 0x8A, 0x37, 0xEC, 0xF7, 0xCC, 0x8C, 0x12, 0xE1 } };
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Ozmosis
|
|
|
|
EFI_GUID mOzmosisProprietary1Guid = { 0x1F8E0C02, 0x58A9, 0x4E34, { 0xAE, 0x22, 0x2B, 0x63, 0x74, 0x5F, 0xA1, 0x01 } };
|
|
|
|
EFI_GUID mOzmosisProprietary2Guid = { 0x9480E8A1, 0x1793, 0x46C9, { 0x91, 0xD8, 0x11, 0x08, 0xDB, 0xA4, 0x73, 0x1C } };
|
|
|
|
|
|
|
|
// BootChime
|
|
|
|
extern EFI_GUID gBootChimeVendorVariableGuid;
|
|
|
|
|
2019-10-22 14:00:07 +02:00
|
|
|
//Apple variables
|
|
|
|
extern EFI_GUID gAppleCoreStorageVariableGuid;
|
|
|
|
extern EFI_GUID gAppleTamperResistantBootVariableGuid;
|
|
|
|
extern EFI_GUID gAppleWirelessNetworkVariableGuid;
|
|
|
|
extern EFI_GUID gApplePersonalizationVariableGuid;
|
|
|
|
extern EFI_GUID gAppleNetbootVariableGuid;
|
|
|
|
extern EFI_GUID gAppleSecureBootVariableGuid;
|
|
|
|
extern EFI_GUID gAppleTamperResistantBootSecureVariableGuid;
|
|
|
|
extern EFI_GUID gAppleTamperResistantBootEfiUserVariableGuid;
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
APPLE_SMC_IO_PROTOCOL *gAppleSmc = NULL;
|
|
|
|
|
|
|
|
/** returns given time as miliseconds.
|
|
|
|
* assumes 31 days per month, so it's not correct,
|
|
|
|
* but is enough for basic checks.
|
|
|
|
*/
|
|
|
|
UINT64
|
|
|
|
GetEfiTimeInMs (
|
|
|
|
IN EFI_TIME *T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT64 TimeMs;
|
|
|
|
|
|
|
|
TimeMs = T->Year - 1900;
|
|
|
|
// is 64bit multiply workign in 32 bit?
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 12) + T->Month;
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 31) + T->Day; // counting with 31 day
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 24) + T->Hour;
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 60) + T->Minute;
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 60) + T->Second;
|
|
|
|
TimeMs = MultU64x32 (TimeMs, 1000) + DivU64x32(T->Nanosecond, 1000000);
|
|
|
|
|
|
|
|
return TimeMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reads and returns value of NVRAM variable. */
|
2020-10-03 19:02:31 +02:00
|
|
|
void *GetNvramVariable(
|
2020-02-17 21:41:09 +01:00
|
|
|
IN CONST CHAR16 *VariableName,
|
2019-09-03 11:58:42 +02:00
|
|
|
IN EFI_GUID *VendorGuid,
|
2020-08-15 15:47:56 +02:00
|
|
|
OUT UINT32 *Attributes OPTIONAL,
|
|
|
|
OUT UINTN *DataSize OPTIONAL)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2020-10-03 19:02:31 +02:00
|
|
|
void *Data = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// Pass in a zero size buffer to find the required buffer size.
|
|
|
|
//
|
|
|
|
UINTN IntDataSize = 0;
|
|
|
|
|
2020-08-15 15:47:56 +02:00
|
|
|
Status = gRT->GetVariable (VariableName, VendorGuid, Attributes, &IntDataSize, NULL);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (IntDataSize == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
//
|
|
|
|
// Allocate the buffer to return
|
|
|
|
//
|
2020-08-15 15:47:56 +02:00
|
|
|
Data = (__typeof__(Data))AllocateZeroPool(IntDataSize + 1);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Data != NULL) {
|
|
|
|
//
|
|
|
|
// Read variable into the allocated buffer.
|
|
|
|
//
|
|
|
|
Status = gRT->GetVariable (VariableName, VendorGuid, Attributes, &IntDataSize, Data);
|
2020-04-23 11:08:10 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
FreePool(Data);
|
2019-09-03 11:58:42 +02:00
|
|
|
IntDataSize = 0;
|
|
|
|
Data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DataSize != NULL) {
|
|
|
|
*DataSize = IntDataSize;
|
|
|
|
}
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
2020-08-15 15:47:56 +02:00
|
|
|
/** Reads and returns value of NVRAM variable. */
|
|
|
|
XString8 GetNvramVariableAsXString8(
|
|
|
|
IN CONST CHAR16 *VariableName,
|
|
|
|
IN EFI_GUID *VendorGuid,
|
|
|
|
OUT UINT32 *Attributes OPTIONAL,
|
|
|
|
OUT UINTN *DataSize OPTIONAL)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
XString8 returnValue;
|
|
|
|
//
|
|
|
|
// Pass in a zero size buffer to find the required buffer size.
|
|
|
|
//
|
|
|
|
UINTN IntDataSize = 0;
|
|
|
|
|
|
|
|
Status = gRT->GetVariable (VariableName, VendorGuid, Attributes, &IntDataSize, NULL);
|
|
|
|
if (IntDataSize == 0) {
|
|
|
|
return NullXString8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
//
|
|
|
|
// Read variable into the allocated buffer.
|
|
|
|
//
|
|
|
|
Status = gRT->GetVariable(VariableName, VendorGuid, Attributes, &IntDataSize, returnValue.dataSized(IntDataSize+1));
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
IntDataSize = 0;
|
|
|
|
returnValue.setEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DataSize != NULL) {
|
|
|
|
*DataSize = IntDataSize;
|
|
|
|
}
|
2021-04-08 17:07:05 +02:00
|
|
|
returnValue.dataSized(IntDataSize+1)[IntDataSize] = 0;
|
|
|
|
returnValue.updateSize();
|
2020-08-15 15:47:56 +02:00
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/** Sets NVRAM variable. Does nothing if variable with the same data and attributes already exists. */
|
|
|
|
EFI_STATUS
|
|
|
|
SetNvramVariable (
|
2020-02-17 21:41:09 +01:00
|
|
|
IN CONST CHAR16 *VariableName,
|
2019-12-27 17:12:28 +01:00
|
|
|
IN EFI_GUID *VendorGuid,
|
|
|
|
IN UINT32 Attributes,
|
|
|
|
IN UINTN DataSize,
|
2020-10-03 19:02:31 +02:00
|
|
|
IN CONST void *Data
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
2021-02-19 21:07:24 +01:00
|
|
|
EFI_STATUS Status = EFI_SUCCESS;
|
2020-10-03 19:02:31 +02:00
|
|
|
void *OldData;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINTN OldDataSize = 0;
|
|
|
|
UINT32 OldAttributes = 0;
|
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG("SetNvramVariable (%ls, guid, 0x%X, %lld):", VariableName, Attributes, DataSize);
|
2020-04-28 12:49:24 +02:00
|
|
|
OldData = (__typeof__(OldData))GetNvramVariable(VariableName, VendorGuid, &OldAttributes, &OldDataSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (OldData != NULL) {
|
|
|
|
// var already exists - check if it equal to new value
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG(" exists(0x%X, %lld)", OldAttributes, OldDataSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
if ((OldAttributes == Attributes) &&
|
|
|
|
(OldDataSize == DataSize) &&
|
|
|
|
(CompareMem (OldData, Data, DataSize) == 0)) {
|
|
|
|
// it's the same - do nothing
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG(", equal -> not writing again.\n");
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(OldData);
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG(", not equal\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(OldData);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// not the same - delete previous one if attributes are different
|
|
|
|
if (OldAttributes != Attributes) {
|
2021-02-19 21:07:24 +01:00
|
|
|
Status = DeleteNvramVariable(VariableName, VendorGuid);
|
|
|
|
// DBG(", diff. attr: deleting old (%s)", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG("\n"); // for debug without Status
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
Status = gRT->SetVariable(VariableName, VendorGuid, Attributes, DataSize, (void*)Data); // CONST missing in EFI_SET_VARIABLE->SetVariable
|
|
|
|
// DBG(" -> writing new (%s)\n", efiStrError(Status));
|
|
|
|
return Status;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
|
2020-08-15 15:47:56 +02:00
|
|
|
EFI_STATUS
|
|
|
|
SetNvramXString8 (
|
|
|
|
IN CONST CHAR16 *VariableName,
|
|
|
|
IN EFI_GUID *VendorGuid,
|
|
|
|
IN UINT32 Attributes,
|
|
|
|
const XString8& s
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return SetNvramVariable(VariableName, VendorGuid, Attributes, s.sizeInBytes(), (void*)s.c_str());
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/** Sets NVRAM variable. Does nothing if variable with the same name already exists. */
|
|
|
|
EFI_STATUS
|
|
|
|
AddNvramVariable (
|
2020-02-17 21:41:09 +01:00
|
|
|
IN CONST CHAR16 *VariableName,
|
2019-09-03 11:58:42 +02:00
|
|
|
IN EFI_GUID *VendorGuid,
|
|
|
|
IN UINT32 Attributes,
|
|
|
|
IN UINTN DataSize,
|
2020-10-03 19:02:31 +02:00
|
|
|
IN const void *Data
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
2021-02-19 21:07:24 +01:00
|
|
|
EFI_STATUS Status;
|
2020-10-03 19:02:31 +02:00
|
|
|
void *OldData;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("SetNvramVariable (%ls, guid, 0x%X, %lld):\n", VariableName, Attributes, DataSize);
|
2020-04-28 12:49:24 +02:00
|
|
|
OldData = (__typeof__(OldData))GetNvramVariable(VariableName, VendorGuid, NULL, NULL);
|
2021-02-19 21:07:24 +01:00
|
|
|
if (OldData == NULL) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// set new value
|
2021-02-19 21:07:24 +01:00
|
|
|
Status = gRT->SetVariable(VariableName, VendorGuid, Attributes, DataSize, Data);
|
|
|
|
DBG(" -> writing new (%s)\n", efiStrError(Status));
|
|
|
|
} else {
|
|
|
|
FreePool(OldData);
|
|
|
|
Status = EFI_ABORTED;
|
|
|
|
}
|
|
|
|
return Status;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:47:56 +02:00
|
|
|
EFI_STATUS
|
|
|
|
AddNvramXString8(
|
|
|
|
IN CONST CHAR16 *VariableName,
|
|
|
|
IN EFI_GUID *VendorGuid,
|
|
|
|
IN UINT32 Attributes,
|
|
|
|
const XString8& s
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return AddNvramVariable(VariableName, VendorGuid, Attributes, s.sizeInBytes(), (void*)s.c_str());
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/** Deletes NVRAM variable. */
|
|
|
|
EFI_STATUS
|
|
|
|
DeleteNvramVariable (
|
2020-02-17 21:41:09 +01:00
|
|
|
IN CONST CHAR16 *VariableName,
|
2019-09-03 11:58:42 +02:00
|
|
|
IN EFI_GUID *VendorGuid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
|
|
|
// Delete: attributes and data size = 0
|
|
|
|
Status = gRT->SetVariable (VariableName, VendorGuid, 0, 0, NULL);
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("DeleteNvramVariable (%ls, guid = %s):\n", VariableName, efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
IsDeletableVariable (
|
|
|
|
IN CHAR16 *Name,
|
|
|
|
IN EFI_GUID *Guid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Apple GUIDs
|
2019-10-22 14:00:07 +02:00
|
|
|
if (CompareGuid(Guid, &gEfiAppleVendorGuid) ||
|
|
|
|
CompareGuid(Guid, &gEfiAppleBootGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleCoreStorageVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleTamperResistantBootVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleWirelessNetworkVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gApplePersonalizationVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleNetbootVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleSecureBootVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleTamperResistantBootSecureVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gAppleTamperResistantBootEfiUserVariableGuid)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// Disable Clover Boot Options from being deleted
|
|
|
|
// Global variable boot options
|
|
|
|
/*} else if (CompareGuid (Guid, &gEfiGlobalVariableGuid)) {
|
|
|
|
// Only erase boot and driver entries for BDS
|
|
|
|
// I.e. BootOrder, Boot####, DriverOrder, Driver####
|
|
|
|
if (!StrnCmp (Name, L"Boot", StrLen(L"Boot")) ||
|
|
|
|
!StrnCmp (Name, L"Driver", StrLen(L"Driver"))) {
|
|
|
|
return TRUE;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// Lilu / OpenCore extensions
|
|
|
|
} else if (CompareGuid (Guid, &gOcVendorVariableGuid) ||
|
2019-10-22 14:00:07 +02:00
|
|
|
CompareGuid(Guid, &gOcReadOnlyVariableGuid) ||
|
|
|
|
CompareGuid(Guid, &gOcWriteOnlyVariableGuid)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// Ozmozis extensions
|
|
|
|
} else if (CompareGuid (Guid, &mOzmosisProprietary1Guid) ||
|
2019-10-22 14:00:07 +02:00
|
|
|
CompareGuid (Guid, &mOzmosisProprietary2Guid)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// BootChime
|
|
|
|
} else if (CompareGuid (Guid, &gBootChimeVendorVariableGuid)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset Native NVRAM by vit9696, reworked and implemented by Sherlocks
|
|
|
|
EFI_STATUS
|
|
|
|
ResetNativeNvram ()
|
|
|
|
{
|
2019-11-22 14:19:43 +01:00
|
|
|
EFI_STATUS Status = EFI_NOT_FOUND;
|
|
|
|
EFI_GUID Guid;
|
|
|
|
CHAR16 *Name;
|
|
|
|
UINTN NameSize;
|
|
|
|
UINTN NewNameSize;
|
|
|
|
BOOLEAN Restart = TRUE;
|
|
|
|
UINTN VolumeIndex;
|
|
|
|
REFIT_VOLUME *Volume;
|
2020-10-03 19:02:31 +02:00
|
|
|
EFI_FILE* FileHandle;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//DbgHeader("ResetNativeNvram: cleanup NVRAM variables");
|
|
|
|
|
|
|
|
NameSize = sizeof (CHAR16);
|
2020-08-15 15:47:56 +02:00
|
|
|
Name = (__typeof__(Name))AllocateZeroPool(NameSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Name == NULL) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
if (Restart) {
|
|
|
|
ZeroMem (&Guid, sizeof(Guid));
|
|
|
|
ZeroMem (Name, sizeof(Name));
|
|
|
|
Restart = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewNameSize = NameSize;
|
|
|
|
Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
2019-12-21 01:31:49 +01:00
|
|
|
Name = (__typeof__(Name))ReallocatePool (NameSize, NewNameSize, Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Name == NULL) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
|
|
|
|
NameSize = NewNameSize;
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:08:10 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:08:10 +02:00
|
|
|
if (!EFI_ERROR(Status)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
if (IsDeletableVariable (Name, &Guid)) {
|
2020-04-23 18:05:21 +02:00
|
|
|
//DBG("Deleting %s:%ls...", strguid(&Guid), Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
Status = DeleteNvramVariable(Name, &Guid);
|
2020-04-23 11:08:10 +02:00
|
|
|
if (!EFI_ERROR(Status)) {
|
2020-04-23 18:05:21 +02:00
|
|
|
//DBG("OK\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
Restart = TRUE;
|
|
|
|
} else {
|
2020-08-25 17:35:19 +02:00
|
|
|
//DBG("FAIL (%s)\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Name) {
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 14:19:43 +01:00
|
|
|
if (gFirmwareClover || gDriversFlags.EmuVariableLoaded) {
|
|
|
|
//DBG("Searching volumes for nvram.plist\n");
|
2020-02-27 15:34:29 +01:00
|
|
|
for (VolumeIndex = 0; VolumeIndex < Volumes.size(); ++VolumeIndex) {
|
|
|
|
Volume = &Volumes[VolumeIndex];
|
2019-11-22 14:19:43 +01:00
|
|
|
|
|
|
|
if (!Volume->RootDir) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2019-11-22 14:19:43 +01:00
|
|
|
Status = Volume->RootDir->Open (Volume->RootDir, &FileHandle, L"nvram.plist", EFI_FILE_MODE_READ, 0);
|
|
|
|
if (EFI_ERROR(Status)) {
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("- [%02d]: '%ls' - no nvram.plist - skipping!\n", VolumeIndex, Volume->VolName);
|
2019-11-22 14:19:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the partition where nvram.plist can be deleted and delete it
|
|
|
|
if (Volume != NULL) {
|
2020-08-09 17:55:30 +02:00
|
|
|
if (StriStr(Volume->VolName.wc_str(), L"EFI") != NULL) {
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("- [%02d]: '%ls' - found nvram.plist and deleted it\n", VolumeIndex, Volume->VolName);
|
2019-11-22 14:19:43 +01:00
|
|
|
Status = DeleteFile (Volume->RootDir, L"nvram.plist");
|
|
|
|
} else {
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("- [%02d]: '%ls' - found nvram.plist but can't delete it\n", VolumeIndex, Volume->VolName);
|
2019-11-22 14:19:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2019-11-22 14:19:43 +01:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// Print all fakesmc variables, i.e. SMC keys
|
|
|
|
///
|
|
|
|
UINT32 KeyFromName(CHAR16 *Name)
|
|
|
|
{
|
|
|
|
//fakesmc-key-CLKT-ui32: Size = 4, Data: 00 00 8C BE
|
|
|
|
UINT32 Key;
|
|
|
|
Key = ((Name[12] & 0xFF) << 24) + ((Name[13] & 0xFF) << 16) +
|
|
|
|
((Name[14] & 0xFF) << 8) + ((Name[15] & 0xFF) << 0);
|
|
|
|
return Key;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 TypeFromName(CHAR16 *Name)
|
|
|
|
{
|
|
|
|
//fakesmc-key-CLKT-ui32: Size = 4, Data: 00 00 8C BE
|
|
|
|
UINT32 Key;
|
|
|
|
Key = ((Name[17] & 0xFF) << 24) + ((Name[18] & 0xFF) << 16) +
|
|
|
|
((Name[19] & 0xFF) << 8) + ((Name[20] & 0xFF) << 0);
|
|
|
|
if (Name[20] == L'\0') {
|
|
|
|
Key += 0x20; //' '
|
|
|
|
}
|
|
|
|
return Key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-17 21:41:09 +01:00
|
|
|
UINT32 FourCharKey(CONST CHAR8 *Name)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
return (Name[0] << 24) + (Name[1] << 16) + (Name[2] << 8) + Name[3]; //Big Endian
|
|
|
|
}
|
|
|
|
|
|
|
|
INT8 NKey[4] = {0, 0, 0, 0};
|
|
|
|
INT8 SAdr[4] = {0, 0, 3, 0};
|
|
|
|
INT8 SNum[1] = {1};
|
|
|
|
|
2020-10-03 19:02:31 +02:00
|
|
|
void
|
2019-09-03 11:58:42 +02:00
|
|
|
GetSmcKeys (BOOLEAN WriteToSMC)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
CHAR16 *Name;
|
|
|
|
EFI_GUID Guid;
|
|
|
|
UINTN NameSize;
|
|
|
|
UINTN NewNameSize;
|
|
|
|
UINT8 *Data;
|
|
|
|
UINTN DataSize;
|
|
|
|
INTN NumKey = 0;
|
|
|
|
STATIC UINTN Once = 0;
|
|
|
|
if (Once++) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NameSize = sizeof (CHAR16);
|
2020-08-15 15:47:56 +02:00
|
|
|
Name = (__typeof__(Name))AllocateZeroPool(NameSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Name == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DbgHeader("Dump SMC keys from NVRAM");
|
2020-10-03 19:02:31 +02:00
|
|
|
Status = gBS->LocateProtocol(&gAppleSMCProtocolGuid, NULL, (void**)&gAppleSmc);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
DBG("found AppleSMC protocol\n");
|
|
|
|
} else {
|
|
|
|
DBG("no AppleSMC protocol\n");
|
|
|
|
gAppleSmc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
NewNameSize = NameSize;
|
|
|
|
Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
2019-12-21 01:31:49 +01:00
|
|
|
Name = (__typeof__(Name))ReallocatePool (NameSize, NewNameSize, Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Name == NULL) {
|
|
|
|
return; //if something wrong then just do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
|
|
|
|
NameSize = NewNameSize;
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:08:10 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
break; //no more variables
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StrStr(Name, L"fakesmc-key")) {
|
|
|
|
continue; //the variable is not interesting for us
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:49:24 +02:00
|
|
|
Data = (__typeof__(Data))GetNvramVariable(Name, &Guid, NULL, &DataSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Data) {
|
2021-02-19 21:07:24 +01:00
|
|
|
UINTN Index;
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG(" %ls:", Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
for (Index = 0; Index < DataSize; Index++) {
|
2020-04-17 15:14:24 +02:00
|
|
|
DBG("%02hhX ", *((UINT8*)Data + Index));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gAppleSmc && WriteToSMC) {
|
|
|
|
Status = gAppleSmc->SmcAddKey(gAppleSmc, KeyFromName(Name), (SMC_DATA_SIZE)DataSize, TypeFromName(Name), 0xC0);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
Status = gAppleSmc->SmcWriteValue(gAppleSmc, KeyFromName(Name), (SMC_DATA_SIZE)DataSize, Data);
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG("Write to AppleSMC status=%s\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
NumKey++;
|
|
|
|
}
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(Data);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (WriteToSMC && gAppleSmc && (gAppleSmc->Signature == NON_APPLE_SMC_SIGNATURE)) {
|
|
|
|
CHAR8 Mode = SMC_MODE_APPCODE;
|
|
|
|
NKey[3] = NumKey & 0xFF;
|
|
|
|
NKey[2] = (NumKey >> 8) & 0xFF; //key, size, type, attr
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("Registered %lld SMC keys\n", NumKey);
|
2019-09-03 11:58:42 +02:00
|
|
|
Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("#KEY"), 4, SmcKeyTypeUint32, 0xC0);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("#KEY"), 4, (SMC_DATA *)&NKey);
|
|
|
|
}
|
|
|
|
Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Adr"), 4, SmcKeyTypeUint32, 0x08);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Adr"), 4, (SMC_DATA *)&SAdr);
|
|
|
|
}
|
|
|
|
Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Num"), 1, SmcKeyTypeUint8, 0x08);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Num"), 1, (SMC_DATA *)&SNum);
|
|
|
|
}
|
|
|
|
Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("RMde"), 1, SmcKeyTypeChar, 0xC0);
|
|
|
|
if (!EFI_ERROR(Status)) {
|
|
|
|
Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("RMde"), 1, (SMC_DATA *)&Mode);
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(Name);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-07-14 09:51:12 +02:00
|
|
|
#if CHECK_SMC
|
2020-10-03 19:02:31 +02:00
|
|
|
void DumpSmcKeys()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
if (!gAppleSmc || !gAppleSmc->DumpData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gAppleSmc->DumpData(gAppleSmc);
|
|
|
|
}
|
2020-07-14 09:51:12 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/** Searches for GPT HDD dev path node and return pointer to partition GUID or NULL. */
|
|
|
|
EFI_GUID
|
|
|
|
*FindGPTPartitionGuidInDevicePath (
|
2020-09-28 17:57:50 +02:00
|
|
|
const EFI_DEVICE_PATH_PROTOCOL *DevicePath
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
HARDDRIVE_DEVICE_PATH *HDDDevPath;
|
|
|
|
EFI_GUID *Guid = NULL;
|
|
|
|
|
|
|
|
if (DevicePath == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!IsDevicePathEndType(DevicePath) &&
|
|
|
|
!(DevicePathType(DevicePath) == MEDIA_DEVICE_PATH && DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {
|
|
|
|
DevicePath = NextDevicePathNode(DevicePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DevicePathType(DevicePath) == MEDIA_DEVICE_PATH && DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP) {
|
|
|
|
HDDDevPath = (HARDDRIVE_DEVICE_PATH*)DevicePath;
|
|
|
|
if (HDDDevPath->SignatureType == SIGNATURE_TYPE_GUID) {
|
|
|
|
Guid = (EFI_GUID*)HDDDevPath->Signature;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** detailed debug for BootVolumeDevicePathEqual */
|
2021-02-20 19:33:09 +01:00
|
|
|
#define DBG_DP(...)
|
|
|
|
//#define DBG_DP(...) DBG(__VA_ARGS__)
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/** Returns TRUE if dev paths are equal. Ignores some differences. */
|
|
|
|
BOOLEAN
|
|
|
|
BootVolumeDevicePathEqual (
|
2021-02-19 21:07:24 +01:00
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1,
|
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
BOOLEAN Equal;
|
|
|
|
UINT8 Type1;
|
|
|
|
UINT8 SubType1;
|
|
|
|
UINT8 Type2;
|
|
|
|
UINTN Len1;
|
|
|
|
UINT8 SubType2;
|
|
|
|
UINTN Len2;
|
|
|
|
SATA_DEVICE_PATH *SataNode1;
|
|
|
|
SATA_DEVICE_PATH *SataNode2;
|
|
|
|
BOOLEAN ForceEqualNodes;
|
|
|
|
|
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG_DP (" BootVolumeDevicePathEqual:\n %ls\n %ls\n", FileDevicePathToStr (DevicePath1), FileDevicePathToStr (DevicePath2));
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" N1: (Type, Subtype, Len) N2: (Type, Subtype, Len)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
Equal = FALSE;
|
|
|
|
while (TRUE) {
|
|
|
|
Type1 = DevicePathType (DevicePath1);
|
|
|
|
SubType1 = DevicePathSubType (DevicePath1);
|
|
|
|
Len1 = DevicePathNodeLength (DevicePath1);
|
|
|
|
|
|
|
|
Type2 = DevicePathType (DevicePath2);
|
|
|
|
SubType2 = DevicePathSubType (DevicePath2);
|
|
|
|
Len2 = DevicePathNodeLength (DevicePath2);
|
|
|
|
|
|
|
|
ForceEqualNodes = FALSE;
|
|
|
|
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" N1: (%d, %d, %lld)", Type1, SubType1, Len1);
|
|
|
|
// DBG_DP (" N2: (%d, %d, %lld)", Type2, SubType2, Len2);
|
2021-02-19 21:07:24 +01:00
|
|
|
|
|
|
|
// DBG_DP ("%ls\n", DevicePathToStr (DevicePath1));
|
|
|
|
// DBG_DP ("%ls\n", DevicePathToStr (DevicePath2));
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Some eSata device can have path:
|
|
|
|
// PciRoot(0x0)/Pci(0x1C,0x5)/Pci(0x0,0x0)/VenHw(CF31FAC5-C24E-11D2-85F3-00A0C93EC93B,80)
|
|
|
|
// while macOS can set it as
|
|
|
|
// PciRoot(0x0)/Pci(0x1C,0x5)/Pci(0x0,0x0)/Sata(0x0,0x0,0x0)
|
|
|
|
// we'll assume VenHw and Sata nodes to be equal to cover that
|
|
|
|
// as well add NVME to this comparison
|
|
|
|
//
|
|
|
|
if (Type1 == MESSAGING_DEVICE_PATH && SubType1 == MSG_SATA_DP) {
|
|
|
|
if ((Type2 == HARDWARE_DEVICE_PATH && SubType2 == HW_VENDOR_DP)
|
|
|
|
|| (Type2 == MESSAGING_DEVICE_PATH && SubType2 == MSG_NVME_NAMESPACE_DP)) {
|
|
|
|
ForceEqualNodes = TRUE;
|
|
|
|
}
|
|
|
|
} else if (Type2 == MESSAGING_DEVICE_PATH && SubType2 == MSG_SATA_DP &&
|
|
|
|
((Type1 == HARDWARE_DEVICE_PATH && SubType1 == HW_VENDOR_DP)
|
|
|
|
|| (Type1 == MESSAGING_DEVICE_PATH && SubType1 == MSG_NVME_NAMESPACE_DP))) {
|
|
|
|
ForceEqualNodes = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// UEFI can see it as PcieRoot, while macOS could generate PciRoot
|
|
|
|
// we'll assume Acpi dev path nodes to be equal to cover that
|
|
|
|
//
|
|
|
|
if (Type1 == ACPI_DEVICE_PATH && Type2 == ACPI_DEVICE_PATH) {
|
|
|
|
ForceEqualNodes = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ForceEqualNodes) {
|
|
|
|
// assume equal nodes
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - forcing equal nodes\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
DevicePath1 = NextDevicePathNode (DevicePath1);
|
|
|
|
DevicePath2 = NextDevicePathNode (DevicePath2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Type1 != Type2 || SubType1 != SubType2 || Len1 != Len2) {
|
|
|
|
// Not equal
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - not equal\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Same type/subtype/len ...
|
|
|
|
//
|
|
|
|
if (IsDevicePathEnd (DevicePath1)) {
|
|
|
|
// END node - they are the same
|
|
|
|
Equal = TRUE;
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - END = equal\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do mem compare of nodes or special compare for selected types/subtypes
|
|
|
|
//
|
|
|
|
if (Type1 == MESSAGING_DEVICE_PATH && SubType1 == MSG_SATA_DP) {
|
|
|
|
//
|
|
|
|
// Ignore
|
|
|
|
//
|
|
|
|
SataNode1 = (SATA_DEVICE_PATH *)DevicePath1;
|
|
|
|
SataNode2 = (SATA_DEVICE_PATH *)DevicePath2;
|
|
|
|
if (SataNode1->HBAPortNumber != SataNode2->HBAPortNumber) {
|
|
|
|
// not equal
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - not equal SataNode.HBAPortNumber\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SataNode1->Lun != SataNode2->Lun) {
|
|
|
|
// not equal
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - not equal SataNode.Lun\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - forcing equal nodes");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (CompareMem (DevicePath1, DevicePath2, DevicePathNodeLength (DevicePath1)) != 0) {
|
|
|
|
// Not equal
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP (" - not equal\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:33:09 +01:00
|
|
|
// DBG_DP ("\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// Advance to next node
|
|
|
|
//
|
|
|
|
DevicePath1 = NextDevicePathNode (DevicePath1);
|
|
|
|
DevicePath2 = NextDevicePathNode (DevicePath2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns TRUE if dev paths contain the same MEDIA_DEVICE_PATH. */
|
|
|
|
BOOLEAN
|
|
|
|
BootVolumeMediaDevicePathNodesEqual (
|
2021-02-19 21:07:24 +01:00
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1,
|
|
|
|
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
2020-09-07 00:19:48 +02:00
|
|
|
DevicePath1 = Clover_FindDevicePathNodeWithType (DevicePath1, MEDIA_DEVICE_PATH, 0);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (DevicePath1 == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-09-07 00:19:48 +02:00
|
|
|
DevicePath2 = Clover_FindDevicePathNodeWithType (DevicePath2, MEDIA_DEVICE_PATH, 0);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (DevicePath2 == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (DevicePathNodeLength (DevicePath1) == DevicePathNodeLength (DevicePath1))
|
|
|
|
&& (CompareMem (DevicePath1, DevicePath2, DevicePathNodeLength (DevicePath1)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Reads gEfiAppleBootGuid:efi-boot-device-data and BootCampHD NVRAM variables and parses them
|
|
|
|
* into gEfiBootVolume, gEfiBootLoaderPath and gEfiBootDeviceGuid.
|
|
|
|
* Vars after this call:
|
|
|
|
* gEfiBootDeviceData - original efi-boot-device-data
|
|
|
|
* gBootCampHD - if gEfiBootDeviceData starts with MemoryMapped node, then BootCampHD variable (device path), NULL otherwise
|
|
|
|
* gEfiBootVolume - volume device path (from efi-boot-device-data or BootCampHD)
|
|
|
|
* gEfiBootLoaderPath - file path (from efi-boot-device-data or BootCampHD) or NULL
|
|
|
|
* gEfiBootDeviceGuid - GPT volume GUID if gEfiBootVolume or NULL
|
|
|
|
*/
|
|
|
|
EFI_STATUS
|
|
|
|
GetEfiBootDeviceFromNvram ()
|
|
|
|
{
|
|
|
|
UINTN Size = 0;
|
|
|
|
EFI_GUID *Guid;
|
|
|
|
FILEPATH_DEVICE_PATH *FileDevPath;
|
|
|
|
|
|
|
|
|
|
|
|
DbgHeader("GetEfiBootDeviceFromNvram");
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG("GetEfiBootDeviceFromNvram:");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (gEfiBootDeviceData != NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG(" - [!] already parsed\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:49:24 +02:00
|
|
|
gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"efi-boot-next-data", &gEfiAppleBootGuid, NULL, &Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gEfiBootDeviceData != NULL) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("Got efi-boot-next-data size=%lld\n", Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (IsDevicePathValid(gEfiBootDeviceData, Size)) {
|
2020-03-25 19:32:44 +01:00
|
|
|
// DBG(" - efi-boot-next-data: %ls\n", FileDevicePathToStr (gEfiBootDeviceData));
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - device path for efi-boot-next-data is invalid\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
FreePool(gEfiBootDeviceData);
|
|
|
|
gEfiBootDeviceData = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gEfiBootDeviceData == NULL) {
|
2020-10-03 19:02:31 +02:00
|
|
|
void *Value;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINTN Size2=0;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
Status = GetVariable2 (L"aptiofixflag", &gEfiAppleBootGuid, &Value, &Size2);
|
|
|
|
if (EFI_ERROR(Status)) {
|
2020-04-28 12:49:24 +02:00
|
|
|
gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"efi-boot-device-data", &gEfiAppleBootGuid, NULL, &Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-04-28 12:49:24 +02:00
|
|
|
gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"specialbootdevice", &gEfiAppleBootGuid, NULL, &Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gEfiBootDeviceData != NULL) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("Got efi-boot-device-data size=%lld\n", Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (!IsDevicePathValid(gEfiBootDeviceData, Size)) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - device path for efi-boot-device-data is invalid\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
FreePool(gEfiBootDeviceData);
|
|
|
|
gEfiBootDeviceData = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gEfiBootDeviceData == NULL) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - [!] efi-boot-device-data not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG("\n");
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - efi-boot-device-data: %ls\n", FileDevicePathToXStringW(gEfiBootDeviceData).wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
gEfiBootVolume = gEfiBootDeviceData;
|
|
|
|
|
|
|
|
//
|
|
|
|
// if gEfiBootDeviceData starts with MemoryMapped node,
|
|
|
|
// then Startup Disk sets BootCampHD to Win disk dev path.
|
|
|
|
//
|
|
|
|
if (DevicePathType(gEfiBootDeviceData) == HARDWARE_DEVICE_PATH && DevicePathSubType (gEfiBootDeviceData) == HW_MEMMAP_DP) {
|
2020-04-28 12:49:24 +02:00
|
|
|
gBootCampHD = (__typeof__(gBootCampHD))GetNvramVariable(L"BootCampHD", &gEfiAppleBootGuid, NULL, &Size);
|
2019-09-03 11:58:42 +02:00
|
|
|
gEfiBootVolume = gBootCampHD;
|
|
|
|
|
|
|
|
if (gBootCampHD == NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG(" - [!] Error: BootCampHD not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsDevicePathValid(gBootCampHD, Size)) {
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG(" Error: BootCampHD device path is invalid\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
FreePool(gBootCampHD);
|
|
|
|
gEfiBootVolume = gBootCampHD = NULL;
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - BootCampHD: %ls\n", FileDevicePathToXStringW(gBootCampHD).wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// if gEfiBootVolume contains FilePathNode, then split them into gEfiBootVolume dev path and gEfiBootLoaderPath
|
|
|
|
//
|
|
|
|
gEfiBootLoaderPath = NULL;
|
2020-09-07 00:19:48 +02:00
|
|
|
FileDevPath = (FILEPATH_DEVICE_PATH *)Clover_FindDevicePathNodeWithType (gEfiBootVolume, MEDIA_DEVICE_PATH, MEDIA_FILEPATH_DP);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (FileDevPath != NULL) {
|
2020-04-27 17:16:43 +02:00
|
|
|
gEfiBootLoaderPath = (__typeof__(gEfiBootLoaderPath))AllocateCopyPool(StrSize(FileDevPath->PathName), FileDevPath->PathName);
|
2019-09-03 11:58:42 +02:00
|
|
|
// copy DevPath and write end of path node after in place of file path node
|
|
|
|
gEfiBootVolume = DuplicateDevicePath (gEfiBootVolume);
|
2020-09-07 00:19:48 +02:00
|
|
|
FileDevPath = (FILEPATH_DEVICE_PATH *)Clover_FindDevicePathNodeWithType (gEfiBootVolume, MEDIA_DEVICE_PATH, MEDIA_FILEPATH_DP);
|
2019-09-03 11:58:42 +02:00
|
|
|
SetDevicePathEndNode (FileDevPath);
|
|
|
|
// gEfiBootVolume now contains only Volume path
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - Volume: '%ls'\n", FileDevicePathToXStringW(gEfiBootVolume).wc_str());
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - LoaderPath: '%ls'\n", gEfiBootLoaderPath);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// if this is GPT disk, extract GUID
|
|
|
|
// gEfiBootDeviceGuid can be used as a flag for GPT disk then
|
|
|
|
//
|
|
|
|
Guid = FindGPTPartitionGuidInDevicePath (gEfiBootVolume);
|
|
|
|
if (Guid != NULL) {
|
2019-12-21 01:31:49 +01:00
|
|
|
gEfiBootDeviceGuid = (__typeof__(gEfiBootDeviceGuid))AllocatePool (sizeof(EFI_GUID));
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gEfiBootDeviceGuid != NULL) {
|
2020-05-01 18:26:28 +02:00
|
|
|
CopyMem(gEfiBootDeviceGuid, Guid, sizeof(EFI_GUID));
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - Guid = %s\n", strguid(gEfiBootDeviceGuid));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Loads and parses nvram.plist into gNvramDict. */
|
|
|
|
EFI_STATUS
|
2020-05-15 05:23:33 +02:00
|
|
|
LoadNvramPlist(
|
2019-09-03 11:58:42 +02:00
|
|
|
IN EFI_FILE *RootDir,
|
2020-02-17 21:41:09 +01:00
|
|
|
IN CONST CHAR16* NVRAMPlistPath
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
2021-02-21 16:30:06 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
CHAR8 *NvramPtr = NULL;
|
|
|
|
UINTN Size = 0;
|
|
|
|
|
|
|
|
DBG(" begin load gNvramDict=0x%llX\n", (uintptr_t)gNvramDict);
|
|
|
|
//
|
|
|
|
// skip loading if already loaded
|
|
|
|
//
|
|
|
|
if (gNvramDict != NULL) {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// load nvram.plist
|
|
|
|
//
|
|
|
|
Status = egLoadFile(RootDir, NVRAMPlistPath, (UINT8**)&NvramPtr, &Size);
|
|
|
|
if(EFI_ERROR(Status)) {
|
|
|
|
DBG(" not present\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return Status;
|
2021-02-21 16:30:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG(" loaded, size=%llu\n", Size);
|
|
|
|
|
|
|
|
//
|
|
|
|
// parse it into gNvramDict
|
|
|
|
//
|
|
|
|
Status = ParseXML((const CHAR8*)NvramPtr, &gNvramDict, Size);
|
|
|
|
if(Status != EFI_SUCCESS) {
|
|
|
|
DBG(" parsing error\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FreePool(NvramPtr);
|
|
|
|
// we will leave nvram.plist loaded and parsed for later processing
|
|
|
|
//FreeTag(gNvramDict);
|
|
|
|
|
|
|
|
return Status;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2021-02-21 16:30:06 +01:00
|
|
|
#define SEARCH_ONLY_EFI 1
|
2019-09-03 11:58:42 +02:00
|
|
|
/** Searches all volumes for the most recent nvram.plist and loads it into gNvramDict. */
|
|
|
|
EFI_STATUS
|
2020-05-15 05:23:33 +02:00
|
|
|
LoadLatestNvramPlist()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2021-02-21 16:30:06 +01:00
|
|
|
EFI_FILE* FileHandle = NULL;
|
2020-02-21 04:54:01 +01:00
|
|
|
EFI_FILE_INFO *FileInfo = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT64 LastModifTimeMs;
|
|
|
|
UINT64 ModifTimeMs;
|
2021-02-21 16:30:06 +01:00
|
|
|
#if !SEARCH_ONLY_EFI
|
|
|
|
REFIT_VOLUME *Volume;
|
2020-02-21 04:54:01 +01:00
|
|
|
REFIT_VOLUME *VolumeWithLatestNvramPlist = NULL;
|
2021-02-21 16:30:06 +01:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//there are debug messages not needed for users
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG("Searching volumes for latest nvram.plist ...");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// skip loading if already loaded
|
|
|
|
//
|
|
|
|
if (gNvramDict != NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" already loaded\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG("\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// find latest nvram.plist
|
|
|
|
//
|
|
|
|
|
|
|
|
LastModifTimeMs = 0;
|
2021-02-21 16:30:06 +01:00
|
|
|
#if SEARCH_ONLY_EFI
|
|
|
|
UINTN HandleCount = 0;
|
|
|
|
EFI_HANDLE *Handles = NULL;
|
|
|
|
EFI_FILE* RootDir;
|
|
|
|
EFI_FILE* NewestRootDir = NULL;
|
|
|
|
UINTN indexSuccess = 0;
|
|
|
|
|
|
|
|
Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiPartTypeSystemPartGuid, NULL, &HandleCount, &Handles);
|
|
|
|
if (!EFI_ERROR(Status) && HandleCount > 0) {
|
|
|
|
for (UINTN indexHandle = 0; indexHandle < HandleCount; indexHandle++) {
|
|
|
|
RootDir = EfiLibOpenRoot(Handles[indexHandle]);
|
|
|
|
Status = RootDir->Open(RootDir, &FileHandle, L"nvram.plist", EFI_FILE_MODE_READ, 0);
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
DBG(" - [%lld] no nvram.plist - skipping!\n", indexHandle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FileInfo = EfiLibFileInfo(FileHandle);
|
|
|
|
// DBG("got FileInfo=0x%X\n", FileInfo);
|
|
|
|
if (FileInfo == NULL) {
|
|
|
|
DBG(" - no nvram.plist file info - skipping!\n");
|
|
|
|
FileHandle->Close(FileHandle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DBG(" Modified = ");
|
|
|
|
ModifTimeMs = GetEfiTimeInMs (&(FileInfo->ModificationTime));
|
|
|
|
DBG("%d-%d-%d %d:%d:%d (%lld ms)\n",
|
|
|
|
FileInfo->ModificationTime.Year, FileInfo->ModificationTime.Month, FileInfo->ModificationTime.Day,
|
|
|
|
FileInfo->ModificationTime.Hour, FileInfo->ModificationTime.Minute, FileInfo->ModificationTime.Second,
|
|
|
|
ModifTimeMs);
|
|
|
|
FreePool(FileInfo);
|
|
|
|
FileHandle->Close(FileHandle);
|
|
|
|
|
|
|
|
// check if newer
|
|
|
|
if (LastModifTimeMs < ModifTimeMs) {
|
|
|
|
DBG(" - newer - will use this one\n");
|
|
|
|
NewestRootDir = RootDir;
|
|
|
|
LastModifTimeMs = ModifTimeMs;
|
|
|
|
indexSuccess = indexHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewestRootDir == NULL) {
|
|
|
|
Status = EFI_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// if we have nvram.plist - load it
|
|
|
|
//
|
|
|
|
DBG("Loading nvram.plist from EFI index %lld", indexSuccess);
|
|
|
|
Status = LoadNvramPlist(NewestRootDir, L"nvram.plist");
|
|
|
|
}
|
|
|
|
FreePool(Handles);
|
|
|
|
}
|
2020-02-21 04:54:01 +01:00
|
|
|
|
2021-02-21 16:30:06 +01:00
|
|
|
#else
|
2019-09-03 11:58:42 +02:00
|
|
|
// search all volumes
|
2020-02-27 15:34:29 +01:00
|
|
|
for (UINTN Index = 0; Index < Volumes.size(); ++Index) {
|
|
|
|
Volume = &Volumes[Index];
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (!Volume->RootDir) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Guid = FindGPTPartitionGuidInDevicePath (Volume->DevicePath);
|
|
|
|
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" %2d. Volume '%ls', GUID = %s", Index, Volume->VolName, strguid(Guid));
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Guid == NULL) {
|
|
|
|
// not a GUID partition
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - not GPT");
|
2019-09-03 11:58:42 +02:00
|
|
|
} */
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("Volume[%lld]\n", Index);
|
2019-09-03 11:58:42 +02:00
|
|
|
// check if nvram.plist exists
|
|
|
|
Status = Volume->RootDir->Open (Volume->RootDir, &FileHandle, L"nvram.plist", EFI_FILE_MODE_READ, 0);
|
|
|
|
if (EFI_ERROR(Status)) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - no nvram.plist - skipping!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" Status=%s\n", efiStrError(Status));
|
2021-02-02 10:02:21 +01:00
|
|
|
if (GlobalConfig.isFastBoot()) {
|
2019-09-03 11:58:42 +02:00
|
|
|
VolumeWithLatestNvramPlist = Volume;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get nvram.plist modification date
|
|
|
|
FileInfo = EfiLibFileInfo(FileHandle);
|
2020-03-25 19:32:44 +01:00
|
|
|
// DBG("got FileInfo=0x%X\n", FileInfo);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (FileInfo == NULL) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - no nvram.plist file info - skipping!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
FileHandle->Close(FileHandle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" Modified = ");
|
2020-02-21 04:54:01 +01:00
|
|
|
ModifTimeMs = GetEfiTimeInMs (&(FileInfo->ModificationTime));
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("%d-%d-%d %d:%d:%d (%lld ms)\n",
|
2019-09-03 11:58:42 +02:00
|
|
|
FileInfo->ModificationTime.Year, FileInfo->ModificationTime.Month, FileInfo->ModificationTime.Day,
|
|
|
|
FileInfo->ModificationTime.Hour, FileInfo->ModificationTime.Minute, FileInfo->ModificationTime.Second,
|
2021-02-19 21:07:24 +01:00
|
|
|
ModifTimeMs);
|
2020-04-23 11:08:10 +02:00
|
|
|
FreePool(FileInfo);
|
2019-09-03 11:58:42 +02:00
|
|
|
FileHandle->Close(FileHandle);
|
|
|
|
|
|
|
|
// check if newer
|
|
|
|
if (LastModifTimeMs < ModifTimeMs) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" - newer - will use this one\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
VolumeWithLatestNvramPlist = Volume;
|
|
|
|
LastModifTimeMs = ModifTimeMs;
|
|
|
|
}
|
2020-05-15 05:23:33 +02:00
|
|
|
// else {
|
|
|
|
// DBG(" - older - skipping!\n");
|
|
|
|
// }
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Status = EFI_NOT_FOUND;
|
|
|
|
|
|
|
|
//
|
|
|
|
// if we have nvram.plist - load it
|
|
|
|
//
|
|
|
|
if (VolumeWithLatestNvramPlist != NULL) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG("Loading nvram.plist from Vol '%ls' -", VolumeWithLatestNvramPlist->VolName.wc_str());
|
2020-05-15 05:23:33 +02:00
|
|
|
Status = LoadNvramPlist(VolumeWithLatestNvramPlist->RootDir, L"nvram.plist");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-15 05:23:33 +02:00
|
|
|
// else {
|
|
|
|
// DBG(" nvram.plist not found!\n");
|
|
|
|
// }
|
2021-02-21 16:30:06 +01:00
|
|
|
#endif
|
2020-08-25 17:35:19 +02:00
|
|
|
DBG("loaded Status=%s\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Puts all vars from nvram.plist to RT vars. Should be used in CloverEFI only
|
|
|
|
* or if some UEFI boot uses EmuRuntimeDxe driver.
|
|
|
|
*/
|
2020-10-03 19:02:31 +02:00
|
|
|
void
|
2019-09-03 11:58:42 +02:00
|
|
|
PutNvramPlistToRtVars ()
|
|
|
|
{
|
2020-05-15 05:23:33 +02:00
|
|
|
// EFI_STATUS Status;
|
2021-02-19 21:07:24 +01:00
|
|
|
// const TagStruct* ValTag;
|
2021-02-20 17:32:23 +01:00
|
|
|
INTN Size;
|
2021-02-19 21:07:24 +01:00
|
|
|
const VOID *Value;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (gNvramDict == NULL) {
|
2020-05-15 05:23:33 +02:00
|
|
|
/*Status = */LoadLatestNvramPlist();
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gNvramDict == NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG("PutNvramPlistToRtVars: nvram.plist not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
DbgHeader("PutNvramPlistToRtVars");
|
2021-02-19 21:07:24 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
// iterate over dict elements
|
2020-08-22 15:39:24 +02:00
|
|
|
size_t count = gNvramDict->dictKeyCount(); // ok
|
|
|
|
for (size_t tagIdx = 0 ; tagIdx < count ; tagIdx++ )
|
2020-08-18 21:30:14 +02:00
|
|
|
{
|
2020-08-25 17:35:19 +02:00
|
|
|
const TagKey* keyTag;
|
2020-08-22 15:39:24 +02:00
|
|
|
const TagStruct* valueTag;
|
2020-08-25 17:35:19 +02:00
|
|
|
if ( EFI_ERROR(gNvramDict->getKeyAndValueAtIndex(tagIdx, &keyTag, &valueTag)) ) { //If GetKeyValueAtIndex return success, key and value != NULL
|
2020-08-22 15:39:24 +02:00
|
|
|
MsgLog("MALFORMED PLIST nvram.plist. A key is expected at pos : %zu\n", tagIdx);
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
EFI_GUID *VendorGuid = &gEfiAppleBootGuid;
|
2020-08-22 15:39:24 +02:00
|
|
|
// process only valid <key> tags
|
|
|
|
if ( valueTag == NULL ) {
|
2021-02-19 21:07:24 +01:00
|
|
|
// DBG(" ERROR: ValTag is not NULL\n");
|
2020-08-22 15:39:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip OsxAptioFixDrv-RelocBase - appears and causes trouble
|
|
|
|
// in kernel and kext patcher when mixing UEFI and CloverEFI boot
|
|
|
|
if ( keyTag->keyStringValue() == "OsxAptioFixDrv-RelocBase"_XS8 ) {
|
|
|
|
DBG(" Skipping OsxAptioFixDrv-RelocBase\n");
|
|
|
|
continue;
|
|
|
|
} else if ( keyTag->keyStringValue() == "OsxAptioFixDrv-ErrorExitingBootServices"_XS8 ) {
|
|
|
|
DBG(" Skipping OsxAptioFixDrv-ErrorExitingBootServices\n");
|
|
|
|
continue;
|
|
|
|
} else if ( keyTag->keyStringValue() == "EmuVariableUefiPresent"_XS8 ) {
|
|
|
|
DBG(" Skipping EmuVariableUefiPresent\n");
|
|
|
|
continue;
|
|
|
|
} else if ( keyTag->keyStringValue() == "aapl,panic-info"_XS8 ) {
|
|
|
|
DBG(" Skipping aapl,panic-info\n");
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
|
|
|
|
if (keyTag->keyStringValue() == "Boot0082"_XS8 ||
|
|
|
|
keyTag->keyStringValue() == "BootNext"_XS8 ) {
|
2020-08-22 15:39:24 +02:00
|
|
|
VendorGuid = &gEfiGlobalVariableGuid;
|
|
|
|
// it may happen only in this case
|
2021-02-02 10:02:21 +01:00
|
|
|
gSettings.Boot.HibernationFixup = TRUE;
|
2020-08-22 15:39:24 +02:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
// AsciiStrToUnicodeStrS(Tag.stringValue(), KeyBuf, 128);
|
|
|
|
XStringW KeyBuf = keyTag->keyStringValue();
|
|
|
|
// if (!gSettings.Boot.DebugLog) {
|
|
|
|
DBG(" Adding Key: %ls: ", KeyBuf.wc_str());
|
|
|
|
// }
|
|
|
|
// process value tag
|
2020-08-22 15:39:24 +02:00
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
if (valueTag->isString()) {
|
|
|
|
// <string> element
|
|
|
|
Value = (void*)valueTag->getString()->stringValue().c_str();
|
|
|
|
Size = valueTag->getString()->stringValue().length();
|
2021-02-20 19:33:09 +01:00
|
|
|
// if (!gSettings.Boot.DebugLog) {
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG("String: Size = %llu, Val = '%s'\n", Size, valueTag->getString()->stringValue().c_str());
|
2021-02-20 19:33:09 +01:00
|
|
|
// }
|
2021-02-19 21:07:24 +01:00
|
|
|
} else if (valueTag->isData()) {
|
|
|
|
|
|
|
|
// <data> element
|
|
|
|
Size = valueTag->getData()->dataLenValue();
|
|
|
|
Value = valueTag->getData()->dataValue();
|
2021-02-20 19:33:09 +01:00
|
|
|
// if (gSettings.Boot.DebugLog) {
|
2021-02-21 07:57:43 +01:00
|
|
|
DBG("Size = %llu, Data: ", Size);
|
|
|
|
for (INTN i = 0; i < Size; i++) {
|
|
|
|
DBG("%02hhX ", *(((UINT8*)Value) + i));
|
|
|
|
}
|
2021-02-20 19:33:09 +01:00
|
|
|
// }
|
|
|
|
// if (!gSettings.Boot.DebugLog) {
|
|
|
|
DBG("\n");
|
|
|
|
//
|
2021-02-19 21:07:24 +01:00
|
|
|
} else {
|
|
|
|
DBG("ERROR: Unsupported tag type: %s\n", valueTag->getTypeAsXString8().c_str());
|
|
|
|
continue;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2021-02-19 21:07:24 +01:00
|
|
|
|
2020-08-22 15:39:24 +02:00
|
|
|
if (Size == 0 || !Value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:07:24 +01:00
|
|
|
// set RT var: all vars visible in nvram.plist are gEfiAppleBootGuid
|
2020-08-22 15:39:24 +02:00
|
|
|
SetNvramVariable (
|
|
|
|
KeyBuf.wc_str(),
|
2019-09-03 11:58:42 +02:00
|
|
|
VendorGuid,
|
|
|
|
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
|
|
|
|
Size,
|
|
|
|
Value
|
2020-08-22 15:39:24 +02:00
|
|
|
);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Performs detailed search for Startup Disk or last Clover boot volume
|
|
|
|
* by looking for gEfiAppleBootGuid:efi-boot-device-data and BootCampHD RT vars.
|
|
|
|
* Returns MainMenu entry index or -1 if not found.
|
|
|
|
*/
|
|
|
|
INTN
|
|
|
|
FindStartupDiskVolume (
|
|
|
|
REFIT_MENU_SCREEN *MainMenu
|
|
|
|
)
|
|
|
|
{
|
|
|
|
INTN Index;
|
2020-02-28 21:28:33 +01:00
|
|
|
// LEGACY_ENTRY *LegacyEntry;
|
|
|
|
// LOADER_ENTRY *LoaderEntry;
|
2020-08-09 17:55:30 +02:00
|
|
|
// REFIT_VOLUME *Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
REFIT_VOLUME *DiskVolume;
|
|
|
|
BOOLEAN IsPartitionVolume;
|
2020-04-27 11:50:49 +02:00
|
|
|
XStringW LoaderPath;
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW EfiBootVolumeStr;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// search RT vars for efi-boot-device-data
|
|
|
|
// and try to find that volume
|
|
|
|
//
|
|
|
|
GetEfiBootDeviceFromNvram ();
|
|
|
|
if (gEfiBootVolume == NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG(" - [!] EfiBootVolume not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DbgHeader("FindStartupDiskVolume");
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check if gEfiBootVolume is disk or partition volume
|
|
|
|
//
|
2020-08-09 17:55:30 +02:00
|
|
|
EfiBootVolumeStr = FileDevicePathToXStringW(gEfiBootVolume);
|
2020-09-07 00:19:48 +02:00
|
|
|
IsPartitionVolume = NULL != Clover_FindDevicePathNodeWithType (gEfiBootVolume, MEDIA_DEVICE_PATH, 0);
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - Volume: %ls = %ls\n", IsPartitionVolume ? L"partition" : L"disk", EfiBootVolumeStr.wc_str());
|
2020-06-04 12:53:53 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// 1. gEfiBootVolume + gEfiBootLoaderPath
|
|
|
|
// PciRoot(0x0)/.../Sata(...)/HD(...)/\EFI\BOOT\XXX.EFI - set by Clover
|
|
|
|
//
|
|
|
|
if (gEfiBootLoaderPath != NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching for that partition and loader '%ls'\n", gEfiBootLoaderPath);
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
|
|
|
if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
LOADER_ENTRY& LoaderEntry = *MainMenu->Entries[Index].getLOADER_ENTRY();
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LoaderEntry.Volume;
|
2020-02-28 21:28:33 +01:00
|
|
|
LoaderPath = LoaderEntry.LoaderPath;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume != NULL && BootVolumeDevicePathEqual(gEfiBootVolume, Volume->DevicePath)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" checking '%ls'\n", DevicePathToXStringW(Volume->DevicePath).wc_str());
|
2020-06-04 12:53:53 +02:00
|
|
|
DBG(" '%ls'\n", LoaderPath.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
// case insensitive cmp
|
2021-04-11 07:18:52 +02:00
|
|
|
if ( LoaderPath.isEqualIC(gEfiBootLoaderPath) ) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// that's the one
|
2021-02-28 20:42:57 +01:00
|
|
|
DBG(" - found entry %lld. '%ls', Volume '%ls', '%ls'\n", Index, LoaderEntry.Title.s(), Volume->VolName.wc_str(), LoaderPath.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// search again, but compare only Media dev path nodes
|
|
|
|
// (in case of some dev path differences we do not cover)
|
|
|
|
//
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching again, but comparing Media dev path nodes\n");
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
|
|
|
if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
LOADER_ENTRY& LoaderEntry = *MainMenu->Entries[Index].getLOADER_ENTRY();
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LoaderEntry.Volume;
|
2020-06-04 12:53:53 +02:00
|
|
|
EFI_DEVICE_PATH *DevicePath = LoaderEntry.DevicePath;
|
2020-09-07 00:19:48 +02:00
|
|
|
EFI_DEVICE_PATH *MediaPath = Clover_FindDevicePathNodeWithType(DevicePath, MEDIA_DEVICE_PATH, MEDIA_VENDOR_DP);
|
2020-06-04 19:53:23 +02:00
|
|
|
if (MediaPath) {
|
|
|
|
EFI_GUID *MediaPathGuid = (EFI_GUID *)&((VENDOR_DEVICE_PATH_WITH_DATA*)MediaPath)->VendorDefinedData;
|
2020-09-23 15:47:47 +02:00
|
|
|
XStringW MediaPathGuidStr = GuidLEToXStringW(*MediaPathGuid);
|
2021-02-19 21:07:24 +01:00
|
|
|
DBG(" checking '%ls'\n", MediaPathGuidStr.wc_str());
|
2020-06-04 19:53:23 +02:00
|
|
|
if (StrStr(gEfiBootLoaderPath, MediaPathGuidStr.wc_str())) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found entry %lld. '%ls', Volume '%ls', '%ls'\n", Index, LoaderEntry.Title.s(), Volume->VolName.wc_str(), LoaderPath.wc_str());
|
2020-06-04 19:53:23 +02:00
|
|
|
return Index;
|
|
|
|
}
|
2020-06-04 12:53:53 +02:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-06-05 11:43:12 +02:00
|
|
|
|
|
|
|
//Slice - why all further? For legacy boot which is not working???
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// 2. gEfiBootVolume - partition volume
|
|
|
|
// PciRoot(0x0)/.../Sata(...)/HD(...) - set by Clover or macOS
|
|
|
|
//
|
|
|
|
if (IsPartitionVolume) {
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching for that partition\n");
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = NULL;
|
2020-02-28 21:28:33 +01:00
|
|
|
if (MainMenu->Entries[Index].getLEGACY_ENTRY()) {
|
|
|
|
Volume = MainMenu->Entries[Index].getLEGACY_ENTRY()->Volume;
|
|
|
|
} else if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
Volume = MainMenu->Entries[Index].getLOADER_ENTRY()->Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
if (Volume != NULL && BootVolumeDevicePathEqual (gEfiBootVolume, Volume->DevicePath)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found entry %lld. '%ls', Volume '%ls'\n", Index, MainMenu->Entries[Index].Title.s(), Volume->VolName.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// search again, but compare only Media dev path nodes
|
|
|
|
//
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching again, but comparing Media dev path nodes\n");
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = NULL;
|
2020-02-28 21:28:33 +01:00
|
|
|
if (MainMenu->Entries[Index].getLEGACY_ENTRY()) {
|
|
|
|
Volume = MainMenu->Entries[Index].getLEGACY_ENTRY()->Volume;
|
|
|
|
} else if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
Volume = MainMenu->Entries[Index].getLOADER_ENTRY()->Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
if (Volume != NULL && BootVolumeMediaDevicePathNodesEqual (gEfiBootVolume, Volume->DevicePath)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found entry %lld. '%ls', Volume '%ls'\n", Index, MainMenu->Entries[Index].Title.s(), Volume->VolName.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 3. gEfiBootVolume - disk volume
|
|
|
|
// PciRoot(0x0)/.../Sata(...) - set by macOS for Win boot
|
|
|
|
//
|
|
|
|
// 3.1 First find disk volume in Volumes[]
|
|
|
|
//
|
|
|
|
DiskVolume = NULL;
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching for that disk\n");
|
2020-03-02 13:00:24 +01:00
|
|
|
for (Index = 0; Index < (INTN)Volumes.size(); ++Index) {
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = &Volumes[Index];
|
2019-09-03 11:58:42 +02:00
|
|
|
if (BootVolumeDevicePathEqual (gEfiBootVolume, Volume->DevicePath)) {
|
|
|
|
// that's the one
|
|
|
|
DiskVolume = Volume;
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found disk as volume %lld. '%ls'\n", Index, Volume->VolName.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DiskVolume == NULL) {
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 3.2 DiskVolume
|
|
|
|
// search for first entry with win loader or win partition on that disk
|
|
|
|
//
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - searching for first entry with win loader or win partition on that disk\n");
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
|
|
|
if (MainMenu->Entries[Index].getLEGACY_ENTRY()) {
|
|
|
|
LEGACY_ENTRY& LegacyEntry = (LEGACY_ENTRY&)MainMenu->Entries[Index];
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LegacyEntry.Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume != NULL && Volume->WholeDiskBlockIO == DiskVolume->BlockIO) {
|
|
|
|
// check for Win
|
2020-04-23 18:05:21 +02:00
|
|
|
//DBG(" checking legacy entry %d. %ls\n", Index, LegacyEntry.Title);
|
|
|
|
//DBG(" %ls\n", DevicePathToStr (Volume->DevicePath));
|
|
|
|
//DBG(" OSType = %d\n", Volume->OSType);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume->LegacyOS->Type == OSTYPE_WIN) {
|
|
|
|
// that's the one - legacy win partition
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found legacy entry %lld. '%ls', Volume '%ls'\n", Index, LegacyEntry.Title.s(), Volume->VolName.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 21:28:33 +01:00
|
|
|
} else if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
LOADER_ENTRY& LoaderEntry = *MainMenu->Entries[Index].getLOADER_ENTRY();
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LoaderEntry.Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume != NULL && Volume->WholeDiskBlockIO == DiskVolume->BlockIO) {
|
|
|
|
// check for Win
|
2020-04-23 18:05:21 +02:00
|
|
|
//DBG(" checking loader entry %d. %ls\n", Index, LoaderEntry.Title);
|
|
|
|
//DBG(" %ls\n", DevicePathToStr (Volume->DevicePath));
|
|
|
|
//DBG(" LoaderPath = %ls\n", LoaderEntry.LoaderPath);
|
|
|
|
//DBG(" LoaderType = %d\n", LoaderEntry.LoaderType);
|
2020-02-28 21:28:33 +01:00
|
|
|
if (LoaderEntry.LoaderType == OSTYPE_WINEFI) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// that's the one - win loader entry
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found loader entry %lld. '%ls', Volume '%ls', '%ls'\n", Index, LoaderEntry.Title.s(), Volume->VolName.wc_str(), LoaderEntry.LoaderPath.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// 3.3 DiskVolume, but no Win entry
|
|
|
|
// PciRoot(0x0)/.../Sata(...)
|
|
|
|
// just find first menu entry on that disk?
|
|
|
|
//
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - searching for any entry from disk '%ls'\n", DiskVolume->VolName.wc_str());
|
2020-02-28 21:28:33 +01:00
|
|
|
for (Index = 0; ((Index < (INTN)MainMenu->Entries.size()) && (MainMenu->Entries[Index].Row == 0)); ++Index) {
|
|
|
|
if (MainMenu->Entries[Index].getLEGACY_ENTRY()) {
|
|
|
|
LEGACY_ENTRY& LegacyEntry = (LEGACY_ENTRY&)MainMenu->Entries[Index];
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LegacyEntry.Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume != NULL && Volume->WholeDiskBlockIO == DiskVolume->BlockIO) {
|
|
|
|
// that's the one
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found legacy entry %lld. '%ls', Volume '%ls'\n", Index, LegacyEntry.Title.s(), Volume->VolName.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
2020-02-28 21:28:33 +01:00
|
|
|
} else if (MainMenu->Entries[Index].getLOADER_ENTRY()) {
|
|
|
|
LOADER_ENTRY& LoaderEntry = *MainMenu->Entries[Index].getLOADER_ENTRY();
|
2020-08-09 17:55:30 +02:00
|
|
|
REFIT_VOLUME* Volume = LoaderEntry.Volume;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Volume != NULL && Volume->WholeDiskBlockIO == DiskVolume->BlockIO) {
|
|
|
|
// that's the one
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" - found loader entry %lld. '%ls', Volume '%ls', '%ls'\n", Index, LoaderEntry.Title.s(), Volume->VolName.wc_str(), LoaderEntry.LoaderPath.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" - [!] not found\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Sets efi-boot-device-data RT var to currently selected Volume and LoadePath. */
|
|
|
|
EFI_STATUS SetStartupDiskVolume (
|
|
|
|
IN REFIT_VOLUME *Volume,
|
2020-04-27 11:50:49 +02:00
|
|
|
IN CONST XStringW& LoaderPath
|
2019-09-03 11:58:42 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_DEVICE_PATH_PROTOCOL *DevPath;
|
|
|
|
EFI_DEVICE_PATH_PROTOCOL *FileDevPath;
|
|
|
|
EFI_GUID *Guid;
|
2020-03-29 15:44:08 +02:00
|
|
|
// CHAR8 *EfiBootDevice;
|
|
|
|
// CONST CHAR8 *EfiBootDeviceTmpl;
|
|
|
|
// UINTN Size;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 Attributes;
|
|
|
|
|
|
|
|
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG("SetStartupDiskVolume:\n");
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" * Volume: '%ls'\n", Volume->VolName.wc_str());
|
2020-04-27 11:50:49 +02:00
|
|
|
DBG(" * LoaderPath: '%ls'\n", LoaderPath.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// construct dev path for Volume/LoaderPath
|
|
|
|
//
|
|
|
|
DevPath = Volume->DevicePath;
|
2020-04-27 11:50:49 +02:00
|
|
|
if (LoaderPath.notEmpty()) {
|
2020-04-27 14:13:09 +02:00
|
|
|
FileDevPath = FileDevicePath (NULL, LoaderPath);
|
2019-09-03 11:58:42 +02:00
|
|
|
DevPath = AppendDevicePathNode (DevPath, FileDevPath);
|
|
|
|
}
|
2020-08-09 17:55:30 +02:00
|
|
|
DBG(" * DevPath: %ls\n", Volume->VolName.wc_str()/*, FileDevicePathToStr (DevPath)*/);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
Guid = FindGPTPartitionGuidInDevicePath (Volume->DevicePath);
|
2020-04-23 18:05:21 +02:00
|
|
|
DBG(" * GUID = %s\n", strguid(Guid));
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// let's save it without EFI_VARIABLE_NON_VOLATILE in CloverEFI like other vars so far
|
|
|
|
//
|
|
|
|
if (!gFirmwareClover && !gDriversFlags.EmuVariableLoaded) {
|
|
|
|
Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
|
|
|
|
} else {
|
|
|
|
Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// set efi-boot-device-data to volume dev path
|
|
|
|
//
|
|
|
|
Status = SetNvramVariable (L"efi-boot-device-data", &gEfiAppleBootGuid, Attributes, GetDevicePathSize(DevPath), DevPath);
|
|
|
|
|
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// set efi-boot-device to XML string
|
|
|
|
// (probably not needed at all)
|
|
|
|
//
|
|
|
|
if (Guid != NULL) {
|
2020-04-30 08:03:56 +02:00
|
|
|
XString8 EfiBootDevice;
|
2020-08-11 08:00:19 +02:00
|
|
|
EfiBootDevice.S8Printf(
|
2020-03-29 15:44:08 +02:00
|
|
|
"<array><dict>"
|
2019-09-03 11:58:42 +02:00
|
|
|
"<key>IOMatch</key>"
|
|
|
|
"<dict>"
|
|
|
|
"<key>IOProviderClass</key><string>IOMedia</string>"
|
|
|
|
"<key>IOPropertyMatch</key>"
|
2020-03-25 19:32:44 +01:00
|
|
|
"<dict><key>UUID</key><string>%s</string></dict>"
|
2019-09-03 11:58:42 +02:00
|
|
|
"</dict>"
|
2020-03-29 15:44:08 +02:00
|
|
|
"</dict></array>", strguid(Guid));
|
|
|
|
DBG (" * efi-boot-device: %s\n", EfiBootDevice.c_str());
|
2020-08-15 15:47:56 +02:00
|
|
|
Status = SetNvramXString8(L"efi-boot-device", &gEfiAppleBootGuid, Attributes, EfiBootDevice);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Deletes Startup disk vars: efi-boot-device, efi-boot-device-data, BootCampHD. */
|
2020-10-03 19:02:31 +02:00
|
|
|
void
|
2019-09-03 11:58:42 +02:00
|
|
|
RemoveStartupDiskVolume ()
|
|
|
|
{
|
|
|
|
// EFI_STATUS Status;
|
|
|
|
|
2020-04-23 18:05:21 +02:00
|
|
|
// DBG("RemoveStartupDiskVolume:\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/*Status =*/ DeleteNvramVariable (L"efi-boot-device", &gEfiAppleBootGuid);
|
2020-08-25 17:35:19 +02:00
|
|
|
// DBG(" * efi-boot-device = %s\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/*Status =*/ DeleteNvramVariable (L"efi-boot-device-data", &gEfiAppleBootGuid);
|
2020-08-25 17:35:19 +02:00
|
|
|
// DBG(" * efi-boot-device-data = %s\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/*Status =*/ DeleteNvramVariable (L"BootCampHD", &gEfiAppleBootGuid);
|
2020-08-25 17:35:19 +02:00
|
|
|
// DBG(" * BootCampHD = %s\n", efiStrError(Status));
|
|
|
|
// DBG("Removed efi-boot-device-data variable: %s\n", efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-04-16 09:15:26 +02:00
|
|
|
|
|
|
|
|
2020-10-03 19:02:31 +02:00
|
|
|
void ResetNvram ()
|
2020-04-16 09:15:26 +02:00
|
|
|
{
|
|
|
|
if (gFirmwareClover || gDriversFlags.EmuVariableLoaded) {
|
|
|
|
if (gEmuVariableControl != NULL) {
|
|
|
|
gEmuVariableControl->InstallEmulation(gEmuVariableControl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetNativeNvram ();
|
|
|
|
|
|
|
|
if (gFirmwareClover || gDriversFlags.EmuVariableLoaded) {
|
|
|
|
if (gEmuVariableControl != NULL) {
|
|
|
|
gEmuVariableControl->UninstallEmulation(gEmuVariableControl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt warm reboot
|
|
|
|
// gRT->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, NULL);
|
|
|
|
// Warm reboot may not be supported attempt cold reboot
|
|
|
|
// gRT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
|
|
|
|
// Terminate the screen and just exit
|
|
|
|
// TerminateScreen();
|
|
|
|
}
|