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