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$"); 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 42 static d_ioctl_t efidev_ioctl; 43 44 static struct cdevsw efi_cdevsw = { 45 .d_name = "efi", 46 .d_version = D_VERSION, 47 .d_ioctl = efidev_ioctl, 48 }; 49 50 /* ARGSUSED */ 51 static int 52 efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr, 53 int flags __unused, struct thread *td __unused) 54 { 55 int error; 56 57 switch (cmd) { 58 case EFIIOC_GET_TABLE: 59 { 60 struct efi_get_table_ioc *egtioc = 61 (struct efi_get_table_ioc *)addr; 62 63 error = efi_get_table(&egtioc->uuid, &egtioc->ptr); 64 break; 65 } 66 case EFIIOC_GET_TIME: 67 { 68 struct efi_tm *tm = (struct efi_tm *)addr; 69 70 error = efi_get_time(tm); 71 break; 72 } 73 case EFIIOC_SET_TIME: 74 { 75 struct efi_tm *tm = (struct efi_tm *)addr; 76 77 error = efi_set_time(tm); 78 break; 79 } 80 case EFIIOC_VAR_GET: 81 { 82 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 83 void *data; 84 efi_char *name; 85 86 data = malloc(ev->datasize, M_TEMP, M_WAITOK); 87 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 88 error = copyin(ev->name, name, ev->namesize); 89 if (error) 90 goto vg_out; 91 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { 92 error = EINVAL; 93 goto vg_out; 94 } 95 96 error = efi_var_get(name, &ev->vendor, &ev->attrib, 97 &ev->datasize, data); 98 99 if (error == 0) { 100 error = copyout(data, ev->data, ev->datasize); 101 } else if (error == EOVERFLOW) { 102 /* 103 * Pass back the size we really need, but 104 * convert the error to 0 so the copyout 105 * happens. datasize was updated in the 106 * efi_var_get call. 107 */ 108 ev->data = NULL; 109 error = 0; 110 } 111 vg_out: 112 free(data, M_TEMP); 113 free(name, M_TEMP); 114 break; 115 } 116 case EFIIOC_VAR_NEXT: 117 { 118 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 119 efi_char *name; 120 121 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 122 error = copyin(ev->name, name, ev->namesize); 123 if (error) 124 goto vn_out; 125 /* Note: namesize is the buffer size, not the string lenght */ 126 127 error = efi_var_nextname(&ev->namesize, name, &ev->vendor); 128 if (error == 0) { 129 error = copyout(name, ev->name, ev->namesize); 130 } else if (error == EOVERFLOW) { 131 ev->name = NULL; 132 error = 0; 133 } 134 vn_out: 135 free(name, M_TEMP); 136 break; 137 } 138 case EFIIOC_VAR_SET: 139 { 140 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; 141 void *data = NULL; 142 efi_char *name; 143 144 /* datasize == 0 -> delete (more or less) */ 145 if (ev->datasize > 0) 146 data = malloc(ev->datasize, M_TEMP, M_WAITOK); 147 name = malloc(ev->namesize, M_TEMP, M_WAITOK); 148 if (ev->datasize) { 149 error = copyin(ev->data, data, ev->datasize); 150 if (error) 151 goto vs_out; 152 } 153 error = copyin(ev->name, name, ev->namesize); 154 if (error) 155 goto vs_out; 156 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { 157 error = EINVAL; 158 goto vs_out; 159 } 160 161 error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize, 162 data); 163 vs_out: 164 free(data, M_TEMP); 165 free(name, M_TEMP); 166 break; 167 } 168 default: 169 error = ENOTTY; 170 break; 171 } 172 173 return (error); 174 } 175 176 int 177 efidev_init(struct cdev **cdev) 178 { 179 180 *cdev = make_dev(&efi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0700, 181 "efi"); 182 183 return (0); 184 } 185 186 int 187 efidev_uninit(struct cdev *cdev) 188 { 189 190 destroy_dev(cdev); 191 192 return (0); 193 } 194