2 * Copyright (c) 2015 Netflix, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
36 #include "bootstrap.h"
42 * Simple wrappers to the underlying UEFI functions.
43 * See http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES
47 efi_get_next_variable_name(UINTN *variable_name_size, CHAR16 *variable_name,
48 EFI_GUID *vendor_guid)
50 return (RS->GetNextVariableName(variable_name_size, variable_name,
55 efi_get_variable(CHAR16 *variable_name, EFI_GUID *vendor_guid,
56 UINT32 *attributes, UINTN *data_size, void *data)
58 return (RS->GetVariable(variable_name, vendor_guid, attributes,
63 efi_set_variable(CHAR16 *variable_name, EFI_GUID *vendor_guid,
64 UINT32 attributes, UINTN data_size, void *data)
66 return (RS->SetVariable(variable_name, vendor_guid, attributes,
71 efi_init_environment(void)
75 snprintf(var, sizeof(var), "%d.%02d", ST->Hdr.Revision >> 16,
76 ST->Hdr.Revision & 0xffff);
77 env_setenv("efi-version", EV_VOLATILE, var, env_noset, env_nounset);
80 COMMAND_SET(efishow, "efi-show", "print some or all EFI variables", command_efi_show);
83 efi_print_var(CHAR16 *varnamearg, EFI_GUID *matchguid, int lflag)
94 status = RS->GetVariable(varnamearg, matchguid, &attr,
96 if (status != EFI_BUFFER_TOO_SMALL) {
97 printf("Can't get the variable: error %#lx\n",
98 EFI_ERROR_CODE(status));
101 data = malloc(datasz);
102 status = RS->GetVariable(varnamearg, matchguid, &attr,
104 if (status != EFI_SUCCESS) {
105 printf("Can't get the variable: error %#lx\n",
106 EFI_ERROR_CODE(status));
109 uuid_to_string((uuid_t *)matchguid, &str, &uuid_status);
111 printf("%s 0x%x %S", str, attr, varnamearg);
113 printf("%s 0x%x %S=", str, attr, varnamearg);
117 for (i = 0; i < datasz - 1; i++) {
118 /* Quick hack to see if this ascii-ish string printable range plus tab, cr and lf */
119 if ((str[i] < 32 || str[i] > 126) && str[i] != 9 && str[i] != 10 && str[i] != 13) {
124 if (str[datasz - 1] != '\0')
129 for (i = 0; i < datasz / 2; i++) {
130 if (isalnum(data[i]) || isspace(data[i]))
131 printf("%c", data[i]);
133 printf("\\x%02x", data[i]);
138 if (pager_output("\n"))
144 command_efi_show(int argc, char *argv[])
150 * print all the env vars tagged with UUID
152 * search all the env vars and print the ones matching var
153 * eif-show -u UUID -v var
155 * print all the env vars that match UUID and var
157 /* NB: We assume EFI_GUID is the same as uuid_t */
158 int aflag = 0, gflag = 0, lflag = 0, vflag = 0;
162 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
163 EFI_GUID matchguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
164 uint32_t uuid_status;
167 CHAR16 varnamearg[128];
171 while ((ch = getopt(argc, argv, "ag:lv:")) != -1) {
178 uuid_from_string(optarg, (uuid_t *)&matchguid,
180 if (uuid_status != uuid_s_ok) {
181 printf("uid %s could not be parsed\n", optarg);
190 if (strlen(optarg) >= nitems(varnamearg)) {
191 printf("Variable %s is longer than %zd characters\n",
192 optarg, nitems(varnamearg));
195 for (i = 0; i < strlen(optarg); i++)
196 varnamearg[i] = optarg[i];
200 printf("Invalid argument %c\n", ch);
205 if (aflag && (gflag || vflag)) {
206 printf("-a isn't compatible with -v or -u\n");
210 if (aflag && optind < argc) {
211 printf("-a doesn't take any args\n");
222 if (vflag && gflag) {
223 rv = efi_print_var(varnamearg, &matchguid, lflag);
230 if (strlen(optarg) >= nitems(varnamearg)) {
231 printf("Variable %s is longer than %zd characters\n",
232 optarg, nitems(varnamearg));
236 for (i = 0; i < strlen(optarg); i++)
237 varnamearg[i] = optarg[i];
240 uuid_from_string(optarg, (uuid_t *)&matchguid,
242 if (uuid_status != uuid_s_ok) {
243 printf("uid %s could not be parsed\n", optarg);
247 rv = efi_print_var(varnamearg, &matchguid, lflag);
253 printf("Too many args %d\n", argc);
259 * Initiate the search -- note the standard takes pain
260 * to specify the initial call must be a poiner to a NULL
264 varname = malloc(varalloc);
265 if (varname == NULL) {
266 printf("Can't allocate memory to get variables\n");
273 status = RS->GetNextVariableName(&varsz, varname, &varguid);
274 if (status == EFI_BUFFER_TOO_SMALL) {
276 newnm = realloc(varname, varalloc);
278 printf("Can't allocate memory to get variables\n");
284 continue; /* Try again with bigger buffer */
286 if (status != EFI_SUCCESS)
289 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
294 if (wcscmp(varnamearg, varname) == 0) {
295 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
301 if (memcmp(&varguid, &matchguid, sizeof(varguid)) == 0) {
302 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
314 COMMAND_SET(efiset, "efi-set", "set EFI variables", command_efi_set);
317 command_efi_set(int argc, char *argv[])
319 char *uuid, *var, *val;
326 printf("efi-set uuid var new-value\n");
332 uuid_from_string(uuid, (uuid_t *)&guid, &status);
333 if (status != uuid_s_ok) {
334 printf("Invalid uuid %s %d\n", uuid, status);
337 cpy8to16(var, wvar, sizeof(wvar));
338 err = RS->SetVariable(wvar, &guid,
339 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
340 strlen(val) + 1, val);
341 if (EFI_ERROR(err)) {
342 printf("Failed to set variable: error %lu\n", EFI_ERROR_CODE(err));
348 COMMAND_SET(efiunset, "efi-unset", "delete / unset EFI variables", command_efi_unset);
351 command_efi_unset(int argc, char *argv[])
360 printf("efi-unset uuid var\n");
365 uuid_from_string(uuid, (uuid_t *)&guid, &status);
366 if (status != uuid_s_ok) {
367 printf("Invalid uuid %s\n", uuid);
370 cpy8to16(var, wvar, sizeof(wvar));
371 err = RS->SetVariable(wvar, &guid, 0, 0, NULL);
372 if (EFI_ERROR(err)) {
373 printf("Failed to unset variable: error %lu\n", EFI_ERROR_CODE(err));
381 * FreeBSD's loader interaction words and extras
383 * efi-setenv ( value n name n guid n attr -- 0 | -1)
384 * efi-getenv ( guid n addr n -- addr' n' | -1 )
385 * efi-unsetenv ( name n guid n'' -- )
390 * efi-setenv ( value n name n guid n attr -- 0 | -1)
392 * Set environment variables using the SetVariable EFI runtime service.
394 * Value and guid are passed through in binary form (so guid needs to be
395 * converted to binary form from its string form). Name is converted from
396 * ASCII to CHAR16. Since ficl doesn't have support for internationalization,
397 * there's no native CHAR16 interface provided.
399 * attr is an int in the bitmask of the following attributes for this variable.
402 * 2 Boot service access
404 * (corresponding to the same bits in the UEFI spec).
407 ficlEfiSetenv(FICL_VM *pVM)
409 char *value = NULL, *guid = NULL;
412 char *namep, *valuep, *guidp;
413 int names, values, guids, attr;
420 vmCheckStack(pVM, 6, 0);
422 attr = stackPopINT(pVM->pStack);
423 guids = stackPopINT(pVM->pStack);
424 guidp = (char*)stackPopPtr(pVM->pStack);
425 names = stackPopINT(pVM->pStack);
426 namep = (char*)stackPopPtr(pVM->pStack);
427 values = stackPopINT(pVM->pStack);
428 valuep = (char*)stackPopPtr(pVM->pStack);
430 guid = (char*)ficlMalloc(guids);
433 memcpy(guid, guidp, guids);
434 uuid_from_string(guid, &u, &ustatus);
435 if (ustatus != uuid_s_ok) {
436 stackPushINT(pVM->pStack, -1);
440 name = ficlMalloc((names + 1) * sizeof(CHAR16));
443 for (i = 0; i < names; i++)
447 value = ficlMalloc(values + 1);
450 memcpy(value, valuep, values);
452 status = efi_set_variable(name, (EFI_GUID *)&u, attr, values, value);
453 if (status == EFI_SUCCESS)
454 stackPushINT(pVM->pStack, 0);
456 stackPushINT(pVM->pStack, -1);
464 vmThrowErr(pVM, "Error: out of memory");
468 ficlEfiGetenv(FICL_VM *pVM)
475 vmCheckStack(pVM, 2, 2);
477 names = stackPopINT(pVM->pStack);
478 namep = (char*) stackPopPtr(pVM->pStack);
480 name = (char*) ficlMalloc(names+1);
482 vmThrowErr(pVM, "Error: out of memory");
483 strncpy(name, namep, names);
486 value = getenv(name);
490 stackPushPtr(pVM->pStack, value);
491 stackPushINT(pVM->pStack, strlen(value));
493 stackPushINT(pVM->pStack, -1);
497 ficlEfiUnsetenv(FICL_VM *pVM)
504 vmCheckStack(pVM, 2, 0);
506 names = stackPopINT(pVM->pStack);
507 namep = (char*) stackPopPtr(pVM->pStack);
509 name = (char*) ficlMalloc(names+1);
511 vmThrowErr(pVM, "Error: out of memory");
512 strncpy(name, namep, names);
519 /**************************************************************************
520 ** Add FreeBSD UEFI platform extensions into the system dictionary
521 **************************************************************************/
522 void ficlEfiCompilePlatform(FICL_SYSTEM *pSys)
524 FICL_DICT *dp = pSys->dp;
527 dictAppendWord(dp, "efi-setenv", ficlEfiSetenv, FW_DEFAULT);
528 dictAppendWord(dp, "efi-getenv", ficlEfiGetenv, FW_DEFAULT);
529 dictAppendWord(dp, "efi-unsetenv", ficlEfiUnsetenv, FW_DEFAULT);
532 FICL_COMPILE_SET(ficlEfiCompilePlatform);
534 #endif /* BOOT_FORTH */