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