void efi_init() { // Determine if running UEFI. If no -> then exit FIRMWARE_TYPE fwtype; DWORD dwRet; GetFirmwareType(&fwtype); if (fwtype != FirmwareTypeUefi) { printf("You are not running UEFI!\n"); exit(-1); } ObtainPrivileges(SE_SYSTEM_ENVIRONMENT_NAME); // Perform check if UEFI variables are present. // If not, we get ERROR_INVALID_FUNCTION on this. // If yes, we get ERROR_NOACCESS because we are accessing not-existing variable. // Any other errors are unexpected. dwRet = 0; if (GetFirmwareEnvironmentVariable(___T(""), ___T("{00000000-0000-0000-0000-000000000000}"), NULL, 0) == 0) { if (GetLastError() == ERROR_INVALID_FUNCTION) { printf("Cannot access UEFI (are you running BIOS?)\n"); exit(-1); } else if (GetLastError() == ERROR_NOACCESS) { // Expected } else { PrintError(GetLastError()); exit(GetLastError()); } } }
int ast_read_efivar (char *var, size_t bufSiz, char *guid, char *name) { DWORD nBytesStored = 0; if (ast_privilege_obtain (SE_SYSTEM_ENVIRONMENT_NAME) == EXIT_SUCCESS) { // "If the function succeeds, the return value is the number of bytes stored in the pBuffer buffer." // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms724325(v=vs.85).aspx nBytesStored = GetFirmwareEnvironmentVariable (name, guid, var, bufSiz); if (nBytesStored != 0) { if (nBytesStored <= bufSiz) { return EXIT_SUCCESS; } else { // Memory overflows??? fprintf (stderr, " ** Warning: nBytesStored > bufSiz.\n"); return EXIT_SUCCESS; // XXX } } else { // GetFirmwareEnvironmentVariable failed fprintf (stderr, " ** GetFirmwareEnvironmentVariable failed with error %lu.\n", GetLastError ()); return EXIT_FAILURE; } } else { // ast_privilege_obtain failed fprintf (stderr, " ** Cannot obtain sufficient privilege. Failed to read efivar.\n"); return EXIT_FAILURE; } }
static int _ast_get_firmware_type_on_win8_lesser (enum AST_FIRMWARE_TYPE *T) { static const char *EFIDummyGUID = "{000000000-0000-0000-0000-000000000000}"; static const char *EFIDummyName = ""; void *buffer = malloc (1); DWORD ret = 0; if (ast_privilege_obtain (SE_SYSTEM_ENVIRONMENT_NAME) == EXIT_SUCCESS) { ret = GetFirmwareEnvironmentVariable (EFIDummyName, EFIDummyGUID, buffer, 1); if (ret == ERROR_INVALID_FUNCTION) { // Not a UEFI machine. XXX: Currently returns BIOS. *T = AST_FIRMWARE_TYPE_BIOS; return EXIT_SUCCESS; } else { // XXX: Is a UEFI machine, whatever. *T = AST_FIRMWARE_TYPE_UEFI; return EXIT_SUCCESS; } } else { fprintf (stderr, " ** failed to obtain appropriate system privilege.\n"); return EXIT_FAILURE; } return EXIT_FAILURE; }
/* * AboutDialogQuerySecureBootState * * Purpose: * * Query Firmware type and SecureBoot state if firmware is EFI. * */ BOOL AboutDialogQuerySecureBootState( _In_ PBOOLEAN pbSecureBoot ) { BOOL cond = FALSE, bResult = FALSE; BOOLEAN bSecureBoot = FALSE; HKEY hKey; DWORD dwState, dwSize, returnLength; LRESULT lRet; //first attempt, query firmware environment variable, will not work if not fulladmin do { if (!supEnablePrivilege(SE_SYSTEM_ENVIRONMENT_PRIVILEGE, TRUE)) break; bSecureBoot = FALSE; returnLength = GetFirmwareEnvironmentVariable(L"SecureBoot", L"{8be4df61-93ca-11d2-aa0d-00e098032b8c}", &bSecureBoot, sizeof(BOOLEAN)); supEnablePrivilege(SE_SYSTEM_ENVIRONMENT_PRIVILEGE, FALSE); if (returnLength != 0) { if (pbSecureBoot) { *pbSecureBoot = bSecureBoot; } bResult = TRUE; } } while (cond); if (bResult) { return bResult; } //second attempt, query state from registry do { lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, T_SECUREBOOTSTATEKEY, 0, KEY_QUERY_VALUE, &hKey); if (lRet != ERROR_SUCCESS) break; dwState = 0; dwSize = sizeof(DWORD); lRet = RegQueryValueExW(hKey, T_SECUREBOOTSTATEVALUE, NULL, NULL, (LPBYTE)&dwState, &dwSize); if (lRet != ERROR_SUCCESS) break; if (pbSecureBoot) { *pbSecureBoot = (dwState == 1); } bResult = TRUE; RegCloseKey(hKey); } while (cond); return bResult; }
BDS_LOAD_OPTION* GetBootEntry(LPCWSTR BootEntry, int id) { int len; UINT8 buffer[BUFFER_SIZE]; //Block of bytes BDS_LOAD_OPTION* pBootEntry; len = GetFirmwareEnvironmentVariable(BootEntry, EFI_GLOBAL_VARIABLE, buffer, sizeof(char) * BUFFER_SIZE); //EFI Variables names are case sensitive if (!len) return NULL; //Alocate New BootDevice pBootEntry = (BDS_LOAD_OPTION*)calloc(1, sizeof(BDS_LOAD_OPTION)); if (!pBootEntry) return NULL; //Update BootOption ID pBootEntry->LoadOptionIndex = id; //Get Attributes pBootEntry->Attributes = GET_ATTRIBUTES(buffer); //Get FilePathListLength pBootEntry->FilePathListLength = GET_FILELISTLENGTH(buffer); if (pBootEntry->FilePathListLength > len) return NULL; //Get Description int descSize = wstrlen((WCHAR*)DESCRIPTION_OFFSET(buffer)); if (descSize == 0) return NULL; pBootEntry->Description = ALLOCATE_WCHAR_STRING(descSize); memcpy(pBootEntry->Description, DESCRIPTION_OFFSET(buffer), descSize); DBG_INFO("%s,%d : %s\n",BootEntry, id, pBootEntry->Description); //Get FilePathList pBootEntry->FilePathList = (EFI_DEVICE_PATH_PROTOCOL*)calloc(1, pBootEntry->FilePathListLength); if (!pBootEntry->FilePathList) return NULL; memcpy(pBootEntry->FilePathList, (EFI_DEVICE_PATH_PROTOCOL*)(DESCRIPTION_OFFSET(buffer) + descSize), pBootEntry->FilePathListLength); //UINT8 OptionalData = (UINT8)((UINT8*)pBootEntry - ((UINT8*)pBootEntry->FilePathList) + pBootEntry->FilePathListLength); //Read FilePathList //GetFilePathList(pBootEntry, buffer, descSize); return pBootEntry; }
int read_efi_variable(const char* name, uint16_t** data) { // Windows does not allocate memory for it so we have to do it uint16_t* res = (uint16_t *)malloc(sizeof(uint16_t)* 1024); int data_size = GetFirmwareEnvironmentVariable( ___T(name), ___T(EFI_GLOBAL_VARIABLE), res, sizeof(uint16_t)* 1024); if (data_size) { *data = res; return data_size / 2; } else { PrintError(GetLastError()); exit(GetLastError()); } }