Skip to content

Commit

Permalink
Save ESP filename
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Tia <javier.tia@linaro.org>
  • Loading branch information
jetm committed Oct 16, 2024
1 parent 6092a82 commit c64f7c1
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include "efivar.h"

#define EFI_VARTOFILE "b2ac5fc9-92b7-4acd-aeac-11e818c3130c-VarToFile"
#define VAR2FILE "/sys/firmware/efi/efivars/VarToFile-b2ac5fc9-92b7-4acd-aeac-11e818c3130c"
#define EFI_RTSTORAGEVOLATILE "b2ac5fc9-92b7-4acd-aeac-11e818c3130c-RTStorageVolatile"
#define PATH_SIZE 4096
#define PATH_SIZE 4096

static const char *esp_paths[] = {
"/boot",
Expand Down Expand Up @@ -345,8 +345,6 @@ get_esp_filename(char **esp_filename)
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;
Expand All @@ -373,6 +371,49 @@ search_esp_filename(const char *filename, char *esp_filename)
esp_filename[0] = '\0';
}

static
void save_esp_filename(const char *esp_filename) {
FILE *var2file = NULL;
FILE *output_file = NULL;
unsigned char buffer[1024];
size_t bytes_read, bytes_written;
bool fail = false;

var2file = fopen(VAR2FILE, "rb");
if (var2file == NULL) {
fprintf(stderr, "Error: Could not open input file '%s'\n", VAR2FILE);
exit(1);
}

output_file = fopen(esp_filename, "wb");
if (output_file == NULL) {
fprintf(stderr, "Error: Could not open output file '%s'\n", esp_filename);
fclose(var2file);
exit(1);
}

if (fread(buffer, 1, 4, var2file) < 4) {
fprintf(stderr, "Error: Could not skip first 4 bytes or file is too small\n");
fail = true;
goto clean;
}

while ((bytes_read = fread(buffer, 1, sizeof(buffer), var2file)) > 0) {
bytes_written = fwrite(buffer, 1, bytes_read, output_file);
if (bytes_written != bytes_read) {
fprintf(stderr, "Error: Could not write data to output file\n");
fail = true;
goto clean;
}
}

clean:
fclose(var2file);
fclose(output_file);
if (fail)
exit(1);
}

void PUBLIC
efi_save_esp_filename(void)
{
Expand All @@ -387,7 +428,7 @@ efi_save_esp_filename(void)

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);
save_esp_filename(esp_filename_path);
}
else {
fprintf(stderr, "efivar: no found file in ESP part to save VarToFile\n");
Expand Down

0 comments on commit c64f7c1

Please sign in to comment.