1*8dff7262SToomas Soome /*
2*8dff7262SToomas Soome  * Copyright (c) 2018 Netflix, Inc.
3*8dff7262SToomas Soome  * All rights reserved.
4*8dff7262SToomas Soome  *
5*8dff7262SToomas Soome  * Redistribution and use in source and binary forms, with or without
6*8dff7262SToomas Soome  * modification, are permitted provided that the following conditions
7*8dff7262SToomas Soome  * are met:
8*8dff7262SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*8dff7262SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*8dff7262SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*8dff7262SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*8dff7262SToomas Soome  *    documentation and/or other materials provided with the distribution.
13*8dff7262SToomas Soome  *
14*8dff7262SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*8dff7262SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*8dff7262SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*8dff7262SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*8dff7262SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*8dff7262SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*8dff7262SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*8dff7262SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*8dff7262SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*8dff7262SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*8dff7262SToomas Soome  * SUCH DAMAGE.
25*8dff7262SToomas Soome  */
26*8dff7262SToomas Soome 
27*8dff7262SToomas Soome #include <sys/cdefs.h>
28*8dff7262SToomas Soome 
29*8dff7262SToomas Soome #include <stand.h>
30*8dff7262SToomas Soome #include <efi.h>
31*8dff7262SToomas Soome #include <efichar.h>
32*8dff7262SToomas Soome #include <efilib.h>
33*8dff7262SToomas Soome 
34*8dff7262SToomas Soome static EFI_GUID illumosBootVarGUID = ILLUMOS_BOOT_VAR_GUID;
35*8dff7262SToomas Soome static EFI_GUID GlobalBootVarGUID = EFI_GLOBAL_VARIABLE;
36*8dff7262SToomas Soome 
37*8dff7262SToomas Soome EFI_STATUS
efi_getenv(EFI_GUID * g,const char * v,void * data,size_t * len)38*8dff7262SToomas Soome efi_getenv(EFI_GUID *g, const char *v, void *data, size_t *len)
39*8dff7262SToomas Soome {
40*8dff7262SToomas Soome 	size_t ul;
41*8dff7262SToomas Soome 	CHAR16 *uv;
42*8dff7262SToomas Soome 	UINT32 attr;
43*8dff7262SToomas Soome 	UINTN dl;
44*8dff7262SToomas Soome 	EFI_STATUS rv;
45*8dff7262SToomas Soome 
46*8dff7262SToomas Soome 	uv = NULL;
47*8dff7262SToomas Soome 	if (utf8_to_ucs2(v, &uv, &ul) != 0)
48*8dff7262SToomas Soome 		return (EFI_OUT_OF_RESOURCES);
49*8dff7262SToomas Soome 	dl = *len;
50*8dff7262SToomas Soome 	rv = RS->GetVariable(uv, g, &attr, &dl, data);
51*8dff7262SToomas Soome 	if (rv == EFI_SUCCESS)
52*8dff7262SToomas Soome 		*len = dl;
53*8dff7262SToomas Soome 	free(uv);
54*8dff7262SToomas Soome 	return (rv);
55*8dff7262SToomas Soome }
56*8dff7262SToomas Soome 
57*8dff7262SToomas Soome EFI_STATUS
efi_global_getenv(const char * v,void * data,size_t * len)58*8dff7262SToomas Soome efi_global_getenv(const char *v, void *data, size_t *len)
59*8dff7262SToomas Soome {
60*8dff7262SToomas Soome 
61*8dff7262SToomas Soome 	return (efi_getenv(&GlobalBootVarGUID, v, data, len));
62*8dff7262SToomas Soome }
63*8dff7262SToomas Soome 
64*8dff7262SToomas Soome EFI_STATUS
efi_illumos_getenv(const char * v,void * data,size_t * len)65*8dff7262SToomas Soome efi_illumos_getenv(const char *v, void *data, size_t *len)
66*8dff7262SToomas Soome {
67*8dff7262SToomas Soome 
68*8dff7262SToomas Soome 	return (efi_getenv(&illumosBootVarGUID, v, data, len));
69*8dff7262SToomas Soome }
70*8dff7262SToomas Soome 
71*8dff7262SToomas Soome EFI_STATUS
efi_setenv_illumos_wcs(const char * varname,CHAR16 * valstr)72*8dff7262SToomas Soome efi_setenv_illumos_wcs(const char *varname, CHAR16 *valstr)
73*8dff7262SToomas Soome {
74*8dff7262SToomas Soome 	CHAR16 *var = NULL;
75*8dff7262SToomas Soome 	size_t len;
76*8dff7262SToomas Soome 	EFI_STATUS rv;
77*8dff7262SToomas Soome 
78*8dff7262SToomas Soome 	if (utf8_to_ucs2(varname, &var, &len) != 0)
79*8dff7262SToomas Soome 		return (EFI_OUT_OF_RESOURCES);
80*8dff7262SToomas Soome 	rv = RS->SetVariable(var, &illumosBootVarGUID,
81*8dff7262SToomas Soome 	    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
82*8dff7262SToomas Soome 	    (ucs2len(valstr) + 1) * sizeof (CHAR16), valstr);
83*8dff7262SToomas Soome 	free(var);
84*8dff7262SToomas Soome 	return (rv);
85*8dff7262SToomas Soome }
86