xref: /freebsd/stand/efi/libefi/efienv.c (revision 8f76bb7dad48538c6832c2fb466a433d2a3f8cd5)
1 /*-
2  * Copyright (c) 2018 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 #include <stand.h>
28 #include <efi.h>
29 #include <efichar.h>
30 #include <efilib.h>
31 
32 static EFI_GUID FreeBSDBootVarGUID = FREEBSD_BOOT_VAR_GUID;
33 static EFI_GUID GlobalBootVarGUID = EFI_GLOBAL_VARIABLE;
34 
35 EFI_STATUS
36 efi_getenv(EFI_GUID *g, const char *v, void *data, size_t *len)
37 {
38 	size_t ul;
39 	CHAR16 *uv;
40 	UINT32 attr;
41 	UINTN dl;
42 	EFI_STATUS rv;
43 
44 	uv = NULL;
45 	if (utf8_to_ucs2(v, &uv, &ul) != 0)
46 		return (EFI_OUT_OF_RESOURCES);
47 	dl = *len;
48 	rv = RS->GetVariable(uv, g, &attr, &dl, data);
49 	if (rv == EFI_SUCCESS || rv == EFI_BUFFER_TOO_SMALL)
50 		*len = dl;
51 	free(uv);
52 	return (rv);
53 }
54 
55 EFI_STATUS
56 efi_global_getenv(const char *v, void *data, size_t *len)
57 {
58 
59 	return (efi_getenv(&GlobalBootVarGUID, v, data, len));
60 }
61 
62 EFI_STATUS
63 efi_freebsd_getenv(const char *v, void *data, size_t *len)
64 {
65 
66 	return (efi_getenv(&FreeBSDBootVarGUID, v, data, len));
67 }
68 
69 /*
70  * efi_setenv -- Sets an env variable.
71  */
72 EFI_STATUS
73 efi_setenv(EFI_GUID *guid, const char *varname, UINT32 attr, void *data, __size_t len)
74 {
75 	EFI_STATUS rv;
76 	CHAR16 *uv;
77 	size_t ul;
78 
79 	uv = NULL;
80 	if (utf8_to_ucs2(varname, &uv, &ul) != 0)
81 		return (EFI_OUT_OF_RESOURCES);
82 
83 	rv = RS->SetVariable(uv, guid, attr, len, data);
84 	free(uv);
85 	return (rv);
86 }
87 
88 EFI_STATUS
89 efi_setenv_freebsd_wcs(const char *varname, CHAR16 *valstr)
90 {
91 	CHAR16 *var = NULL;
92 	size_t len;
93 	EFI_STATUS rv;
94 
95 	if (utf8_to_ucs2(varname, &var, &len) != 0)
96 		return (EFI_OUT_OF_RESOURCES);
97 	rv = RS->SetVariable(var, &FreeBSDBootVarGUID,
98 	    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
99 	    (ucs2len(valstr) + 1) * sizeof(efi_char), valstr);
100 	free(var);
101 	return (rv);
102 }
103 
104 /*
105  * efi_delenv -- deletes the specified env variable
106  */
107 EFI_STATUS
108 efi_delenv(EFI_GUID *guid, const char *name)
109 {
110 	CHAR16 *var;
111 	size_t len;
112 	EFI_STATUS rv;
113 
114 	var = NULL;
115 	if (utf8_to_ucs2(name, &var, &len) != 0)
116 		return (EFI_OUT_OF_RESOURCES);
117 
118 	rv = RS->SetVariable(var, guid, 0, 0, NULL);
119 	free(var);
120 	return (rv);
121 }
122 
123 EFI_STATUS
124 efi_freebsd_delenv(const char *name)
125 {
126 	return (efi_delenv(&FreeBSDBootVarGUID, name));
127 }
128