efidev.c revision 331019
1/*- 2 * Copyright (c) 2016 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 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/11/sys/dev/efidev/efidev.c 331019 2018-03-15 19:31:39Z kevans $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/bus.h> 34#include <sys/conf.h> 35#include <sys/lock.h> 36#include <sys/malloc.h> 37#include <sys/module.h> 38 39#include <machine/efi.h> 40#include <sys/efiio.h> 41 42static d_open_t efidev_open; 43static d_ioctl_t efidev_ioctl; 44 45static struct cdevsw efi_cdevsw = { 46 .d_name = "efi", 47 .d_version = D_VERSION, 48 .d_open = efidev_open, 49 .d_ioctl = efidev_ioctl, 50}; 51 52static int 53efidev_open(struct cdev *dev __unused, int oflags __unused, 54 int devtype __unused, struct thread *td __unused) 55{ 56 /* 57 * Only return success when we have an actual runtime to call. 58 */ 59 60 return efi_rt_ok(); 61} 62 63static int 64efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr, 65 int flags __unused, struct thread *td __unused) 66{ 67 int error; 68 69 switch (cmd) { 70 case EFIIOC_GET_TABLE: 71 { 72 struct efi_get_table_ioc *egtioc = 73 (struct efi_get_table_ioc *)addr; 74 75 error = efi_get_table(&egtioc->uuid, &egtioc->ptr); 76 break; 77 } 78 case EFIIOC_GET_TIME: 79 { 80 struct efi_tm *tm = (struct efi_tm *)addr; 81 82 error = efi_get_time(tm); 83 break; 84 } 85 case EFIIOC_SET_TIME: 86 { 87 struct efi_tm *tm = (struct efi_tm *)addr; 88 89 error = efi_set_time(tm); 90 break; 91 } 92 case EFIIOC_VAR_GET: 93 { 94 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 95 void *data; 96 efi_char *name; 97 98 data = malloc(ev->datasize, M_TEMP, M_WAITOK); 99 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 100 error = copyin(ev->name, name, ev->namesize); 101 if (error) 102 goto vg_out; 103 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { 104 error = EINVAL; 105 goto vg_out; 106 } 107 108 error = efi_var_get(name, &ev->vendor, &ev->attrib, 109 &ev->datasize, data); 110 111 if (error == 0) { 112 error = copyout(data, ev->data, ev->datasize); 113 } else if (error == EOVERFLOW) { 114 /* 115 * Pass back the size we really need, but 116 * convert the error to 0 so the copyout 117 * happens. datasize was updated in the 118 * efi_var_get call. 119 */ 120 ev->data = NULL; 121 error = 0; 122 } 123vg_out: 124 free(data, M_TEMP); 125 free(name, M_TEMP); 126 break; 127 } 128 case EFIIOC_VAR_NEXT: 129 { 130 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 131 efi_char *name; 132 133 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 134 error = copyin(ev->name, name, ev->namesize); 135 if (error) 136 goto vn_out; 137 /* Note: namesize is the buffer size, not the string lenght */ 138 139 error = efi_var_nextname(&ev->namesize, name, &ev->vendor); 140 if (error == 0) { 141 error = copyout(name, ev->name, ev->namesize); 142 } else if (error == EOVERFLOW) { 143 ev->name = NULL; 144 error = 0; 145 } 146 vn_out: 147 free(name, M_TEMP); 148 break; 149 } 150 case EFIIOC_VAR_SET: 151 { 152 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 153 void *data = NULL; 154 efi_char *name; 155 156 /* datasize == 0 -> delete (more or less) */ 157 if (ev->datasize > 0) 158 data = malloc(ev->datasize, M_TEMP, M_WAITOK); 159 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 160 if (ev->datasize) { 161 error = copyin(ev->data, data, ev->datasize); 162 if (error) 163 goto vs_out; 164 } 165 error = copyin(ev->name, name, ev->namesize); 166 if (error) 167 goto vs_out; 168 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { 169 error = EINVAL; 170 goto vs_out; 171 } 172 173 error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize, 174 data); 175vs_out: 176 free(data, M_TEMP); 177 free(name, M_TEMP); 178 break; 179 } 180 default: 181 error = ENOTTY; 182 break; 183 } 184 185 return (error); 186} 187 188static struct cdev *efidev; 189 190static int 191efidev_modevents(module_t m, int event, void *arg __unused) 192{ 193 struct make_dev_args mda; 194 int error; 195 196 switch (event) { 197 case MOD_LOAD: 198 make_dev_args_init(&mda); 199 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; 200 mda.mda_devsw = &efi_cdevsw; 201 mda.mda_uid = UID_ROOT; 202 mda.mda_gid = GID_WHEEL; 203 mda.mda_mode = 0700; 204 error = make_dev_s(&mda, &efidev, "efi"); 205 return (error); 206 207 case MOD_UNLOAD: 208 if (efidev != NULL) 209 destroy_dev(efidev); 210 efidev = NULL; 211 return (0); 212 213 case MOD_SHUTDOWN: 214 return (0); 215 216 default: 217 return (EOPNOTSUPP); 218 } 219} 220 221static moduledata_t efidev_moddata = { 222 .name = "efidev", 223 .evhand = efidev_modevents, 224 .priv = NULL, 225}; 226 227DECLARE_MODULE(efidev, efidev_moddata, SI_SUB_DEVFS, SI_ORDER_ANY); 228MODULE_VERSION(efidev, 1); 229MODULE_DEPEND(efidev, efirt, 1, 1, 1); 230