1 /*- 2 * Copyright (c) 2004 Marcel Moolenaar 3 * Copyright (c) 2001 Doug Rabson 4 * Copyright (c) 2016 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Konstantin Belousov 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/efi.h> 37 #include <sys/kernel.h> 38 #include <sys/linker.h> 39 #include <sys/lock.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 #include <sys/clock.h> 43 #include <sys/proc.h> 44 #include <sys/rwlock.h> 45 #include <sys/sched.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 #include <sys/vmmeter.h> 49 50 #include <machine/fpu.h> 51 #include <machine/efi.h> 52 #include <machine/metadata.h> 53 #include <machine/vmparam.h> 54 55 #include <vm/vm.h> 56 #include <vm/pmap.h> 57 #include <vm/vm_map.h> 58 59 static struct efi_systbl *efi_systbl; 60 static struct efi_cfgtbl *efi_cfgtbl; 61 static struct efi_rt *efi_runtime; 62 63 static int efi_status2err[25] = { 64 0, /* EFI_SUCCESS */ 65 ENOEXEC, /* EFI_LOAD_ERROR */ 66 EINVAL, /* EFI_INVALID_PARAMETER */ 67 ENOSYS, /* EFI_UNSUPPORTED */ 68 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */ 69 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */ 70 EBUSY, /* EFI_NOT_READY */ 71 EIO, /* EFI_DEVICE_ERROR */ 72 EROFS, /* EFI_WRITE_PROTECTED */ 73 EAGAIN, /* EFI_OUT_OF_RESOURCES */ 74 EIO, /* EFI_VOLUME_CORRUPTED */ 75 ENOSPC, /* EFI_VOLUME_FULL */ 76 ENXIO, /* EFI_NO_MEDIA */ 77 ESTALE, /* EFI_MEDIA_CHANGED */ 78 ENOENT, /* EFI_NOT_FOUND */ 79 EACCES, /* EFI_ACCESS_DENIED */ 80 ETIMEDOUT, /* EFI_NO_RESPONSE */ 81 EADDRNOTAVAIL, /* EFI_NO_MAPPING */ 82 ETIMEDOUT, /* EFI_TIMEOUT */ 83 EDOOFUS, /* EFI_NOT_STARTED */ 84 EALREADY, /* EFI_ALREADY_STARTED */ 85 ECANCELED, /* EFI_ABORTED */ 86 EPROTO, /* EFI_ICMP_ERROR */ 87 EPROTO, /* EFI_TFTP_ERROR */ 88 EPROTO /* EFI_PROTOCOL_ERROR */ 89 }; 90 91 static int 92 efi_status_to_errno(efi_status status) 93 { 94 u_long code; 95 96 code = status & 0x3ffffffffffffffful; 97 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS); 98 } 99 100 static struct mtx efi_lock; 101 102 static int 103 efi_init(void) 104 { 105 struct efi_map_header *efihdr; 106 struct efi_md *map; 107 caddr_t kmdp; 108 size_t efisz; 109 110 mtx_init(&efi_lock, "efi", NULL, MTX_DEF); 111 112 if (efi_systbl_phys == 0) { 113 if (bootverbose) 114 printf("EFI systbl not available\n"); 115 return (0); 116 } 117 if (!PMAP_HAS_DMAP) { 118 if (bootverbose) 119 printf("EFI systbl requires direct map\n"); 120 return (0); 121 } 122 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys); 123 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 124 efi_systbl = NULL; 125 if (bootverbose) 126 printf("EFI systbl signature invalid\n"); 127 return (0); 128 } 129 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 130 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 131 if (efi_cfgtbl == NULL) { 132 if (bootverbose) 133 printf("EFI config table is not present\n"); 134 } 135 136 kmdp = preload_search_by_type("elf kernel"); 137 if (kmdp == NULL) 138 kmdp = preload_search_by_type("elf64 kernel"); 139 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 140 MODINFO_METADATA | MODINFOMD_EFI_MAP); 141 if (efihdr == NULL) { 142 if (bootverbose) 143 printf("EFI map is not present\n"); 144 return (0); 145 } 146 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 147 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 148 if (efihdr->descriptor_size == 0) 149 return (ENOMEM); 150 151 if (!efi_create_1t1_map(map, efihdr->memory_size / 152 efihdr->descriptor_size, efihdr->descriptor_size)) { 153 if (bootverbose) 154 printf("EFI cannot create runtime map\n"); 155 return (ENOMEM); 156 } 157 158 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 159 (struct efi_rt *)efi_systbl->st_rt; 160 if (efi_runtime == NULL) { 161 if (bootverbose) 162 printf("EFI runtime services table is not present\n"); 163 efi_destroy_1t1_map(); 164 return (ENXIO); 165 } 166 167 return (0); 168 } 169 170 static void 171 efi_uninit(void) 172 { 173 174 efi_destroy_1t1_map(); 175 176 efi_systbl = NULL; 177 efi_cfgtbl = NULL; 178 efi_runtime = NULL; 179 180 mtx_destroy(&efi_lock); 181 } 182 183 int 184 efi_rt_ok(void) 185 { 186 187 if (efi_runtime == NULL) 188 return (ENXIO); 189 return (0); 190 } 191 192 static int 193 efi_enter(void) 194 { 195 struct thread *td; 196 pmap_t curpmap; 197 198 if (efi_runtime == NULL) 199 return (ENXIO); 200 td = curthread; 201 curpmap = &td->td_proc->p_vmspace->vm_pmap; 202 PMAP_LOCK(curpmap); 203 mtx_lock(&efi_lock); 204 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); 205 return (efi_arch_enter()); 206 } 207 208 static void 209 efi_leave(void) 210 { 211 struct thread *td; 212 pmap_t curpmap; 213 214 efi_arch_leave(); 215 216 curpmap = &curproc->p_vmspace->vm_pmap; 217 td = curthread; 218 fpu_kern_leave(td, NULL); 219 mtx_unlock(&efi_lock); 220 PMAP_UNLOCK(curpmap); 221 } 222 223 int 224 efi_get_table(struct uuid *uuid, void **ptr) 225 { 226 struct efi_cfgtbl *ct; 227 u_long count; 228 229 if (efi_cfgtbl == NULL || efi_systbl == NULL) 230 return (ENXIO); 231 count = efi_systbl->st_entries; 232 ct = efi_cfgtbl; 233 while (count--) { 234 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { 235 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data); 236 return (0); 237 } 238 ct++; 239 } 240 return (ENOENT); 241 } 242 243 static int 244 efi_get_time_locked(struct efi_tm *tm) 245 { 246 efi_status status; 247 int error; 248 249 EFI_TIME_OWNED() 250 error = efi_enter(); 251 if (error != 0) 252 return (error); 253 status = efi_runtime->rt_gettime(tm, NULL); 254 efi_leave(); 255 error = efi_status_to_errno(status); 256 return (error); 257 } 258 259 int 260 efi_get_time(struct efi_tm *tm) 261 { 262 int error; 263 264 if (efi_runtime == NULL) 265 return (ENXIO); 266 EFI_TIME_LOCK() 267 error = efi_get_time_locked(tm); 268 EFI_TIME_UNLOCK() 269 return (error); 270 } 271 272 int 273 efi_reset_system(void) 274 { 275 int error; 276 277 error = efi_enter(); 278 if (error != 0) 279 return (error); 280 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL); 281 efi_leave(); 282 return (EIO); 283 } 284 285 static int 286 efi_set_time_locked(struct efi_tm *tm) 287 { 288 efi_status status; 289 int error; 290 291 EFI_TIME_OWNED(); 292 error = efi_enter(); 293 if (error != 0) 294 return (error); 295 status = efi_runtime->rt_settime(tm); 296 efi_leave(); 297 error = efi_status_to_errno(status); 298 return (error); 299 } 300 301 int 302 efi_set_time(struct efi_tm *tm) 303 { 304 int error; 305 306 if (efi_runtime == NULL) 307 return (ENXIO); 308 EFI_TIME_LOCK() 309 error = efi_set_time_locked(tm); 310 EFI_TIME_UNLOCK() 311 return (error); 312 } 313 314 int 315 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 316 size_t *datasize, void *data) 317 { 318 efi_status status; 319 int error; 320 321 error = efi_enter(); 322 if (error != 0) 323 return (error); 324 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data); 325 efi_leave(); 326 error = efi_status_to_errno(status); 327 return (error); 328 } 329 330 int 331 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 332 { 333 efi_status status; 334 int error; 335 336 error = efi_enter(); 337 if (error != 0) 338 return (error); 339 status = efi_runtime->rt_scanvar(namesize, name, vendor); 340 efi_leave(); 341 error = efi_status_to_errno(status); 342 return (error); 343 } 344 345 int 346 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 347 size_t datasize, void *data) 348 { 349 efi_status status; 350 int error; 351 352 error = efi_enter(); 353 if (error != 0) 354 return (error); 355 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data); 356 efi_leave(); 357 error = efi_status_to_errno(status); 358 return (error); 359 } 360 361 static int 362 efirt_modevents(module_t m, int event, void *arg __unused) 363 { 364 365 switch (event) { 366 case MOD_LOAD: 367 return (efi_init()); 368 369 case MOD_UNLOAD: 370 efi_uninit(); 371 return (0); 372 373 case MOD_SHUTDOWN: 374 return (0); 375 376 default: 377 return (EOPNOTSUPP); 378 } 379 } 380 381 static moduledata_t efirt_moddata = { 382 .name = "efirt", 383 .evhand = efirt_modevents, 384 .priv = NULL, 385 }; 386 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY); 387 MODULE_VERSION(efirt, 1); 388