A
A
Alexey Skobkin2018-07-24 23:57:26
linux
Alexey Skobkin, 2018-07-24 23:57:26

How to create a UEFI variable and write a string value to it in Linux?

The computer has Gentoo Linux and Windows installed in dual-boot.
Systems boot in EFI mode. The default bootloader is systemd-boot, which in turn boots Gentoo Linux by default after a timeout.
There is a goal to make it possible to reboot immediately in Windows without having to change the selection in systemd-boot.
As far as I understand from the documentation , this can be done using a variable LoaderEntryOneShot. But I have not succeeded in
writing down the value auto-windows( automatically detected by Windows Boot Manager).
What I tried:

# здесь и далее UUID сгенерирован рандомно с помощью uuidgen
# в файле /tmp/efivar-auto-windows лежит значение, которое я хотел записать в переменную
efivar -w -n 926b68c6-77da-4cf6-ba58-2f27df0f705d-LoaderEntryOneShot -f /tmp/efivar-auto-windows 
# efivar: Input/output error

echo "auto-windows" > /sys/firmware/efi/efivars/LoaderEntryOneShot-926b68c6-77da-4cf6-ba58-2f27df0f705d
# echo: write error: invalid argument

After various experiments like the above efivar -l, I started to display this variable, but the contents still, apparently, were not recorded:
# efivar -p -n 926b68c6-77da-4cf6-ba58-2f27df0f705d-LoaderEntryOneShot                                                                                                                                                                                                      
GUID: 926b68c6-77da-4cf6-ba58-2f27df0f705d
Name: "LoaderEntryOneShot"
Attributes:
Value:

PS man efivarI looked, but this is one of the most pointless man pages I've ever seen.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2018-07-25
@skobkin

What I learned from the documentation and links:
The name of the variable should be

LoaderEntryOneShot-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f
- this is the GUID that the loader expects.
With the contents of the variable, it is more difficult - this is a binary file.
The efivars file system needs to be mounted. mount -t efivarfs none /sys/firmware/efi/efivars
The binary structure of the file is as follows:
struct new_efi_variable {
u32 attributes;
u8 data[0];
};

Hence, the first 4 bytes define the attributes. Most important:
#define EFI_VARIABLE_NON_VOLATILE       0x00000001
#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x00000002
#define EFI_VARIABLE_RUNTIME_ACCESS     0x00000004

Next, there should be the value of your auto-windows variable .
As a result, I believe, the variable is created like this:
Try it and correct me if I'm wrong with the solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question