1 /*- 2 * Copyright (c) 2004 Marcel Moolenaar 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 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 * $FreeBSD$ 27 */ 28 29 #ifndef _SYS_EFI_H_ 30 #define _SYS_EFI_H_ 31 32 #include <sys/uuid.h> 33 #include <machine/efi.h> 34 35 #define EFI_PAGE_SHIFT 12 36 #define EFI_PAGE_SIZE (1 << EFI_PAGE_SHIFT) 37 #define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1) 38 39 #define EFI_TABLE_SMBIOS \ 40 {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}} 41 #define EFI_TABLE_SMBIOS3 \ 42 {0xf2fd1544,0x9794,0x4a2c,0x99,0x2e,{0xe5,0xbb,0xcf,0x20,0xe3,0x94}} 43 44 enum efi_reset { 45 EFI_RESET_COLD = 0, 46 EFI_RESET_WARM = 1, 47 EFI_RESET_SHUTDOWN = 2, 48 }; 49 50 typedef uint16_t efi_char; 51 typedef unsigned long efi_status; 52 53 struct efi_cfgtbl { 54 struct uuid ct_uuid; 55 void *ct_data; 56 }; 57 58 struct efi_md { 59 uint32_t md_type; 60 #define EFI_MD_TYPE_NULL 0 61 #define EFI_MD_TYPE_CODE 1 /* Loader text. */ 62 #define EFI_MD_TYPE_DATA 2 /* Loader data. */ 63 #define EFI_MD_TYPE_BS_CODE 3 /* Boot services text. */ 64 #define EFI_MD_TYPE_BS_DATA 4 /* Boot services data. */ 65 #define EFI_MD_TYPE_RT_CODE 5 /* Runtime services text. */ 66 #define EFI_MD_TYPE_RT_DATA 6 /* Runtime services data. */ 67 #define EFI_MD_TYPE_FREE 7 /* Unused/free memory. */ 68 #define EFI_MD_TYPE_BAD 8 /* Bad memory */ 69 #define EFI_MD_TYPE_RECLAIM 9 /* ACPI reclaimable memory. */ 70 #define EFI_MD_TYPE_FIRMWARE 10 /* ACPI NV memory */ 71 #define EFI_MD_TYPE_IOMEM 11 /* Memory-mapped I/O. */ 72 #define EFI_MD_TYPE_IOPORT 12 /* I/O port space. */ 73 #define EFI_MD_TYPE_PALCODE 13 /* PAL */ 74 #define EFI_MD_TYPE_PERSISTENT 14 /* Persistent memory. */ 75 uint32_t __pad; 76 uint64_t md_phys; 77 uint64_t md_virt; 78 uint64_t md_pages; 79 uint64_t md_attr; 80 #define EFI_MD_ATTR_UC 0x0000000000000001UL 81 #define EFI_MD_ATTR_WC 0x0000000000000002UL 82 #define EFI_MD_ATTR_WT 0x0000000000000004UL 83 #define EFI_MD_ATTR_WB 0x0000000000000008UL 84 #define EFI_MD_ATTR_UCE 0x0000000000000010UL 85 #define EFI_MD_ATTR_WP 0x0000000000001000UL 86 #define EFI_MD_ATTR_RP 0x0000000000002000UL 87 #define EFI_MD_ATTR_XP 0x0000000000004000UL 88 #define EFI_MD_ATTR_NV 0x0000000000008000UL 89 #define EFI_MD_ATTR_MORE_RELIABLE \ 90 0x0000000000010000UL 91 #define EFI_MD_ATTR_RO 0x0000000000020000UL 92 #define EFI_MD_ATTR_RT 0x8000000000000000UL 93 }; 94 95 #define efi_next_descriptor(ptr, size) \ 96 ((struct efi_md *)(((uint8_t *)(ptr)) + (size))) 97 98 struct efi_tm { 99 uint16_t tm_year; /* 1998 - 20XX */ 100 uint8_t tm_mon; /* 1 - 12 */ 101 uint8_t tm_mday; /* 1 - 31 */ 102 uint8_t tm_hour; /* 0 - 23 */ 103 uint8_t tm_min; /* 0 - 59 */ 104 uint8_t tm_sec; /* 0 - 59 */ 105 uint8_t __pad1; 106 uint32_t tm_nsec; /* 0 - 999,999,999 */ 107 int16_t tm_tz; /* -1440 to 1440 or 2047 */ 108 uint8_t tm_dst; 109 uint8_t __pad2; 110 }; 111 112 struct efi_tmcap { 113 uint32_t tc_res; /* 1e-6 parts per million */ 114 uint32_t tc_prec; /* hertz */ 115 uint8_t tc_stz; /* Set clears sub-second time */ 116 }; 117 118 struct efi_tblhdr { 119 uint64_t th_sig; 120 uint32_t th_rev; 121 uint32_t th_hdrsz; 122 uint32_t th_crc32; 123 uint32_t __res; 124 }; 125 126 #ifdef _KERNEL 127 128 #ifdef EFIABI_ATTR 129 struct efi_rt { 130 struct efi_tblhdr rt_hdr; 131 efi_status (*rt_gettime)(struct efi_tm *, struct efi_tmcap *) 132 EFIABI_ATTR; 133 efi_status (*rt_settime)(struct efi_tm *) EFIABI_ATTR; 134 efi_status (*rt_getwaketime)(uint8_t *, uint8_t *, 135 struct efi_tm *) EFIABI_ATTR; 136 efi_status (*rt_setwaketime)(uint8_t, struct efi_tm *) 137 EFIABI_ATTR; 138 efi_status (*rt_setvirtual)(u_long, u_long, uint32_t, 139 struct efi_md *) EFIABI_ATTR; 140 efi_status (*rt_cvtptr)(u_long, void **) EFIABI_ATTR; 141 efi_status (*rt_getvar)(efi_char *, struct uuid *, uint32_t *, 142 u_long *, void *) EFIABI_ATTR; 143 efi_status (*rt_scanvar)(u_long *, efi_char *, struct uuid *) 144 EFIABI_ATTR; 145 efi_status (*rt_setvar)(efi_char *, struct uuid *, uint32_t, 146 u_long, void *) EFIABI_ATTR; 147 efi_status (*rt_gethicnt)(uint32_t *) EFIABI_ATTR; 148 efi_status (*rt_reset)(enum efi_reset, efi_status, u_long, 149 efi_char *) EFIABI_ATTR; 150 }; 151 #endif 152 153 struct efi_systbl { 154 struct efi_tblhdr st_hdr; 155 #define EFI_SYSTBL_SIG 0x5453595320494249UL 156 efi_char *st_fwvendor; 157 uint32_t st_fwrev; 158 uint32_t __pad; 159 void *st_cin; 160 void *st_cinif; 161 void *st_cout; 162 void *st_coutif; 163 void *st_cerr; 164 void *st_cerrif; 165 uint64_t st_rt; 166 void *st_bs; 167 u_long st_entries; 168 uint64_t st_cfgtbl; 169 }; 170 171 extern vm_paddr_t efi_systbl_phys; 172 173 struct efirt_callinfo; 174 175 /* Internal MD EFI functions */ 176 int efi_arch_enter(void); 177 void efi_arch_leave(void); 178 vm_offset_t efi_phys_to_kva(vm_paddr_t); 179 int efi_rt_arch_call(struct efirt_callinfo *); 180 bool efi_create_1t1_map(struct efi_md *, int, int); 181 void efi_destroy_1t1_map(void); 182 183 struct efi_ops { 184 /* 185 * The EFI calls might be virtualized in some environments, requiring 186 * FreeBSD to use a different interface (ie: hypercalls) in order to 187 * access them. 188 */ 189 int (*rt_ok)(void); 190 int (*get_table)(struct uuid *, void **); 191 int (*get_time)(struct efi_tm *); 192 int (*get_time_capabilities)(struct efi_tmcap *); 193 int (*reset_system)(enum efi_reset); 194 int (*set_time)(struct efi_tm *); 195 int (*var_get)(uint16_t *, struct uuid *, uint32_t *, size_t *, 196 void *); 197 int (*var_nextname)(size_t *, uint16_t *, struct uuid *); 198 int (*var_set)(uint16_t *, struct uuid *, uint32_t, size_t, void *); 199 }; 200 extern const struct efi_ops *active_efi_ops; 201 202 /* Public MI EFI functions */ 203 static inline int efi_rt_ok(void) 204 { 205 206 if (active_efi_ops->rt_ok == NULL) 207 return (ENXIO); 208 return (active_efi_ops->rt_ok()); 209 } 210 211 static inline int efi_get_table(struct uuid *uuid, void **ptr) 212 { 213 214 if (active_efi_ops->get_table == NULL) 215 return (ENXIO); 216 return (active_efi_ops->get_table(uuid, ptr)); 217 } 218 219 static inline int efi_get_time(struct efi_tm *tm) 220 { 221 222 if (active_efi_ops->get_time == NULL) 223 return (ENXIO); 224 return (active_efi_ops->get_time(tm)); 225 } 226 227 static inline int efi_get_time_capabilities(struct efi_tmcap *tmcap) 228 { 229 230 if (active_efi_ops->get_time_capabilities == NULL) 231 return (ENXIO); 232 return (active_efi_ops->get_time_capabilities(tmcap)); 233 } 234 235 static inline int efi_reset_system(enum efi_reset type) 236 { 237 238 if (active_efi_ops->reset_system == NULL) 239 return (ENXIO); 240 return (active_efi_ops->reset_system(type)); 241 } 242 243 static inline int efi_set_time(struct efi_tm *tm) 244 { 245 246 if (active_efi_ops->set_time == NULL) 247 return (ENXIO); 248 return (active_efi_ops->set_time(tm)); 249 } 250 251 static inline int efi_var_get(uint16_t *name, struct uuid *vendor, 252 uint32_t *attrib, size_t *datasize, void *data) 253 { 254 255 if (active_efi_ops->var_get == NULL) 256 return (ENXIO); 257 return (active_efi_ops->var_get(name, vendor, attrib, datasize, data)); 258 } 259 260 static inline int efi_var_nextname(size_t *namesize, uint16_t *name, 261 struct uuid *vendor) 262 { 263 264 if (active_efi_ops->var_nextname == NULL) 265 return (ENXIO); 266 return (active_efi_ops->var_nextname(namesize, name, vendor)); 267 } 268 269 static inline int efi_var_set(uint16_t *name, struct uuid *vendor, 270 uint32_t attrib, size_t datasize, void *data) 271 { 272 273 if (active_efi_ops->var_set == NULL) 274 return (ENXIO); 275 return (active_efi_ops->var_set(name, vendor, attrib, datasize, data)); 276 } 277 278 int efi_status_to_errno(efi_status status); 279 280 #endif /* _KERNEL */ 281 282 #endif /* _SYS_EFI_H_ */ 283