Skip to content

Instantly share code, notes, and snippets.

@xCuri0
Created August 14, 2022 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xCuri0/23307bb0cf6f02a8be8df3a3276139cb to your computer and use it in GitHub Desktop.
Save xCuri0/23307bb0cf6f02a8be8df3a3276139cb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <Windows.h>
#include <string>
#define VARIABLE_ATTRIBUTE_NON_VOLATILE 0x00000001
#define VARIABLE_ATTRIBUTE_BOOTSERVICE_ACCESS 0x00000002
#define VARIABLE_ATTRIBUTE_RUNTIME_ACCESS 0x00000004
BOOLEAN RaisePriviledge()
{
DWORD len;
HANDLE hTok;
TOKEN_PRIVILEGES tokp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hTok)) {
return FALSE;
}
LookupPrivilegeValue(NULL, SE_SYSTEM_ENVIRONMENT_NAME, &tokp.Privileges[0].Luid);
tokp.PrivilegeCount = 1;
tokp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hTok, FALSE, &tokp, 0, NULL, &len);
if (GetLastError() != ERROR_SUCCESS) {
return FALSE;
}
return TRUE;
}
int main()
{
std::string i;
UINT8 rBarState;
DWORD rSize;
DWORD size = sizeof(UINT8);
DWORD dwAttributes = VARIABLE_ATTRIBUTE_NON_VOLATILE | VARIABLE_ATTRIBUTE_BOOTSERVICE_ACCESS | VARIABLE_ATTRIBUTE_RUNTIME_ACCESS;
const TCHAR name[] = TEXT("ReBarState");
const TCHAR guid[] = TEXT("{A3C5B77A-C88F-4A93-BF1C-4A92A32C65CE}");
if (!RaisePriviledge()) {
std::cout << "Failed to obtain SE_SYSTEM_ENVIRONMENT_NAME. Try running as admin\n";
}
else {
std::cout << "Obtained SE_SYSTEM_ENVIRONMENT_NAME\n";
}
rSize = GetFirmwareEnvironmentVariable(name, guid, &rBarState, 1);
if (rSize) {
std::cout << "Current ReBarState " << +rBarState << " len " << rSize << "\n";
}
std::cout << "\nVerify that 4G Decoding is enabled otherwise system will not POST with GPU. Also make sure you have suffecient MMIOH space for the GPU BAR size (test with Linux if you have AMD)\n";
std::cout << "\nEnter ReBarState Value\n 0: disabled \n >0: maximum BAR size set to x^2 \n 32: Unlmited BAR size\n\n";
std::getline(std::cin, i);
rBarState = std::stoi(i);
std::cout << "Recived value of " << +rBarState << ". Writing to ReBarState\n\n";
if (SetFirmwareEnvironmentVariableEx(name, guid, &rBarState, size, dwAttributes) == TRUE) {
std::cout << "Successfully wrote ReBarState UEFI variable\n";
}
else {
std::cout << "Failed to write ReBarState UEFI variable\n";
}
std::cout << "You can close the app now\n";
std::cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment