From 3d5efa211786ae4a5118cd00b36955168378e390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20Kentt=C3=A4?= Date: Thu, 2 Apr 2026 08:29:02 +0300 Subject: [PATCH] Fix GetVariable with >4kB data --- src/Efi.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Efi.cs b/src/Efi.cs index 93dae33..5ab3bac 100644 --- a/src/Efi.cs +++ b/src/Efi.cs @@ -35,6 +35,9 @@ public class Efi { [DllImport("kernel32.dll", SetLastError = true)] private static extern UInt32 GetSystemFirmwareTable(UInt32 provider, UInt32 table, [MarshalAs(UnmanagedType.LPArray)] byte[] buffer, UInt32 len); + [DllImport("kernel32.dll", SetLastError = true)] + private static extern void SetLastError(UInt32 errorCode); + [StructLayout(LayoutKind.Sequential, Pack = 1)] private struct TokPriv1Luid { public int Count; @@ -144,6 +147,7 @@ public class Efi { for (UInt32 i = 4096; i <= 1024*1024; i *= 2) { byte[] buf = new byte[i]; + SetLastError(0); UInt32 len = GetFirmwareEnvironmentVariableEx(name, guid, buf, (UInt32) buf.Length, out result.Attributes); if (len == buf.Length) { continue; @@ -159,7 +163,7 @@ public class Efi { return result; } const int ERROR_INVALID_FUNCTION = 0x1; - var msg = $"GetVariable: error 0x{error:X08}: ${new Win32Exception(error).Message}"; + var msg = $"GetVariable: error 0x{error:X08}: {new Win32Exception(error).Message}"; throw error == ERROR_INVALID_FUNCTION ? new NotImplementedException(msg) : new Exception(msg); } throw new Exception("GetVariable: result exceeds 1MB");