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.Linq;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.Win32;
/**
@@ -147,24 +148,21 @@ public class Efi {
if (len == buf.Length) {
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];
Array.Copy(buf, 0, result.Data, 0, len);
return result;
}
switch (len != 0 ? 0 : Marshal.GetLastWin32Error()) {
case 203:
// Not found.
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());
}
const int ERROR_INVALID_FUNCTION = 0x1;
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("GetFirmwareEnvironmentVariable: too big data");
throw new Exception("GetVariable: result exceeds 1MB");
}
/**
@@ -269,6 +267,8 @@ public class Efi {
return "Log is empty.";
}
return new string(BytesToUInt16s(log.Data).Select(i => (char)i).ToArray());
} catch (NotImplementedException e) {
throw e;
} catch (Exception e) {
return $"Log not found: {e.ToString()}";
}

View File

@@ -966,7 +966,14 @@ public class Setup {
InitEspPath();
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);
Efi.LogBGRT();
EfiBootEntries.TryLogEntries();