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 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys); 118 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 119 efi_systbl = NULL; 120 if (bootverbose) 121 printf("EFI systbl signature invalid\n"); 122 return (0); 123 } 124 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 125 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 126 if (efi_cfgtbl == NULL) { 127 if (bootverbose) 128 printf("EFI config table is not present\n"); 129 } 130 131 kmdp = preload_search_by_type("elf kernel"); 132 if (kmdp == NULL) 133 kmdp = preload_search_by_type("elf64 kernel"); 134 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 135 MODINFO_METADATA | MODINFOMD_EFI_MAP); 136 if (efihdr == NULL) { 137 if (bootverbose) 138 printf("EFI map is not present\n"); 139 return (0); 140 } 141 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 142 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 143 if (efihdr->descriptor_size == 0) 144 return (ENOMEM); 145 146 if (!efi_create_1t1_map(map, efihdr->memory_size / 147 efihdr->descriptor_size, efihdr->descriptor_size)) { 148 if (bootverbose) 149 printf("EFI cannot create runtime map\n"); 150 return (ENOMEM); 151 } 152 153 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 154 (struct efi_rt *)efi_systbl->st_rt; 155 if (efi_runtime == NULL) { 156 if (bootverbose) 157 printf("EFI runtime services table is not present\n"); 158 efi_destroy_1t1_map(); 159 return (ENXIO); 160 } 161 162 return (0); 163 } 164 165 static void 166 efi_uninit(void) 167 { 168 169 efi_destroy_1t1_map(); 170 171 efi_systbl = NULL; 172 efi_cfgtbl = NULL; 173 efi_runtime = NULL; 174 175 mtx_destroy(&efi_lock); 176 } 177 178 int 179 efi_rt_ok(void) 180 { 181 182 if (efi_runtime == NULL) 183 return (ENXIO); 184 return (0); 185 } 186 187 static int 188 efi_enter(void) 189 { 190 struct thread *td; 191 pmap_t curpmap; 192 int error; 193 194 if (efi_runtime == NULL) 195 return (ENXIO); 196 td = curthread; 197 curpmap = &td->td_proc->p_vmspace->vm_pmap; 198 PMAP_LOCK(curpmap); 199 mtx_lock(&efi_lock); 200 error = fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); 201 if (error != 0) { 202 PMAP_UNLOCK(curpmap); 203 return (error); 204 } 205 206 return (efi_arch_enter()); 207 } 208 209 static void 210 efi_leave(void) 211 { 212 struct thread *td; 213 pmap_t curpmap; 214 215 efi_arch_leave(); 216 217 curpmap = &curproc->p_vmspace->vm_pmap; 218 td = curthread; 219 fpu_kern_leave(td, NULL); 220 mtx_unlock(&efi_lock); 221 PMAP_UNLOCK(curpmap); 222 } 223 224 int 225 efi_get_table(struct uuid *uuid, void **ptr) 226 { 227 struct efi_cfgtbl *ct; 228 u_long count; 229 230 if (efi_cfgtbl == NULL || efi_systbl == NULL) 231 return (ENXIO); 232 count = efi_systbl->st_entries; 233 ct = efi_cfgtbl; 234 while (count--) { 235 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { 236 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data); 237 return (0); 238 } 239 ct++; 240 } 241 return (ENOENT); 242 } 243 244 static int 245 efi_get_time_locked(struct efi_tm *tm) 246 { 247 efi_status status; 248 int error; 249 250 EFI_TIME_OWNED() 251 error = efi_enter(); 252 if (error != 0) 253 return (error); 254 status = efi_runtime->rt_gettime(tm, NULL); 255 efi_leave(); 256 error = efi_status_to_errno(status); 257 return (error); 258 } 259 260 int 261 efi_get_time(struct efi_tm *tm) 262 { 263 int error; 264 265 if (efi_runtime == NULL) 266 return (ENXIO); 267 EFI_TIME_LOCK() 268 error = efi_get_time_locked(tm); 269 EFI_TIME_UNLOCK() 270 return (error); 271 } 272 273 int 274 efi_reset_system(void) 275 { 276 int error; 277 278 error = efi_enter(); 279 if (error != 0) 280 return (error); 281 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL); 282 efi_leave(); 283 return (EIO); 284 } 285 286 static int 287 efi_set_time_locked(struct efi_tm *tm) 288 { 289 efi_status status; 290 int error; 291 292 EFI_TIME_OWNED(); 293 error = efi_enter(); 294 if (error != 0) 295 return (error); 296 status = efi_runtime->rt_settime(tm); 297 efi_leave(); 298 error = efi_status_to_errno(status); 299 return (error); 300 } 301 302 int 303 efi_set_time(struct efi_tm *tm) 304 { 305 int error; 306 307 if (efi_runtime == NULL) 308 return (ENXIO); 309 EFI_TIME_LOCK() 310 error = efi_set_time_locked(tm); 311 EFI_TIME_UNLOCK() 312 return (error); 313 } 314 315 int 316 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 317 size_t *datasize, void *data) 318 { 319 efi_status status; 320 int error; 321 322 error = efi_enter(); 323 if (error != 0) 324 return (error); 325 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data); 326 efi_leave(); 327 error = efi_status_to_errno(status); 328 return (error); 329 } 330 331 int 332 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 333 { 334 efi_status status; 335 int error; 336 337 error = efi_enter(); 338 if (error != 0) 339 return (error); 340 status = efi_runtime->rt_scanvar(namesize, name, vendor); 341 efi_leave(); 342 error = efi_status_to_errno(status); 343 return (error); 344 } 345 346 int 347 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 348 size_t datasize, void *data) 349 { 350 efi_status status; 351 int error; 352 353 error = efi_enter(); 354 if (error != 0) 355 return (error); 356 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data); 357 efi_leave(); 358 error = efi_status_to_errno(status); 359 return (error); 360 } 361 362 static int 363 efirt_modevents(module_t m, int event, void *arg __unused) 364 { 365 366 switch (event) { 367 case MOD_LOAD: 368 return (efi_init()); 369 370 case MOD_UNLOAD: 371 efi_uninit(); 372 return (0); 373 374 case MOD_SHUTDOWN: 375 return (0); 376 377 default: 378 return (EOPNOTSUPP); 379 } 380 } 381 382 static moduledata_t efirt_moddata = { 383 .name = "efirt", 384 .evhand = efirt_modevents, 385 .priv = NULL, 386 }; 387 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY); 388 MODULE_VERSION(efirt, 1); 389