1 /*
2 * Copyright (c) 2015 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 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <stand.h>
31 #include <string.h>
32 #include <efi.h>
33 #include <efilib.h>
34 #include <uuid.h>
35 #include <stdbool.h>
36 #include "bootstrap.h"
37
38 void
efi_init_environment(void)39 efi_init_environment(void)
40 {
41 char var[128];
42
43 snprintf(var, sizeof(var), "%d.%02d", ST->Hdr.Revision >> 16,
44 ST->Hdr.Revision & 0xffff);
45 env_setenv("efi-version", EV_VOLATILE, var, env_noset, env_nounset);
46 }
47
48 COMMAND_SET(efishow, "efi-show", "print some or all EFI variables", command_efi_show);
49
50 const char *
efi_memory_type(EFI_MEMORY_TYPE type)51 efi_memory_type(EFI_MEMORY_TYPE type)
52 {
53 const char *types[] = {
54 "Reserved",
55 "LoaderCode",
56 "LoaderData",
57 "BootServicesCode",
58 "BootServicesData",
59 "RuntimeServicesCode",
60 "RuntimeServicesData",
61 "ConventionalMemory",
62 "UnusableMemory",
63 "ACPIReclaimMemory",
64 "ACPIMemoryNVS",
65 "MemoryMappedIO",
66 "MemoryMappedIOPortSpace",
67 "PalCode",
68 "PersistentMemory"
69 };
70
71 switch (type) {
72 case EfiReservedMemoryType:
73 case EfiLoaderCode:
74 case EfiLoaderData:
75 case EfiBootServicesCode:
76 case EfiBootServicesData:
77 case EfiRuntimeServicesCode:
78 case EfiRuntimeServicesData:
79 case EfiConventionalMemory:
80 case EfiUnusableMemory:
81 case EfiACPIReclaimMemory:
82 case EfiACPIMemoryNVS:
83 case EfiMemoryMappedIO:
84 case EfiMemoryMappedIOPortSpace:
85 case EfiPalCode:
86 case EfiPersistentMemory:
87 return (types[type]);
88 default:
89 return ("Unknown");
90 }
91 }
92
93 static int
efi_print_var(CHAR16 * varnamearg,EFI_GUID * matchguid,int lflag)94 efi_print_var(CHAR16 *varnamearg, EFI_GUID *matchguid, int lflag)
95 {
96 UINTN datasz, i;
97 EFI_STATUS status;
98 UINT32 attr;
99 CHAR16 *data;
100 char *str;
101 uint32_t uuid_status;
102 int is_ascii;
103
104 datasz = 0;
105 status = RS->GetVariable(varnamearg, matchguid, &attr,
106 &datasz, NULL);
107 if (status != EFI_BUFFER_TOO_SMALL) {
108 printf("Can't get the variable: error %#lx\n",
109 EFI_ERROR_CODE(status));
110 return (CMD_ERROR);
111 }
112 data = malloc(datasz);
113 status = RS->GetVariable(varnamearg, matchguid, &attr,
114 &datasz, data);
115 if (status != EFI_SUCCESS) {
116 printf("Can't get the variable: error %#lx\n",
117 EFI_ERROR_CODE(status));
118 return (CMD_ERROR);
119 }
120 uuid_to_string((uuid_t *)matchguid, &str, &uuid_status);
121 if (lflag) {
122 printf("%s 0x%x %S", str, attr, varnamearg);
123 } else {
124 printf("%s 0x%x %S=", str, attr, varnamearg);
125 is_ascii = 1;
126 free(str);
127 str = (char *)data;
128 for (i = 0; i < datasz - 1; i++) {
129 /* Quick hack to see if this ascii-ish string printable range plus tab, cr and lf */
130 if ((str[i] < 32 || str[i] > 126) && str[i] != 9 && str[i] != 10 && str[i] != 13) {
131 is_ascii = 0;
132 break;
133 }
134 }
135 if (str[datasz - 1] != '\0')
136 is_ascii = 0;
137 if (is_ascii)
138 printf("%s", str);
139 else {
140 for (i = 0; i < datasz / 2; i++) {
141 if (isalnum(data[i]) || isspace(data[i]))
142 printf("%c", data[i]);
143 else
144 printf("\\x%02x", data[i]);
145 }
146 }
147 }
148 free(data);
149 if (pager_output("\n"))
150 return (CMD_WARN);
151 return (CMD_OK);
152 }
153
154 static int
command_efi_show(int argc,char * argv[])155 command_efi_show(int argc, char *argv[])
156 {
157 /*
158 * efi-show [-a]
159 * print all the env
160 * efi-show -g UUID
161 * print all the env vars tagged with UUID
162 * efi-show -v var
163 * search all the env vars and print the ones matching var
164 * efi-show -g UUID -v var
165 * efi-show UUID var
166 * print all the env vars that match UUID and var
167 */
168 /* NB: We assume EFI_GUID is the same as uuid_t */
169 int aflag = 0, gflag = 0, lflag = 0, vflag = 0;
170 int ch, rv;
171 unsigned i;
172 EFI_STATUS status;
173 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
174 EFI_GUID matchguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
175 uint32_t uuid_status;
176 CHAR16 *varname;
177 CHAR16 *newnm;
178 CHAR16 varnamearg[128];
179 UINTN varalloc;
180 UINTN varsz;
181
182 while ((ch = getopt(argc, argv, "ag:lv:")) != -1) {
183 switch (ch) {
184 case 'a':
185 aflag = 1;
186 break;
187 case 'g':
188 gflag = 1;
189 uuid_from_string(optarg, (uuid_t *)&matchguid,
190 &uuid_status);
191 if (uuid_status != uuid_s_ok) {
192 printf("uid %s could not be parsed\n", optarg);
193 return (CMD_ERROR);
194 }
195 break;
196 case 'l':
197 lflag = 1;
198 break;
199 case 'v':
200 vflag = 1;
201 if (strlen(optarg) >= nitems(varnamearg)) {
202 printf("Variable %s is longer than %zd characters\n",
203 optarg, nitems(varnamearg));
204 return (CMD_ERROR);
205 }
206 for (i = 0; i < strlen(optarg); i++)
207 varnamearg[i] = optarg[i];
208 varnamearg[i] = 0;
209 break;
210 default:
211 printf("Invalid argument %c\n", ch);
212 return (CMD_ERROR);
213 }
214 }
215
216 if (aflag && (gflag || vflag)) {
217 printf("-a isn't compatible with -v or -u\n");
218 return (CMD_ERROR);
219 }
220
221 if (aflag && optind < argc) {
222 printf("-a doesn't take any args\n");
223 return (CMD_ERROR);
224 }
225
226 if (optind == argc)
227 aflag = 1;
228
229 argc -= optind;
230 argv += optind;
231
232 pager_open();
233 if (vflag && gflag) {
234 rv = efi_print_var(varnamearg, &matchguid, lflag);
235 pager_close();
236 return (rv);
237 }
238
239 if (argc == 2) {
240 optarg = argv[0];
241 if (strlen(optarg) >= nitems(varnamearg)) {
242 printf("Variable %s is longer than %zd characters\n",
243 optarg, nitems(varnamearg));
244 pager_close();
245 return (CMD_ERROR);
246 }
247 for (i = 0; i < strlen(optarg); i++)
248 varnamearg[i] = optarg[i];
249 varnamearg[i] = 0;
250 optarg = argv[1];
251 uuid_from_string(optarg, (uuid_t *)&matchguid,
252 &uuid_status);
253 if (uuid_status != uuid_s_ok) {
254 printf("uid %s could not be parsed\n", optarg);
255 pager_close();
256 return (CMD_ERROR);
257 }
258 rv = efi_print_var(varnamearg, &matchguid, lflag);
259 pager_close();
260 return (rv);
261 }
262
263 if (argc > 0) {
264 printf("Too many args %d\n", argc);
265 pager_close();
266 return (CMD_ERROR);
267 }
268
269 /*
270 * Initiate the search -- note the standard takes pain
271 * to specify the initial call must be a poiner to a NULL
272 * character.
273 */
274 varalloc = 1024;
275 varname = malloc(varalloc);
276 if (varname == NULL) {
277 printf("Can't allocate memory to get variables\n");
278 pager_close();
279 return (CMD_ERROR);
280 }
281 varname[0] = 0;
282 while (1) {
283 varsz = varalloc;
284 status = RS->GetNextVariableName(&varsz, varname, &varguid);
285 if (status == EFI_BUFFER_TOO_SMALL) {
286 varalloc = varsz;
287 newnm = realloc(varname, varalloc);
288 if (newnm == NULL) {
289 printf("Can't allocate memory to get variables\n");
290 free(varname);
291 pager_close();
292 return (CMD_ERROR);
293 }
294 varname = newnm;
295 continue; /* Try again with bigger buffer */
296 }
297 if (status != EFI_SUCCESS)
298 break;
299 if (aflag) {
300 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
301 break;
302 continue;
303 }
304 if (vflag) {
305 if (wcscmp(varnamearg, varname) == 0) {
306 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
307 break;
308 continue;
309 }
310 }
311 if (gflag) {
312 if (memcmp(&varguid, &matchguid, sizeof(varguid)) == 0) {
313 if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
314 break;
315 continue;
316 }
317 }
318 }
319 free(varname);
320 pager_close();
321
322 return (CMD_OK);
323 }
324
325 COMMAND_SET(efiset, "efi-set", "set EFI variables", command_efi_set);
326
327 static int
command_efi_set(int argc,char * argv[])328 command_efi_set(int argc, char *argv[])
329 {
330 char *uuid, *var, *val;
331 CHAR16 wvar[128];
332 EFI_GUID guid;
333 uint32_t status;
334 EFI_STATUS err;
335
336 if (argc != 4) {
337 printf("efi-set uuid var new-value\n");
338 return (CMD_ERROR);
339 }
340 uuid = argv[1];
341 var = argv[2];
342 val = argv[3];
343 uuid_from_string(uuid, (uuid_t *)&guid, &status);
344 if (status != uuid_s_ok) {
345 printf("Invalid uuid %s %d\n", uuid, status);
346 return (CMD_ERROR);
347 }
348 cpy8to16(var, wvar, sizeof(wvar));
349 err = RS->SetVariable(wvar, &guid,
350 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
351 strlen(val) + 1, val);
352 if (EFI_ERROR(err)) {
353 printf("Failed to set variable: error %lu\n", EFI_ERROR_CODE(err));
354 return (CMD_ERROR);
355 }
356 return (CMD_OK);
357 }
358
359 COMMAND_SET(efiunset, "efi-unset", "delete / unset EFI variables", command_efi_unset);
360
361 static int
command_efi_unset(int argc,char * argv[])362 command_efi_unset(int argc, char *argv[])
363 {
364 char *uuid, *var;
365 CHAR16 wvar[128];
366 EFI_GUID guid;
367 uint32_t status;
368 EFI_STATUS err;
369
370 if (argc != 3) {
371 printf("efi-unset uuid var\n");
372 return (CMD_ERROR);
373 }
374 uuid = argv[1];
375 var = argv[2];
376 uuid_from_string(uuid, (uuid_t *)&guid, &status);
377 if (status != uuid_s_ok) {
378 printf("Invalid uuid %s\n", uuid);
379 return (CMD_ERROR);
380 }
381 cpy8to16(var, wvar, sizeof(wvar));
382 err = RS->SetVariable(wvar, &guid, 0, 0, NULL);
383 if (EFI_ERROR(err)) {
384 printf("Failed to unset variable: error %lu\n", EFI_ERROR_CODE(err));
385 return (CMD_ERROR);
386 }
387 return (CMD_OK);
388 }
389