Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Rikitav/Unified.Firmware/llms.txt
Use this file to discover all available pages before exploring further.
FirmwareEnvironment is the core class for reading and writing UEFI NVRAM variables. Each instance is scoped to a specific vendor namespace (identified by a Guid) and a specific backend (an IFirmwareBackend implementation). The class provides generic methods that handle marshalling between managed types and raw UEFI variable memory automatically, covering scalar structs, Unicode strings, and struct arrays. The static Global property gives immediate access to the standard UEFI global namespace without constructing an instance manually.
Namespace: Unified.Firmware
Constructor
The platform backend used to perform the low-level read and write operations. Typically
FirmwareInterface.CurrentBackend, but any custom IFirmwareBackend implementation is accepted.The GUID that identifies the UEFI variable namespace (vendor) this environment is scoped to. Well-known GUIDs are available as constants in
FirmwareVendors (for example, FirmwareVendors.GlobalVariable = 8BE4DF61-93CA-11D2-AA0D-00E098032B8C).Static Properties
Global
Returns the sharedGlobalFirmwareEnvironment instance, pre-configured for the UEFI global variable namespace (FirmwareVendors.GlobalVariable) and backed by FirmwareInterface.CurrentBackend. The instance is lazily created on first access and cached. Use this property to read or write standard UEFI variables such as BootOrder, SecureBoot, and Timeout without constructing a FirmwareEnvironment manually.
| Type | GlobalFirmwareEnvironment |
| Access | static read-only |
Instance Properties
Backend
TheIFirmwareBackend implementation this environment delegates all read and write calls to.
| Type | IFirmwareBackend |
| Access | read-only |
VendorGuid
The vendor GUID that identifies the UEFI variable namespace for this environment. All variable names passed to the read and write methods are resolved within this namespace.| Type | Guid |
| Access | read-only |
Methods
ReadVariable<T>
Reads a single UEFI variable and marshals its raw bytes to a value typeT. The buffer size is derived from Marshal.SizeOf<T>().
The name of the UEFI variable to read. Must not be null or empty.
Output parameter. When the method returns, contains the
VariableAttributes flags stored alongside the variable (for example, NON_VOLATILE | BOOTSERVICE_ACCESS | RUNTIME_ACCESS).T — the variable value, unmarshalled from raw UEFI memory.
WriteVariable<T>
Marshals a value typeT to raw bytes and writes it to NVRAM as a UEFI variable.
The name of the UEFI variable to write. Must not be null or empty.
The value to store. The method allocates unmanaged memory, copies the struct into it, calls the backend, then frees the buffer — callers do not manage memory.
The attributes to associate with the variable, such as
NON_VOLATILE | BOOTSERVICE_ACCESS | RUNTIME_ACCESS.ReadStringVariable
Reads a UEFI variable whose value is a null-terminated UTF-16 Unicode string. Internally allocates a 2 KB buffer, reads the variable, converts the pointer to a managedstring via Marshal.PtrToStringUni, and frees the buffer.
The name of the UEFI variable to read.
Output parameter. Receives the attributes of the variable on return.
string — the variable value, or null if the variable does not exist.
WriteStringVariable
Encodes a managedstring as UTF-16 Unicode and writes it to NVRAM as a UEFI string variable.
The name of the UEFI variable to write.
The string value to store. The method converts it to a global Unicode pointer, writes the variable, then frees the pointer.
The attributes to store with the variable.
ReadArrayVariable<T>
Reads a UEFI variable whose value is an array of value typeT elements. The method allocates a buffer of Marshal.SizeOf<T>() * 256 bytes, reads the variable, derives the element count from the returned dataSize, and returns a managed T[].
The name of the UEFI variable to read.
Output parameter. Receives the attributes of the variable on return.
T[] — the array of values stored in the variable. Returns an empty array if the variable contains no data.
WriteArrayVariable<T>
Writes aT[] array to NVRAM as a UEFI variable. The method pins the array in memory and passes a pointer to the backend — no intermediate copy is made.
The name of the UEFI variable to write.
The array to write. Must not be null and must contain at least one element.
The attributes to store with the variable.
FirmwareEnvironmentException
FirmwareEnvironmentException is the library’s dedicated exception type for UEFI variable access errors. It inherits from System.Exception and is thrown by backend implementations when a read or write operation fails at the platform layer (for example, a Win32 API error or a missing /sys/firmware/efi/efivars entry).
| Constructor | Description |
|---|---|
FirmwareEnvironmentException() | Initialises with an empty message and no inner exception. |
FirmwareEnvironmentException(string message) | Initialises with the specified error message. |
FirmwareEnvironmentException(string message, Exception? innerException) | Initialises with a message and an inner exception that caused this error. |
Custom Vendor Namespace Example
Use a customGuid to scope an environment to a specific OEM or application namespace: