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
efi_getenv(EFI_GUID * g,const char * v,void * data,size_t * len)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)
52 		*len = dl;
53 	free(uv);
54 	return (rv);
55 }
56 
57 EFI_STATUS
efi_global_getenv(const char * v,void * data,size_t * len)58 efi_global_getenv(const char *v, void *data, size_t *len)
59 {
60 
61 	return (efi_getenv(&GlobalBootVarGUID, v, data, len));
62 }
63 
64 EFI_STATUS
efi_illumos_getenv(const char * v,void * data,size_t * len)65 efi_illumos_getenv(const char *v, void *data, size_t *len)
66 {
67 
68 	return (efi_getenv(&illumosBootVarGUID, v, data, len));
69 }
70 
71 EFI_STATUS
efi_setenv_illumos_wcs(const char * varname,CHAR16 * valstr)72 efi_setenv_illumos_wcs(const char *varname, CHAR16 *valstr)
73 {
74 	CHAR16 *var = NULL;
75 	size_t len;
76 	EFI_STATUS rv;
77 
78 	if (utf8_to_ucs2(varname, &var, &len) != 0)
79 		return (EFI_OUT_OF_RESOURCES);
80 	rv = RS->SetVariable(var, &illumosBootVarGUID,
81 	    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
82 	    (ucs2len(valstr) + 1) * sizeof (CHAR16), valstr);
83 	free(var);
84 	return (rv);
85 }
86