1 /*- 2 * Copyright (c) 2016-2021 Netflix, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <ctype.h> 30 #include <efivar.h> 31 #include <efivar-dp.h> 32 #include <err.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <getopt.h> 36 #include <stdarg.h> 37 #include <stdbool.h> 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include "efiutil.h" 44 #include "efichar.h" 45 46 /* options descriptor */ 47 static struct option longopts[] = { 48 { "append", no_argument, NULL, 'a' }, 49 { "ascii", no_argument, NULL, 'A' }, 50 { "attributes", required_argument, NULL, 't' }, 51 { "binary", no_argument, NULL, 'b' }, 52 { "delete", no_argument, NULL, 'D' }, 53 { "device", no_argument, NULL, 'd' }, 54 { "device-path", no_argument, NULL, 'd' }, 55 { "fromfile", required_argument, NULL, 'f' }, 56 { "guid", no_argument, NULL, 'g' }, 57 { "hex", no_argument, NULL, 'H' }, 58 { "list-guids", no_argument, NULL, 'L' }, 59 { "list", no_argument, NULL, 'l' }, 60 { "load-option", no_argument, NULL, 'O' }, 61 { "name", required_argument, NULL, 'n' }, 62 { "no-name", no_argument, NULL, 'N' }, 63 { "print", no_argument, NULL, 'p' }, 64 // { "print-decimal", no_argument, NULL, 'd' }, /* unimplemnted clash with linux version */ 65 { "quiet", no_argument, NULL, 'q' }, 66 { "raw-guid", no_argument, NULL, 'R' }, 67 { "utf8", no_argument, NULL, 'u' }, 68 { "write", no_argument, NULL, 'w' }, 69 { NULL, 0, NULL, 0 } 70 }; 71 72 73 static bool aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag, 74 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag, quiet; 75 static char *varname; 76 static char *fromfile; 77 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS; 78 79 static void 80 usage(void) 81 { 82 83 errx(1, "efivar [-abdDHlLNpqRtuw] [-n name] [-f file] [--append] [--ascii]\n" 84 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n" 85 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n" 86 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n" 87 "\t[--quiet]\n" 88 "\tname[=value]"); 89 } 90 91 static void 92 rep_err(int eval, const char *fmt, ...) 93 { 94 va_list ap; 95 96 if (quiet) 97 exit(eval); 98 99 va_start(ap, fmt); 100 verr(eval, fmt, ap); 101 va_end(ap); 102 } 103 104 static void 105 rep_errx(int eval, const char *fmt, ...) 106 { 107 va_list ap; 108 109 if (quiet) 110 exit(eval); 111 112 va_start(ap, fmt); 113 verrx(eval, fmt, ap); 114 va_end(ap); 115 } 116 117 static void 118 breakdown_name(char *name, efi_guid_t *guid, char **vname) 119 { 120 char *cp, *ocp; 121 122 ocp = NULL; 123 while (true) { 124 cp = strrchr(name, '-'); 125 if (cp == NULL) { 126 if (ocp != NULL) 127 *ocp = '-'; 128 rep_errx(1, "Invalid guid in: %s", name); 129 } 130 if (ocp != NULL) 131 *ocp = '-'; 132 *vname = cp + 1; 133 *cp = '\0'; 134 ocp = cp; 135 if (efi_name_to_guid(name, guid) >= 0) 136 break; 137 } 138 } 139 140 static uint8_t * 141 get_value(char *val, size_t *datalen) 142 { 143 static char buffer[16*1024]; 144 145 if (val != NULL) { 146 *datalen = strlen(val); 147 return ((uint8_t *)val); 148 } 149 /* Read from stdin */ 150 *datalen = sizeof(buffer); 151 *datalen = read(0, buffer, *datalen); 152 return ((uint8_t *)buffer); 153 } 154 155 static void 156 append_variable(char *name, char *val) 157 { 158 char *vname; 159 efi_guid_t guid; 160 size_t datalen; 161 uint8_t *data; 162 163 breakdown_name(name, &guid, &vname); 164 data = get_value(val, &datalen); 165 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0) 166 rep_err(1, "efi_append_variable"); 167 } 168 169 static void 170 delete_variable(char *name) 171 { 172 char *vname; 173 efi_guid_t guid; 174 175 breakdown_name(name, &guid, &vname); 176 if (efi_del_variable(guid, vname) < 0) 177 rep_err(1, "efi_del_variable"); 178 } 179 180 static void 181 write_variable(char *name, char *val) 182 { 183 char *vname; 184 efi_guid_t guid; 185 size_t datalen; 186 uint8_t *data; 187 188 breakdown_name(name, &guid, &vname); 189 data = get_value(val, &datalen); 190 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0) 191 rep_err(1, "efi_set_variable"); 192 } 193 194 static void 195 devpath_dump(uint8_t *data, size_t datalen) 196 { 197 char buffer[1024]; 198 199 efidp_format_device_path(buffer, sizeof(buffer), 200 (const_efidp)data, datalen); 201 if (!Nflag) 202 printf(": "); 203 printf("%s\n", buffer); 204 } 205 206 static void 207 pretty_guid(efi_guid_t *guid, char **gname) 208 { 209 char *pretty = NULL; 210 211 if (gflag) 212 efi_guid_to_name(guid, &pretty); 213 214 if (pretty == NULL) 215 efi_guid_to_str(guid, gname); 216 else 217 *gname = pretty; 218 } 219 220 static void 221 print_var(efi_guid_t *guid, char *name) 222 { 223 uint32_t att; 224 uint8_t *data; 225 size_t datalen; 226 char *gname = NULL; 227 int rv; 228 229 if (guid) 230 pretty_guid(guid, &gname); 231 if (pflag || fromfile) { 232 if (fromfile) { 233 int fd; 234 235 fd = open(fromfile, O_RDONLY); 236 if (fd < 0) 237 rep_err(1, "open %s", fromfile); 238 data = malloc(64 * 1024); 239 if (data == NULL) 240 rep_err(1, "malloc"); 241 datalen = read(fd, data, 64 * 1024); 242 if ((ssize_t)datalen < 0) 243 rep_err(1, "read"); 244 if (datalen == 0) 245 rep_errx(1, "empty file"); 246 close(fd); 247 } else { 248 rv = efi_get_variable(*guid, name, &data, &datalen, &att); 249 if (rv < 0) 250 rep_err(1, "fetching %s-%s", gname, name); 251 } 252 253 254 if (!Nflag) 255 printf("%s-%s\n", gname, name); 256 if (load_opt_flag) 257 efi_print_load_option(data, datalen, Aflag, bflag, uflag); 258 else if (Aflag) 259 asciidump(data, datalen); 260 else if (uflag) 261 utf8dump(data, datalen); 262 else if (bflag) 263 bindump(data, datalen); 264 else if (dflag) 265 devpath_dump(data, datalen); 266 else 267 hexdump(data, datalen); 268 } else { 269 printf("%s-%s", gname, name); 270 } 271 free(gname); 272 if (!Nflag) 273 printf("\n"); 274 } 275 276 static void 277 print_variable(char *name) 278 { 279 char *vname; 280 efi_guid_t guid; 281 282 breakdown_name(name, &guid, &vname); 283 print_var(&guid, vname); 284 } 285 286 static void 287 print_variables(void) 288 { 289 int rv; 290 char *name = NULL; 291 efi_guid_t *guid = NULL; 292 293 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0) 294 print_var(guid, name); 295 296 if (rv < 0) 297 rep_err(1, "Error listing names"); 298 } 299 300 static void 301 print_known_guid(void) 302 { 303 struct uuid_table *tbl; 304 int i, n; 305 306 n = efi_known_guid(&tbl); 307 for (i = 0; i < n; i++) 308 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name); 309 } 310 311 static void 312 parse_args(int argc, char **argv) 313 { 314 int ch, i; 315 316 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpqRt:uw", 317 longopts, NULL)) != -1) { 318 switch (ch) { 319 case 'a': 320 aflag = true; 321 break; 322 case 'A': 323 Aflag = true; 324 break; 325 case 'b': 326 bflag = true; 327 break; 328 case 'd': 329 dflag = true; 330 break; 331 case 'D': 332 Dflag = true; 333 break; 334 case 'g': 335 gflag = true; 336 break; 337 case 'H': 338 Hflag = true; 339 break; 340 case 'l': 341 lflag = true; 342 break; 343 case 'L': 344 Lflag = true; 345 break; 346 case 'n': 347 varname = optarg; 348 break; 349 case 'N': 350 Nflag = true; 351 break; 352 case 'O': 353 load_opt_flag = true; 354 break; 355 case 'p': 356 pflag = true; 357 break; 358 case 'q': 359 quiet = true; 360 break; 361 case 'R': 362 Rflag = true; 363 break; 364 case 't': 365 attrib = strtoul(optarg, NULL, 16); 366 break; 367 case 'u': 368 uflag = true; 369 break; 370 case 'w': 371 wflag = true; 372 break; 373 case 'f': 374 free(fromfile); 375 fromfile = strdup(optarg); 376 break; 377 case 0: 378 rep_errx(1, "unknown or unimplemented option\n"); 379 break; 380 default: 381 usage(); 382 } 383 } 384 argc -= optind; 385 argv += optind; 386 387 if (argc == 1) 388 varname = argv[0]; 389 390 if ((int)aflag + (int)Dflag + (int)wflag > 1) { 391 warnx("Can only use one of -a (--append), " 392 "-D (--delete) and -w (--write)"); 393 usage(); 394 } 395 396 if ((int)aflag + (int)Dflag + (int)wflag > 0 && varname == NULL) { 397 warnx("Must specify a variable for -a (--append), " 398 "-D (--delete) or -w (--write)"); 399 usage(); 400 } 401 402 if (aflag) 403 append_variable(varname, NULL); 404 else if (Dflag) 405 delete_variable(varname); 406 else if (wflag) 407 write_variable(varname, NULL); 408 else if (Lflag) 409 print_known_guid(); 410 else if (fromfile) { 411 Nflag = true; 412 print_var(NULL, NULL); 413 } else if (varname) { 414 pflag = true; 415 print_variable(varname); 416 } else if (argc > 0) { 417 pflag = true; 418 for (i = 0; i < argc; i++) 419 print_variable(argv[i]); 420 } else 421 print_variables(); 422 } 423 424 int 425 main(int argc, char **argv) 426 { 427 428 parse_args(argc, argv); 429 } 430