Report if UEFI is missing (can't read variables)

This commit is contained in:
Lauri Kenttä
2025-01-02 19:16:18 +02:00
parent afd0780b61
commit a1f6297759
2 changed files with 21 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Text; using System.Text;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.Win32; using Microsoft.Win32;
/** /**
@@ -147,24 +148,21 @@ public class Efi {
if (len == buf.Length) { if (len == buf.Length) {
continue; continue;
} }
if (len > 0 || Marshal.GetLastWin32Error() == 0) { var error = Marshal.GetLastWin32Error();
const int ERROR_ENVVAR_NOT_FOUND = 0xCB;
if (error == ERROR_ENVVAR_NOT_FOUND) {
return result;
}
if (len > 0 || error == 0) {
result.Data = new byte[len]; result.Data = new byte[len];
Array.Copy(buf, 0, result.Data, 0, len); Array.Copy(buf, 0, result.Data, 0, len);
return result; return result;
} }
switch (len != 0 ? 0 : Marshal.GetLastWin32Error()) { const int ERROR_INVALID_FUNCTION = 0x1;
case 203: var msg = $"GetVariable: error 0x{error:X08}: ${new Win32Exception(error).Message}";
// Not found. throw error == ERROR_INVALID_FUNCTION ? new NotImplementedException(msg) : new Exception(msg);
return result;
case 87:
throw new Exception("GetVariable: Invalid parameter");
case 1314:
throw new Exception("GetVariable: Privilege not held");
default:
throw new Exception("GetVariable: error " + Marshal.GetLastWin32Error());
} }
} throw new Exception("GetVariable: result exceeds 1MB");
throw new Exception("GetFirmwareEnvironmentVariable: too big data");
} }
/** /**
@@ -269,6 +267,8 @@ public class Efi {
return "Log is empty."; return "Log is empty.";
} }
return new string(BytesToUInt16s(log.Data).Select(i => (char)i).ToArray()); return new string(BytesToUInt16s(log.Data).Select(i => (char)i).ToArray());
} catch (NotImplementedException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
return $"Log not found: {e.ToString()}"; return $"Log not found: {e.ToString()}";
} }

View File

@@ -966,7 +966,14 @@ public class Setup {
InitEspPath(); InitEspPath();
InitEspInfo(); InitEspInfo();
var bootLog = $"\n--- BOOT LOG START ---\n{Efi.GetHackBGRTLog()}\n--- BOOT LOG END ---"; Func<string> tryGetBootLog = () => {
try {
return $"\n--- BOOT LOG START ---\n{Efi.GetHackBGRTLog()}\n--- BOOT LOG END ---";
} catch (NotImplementedException e) {
throw new SetupException($"Looks like you're not booting in UEFI mode. ({e.Message})");
}
};
var bootLog = tryGetBootLog();
Setup.Log(bootLog); Setup.Log(bootLog);
Efi.LogBGRT(); Efi.LogBGRT();
EfiBootEntries.TryLogEntries(); EfiBootEntries.TryLogEntries();