Created
July 30, 2018 03:36
-
-
Save Orochimarufan/3c124683408e26452e50f4c215144e83 to your computer and use it in GitHub Desktop.
Set rEFInd next boot default from Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2018 Taeyeon Mori | |
// CC0 | |
// | |
#include "stdafx.h" | |
#include <Windows.h> | |
#define REFIND_GUID L"{36d08fa7-cf0b-42f5-8f14-68df73ed3740}" | |
#define PREVIOUS_BOOT L"PreviousBoot" | |
void wperror(const char *name) | |
{ | |
TCHAR buf[1024]; | |
DWORD err = GetLastError(); | |
DWORD len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, 0, buf, 1024, nullptr); | |
buf[len] = 0; | |
printf("%s error (0x%lx): %ls\r\n", name, err, buf); | |
} | |
// See https://docs.microsoft.com/en-us/windows/desktop/SecAuthZ/enabling-and-disabling-privileges-in-c-- | |
BOOL SetPrivilege( | |
HANDLE hToken, // access token handle | |
LPCTSTR lpszPrivilege, // name of privilege to enable/disable | |
BOOL bEnablePrivilege // to enable or disable privilege | |
) | |
{ | |
TOKEN_PRIVILEGES tp; | |
LUID luid; | |
if (!LookupPrivilegeValue( | |
NULL, // lookup privilege on local system | |
lpszPrivilege, // privilege to lookup | |
&luid)) // receives LUID of privilege | |
{ | |
wperror("LookupPrivilegeValue"); | |
return FALSE; | |
} | |
tp.PrivilegeCount = 1; | |
tp.Privileges[0].Luid = luid; | |
if (bEnablePrivilege) | |
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
else | |
tp.Privileges[0].Attributes = 0; | |
// Enable the privilege or disable all privileges. | |
if (!AdjustTokenPrivileges( | |
hToken, | |
FALSE, | |
&tp, | |
sizeof(TOKEN_PRIVILEGES), | |
(PTOKEN_PRIVILEGES)NULL, | |
(PDWORD)NULL)) | |
{ | |
wperror("AdjustTokenPrivileges"); | |
return FALSE; | |
} | |
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) | |
{ | |
printf("The token does not have the specified privilege. \n"); | |
return FALSE; | |
} | |
return TRUE; | |
} | |
void synopsis() | |
{ | |
puts("Synopsis: rEFIndNextBoot.exe [entry]"); | |
puts(""); | |
puts("Set or get (when entry not given) the rEFInd PreviousBoot nvram variable"); | |
} | |
int wmain(int argc, wchar_t **argv, wchar_t **envp) | |
{ | |
if (argc > 2) | |
{ | |
puts("Error: too many arguments."); | |
puts(""); | |
synopsis(); | |
return 5; | |
} | |
// Take privileges | |
HANDLE at; | |
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &at)) | |
{ | |
wperror("OpenProcessToken"); | |
return 40; | |
} | |
if (!SetPrivilege(at, SE_SYSTEM_ENVIRONMENT_NAME, true)) | |
{ | |
return 41; | |
} | |
// Set new value | |
if (argc == 2) | |
{ | |
if (!SetFirmwareEnvironmentVariable(PREVIOUS_BOOT, REFIND_GUID, argv[1], wcslen(argv[1]) * sizeof(wchar_t))) | |
{ | |
wperror("SetFirmwareEnvironmentVariable"); | |
return 5; | |
} | |
} | |
// Print current value | |
wchar_t buf[1024]; | |
DWORD len = GetFirmwareEnvironmentVariable(PREVIOUS_BOOT, REFIND_GUID, buf, sizeof(buf)); | |
if (len > 0) | |
{ | |
buf[len] = 0; | |
printf("rEFInd PreviousBoot value (%d): %ls\r\n", len / sizeof(wchar_t), buf); | |
return 0; | |
} | |
else | |
{ | |
wperror("GetFirmwareEnvironmentVariable"); | |
return 3; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment