VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/efi/efivarstorevfs.cpp@ 90061

Last change on this file since 90061 was 90061, checked in by vboxsync, 3 years ago

Runtime/RTEfi*: Start of a VFS module to access a EFI variable store stored in the NVRAM file to allow changing the firmware config without having the VM running, bugref:9580

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.3 KB
Line 
1/* $Id: efivarstorevfs.cpp 90061 2021-07-06 12:48:53Z vboxsync $ */
2/** @file
3 * IPRT - Expose a EFI variable store as a Virtual Filesystem.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FS
32#include <iprt/efi.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/file.h>
37#include <iprt/err.h>
38#include <iprt/log.h>
39#include <iprt/mem.h>
40#include <iprt/string.h>
41#include <iprt/uuid.h>
42#include <iprt/utf16.h>
43#include <iprt/vfs.h>
44#include <iprt/vfslowlevel.h>
45#include <iprt/formats/efi-fv.h>
46#include <iprt/formats/efi-varstore.h>
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57/** Pointer to the varstore filesystem data. */
58typedef struct RTEFIVARSTORE *PRTEFIVARSTORE;
59
60
61/**
62 * EFI variable entry.
63 */
64typedef struct RTEFIVAR
65{
66 /** Pointer to the owning variable store. */
67 PRTEFIVARSTORE pVarStore;
68 /** Offset of the variable header located in the backing image. */
69 uint64_t offVarHdr;
70 /** Offset of the variable data located in the backing image. */
71 uint64_t offVarData;
72 /** The validated variable header. */
73 EFI_AUTH_VAR_HEADER VarHdr;
74 /** Name of the variable. */
75 char *pszName;
76 /** The time creation/update time. */
77 RTTIMESPEC Time;
78 /** The vendor UUID of the variable. */
79 RTUUID Uuid;
80} RTEFIVAR;
81/** Pointer to an EFI variable. */
82typedef RTEFIVAR *PRTEFIVAR;
83
84
85/**
86 * EFI GUID entry.
87 */
88typedef struct RTEFIGUID
89{
90 /** The UUID rpresentation of the GUID. */
91 RTUUID Uuid;
92 /** Pointer to the array of indices into RTEFIVARSTORE::paVars. */
93 uint32_t *paidxVars;
94 /** Number of valid indices in the array. */
95 uint32_t cVars;
96 /** Maximum number of indices the array can hold. */
97 uint32_t cVarsMax;
98} RTEFIGUID;
99/** Pointer to an EFI variable. */
100typedef RTEFIGUID *PRTEFIGUID;
101
102
103/**
104 * EFI variable store filesystem volume.
105 */
106typedef struct RTEFIVARSTORE
107{
108 /** Handle to itself. */
109 RTVFS hVfsSelf;
110 /** The file, partition, or whatever backing the volume has. */
111 RTVFSFILE hVfsBacking;
112 /** The size of the backing thingy. */
113 uint64_t cbBacking;
114
115 /** RTVFSMNT_F_XXX. */
116 uint32_t fMntFlags;
117 /** RTEFIVARSTOREVFS_F_XXX (currently none defined). */
118 uint32_t fVarStoreFlags;
119
120 /** Size of the variable store (minus the header). */
121 size_t cbVarStore;
122 /** Start offset into the backing image where the variable data starts. */
123 uint64_t offStoreData;
124 /** Flag whether the variable store uses authenticated variables. */
125 bool fAuth;
126 /** Number of bytes occupied by existing variables. */
127 uint64_t cbVarData;
128
129 /** Pointer to the array of variables sorted by start offset. */
130 PRTEFIVAR paVars;
131 /** Number of valid variables in the array. */
132 uint32_t cVars;
133 /** Maximum number of variables the array can hold. */
134 uint32_t cVarsMax;
135
136 /** Pointer to the array of vendor GUIDS. */
137 PRTEFIGUID paGuids;
138 /** Number of valid GUIDS in the array. */
139 uint32_t cGuids;
140 /** Maximum number of GUIDS the array can hold. */
141 uint32_t cGuidsMax;
142
143} RTEFIVARSTORE;
144
145
146/**
147 * Variable store directory type.
148 */
149typedef enum RTEFIVARSTOREDIRTYPE
150{
151 /** Invalid directory type. */
152 RTEFIVARSTOREDIRTYPE_INVALID = 0,
153 /** Root directory type. */
154 RTEFIVARSTOREDIRTYPE_ROOT,
155 /** 'by-name' directory. */
156 RTEFIVARSTOREDIRTYPE_BY_NAME,
157 /** 'by-guid' directory. */
158 RTEFIVARSTOREDIRTYPE_BY_GUID,
159 /** Specific 'by-uuid/{...}' directory. */
160 RTEFIVARSTOREDIRTYPE_GUID,
161 /** 32bit blowup hack. */
162 RTEFIVARSTOREDIRTYPE_32BIT_HACK = 0x7fffffff
163} RTEFIVARSTOREDIRTYPE;
164
165
166/**
167 * Variable store directory.
168 */
169typedef struct RTEFIVARSTOREDIR
170{
171 /* Flag whether we reached the end of directory entries. */
172 bool fNoMoreFiles;
173 /** The index of the next item to read. */
174 uint32_t idxNext;
175 /** Type of the directory. */
176 RTEFIVARSTOREDIRTYPE enmType;
177 /** The variable store associated with this directory. */
178 PRTEFIVARSTORE pVarStore;
179 /** Pointer to the GUID entry, only valid for RTEFIVARSTOREDIRTYPE_GUID. */
180 PRTEFIGUID pGuid;
181 /** Time when the directory was created. */
182 RTTIMESPEC Time;
183} RTEFIVARSTOREDIR;
184/** Pointer to an Variable store directory. */
185typedef RTEFIVARSTOREDIR *PRTEFIVARSTOREDIR;
186
187
188/**
189 * Open file instance.
190 */
191typedef struct RTEFIVARFILE
192{
193 /** Variable store this file belongs to. */
194 PRTEFIVARSTORE pVarStore;
195 /** The underlying variable structure. */
196 PRTEFIVAR pVar;
197 /** Current offset into the file for I/O. */
198 RTFOFF offFile;
199} RTEFIVARFILE;
200/** Pointer to an open file instance. */
201typedef RTEFIVARFILE *PRTEFIVARFILE;
202
203
204/*********************************************************************************************************************************
205* Internal Functions *
206*********************************************************************************************************************************/
207static int rtEfiVarStore_NewDirByType(PRTEFIVARSTORE pThis, RTEFIVARSTOREDIRTYPE enmDirType, PRTEFIGUID pGuid, PRTVFSOBJ phVfsObj);
208
209
210#ifdef LOG_ENABLED
211/**
212 * Logs a firmware volume header.
213 *
214 * @returns nothing.
215 * @param pFvHdr The firmware volume header.
216 */
217static void rtEfiVarStoreFvHdr_Log(PCEFI_FIRMWARE_VOLUME_HEADER pFvHdr)
218{
219 if (LogIs2Enabled())
220 {
221 Log2(("EfiVarStore: Volume Header:\n"));
222 Log2(("EfiVarStore: abZeroVec %#.*Rhxs\n", sizeof(pFvHdr->abZeroVec), &pFvHdr->abZeroVec[0]));
223 Log2(("EfiVarStore: GuidFilesystem %#.*Rhxs\n", sizeof(pFvHdr->GuidFilesystem), &pFvHdr->GuidFilesystem));
224 Log2(("EfiVarStore: cbFv %#RX64\n", RT_LE2H_U64(pFvHdr->cbFv)));
225 Log2(("EfiVarStore: u32Signature %#RX32\n", RT_LE2H_U32(pFvHdr->u32Signature)));
226 Log2(("EfiVarStore: fAttr %#RX32\n", RT_LE2H_U32(pFvHdr->fAttr)));
227 Log2(("EfiVarStore: cbFvHdr %#RX16\n", RT_LE2H_U16(pFvHdr->cbFvHdr)));
228 Log2(("EfiVarStore: u16Chksum %#RX16\n", RT_LE2H_U16(pFvHdr->u16Chksum)));
229 Log2(("EfiVarStore: offExtHdr %#RX16\n", RT_LE2H_U16(pFvHdr->offExtHdr)));
230 Log2(("EfiVarStore: bRsvd %#RX8\n", pFvHdr->bRsvd));
231 Log2(("EfiVarStore: bRevision %#RX8\n", pFvHdr->bRevision));
232 }
233}
234
235
236/**
237 * Logs a variable store header.
238 *
239 * @returns nothing.
240 * @param pStoreHdr The variable store header.
241 */
242static void rtEfiVarStoreHdr_Log(PCEFI_VARSTORE_HEADER pStoreHdr)
243{
244 if (LogIs2Enabled())
245 {
246 Log2(("EfiVarStore: Variable Store Header:\n"));
247 Log2(("EfiVarStore: GuidVarStore %#.*Rhxs\n", sizeof(pStoreHdr->GuidVarStore), &pStoreHdr->GuidVarStore));
248 Log2(("EfiVarStore: cbVarStore %#RX32\n", RT_LE2H_U32(pStoreHdr->cbVarStore)));
249 Log2(("EfiVarStore: bFmt %#RX8\n", pStoreHdr->bFmt));
250 Log2(("EfiVarStore: bState %#RX8\n", pStoreHdr->bState));
251 }
252}
253
254
255/**
256 * Logs a authenticated variable header.
257 *
258 * @returns nothing.
259 * @param pVarHdr The authenticated variable header.
260 * @param offVar Offset of the authenticated variable header.
261 */
262static void rtEfiVarStoreAuthVarHdr_Log(PCEFI_AUTH_VAR_HEADER pVarHdr, uint64_t offVar)
263{
264 if (LogIs2Enabled())
265 {
266 Log2(("EfiVarStore: Authenticated Variable Header at offset %#RU64:\n", offVar));
267 Log2(("EfiVarStore: u16StartId %#RX16\n", RT_LE2H_U16(pVarHdr->u16StartId)));
268 Log2(("EfiVarStore: bState %#RX8\n", pVarHdr->bState));
269 Log2(("EfiVarStore: bRsvd %#RX8\n", pVarHdr->bRsvd));
270 Log2(("EfiVarStore: fAttr %#RX32\n", RT_LE2H_U32(pVarHdr->fAttr)));
271 Log2(("EfiVarStore: cMonotonic %#RX64\n", RT_LE2H_U64(pVarHdr->cMonotonic)));
272 Log2(("EfiVarStore: Timestamp.u16Year %#RX16\n", RT_LE2H_U16(pVarHdr->Timestamp.u16Year)));
273 Log2(("EfiVarStore: Timestamp.u8Month %#RX8\n", pVarHdr->Timestamp.u8Month));
274 Log2(("EfiVarStore: Timestamp.u8Day %#RX8\n", pVarHdr->Timestamp.u8Day));
275 Log2(("EfiVarStore: Timestamp.u8Hour %#RX8\n", pVarHdr->Timestamp.u8Hour));
276 Log2(("EfiVarStore: Timestamp.u8Minute %#RX8\n", pVarHdr->Timestamp.u8Minute));
277 Log2(("EfiVarStore: Timestamp.u8Second %#RX8\n", pVarHdr->Timestamp.u8Second));
278 Log2(("EfiVarStore: Timestamp.bPad0 %#RX8\n", pVarHdr->Timestamp.bPad0));
279 Log2(("EfiVarStore: Timestamp.u32Nanosecond %#RX32\n", RT_LE2H_U32(pVarHdr->Timestamp.u32Nanosecond)));
280 Log2(("EfiVarStore: Timestamp.iTimezone %#RI16\n", RT_LE2H_S16(pVarHdr->Timestamp.iTimezone)));
281 Log2(("EfiVarStore: Timestamp.u8Daylight %#RX8\n", pVarHdr->Timestamp.u8Daylight));
282 Log2(("EfiVarStore: Timestamp.bPad1 %#RX8\n", pVarHdr->Timestamp.bPad1));
283 Log2(("EfiVarStore: idPubKey %#RX32\n", RT_LE2H_U32(pVarHdr->idPubKey)));
284 Log2(("EfiVarStore: cbName %#RX32\n", RT_LE2H_U32(pVarHdr->cbName)));
285 Log2(("EfiVarStore: cbData %#RX32\n", RT_LE2H_U32(pVarHdr->cbData)));
286 Log2(("EfiVarStore: GuidVendor %#.*Rhxs\n", sizeof(pVarHdr->GuidVendor), &pVarHdr->GuidVendor));
287 }
288}
289#endif
290
291
292/**
293 * Worker for rtEfiVarStoreFile_QueryInfo() and rtEfiVarStoreDir_QueryInfo().
294 *
295 * @returns IPRT status code.
296 * @param cbObject Size of the object in bytes.
297 * @param fIsDir Flag whether the object is a directory or file.
298 * @param pTime The time to use.
299 * @param pObjInfo The FS object information structure to fill in.
300 * @param enmAddAttr What to fill in.
301 */
302static int rtEfiVarStore_QueryInfo(uint64_t cbObject, bool fIsDir, PCRTTIMESPEC pTime, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
303{
304 pObjInfo->cbObject = cbObject;
305 pObjInfo->cbAllocated = cbObject;
306 pObjInfo->AccessTime = *pTime;
307 pObjInfo->ModificationTime = *pTime;
308 pObjInfo->ChangeTime = *pTime;
309 pObjInfo->BirthTime = *pTime;
310 pObjInfo->Attr.fMode = fIsDir
311 ? RTFS_TYPE_DIRECTORY | RTFS_UNIX_ALL_PERMS
312 : RTFS_TYPE_FILE | RTFS_UNIX_ALL_PERMS;
313 pObjInfo->Attr.enmAdditional = enmAddAttr;
314
315 switch (enmAddAttr)
316 {
317 case RTFSOBJATTRADD_NOTHING: RT_FALL_THRU();
318 case RTFSOBJATTRADD_UNIX:
319 pObjInfo->Attr.u.Unix.uid = NIL_RTUID;
320 pObjInfo->Attr.u.Unix.gid = NIL_RTGID;
321 pObjInfo->Attr.u.Unix.cHardlinks = 1;
322 pObjInfo->Attr.u.Unix.INodeIdDevice = 0;
323 pObjInfo->Attr.u.Unix.INodeId = 0;
324 pObjInfo->Attr.u.Unix.fFlags = 0;
325 pObjInfo->Attr.u.Unix.GenerationId = 0;
326 pObjInfo->Attr.u.Unix.Device = 0;
327 break;
328 case RTFSOBJATTRADD_UNIX_OWNER:
329 pObjInfo->Attr.u.UnixOwner.uid = 0;
330 pObjInfo->Attr.u.UnixOwner.szName[0] = '\0';
331 break;
332 case RTFSOBJATTRADD_UNIX_GROUP:
333 pObjInfo->Attr.u.UnixGroup.gid = 0;
334 pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
335 break;
336 case RTFSOBJATTRADD_EASIZE:
337 pObjInfo->Attr.u.EASize.cb = 0;
338 break;
339 default:
340 return VERR_INVALID_PARAMETER;
341 }
342 return VINF_SUCCESS;
343}
344
345
346/**
347 * Tries to find and return the GUID entry for the given UUID.
348 *
349 * @returns Pointer to the GUID entry or NULL if not found.
350 * @param pThis The EFI variable store instance.
351 * @param pUuid The UUID to look for.
352 */
353static PRTEFIGUID rtEfiVarStore_GetGuid(PRTEFIVARSTORE pThis, PCRTUUID pUuid)
354{
355 for (uint32_t i = 0; i < pThis->cGuids; i++)
356 if (!RTUuidCompare(&pThis->paGuids[i].Uuid, pUuid))
357 return &pThis->paGuids[i];
358
359 return NULL;
360}
361
362
363/**
364 * Adds the given UUID to the array of known GUIDs.
365 *
366 * @returns Pointer to the GUID entry or NULL if out of memory.
367 * @param pThis The EFI variable store instance.
368 * @param pUuid The UUID to add.
369 */
370static PRTEFIGUID rtEfiVarStore_AddGuid(PRTEFIVARSTORE pThis, PCRTUUID pUuid)
371{
372 if (pThis->cGuids == pThis->cGuidsMax)
373 {
374 /* Grow the array. */
375 uint32_t cGuidsMaxNew = pThis->cGuidsMax + 10;
376 PRTEFIGUID paGuidsNew = (PRTEFIGUID)RTMemRealloc(pThis->paGuids, cGuidsMaxNew * sizeof(RTEFIGUID));
377 if (!paGuidsNew)
378 return NULL;
379
380 pThis->paGuids = paGuidsNew;
381 pThis->cGuidsMax = cGuidsMaxNew;
382 }
383
384 PRTEFIGUID pGuid = &pThis->paGuids[pThis->cGuids++];
385 pGuid->Uuid = *pUuid;
386 pGuid->paidxVars = NULL;
387 pGuid->cVars = 0;
388 pGuid->cVarsMax = 0;
389 return pGuid;
390}
391
392
393/**
394 * Adds the given variable to the GUID array.
395 *
396 * @returns IPRT status code.
397 * @param pThis The EFI variable store instance.
398 * @param pUuid The UUID of the variable.
399 * @param idVar The variable index into the array.
400 */
401static int rtEfiVarStore_AddVarByGuid(PRTEFIVARSTORE pThis, PCRTUUID pUuid, uint32_t idVar)
402{
403 PRTEFIGUID pGuid = rtEfiVarStore_GetGuid(pThis, pUuid);
404 if (!pGuid)
405 pGuid = rtEfiVarStore_AddGuid(pThis, pUuid);
406
407 if ( pGuid
408 && pGuid->cVars == pGuid->cVarsMax)
409 {
410 /* Grow the array. */
411 uint32_t cVarsMaxNew = pGuid->cVarsMax + 10;
412 uint32_t *paidxVarsNew = (uint32_t *)RTMemRealloc(pGuid->paidxVars, cVarsMaxNew * sizeof(uint32_t));
413 if (!paidxVarsNew)
414 return VERR_NO_MEMORY;
415
416 pGuid->paidxVars = paidxVarsNew;
417 pGuid->cVarsMax = cVarsMaxNew;
418 }
419
420 int rc = VINF_SUCCESS;
421 if (pGuid)
422 pGuid->paidxVars[pGuid->cVars++] = idVar;
423 else
424 rc = VERR_NO_MEMORY;
425
426 return rc;
427}
428
429
430/*
431 *
432 * File operations.
433 * File operations.
434 * File operations.
435 *
436 */
437
438/**
439 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
440 */
441static DECLCALLBACK(int) rtEfiVarStoreFile_Close(void *pvThis)
442{
443 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
444 LogFlow(("rtEfiVarStoreFile_Close(%p/%p)\n", pThis, pThis->pVar));
445 RT_NOREF(pThis);
446 return VINF_SUCCESS;
447}
448
449
450/**
451 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
452 */
453static DECLCALLBACK(int) rtEfiVarStoreFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
454{
455 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
456 return rtEfiVarStore_QueryInfo(RT_LE2H_U32(pThis->pVar->VarHdr.cbData), false /*fIsDir*/,
457 &pThis->pVar->Time, pObjInfo, enmAddAttr);
458}
459
460
461/**
462 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
463 */
464static DECLCALLBACK(int) rtEfiVarStoreFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
465{
466 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
467 PRTEFIVARSTORE pVarStore = pThis->pVarStore;
468 AssertReturn(pSgBuf->cSegs == 1, VERR_INTERNAL_ERROR_3);
469 RT_NOREF(fBlocking);
470
471 if (off == -1)
472 off = pThis->offFile;
473 else
474 AssertReturn(off >= 0, VERR_INTERNAL_ERROR_3);
475
476 int rc;
477 size_t cbRead = pSgBuf->paSegs[0].cbSeg;
478 size_t cbData = RT_LE2H_U32(pThis->pVar->VarHdr.cbData);
479 size_t cbThisRead = RT_MIN(cbData - off, cbRead);
480 uint64_t offStart = pThis->pVar->offVarData + off;
481 if (!pcbRead)
482 {
483 if (cbThisRead == cbRead)
484 rc = RTVfsFileReadAt(pVarStore->hVfsBacking, offStart, pSgBuf->paSegs[0].pvSeg, cbThisRead, NULL);
485 else
486 rc = VERR_EOF;
487
488 if (RT_SUCCESS(rc))
489 pThis->offFile = off + cbThisRead;
490 Log6(("rtFsEfiVarStore_Read: off=%#RX64 cbSeg=%#x -> %Rrc\n", off, pSgBuf->paSegs[0].cbSeg, rc));
491 }
492 else
493 {
494 if ((uint64_t)off >= cbData)
495 {
496 *pcbRead = 0;
497 rc = VINF_EOF;
498 }
499 else
500 {
501 rc = RTVfsFileReadAt(pVarStore->hVfsBacking, offStart, pSgBuf->paSegs[0].pvSeg, cbThisRead, NULL);
502 if (RT_SUCCESS(rc))
503 {
504 /* Return VINF_EOF if beyond end-of-file. */
505 if (cbThisRead < cbRead)
506 rc = VINF_EOF;
507 pThis->offFile = off + cbThisRead;
508 *pcbRead = cbThisRead;
509 }
510 else
511 *pcbRead = 0;
512 }
513 Log6(("rtFsEfiVarStore_Read: off=%#RX64 cbSeg=%#x -> %Rrc *pcbRead=%#x\n", off, pSgBuf->paSegs[0].cbSeg, rc, *pcbRead));
514 }
515
516 return rc;
517}
518
519
520/**
521 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
522 */
523static DECLCALLBACK(int) rtEfiVarStoreFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
524{
525 RT_NOREF(pvThis, off, pSgBuf, fBlocking, pcbWritten);
526 return VERR_WRITE_PROTECT;
527}
528
529
530/**
531 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
532 */
533static DECLCALLBACK(int) rtEfiVarStoreFile_Flush(void *pvThis)
534{
535 RT_NOREF(pvThis);
536 return VINF_SUCCESS;
537}
538
539
540/**
541 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
542 */
543static DECLCALLBACK(int) rtEfiVarStoreFile_Tell(void *pvThis, PRTFOFF poffActual)
544{
545 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
546 *poffActual = pThis->offFile;
547 return VINF_SUCCESS;
548}
549
550
551/**
552 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
553 */
554static DECLCALLBACK(int) rtEfiVarStoreFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
555{
556 RT_NOREF(pvThis, fMode, fMask);
557 return VERR_WRITE_PROTECT;
558}
559
560
561/**
562 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
563 */
564static DECLCALLBACK(int) rtEfiVarStoreFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
565 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
566{
567 RT_NOREF(pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
568 return VERR_WRITE_PROTECT;
569}
570
571
572/**
573 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
574 */
575static DECLCALLBACK(int) rtEfiVarStoreFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
576{
577 RT_NOREF(pvThis, uid, gid);
578 return VERR_WRITE_PROTECT;
579}
580
581
582/**
583 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
584 */
585static DECLCALLBACK(int) rtEfiVarStoreFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
586{
587 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
588 RTFOFF offNew;
589 switch (uMethod)
590 {
591 case RTFILE_SEEK_BEGIN:
592 offNew = offSeek;
593 break;
594 case RTFILE_SEEK_END:
595 offNew = RT_LE2H_U32(pThis->pVar->VarHdr.cbData) + offSeek;
596 break;
597 case RTFILE_SEEK_CURRENT:
598 offNew = (RTFOFF)pThis->offFile + offSeek;
599 break;
600 default:
601 return VERR_INVALID_PARAMETER;
602 }
603 if (offNew >= 0)
604 {
605 pThis->offFile = offNew;
606 *poffActual = offNew;
607 return VINF_SUCCESS;
608 }
609 return VERR_NEGATIVE_SEEK;
610}
611
612
613/**
614 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
615 */
616static DECLCALLBACK(int) rtEfiVarStoreFile_QuerySize(void *pvThis, uint64_t *pcbFile)
617{
618 PRTEFIVARFILE pThis = (PRTEFIVARFILE)pvThis;
619 *pcbFile = (uint64_t)RT_LE2H_U32(pThis->pVar->VarHdr.cbData);
620 return VINF_SUCCESS;
621}
622
623
624/**
625 * @interface_method_impl{RTVFSFILEOPS,pfnSetSize}
626 */
627static DECLCALLBACK(int) rtEfiVarStoreFile_SetSize(void *pvThis, uint64_t cbFile, uint32_t fFlags)
628{
629 RT_NOREF(pvThis, cbFile, fFlags);
630 return VERR_WRITE_PROTECT;
631}
632
633
634/**
635 * @interface_method_impl{RTVFSFILEOPS,pfnQueryMaxSize}
636 */
637static DECLCALLBACK(int) rtEfiVarStoreFile_QueryMaxSize(void *pvThis, uint64_t *pcbMax)
638{
639 RT_NOREF(pvThis);
640 *pcbMax = INT64_MAX; /** @todo */
641 return VINF_SUCCESS;
642}
643
644
645/**
646 * EXT file operations.
647 */
648static const RTVFSFILEOPS g_rtEfiVarStoreFileOps =
649{
650 { /* Stream */
651 { /* Obj */
652 RTVFSOBJOPS_VERSION,
653 RTVFSOBJTYPE_FILE,
654 "EFiVarStore File",
655 rtEfiVarStoreFile_Close,
656 rtEfiVarStoreFile_QueryInfo,
657 RTVFSOBJOPS_VERSION
658 },
659 RTVFSIOSTREAMOPS_VERSION,
660 RTVFSIOSTREAMOPS_FEAT_NO_SG,
661 rtEfiVarStoreFile_Read,
662 rtEfiVarStoreFile_Write,
663 rtEfiVarStoreFile_Flush,
664 NULL /*PollOne*/,
665 rtEfiVarStoreFile_Tell,
666 NULL /*pfnSkip*/,
667 NULL /*pfnZeroFill*/,
668 RTVFSIOSTREAMOPS_VERSION,
669 },
670 RTVFSFILEOPS_VERSION,
671 0,
672 { /* ObjSet */
673 RTVFSOBJSETOPS_VERSION,
674 RT_UOFFSETOF(RTVFSFILEOPS, ObjSet) - RT_UOFFSETOF(RTVFSFILEOPS, Stream.Obj),
675 rtEfiVarStoreFile_SetMode,
676 rtEfiVarStoreFile_SetTimes,
677 rtEfiVarStoreFile_SetOwner,
678 RTVFSOBJSETOPS_VERSION
679 },
680 rtEfiVarStoreFile_Seek,
681 rtEfiVarStoreFile_QuerySize,
682 rtEfiVarStoreFile_SetSize,
683 rtEfiVarStoreFile_QueryMaxSize,
684 RTVFSFILEOPS_VERSION
685};
686
687
688/**
689 * Creates a new VFS file from the given regular file inode.
690 *
691 * @returns IPRT status code.
692 * @param pThis The ext volume instance.
693 * @param fOpen Open flags passed.
694 * @param iInode The inode for the file.
695 * @param phVfsFile Where to store the VFS file handle on success.
696 * @param pErrInfo Where to record additional error information on error, optional.
697 */
698static int rtEfiVarStore_NewFile(PRTEFIVARSTORE pThis, uint64_t fOpen, PRTEFIVAR pVar,
699 PRTVFSOBJ phVfsObj)
700{
701 RTVFSFILE hVfsFile;
702 PRTEFIVARFILE pNewFile;
703 int rc = RTVfsNewFile(&g_rtEfiVarStoreFileOps, sizeof(*pNewFile), fOpen, pThis->hVfsSelf, NIL_RTVFSLOCK,
704 &hVfsFile, (void **)&pNewFile);
705 if (RT_SUCCESS(rc))
706 {
707 pNewFile->pVarStore = pThis;
708 pNewFile->pVar = pVar;
709 pNewFile->offFile = 0;
710
711 *phVfsObj = RTVfsObjFromFile(hVfsFile);
712 RTVfsFileRelease(hVfsFile);
713 AssertStmt(*phVfsObj != NIL_RTVFSOBJ, rc = VERR_INTERNAL_ERROR_3);
714 }
715
716 return rc;
717}
718
719
720
721/*
722 *
723 * Directory instance methods
724 * Directory instance methods
725 * Directory instance methods
726 *
727 */
728
729/**
730 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
731 */
732static DECLCALLBACK(int) rtEfiVarStoreDir_Close(void *pvThis)
733{
734 PRTEFIVARSTOREDIR pThis = (PRTEFIVARSTOREDIR)pvThis;
735 LogFlowFunc(("pThis=%p\n", pThis));
736 pThis->pVarStore = NULL;
737 return VINF_SUCCESS;
738}
739
740
741/**
742 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
743 */
744static DECLCALLBACK(int) rtEfiVarStoreDir_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
745{
746 PRTEFIVARSTOREDIR pThis = (PRTEFIVARSTOREDIR)pvThis;
747 LogFlowFunc(("\n"));
748 return rtEfiVarStore_QueryInfo(1, true /*fIsDir*/, &pThis->Time, pObjInfo, enmAddAttr);
749}
750
751
752/**
753 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
754 */
755static DECLCALLBACK(int) rtEfiVarStoreDir_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
756{
757 LogFlowFunc(("\n"));
758 RT_NOREF(pvThis, fMode, fMask);
759 return VERR_WRITE_PROTECT;
760}
761
762
763/**
764 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
765 */
766static DECLCALLBACK(int) rtEfiVarStoreDir_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
767 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
768{
769 LogFlowFunc(("\n"));
770 RT_NOREF(pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
771 return VERR_WRITE_PROTECT;
772}
773
774
775/**
776 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
777 */
778static DECLCALLBACK(int) rtEfiVarStoreDir_SetOwner(void *pvThis, RTUID uid, RTGID gid)
779{
780 LogFlowFunc(("\n"));
781 RT_NOREF(pvThis, uid, gid);
782 return VERR_WRITE_PROTECT;
783}
784
785
786/**
787 * @interface_method_impl{RTVFSDIROPS,pfnOpen}
788 */
789static DECLCALLBACK(int) rtEfiVarStoreDir_Open(void *pvThis, const char *pszEntry, uint64_t fOpen,
790 uint32_t fFlags, PRTVFSOBJ phVfsObj)
791{
792 LogFlowFunc(("pszEntry='%s' fOpen=%#RX64 fFlags=%#x\n", pszEntry, fOpen, fFlags));
793 PRTEFIVARSTOREDIR pThis = (PRTEFIVARSTOREDIR)pvThis;
794 PRTEFIVARSTORE pVarStore = pThis->pVarStore;
795 int rc = VINF_SUCCESS;
796
797 /*
798 * Special cases '.' and '.'
799 */
800 if (pszEntry[0] == '.')
801 {
802 RTEFIVARSTOREDIRTYPE enmDirTypeNew = RTEFIVARSTOREDIRTYPE_INVALID;
803 if (pszEntry[1] == '\0')
804 enmDirTypeNew = pThis->enmType;
805 else if (pszEntry[1] == '.' && pszEntry[2] == '\0')
806 {
807 if ( pThis->enmType == RTEFIVARSTOREDIRTYPE_BY_NAME
808 || pThis->enmType == RTEFIVARSTOREDIRTYPE_BY_GUID
809 || pThis->enmType == RTEFIVARSTOREDIRTYPE_ROOT)
810 enmDirTypeNew = RTEFIVARSTOREDIRTYPE_ROOT;
811 else if (pThis->enmType == RTEFIVARSTOREDIRTYPE_GUID)
812 enmDirTypeNew = RTEFIVARSTOREDIRTYPE_BY_GUID;
813 else
814 AssertMsgFailedReturn(("Invalid directory type %d\n", pThis->enmType), VERR_ACCESS_DENIED);
815 }
816
817 if (enmDirTypeNew != RTEFIVARSTOREDIRTYPE_INVALID)
818 {
819 if (fFlags & RTVFSOBJ_F_OPEN_DIRECTORY)
820 {
821 if ( (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN
822 || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN_CREATE)
823 rc = rtEfiVarStore_NewDirByType(pVarStore, enmDirTypeNew, NULL /*pGuid*/, phVfsObj);
824 else
825 rc = VERR_ACCESS_DENIED;
826 }
827 else
828 rc = VERR_IS_A_DIRECTORY;
829 return rc;
830 }
831 }
832
833 /*
834 * We cannot create or replace anything, just open stuff.
835 */
836 if ( (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN
837 || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN_CREATE)
838 { /* likely */ }
839 else
840 return VERR_WRITE_PROTECT;
841
842 switch (pThis->enmType)
843 {
844 case RTEFIVARSTOREDIRTYPE_ROOT:
845 {
846 if (!strcmp(pszEntry, "by-name"))
847 return rtEfiVarStore_NewDirByType(pVarStore, RTEFIVARSTOREDIRTYPE_BY_NAME, NULL /*pGuid*/, phVfsObj);
848 else if (!strcmp(pszEntry, "by-uuid"))
849 return rtEfiVarStore_NewDirByType(pVarStore, RTEFIVARSTOREDIRTYPE_BY_GUID, NULL /*pGuid*/, phVfsObj);
850 break;
851 }
852 case RTEFIVARSTOREDIRTYPE_GUID: /** @todo This looks through all variables, not only the ones with the GUID. */
853 case RTEFIVARSTOREDIRTYPE_BY_NAME:
854 {
855 /* Look for the name. */
856 for (uint32_t i = 0; i < pVarStore->cVars; i++)
857 if (!strcmp(pszEntry, pVarStore->paVars[i].pszName))
858 return rtEfiVarStore_NewFile(pVarStore, fOpen, &pVarStore->paVars[i], phVfsObj);
859
860 rc = VERR_FILE_NOT_FOUND;
861 break;
862 }
863 case RTEFIVARSTOREDIRTYPE_BY_GUID:
864 {
865 /* Look for the name. */
866 for (uint32_t i = 0; i < pVarStore->cGuids; i++)
867 {
868 PRTEFIGUID pGuid = &pVarStore->paGuids[i];
869 char szUuid[RTUUID_STR_LENGTH];
870 rc = RTUuidToStr(&pGuid->Uuid, szUuid, sizeof(szUuid));
871 AssertRC(rc);
872
873 if (!strcmp(pszEntry, szUuid))
874 return rtEfiVarStore_NewDirByType(pVarStore, RTEFIVARSTOREDIRTYPE_GUID, pGuid, phVfsObj);
875 }
876
877 rc = VERR_FILE_NOT_FOUND;
878 break;
879 }
880 case RTEFIVARSTOREDIRTYPE_INVALID:
881 default:
882 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
883 }
884
885 LogFlow(("rtEfiVarStoreDir_Open(%s): returns %Rrc\n", pszEntry, rc));
886 return rc;
887}
888
889
890/**
891 * @interface_method_impl{RTVFSDIROPS,pfnCreateDir}
892 */
893static DECLCALLBACK(int) rtEfiVarStoreDir_CreateDir(void *pvThis, const char *pszSubDir, RTFMODE fMode, PRTVFSDIR phVfsDir)
894{
895 RT_NOREF(pvThis, pszSubDir, fMode, phVfsDir);
896 LogFlowFunc(("\n"));
897 return VERR_WRITE_PROTECT;
898}
899
900
901/**
902 * @interface_method_impl{RTVFSDIROPS,pfnOpenSymlink}
903 */
904static DECLCALLBACK(int) rtEfiVarStoreDir_OpenSymlink(void *pvThis, const char *pszSymlink, PRTVFSSYMLINK phVfsSymlink)
905{
906 RT_NOREF(pvThis, pszSymlink, phVfsSymlink);
907 LogFlowFunc(("\n"));
908 return VERR_NOT_SUPPORTED;
909}
910
911
912/**
913 * @interface_method_impl{RTVFSDIROPS,pfnCreateSymlink}
914 */
915static DECLCALLBACK(int) rtEfiVarStoreDir_CreateSymlink(void *pvThis, const char *pszSymlink, const char *pszTarget,
916 RTSYMLINKTYPE enmType, PRTVFSSYMLINK phVfsSymlink)
917{
918 RT_NOREF(pvThis, pszSymlink, pszTarget, enmType, phVfsSymlink);
919 LogFlowFunc(("\n"));
920 return VERR_WRITE_PROTECT;
921}
922
923
924/**
925 * @interface_method_impl{RTVFSDIROPS,pfnUnlinkEntry}
926 */
927static DECLCALLBACK(int) rtEfiVarStoreDir_UnlinkEntry(void *pvThis, const char *pszEntry, RTFMODE fType)
928{
929 RT_NOREF(pvThis, pszEntry, fType);
930 LogFlowFunc(("\n"));
931 return VERR_WRITE_PROTECT;
932}
933
934
935/**
936 * @interface_method_impl{RTVFSDIROPS,pfnRenameEntry}
937 */
938static DECLCALLBACK(int) rtEfiVarStoreDir_RenameEntry(void *pvThis, const char *pszEntry, RTFMODE fType, const char *pszNewName)
939{
940 RT_NOREF(pvThis, pszEntry, fType, pszNewName);
941 LogFlowFunc(("\n"));
942 return VERR_WRITE_PROTECT;
943}
944
945
946/**
947 * @interface_method_impl{RTVFSDIROPS,pfnRewindDir}
948 */
949static DECLCALLBACK(int) rtEfiVarStoreDir_RewindDir(void *pvThis)
950{
951 PRTEFIVARSTOREDIR pThis = (PRTEFIVARSTOREDIR)pvThis;
952 LogFlowFunc(("\n"));
953
954 pThis->idxNext = 0;
955 return VINF_SUCCESS;
956}
957
958
959/**
960 * @interface_method_impl{RTVFSDIROPS,pfnReadDir}
961 */
962static DECLCALLBACK(int) rtEfiVarStoreDir_ReadDir(void *pvThis, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
963 RTFSOBJATTRADD enmAddAttr)
964{
965 PRTEFIVARSTOREDIR pThis = (PRTEFIVARSTOREDIR)pvThis;
966 PRTEFIVARSTORE pVarStore = pThis->pVarStore;
967 LogFlowFunc(("\n"));
968
969 if (pThis->fNoMoreFiles)
970 return VERR_NO_MORE_FILES;
971
972 int rc = VINF_SUCCESS;
973 char aszUuid[RTUUID_STR_LENGTH];
974 const char *pszName = NULL;
975 size_t cbName = 0;
976 uint64_t cbObject = 0;
977 bool fIsDir = false;
978 bool fNoMoreFiles = false;
979 RTTIMESPEC Time;
980 PCRTTIMESPEC pTimeSpec = &Time;
981 RTTimeNow(&Time);
982
983 switch (pThis->enmType)
984 {
985 case RTEFIVARSTOREDIRTYPE_ROOT:
986 {
987 if (pThis->idxNext == 0)
988 {
989 pszName = "by-name";
990 cbName = sizeof("by-name");
991 cbObject = 1;
992 fIsDir = true;
993 }
994 else if (pThis->idxNext == 1)
995 {
996 pszName = "by-uuid";
997 cbName = sizeof("by-uuid");
998 cbObject = 1;
999 fIsDir = true;
1000 fNoMoreFiles = true;
1001 }
1002 break;
1003 }
1004 case RTEFIVARSTOREDIRTYPE_BY_NAME:
1005 {
1006 PRTEFIVAR pVar = &pVarStore->paVars[pThis->idxNext];
1007 if (pThis->idxNext + 1 == pVarStore->cVars)
1008 fNoMoreFiles = true;
1009 pszName = pVar->pszName;
1010 cbName = strlen(pszName) + 1;
1011 cbObject = RT_LE2H_U32(pVar->VarHdr.cbData);
1012 pTimeSpec = &pVar->Time;
1013 break;
1014 }
1015 case RTEFIVARSTOREDIRTYPE_BY_GUID:
1016 {
1017 PRTEFIGUID pGuid = &pVarStore->paGuids[pThis->idxNext];
1018 if (pThis->idxNext + 1 == pVarStore->cGuids)
1019 fNoMoreFiles = true;
1020 pszName = &aszUuid[0];
1021 cbName = sizeof(aszUuid);
1022 cbObject = 1;
1023 rc = RTUuidToStr(&pGuid->Uuid, &aszUuid[0], cbName);
1024 AssertRC(rc);
1025 break;
1026 }
1027 case RTEFIVARSTOREDIRTYPE_GUID:
1028 {
1029 PRTEFIGUID pGuid = pThis->pGuid;
1030 uint32_t idVar = pGuid->paidxVars[pThis->idxNext];
1031 PRTEFIVAR pVar = &pVarStore->paVars[idVar];
1032 if (pThis->idxNext + 1 == pGuid->cVars)
1033 fNoMoreFiles = true;
1034 pszName = pVar->pszName;
1035 cbName = strlen(pszName) + 1;
1036 cbObject = RT_LE2H_U32(pVar->VarHdr.cbData);
1037 pTimeSpec = &pVar->Time;
1038 break;
1039 }
1040 case RTEFIVARSTOREDIRTYPE_INVALID:
1041 default:
1042 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
1043 }
1044
1045 if (cbName <= 255)
1046 {
1047 size_t const cbDirEntry = *pcbDirEntry;
1048
1049 *pcbDirEntry = RT_UOFFSETOF_DYN(RTDIRENTRYEX, szName[cbName + 2]);
1050 if (*pcbDirEntry <= cbDirEntry)
1051 {
1052 memcpy(&pDirEntry->szName[0], pszName, cbName);
1053 pDirEntry->szName[cbName] = '\0';
1054 pDirEntry->cbName = cbName;
1055 rc = rtEfiVarStore_QueryInfo(cbObject, fIsDir, &Time, &pDirEntry->Info, enmAddAttr);
1056 if (RT_SUCCESS(rc))
1057 {
1058 pThis->fNoMoreFiles = fNoMoreFiles;
1059 pThis->idxNext++;
1060 return VINF_SUCCESS;
1061 }
1062 }
1063 else
1064 rc = VERR_BUFFER_OVERFLOW;
1065 }
1066 else
1067 rc = VERR_FILENAME_TOO_LONG;
1068 return rc;
1069}
1070
1071
1072/**
1073 * EFI variable store directory operations.
1074 */
1075static const RTVFSDIROPS g_rtEfiVarStoreDirOps =
1076{
1077 { /* Obj */
1078 RTVFSOBJOPS_VERSION,
1079 RTVFSOBJTYPE_DIR,
1080 "EfiVarStore Dir",
1081 rtEfiVarStoreDir_Close,
1082 rtEfiVarStoreDir_QueryInfo,
1083 RTVFSOBJOPS_VERSION
1084 },
1085 RTVFSDIROPS_VERSION,
1086 0,
1087 { /* ObjSet */
1088 RTVFSOBJSETOPS_VERSION,
1089 RT_UOFFSETOF(RTVFSDIROPS, ObjSet) - RT_UOFFSETOF(RTVFSDIROPS, Obj),
1090 rtEfiVarStoreDir_SetMode,
1091 rtEfiVarStoreDir_SetTimes,
1092 rtEfiVarStoreDir_SetOwner,
1093 RTVFSOBJSETOPS_VERSION
1094 },
1095 rtEfiVarStoreDir_Open,
1096 NULL /* pfnFollowAbsoluteSymlink */,
1097 NULL /* pfnOpenFile */,
1098 NULL /* pfnOpenDir */,
1099 rtEfiVarStoreDir_CreateDir,
1100 rtEfiVarStoreDir_OpenSymlink,
1101 rtEfiVarStoreDir_CreateSymlink,
1102 NULL /* pfnQueryEntryInfo */,
1103 rtEfiVarStoreDir_UnlinkEntry,
1104 rtEfiVarStoreDir_RenameEntry,
1105 rtEfiVarStoreDir_RewindDir,
1106 rtEfiVarStoreDir_ReadDir,
1107 RTVFSDIROPS_VERSION,
1108};
1109
1110
1111static int rtEfiVarStore_NewDirByType(PRTEFIVARSTORE pThis, RTEFIVARSTOREDIRTYPE enmDirType, PRTEFIGUID pGuid, PRTVFSOBJ phVfsObj)
1112{
1113 RTVFSDIR hVfsDir;
1114 PRTEFIVARSTOREDIR pDir;
1115 int rc = RTVfsNewDir(&g_rtEfiVarStoreDirOps, sizeof(*pDir), 0 /*fFlags*/, pThis->hVfsSelf, NIL_RTVFSLOCK,
1116 &hVfsDir, (void **)&pDir);
1117 if (RT_SUCCESS(rc))
1118 {
1119 pDir->idxNext = 0;
1120 pDir->enmType = enmDirType;
1121 pDir->pVarStore = pThis;
1122 pDir->pGuid = pGuid;
1123 RTTimeNow(&pDir->Time);
1124
1125 *phVfsObj = RTVfsObjFromDir(hVfsDir);
1126 RTVfsDirRelease(hVfsDir);
1127 AssertStmt(*phVfsObj != NIL_RTVFSOBJ, rc = VERR_INTERNAL_ERROR_3);
1128 }
1129
1130 return rc;
1131}
1132
1133
1134/*
1135 *
1136 * Volume level code.
1137 * Volume level code.
1138 * Volume level code.
1139 *
1140 */
1141
1142/**
1143 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnClose}
1144 */
1145static DECLCALLBACK(int) rtEfiVarStore_Close(void *pvThis)
1146{
1147 PRTEFIVARSTORE pThis = (PRTEFIVARSTORE)pvThis;
1148
1149 /*
1150 * Backing file and handles.
1151 */
1152 RTVfsFileRelease(pThis->hVfsBacking);
1153 pThis->hVfsBacking = NIL_RTVFSFILE;
1154 pThis->hVfsSelf = NIL_RTVFS;
1155 if (pThis->paVars)
1156 {
1157 for (uint32_t i = 0; i < pThis->cVars; i++)
1158 RTStrFree(pThis->paVars[i].pszName);
1159
1160 RTMemFree(pThis->paVars);
1161 pThis->paVars = NULL;
1162 pThis->cVars = 0;
1163 pThis->cVarsMax = 0;
1164 }
1165
1166 if (pThis->paGuids)
1167 {
1168 for (uint32_t i = 0; i < pThis->cGuids; i++)
1169 {
1170 PRTEFIGUID pGuid = &pThis->paGuids[i];
1171
1172 AssertPtr(pGuid->paidxVars);
1173 RTMemFree(pGuid->paidxVars);
1174 pGuid->paidxVars = NULL;
1175 }
1176
1177 RTMemFree(pThis->paGuids);
1178 pThis->paGuids = NULL;
1179 }
1180
1181 return VINF_SUCCESS;
1182}
1183
1184
1185/**
1186 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnQueryInfo}
1187 */
1188static DECLCALLBACK(int) rtEfiVarStore_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1189{
1190 RT_NOREF(pvThis, pObjInfo, enmAddAttr);
1191 return VERR_WRONG_TYPE;
1192}
1193
1194
1195/**
1196 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnOpenRoot}
1197 */
1198static DECLCALLBACK(int) rtEfiVarStore_OpenRoot(void *pvThis, PRTVFSDIR phVfsDir)
1199{
1200 PRTEFIVARSTORE pThis = (PRTEFIVARSTORE)pvThis;
1201 PRTEFIVARSTOREDIR pRootDir;
1202 int rc = RTVfsNewDir(&g_rtEfiVarStoreDirOps, sizeof(*pRootDir), 0 /*fFlags*/, pThis->hVfsSelf, NIL_RTVFSLOCK,
1203 phVfsDir, (void **)&pRootDir);
1204 if (RT_SUCCESS(rc))
1205 {
1206 pRootDir->idxNext = 0;
1207 pRootDir->enmType = RTEFIVARSTOREDIRTYPE_ROOT;
1208 pRootDir->pVarStore = pThis;
1209 }
1210
1211 LogFlowFunc(("returns %Rrc\n", rc));
1212 return rc;
1213}
1214
1215
1216DECL_HIDDEN_CONST(const RTVFSOPS) g_rtEfiVarStoreOps =
1217{
1218 /* .Obj = */
1219 {
1220 /* .uVersion = */ RTVFSOBJOPS_VERSION,
1221 /* .enmType = */ RTVFSOBJTYPE_VFS,
1222 /* .pszName = */ "EfiVarStore",
1223 /* .pfnClose = */ rtEfiVarStore_Close,
1224 /* .pfnQueryInfo = */ rtEfiVarStore_QueryInfo,
1225 /* .uEndMarker = */ RTVFSOBJOPS_VERSION
1226 },
1227 /* .uVersion = */ RTVFSOPS_VERSION,
1228 /* .fFeatures = */ 0,
1229 /* .pfnOpenRoot = */ rtEfiVarStore_OpenRoot,
1230 /* .pfnQueryRangeState = */ NULL,
1231 /* .uEndMarker = */ RTVFSOPS_VERSION
1232};
1233
1234
1235/**
1236 * Validates the given firmware header.
1237 *
1238 * @returns true if the given header is considered valid, flse otherwise.
1239 * @param pThis The EFI variable store instance.
1240 * @param pFvHdr The firmware volume header to validate.
1241 * @param poffData The offset into the backing where the data area begins.
1242 * @param pErrInfo Where to return additional error info.
1243 */
1244static int rtEfiVarStoreFvHdr_Validate(PRTEFIVARSTORE pThis, PCEFI_FIRMWARE_VOLUME_HEADER pFvHdr, uint64_t *poffData,
1245 PRTERRINFO pErrInfo)
1246{
1247#ifdef LOG_ENABLED
1248 rtEfiVarStoreFvHdr_Log(pFvHdr);
1249#endif
1250
1251 EFI_GUID GuidNvData = EFI_VARSTORE_FILESYSTEM_GUID;
1252 if (memcmp(&pFvHdr->GuidFilesystem, &GuidNvData, sizeof(GuidNvData)))
1253 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Filesystem GUID doesn't indicate a variable store");
1254 if (RT_LE2H_U64(pFvHdr->cbFv) > pThis->cbBacking)
1255 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Firmware volume length exceeds size of backing storage (truncated file?)");
1256 /* Signature was already verfied by caller. */
1257 /** @todo Check attributes. */
1258 if (pFvHdr->bRsvd != 0)
1259 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Reserved field of header is not 0");
1260 if (pFvHdr->bRevision != EFI_FIRMWARE_VOLUME_HEADER_REVISION)
1261 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Unexpected revision of the firmware volume header");
1262 if (RT_LE2H_U16(pFvHdr->offExtHdr) != 0)
1263 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Firmware volume header contains unsupported extended headers");
1264
1265 /* Start calculating the checksum of the main header. */
1266 uint16_t u16Chksum = 0;
1267 const uint16_t *pu16 = (const uint16_t *)pFvHdr;
1268 while (pu16 < (const uint16_t *)pFvHdr + (sizeof(*pFvHdr) / sizeof(uint16_t)))
1269 u16Chksum += RT_LE2H_U16(*pu16++);
1270
1271 /* Read in the block map and verify it as well. */
1272 uint64_t cbFvVol = 0;
1273 uint16_t cbFvHdr = sizeof(*pFvHdr);
1274 uint64_t offBlockMap = sizeof(*pFvHdr);
1275 for (;;)
1276 {
1277 EFI_FW_BLOCK_MAP BlockMap;
1278 int rc = RTVfsFileReadAt(pThis->hVfsBacking, offBlockMap, &BlockMap, sizeof(BlockMap), NULL);
1279 if (RT_FAILURE(rc))
1280 return RTERRINFO_LOG_SET_F(pErrInfo, rc, "Reading block map entry from %#RX64 failed", offBlockMap);
1281
1282 cbFvHdr += sizeof(BlockMap);
1283 offBlockMap += sizeof(BlockMap);
1284
1285 /* A zero entry denotes the end. */
1286 if ( !RT_LE2H_U32(BlockMap.cBlocks)
1287 && !RT_LE2H_U32(BlockMap.cbBlock))
1288 break;
1289
1290 cbFvVol += RT_LE2H_U32(BlockMap.cBlocks) * RT_LE2H_U32(BlockMap.cbBlock);
1291
1292 pu16 = (const uint16_t *)&BlockMap;
1293 while (pu16 < (const uint16_t *)&BlockMap + (sizeof(BlockMap) / sizeof(uint16_t)))
1294 u16Chksum += RT_LE2H_U16(*pu16++);
1295 }
1296
1297 *poffData = offBlockMap;
1298
1299 if (u16Chksum)
1300 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Firmware volume header has incorrect checksum");
1301 if (RT_LE2H_U64(pFvHdr->cbFvHdr) != cbFvHdr)
1302 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Unexpected firmware volume header size");
1303
1304 return VINF_SUCCESS;
1305}
1306
1307
1308/**
1309 * Validates the given variable store header.
1310 *
1311 * @returns true if the given header is considered valid, false otherwise.
1312 * @param pThis The EFI variable store instance.
1313 * @param pHdr The variable store header to validate.
1314 * @param pfAuth Where to store whether the variable store uses authenticated variables or not.
1315 * @param pErrInfo Where to return additional error info.
1316 */
1317static int rtEfiVarStoreHdr_Validate(PRTEFIVARSTORE pThis, PCEFI_VARSTORE_HEADER pHdr, bool *pfAuth, PRTERRINFO pErrInfo)
1318{
1319#ifdef LOG_ENABLED
1320 rtEfiVarStoreHdr_Log(pHdr);
1321#endif
1322
1323 EFI_GUID GuidVarStoreAuth = EFI_VARSTORE_HEADER_GUID_AUTHENTICATED_VARIABLE;
1324 EFI_GUID GuidVarStore = EFI_VARSTORE_HEADER_GUID_VARIABLE;
1325 if (!memcmp(&pHdr->GuidVarStore, &GuidVarStoreAuth, sizeof(GuidVarStoreAuth)))
1326 *pfAuth = true;
1327 else if (!memcmp(&pHdr->GuidVarStore, &GuidVarStore, sizeof(GuidVarStore)))
1328 *pfAuth = false;
1329 else
1330 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable store GUID doesn't indicate a variable store");
1331 if (RT_LE2H_U32(pHdr->cbVarStore) >= pThis->cbBacking)
1332 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable store length exceeds size of backing storage (truncated file?)");
1333 if (pHdr->bFmt != EFI_VARSTORE_HEADER_FMT_FORMATTED)
1334 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable store is not formatted");
1335 if (pHdr->bState != EFI_VARSTORE_HEADER_STATE_HEALTHY)
1336 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable store is not healthy");
1337
1338 return VINF_SUCCESS;
1339}
1340
1341
1342/**
1343 * Validates the given authenticate variable header.
1344 *
1345 * @returns true if the given header is considered valid, false otherwise.
1346 * @param pThis The EFI variable store instance.
1347 * @param pVarHdr The variable header to validate.
1348 * @param offVar Offset of the authenticated variable header.
1349 * @param pErrInfo Where to return additional error info.
1350 */
1351static int rtEfiVarStoreAuthVar_Validate(PRTEFIVARSTORE pThis, PCEFI_AUTH_VAR_HEADER pVarHdr, uint64_t offVar, PRTERRINFO pErrInfo)
1352{
1353#ifdef LOG_ENABLED
1354 rtEfiVarStoreAuthVarHdr_Log(pVarHdr, offVar);
1355#endif
1356
1357 uint32_t cbName = RT_LE2H_U32(pVarHdr->cbName);
1358 uint32_t cbData = RT_LE2H_U32(pVarHdr->cbData);
1359 uint64_t cbVarMax = pThis->cbBacking - offVar - sizeof(*pVarHdr);
1360 if ( cbVarMax <= cbName
1361 || cbVarMax - cbName <= cbData)
1362 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable exceeds remaining space in store (cbName=%llu cbData=%llu cbVarMax=%llu)",
1363 cbName, cbData, cbVarMax);
1364
1365 return VINF_SUCCESS;
1366}
1367
1368
1369/**
1370 * Loads the authenticated variable at the given offset.
1371 *
1372 * @returns IPRT status code.
1373 * @retval VERR_EOF if the end of the store was reached.
1374 * @param pThis The EFI variable store instance.
1375 * @param offVar Offset of the variable to load.
1376 * @param poffVarEnd Where to store the offset pointing to the end of the variable.
1377 * @param fIgnoreDelVars Flag whether to ignore deleted variables.
1378 * @param pErrInfo Where to return additional error info.
1379 */
1380static int rtEfiVarStoreLoadAuthVar(PRTEFIVARSTORE pThis, uint64_t offVar, uint64_t *poffVarEnd,
1381 bool fIgnoreDelVars, PRTERRINFO pErrInfo)
1382{
1383 EFI_AUTH_VAR_HEADER VarHdr;
1384 int rc = RTVfsFileReadAt(pThis->hVfsBacking, offVar, &VarHdr, sizeof(VarHdr), NULL);
1385 if (RT_FAILURE(rc))
1386 return rc;
1387
1388 rc = rtEfiVarStoreAuthVar_Validate(pThis, &VarHdr, offVar, pErrInfo);
1389 if (RT_FAILURE(rc))
1390 return rc;
1391
1392 if (poffVarEnd)
1393 *poffVarEnd = offVar + sizeof(VarHdr) + RT_LE2H_U32(VarHdr.cbData) + RT_LE2H_U32(VarHdr.cbName);
1394
1395 /* Only add complete variables or deleted variables when requested. */
1396 if ( ( fIgnoreDelVars
1397 && VarHdr.bState != EFI_AUTH_VAR_HEADER_STATE_ADDED)
1398 || VarHdr.bState == EFI_AUTH_VAR_HEADER_STATE_HDR_VALID_ONLY)
1399 return VINF_SUCCESS;
1400
1401 pThis->cbVarData += sizeof(VarHdr) + RT_LE2H_U32(VarHdr.cbData) + RT_LE2H_U32(VarHdr.cbName);
1402
1403 RTUTF16 awchName[128]; RT_ZERO(awchName);
1404 if (RT_LE2H_U32(VarHdr.cbName) > sizeof(awchName) - sizeof(RTUTF16))
1405 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "Variable name is too long (%llu vs. %llu)\n",
1406 RT_LE2H_U32(VarHdr.cbName), RT_LE2H_U32(VarHdr.cbName), sizeof(awchName));
1407
1408 rc = RTVfsFileReadAt(pThis->hVfsBacking, offVar + sizeof(VarHdr), &awchName[0], RT_LE2H_U32(VarHdr.cbName), NULL);
1409 if (RT_FAILURE(rc))
1410 return rc;
1411
1412 Log2(("Variable name '%ls'\n", &awchName[0]));
1413 if (pThis->cVars == pThis->cVarsMax)
1414 {
1415 /* Grow the variable array. */
1416 uint32_t cVarsMaxNew = pThis->cVarsMax + 10;
1417 PRTEFIVAR paVarsNew = (PRTEFIVAR)RTMemRealloc(pThis->paVars, cVarsMaxNew * sizeof(RTEFIVAR));
1418 if (!paVarsNew)
1419 return VERR_NO_MEMORY;
1420
1421 pThis->paVars = paVarsNew;
1422 pThis->cVarsMax = cVarsMaxNew;
1423 }
1424
1425 PRTEFIVAR pVar = &pThis->paVars[pThis->cVars++];
1426 pVar->pVarStore = pThis;
1427 pVar->offVarHdr = offVar;
1428 pVar->offVarData = offVar + sizeof(VarHdr) + RT_LE2H_U32(VarHdr.cbName);
1429 memcpy(&pVar->VarHdr, &VarHdr, sizeof(VarHdr));
1430 if (VarHdr.Timestamp.u8Month)
1431 RTEfiTimeToTimeSpec(&pVar->Time, &VarHdr.Timestamp);
1432 else
1433 RTTimeNow(&pVar->Time);
1434
1435 RTEfiGuidToUuid(&pVar->Uuid, &VarHdr.GuidVendor);
1436
1437 rc = RTUtf16ToUtf8(&awchName[0], &pVar->pszName);
1438 if (RT_FAILURE(rc))
1439 pThis->cVars--;
1440
1441 rc = rtEfiVarStore_AddVarByGuid(pThis, &pVar->Uuid, pThis->cVars - 1);
1442
1443 return rc;
1444}
1445
1446
1447/**
1448 * Looks for the next variable starting at the given offset.
1449 *
1450 * @returns IPRT status code.
1451 * @retval VERR_EOF if the end of the store was reached.
1452 * @param pThis The EFI variable store instance.
1453 * @param offStart Where in the image to start looking.
1454 * @param poffVar Where to store the start of the next variable if found.
1455 */
1456static int rtEfiVarStoreFindVar(PRTEFIVARSTORE pThis, uint64_t offStart, uint64_t *poffVar)
1457{
1458 /* Try to find the ID indicating a variable start by loading data in chunks. */
1459 uint64_t offEnd = pThis->offStoreData + pThis->cbVarStore;
1460 while (offStart < offEnd)
1461 {
1462 uint16_t au16Tmp[_1K / sizeof(uint16_t)];
1463 size_t cbThisRead = RT_MIN(sizeof(au16Tmp), offEnd - offStart);
1464 int rc = RTVfsFileReadAt(pThis->hVfsBacking, offStart, &au16Tmp[0], sizeof(au16Tmp), NULL);
1465 if (RT_FAILURE(rc))
1466 return rc;
1467
1468 for (uint32_t i = 0; i < RT_ELEMENTS(au16Tmp); i++)
1469 if (RT_LE2H_U16(au16Tmp[i]) == EFI_AUTH_VAR_HEADER_START)
1470 {
1471 *poffVar = offStart + i * sizeof(uint16_t);
1472 return VINF_SUCCESS;
1473 }
1474
1475 offStart += cbThisRead;
1476 }
1477
1478 return VERR_EOF;
1479}
1480
1481
1482/**
1483 * Loads and parses the superblock of the filesystem.
1484 *
1485 * @returns IPRT status code.
1486 * @param pThis The EFI variable store instance.
1487 * @param pErrInfo Where to return additional error info.
1488 */
1489static int rtEfiVarStoreLoad(PRTEFIVARSTORE pThis, PRTERRINFO pErrInfo)
1490{
1491 int rc = VINF_SUCCESS;
1492 EFI_FIRMWARE_VOLUME_HEADER FvHdr;
1493 rc = RTVfsFileReadAt(pThis->hVfsBacking, 0, &FvHdr, sizeof(FvHdr), NULL);
1494 if (RT_FAILURE(rc))
1495 return RTERRINFO_LOG_SET(pErrInfo, rc, "Error reading firmware volume header");
1496
1497 /* Validate the signature. */
1498 if (RT_LE2H_U32(FvHdr.u32Signature) != EFI_FIRMWARE_VOLUME_HEADER_SIGNATURE)
1499 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNKNOWN_FORMAT, "Not a EFI variable store - Signature mismatch: %RX32", RT_LE2H_U16(FvHdr.u32Signature));
1500
1501 uint64_t offData = 0;
1502 rc = rtEfiVarStoreFvHdr_Validate(pThis, &FvHdr, &offData, pErrInfo);
1503 if (RT_FAILURE(rc))
1504 return rc;
1505
1506 EFI_VARSTORE_HEADER StoreHdr;
1507 rc = RTVfsFileReadAt(pThis->hVfsBacking, offData, &StoreHdr, sizeof(StoreHdr), NULL);
1508 if (RT_FAILURE(rc))
1509 return RTERRINFO_LOG_SET(pErrInfo, rc, "Error reading variable store header");
1510
1511 rc = rtEfiVarStoreHdr_Validate(pThis, &StoreHdr, &pThis->fAuth, pErrInfo);
1512 if (RT_FAILURE(rc))
1513 return rc;
1514
1515 pThis->offStoreData = offData + sizeof(StoreHdr);
1516 pThis->cbVarStore = RT_LE2H_U32(StoreHdr.cbVarStore) - sizeof(StoreHdr);
1517
1518 /* Go over variables and set up the pointers. */
1519 offData = pThis->offStoreData;
1520 for (;;)
1521 {
1522 uint64_t offVar = 0;
1523
1524 rc = rtEfiVarStoreFindVar(pThis, offData, &offVar);
1525 if (RT_FAILURE(rc))
1526 break;
1527
1528 rc = rtEfiVarStoreLoadAuthVar(pThis, offVar, &offData, true /* fIgnoreDelVars*/, pErrInfo);
1529 if (RT_FAILURE(rc))
1530 break;
1531
1532 /* Align to 16bit boundary. */
1533 offData = RT_ALIGN_64(offData, 2);
1534 } while (RT_SUCCESS(rc));
1535
1536 if (rc == VERR_EOF) /* Reached end of variable store. */
1537 rc = VINF_SUCCESS;
1538
1539 return rc;
1540}
1541
1542
1543RTDECL(int) RTEfiVarStoreOpenAsVfs(RTVFSFILE hVfsFileIn, uint32_t fMntFlags, uint32_t fVarStoreFlags, PRTVFS phVfs, PRTERRINFO pErrInfo)
1544{
1545 AssertPtrReturn(phVfs, VERR_INVALID_POINTER);
1546 AssertReturn(!(fMntFlags & ~RTVFSMNT_F_VALID_MASK), VERR_INVALID_FLAGS);
1547 AssertReturn(!fVarStoreFlags, VERR_INVALID_FLAGS);
1548
1549 uint32_t cRefs = RTVfsFileRetain(hVfsFileIn);
1550 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
1551
1552 /*
1553 * Create a VFS instance and initialize the data so rtFsExtVol_Close works.
1554 */
1555 RTVFS hVfs;
1556 PRTEFIVARSTORE pThis;
1557 int rc = RTVfsNew(&g_rtEfiVarStoreOps, sizeof(*pThis), NIL_RTVFS, RTVFSLOCK_CREATE_RW, &hVfs, (void **)&pThis);
1558 if (RT_SUCCESS(rc))
1559 {
1560 pThis->hVfsBacking = hVfsFileIn;
1561 pThis->hVfsSelf = hVfs;
1562 pThis->fMntFlags = fMntFlags;
1563 pThis->fVarStoreFlags = fVarStoreFlags;
1564
1565 rc = RTVfsFileQuerySize(pThis->hVfsBacking, &pThis->cbBacking);
1566 if (RT_SUCCESS(rc))
1567 {
1568 rc = rtEfiVarStoreLoad(pThis, pErrInfo);
1569 if (RT_SUCCESS(rc))
1570 {
1571 *phVfs = hVfs;
1572 return VINF_SUCCESS;
1573 }
1574 }
1575
1576 RTVfsRelease(hVfs);
1577 *phVfs = NIL_RTVFS;
1578 }
1579 else
1580 RTVfsFileRelease(hVfsFileIn);
1581
1582 return rc;
1583}
1584
1585
1586RTDECL(int) RTEfiVarStoreCreate(RTVFSFILE hVfsFile, uint64_t offStore, uint64_t cbStore, uint32_t fFlags, uint32_t cbBlock,
1587 PRTERRINFO pErrInfo)
1588{
1589 RT_NOREF(pErrInfo);
1590
1591 /*
1592 * Validate input.
1593 */
1594 if (!cbBlock)
1595 cbBlock = 4096;
1596 else
1597 AssertMsgReturn(cbBlock <= 8192 && RT_IS_POWER_OF_TWO(cbBlock),
1598 ("cbBlock=%#x\n", cbBlock), VERR_INVALID_PARAMETER);
1599 AssertReturn(!(fFlags & ~RTEFIVARSTORE_CREATE_F_VALID_MASK), VERR_INVALID_FLAGS);
1600
1601 if (!cbStore)
1602 {
1603 uint64_t cbFile;
1604 int rc = RTVfsFileQuerySize(hVfsFile, &cbFile);
1605 AssertRCReturn(rc, rc);
1606 AssertMsgReturn(cbFile > offStore, ("cbFile=%#RX64 offStore=%#RX64\n", cbFile, offStore), VERR_INVALID_PARAMETER);
1607 cbStore = cbFile - offStore;
1608 }
1609 uint32_t const cBlocks = (uint32_t)(cbStore / cbBlock);
1610
1611 EFI_FIRMWARE_VOLUME_HEADER FvHdr; RT_ZERO(FvHdr);
1612 EFI_FW_BLOCK_MAP BlockMap; RT_ZERO(BlockMap);
1613 EFI_VARSTORE_HEADER VarStoreHdr; RT_ZERO(VarStoreHdr);
1614
1615 /* Firmware volume header. */
1616 FvHdr.GuidFilesystem = EFI_VARSTORE_FILESYSTEM_GUID;
1617 FvHdr.cbFv = RT_H2LE_U64(cbStore);
1618 FvHdr.u32Signature = RT_H2LE_U32(EFI_FIRMWARE_VOLUME_HEADER_SIGNATURE);
1619 FvHdr.fAttr = RT_H2LE_U32(0x4feff); /** @todo */
1620 FvHdr.cbFvHdr = RT_H2LE_U16(sizeof(FvHdr) + sizeof(BlockMap));
1621 FvHdr.bRevision = EFI_FIRMWARE_VOLUME_HEADER_REVISION;
1622
1623 /* Start calculating the checksum of the main header. */
1624 uint16_t u16Chksum = 0;
1625 const uint16_t *pu16 = (const uint16_t *)&FvHdr;
1626 while (pu16 < (const uint16_t *)&FvHdr + (sizeof(FvHdr) / sizeof(uint16_t)))
1627 u16Chksum += RT_LE2H_U16(*pu16++);
1628
1629 /* Block map. */
1630 BlockMap.cbBlock = RT_H2LE_U32(cbBlock);
1631 BlockMap.cBlocks = RT_H2LE_U32(cBlocks);
1632
1633 pu16 = (const uint16_t *)&BlockMap;
1634 while (pu16 < (const uint16_t *)&BlockMap + (sizeof(BlockMap) / sizeof(uint16_t)))
1635 u16Chksum += RT_LE2H_U16(*pu16++);
1636
1637 FvHdr.u16Chksum = RT_H2LE_U16(u16Chksum);
1638
1639 /* Variable store header. */
1640 VarStoreHdr.GuidVarStore = EFI_VARSTORE_HEADER_GUID_AUTHENTICATED_VARIABLE;
1641 VarStoreHdr.cbVarStore = cbStore - sizeof(FvHdr) - sizeof(BlockMap);
1642 VarStoreHdr.bFmt = EFI_VARSTORE_HEADER_FMT_FORMATTED;
1643 VarStoreHdr.bState = EFI_VARSTORE_HEADER_STATE_HEALTHY;
1644
1645 /* Write everything. */
1646 int rc = RTVfsFileWriteAt(hVfsFile, offStore, &FvHdr, sizeof(FvHdr), NULL);
1647 if (RT_SUCCESS(rc))
1648 rc = RTVfsFileWriteAt(hVfsFile, offStore + sizeof(FvHdr), &BlockMap, sizeof(BlockMap), NULL);
1649 if (RT_SUCCESS(rc))
1650 rc = RTVfsFileWriteAt(hVfsFile, offStore + sizeof(FvHdr) + sizeof(BlockMap), &VarStoreHdr, sizeof(VarStoreHdr), NULL);
1651 if (RT_SUCCESS(rc))
1652 {
1653 /* Fill the remainder with 0xff as it would be the case for a real NAND flash device. */
1654 uint8_t abFF[512];
1655 memset(&abFF[0], 0xff, sizeof(abFF));
1656
1657 uint64_t offStart = offStore + sizeof(FvHdr) + sizeof(BlockMap) + sizeof(VarStoreHdr);
1658 uint64_t offEnd = offStore + cbStore;
1659 while ( offStart < offEnd
1660 && RT_SUCCESS(rc))
1661 {
1662 size_t cbThisWrite = RT_MIN(sizeof(abFF), offEnd - offStart);
1663 rc = RTVfsFileWriteAt(hVfsFile, offStart, &abFF[0], cbThisWrite, NULL);
1664 }
1665 }
1666
1667 /** @todo FTW region. */
1668
1669 return rc;
1670}
1671
1672
1673/**
1674 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
1675 */
1676static DECLCALLBACK(int) rtVfsChainEfiVarStore_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
1677 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
1678{
1679 RT_NOREF(pProviderReg);
1680
1681 /*
1682 * Basic checks.
1683 */
1684 if (pElement->enmTypeIn != RTVFSOBJTYPE_FILE)
1685 return pElement->enmTypeIn == RTVFSOBJTYPE_INVALID ? VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT : VERR_VFS_CHAIN_TAKES_FILE;
1686 if ( pElement->enmType != RTVFSOBJTYPE_VFS
1687 && pElement->enmType != RTVFSOBJTYPE_DIR)
1688 return VERR_VFS_CHAIN_ONLY_DIR_OR_VFS;
1689 if (pElement->cArgs > 1)
1690 return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
1691
1692 /*
1693 * Parse the flag if present, save in pElement->uProvider.
1694 */
1695 bool fReadOnly = (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ;
1696 if (pElement->cArgs > 0)
1697 {
1698 const char *psz = pElement->paArgs[0].psz;
1699 if (*psz)
1700 {
1701 if (!strcmp(psz, "ro"))
1702 fReadOnly = true;
1703 else if (!strcmp(psz, "rw"))
1704 fReadOnly = false;
1705 else
1706 {
1707 *poffError = pElement->paArgs[0].offSpec;
1708 return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected 'ro' or 'rw' as argument");
1709 }
1710 }
1711 }
1712
1713 pElement->uProvider = fReadOnly ? RTVFSMNT_F_READ_ONLY : 0;
1714 return VINF_SUCCESS;
1715}
1716
1717
1718/**
1719 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
1720 */
1721static DECLCALLBACK(int) rtVfsChainEfiVarStore_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
1722 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
1723 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
1724{
1725 RT_NOREF(pProviderReg, pSpec, poffError);
1726
1727 int rc;
1728 RTVFSFILE hVfsFileIn = RTVfsObjToFile(hPrevVfsObj);
1729 if (hVfsFileIn != NIL_RTVFSFILE)
1730 {
1731 RTVFS hVfs;
1732 rc = RTEfiVarStoreOpenAsVfs(hVfsFileIn, (uint32_t)pElement->uProvider, (uint32_t)(pElement->uProvider >> 32), &hVfs, pErrInfo);
1733 RTVfsFileRelease(hVfsFileIn);
1734 if (RT_SUCCESS(rc))
1735 {
1736 *phVfsObj = RTVfsObjFromVfs(hVfs);
1737 RTVfsRelease(hVfs);
1738 if (*phVfsObj != NIL_RTVFSOBJ)
1739 return VINF_SUCCESS;
1740 rc = VERR_VFS_CHAIN_CAST_FAILED;
1741 }
1742 }
1743 else
1744 rc = VERR_VFS_CHAIN_CAST_FAILED;
1745 return rc;
1746}
1747
1748
1749/**
1750 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
1751 */
1752static DECLCALLBACK(bool) rtVfsChainEfiVarStore_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
1753 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
1754 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
1755{
1756 RT_NOREF(pProviderReg, pSpec, pReuseSpec);
1757 if ( pElement->paArgs[0].uProvider == pReuseElement->paArgs[0].uProvider
1758 || !pReuseElement->paArgs[0].uProvider)
1759 return true;
1760 return false;
1761}
1762
1763
1764/** VFS chain element 'efivarstore'. */
1765static RTVFSCHAINELEMENTREG g_rtVfsChainEfiVarStoreReg =
1766{
1767 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
1768 /* fReserved = */ 0,
1769 /* pszName = */ "efivarstore",
1770 /* ListEntry = */ { NULL, NULL },
1771 /* pszHelp = */ "Open a EFI variable store, requires a file object on the left side.\n"
1772 "First argument is an optional 'ro' (read-only) or 'rw' (read-write) flag.\n",
1773 /* pfnValidate = */ rtVfsChainEfiVarStore_Validate,
1774 /* pfnInstantiate = */ rtVfsChainEfiVarStore_Instantiate,
1775 /* pfnCanReuseElement = */ rtVfsChainEfiVarStore_CanReuseElement,
1776 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
1777};
1778
1779RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainEfiVarStoreReg, rtVfsChainEfiVarStoreReg);
1780
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette