]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/ficl/efi.c
MFC r303555,r303556,r303936,r303962,r304317,r304532,r305026,r305107,r305132,
[FreeBSD/FreeBSD.git] / sys / boot / ficl / efi.c
1 /*-
2  * Copyright (c) 2014 Netflix, Inc
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD$
27  */
28
29 /*******************************************************************
30 ** e f i . c
31 ** Additional words for EFI
32 ** 
33 *******************************************************************/
34
35 #ifdef TESTMAIN
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <dirent.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #else
44 #include <stand.h>
45 #endif
46 #include "bootstrap.h"
47 #include <string.h>
48 #include "ficl.h"
49
50 /*
51  *              FreeBSD's loader interaction words and extras
52  *
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'' -- )
56  */
57
58 /*
59  * efi-setenv
60  *              efi-setenv  ( value n name n guid n attr -- 0 | -1)
61  *
62  * Set environment variables using the SetVariable EFI runtime service.
63  *
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.
68  *
69  * attr is an int in the bitmask of the following attributes for this variable.
70  *
71  *      1       Non volatile
72  *      2       Boot service access
73  *      4       Run time access
74  * (corresponding to the same bits in the UEFI spec).
75  */
76 void
77 ficlEfiSetenv(FICL_VM *pVM)
78 {
79 #ifndef TESTMAIN
80         char    *value, *guid;
81         CHAR16  *name
82         int     i;
83 #endif
84         char    *namep, *valuep, *guidp;
85         int     names, values, guids, attr;
86
87 #if FICL_ROBUST > 1
88         vmCheckStack(pVM, 6, 0);
89 #endif
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);
97
98 #ifndef TESTMAIN
99         guid = (char*)ficlMalloc(guids);
100         if (guid != NULL)
101                 vmThrowErr(pVM, "Error: out of memory");
102         memcpy(guid, guidp, guids);
103
104         name = (char*)ficlMalloc((names + 1) * sizeof(CHAR16));
105         if (name == NULL)
106                 vmThrowErr(pVM, "Error: out of memory");
107         for (i = 0; i < names; i++)
108                 name[i] = namep[i];
109         name[names] = (CHAR16)0;
110
111         value = (char*)ficlMalloc(values + 1);
112         if (value != NULL)
113                 vmThrowErr(pVM, "Error: out of memory");
114         memcpy(value, valuep, values);
115
116         status = efi_set_variable(name, guid, attr, value);
117         if (status == EFI_SUCCESS)
118                 stackPushINT(pVM->pStack, 0);
119         else
120                 stackPushINT(pVM->pStack, -1);
121
122         ficlFree(name);
123         ficlFree(value);
124         ficlFree(guid);
125 #endif
126
127         return;
128 }
129
130 void
131 ficlEfiGetenv(FICL_VM *pVM)
132 {
133 #ifndef TESTMAIN
134         char    *name, *value;
135 #endif
136         char    *namep;
137         int     names;
138
139 #if FICL_ROBUST > 1
140         vmCheckStack(pVM, 2, 2);
141 #endif
142         names = stackPopINT(pVM->pStack);
143         namep = (char*) stackPopPtr(pVM->pStack);
144
145 #ifndef TESTMAIN
146         name = (char*) ficlMalloc(names+1);
147         if (!name)
148                 vmThrowErr(pVM, "Error: out of memory");
149         strncpy(name, namep, names);
150         name[names] = '\0';
151
152         value = getenv(name);
153         ficlFree(name);
154
155         if(value != NULL) {
156                 stackPushPtr(pVM->pStack, value);
157                 stackPushINT(pVM->pStack, strlen(value));
158         } else
159 #endif
160                 stackPushINT(pVM->pStack, -1);
161
162         return;
163 }
164
165 void
166 ficlEfiUnsetenv(FICL_VM *pVM)
167 {
168 #ifndef TESTMAIN
169         char    *name;
170 #endif
171         char    *namep;
172         int     names;
173
174 #if FICL_ROBUST > 1
175         vmCheckStack(pVM, 2, 0);
176 #endif
177         names = stackPopINT(pVM->pStack);
178         namep = (char*) stackPopPtr(pVM->pStack);
179
180 #ifndef TESTMAIN
181         name = (char*) ficlMalloc(names+1);
182         if (!name)
183                 vmThrowErr(pVM, "Error: out of memory");
184         strncpy(name, namep, names);
185         name[names] = '\0';
186
187         unsetenv(name);
188         ficlFree(name);
189 #endif
190
191         return;
192 }
193 /**************************************************************************
194
195 ** Build FreeBSD platform extensions into the system dictionary
196 **************************************************************************/
197 void ficlEfiCompilePlatform(FICL_SYSTEM *pSys)
198 {
199     FICL_DICT *dp = pSys->dp;
200     assert (dp);
201
202     dictAppendWord(dp, "efi-setenv",    ficlEfiSetenv,      FW_DEFAULT);
203     dictAppendWord(dp, "efi-getenv",    ficlEfiGetenv,      FW_DEFAULT);
204     dictAppendWord(dp, "efi-unsetenv",  ficlEfiUnsetenv,    FW_DEFAULT);
205
206     return;
207 }