-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirmware.cs
62 lines (55 loc) · 1.95 KB
/
Firmware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace BootMan
{
internal static class Firmware
{
public enum FirmwareType
{
FirmwareTypeUnknown,
FirmwareTypeBios,
FirmwareTypeUefi,
FirmwareTypeMax,
}
public static readonly string EFI_GLOBAL_VARIABLE = "{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";
private static class NativeMethods
{
[DllImport("Kernel32.dll", SetLastError = true)]
internal static extern bool GetFirmwareType(out FirmwareType firmwareType);
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern uint GetFirmwareEnvironmentVariable(
string lpName,
string lpGuid,
byte[] lpBuffer,
int nBufferSizeInBytes
);
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool SetFirmwareEnvironmentVariable(
string lpName,
string lpGuid,
byte[] lpBuffer,
int nBufferSizeInBytes
);
}
public static unsafe T GetFirmwareEnvironmentVariable<T>(string name, string guid) where T : unmanaged
{
var buffer = new byte[sizeof(T)];
uint size = NativeMethods.GetFirmwareEnvironmentVariable(name, guid, buffer, buffer.Length);
if (size == 0)
{
throw new Win32Exception();
}
return MemoryMarshal.Cast<byte, T>(buffer)[0];
}
public static FirmwareType GetFirmwareType()
{
bool success = NativeMethods.GetFirmwareType(out FirmwareType firmwareType);
if (!success)
{
throw new Win32Exception();
}
return firmwareType;
}
}
}