Introduce libefi -- a library around EFI runtime services and protocols.
This first commit brings 3 functions for enumerating, retrieving, adding, removing and modifying EFI variables. The immediate use of these include the insertion of a new boot option as part of the installation process. This library uses ioctl(2) requests implemented by io(4) to pass the requests down through the kernel to EFI. These ioctl requests are only implemented on ia64, so libefi is currently only enabled on ia64. The interface is generic and io(4) on mad64/i386 can easily be taught to handle these once EFI support has been added to the kernel there.
This commit is contained in:
parent
9dd3fbb0f2
commit
eb0a7e4130
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=203181
@ -33,8 +33,8 @@ SUBDIR= ${_csu} libc libbsm libauditd libcom_err libcrypt libelf libkvm msun \
|
|||||||
libtacplus libutil ${_libypclnt} libalias libarchive \
|
libtacplus libutil ${_libypclnt} libalias libarchive \
|
||||||
${_libatm} libbegemot ${_libbluetooth} ${_libbsnmp} libbz2 \
|
${_libatm} libbegemot ${_libbluetooth} ${_libbsnmp} libbz2 \
|
||||||
libcalendar libcam libcompat libdevinfo libdevstat libdisk \
|
libcalendar libcam libcompat libdevinfo libdevstat libdisk \
|
||||||
libdwarf libedit libexpat libfetch libftpio libgeom ${_libgpib} \
|
libdwarf libedit ${_libefi} libexpat libfetch libftpio libgeom \
|
||||||
${_libgssapi} ${_librpcsec_gss} libipsec \
|
${_libgpib} ${_libgssapi} ${_librpcsec_gss} libipsec \
|
||||||
${_libipx} libjail libkiconv libmagic libmemstat ${_libmilter} \
|
${_libipx} libjail libkiconv libmagic libmemstat ${_libmilter} \
|
||||||
${_libmp} ${_libncp} ${_libngatm} libopie libpam libpcap \
|
${_libmp} ${_libncp} ${_libngatm} libopie libpam libpcap \
|
||||||
${_libpmc} libproc librt ${_libsdp} ${_libsm} ${_libsmb} \
|
${_libpmc} libproc librt ${_libsdp} ${_libsm} ${_libsmb} \
|
||||||
@ -104,6 +104,7 @@ _libvgl= libvgl
|
|||||||
.endif
|
.endif
|
||||||
|
|
||||||
.if ${MACHINE_ARCH} == "ia64"
|
.if ${MACHINE_ARCH} == "ia64"
|
||||||
|
_libefi= libefi
|
||||||
_libsmb= libsmb
|
_libsmb= libsmb
|
||||||
.endif
|
.endif
|
||||||
|
|
||||||
|
22
lib/libefi/Makefile
Normal file
22
lib/libefi/Makefile
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
.include <bsd.own.mk>
|
||||||
|
|
||||||
|
LIB= efi
|
||||||
|
SHLIB_MAJOR= 1
|
||||||
|
|
||||||
|
SRCS= libefi.c \
|
||||||
|
efi_getvar.c \
|
||||||
|
efi_nextvarname.c \
|
||||||
|
efi_setvar.c
|
||||||
|
|
||||||
|
CFLAGS+= -I${.CURDIR}
|
||||||
|
|
||||||
|
INCS= libefi.h
|
||||||
|
|
||||||
|
MAN+= libefi.3
|
||||||
|
MLINKS+=libefi.3 efi_getvar.3 \
|
||||||
|
libefi.3 efi_nextvarname.3 \
|
||||||
|
libefi.3 efi_setvar.3
|
||||||
|
|
||||||
|
.include <bsd.lib.mk>
|
68
lib/libefi/efi_getvar.c
Normal file
68
lib/libefi/efi_getvar.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <libefi.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "libefi_int.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* EFI_STATUS
|
||||||
|
* GetVariable(
|
||||||
|
* IN CHAR16 *VariableName,
|
||||||
|
* IN EFI_GUID *VendorGuid,
|
||||||
|
* OUT UINT32 *Attributes OPTIONAL,
|
||||||
|
* IN OUT UINTN *DataSize,
|
||||||
|
* OUT VOID *Data
|
||||||
|
* );
|
||||||
|
*/
|
||||||
|
|
||||||
|
int
|
||||||
|
efi_getvar(char *name, uuid_t *vendor, uint32_t *attrib, size_t *datasize,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
struct iodev_efivar_req req;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
req.namesize = 0;
|
||||||
|
error = libefi_utf8_to_ucs2(name, &req.namesize, &req.name);
|
||||||
|
if (error)
|
||||||
|
return (error);
|
||||||
|
|
||||||
|
req.vendor = *vendor;
|
||||||
|
req.datasize = *datasize;
|
||||||
|
req.data = data;
|
||||||
|
req.access = IODEV_EFIVAR_GETVAR;
|
||||||
|
error = libefi_efivar(&req);
|
||||||
|
*datasize = req.datasize;
|
||||||
|
if (!error && attrib != NULL)
|
||||||
|
*attrib = req.attrib;
|
||||||
|
free(req.name);
|
||||||
|
return (error);
|
||||||
|
}
|
66
lib/libefi/efi_nextvarname.c
Normal file
66
lib/libefi/efi_nextvarname.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <libefi.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "libefi_int.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* EFI_STATUS
|
||||||
|
* GetNextVariableName(
|
||||||
|
* IN OUT UINTN *VariableNameSize,
|
||||||
|
* IN OUT CHAR16 *VariableName,
|
||||||
|
* IN OUT EFI_GUID *VendorGuid
|
||||||
|
* );
|
||||||
|
*/
|
||||||
|
|
||||||
|
int
|
||||||
|
efi_nextvarname(size_t *namesize, char *name, uuid_t *vendor)
|
||||||
|
{
|
||||||
|
struct iodev_efivar_req req;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
req.namesize = *namesize;
|
||||||
|
error = libefi_utf8_to_ucs2(name, &req.namesize, &req.name);
|
||||||
|
if (error)
|
||||||
|
return (error);
|
||||||
|
|
||||||
|
req.vendor = *vendor;
|
||||||
|
req.access = IODEV_EFIVAR_NEXTNAME;
|
||||||
|
error = libefi_efivar(&req);
|
||||||
|
*namesize = req.namesize;
|
||||||
|
if (!error) {
|
||||||
|
error = libefi_ucs2_to_utf8(req.name, namesize, name);
|
||||||
|
if (!error)
|
||||||
|
*vendor = req.vendor;
|
||||||
|
}
|
||||||
|
free(req.name);
|
||||||
|
return (error);
|
||||||
|
}
|
66
lib/libefi/efi_setvar.c
Normal file
66
lib/libefi/efi_setvar.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <libefi.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "libefi_int.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* EFI_STATUS
|
||||||
|
* SetVariable(
|
||||||
|
* IN CHAR16 *VariableName,
|
||||||
|
* IN EFI_GUID *VendorGuid,
|
||||||
|
* IN UINT32 Attributes,
|
||||||
|
* IN UINTN DataSize,
|
||||||
|
* IN VOID *Data
|
||||||
|
* );
|
||||||
|
*/
|
||||||
|
|
||||||
|
int
|
||||||
|
efi_setvar(char *name, uuid_t *vendor, uint32_t attrib, size_t datasize,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
struct iodev_efivar_req req;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
req.namesize = 0;
|
||||||
|
error = libefi_utf8_to_ucs2(name, &req.namesize, &req.name);
|
||||||
|
if (error)
|
||||||
|
return (error);
|
||||||
|
|
||||||
|
req.vendor = *vendor;
|
||||||
|
req.attrib = attrib;
|
||||||
|
req.datasize = datasize;
|
||||||
|
req.data = data;
|
||||||
|
req.access = IODEV_EFIVAR_SETVAR;
|
||||||
|
error = libefi_efivar(&req);
|
||||||
|
free(req.name);
|
||||||
|
return (error);
|
||||||
|
}
|
136
lib/libefi/libefi.3
Normal file
136
lib/libefi/libefi.3
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
.\"-
|
||||||
|
.\" Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
.\" All rights reserved.
|
||||||
|
.\"
|
||||||
|
.\" Redistribution and use in source and binary forms, with or without
|
||||||
|
.\" modification, are permitted provided that the following conditions
|
||||||
|
.\" are met:
|
||||||
|
.\" 1. Redistributions of source code must retain the above copyright
|
||||||
|
.\" notice, this list of conditions and the following disclaimer.
|
||||||
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
.\" notice, this list of conditions and the following disclaimer in the
|
||||||
|
.\" documentation and/or other materials provided with the distribution.
|
||||||
|
.\"
|
||||||
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
.\" SUCH DAMAGE.
|
||||||
|
.\"
|
||||||
|
.\" $FreeBSD$
|
||||||
|
.\"
|
||||||
|
.Dd Januari 29, 2010
|
||||||
|
.Dt LIBEFI 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm efi_getvar , efi_nextvarname , efi_setvar
|
||||||
|
.Nd Interface for accessing the EFI variable services
|
||||||
|
.Sh LIBRARY
|
||||||
|
.Lb libefi
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In libefi.h
|
||||||
|
.Ft int
|
||||||
|
.Fn efi_getvar "char *name" "uuid_t *vendor" "uint32_t *attrib" \
|
||||||
|
"size_t *datasize" "void *data"
|
||||||
|
.Ft int
|
||||||
|
.Fn efi_nextvarname "size_t *namesize" "char *name" "uuid_t *vendor"
|
||||||
|
.Ft int
|
||||||
|
.Fn efi_setvar "char *name" "uuid_t *vendor" "uint32_t attrib" \
|
||||||
|
"size_t datasize" "void *data"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Nm libefi
|
||||||
|
library provides access to a select set of the runtime services of the
|
||||||
|
Extensible Firmware Interface (EFI).
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn efi_nextvarname
|
||||||
|
function is used to enumerate the variables.
|
||||||
|
The
|
||||||
|
.Nm namesize
|
||||||
|
parameter needs to be set to the size of the buffer pointed to by
|
||||||
|
.Nm name .
|
||||||
|
On return,
|
||||||
|
.Nm namesize
|
||||||
|
is set to the length of the variable name (including the terminating '\\0')
|
||||||
|
irrespective of whether the buffer was big enough.
|
||||||
|
The buffer pointed to by
|
||||||
|
.Nm name
|
||||||
|
contains the full or partial variable name on return.
|
||||||
|
Only on successful completion of the request is the
|
||||||
|
.Nm vendor
|
||||||
|
updated.
|
||||||
|
The values returned should be passed to successive calls to
|
||||||
|
.Fn efi_nextvarname
|
||||||
|
until all variables have been enumerated.
|
||||||
|
.Pp
|
||||||
|
The variable name and vendor as returned by
|
||||||
|
.Fn efi_nextvarname
|
||||||
|
can be passed to
|
||||||
|
.Fn efi_getvar
|
||||||
|
to obtain the value and attribute of the variable.
|
||||||
|
The buffer that is to contain the value is specified by
|
||||||
|
.Nm data
|
||||||
|
and the size of the buffer is given by
|
||||||
|
.Nm datasize .
|
||||||
|
The attribute pointed to by
|
||||||
|
.Nm attrib
|
||||||
|
consists of the bit values defined by the EFI specification.
|
||||||
|
.Pp
|
||||||
|
Variables can be created, modified and deleted using the
|
||||||
|
.Fn efi_setvar
|
||||||
|
function.
|
||||||
|
All new variables must be non-volatile and runtime accessable in
|
||||||
|
order for the request to succeed.
|
||||||
|
Note that for runtime accessable variables the boottime accessable bit must
|
||||||
|
be set as well.
|
||||||
|
To delete a variable, set
|
||||||
|
.Nm datasize
|
||||||
|
to 0.
|
||||||
|
.Pp
|
||||||
|
The vendor UUID is used to avoid collisions between variable names of
|
||||||
|
different vendors.
|
||||||
|
Variables created for use by FreeBSD should use the
|
||||||
|
.Nm EFI_FREEBSD_VENDOR
|
||||||
|
UUID as defined in the
|
||||||
|
.Nm libefi
|
||||||
|
header file.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
Upon successful completion, these functions return 0.
|
||||||
|
Otherwise, the error number is returned.
|
||||||
|
These functions will fail if:
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EACCES
|
||||||
|
Insufficient permissions to access the EFI services.
|
||||||
|
.It Bq Er EILSEQ
|
||||||
|
The variable name is not in UTF-8.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
The request has invalid parameters.
|
||||||
|
.It Bq Er ENOENT
|
||||||
|
The variable does not exist or no more variables exist.
|
||||||
|
.It Bq Er ENOMEM
|
||||||
|
Temporary storage could not be allocated.
|
||||||
|
.It Bq Er EOVERFLOW
|
||||||
|
The variable name is too long or the data is too big to fit in
|
||||||
|
the buffer provided.
|
||||||
|
.El
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr errno 2 ,
|
||||||
|
.Xr uuid 3
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Nm libefi
|
||||||
|
library first appeared in
|
||||||
|
.Fx 9
|
||||||
|
for the ia64 architecture.
|
||||||
|
.Sh AUTHORS
|
||||||
|
The
|
||||||
|
.Nm libefi
|
||||||
|
library and corresponding manual page were written by
|
||||||
|
.An Marcel Moolenaar Aq marcel@FreeBSD.org .
|
176
lib/libefi/libefi.c
Normal file
176
lib/libefi/libefi.c
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "libefi_int.h"
|
||||||
|
|
||||||
|
static int __iofd = -1;
|
||||||
|
|
||||||
|
static void
|
||||||
|
iodev_fd_close(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
close(__iofd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
iodev_fd(int *fd)
|
||||||
|
{
|
||||||
|
|
||||||
|
*fd = __iofd;
|
||||||
|
if (__iofd != -1)
|
||||||
|
return (0);
|
||||||
|
|
||||||
|
__iofd = open("/dev/io", O_RDWR);
|
||||||
|
if (__iofd == -1)
|
||||||
|
return (errno);
|
||||||
|
|
||||||
|
atexit(iodev_fd_close);
|
||||||
|
*fd = __iofd;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
libefi_ucs2_to_utf8(u_short *nm, size_t *szp, char *name)
|
||||||
|
{
|
||||||
|
size_t len, sz;
|
||||||
|
u_short c;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
sz = *szp;
|
||||||
|
while (*nm) {
|
||||||
|
c = *nm++;
|
||||||
|
if (c > 0x7ff) {
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0xE0 | (c >> 12);
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0x80 | ((c >> 6) & 0x3f);
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0x80 | (c & 0x3f);
|
||||||
|
} else if (c > 0x7f) {
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0xC0 | ((c >> 6) & 0x1f);
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0x80 | (c & 0x3f);
|
||||||
|
} else {
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = (c & 0x7f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (len++ < sz)
|
||||||
|
*name++ = 0;
|
||||||
|
|
||||||
|
*szp = len;
|
||||||
|
return ((len <= sz) ? 0 : EOVERFLOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
libefi_utf8_to_ucs2(char *name, size_t *szp, u_short **nmp)
|
||||||
|
{
|
||||||
|
u_short *nm;
|
||||||
|
size_t sz;
|
||||||
|
uint32_t ucs4;
|
||||||
|
int c, bytes;
|
||||||
|
|
||||||
|
*szp = sz = (*szp == 0) ? strlen(name) * 2 + 2 : *szp;
|
||||||
|
*nmp = nm = malloc(sz);
|
||||||
|
|
||||||
|
ucs4 = 0;
|
||||||
|
bytes = 0;
|
||||||
|
while (sz > 1 && *name != '\0') {
|
||||||
|
c = *name++;
|
||||||
|
/*
|
||||||
|
* Conditionalize on the two major character types:
|
||||||
|
* initial and followup characters.
|
||||||
|
*/
|
||||||
|
if ((c & 0xc0) != 0x80) {
|
||||||
|
/* Initial characters. */
|
||||||
|
if (bytes != 0) {
|
||||||
|
free(nm);
|
||||||
|
return (EILSEQ);
|
||||||
|
}
|
||||||
|
if ((c & 0xf8) == 0xf0) {
|
||||||
|
ucs4 = c & 0x07;
|
||||||
|
bytes = 3;
|
||||||
|
} else if ((c & 0xf0) == 0xe0) {
|
||||||
|
ucs4 = c & 0x0f;
|
||||||
|
bytes = 2;
|
||||||
|
} else if ((c & 0xe0) == 0xc0) {
|
||||||
|
ucs4 = c & 0x1f;
|
||||||
|
bytes = 1;
|
||||||
|
} else {
|
||||||
|
ucs4 = c & 0x7f;
|
||||||
|
bytes = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* Followup characters. */
|
||||||
|
if (bytes > 0) {
|
||||||
|
ucs4 = (ucs4 << 6) + (c & 0x3f);
|
||||||
|
bytes--;
|
||||||
|
} else if (bytes == 0) {
|
||||||
|
free(nm);
|
||||||
|
return (EILSEQ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bytes == 0) {
|
||||||
|
if (ucs4 > 0xffff) {
|
||||||
|
free(nm);
|
||||||
|
return (EILSEQ);
|
||||||
|
}
|
||||||
|
*nm++ = (u_short)ucs4;
|
||||||
|
sz -= 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sz < 2) {
|
||||||
|
free(nm);
|
||||||
|
return (EDOOFUS);
|
||||||
|
}
|
||||||
|
*nm = 0;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
libefi_efivar(struct iodev_efivar_req *req)
|
||||||
|
{
|
||||||
|
int error, fd;
|
||||||
|
|
||||||
|
error = iodev_fd(&fd);
|
||||||
|
if (!error)
|
||||||
|
error = (ioctl(fd, IODEV_EFIVAR, req) == -1) ? errno : 0;
|
||||||
|
if (!error)
|
||||||
|
error = req->result;
|
||||||
|
return (error);
|
||||||
|
}
|
57
lib/libefi/libefi.h
Normal file
57
lib/libefi/libefi.h
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBEFI_H_
|
||||||
|
#define _LIBEFI_H_
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/uuid.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* Attributes. */
|
||||||
|
#define EFI_ATTR_NV 0x0001 /* Variable stored in NVRAM. */
|
||||||
|
#define EFI_ATTR_BS 0x0002 /* Boot services accessable. */
|
||||||
|
#define EFI_ATTR_RT 0x0004 /* Runtime accessable. */
|
||||||
|
#define EFI_ATTR_HR 0x0008 /* Hardware error record. */
|
||||||
|
#define EFI_ATTR_WR 0x0010 /* Authenticated write access. */
|
||||||
|
|
||||||
|
/* Vendor for architecturally defined variables. */
|
||||||
|
#define EFI_GLOBAL_VARIABLE \
|
||||||
|
{0x8be4df61,0x93ca,0x11d2,0xaa,0x0d,{0x00,0xe0,0x98,0x03,0x2b,0x8c}}
|
||||||
|
|
||||||
|
/* Vendor for FreeBSD-specific variables. */
|
||||||
|
#define EFI_FREEBSD_VARIABLE \
|
||||||
|
{0x13c32014,0x0c9c,0x11df,0xa2,0x38,{0x00,0x17,0xa4,0xab,0x91,0x2d}}
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
int efi_getvar (char *, uuid_t *, uint32_t *, size_t *, void *);
|
||||||
|
int efi_nextvarname (size_t *, char *, uuid_t *);
|
||||||
|
int efi_setvar (char *, uuid_t *, uint32_t, size_t, void *);
|
||||||
|
__END_DECLS
|
||||||
|
|
||||||
|
#endif /* _LIBEFI_H_ */
|
40
lib/libefi/libefi_int.h
Normal file
40
lib/libefi/libefi_int.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2010 Marcel Moolenaar
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBEFI_INT_H_
|
||||||
|
#define _LIBEFI_INT_H_
|
||||||
|
|
||||||
|
#include <sys/errno.h>
|
||||||
|
#include <machine/iodev.h>
|
||||||
|
|
||||||
|
int libefi_ucs2_to_utf8(u_short *, size_t *, char *);
|
||||||
|
int libefi_utf8_to_ucs2(char *, size_t *, u_short **);
|
||||||
|
|
||||||
|
int libefi_efivar(struct iodev_efivar_req *);
|
||||||
|
|
||||||
|
#endif /* _LIBEFI_INT_H_ */
|
Loading…
Reference in New Issue
Block a user