Home | History | Annotate | Line # | Download | only in dev
efi.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: efi.c,v 1.1 2021/10/10 13:03:09 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill /*
     30  1.1  jmcneill  * This pseudo-driver implements a /dev/efi character device that provides
     31  1.1  jmcneill  * ioctls for using UEFI runtime time and variable services.
     32  1.1  jmcneill  */
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/cdefs.h>
     35  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.1 2021/10/10 13:03:09 jmcneill Exp $");
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <sys/param.h>
     38  1.1  jmcneill #include <sys/conf.h>
     39  1.1  jmcneill #include <sys/kmem.h>
     40  1.1  jmcneill #include <sys/atomic.h>
     41  1.1  jmcneill #include <sys/efiio.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/efivar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #ifdef _LP64
     46  1.1  jmcneill #define	EFIERR(x)		(0x8000000000000000 | x)
     47  1.1  jmcneill #else
     48  1.1  jmcneill #define	EFIERR(x)		(0x80000000 | x)
     49  1.1  jmcneill #endif
     50  1.1  jmcneill 
     51  1.1  jmcneill #define	EFI_SUCCESS		0
     52  1.1  jmcneill #define	EFI_INVALID_PARAMETER	EFIERR(2)
     53  1.1  jmcneill #define	EFI_UNSUPPORTED		EFIERR(3)
     54  1.1  jmcneill #define	EFI_BUFFER_TOO_SMALL	EFIERR(5)
     55  1.1  jmcneill #define	EFI_DEVICE_ERROR	EFIERR(7)
     56  1.1  jmcneill #define	EFI_WRITE_PROTECTED	EFIERR(8)
     57  1.1  jmcneill #define	EFI_OUT_OF_RESOURCES	EFIERR(9)
     58  1.1  jmcneill #define	EFI_NOT_FOUND		EFIERR(14)
     59  1.1  jmcneill #define	EFI_SECURITY_VIOLATION	EFIERR(26)
     60  1.1  jmcneill 
     61  1.1  jmcneill #include "ioconf.h"
     62  1.1  jmcneill 
     63  1.1  jmcneill /*
     64  1.1  jmcneill  * Maximum length of an EFI variable name. The UEFI spec doesn't specify a
     65  1.1  jmcneill  * constraint, but we want to limit the size to act as a guard rail against
     66  1.1  jmcneill  * allocating too much kernel memory.
     67  1.1  jmcneill  */
     68  1.1  jmcneill #define	EFI_VARNAME_MAXLENGTH		EFI_PAGE_SIZE
     69  1.1  jmcneill 
     70  1.1  jmcneill /*
     71  1.1  jmcneill  * Pointer to arch specific EFI backend.
     72  1.1  jmcneill  */
     73  1.1  jmcneill static const struct efi_ops *efi_ops = NULL;
     74  1.1  jmcneill 
     75  1.1  jmcneill /*
     76  1.1  jmcneill  * Only allow one user of /dev/efi at a time. Even though the MD EFI backends
     77  1.1  jmcneill  * should serialize individual UEFI RT calls, the UEFI specification says
     78  1.1  jmcneill  * that a SetVariable() call between calls to GetNextVariableName() may
     79  1.1  jmcneill  * produce unpredictable results, and we want to avoid this.
     80  1.1  jmcneill  */
     81  1.1  jmcneill static u_int efi_isopen = 0;
     82  1.1  jmcneill 
     83  1.1  jmcneill static dev_type_open(efi_open);
     84  1.1  jmcneill static dev_type_close(efi_close);
     85  1.1  jmcneill static dev_type_ioctl(efi_ioctl);
     86  1.1  jmcneill 
     87  1.1  jmcneill const struct cdevsw efi_cdevsw = {
     88  1.1  jmcneill 	.d_open =	efi_open,
     89  1.1  jmcneill 	.d_close =	efi_close,
     90  1.1  jmcneill 	.d_ioctl =	efi_ioctl,
     91  1.1  jmcneill 	.d_read =	noread,
     92  1.1  jmcneill 	.d_write =	nowrite,
     93  1.1  jmcneill 	.d_stop =	nostop,
     94  1.1  jmcneill 	.d_tty =	notty,
     95  1.1  jmcneill 	.d_poll =	nopoll,
     96  1.1  jmcneill 	.d_mmap =	nommap,
     97  1.1  jmcneill 	.d_kqfilter =	nokqfilter,
     98  1.1  jmcneill 	.d_discard =	nodiscard,
     99  1.1  jmcneill 	.d_flag =	D_OTHER | D_MPSAFE,
    100  1.1  jmcneill };
    101  1.1  jmcneill 
    102  1.1  jmcneill static int
    103  1.1  jmcneill efi_open(dev_t dev, int flags, int type, struct lwp *l)
    104  1.1  jmcneill {
    105  1.1  jmcneill 	if (efi_ops == NULL) {
    106  1.1  jmcneill 		return ENXIO;
    107  1.1  jmcneill 	}
    108  1.1  jmcneill 	if (atomic_cas_uint(&efi_isopen, 0, 1) == 1) {
    109  1.1  jmcneill 		return EBUSY;
    110  1.1  jmcneill 	}
    111  1.1  jmcneill 	return 0;
    112  1.1  jmcneill }
    113  1.1  jmcneill 
    114  1.1  jmcneill static int
    115  1.1  jmcneill efi_close(dev_t dev, int flags, int type, struct lwp *l)
    116  1.1  jmcneill {
    117  1.1  jmcneill 	KASSERT(efi_isopen);
    118  1.1  jmcneill 	atomic_swap_uint(&efi_isopen, 0);
    119  1.1  jmcneill 	return 0;
    120  1.1  jmcneill }
    121  1.1  jmcneill 
    122  1.1  jmcneill static int
    123  1.1  jmcneill efi_status_to_error(efi_status status)
    124  1.1  jmcneill {
    125  1.1  jmcneill 	switch (status) {
    126  1.1  jmcneill 	case EFI_SUCCESS:
    127  1.1  jmcneill 		return 0;
    128  1.1  jmcneill 	case EFI_INVALID_PARAMETER:
    129  1.1  jmcneill 		return EINVAL;
    130  1.1  jmcneill 	case EFI_UNSUPPORTED:
    131  1.1  jmcneill 		return EOPNOTSUPP;
    132  1.1  jmcneill 	case EFI_BUFFER_TOO_SMALL:
    133  1.1  jmcneill 		return ERANGE;
    134  1.1  jmcneill 	case EFI_DEVICE_ERROR:
    135  1.1  jmcneill 		return EIO;
    136  1.1  jmcneill 	case EFI_WRITE_PROTECTED:
    137  1.1  jmcneill 		return EROFS;
    138  1.1  jmcneill 	case EFI_OUT_OF_RESOURCES:
    139  1.1  jmcneill 		return ENOMEM;
    140  1.1  jmcneill 	case EFI_NOT_FOUND:
    141  1.1  jmcneill 		return ENOENT;
    142  1.1  jmcneill 	case EFI_SECURITY_VIOLATION:
    143  1.1  jmcneill 		return EACCES;
    144  1.1  jmcneill 	default:
    145  1.1  jmcneill 		return EIO;
    146  1.1  jmcneill 	}
    147  1.1  jmcneill }
    148  1.1  jmcneill 
    149  1.1  jmcneill static int
    150  1.1  jmcneill efi_ioctl_var_get(struct efi_var_ioc *var)
    151  1.1  jmcneill {
    152  1.1  jmcneill 	uint16_t *namebuf;
    153  1.1  jmcneill 	void *databuf = NULL;
    154  1.1  jmcneill 	efi_status status;
    155  1.1  jmcneill 	int error;
    156  1.1  jmcneill 
    157  1.1  jmcneill 	if (var->name == NULL || var->namesize == 0 ||
    158  1.1  jmcneill 	    (var->data != NULL && var->datasize == 0)) {
    159  1.1  jmcneill 		return EINVAL;
    160  1.1  jmcneill 	}
    161  1.1  jmcneill 	if (var->namesize > EFI_VARNAME_MAXLENGTH) {
    162  1.1  jmcneill 		return ENOMEM;
    163  1.1  jmcneill 	}
    164  1.1  jmcneill 
    165  1.1  jmcneill 	namebuf = kmem_alloc(var->namesize, KM_SLEEP);
    166  1.1  jmcneill 	error = copyin(var->name, namebuf, var->namesize);
    167  1.1  jmcneill 	if (error != 0) {
    168  1.1  jmcneill 		goto done;
    169  1.1  jmcneill 	}
    170  1.1  jmcneill 	if (namebuf[var->namesize / 2 - 1] != '\0') {
    171  1.1  jmcneill 		error = EINVAL;
    172  1.1  jmcneill 		goto done;
    173  1.1  jmcneill 	}
    174  1.1  jmcneill 	if (var->datasize != 0) {
    175  1.1  jmcneill 		databuf = kmem_alloc(var->datasize, KM_SLEEP);
    176  1.1  jmcneill 		error = copyin(var->data, databuf, var->datasize);
    177  1.1  jmcneill 		if (error != 0) {
    178  1.1  jmcneill 			goto done;
    179  1.1  jmcneill 		}
    180  1.1  jmcneill 	}
    181  1.1  jmcneill 
    182  1.1  jmcneill 	status = efi_ops->efi_getvar(namebuf, &var->vendor, &var->attrib,
    183  1.1  jmcneill 	    &var->datasize, databuf);
    184  1.1  jmcneill 	if (status != EFI_SUCCESS && status != EFI_BUFFER_TOO_SMALL) {
    185  1.1  jmcneill 		error = efi_status_to_error(status);
    186  1.1  jmcneill 		goto done;
    187  1.1  jmcneill 	}
    188  1.1  jmcneill 	if (status == EFI_SUCCESS && databuf != NULL) {
    189  1.1  jmcneill 		error = copyout(databuf, var->data, var->datasize);
    190  1.1  jmcneill 	} else {
    191  1.1  jmcneill 		var->data = NULL;
    192  1.1  jmcneill 	}
    193  1.1  jmcneill 
    194  1.1  jmcneill done:
    195  1.1  jmcneill 	kmem_free(namebuf, var->namesize);
    196  1.1  jmcneill 	if (databuf != NULL) {
    197  1.1  jmcneill 		kmem_free(databuf, var->datasize);
    198  1.1  jmcneill 	}
    199  1.1  jmcneill 	return error;
    200  1.1  jmcneill }
    201  1.1  jmcneill 
    202  1.1  jmcneill static int
    203  1.1  jmcneill efi_ioctl_var_next(struct efi_var_ioc *var)
    204  1.1  jmcneill {
    205  1.1  jmcneill 	efi_status status;
    206  1.1  jmcneill 	uint16_t *namebuf;
    207  1.1  jmcneill 	int error;
    208  1.1  jmcneill 
    209  1.1  jmcneill 	if (var->name == NULL || var->namesize == 0) {
    210  1.1  jmcneill 		return EINVAL;
    211  1.1  jmcneill 	}
    212  1.1  jmcneill 	if (var->namesize > EFI_VARNAME_MAXLENGTH) {
    213  1.1  jmcneill 		return ENOMEM;
    214  1.1  jmcneill 	}
    215  1.1  jmcneill 
    216  1.1  jmcneill 	namebuf = kmem_alloc(var->namesize, KM_SLEEP);
    217  1.1  jmcneill 	error = copyin(var->name, namebuf, var->namesize);
    218  1.1  jmcneill 	if (error != 0) {
    219  1.1  jmcneill 		goto done;
    220  1.1  jmcneill 	}
    221  1.1  jmcneill 
    222  1.1  jmcneill 	status = efi_ops->efi_nextvar(&var->namesize, namebuf, &var->vendor);
    223  1.1  jmcneill 	if (status != EFI_SUCCESS && status != EFI_BUFFER_TOO_SMALL) {
    224  1.1  jmcneill 		error = efi_status_to_error(status);
    225  1.1  jmcneill 		goto done;
    226  1.1  jmcneill 	}
    227  1.1  jmcneill 	if (status == EFI_SUCCESS) {
    228  1.1  jmcneill 		error = copyout(namebuf, var->name, var->namesize);
    229  1.1  jmcneill 	} else {
    230  1.1  jmcneill 		var->name = NULL;
    231  1.1  jmcneill 	}
    232  1.1  jmcneill 
    233  1.1  jmcneill done:
    234  1.1  jmcneill 	kmem_free(namebuf, var->namesize);
    235  1.1  jmcneill 	return error;
    236  1.1  jmcneill }
    237  1.1  jmcneill 
    238  1.1  jmcneill static int
    239  1.1  jmcneill efi_ioctl_var_set(struct efi_var_ioc *var)
    240  1.1  jmcneill {
    241  1.1  jmcneill 	efi_status status;
    242  1.1  jmcneill 	uint16_t *namebuf;
    243  1.1  jmcneill 	uint16_t *databuf = NULL;
    244  1.1  jmcneill 	int error;
    245  1.1  jmcneill 
    246  1.1  jmcneill 	if (var->name == NULL || var->namesize == 0) {
    247  1.1  jmcneill 		return EINVAL;
    248  1.1  jmcneill 	}
    249  1.1  jmcneill 
    250  1.1  jmcneill 	namebuf = kmem_alloc(var->namesize, KM_SLEEP);
    251  1.1  jmcneill 	error = copyin(var->name, namebuf, var->namesize);
    252  1.1  jmcneill 	if (error != 0) {
    253  1.1  jmcneill 		goto done;
    254  1.1  jmcneill 	}
    255  1.1  jmcneill 	if (namebuf[var->namesize / 2 - 1] != '\0') {
    256  1.1  jmcneill 		error = EINVAL;
    257  1.1  jmcneill 		goto done;
    258  1.1  jmcneill 	}
    259  1.1  jmcneill 	if (var->datasize != 0) {
    260  1.1  jmcneill 		databuf = kmem_alloc(var->datasize, KM_SLEEP);
    261  1.1  jmcneill 		error = copyin(var->data, databuf, var->datasize);
    262  1.1  jmcneill 		if (error != 0) {
    263  1.1  jmcneill 			goto done;
    264  1.1  jmcneill 		}
    265  1.1  jmcneill 	}
    266  1.1  jmcneill 
    267  1.1  jmcneill 	status = efi_ops->efi_setvar(namebuf, &var->vendor, var->attrib,
    268  1.1  jmcneill 	    var->datasize, databuf);
    269  1.1  jmcneill 	error = efi_status_to_error(status);
    270  1.1  jmcneill 
    271  1.1  jmcneill done:
    272  1.1  jmcneill 	kmem_free(namebuf, var->namesize);
    273  1.1  jmcneill 	if (databuf != NULL) {
    274  1.1  jmcneill 		kmem_free(databuf, var->datasize);
    275  1.1  jmcneill 	}
    276  1.1  jmcneill 	return error;
    277  1.1  jmcneill }
    278  1.1  jmcneill 
    279  1.1  jmcneill static int
    280  1.1  jmcneill efi_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    281  1.1  jmcneill {
    282  1.1  jmcneill 	KASSERT(efi_ops != NULL);
    283  1.1  jmcneill 
    284  1.1  jmcneill 	switch (cmd) {
    285  1.1  jmcneill 	case EFIIOC_VAR_GET:
    286  1.1  jmcneill 		return efi_ioctl_var_get(data);
    287  1.1  jmcneill 	case EFIIOC_VAR_NEXT:
    288  1.1  jmcneill 		return efi_ioctl_var_next(data);
    289  1.1  jmcneill 	case EFIIOC_VAR_SET:
    290  1.1  jmcneill 		return efi_ioctl_var_set(data);
    291  1.1  jmcneill 	}
    292  1.1  jmcneill 
    293  1.1  jmcneill 	return ENOTTY;
    294  1.1  jmcneill }
    295  1.1  jmcneill 
    296  1.1  jmcneill void
    297  1.1  jmcneill efi_register_ops(const struct efi_ops *ops)
    298  1.1  jmcneill {
    299  1.1  jmcneill 	KASSERT(efi_ops == NULL);
    300  1.1  jmcneill 	efi_ops = ops;
    301  1.1  jmcneill }
    302  1.1  jmcneill 
    303  1.1  jmcneill void
    304  1.1  jmcneill efiattach(int count)
    305  1.1  jmcneill {
    306  1.1  jmcneill }
    307