1 | /** @file
|
---|
2 | * IPRT - EFI related utilities.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2021 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_efi_h
|
---|
27 | #define IPRT_INCLUDED_efi_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/cdefs.h>
|
---|
33 | #include <iprt/types.h>
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/vfs.h>
|
---|
36 |
|
---|
37 | #include <iprt/formats/efi-common.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | RT_C_DECLS_BEGIN
|
---|
41 |
|
---|
42 | /** @defgroup grp_rt_efi RTEfi - EFI utilities
|
---|
43 | * @ingroup grp_rt
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 |
|
---|
48 | #ifdef IN_RING3
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Converts an EFI time to a time spec (UTC).
|
---|
52 | *
|
---|
53 | * @returns pTimeSpec on success.
|
---|
54 | * @returns NULL if the pEfiTime data is invalid.
|
---|
55 | * @param pTimeSpec Where to store the converted time.
|
---|
56 | * @param pEfiTime Pointer to the EFI time struct.
|
---|
57 | */
|
---|
58 | RTDECL(PRTTIMESPEC) RTEfiTimeToTimeSpec(PRTTIMESPEC pTimeSpec, PCEFI_TIME pEfiTime);
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Converts a time spec (UTC) to an EFI time.
|
---|
63 | *
|
---|
64 | * @returns pEfiTime on success.
|
---|
65 | * @returns NULL if the pTimeSpec data is invalid.
|
---|
66 | * @param pEfiTime Pointer to the EFI time struct.
|
---|
67 | * @param pTimeSpec The time spec to convert.
|
---|
68 | */
|
---|
69 | RTDECL(PEFI_TIME) RTEfiTimeFromTimeSpec(PEFI_TIME pEfiTime, PCRTTIMESPEC pTimeSpec);
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Converts the given EFI GUID to the IPRT UUID representation.
|
---|
74 | *
|
---|
75 | * @returns pUuid.
|
---|
76 | * @param pUuid Where to store the converted GUID.
|
---|
77 | * @param pEfiGuid The EFI GUID to convert.
|
---|
78 | */
|
---|
79 | RTDECL(PRTUUID) RTEfiGuidToUuid(PRTUUID pUuid, PCEFI_GUID pEfiGuid);
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Converts the given EFI GUID to the IPRT UUID representation.
|
---|
84 | *
|
---|
85 | * @returns pEfiGuid.
|
---|
86 | * @param pEfiGuid Where to store the converted UUID.
|
---|
87 | * @param pUuid The UUID to convert.
|
---|
88 | */
|
---|
89 | RTDECL(PEFI_GUID) RTEfiGuidFromUuid(PEFI_GUID pEfiGuid, PCRTUUID pUuid);
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Compares two EFI GUID values.
|
---|
94 | *
|
---|
95 | * @returns 0 if eq, < 0 or > 0.
|
---|
96 | * @param pGuid1 First value to compare.
|
---|
97 | * @param pGuid2 Second value to compare.
|
---|
98 | */
|
---|
99 | RTDECL(int) RTEfiGuidCompare(PCEFI_GUID pGuid1, PCEFI_GUID pGuid2);
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Opens an EFI variable store.
|
---|
104 | *
|
---|
105 | * @returns IPRT status code.
|
---|
106 | * @param hVfsFileIn The file or device backing the store.
|
---|
107 | * @param fMntFlags RTVFSMNT_F_XXX.
|
---|
108 | * @param fVarStoreFlags Reserved, MBZ.
|
---|
109 | * @param phVfs Where to return the virtual file system handle.
|
---|
110 | * @param pErrInfo Where to return additional error information.
|
---|
111 | */
|
---|
112 | RTDECL(int) RTEfiVarStoreOpenAsVfs(RTVFSFILE hVfsFileIn, uint32_t fMntFlags, uint32_t fVarStoreFlags, PRTVFS phVfs, PRTERRINFO pErrInfo);
|
---|
113 |
|
---|
114 |
|
---|
115 | /** @name RTEFIVARSTORE_CREATE_F_XXX - RTEfiVarStoreCreate flags
|
---|
116 | * @{ */
|
---|
117 | /** Use default options. */
|
---|
118 | #define RTEFIVARSTORE_CREATE_F_DEFAULT UINT32_C(0)
|
---|
119 | /** Don't create a fault tolerant write working space.
|
---|
120 | * The default is to create one reducing the size of the variable store. */
|
---|
121 | #define RTEFIVARSTORE_CREATE_F_NO_FTW_WORKING_SPACE RT_BIT_32(0)
|
---|
122 | /** Mask containing all valid flags. */
|
---|
123 | #define RTEFIVARSTORE_CREATE_F_VALID_MASK UINT32_C(0x00000001)
|
---|
124 | /** @} */
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Creates a new EFI variable store.
|
---|
128 | *
|
---|
129 | * @returns IRPT status code.
|
---|
130 | * @param hVfsFile The store file.
|
---|
131 | * @param offStore The offset into @a hVfsFile of the file.
|
---|
132 | * Typically 0.
|
---|
133 | * @param cbStore The size of the variable store. Pass 0 if the rest of
|
---|
134 | * hVfsFile should be used. The remaining space for variables
|
---|
135 | * will be less because of some metadata overhead.
|
---|
136 | * @param fFlags See RTEFIVARSTORE_F_XXX.
|
---|
137 | * @param cbBlock The logical block size.
|
---|
138 | * @param pErrInfo Additional error information, maybe. Optional.
|
---|
139 | */
|
---|
140 | RTDECL(int) RTEfiVarStoreCreate(RTVFSFILE hVfsFile, uint64_t offStore, uint64_t cbStore, uint32_t fFlags, uint32_t cbBlock,
|
---|
141 | PRTERRINFO pErrInfo);
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * EFI signature type.
|
---|
146 | */
|
---|
147 | typedef enum RTEFISIGTYPE
|
---|
148 | {
|
---|
149 | /** Invalid type, do not use. */
|
---|
150 | RTEFISIGTYPE_INVALID = 0,
|
---|
151 | /** First valid signature type. */
|
---|
152 | RTEFISIGTYPE_FIRST_VALID,
|
---|
153 | /** Signature contains a SHA256 hash. */
|
---|
154 | RTEFISIGTYPE_SHA256 = RTEFISIGTYPE_FIRST_VALID,
|
---|
155 | /** Signature contains a RSA2048 key (only the modulus in big endian form,
|
---|
156 | * the exponent is always 65537/0x10001). */
|
---|
157 | RTEFISIGTYPE_RSA2048,
|
---|
158 | /** Signature contains a RSA2048 signature of a SHA256 hash. */
|
---|
159 | RTEFISIGTYPE_RSA2048_SHA256,
|
---|
160 | /** Signature contains a SHA1 hash. */
|
---|
161 | RTEFISIGTYPE_SHA1,
|
---|
162 | /** Signature contains a RSA2048 signature of a SHA1 hash. */
|
---|
163 | RTEFISIGTYPE_RSA2048_SHA1,
|
---|
164 | /** Signature contains a DER encoded X.509 certificate. */
|
---|
165 | RTEFISIGTYPE_X509,
|
---|
166 | /** First invalid type (do not use). */
|
---|
167 | RTEFISIGTYPE_FIRST_INVALID,
|
---|
168 | /** 32bit blowup hack.*/
|
---|
169 | RTEFISIGTYPE_32BIT_HACK = 0x7fffffff
|
---|
170 | } RTEFISIGTYPE;
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * EFI signature database enumeration callback.
|
---|
175 | *
|
---|
176 | * @returns IPRT status code, any status code other than VINF_SUCCESS will abort the enumeration.
|
---|
177 | * @param hEfiSigDb Handle to the EFI signature database this callback is called on.
|
---|
178 | * @param enmSigType The signature type.
|
---|
179 | * @param pUuidOwner Signature owner UUID.
|
---|
180 | * @param pvSig The signature data (dependent on the type).
|
---|
181 | * @param cbSig Size of the signature in bytes.
|
---|
182 | * @param pvUser Opaque user data passed in RTEfiSigDbEnum().
|
---|
183 | */
|
---|
184 | typedef DECLCALLBACKTYPE(int, FNRTEFISIGDBENUMSIG,(RTEFISIGDB hEfiSigDb, RTEFISIGTYPE enmSigType, PCRTUUID pUuidOwner,
|
---|
185 | const void *pvSig, size_t cbSig, void *pvUser));
|
---|
186 | /** Pointer to a EFI signature database enumeration callback. */
|
---|
187 | typedef FNRTEFISIGDBENUMSIG *PFNRTEFISIGDBENUMSIG;
|
---|
188 |
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Creates an empty EFI signature database.
|
---|
192 | *
|
---|
193 | * @returns IPRT status code.
|
---|
194 | * @param phEfiSigDb Where to store the handle to the empty EFI signature database on success.
|
---|
195 | */
|
---|
196 | RTDECL(int) RTEfiSigDbCreate(PRTEFISIGDB phEfiSigDb);
|
---|
197 |
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Destroys the given EFI signature database handle.
|
---|
201 | *
|
---|
202 | * @returns IPRT status code.
|
---|
203 | * @param hEfiSigDb The EFI signature database handle to destroy.
|
---|
204 | */
|
---|
205 | RTDECL(int) RTEfiSigDbDestroy(RTEFISIGDB hEfiSigDb);
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Adds the signatures from an existing signature database contained in the given file.
|
---|
210 | *
|
---|
211 | * @returns IPRT status code.
|
---|
212 | * @param hEfiSigDb The EFI signature database handle.
|
---|
213 | * @param hVfsFileIn The file handle containing the existing signature database.
|
---|
214 | */
|
---|
215 | RTDECL(int) RTEfiSigDbAddFromExistingDb(RTEFISIGDB hEfiSigDb, RTVFSFILE hVfsFileIn);
|
---|
216 |
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Adds a new signature to the given signature database from the given file.
|
---|
220 | *
|
---|
221 | * @returns IPRT status code.
|
---|
222 | * @param hEfiSigDb The EFI signature database handle.
|
---|
223 | * @param enmSigType Type of the signature.
|
---|
224 | * @param pUuidOwner The UUID of the signature owner.
|
---|
225 | * @param hVfsFileIn File handle containing the signature data.
|
---|
226 | */
|
---|
227 | RTDECL(int) RTEfiSigDbAddSignatureFromFile(RTEFISIGDB hEfiSigDb, RTEFISIGTYPE enmSigType, PCRTUUID pUuidOwner, RTVFSFILE hVfsFileIn);
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Adds a new signature to the given signature database from the given buffer.
|
---|
232 | *
|
---|
233 | * @returns IPRT status code.
|
---|
234 | * @param hEfiSigDb The EFI signature database handle.
|
---|
235 | * @param enmSigType Type of the signature.
|
---|
236 | * @param pUuidOwner The UUID of the signature owner.
|
---|
237 | * @param pvBuf Pointer to the signature data.
|
---|
238 | * @param cbBuf Size of the signature data in bytes.
|
---|
239 | */
|
---|
240 | RTDECL(int) RTEfiSigDbAddSignatureFromBuf(RTEFISIGDB hEfiSigDb, RTEFISIGTYPE enmSigType, PCRTUUID pUuidOwner,
|
---|
241 | const void *pvBuf, size_t cbBuf);
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Writes the given EFI signature database to the given file.
|
---|
246 | *
|
---|
247 | * @returns IPRT status code.
|
---|
248 | * @param hEfiSigDb The EFI signature database handle.
|
---|
249 | * @param hVfsFileOut The file handle to write the signature database to.
|
---|
250 | */
|
---|
251 | RTDECL(int) RTEfiSigDbWriteToFile(RTEFISIGDB hEfiSigDb, RTVFSFILE hVfsFileOut);
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Enumerate all signatures in the given EFI signature database.
|
---|
256 | *
|
---|
257 | * @returns IPRT status code.
|
---|
258 | * @param hEfiSigDb The EFI signature database handle.
|
---|
259 | * @param pfnEnumSig The callback to call for each signature.
|
---|
260 | * @param pvUser Opaque user data to pass to the callback.
|
---|
261 | */
|
---|
262 | RTDECL(int) RTEfiSigDbEnum(RTEFISIGDB hEfiSigDb, PFNRTEFISIGDBENUMSIG pfnEnumSig, void *pvUser);
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Returns a human readable string of the given signature type.
|
---|
267 | *
|
---|
268 | * @returns Human readable string.
|
---|
269 | * @param enmSigType The signature type.
|
---|
270 | */
|
---|
271 | RTDECL(const char *) RTEfiSigDbTypeStringify(RTEFISIGTYPE enmSigType);
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Returns a pointer to the EFI GUID identifying the given signature type.
|
---|
276 | *
|
---|
277 | * @returns Pointer to the EFI GUID.
|
---|
278 | * @param enmSigType The signature type.
|
---|
279 | */
|
---|
280 | RTDECL(PCEFI_GUID) RTEfiSigDbTypeGetGuid(RTEFISIGTYPE enmSigType);
|
---|
281 |
|
---|
282 | #endif /* IN_RING3 */
|
---|
283 |
|
---|
284 | /** @} */
|
---|
285 |
|
---|
286 | RT_C_DECLS_END
|
---|
287 |
|
---|
288 | #endif /* !IPRT_INCLUDED_efi_h */
|
---|
289 |
|
---|