Skip to content

Commit

Permalink
Create generic wrapper for primitive returns of GetFirmwareEnvironmen…
Browse files Browse the repository at this point in the history
…tVariable.
  • Loading branch information
Kytech committed Jan 15, 2023
1 parent cdf46a8 commit 9318b08
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions BootMan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<WindowsPackageType>None</WindowsPackageType>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 16 additions & 4 deletions Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum FirmwareType
FirmwareTypeMax,
}

private static readonly string EFI_GLOBAL_VARIABLE = "{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";
public static readonly string EFI_GLOBAL_VARIABLE = "{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";

[DllImport("Kernel32.dll")]
private static extern bool GetFirmwareType(out FirmwareType firmwareType);
Expand All @@ -22,21 +22,33 @@ public enum FirmwareType
private static extern uint GetFirmwareEnvironmentVariable(
string lpName,
string lpGuid,
Span<ushort> lpBuffer,
byte[] lpBuffer,
uint nSize
);

[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SetFirmwareEnvironmentVariable(
string lpName,
string lpGuid,
Span<ushort> lpBuffer,
byte[] lpBuffer,
uint nSize
);

public static unsafe T GetFirmwareEnvironmentVariable<T>(string name, string guid) where T : unmanaged
{
var buffer = new byte[sizeof(T)];
uint size = GetFirmwareEnvironmentVariable(name, guid, buffer, (uint)buffer.Length);
if (size == 0)
{
throw new Exception($"GetFirmwareEnvironmentVariable failed with error code {Marshal.GetLastPInvokeError()}");
}
return MemoryMarshal.Cast<byte, T>(buffer)[0];
}

public static FirmwareType GetFirmwareType()
{
bool success = Firmware.GetFirmwareType(out FirmwareType firmwareType);
// TODO: Do error handling to indicate win32 error
bool success = GetFirmwareType(out FirmwareType firmwareType);
return success ? firmwareType : FirmwareType.FirmwareTypeUnknown;
}
}
Expand Down
9 changes: 9 additions & 0 deletions app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="BootMan.app"/>

<!-- TODO: Currently prompting for UAC at beginning, but policy can change to permit regular users, so do admin check at rumtime instead -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10, version 1809 (10.0; Build 17763) - minimum required version -->
Expand Down

0 comments on commit 9318b08

Please sign in to comment.