xref: /illumos-gate/usr/src/boot/efi/libefi/efienv.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * Copyright (c) 2018 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 
27 #include <sys/cdefs.h>
28 
29 #include <stand.h>
30 #include <efi.h>
31 #include <efichar.h>
32 #include <efilib.h>
33 
34 static EFI_GUID illumosBootVarGUID = ILLUMOS_BOOT_VAR_GUID;
35 static EFI_GUID GlobalBootVarGUID = EFI_GLOBAL_VARIABLE;
36 
37 EFI_STATUS
38 efi_getenv(EFI_GUID *g, const char *v, void *data, size_t *len)
39 {
40 	size_t ul;
41 	CHAR16 *uv;
42 	UINT32 attr;
43 	UINTN dl;
44 	EFI_STATUS rv;
45 
46 	uv = NULL;
47 	if (utf8_to_ucs2(v, &uv, &ul) != 0)
48 		return (EFI_OUT_OF_RESOURCES);
49 	dl = *len;
50 	rv = RS->GetVariable(uv, g, &attr, &dl, data);
51 	if (rv == EFI_SUCCESS || rv == EFI_BUFFER_TOO_SMALL)
52 		*len = dl;
53 	free(uv);
54 	return (rv);
55 }
56 
57 EFI_STATUS
58 efi_setenv(EFI_GUID *g, uint32_t attr, const char *v, void *data, size_t len)
59 {
60 	size_t ul;
61 	CHAR16 *uv;
62 	EFI_STATUS rv;
63 
64 	uv = NULL;
65 	if (utf8_to_ucs2(v, &uv, &ul) != 0)
66 		return (EFI_OUT_OF_RESOURCES);
67 	rv = RS->SetVariable(uv, g, attr, len, data);
68 	free(uv);
69 	return (rv);
70 }
71 
72 EFI_STATUS
73 efi_global_getenv(const char *v, void *data, size_t *len)
74 {
75 
76 	return (efi_getenv(&GlobalBootVarGUID, v, data, len));
77 }
78 
79 EFI_STATUS
80 efi_global_setenv(const char *v, void *data, size_t len)
81 {
82 	return (efi_setenv(&GlobalBootVarGUID,
83 	    EFI_VARIABLE_NON_VOLATILE |
84 	    EFI_VARIABLE_BOOTSERVICE_ACCESS |
85 	    EFI_VARIABLE_RUNTIME_ACCESS, v, data, len));
86 }
87 
88 EFI_STATUS
89 efi_illumos_getenv(const char *v, void *data, size_t *len)
90 {
91 
92 	return (efi_getenv(&illumosBootVarGUID, v, data, len));
93 }
94 
95 EFI_STATUS
96 efi_setenv_illumos_wcs(const char *var, CHAR16 *valstr)
97 {
98 	return (efi_setenv(&illumosBootVarGUID,
99 	    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
100 	    var, valstr, (ucs2len(valstr) + 1) * sizeof (CHAR16)));
101 }
102