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$");
34 #include "bootstrap.h"
37 int efi_variable_support = 1;
40 * Simple wrappers to the underlying UEFI functions.
41 * See http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES
45 efi_get_next_variable_name(UINTN *variable_name_size, CHAR16 *variable_name, EFI_GUID *vendor_guid)
47 return RS->GetNextVariableName(variable_name_size, variable_name, vendor_guid);
51 efi_get_variable(CHAR16 *variable_name, EFI_GUID *vendor_guid, UINT32 *attributes, UINTN *data_size,
54 return RS->GetVariable(variable_name, vendor_guid, attributes, data_size, data);
58 efi_set_variable(CHAR16 *variable_name, EFI_GUID *vendor_guid, UINT32 attributes, UINTN data_size,
61 return RS->SetVariable(variable_name, vendor_guid, attributes, data_size, data);
65 * FreeBSD's loader interaction words and extras
67 * efi-setenv ( value n name n guid n attr -- 0 | -1)
68 * efi-getenv ( guid n addr n -- addr' n' | -1 )
69 * efi-unsetenv ( name n guid n'' -- )
74 * efi-setenv ( value n name n guid n attr -- 0 | -1)
76 * Set environment variables using the SetVariable EFI runtime service.
78 * Value and guid are passed through in binary form (so guid needs to be
79 * converted to binary form from its string form). Name is converted from
80 * ASCII to CHAR16. Since ficl doesn't have support for internationalization,
81 * there's no native CHAR16 interface provided.
83 * attr is an int in the bitmask of the following attributes for this variable.
86 * 2 Boot service access
88 * (corresponding to the same bits in the UEFI spec).
91 ficlEfiSetenv(FICL_VM *pVM)
94 char *value = NULL, *guid = NULL;
98 char *namep, *valuep, *guidp;
99 int names, values, guids, attr;
105 vmCheckStack(pVM, 6, 0);
107 attr = stackPopINT(pVM->pStack);
108 guids = stackPopINT(pVM->pStack);
109 guidp = (char*)stackPopPtr(pVM->pStack);
110 names = stackPopINT(pVM->pStack);
111 namep = (char*)stackPopPtr(pVM->pStack);
112 values = stackPopINT(pVM->pStack);
113 valuep = (char*)stackPopPtr(pVM->pStack);
116 guid = (char*)ficlMalloc(guids);
118 vmThrowErr(pVM, "Error: out of memory");
119 memcpy(guid, guidp, guids);
120 uuid_from_string(guid, &u, &ustatus);
121 if (ustatus != uuid_s_ok) {
122 stackPushINT(pVM->pStack, -1);
126 name = (CHAR16 *)ficlMalloc((names + 1) * sizeof(CHAR16));
128 vmThrowErr(pVM, "Error: out of memory");
129 for (i = 0; i < names; i++)
131 name[names] = (CHAR16)0;
133 value = (char*)ficlMalloc(values + 1);
135 vmThrowErr(pVM, "Error: out of memory");
136 memcpy(value, valuep, values);
138 status = efi_set_variable(name, (EFI_GUID *)&u, attr, values, value);
139 if (status == EFI_SUCCESS)
140 stackPushINT(pVM->pStack, 0);
142 stackPushINT(pVM->pStack, -1);
153 ficlEfiGetenv(FICL_VM *pVM)
162 vmCheckStack(pVM, 2, 2);
164 names = stackPopINT(pVM->pStack);
165 namep = (char*) stackPopPtr(pVM->pStack);
168 name = (char*) ficlMalloc(names+1);
170 vmThrowErr(pVM, "Error: out of memory");
171 strncpy(name, namep, names);
174 value = getenv(name);
178 stackPushPtr(pVM->pStack, value);
179 stackPushINT(pVM->pStack, strlen(value));
182 stackPushINT(pVM->pStack, -1);
188 ficlEfiUnsetenv(FICL_VM *pVM)
197 vmCheckStack(pVM, 2, 0);
199 names = stackPopINT(pVM->pStack);
200 namep = (char*) stackPopPtr(pVM->pStack);
203 name = (char*) ficlMalloc(names+1);
205 vmThrowErr(pVM, "Error: out of memory");
206 strncpy(name, namep, names);
216 /**************************************************************************
217 ** Add FreeBSD UEFI platform extensions into the system dictionary
218 **************************************************************************/
219 void ficlEfiCompilePlatform(FICL_SYSTEM *pSys)
221 FICL_DICT *dp = pSys->dp;
224 dictAppendWord(dp, "efi-setenv", ficlEfiSetenv, FW_DEFAULT);
225 dictAppendWord(dp, "efi-getenv", ficlEfiGetenv, FW_DEFAULT);
226 dictAppendWord(dp, "efi-unsetenv", ficlEfiUnsetenv, FW_DEFAULT);
228 /* Would like to export the EFI version, but this will do for now */
229 ficlSetEnv(pSys, "efi-boot", 1);
234 FICL_COMPILE_SET(ficlEfiCompilePlatform);