public IntPtr BootEntry; // BOOT_ENTRY, not actually a pointer, just put it her for convenience public unsafe static NtEfiBootEntryList MarshalFromNative(IntPtr data) { var result = new NtEfiBootEntryList(); result.NextEntryOffset = *(uint *)data; result.BootEntry = data + 4; return(result); }
static List <string> read_all_boot_entry_names() { var result = new List <string>(); uint size = 0; var buffer = IntPtr.Zero; var status = Natives.NtEnumerateBootEntries(buffer, ref size); if (status != NtStatus.BufferTooSmall) { if (status >= 0) { // Somehow there are no boot entries in NVRAM. Console.WriteLine("Somehow there are no boot entries in NVRAM."); return(result); } else { // An unexpected error occurred Console.WriteLine("An unexpected error occurred."); throw new Exception(); } } buffer = Marshal.AllocHGlobal((int)size); status = Natives.NtEnumerateBootEntries(buffer, ref size); if (status < 0) { // An unexpected error occurred Console.WriteLine("An unexpected error occurred. 2"); Marshal.FreeHGlobal(buffer); throw new Exception(); } var currPtr = buffer; for (var x = NtEfiBootEntryList.MarshalFromNative(currPtr); ; x = NtEfiBootEntryList.MarshalFromNative(currPtr += (int)x.NextEntryOffset)) { var id = NtEfiBootEntry.MarshalFromNative(x.BootEntry).Id; result.Add($"Boot{id.ToString("X4")}"); if (x.NextEntryOffset == 0) { break; } } Marshal.FreeHGlobal(buffer); return(result); }