Index: usr.sbin/efivar/efivar.c =================================================================== --- usr.sbin/efivar/efivar.c +++ usr.sbin/efivar/efivar.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -62,7 +63,9 @@ static int aflag, Aflag, bflag, dflag, Dflag, Hflag, Nflag, lflag, Lflag, Rflag, wflag, pflag; static char *varname; -static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS; +static char *from = NULL; +#define ATTRIB_DEFAULT (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS) +static u_long attrib = ATTRIB_DEFAULT; static void usage(void) @@ -92,14 +95,22 @@ get_value(char *val, size_t *datalen) { static char buffer[16*1024]; + int fd = 0; if (val != NULL) { *datalen = strlen(val); return ((uint8_t *)val); } /* Read from stdin */ + if (from != NULL) { + fd = open(from, O_RDONLY, 0); + if (fd < 0) + return NULL; + } *datalen = sizeof(buffer); - *datalen = read(0, buffer, *datalen); + *datalen = read(fd, buffer, *datalen); + if (fd != 0) + close(fd); return ((uint8_t *)buffer); } @@ -110,8 +121,13 @@ efi_guid_t guid; size_t datalen; uint8_t *data; + uint32_t old_attrib = 0; breakdown_name(name, &guid, &vname); + if (efi_get_variable(guid, vname, NULL, &datalen, &old_attrib) < 0) + err(1, "efi_get_variable"); + if (attrib == ATTRIB_DEFAULT) + attrib = old_attrib; data = get_value(val, &datalen); if (efi_append_variable(guid, vname, data, datalen, attrib) < 0) err(1, "efi_append_variable"); @@ -135,8 +151,12 @@ efi_guid_t guid; size_t datalen; uint8_t *data; + uint32_t old_attrib = 0; breakdown_name(name, &guid, &vname); + if (efi_get_variable(guid, vname, NULL, &datalen, &old_attrib) >= 0) + if (attrib == ATTRIB_DEFAULT) + attrib = old_attrib; data = get_value(val, &datalen); if (efi_set_variable(guid, vname, data, datalen, attrib, 0) < 0) err(1, "efi_set_variable"); @@ -273,6 +293,9 @@ case 'D': Dflag++; break; + case 'f': + from = optarg; + break; case 'H': Hflag++; break; @@ -295,12 +318,11 @@ Rflag++; break; case 't': - attrib = strtoul(optarg, NULL, 16); + attrib = strtoul(optarg, NULL, 0); break; case 'w': wflag++; break; - case 'f': case 0: errx(1, "unknown or unimplemented option\n"); break;