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