2 * Copyright (c) 2014 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
29 /*******************************************************************
31 ** Additional words for EFI
33 *******************************************************************/
36 #include <sys/types.h>
46 #include "bootstrap.h"
51 * FreeBSD's loader interaction words and extras
53 * efi-setenv ( value n name n guid n attr -- 0 | -1)
54 * efi-getenv ( guid n addr n -- addr' n' | -1 )
55 * efi-unsetenv ( name n guid n'' -- )
60 * efi-setenv ( value n name n guid n attr -- 0 | -1)
62 * Set environment variables using the SetVariable EFI runtime service.
64 * Value and guid are passed through in binary form (so guid needs to be
65 * converted to binary form from its string form). Name is converted from
66 * ASCII to CHAR16. Since ficl doesn't have support for internationalization,
67 * there's no native CHAR16 interface provided.
69 * attr is an int in the bitmask of the following attributes for this variable.
72 * 2 Boot service access
74 * (corresponding to the same bits in the UEFI spec).
77 ficlEfiSetenv(FICL_VM *pVM)
84 char *namep, *valuep, *guidp;
85 int names, values, guids, attr;
88 vmCheckStack(pVM, 6, 0);
90 attr = stackPopINT(pVM->pStack);
91 guids = stackPopINT(pVM->pStack);
92 guidp = (char*)stackPopPtr(pVM->pStack);
93 names = stackPopINT(pVM->pStack);
94 namep = (char*)stackPopPtr(pVM->pStack);
95 values = stackPopINT(pVM->pStack);
96 valuep = (char*)stackPopPtr(pVM->pStack);
99 guid = (char*)ficlMalloc(guids);
101 vmThrowErr(pVM, "Error: out of memory");
102 memcpy(guid, guidp, guids);
104 name = (char*)ficlMalloc((names + 1) * sizeof(CHAR16));
106 vmThrowErr(pVM, "Error: out of memory");
107 for (i = 0; i < names; i++)
109 name[names] = (CHAR16)0;
111 value = (char*)ficlMalloc(values + 1);
113 vmThrowErr(pVM, "Error: out of memory");
114 memcpy(value, valuep, values);
116 status = efi_set_variable(name, guid, attr, value);
117 if (status == EFI_SUCCESS)
118 stackPushINT(pVM->pStack, 0);
120 stackPushINT(pVM->pStack, -1);
131 ficlEfiGetenv(FICL_VM *pVM)
140 vmCheckStack(pVM, 2, 2);
142 names = stackPopINT(pVM->pStack);
143 namep = (char*) stackPopPtr(pVM->pStack);
146 name = (char*) ficlMalloc(names+1);
148 vmThrowErr(pVM, "Error: out of memory");
149 strncpy(name, namep, names);
152 value = getenv(name);
156 stackPushPtr(pVM->pStack, value);
157 stackPushINT(pVM->pStack, strlen(value));
160 stackPushINT(pVM->pStack, -1);
166 ficlEfiUnsetenv(FICL_VM *pVM)
175 vmCheckStack(pVM, 2, 0);
177 names = stackPopINT(pVM->pStack);
178 namep = (char*) stackPopPtr(pVM->pStack);
181 name = (char*) ficlMalloc(names+1);
183 vmThrowErr(pVM, "Error: out of memory");
184 strncpy(name, namep, names);
193 /**************************************************************************
195 ** Build FreeBSD platform extensions into the system dictionary
196 **************************************************************************/
197 void ficlEfiCompilePlatform(FICL_SYSTEM *pSys)
199 FICL_DICT *dp = pSys->dp;
202 dictAppendWord(dp, "efi-setenv", ficlEfiSetenv, FW_DEFAULT);
203 dictAppendWord(dp, "efi-getenv", ficlEfiGetenv, FW_DEFAULT);
204 dictAppendWord(dp, "efi-unsetenv", ficlEfiUnsetenv, FW_DEFAULT);