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