Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/efivar/efivar.c
Show All 26 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <efivar.h> | #include <efivar.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <getopt.h> | #include <getopt.h> | ||||
#include <fcntl.h> | |||||
#include <stddef.h> | #include <stddef.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
/* options descriptor */ | /* options descriptor */ | ||||
static struct option longopts[] = { | static struct option longopts[] = { | ||||
Show All 14 Lines | static struct option longopts[] = { | ||||
{ "write", no_argument, NULL, 'w' }, | { "write", no_argument, NULL, 'w' }, | ||||
{ NULL, 0, NULL, 0 } | { NULL, 0, NULL, 0 } | ||||
}; | }; | ||||
static int aflag, Aflag, bflag, dflag, Dflag, Hflag, Nflag, | static int aflag, Aflag, bflag, dflag, Dflag, Hflag, Nflag, | ||||
lflag, Lflag, Rflag, wflag, pflag; | lflag, Lflag, Rflag, wflag, pflag; | ||||
static char *varname; | 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) | |||||
imp: I don't think this should be done. Also, you're diffing against an old version.
| |||||
static u_long attrib = ATTRIB_DEFAULT; | |||||
static void | static void | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
errx(1, "efivar [-abdDHlLNpRtw] [-n name] [-f file] [--append] [--ascii]\n" | errx(1, "efivar [-abdDHlLNpRtw] [-n name] [-f file] [--append] [--ascii]\n" | ||||
"\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n" | "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n" | ||||
"\t[--list-guids] [--list] [--name name] [--no-name] [--print]\n" | "\t[--list-guids] [--list] [--name name] [--no-name] [--print]\n" | ||||
Show All 13 Lines | breakdown_name(char *name, efi_guid_t *guid, char **vname) | ||||
if (efi_str_to_guid(name, guid) < 0) | if (efi_str_to_guid(name, guid) < 0) | ||||
errx(1, "Invalid guid %s", name); | errx(1, "Invalid guid %s", name); | ||||
} | } | ||||
static uint8_t * | static uint8_t * | ||||
get_value(char *val, size_t *datalen) | get_value(char *val, size_t *datalen) | ||||
{ | { | ||||
static char buffer[16*1024]; | static char buffer[16*1024]; | ||||
int fd = 0; | |||||
if (val != NULL) { | if (val != NULL) { | ||||
*datalen = strlen(val); | *datalen = strlen(val); | ||||
return ((uint8_t *)val); | return ((uint8_t *)val); | ||||
} | } | ||||
/* Read from stdin */ | /* Read from stdin */ | ||||
if (from != NULL) { | |||||
fd = open(from, O_RDONLY, 0); | |||||
if (fd < 0) | |||||
return NULL; | |||||
} | |||||
*datalen = sizeof(buffer); | *datalen = sizeof(buffer); | ||||
*datalen = read(0, buffer, *datalen); | *datalen = read(fd, buffer, *datalen); | ||||
if (fd != 0) | |||||
close(fd); | |||||
return ((uint8_t *)buffer); | return ((uint8_t *)buffer); | ||||
} | } | ||||
static void | static void | ||||
append_variable(char *name, char *val) | append_variable(char *name, char *val) | ||||
{ | { | ||||
char *vname; | char *vname; | ||||
efi_guid_t guid; | efi_guid_t guid; | ||||
size_t datalen; | size_t datalen; | ||||
uint8_t *data; | uint8_t *data; | ||||
uint32_t old_attrib = 0; | |||||
breakdown_name(name, &guid, &vname); | 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); | data = get_value(val, &datalen); | ||||
if (efi_append_variable(guid, vname, data, datalen, attrib) < 0) | if (efi_append_variable(guid, vname, data, datalen, attrib) < 0) | ||||
Not Done Inline ActionsThis is not needed. efi_append_variable already does this: return efi_set_variable(guid, name, data, data_size, attributes | EFI_VARIABLE_APPEND_WRITE, 0); imp: This is not needed. efi_append_variable already does this:
return efi_set_variable… | |||||
err(1, "efi_append_variable"); | err(1, "efi_append_variable"); | ||||
} | } | ||||
static void | static void | ||||
delete_variable(char *name) | delete_variable(char *name) | ||||
{ | { | ||||
char *vname; | char *vname; | ||||
efi_guid_t guid; | efi_guid_t guid; | ||||
breakdown_name(name, &guid, &vname); | breakdown_name(name, &guid, &vname); | ||||
if (efi_del_variable(guid, vname) < 0) | if (efi_del_variable(guid, vname) < 0) | ||||
err(1, "efi_del_variable"); | err(1, "efi_del_variable"); | ||||
} | } | ||||
static void | static void | ||||
write_variable(char *name, char *val) | write_variable(char *name, char *val) | ||||
{ | { | ||||
char *vname; | char *vname; | ||||
efi_guid_t guid; | efi_guid_t guid; | ||||
size_t datalen; | size_t datalen; | ||||
uint8_t *data; | uint8_t *data; | ||||
uint32_t old_attrib = 0; | |||||
breakdown_name(name, &guid, &vname); | breakdown_name(name, &guid, &vname); | ||||
if (efi_get_variable(guid, vname, NULL, &datalen, &old_attrib) >= 0) | |||||
if (attrib == ATTRIB_DEFAULT) | |||||
Not Done Inline ActionsYou're allowed to set a variable that's not been set yet. This is incorrect. imp: You're allowed to set a variable that's not been set yet. This is incorrect.
| |||||
attrib = old_attrib; | |||||
data = get_value(val, &datalen); | data = get_value(val, &datalen); | ||||
Not Done Inline ActionsAnd this is wrong if it was never set. The default should still be 0x7. imp: And this is wrong if it was never set. The default should still be 0x7. | |||||
if (efi_set_variable(guid, vname, data, datalen, attrib, 0) < 0) | if (efi_set_variable(guid, vname, data, datalen, attrib, 0) < 0) | ||||
err(1, "efi_set_variable"); | err(1, "efi_set_variable"); | ||||
} | } | ||||
static void | static void | ||||
asciidump(uint8_t *data, size_t datalen) | asciidump(uint8_t *data, size_t datalen) | ||||
{ | { | ||||
size_t i; | size_t i; | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | case 'b': | ||||
bflag++; | bflag++; | ||||
break; | break; | ||||
case 'd': | case 'd': | ||||
dflag++; | dflag++; | ||||
break; | break; | ||||
case 'D': | case 'D': | ||||
Dflag++; | Dflag++; | ||||
break; | break; | ||||
case 'f': | |||||
from = optarg; | |||||
break; | |||||
case 'H': | case 'H': | ||||
Hflag++; | Hflag++; | ||||
break; | break; | ||||
case 'l': | case 'l': | ||||
lflag++; | lflag++; | ||||
break; | break; | ||||
case 'L': | case 'L': | ||||
Lflag++; | Lflag++; | ||||
break; | break; | ||||
case 'n': | case 'n': | ||||
varname = optarg; | varname = optarg; | ||||
break; | break; | ||||
case 'N': | case 'N': | ||||
Nflag++; | Nflag++; | ||||
break; | break; | ||||
case 'p': | case 'p': | ||||
pflag++; | pflag++; | ||||
break; | break; | ||||
case 'R': | case 'R': | ||||
Rflag++; | Rflag++; | ||||
break; | break; | ||||
case 't': | case 't': | ||||
attrib = strtoul(optarg, NULL, 16); | attrib = strtoul(optarg, NULL, 0); | ||||
break; | break; | ||||
case 'w': | case 'w': | ||||
wflag++; | wflag++; | ||||
break; | break; | ||||
case 'f': | |||||
case 0: | case 0: | ||||
errx(1, "unknown or unimplemented option\n"); | errx(1, "unknown or unimplemented option\n"); | ||||
Not Done Inline ActionsThis is already in -current. imp: This is already in -current.
| |||||
break; | break; | ||||
default: | default: | ||||
usage(); | usage(); | ||||
} | } | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
Show All 38 Lines |
I don't think this should be done. Also, you're diffing against an old version.