Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: efivar: Copy VarToFile to RTStorageVolatile file at ESP #267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/efivarfs.c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message needs to be clearer. It's usually not required to write a file.
Please use the u-boot commit message for guidance and write a proper one here.
Explain what u-boot is doing with these variables and why you need help from efivar & userspace tools to make the change permanent

Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ efivarfs_del_variable(efi_guid_t guid, const char *name)
if (rc < 0)
efi_error("unlink failed");

efi_save_esp_filename();

__typeof__(errno) errno_value = errno;
free(path);
errno = errno_value;
Expand Down Expand Up @@ -442,6 +444,8 @@ efivarfs_set_variable(efi_guid_t guid, const char *name, const uint8_t *data,
goto err;
}

efi_save_esp_filename();

/* we're done */
ret = 0;

Expand Down
86 changes: 83 additions & 3 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

#include "efivar.h"

#define EFI_VARTOFILE "b2ac5fc9-92b7-4acd-aeac-11e818c3130c-VarToFile"
#define EFI_RTSTORAGEVOLATILE "b2ac5fc9-92b7-4acd-aeac-11e818c3130c-RTStorageVolatile"
#define PATH_SIZE 4096

static const char *esp_paths[] = {
"/boot",
"/boot/efi",
"/efi"
};

static int default_probe(void)
{
return 1;
Expand Down Expand Up @@ -65,7 +75,7 @@ _efi_set_variable_variadic(efi_guid_t guid, const char *name, const uint8_t *dat
VERSION(_efi_set_variable_mode,efi_set_variable@@LIBEFIVAR_0.24)
int NONNULL(2, 3) PUBLIC
_efi_set_variable_mode(efi_guid_t guid, const char *name, const uint8_t *data,
size_t data_size, uint32_t attributes, mode_t mode)
size_t data_size, uint32_t attributes, mode_t mode)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldnt change tabbing on this patch

{
int rc;
if (!ops->set_variable) {
Expand Down Expand Up @@ -93,7 +103,7 @@ efi_append_variable(efi_guid_t guid, const char *name, const uint8_t *data,
int rc;
if (!ops->append_variable) {
rc = generic_append_variable(guid, name, data, data_size,
attributes);
attributes);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is irrelevant

if (rc < 0)
efi_error("generic_append_variable() failed");
else
Expand Down Expand Up @@ -145,7 +155,7 @@ efi_get_variable(efi_guid_t guid, const char *name, uint8_t **data,

int NONNULL(2, 3) PUBLIC
efi_get_variable_attributes(efi_guid_t guid, const char *name,
uint32_t *attributes)
uint32_t *attributes)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so is this

{
int rc;
if (!ops->get_variable_attributes) {
Expand Down Expand Up @@ -227,6 +237,76 @@ efi_variables_supported(void)
return 1;
}

static int
get_esp_filename(char **esp_filename)
{
char *dst_name = NULL;
efi_guid_t dst_guid = efi_guid_empty;
size_t filename_size;
uint32_t filename_attr;
uint8_t *filename = NULL;
int rc = 0;

efi_parse_name(EFI_RTSTORAGEVOLATILE, &dst_name, &dst_guid);

if (dst_name && !efi_guid_is_empty(&dst_guid)) {
rc = efi_get_variable(dst_guid, dst_name, &filename, &filename_size, &filename_attr);
if (rc < 0) {
fprintf(stderr, "efivar: Error %d getting filename from RTStorageVolatile\n", rc);
return rc;
}

printf("efivar: RSTStorageVolatile found with %s\n", (char *)filename);
}

*esp_filename = (char *)filename;

free(dst_name);

return 0;
}

static void
search_esp_filename(const char *filename, char *esp_filename)
{
size_t num_paths = sizeof(esp_paths) / sizeof(esp_paths[0]);

for (size_t i = 0; i < num_paths; ++i) {
snprintf(esp_filename, PATH_SIZE, "%s/%s", esp_paths[i], filename);

struct stat buffer;
if (stat(esp_filename, &buffer) == 0) {
return;
}
}

esp_filename[0] = '\0';
}

void PUBLIC
efi_save_esp_filename(void)
{
char esp_filename_path[PATH_SIZE];
char *esp_filename = NULL;
int rc = 0;

rc = get_esp_filename(&esp_filename);
if (rc < 0 || !esp_filename) {
goto cleanup;
}

search_esp_filename(esp_filename, esp_filename_path);
if (esp_filename_path[0] != '\0') {
printf("efivar: VarToFile content saved at %s\n", esp_filename_path);
}
else {
fprintf(stderr, "efivar: no found file in ESP part to save VarToFile\n");
}

cleanup:
free(esp_filename);
}

static void CONSTRUCTOR libefivar_init(void);

static void CONSTRUCTOR
Expand Down
8 changes: 6 additions & 2 deletions src/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ vars_del_variable(efi_guid_t guid, const char *name)
}

rc = write(fd, buf, buf_size);
if (rc >= 0)
if (rc >= 0) {
efi_save_esp_filename();
ret = 0;
}
else
efi_error("write() failed");
err:
Expand Down Expand Up @@ -594,8 +596,10 @@ vars_set_variable(efi_guid_t guid, const char *name, const uint8_t *data,
rc = write(fd, &var32, sizeof(var32));
}

if (rc >= 0)
if (rc >= 0) {
efi_save_esp_filename();
ret = 0;
}
else
efi_error("write() failed");

Expand Down