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 bool 103 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr) 104 { 105 struct efi_md *p; 106 int i; 107 108 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p, 109 descsz)) { 110 if ((p->md_attr & EFI_MD_ATTR_RT) == 0) 111 continue; 112 113 if (addr >= (uintptr_t)p->md_virt && 114 addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE) 115 return (true); 116 } 117 118 return (false); 119 } 120 121 static int 122 efi_init(void) 123 { 124 struct efi_map_header *efihdr; 125 struct efi_md *map; 126 caddr_t kmdp; 127 size_t efisz; 128 129 mtx_init(&efi_lock, "efi", NULL, MTX_DEF); 130 131 if (efi_systbl_phys == 0) { 132 if (bootverbose) 133 printf("EFI systbl not available\n"); 134 return (0); 135 } 136 if (!PMAP_HAS_DMAP) { 137 if (bootverbose) 138 printf("EFI systbl requires direct map\n"); 139 return (0); 140 } 141 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys); 142 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 143 efi_systbl = NULL; 144 if (bootverbose) 145 printf("EFI systbl signature invalid\n"); 146 return (0); 147 } 148 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 149 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 150 if (efi_cfgtbl == NULL) { 151 if (bootverbose) 152 printf("EFI config table is not present\n"); 153 } 154 155 kmdp = preload_search_by_type("elf kernel"); 156 if (kmdp == NULL) 157 kmdp = preload_search_by_type("elf64 kernel"); 158 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 159 MODINFO_METADATA | MODINFOMD_EFI_MAP); 160 if (efihdr == NULL) { 161 if (bootverbose) 162 printf("EFI map is not present\n"); 163 return (0); 164 } 165 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 166 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 167 if (efihdr->descriptor_size == 0) 168 return (ENOMEM); 169 170 if (!efi_create_1t1_map(map, efihdr->memory_size / 171 efihdr->descriptor_size, efihdr->descriptor_size)) { 172 if (bootverbose) 173 printf("EFI cannot create runtime map\n"); 174 return (ENOMEM); 175 } 176 177 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 178 (struct efi_rt *)efi_systbl->st_rt; 179 if (efi_runtime == NULL) { 180 if (bootverbose) 181 printf("EFI runtime services table is not present\n"); 182 efi_destroy_1t1_map(); 183 return (ENXIO); 184 } 185 186 /* 187 * Some UEFI implementations have multiple implementations of the 188 * RS->GetTime function. They switch from one we can only use early 189 * in the boot process to one valid as a RunTime service only when we 190 * call RS->SetVirtualAddressMap. As this is not always the case, e.g. 191 * with an old loader.efi, check if the RS->GetTime function is within 192 * the EFI map, and fail to attach if not. 193 */ 194 if (!efi_is_in_map(map, efihdr->memory_size / efihdr->descriptor_size, 195 efihdr->descriptor_size, (vm_offset_t)efi_runtime->rt_gettime)) { 196 if (bootverbose) 197 printf( 198 "EFI runtime services table has an invalid pointer\n"); 199 efi_runtime = NULL; 200 efi_destroy_1t1_map(); 201 return (ENXIO); 202 } 203 204 return (0); 205 } 206 207 static void 208 efi_uninit(void) 209 { 210 211 efi_destroy_1t1_map(); 212 213 efi_systbl = NULL; 214 efi_cfgtbl = NULL; 215 efi_runtime = NULL; 216 217 mtx_destroy(&efi_lock); 218 } 219 220 int 221 efi_rt_ok(void) 222 { 223 224 if (efi_runtime == NULL) 225 return (ENXIO); 226 return (0); 227 } 228 229 static int 230 efi_enter(void) 231 { 232 struct thread *td; 233 pmap_t curpmap; 234 235 if (efi_runtime == NULL) 236 return (ENXIO); 237 td = curthread; 238 curpmap = &td->td_proc->p_vmspace->vm_pmap; 239 PMAP_LOCK(curpmap); 240 mtx_lock(&efi_lock); 241 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); 242 return (efi_arch_enter()); 243 } 244 245 static void 246 efi_leave(void) 247 { 248 struct thread *td; 249 pmap_t curpmap; 250 251 efi_arch_leave(); 252 253 curpmap = &curproc->p_vmspace->vm_pmap; 254 td = curthread; 255 fpu_kern_leave(td, NULL); 256 mtx_unlock(&efi_lock); 257 PMAP_UNLOCK(curpmap); 258 } 259 260 int 261 efi_get_table(struct uuid *uuid, void **ptr) 262 { 263 struct efi_cfgtbl *ct; 264 u_long count; 265 266 if (efi_cfgtbl == NULL || efi_systbl == NULL) 267 return (ENXIO); 268 count = efi_systbl->st_entries; 269 ct = efi_cfgtbl; 270 while (count--) { 271 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { 272 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data); 273 return (0); 274 } 275 ct++; 276 } 277 return (ENOENT); 278 } 279 280 static int 281 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap) 282 { 283 efi_status status; 284 int error; 285 286 EFI_TIME_OWNED() 287 error = efi_enter(); 288 if (error != 0) 289 return (error); 290 status = efi_runtime->rt_gettime(tm, tmcap); 291 efi_leave(); 292 error = efi_status_to_errno(status); 293 return (error); 294 } 295 296 int 297 efi_get_time(struct efi_tm *tm) 298 { 299 struct efi_tmcap dummy; 300 int error; 301 302 if (efi_runtime == NULL) 303 return (ENXIO); 304 EFI_TIME_LOCK() 305 /* 306 * UEFI spec states that the Capabilities argument to GetTime is 307 * optional, but some UEFI implementations choke when passed a NULL 308 * pointer. Pass a dummy efi_tmcap, even though we won't use it, 309 * to workaround such implementations. 310 */ 311 error = efi_get_time_locked(tm, &dummy); 312 EFI_TIME_UNLOCK() 313 return (error); 314 } 315 316 int 317 efi_get_time_capabilities(struct efi_tmcap *tmcap) 318 { 319 struct efi_tm dummy; 320 int error; 321 322 if (efi_runtime == NULL) 323 return (ENXIO); 324 EFI_TIME_LOCK() 325 error = efi_get_time_locked(&dummy, tmcap); 326 EFI_TIME_UNLOCK() 327 return (error); 328 } 329 330 int 331 efi_reset_system(void) 332 { 333 int error; 334 335 error = efi_enter(); 336 if (error != 0) 337 return (error); 338 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL); 339 efi_leave(); 340 return (EIO); 341 } 342 343 static int 344 efi_set_time_locked(struct efi_tm *tm) 345 { 346 efi_status status; 347 int error; 348 349 EFI_TIME_OWNED(); 350 error = efi_enter(); 351 if (error != 0) 352 return (error); 353 status = efi_runtime->rt_settime(tm); 354 efi_leave(); 355 error = efi_status_to_errno(status); 356 return (error); 357 } 358 359 int 360 efi_set_time(struct efi_tm *tm) 361 { 362 int error; 363 364 if (efi_runtime == NULL) 365 return (ENXIO); 366 EFI_TIME_LOCK() 367 error = efi_set_time_locked(tm); 368 EFI_TIME_UNLOCK() 369 return (error); 370 } 371 372 int 373 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 374 size_t *datasize, void *data) 375 { 376 efi_status status; 377 int error; 378 379 error = efi_enter(); 380 if (error != 0) 381 return (error); 382 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data); 383 efi_leave(); 384 error = efi_status_to_errno(status); 385 return (error); 386 } 387 388 int 389 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 390 { 391 efi_status status; 392 int error; 393 394 error = efi_enter(); 395 if (error != 0) 396 return (error); 397 status = efi_runtime->rt_scanvar(namesize, name, vendor); 398 efi_leave(); 399 error = efi_status_to_errno(status); 400 return (error); 401 } 402 403 int 404 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 405 size_t datasize, void *data) 406 { 407 efi_status status; 408 int error; 409 410 error = efi_enter(); 411 if (error != 0) 412 return (error); 413 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data); 414 efi_leave(); 415 error = efi_status_to_errno(status); 416 return (error); 417 } 418 419 static int 420 efirt_modevents(module_t m, int event, void *arg __unused) 421 { 422 423 switch (event) { 424 case MOD_LOAD: 425 return (efi_init()); 426 427 case MOD_UNLOAD: 428 efi_uninit(); 429 return (0); 430 431 case MOD_SHUTDOWN: 432 return (0); 433 434 default: 435 return (EOPNOTSUPP); 436 } 437 } 438 439 static moduledata_t efirt_moddata = { 440 .name = "efirt", 441 .evhand = efirt_modevents, 442 .priv = NULL, 443 }; 444 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY); 445 MODULE_VERSION(efirt, 1); 446