2 * Copyright (c) 2016 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: head/usr.sbin/efivar/efivar.c 307390 2016-10-16 05:53:18Z imp $");
41 /* options descriptor */
42 static struct option longopts
[] = {
43 { "append", no_argument
, NULL
, 'a' },
44 { "ascii", no_argument
, NULL
, 'A' },
45 { "attributes", required_argument
, NULL
, 't' },
46 { "binary", no_argument
, NULL
, 'b' },
47 { "delete", no_argument
, NULL
, 'D' },
48 { "fromfile", required_argument
, NULL
, 'f' },
49 { "hex", no_argument
, NULL
, 'H' },
50 { "list-guids", no_argument
, NULL
, 'L' },
51 { "list", no_argument
, NULL
, 'l' },
52 { "name", required_argument
, NULL
, 'n' },
53 { "no-name", no_argument
, NULL
, 'N' },
54 { "print", no_argument
, NULL
, 'p' },
55 { "print-decimal", no_argument
, NULL
, 'd' },
56 { "raw-guid", no_argument
, NULL
, 'R' },
57 { "write", no_argument
, NULL
, 'w' },
62 static int aflag
, Aflag
, bflag
, dflag
, Dflag
, Hflag
, Nflag
,
63 lflag
, Lflag
, Rflag
, wflag
, pflag
;
65 static u_long attrib
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
71 errx(1, "efivar [-abdDHlLNpRtw] [-n name] [-f file] [--append] [--ascii]\n"
72 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
73 "\t[--list-guids] [--list] [--name name] [--no-name] [--print]\n"
74 "\t[--print-decimal] [--raw-guid] [--write] name[=value]");
78 breakdown_name(char *name
, efi_guid_t
*guid
, char **vname
)
82 cp
= strrchr(name
, '-');
84 errx(1, "Invalid name: %s", name
);
87 if (efi_str_to_guid(name
, guid
) < 0)
88 errx(1, "Invalid guid %s", name
);
92 get_value(char *val
, size_t *datalen
)
94 static char buffer
[16*1024];
97 *datalen
= strlen(val
);
98 return ((uint8_t *)val
);
100 /* Read from stdin */
101 *datalen
= sizeof(buffer
);
102 *datalen
= read(0, buffer
, *datalen
);
103 return ((uint8_t *)buffer
);
107 append_variable(char *name
, char *val
)
114 breakdown_name(name
, &guid
, &vname
);
115 data
= get_value(val
, &datalen
);
116 if (efi_append_variable(guid
, vname
, data
, datalen
, attrib
) < 0)
117 err(1, "efi_append_variable");
121 delete_variable(char *name
)
126 breakdown_name(name
, &guid
, &vname
);
127 if (efi_del_variable(guid
, vname
) < 0)
128 err(1, "efi_del_variable");
132 write_variable(char *name
, char *val
)
139 breakdown_name(name
, &guid
, &vname
);
140 data
= get_value(val
, &datalen
);
141 if (efi_set_variable(guid
, vname
, data
, datalen
, attrib
, 0) < 0)
142 err(1, "efi_set_variable");
146 asciidump(uint8_t *data
, size_t datalen
)
154 for (i
= 0; i
< datalen
; i
++) {
155 if (isprint(data
[i
])) {
161 printf("%c", data
[i
]);
168 printf("%%%02x", data
[i
]);
175 hexdump(uint8_t *data
, size_t datalen
)
181 for (i
= 0; i
< datalen
; i
++) {
185 printf("%04x: ", (int)i
);
187 printf("%02x ", data
[i
]);
193 bindump(uint8_t *data
, size_t datalen
)
195 write(1, data
, datalen
);
199 print_var(efi_guid_t
*guid
, char *name
)
207 efi_guid_to_str(guid
, &gname
);
209 printf("%s-%s", gname
, name
);
211 rv
= efi_get_variable(*guid
, name
, &data
, &datalen
, &att
);
214 printf("\n --- Error getting value --- %d", errno
);
217 asciidump(data
, datalen
);
219 bindump(data
, datalen
);
221 hexdump(data
, datalen
);
230 print_variable(char *name
)
235 breakdown_name(name
, &guid
, &vname
);
236 print_var(&guid
, vname
);
240 print_variables(void)
244 efi_guid_t
*guid
= NULL
;
246 while ((rv
= efi_get_next_variable_name(&guid
, &name
)) > 0)
247 print_var(guid
, name
);
250 err(1, "Error listing names");
254 parse_args(int argc
, char **argv
)
258 while ((ch
= getopt_long(argc
, argv
, "aAbdDf:HlLNn:pRt:w",
259 longopts
, NULL
)) != -1) {
298 attrib
= strtoul(optarg
, NULL
, 16);
305 errx(1, "unknown or unimplemented option\n");
317 if (aflag
+ Dflag
+ wflag
> 1) {
318 warnx("Can only use one of -a (--append), "
319 "-D (--delete) and -w (--write)");
323 if (aflag
+ Dflag
+ wflag
> 0 && varname
== NULL
) {
324 warnx("Must specify a variable for -a (--append), "
325 "-D (--delete) or -w (--write)");
330 append_variable(varname
, NULL
);
332 delete_variable(varname
);
334 write_variable(varname
, NULL
);
337 print_variable(varname
);
338 } else if (argc
> 0) {
340 for (i
= 0; i
< argc
; i
++)
341 print_variable(argv
[i
]);
347 main(int argc
, char **argv
)
350 parse_args(argc
, argv
);