読者です 読者をやめる 読者になる 読者になる

はわわーっ

はわわわわっ

powershellからNVRAMよむやつ

windows powershell
Add-Type -Language CSharp -TypeDefinition @'
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    public class UefiNvram {
        [DllImport("advapi32.dll", SetLastError = true)]
        static extern bool
        OpenProcessToken(
            IntPtr ProcessHandle,
            uint DesiredAccess,
            out IntPtr TokenHandle
            );

        [DllImport("advapi32.dll", SetLastError = true)]
        static extern bool
        LookupPrivilegeValue(
            string lpSystemName,
            string lpName,
            out long lpLuid
            );

        [DllImport("advapi32.dll", SetLastError = true)]
        static extern bool
        AdjustTokenPrivileges(
            IntPtr TokenHandle,
            [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
            ref TokenPrivileges NewState,
            uint BufferLength,
            IntPtr Null1,
            IntPtr Null2
            );

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint
        GetFirmwareEnvironmentVariableA(
            string lpName,
            string Guid,
            IntPtr pBuffer,
            uint nSize,
            IntPtr pdwAttributes
            );

        private const uint TokenQuery = 0x08;
        private const uint TokenAdjustPrivileges = 0x20;
        private const uint SePrivilegeEnabled = 0x02;
        private const string SeSystemEnvironmentName = "SeSystemEnvironmentPrivilege";
        private const string UefiGlobalGuid = "{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}";

        [StructLayout(LayoutKind.Sequential)]
        public struct LuidAndAttribute
        {
            public long Luid;
            public uint Attributes;
        };

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct TokenPrivileges
        {
            public uint PrivilegeCount;
            public LuidAndAttribute Privileges;
        };

        public static bool AdjustPrivileges()
        {
            IntPtr htok;

            if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TokenQuery | TokenAdjustPrivileges, out htok) ||
                Marshal.GetLastWin32Error() != 0) {
                Console.WriteLine("OpenProcessToken failed");
                return false;
            }

            TokenPrivileges tp = new TokenPrivileges { PrivilegeCount = 1, Privileges = new LuidAndAttribute { Attributes = SePrivilegeEnabled } };

            if (!LookupPrivilegeValue(null, SeSystemEnvironmentName, out tp.Privileges.Luid) ||
                Marshal.GetLastWin32Error() != 0) {
                Console.WriteLine("LookupPrivilegeValue failed");
                return false;
            }

            if (!AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero) ||
                Marshal.GetLastWin32Error() != 0) {
                Console.WriteLine("AdjustTokenPrivileges failed");
                Console.WriteLine(Marshal.GetLastWin32Error());
                return false;
            }

            return true;
        }

        public static void BootOrder()
        {
            const int bufsize = 0x100;
            IntPtr buf = Marshal.AllocCoTaskMem(bufsize);
            uint retlen = GetFirmwareEnvironmentVariableA("BootOrder", UefiGlobalGuid, buf, bufsize, IntPtr.Zero);

            if (retlen == 0) {
                Console.WriteLine("GetFirmwareEnvironmentVariableA failed");
                Console.WriteLine(retlen);
                Console.WriteLine(Marshal.GetLastWin32Error());
                return;
            }

            int datalen = (int)retlen;
            byte[] data = new byte[datalen];
            Marshal.Copy(buf, data, 0, datalen);
            for (int i = 0; i < datalen; i++)
                Console.Write("{0:x02} ", data[i]);
            Console.WriteLine();

            Marshal.FreeCoTaskMem(buf);
        }
    }
'@

if ([UefiNvram]::AdjustPrivileges()) {
    [UefiNvram]::BootOrder()
}