VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/DevEFI.cpp@ 55909

Last change on this file since 55909 was 55023, checked in by vboxsync, 10 years ago

DevEFI: s/AssertPtrReturn/AssertReturn/

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 82.2 KB
Line 
1/* $Id: DevEFI.cpp 55023 2015-03-31 10:33:48Z vboxsync $ */
2/** @file
3 * DevEFI - EFI <-> VirtualBox Integration Framework.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DEV_EFI
22
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/vmm/pgm.h>
25#include <VBox/vmm/cpum.h>
26#include <VBox/vmm/mm.h>
27#include <VBox/log.h>
28#include <VBox/err.h>
29#include <VBox/param.h>
30#include <VBox/vmm/dbgf.h>
31#include <VBox/vmm/pdmnvram.h>
32
33#include <iprt/asm.h>
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/file.h>
37#include <iprt/mem.h>
38#include <iprt/string.h>
39#include <iprt/uuid.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#include <iprt/mp.h>
43#include <iprt/list.h>
44#if defined(DEBUG) && defined(IN_RING3)
45# include <iprt/stream.h>
46# define DEVEFI_WITH_VBOXDBG_SCRIPT
47#endif
48
49#include "DevEFI.h"
50#include "VBoxDD.h"
51#include "VBoxDD2.h"
52#include "../PC/DevFwCommon.h"
53
54/* EFI includes */
55#include <ProcessorBind.h>
56#include <Common/UefiBaseTypes.h>
57#include <Common/PiFirmwareVolume.h>
58#include <Common/PiFirmwareFile.h>
59
60
61/*******************************************************************************
62* Structures and Typedefs *
63*******************************************************************************/
64/**
65 * EFI NVRAM variable.
66 */
67typedef struct EFIVAR
68{
69 /** The list node for the variable. */
70 RTLISTNODE ListNode;
71 /** The unique sequence number of the variable.
72 * This is used to find pCurVar when restoring saved state and therefore only
73 * set when saving. */
74 uint32_t idUniqueSavedState;
75 /** The value attributess. */
76 uint32_t fAttributes;
77 /** The variable name length (not counting the terminator char). */
78 uint32_t cchName;
79 /** The size of the value. This cannot be zero. */
80 uint32_t cbValue;
81 /** The vendor UUID scoping the variable name. */
82 RTUUID uuid;
83 /** The variable name. */
84 char szName[EFI_VARIABLE_NAME_MAX];
85 /** The variable value bytes. */
86 uint8_t abValue[EFI_VARIABLE_VALUE_MAX];
87} EFIVAR;
88/** Pointer to an EFI NVRAM variable. */
89typedef EFIVAR *PEFIVAR;
90/** Pointer to a const EFI NVRAM variable. */
91typedef EFIVAR const *PCEFIVAR;
92/** Pointer to an EFI NVRAM variable pointer. */
93typedef PEFIVAR *PPEFIVAR;
94
95/**
96 * NVRAM state.
97 */
98typedef struct NVRAMDESC
99{
100 /** The current operation. */
101 EFIVAROP enmOp;
102 /** The current status. */
103 uint32_t u32Status;
104 /** The current */
105 uint32_t offOpBuffer;
106 /** The current number of variables. */
107 uint32_t cVariables;
108 /** The list of variables. */
109 RTLISTANCHOR VarList;
110
111 /** The unique variable sequence ID, for the saved state only.
112 * @todo It's part of this structure for hysterical raisins, consider remove it
113 * when changing the saved state format the next time. */
114 uint32_t idUniqueCurVar;
115 /** Variable buffered used both when adding and querying NVRAM variables.
116 * When querying a variable, a copy of it is stored in this buffer and read
117 * from it. When adding, updating or deleting a variable, this buffer is used
118 * to set up the parameters before taking action. */
119 EFIVAR VarOpBuf;
120 /** The current variable. This is only used by EFI_VARIABLE_OP_QUERY_NEXT,
121 * the attribute readers work against the copy in VarOpBuf. */
122 PEFIVAR pCurVar;
123} NVRAMDESC;
124
125
126/**
127 * The EFI device state structure.
128 */
129typedef struct DEVEFI
130{
131 /** Pointer back to the device instance. */
132 PPDMDEVINS pDevIns;
133
134 /** EFI message buffer. */
135 char szMsg[VBOX_EFI_DEBUG_BUFFER];
136 /** EFI message buffer index. */
137 uint32_t iMsg;
138
139 /** EFI panic message buffer. */
140 char szPanicMsg[2048];
141 /** EFI panic message buffer index. */
142 uint32_t iPanicMsg;
143
144 struct
145 {
146 /** The current/last image event. */
147 uint8_t uEvt;
148 /** Module path/name offset. */
149 uint8_t offName;
150 /** The offset of the last component in the module path/name. */
151 uint8_t offNameLastComponent;
152 /** Alignment padding. */
153 uint8_t abPadding[5];
154 /** First address associated with the event (image address). */
155 uint64_t uAddr0;
156 /** Second address associated with the event (old image address). */
157 uint64_t uAddr1;
158 /** The size associated with the event (0 if none). */
159 uint64_t cb0;
160 /** The module name. */
161 char szName[256];
162 } ImageEvt;
163
164 /** The system EFI ROM data. */
165 uint8_t *pu8EfiRom;
166 /** The size of the system EFI ROM. */
167 uint64_t cbEfiRom;
168 /** The name of the EFI ROM file. */
169 char *pszEfiRomFile;
170 /** Thunk page pointer. */
171 uint8_t *pu8EfiThunk;
172 /** First entry point of the EFI firmware. */
173 RTGCPHYS GCEntryPoint0;
174 /** Second Entry Point (PeiCore)*/
175 RTGCPHYS GCEntryPoint1;
176 /** EFI firmware physical load address. */
177 RTGCPHYS GCLoadAddress;
178 /** Current info selector. */
179 uint32_t iInfoSelector;
180 /** Current info position. */
181 int32_t offInfo;
182
183 /** Number of virtual CPUs. (Config) */
184 uint32_t cCpus;
185 /** RAM below 4GB (in bytes). (Config) */
186 uint32_t cbBelow4GB;
187 /** RAM above 4GB (in bytes). (Config) */
188 uint64_t cbAbove4GB;
189 /** The total amount of memory. */
190 uint64_t cbRam;
191 /** The size of the RAM hole below 4GB. */
192 uint64_t cbRamHole;
193
194 /** The size of the DMI tables. */
195 uint16_t cbDmiTables;
196 /** Number of the DMI tables. */
197 uint16_t cNumDmiTables;
198 /** The DMI tables. */
199 uint8_t au8DMIPage[0x1000];
200
201 /** I/O-APIC enabled? */
202 uint8_t u8IOAPIC;
203
204 /** Boot parameters passed to the firmware. */
205 char szBootArgs[256];
206
207 /** Host UUID (for DMI). */
208 RTUUID aUuid;
209
210 /** Device properties buffer. */
211 R3PTRTYPE(uint8_t *) pbDeviceProps;
212 /** Device properties buffer size. */
213 uint32_t cbDeviceProps;
214
215 /** Virtual machine front side bus frequency. */
216 uint64_t u64FsbFrequency;
217 /** Virtual machine time stamp counter frequency. */
218 uint64_t u64TscFrequency;
219 /** Virtual machine CPU frequency. */
220 uint64_t u64CpuFrequency;
221 /** GOP mode. */
222 uint32_t u32GopMode;
223 /** Uga mode horizontal resolution. */
224 uint32_t cxUgaResolution;
225 /** Uga mode vertical resolution. */
226 uint32_t cyUgaResolution;
227
228
229 /** NVRAM state variables. */
230 NVRAMDESC NVRAM;
231
232 /**
233 * NVRAM port - LUN\#0.
234 */
235 struct
236 {
237 /** The base interface we provide the NVRAM driver. */
238 PDMIBASE IBase;
239 /** The NVRAM driver base interface. */
240 PPDMIBASE pDrvBase;
241 /** The NVRAM interface provided by the driver. */
242 PPDMINVRAMCONNECTOR pNvramDrv;
243 } Lun0;
244} DEVEFI;
245typedef DEVEFI *PDEVEFI;
246
247
248/*******************************************************************************
249* Defined Constants And Macros *
250*******************************************************************************/
251/** The saved state version. */
252#define EFI_SSM_VERSION 2
253/** The saved state version from VBox 4.2. */
254#define EFI_SSM_VERSION_4_2 1
255
256/** Non-volatile EFI variable. */
257#define VBOX_EFI_VARIABLE_NON_VOLATILE UINT32_C(0x00000001)
258/** Non-volatile EFI variable. */
259#define VBOX_EFI_VARIABLE_READ_ONLY UINT32_C(0x00000008)
260
261
262/*******************************************************************************
263* Global Variables *
264*******************************************************************************/
265/** Saved state NVRAMDESC field descriptors. */
266static SSMFIELD const g_aEfiNvramDescField[] =
267{
268 SSMFIELD_ENTRY( NVRAMDESC, enmOp),
269 SSMFIELD_ENTRY( NVRAMDESC, u32Status),
270 SSMFIELD_ENTRY( NVRAMDESC, offOpBuffer),
271 SSMFIELD_ENTRY_IGNORE(NVRAMDESC, VarOpBuf),
272 SSMFIELD_ENTRY( NVRAMDESC, cVariables),
273 SSMFIELD_ENTRY_OLD( idUnquireLast, 4),
274 SSMFIELD_ENTRY_IGNORE(NVRAMDESC, VarList),
275 SSMFIELD_ENTRY( NVRAMDESC, idUniqueCurVar),
276 SSMFIELD_ENTRY_IGNORE(NVRAMDESC, pCurVar),
277 SSMFIELD_ENTRY_TERM()
278};
279
280/** Saved state EFIVAR field descriptors. */
281static SSMFIELD const g_aEfiVariableDescFields[] =
282{
283 SSMFIELD_ENTRY_IGNORE(EFIVAR, ListNode),
284 SSMFIELD_ENTRY( EFIVAR, idUniqueSavedState),
285 SSMFIELD_ENTRY( EFIVAR, uuid),
286 SSMFIELD_ENTRY( EFIVAR, szName),
287 SSMFIELD_ENTRY_OLD( cchName, 4),
288 SSMFIELD_ENTRY( EFIVAR, abValue),
289 SSMFIELD_ENTRY( EFIVAR, cbValue),
290 SSMFIELD_ENTRY( EFIVAR, fAttributes),
291 SSMFIELD_ENTRY_TERM()
292};
293
294
295
296
297/**
298 * Flushes the variable list.
299 *
300 * @param pThis The EFI state.
301 */
302static void nvramFlushDeviceVariableList(PDEVEFI pThis)
303{
304 while (!RTListIsEmpty(&pThis->NVRAM.VarList))
305 {
306 PEFIVAR pEfiVar = RTListNodeGetNext(&pThis->NVRAM.VarList, EFIVAR, ListNode);
307 RTListNodeRemove(&pEfiVar->ListNode);
308 RTMemFree(pEfiVar);
309 }
310
311 pThis->NVRAM.pCurVar = NULL;
312}
313
314/**
315 * This function looks up variable in NVRAM list.
316 */
317static int nvramLookupVariableByUuidAndName(PDEVEFI pThis, char *pszVariableName, PCRTUUID pUuid, PPEFIVAR ppEfiVar)
318{
319 LogFlowFunc(("%RTuuid::'%s'\n", pUuid, pszVariableName));
320 size_t const cchVariableName = strlen(pszVariableName);
321 int rc = VERR_NOT_FOUND;
322
323 /*
324 * Start by checking the last variable queried.
325 */
326 if ( pThis->NVRAM.pCurVar
327 && pThis->NVRAM.pCurVar->cchName == cchVariableName
328 && memcmp(pThis->NVRAM.pCurVar->szName, pszVariableName, cchVariableName + 1) == 0
329 && RTUuidCompare(&pThis->NVRAM.pCurVar->uuid, pUuid) == 0
330 )
331 {
332 *ppEfiVar = pThis->NVRAM.pCurVar;
333 rc = VINF_SUCCESS;
334 }
335 else
336 {
337 /*
338 * Linear list search.
339 */
340 PEFIVAR pEfiVar;
341 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
342 {
343 Assert(strlen(pEfiVar->szName) == pEfiVar->cchName);
344 if ( pEfiVar->cchName == cchVariableName
345 && memcmp(pEfiVar->szName, pszVariableName, cchVariableName + 1) == 0
346 && RTUuidCompare(&pEfiVar->uuid, pUuid) == 0)
347 {
348 *ppEfiVar = pEfiVar;
349 rc = VINF_SUCCESS;
350 break;
351 }
352 }
353 }
354
355 LogFlowFunc(("rc=%Rrc pEfiVar=%p\n", rc, *ppEfiVar));
356 return rc;
357}
358
359
360/**
361 * Inserts the EFI variable into the list.
362 *
363 * This enforces the desired list ordering and/or insertion policy.
364 *
365 * @param pThis The EFI state.
366 * @param pEfiVar The variable to insert.
367 */
368static void nvramInsertVariable(PDEVEFI pThis, PEFIVAR pEfiVar)
369{
370#if 1
371 /*
372 * Sorted by UUID and name.
373 */
374 PEFIVAR pCurVar;
375 RTListForEach(&pThis->NVRAM.VarList, pCurVar, EFIVAR, ListNode)
376 {
377 int iDiff = RTUuidCompare(&pEfiVar->uuid, &pCurVar->uuid);
378 if (!iDiff)
379 iDiff = strcmp(pEfiVar->szName, pCurVar->szName);
380 if (iDiff < 0)
381 {
382 RTListNodeInsertBefore(&pCurVar->ListNode, &pEfiVar->ListNode);
383 return;
384 }
385 }
386#endif
387
388 /*
389 * Add it at the end.
390 */
391 RTListAppend(&pThis->NVRAM.VarList, &pEfiVar->ListNode);
392}
393
394
395/**
396 * Creates an device internal list of variables.
397 *
398 * @returns VBox status code.
399 * @param pThis The EFI state.
400 */
401static int nvramLoad(PDEVEFI pThis)
402{
403 int rc;
404 for (uint32_t iVar = 0; iVar < EFI_VARIABLE_MAX; iVar++)
405 {
406 PEFIVAR pEfiVar = (PEFIVAR)RTMemAllocZ(sizeof(EFIVAR));
407 AssertReturn(pEfiVar, VERR_NO_MEMORY);
408
409 pEfiVar->cchName = sizeof(pEfiVar->szName);
410 pEfiVar->cbValue = sizeof(pEfiVar->abValue);
411 rc = pThis->Lun0.pNvramDrv->pfnVarQueryByIndex(pThis->Lun0.pNvramDrv, iVar,
412 &pEfiVar->uuid, &pEfiVar->szName[0], &pEfiVar->cchName,
413 &pEfiVar->fAttributes, &pEfiVar->abValue[0], &pEfiVar->cbValue);
414 if (RT_SUCCESS(rc))
415 {
416 /* Some validations. */
417 rc = RTStrValidateEncoding(pEfiVar->szName);
418 size_t cchName = RTStrNLen(pEfiVar->szName, sizeof(pEfiVar->szName));
419 if (cchName != pEfiVar->cchName)
420 rc = VERR_INVALID_PARAMETER;
421 if (pEfiVar->cbValue == 0)
422 rc = VERR_NO_DATA;
423 if (RT_FAILURE(rc))
424 LogRel(("EFI/nvramLoad: Bad variable #%u: cbValue=%#x cchName=%#x (strlen=%#x) szName=%.*Rhxs\n",
425 pEfiVar->cbValue, pEfiVar->cchName, cchName, pEfiVar->cchName + 1, pEfiVar->szName));
426 }
427 if (RT_FAILURE(rc))
428 {
429 RTMemFree(pEfiVar);
430 if (rc == VERR_NOT_FOUND)
431 rc = VINF_SUCCESS;
432 AssertRC(rc);
433 return rc;
434 }
435
436 /* Append it. */
437 nvramInsertVariable(pThis, pEfiVar);
438 pThis->NVRAM.cVariables++;
439 }
440
441 AssertLogRelMsgFailed(("EFI: Too many variables.\n"));
442 return VERR_TOO_MUCH_DATA;
443}
444
445
446/**
447 * Let the NVRAM driver store the internal NVRAM variable list.
448 *
449 * @returns VBox status code.
450 * @param pThis The EFI state.
451 */
452static int nvramStore(PDEVEFI pThis)
453{
454 /*
455 * Count the non-volatile variables and issue the begin call.
456 */
457 PEFIVAR pEfiVar;
458 uint32_t cNonVolatile = 0;
459 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
460 if (pEfiVar->fAttributes & VBOX_EFI_VARIABLE_NON_VOLATILE)
461 cNonVolatile++;
462 int rc = pThis->Lun0.pNvramDrv->pfnVarStoreSeqBegin(pThis->Lun0.pNvramDrv, cNonVolatile);
463 if (RT_SUCCESS(rc))
464 {
465 /*
466 * Store each non-volatile variable.
467 */
468 uint32_t idxVar = 0;
469 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
470 {
471 /* Skip volatile variables. */
472 if (!(pEfiVar->fAttributes & VBOX_EFI_VARIABLE_NON_VOLATILE))
473 continue;
474
475 int rc2 = pThis->Lun0.pNvramDrv->pfnVarStoreSeqPut(pThis->Lun0.pNvramDrv, idxVar,
476 &pEfiVar->uuid, pEfiVar->szName, pEfiVar->cchName,
477 pEfiVar->fAttributes, pEfiVar->abValue, pEfiVar->cbValue);
478 if (RT_FAILURE(rc2) && RT_SUCCESS_NP(rc))
479 {
480 LogRel(("EFI: pfnVarStoreVarByIndex failed: %Rrc\n", rc));
481 rc = rc2;
482 }
483 idxVar++;
484 }
485 Assert(idxVar == cNonVolatile);
486
487 /*
488 * Done.
489 */
490 rc = pThis->Lun0.pNvramDrv->pfnVarStoreSeqEnd(pThis->Lun0.pNvramDrv, rc);
491 }
492 else
493 LogRel(("EFI: pfnVarStoreBegin failed: %Rrc\n", rc));
494 return rc;
495}
496
497/**
498 * EFI_VARIABLE_OP_QUERY and EFI_VARIABLE_OP_QUERY_NEXT worker that copies the
499 * variable into the VarOpBuf, set pCurVar and u32Status.
500 *
501 * @param pThis The EFI state.
502 * @param pEfiVar The resulting variable. NULL if not found / end.
503 * @param fEnumQuery Set if enumeration query, clear if specific.
504 */
505static void nvramWriteVariableOpQueryCopyResult(PDEVEFI pThis, PEFIVAR pEfiVar, bool fEnumQuery)
506{
507 RT_ZERO(pThis->NVRAM.VarOpBuf.abValue);
508 if (pEfiVar)
509 {
510 RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
511 pThis->NVRAM.VarOpBuf.uuid = pEfiVar->uuid;
512 pThis->NVRAM.VarOpBuf.cchName = pEfiVar->cchName;
513 memcpy(pThis->NVRAM.VarOpBuf.szName, pEfiVar->szName, pEfiVar->cchName); /* no need for + 1. */
514 pThis->NVRAM.VarOpBuf.fAttributes = pEfiVar->fAttributes;
515 pThis->NVRAM.VarOpBuf.cbValue = pEfiVar->cbValue;
516 memcpy(pThis->NVRAM.VarOpBuf.abValue, pEfiVar->abValue, pEfiVar->cbValue);
517 pThis->NVRAM.pCurVar = pEfiVar;
518 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
519 LogFlow(("EFI: Variable query -> %RTuuid::'%s' (%d) abValue=%.*Rhxs\n", &pThis->NVRAM.VarOpBuf.uuid,
520 pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.cchName,
521 pThis->NVRAM.VarOpBuf.cbValue, pThis->NVRAM.VarOpBuf.abValue));
522 }
523 else
524 {
525 if (fEnumQuery)
526 LogFlow(("EFI: Variable query -> NOT_FOUND \n"));
527 else
528 LogFlow(("EFI: Variable query %RTuuid::'%s' -> NOT_FOUND \n",
529 &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
530 RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
531 pThis->NVRAM.VarOpBuf.fAttributes = 0;
532 pThis->NVRAM.VarOpBuf.cbValue = 0;
533 pThis->NVRAM.VarOpBuf.cchName = 0;
534 pThis->NVRAM.pCurVar = NULL;
535 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_NOT_FOUND;
536 }
537}
538
539/**
540 * Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_QUERY.
541 *
542 * @returns IOM strict status code.
543 * @param pThis The EFI state.
544 */
545static int nvramWriteVariableOpQuery(PDEVEFI pThis)
546{
547 Log(("EFI_VARIABLE_OP_QUERY: %RTuuid::'%s'\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
548
549 PEFIVAR pEfiVar;
550 int rc = nvramLookupVariableByUuidAndName(pThis,
551 pThis->NVRAM.VarOpBuf.szName,
552 &pThis->NVRAM.VarOpBuf.uuid,
553 &pEfiVar);
554 nvramWriteVariableOpQueryCopyResult(pThis, RT_SUCCESS(rc) ? pEfiVar : NULL, false /*fEnumQuery*/);
555 return VINF_SUCCESS;
556}
557
558/**
559 * Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_QUERY_NEXT.
560 *
561 * This simply walks the list.
562 *
563 * @returns IOM strict status code.
564 * @param pThis The EFI state.
565 */
566static int nvramWriteVariableOpQueryNext(PDEVEFI pThis)
567{
568 Log(("EFI_VARIABLE_OP_QUERY_NEXT: pCurVar=%p\n", pThis->NVRAM.pCurVar));
569 PEFIVAR pEfiVar = pThis->NVRAM.pCurVar;
570 if (pEfiVar)
571 pEfiVar = RTListGetNext(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode);
572 else
573 pEfiVar = RTListGetFirst(&pThis->NVRAM.VarList, EFIVAR, ListNode);
574 nvramWriteVariableOpQueryCopyResult(pThis, pEfiVar, true /* fEnumQuery */);
575 return VINF_SUCCESS;
576}
577
578/**
579 * Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_ADD.
580 *
581 * @returns IOM strict status code.
582 * @param pThis The EFI state.
583 */
584static int nvramWriteVariableOpAdd(PDEVEFI pThis)
585{
586 Log(("EFI_VARIABLE_OP_ADD: %RTuuid::'%s' fAttributes=%#x abValue=%.*Rhxs\n",
587 &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes,
588 pThis->NVRAM.VarOpBuf.cbValue, pThis->NVRAM.VarOpBuf.abValue));
589
590 /*
591 * Validate and adjust the input a little before we start.
592 */
593 int rc = RTStrValidateEncoding(pThis->NVRAM.VarOpBuf.szName);
594 if (RT_FAILURE(rc))
595 LogRel(("EFI: Badly encoded variable name: %.*Rhxs\n", pThis->NVRAM.VarOpBuf.cchName + 1, pThis->NVRAM.VarOpBuf.szName));
596 if (RT_FAILURE(rc))
597 {
598 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
599 return VINF_SUCCESS;
600 }
601 pThis->NVRAM.VarOpBuf.cchName = (uint32_t)RTStrNLen(pThis->NVRAM.VarOpBuf.szName, sizeof(pThis->NVRAM.VarOpBuf.szName));
602
603 /*
604 * Look it up and see what to do.
605 */
606 PEFIVAR pEfiVar;
607 rc = nvramLookupVariableByUuidAndName(pThis,
608 pThis->NVRAM.VarOpBuf.szName,
609 &pThis->NVRAM.VarOpBuf.uuid,
610 &pEfiVar);
611 if (RT_SUCCESS(rc))
612 {
613 LogFlowFunc(("Old abValue=%.*Rhxs\n", pEfiVar->cbValue, pEfiVar->abValue));
614#if 0 /** @todo Implement read-only EFI variables. */
615 if (pEfiVar->fAttributes & EFI_VARIABLE_XXXXXXX)
616 {
617 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_RO;
618 break;
619 }
620#endif
621
622 if (pThis->NVRAM.VarOpBuf.cbValue == 0)
623 {
624 /*
625 * Delete it.
626 */
627 LogRel(("EFI: Deleting variable %RTuuid::'%s'\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
628 RTListNodeRemove(&pEfiVar->ListNode);
629 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
630 pThis->NVRAM.cVariables--;
631
632 if (pThis->NVRAM.pCurVar == pEfiVar)
633 pThis->NVRAM.pCurVar = NULL;
634 RTMemFree(pEfiVar);
635 pEfiVar = NULL;
636 }
637 else
638 {
639 /*
640 * Update/replace it. (The name and UUID are unchanged, of course.)
641 */
642 LogRel(("EFI: Replacing variable %RTuuid::'%s' fAttrib=%#x cbValue=%#x\n", &pThis->NVRAM.VarOpBuf.uuid,
643 pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
644 pEfiVar->fAttributes = pThis->NVRAM.VarOpBuf.fAttributes;
645 pEfiVar->cbValue = pThis->NVRAM.VarOpBuf.cbValue;
646 memcpy(pEfiVar->abValue, pThis->NVRAM.VarOpBuf.abValue, pEfiVar->cbValue);
647 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
648 }
649 }
650 else if (pThis->NVRAM.VarOpBuf.cbValue == 0)
651 {
652 /* delete operation, but nothing to delete. */
653 LogFlow(("nvramWriteVariableOpAdd: Delete (not found)\n"));
654 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
655 }
656 else if (pThis->NVRAM.cVariables < EFI_VARIABLE_MAX)
657 {
658 /*
659 * Add a new variable.
660 */
661 LogRel(("EFI: Adding variable %RTuuid::'%s' fAttrib=%#x cbValue=%#x\n", &pThis->NVRAM.VarOpBuf.uuid,
662 pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
663 pEfiVar = (PEFIVAR)RTMemAllocZ(sizeof(EFIVAR));
664 if (pEfiVar)
665 {
666 pEfiVar->uuid = pThis->NVRAM.VarOpBuf.uuid;
667 pEfiVar->cchName = pThis->NVRAM.VarOpBuf.cchName;
668 memcpy(pEfiVar->szName, pThis->NVRAM.VarOpBuf.szName, pEfiVar->cchName); /* The buffer is zeroed, so skip '\0'. */
669 pEfiVar->fAttributes = pThis->NVRAM.VarOpBuf.fAttributes;
670 pEfiVar->cbValue = pThis->NVRAM.VarOpBuf.cbValue;
671 memcpy(pEfiVar->abValue, pThis->NVRAM.VarOpBuf.abValue, pEfiVar->cbValue);
672
673 nvramInsertVariable(pThis, pEfiVar);
674 pThis->NVRAM.cVariables++;
675 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
676 }
677 else
678 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
679 }
680 else
681 {
682 /*
683 * Too many variables.
684 */
685 LogRelMax(5, ("EFI: Too many variables (%RTuuid::'%s' fAttrib=%#x cbValue=%#x)\n", &pThis->NVRAM.VarOpBuf.uuid,
686 pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
687 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
688 Log(("nvramWriteVariableOpAdd: Too many variabled.\n"));
689 }
690
691 LogFunc(("cVariables=%u u32Status=%#x\n", pThis->NVRAM.cVariables, pThis->NVRAM.u32Status));
692 return VINF_SUCCESS;
693}
694
695/**
696 * Implements EFI_VARIABLE_PARAM writes.
697 *
698 * @returns IOM strict status code.
699 * @param pThis The EFI state.
700 * @param u32Value The value being written.
701 */
702static int nvramWriteVariableParam(PDEVEFI pThis, uint32_t u32Value)
703{
704 int rc = VINF_SUCCESS;
705 switch (pThis->NVRAM.enmOp)
706 {
707 case EFI_VM_VARIABLE_OP_START:
708 switch (u32Value)
709 {
710 case EFI_VARIABLE_OP_QUERY:
711 rc = nvramWriteVariableOpQuery(pThis);
712 break;
713
714 case EFI_VARIABLE_OP_QUERY_NEXT:
715 rc = nvramWriteVariableOpQueryNext(pThis);
716 break;
717
718 case EFI_VARIABLE_OP_QUERY_REWIND:
719 Log2(("EFI_VARIABLE_OP_QUERY_REWIND\n"));
720 pThis->NVRAM.pCurVar = NULL;
721 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
722 break;
723
724 case EFI_VARIABLE_OP_ADD:
725 rc = nvramWriteVariableOpAdd(pThis);
726 break;
727
728 default:
729 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
730 LogRel(("EFI: Unknown EFI_VM_VARIABLE_OP_START value %#x\n", u32Value));
731 break;
732 }
733 break;
734
735 case EFI_VM_VARIABLE_OP_GUID:
736 Log2(("EFI_VM_VARIABLE_OP_GUID[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
737 if (pThis->NVRAM.offOpBuffer < sizeof(pThis->NVRAM.VarOpBuf.uuid))
738 pThis->NVRAM.VarOpBuf.uuid.au8[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
739 else
740 {
741 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_GUID write (%#x).\n", u32Value));
742 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
743 }
744 break;
745
746 case EFI_VM_VARIABLE_OP_ATTRIBUTE:
747 Log2(("EFI_VM_VARIABLE_OP_ATTRIBUTE=%#x\n", u32Value));
748 pThis->NVRAM.VarOpBuf.fAttributes = u32Value;
749 break;
750
751 case EFI_VM_VARIABLE_OP_NAME:
752 Log2(("EFI_VM_VARIABLE_OP_NAME[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
753 if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cchName)
754 pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
755 else if (u32Value == 0)
756 Assert(pThis->NVRAM.VarOpBuf.szName[sizeof(pThis->NVRAM.VarOpBuf.szName) - 1] == 0);
757 else
758 {
759 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME write (%#x).\n", u32Value));
760 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
761 }
762 break;
763
764 case EFI_VM_VARIABLE_OP_NAME_LENGTH:
765 Log2(("EFI_VM_VARIABLE_OP_NAME_LENGTH=%#x\n", u32Value));
766 RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
767 if (u32Value < sizeof(pThis->NVRAM.VarOpBuf.szName))
768 pThis->NVRAM.VarOpBuf.cchName = u32Value;
769 else
770 {
771 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_LENGTH write (%#x, max %#x).\n",
772 u32Value, sizeof(pThis->NVRAM.VarOpBuf.szName) - 1));
773 pThis->NVRAM.VarOpBuf.cchName = sizeof(pThis->NVRAM.VarOpBuf.szName) - 1;
774 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
775 }
776 Assert(pThis->NVRAM.offOpBuffer == 0);
777 break;
778
779 case EFI_VM_VARIABLE_OP_NAME_UTF16:
780 {
781 Log2(("EFI_VM_VARIABLE_OP_NAME_UTF16[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
782 /* Currently simplifying this to UCS2, i.e. no surrogates. */
783 if (pThis->NVRAM.offOpBuffer == 0)
784 RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
785 uint32_t cbUtf8 = (uint32_t)RTStrCpSize(u32Value);
786 if (pThis->NVRAM.offOpBuffer + cbUtf8 < sizeof(pThis->NVRAM.VarOpBuf.szName))
787 {
788 RTStrPutCp(&pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer], u32Value);
789 pThis->NVRAM.offOpBuffer += cbUtf8;
790 }
791 else if (u32Value == 0)
792 Assert(pThis->NVRAM.VarOpBuf.szName[sizeof(pThis->NVRAM.VarOpBuf.szName) - 1] == 0);
793 else
794 {
795 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_UTF16 write (%#x).\n", u32Value));
796 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
797 }
798 break;
799 }
800
801 case EFI_VM_VARIABLE_OP_VALUE:
802 Log2(("EFI_VM_VARIABLE_OP_VALUE[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
803 if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cbValue)
804 pThis->NVRAM.VarOpBuf.abValue[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
805 else
806 {
807 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE write (%#x).\n", u32Value));
808 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
809 }
810 break;
811
812 case EFI_VM_VARIABLE_OP_VALUE_LENGTH:
813 Log2(("EFI_VM_VARIABLE_OP_VALUE_LENGTH=%#x\n", u32Value));
814 RT_ZERO(pThis->NVRAM.VarOpBuf.abValue);
815 if (u32Value <= sizeof(pThis->NVRAM.VarOpBuf.abValue))
816 pThis->NVRAM.VarOpBuf.cbValue = u32Value;
817 else
818 {
819 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE_LENGTH write (%#x, max %#x).\n",
820 u32Value, sizeof(pThis->NVRAM.VarOpBuf.abValue)));
821 pThis->NVRAM.VarOpBuf.cbValue = sizeof(pThis->NVRAM.VarOpBuf.abValue);
822 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
823 }
824 Assert(pThis->NVRAM.offOpBuffer == 0);
825 break;
826
827 default:
828 pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
829 LogRel(("EFI: Unexpected variable operation %#x\n", pThis->NVRAM.enmOp));
830 break;
831 }
832 return VINF_SUCCESS;
833}
834
835/**
836 * Implements EFI_VARIABLE_OP reads.
837 *
838 * @returns IOM strict status code.
839 * @param pThis The EFI state.
840 * @param u32Value The value being written.
841 */
842static int nvramReadVariableOp(PDEVEFI pThis, uint32_t *pu32, unsigned cb)
843{
844 switch (pThis->NVRAM.enmOp)
845 {
846 case EFI_VM_VARIABLE_OP_START:
847 *pu32 = pThis->NVRAM.u32Status;
848 break;
849
850 case EFI_VM_VARIABLE_OP_GUID:
851 if (pThis->NVRAM.offOpBuffer < sizeof(pThis->NVRAM.VarOpBuf.uuid) && cb == 1)
852 *pu32 = pThis->NVRAM.VarOpBuf.uuid.au8[pThis->NVRAM.offOpBuffer++];
853 else
854 {
855 if (cb == 1)
856 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_GUID read.\n"));
857 else
858 LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_GUID read size (%d).\n", cb));
859 *pu32 = UINT32_MAX;
860 }
861 break;
862
863 case EFI_VM_VARIABLE_OP_ATTRIBUTE:
864 *pu32 = pThis->NVRAM.VarOpBuf.fAttributes;
865 break;
866
867 case EFI_VM_VARIABLE_OP_NAME:
868 /* allow reading terminator char */
869 if (pThis->NVRAM.offOpBuffer <= pThis->NVRAM.VarOpBuf.cchName && cb == 1)
870 *pu32 = pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer++];
871 else
872 {
873 if (cb == 1)
874 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME read.\n"));
875 else
876 LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_NAME read size (%d).\n", cb));
877 *pu32 = UINT32_MAX;
878 }
879 break;
880
881 case EFI_VM_VARIABLE_OP_NAME_LENGTH:
882 *pu32 = pThis->NVRAM.VarOpBuf.cchName;
883 break;
884
885 case EFI_VM_VARIABLE_OP_NAME_UTF16:
886 /* Lazy bird: ASSUME no surrogate pairs. */
887 if (pThis->NVRAM.offOpBuffer <= pThis->NVRAM.VarOpBuf.cchName && cb == 2)
888 {
889 char const *psz1 = &pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer];
890 char const *psz2 = psz1;
891 RTUNICP Cp;
892 RTStrGetCpEx(&psz2, &Cp);
893 *pu32 = Cp;
894 Log2(("EFI_VM_VARIABLE_OP_NAME_UTF16[%u] => %#x (+%d)\n", pThis->NVRAM.offOpBuffer, *pu32, psz2 - psz1));
895 pThis->NVRAM.offOpBuffer += psz2 - psz1;
896 }
897 else
898 {
899 if (cb == 2)
900 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_UTF16 read.\n"));
901 else
902 LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_NAME_UTF16 read size (%d).\n", cb));
903 *pu32 = UINT32_MAX;
904 }
905 break;
906
907 case EFI_VM_VARIABLE_OP_NAME_LENGTH_UTF16:
908 /* Lazy bird: ASSUME no surrogate pairs. */
909 *pu32 = (uint32_t)RTStrUniLen(pThis->NVRAM.VarOpBuf.szName);
910 break;
911
912 case EFI_VM_VARIABLE_OP_VALUE:
913 if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cbValue && cb == 1)
914 *pu32 = pThis->NVRAM.VarOpBuf.abValue[pThis->NVRAM.offOpBuffer++];
915 else
916 {
917 if (cb == 1)
918 LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE read.\n"));
919 else
920 LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_VALUE read size (%d).\n", cb));
921 *pu32 = UINT32_MAX;
922 }
923 break;
924
925 case EFI_VM_VARIABLE_OP_VALUE_LENGTH:
926 *pu32 = pThis->NVRAM.VarOpBuf.cbValue;
927 break;
928
929 default:
930 *pu32 = UINT32_MAX;
931 break;
932 }
933 return VINF_SUCCESS;
934}
935
936
937/**
938 * Checks if the EFI variable value looks like a printable UTF-8 string.
939 *
940 * @returns true if it is, false if not.
941 * @param pEfiVar The variable.
942 * @param pfZeroTerm Where to return whether the string is zero
943 * terminated.
944 */
945static bool efiInfoNvramIsUtf8(PCEFIVAR pEfiVar, bool *pfZeroTerm)
946{
947 if (pEfiVar->cbValue < 2)
948 return false;
949 const char *pachValue = (const char *)&pEfiVar->abValue[0];
950 *pfZeroTerm = pachValue[pEfiVar->cbValue - 1] == 0;
951
952 /* Check the length. */
953 size_t cchValue = RTStrNLen((const char *)pEfiVar->abValue, pEfiVar->cbValue);
954 if (cchValue != pEfiVar->cbValue - *pfZeroTerm)
955 return false; /* stray zeros in the value, forget it. */
956
957 /* Check that the string is valid UTF-8 and printable. */
958 const char *pchCur = pachValue;
959 while ((uintptr_t)(pchCur - pachValue) < cchValue)
960 {
961 RTUNICP uc;
962 int rc = RTStrGetCpEx(&pachValue, &uc);
963 if (RT_FAILURE(rc))
964 return false;
965 /** @todo Missing RTUniCpIsPrintable. */
966 if (uc < 128 && !RT_C_IS_PRINT(uc))
967 return false;
968 }
969
970 return true;
971}
972
973
974/**
975 * Checks if the EFI variable value looks like a printable UTF-16 string.
976 *
977 * @returns true if it is, false if not.
978 * @param pEfiVar The variable.
979 * @param pfZeroTerm Where to return whether the string is zero
980 * terminated.
981 */
982static bool efiInfoNvramIsUtf16(PCEFIVAR pEfiVar, bool *pfZeroTerm)
983{
984 if (pEfiVar->cbValue < 4 || (pEfiVar->cbValue & 1))
985 return false;
986
987 PCRTUTF16 pwcValue = (PCRTUTF16)&pEfiVar->abValue[0];
988 size_t cwcValue = pEfiVar->cbValue / sizeof(RTUTF16);
989 *pfZeroTerm = pwcValue[cwcValue - 1] == 0;
990 if (!*pfZeroTerm && RTUtf16IsHighSurrogate(pwcValue[cwcValue - 1]))
991 return false; /* Catch bad string early, before reading a char too many. */
992 cwcValue -= *pfZeroTerm;
993 if (cwcValue < 2)
994 return false;
995
996 /* Check that the string is valid UTF-16, printable and spans the whole
997 value length. */
998 size_t cAscii = 0;
999 PCRTUTF16 pwcCur = pwcValue;
1000 while ((uintptr_t)(pwcCur - pwcValue) < cwcValue)
1001 {
1002 RTUNICP uc;
1003 int rc = RTUtf16GetCpEx(&pwcCur, &uc);
1004 if (RT_FAILURE(rc))
1005 return false;
1006 /** @todo Missing RTUniCpIsPrintable. */
1007 if (uc < 128 && !RT_C_IS_PRINT(uc))
1008 return false;
1009 cAscii += uc < 128;
1010 }
1011 if (cAscii < 2)
1012 return false;
1013
1014 return true;
1015}
1016
1017
1018/**
1019 * @implement_callback_method{FNDBGFHANDLERDEV}
1020 */
1021static DECLCALLBACK(void) efiInfoNvram(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1022{
1023 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1024 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1025
1026 pHlp->pfnPrintf(pHlp, "NVRAM variables: %u\n", pThis->NVRAM.cVariables);
1027 PCEFIVAR pEfiVar;
1028 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
1029 {
1030 /* Detect UTF-8 and UTF-16 strings. */
1031 bool fZeroTerm = false;
1032 if (efiInfoNvramIsUtf8(pEfiVar, &fZeroTerm))
1033 pHlp->pfnPrintf(pHlp,
1034 "Variable - fAttr=%#04x - '%RTuuid:%s' - cb=%#04x\n"
1035 "String value (UTF-8%s): \"%.*s\"\n",
1036 pEfiVar->fAttributes, &pEfiVar->uuid, pEfiVar->szName, pEfiVar->cbValue,
1037 fZeroTerm ? "" : ",nz", pEfiVar->cbValue, pEfiVar->abValue);
1038 else if (efiInfoNvramIsUtf16(pEfiVar, &fZeroTerm))
1039 pHlp->pfnPrintf(pHlp,
1040 "Variable - fAttr=%#04x - '%RTuuid:%s' - cb=%#04x\n"
1041 "String value (UTF-16%s): \"%.*ls\"\n",
1042 pEfiVar->fAttributes, &pEfiVar->uuid, pEfiVar->szName, pEfiVar->cbValue,
1043 fZeroTerm ? "" : ",nz", pEfiVar->cbValue, pEfiVar->abValue);
1044 else
1045 pHlp->pfnPrintf(pHlp,
1046 "Variable - fAttr=%#04x - '%RTuuid:%s' - cb=%#04x\n"
1047 "%.*Rhxd\n",
1048 pEfiVar->fAttributes, &pEfiVar->uuid, pEfiVar->szName, pEfiVar->cbValue,
1049 pEfiVar->cbValue, pEfiVar->abValue);
1050
1051 }
1052
1053 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1054}
1055
1056
1057
1058/**
1059 * Gets the info item size.
1060 *
1061 * @returns Size in bytes, UINT32_MAX on error.
1062 * @param pThis .
1063 */
1064static uint32_t efiInfoSize(PDEVEFI pThis)
1065{
1066 switch (pThis->iInfoSelector)
1067 {
1068 case EFI_INFO_INDEX_VOLUME_BASE:
1069 case EFI_INFO_INDEX_VOLUME_SIZE:
1070 case EFI_INFO_INDEX_TEMPMEM_BASE:
1071 case EFI_INFO_INDEX_TEMPMEM_SIZE:
1072 case EFI_INFO_INDEX_STACK_BASE:
1073 case EFI_INFO_INDEX_STACK_SIZE:
1074 case EFI_INFO_INDEX_GOP_MODE:
1075 case EFI_INFO_INDEX_UGA_VERTICAL_RESOLUTION:
1076 case EFI_INFO_INDEX_UGA_HORIZONTAL_RESOLUTION:
1077 return 4;
1078 case EFI_INFO_INDEX_BOOT_ARGS:
1079 return (uint32_t)RTStrNLen(pThis->szBootArgs, sizeof(pThis->szBootArgs)) + 1;
1080 case EFI_INFO_INDEX_DEVICE_PROPS:
1081 return pThis->cbDeviceProps;
1082 case EFI_INFO_INDEX_FSB_FREQUENCY:
1083 case EFI_INFO_INDEX_CPU_FREQUENCY:
1084 case EFI_INFO_INDEX_TSC_FREQUENCY:
1085 return 8;
1086 }
1087 return UINT32_MAX;
1088}
1089
1090
1091/**
1092 * efiInfoNextByte for a uint64_t value.
1093 *
1094 * @returns Next (current) byte.
1095 * @param pThis The EFI instance data.
1096 * @param u64 The value.
1097 */
1098static uint8_t efiInfoNextByteU64(PDEVEFI pThis, uint64_t u64)
1099{
1100 uint64_t off = pThis->offInfo;
1101 if (off >= 8)
1102 return 0;
1103 return (uint8_t)(u64 >> (off * 8));
1104}
1105
1106/**
1107 * efiInfoNextByte for a uint32_t value.
1108 *
1109 * @returns Next (current) byte.
1110 * @param pThis The EFI instance data.
1111 * @param u32 The value.
1112 */
1113static uint8_t efiInfoNextByteU32(PDEVEFI pThis, uint32_t u32)
1114{
1115 uint32_t off = pThis->offInfo;
1116 if (off >= 4)
1117 return 0;
1118 return (uint8_t)(u32 >> (off * 8));
1119}
1120
1121/**
1122 * efiInfoNextByte for a buffer.
1123 *
1124 * @returns Next (current) byte.
1125 * @param pThis The EFI instance data.
1126 * @param pvBuf The buffer.
1127 * @param cbBuf The buffer size.
1128 */
1129static uint8_t efiInfoNextByteBuf(PDEVEFI pThis, void const *pvBuf, size_t cbBuf)
1130{
1131 uint32_t off = pThis->offInfo;
1132 if (off >= cbBuf)
1133 return 0;
1134 return ((uint8_t const *)pvBuf)[off];
1135}
1136
1137/**
1138 * Gets the next info byte.
1139 *
1140 * @returns Next (current) byte.
1141 * @param pThis The EFI instance data.
1142 */
1143static uint8_t efiInfoNextByte(PDEVEFI pThis)
1144{
1145 switch (pThis->iInfoSelector)
1146 {
1147
1148 case EFI_INFO_INDEX_VOLUME_BASE: return efiInfoNextByteU64(pThis, pThis->GCLoadAddress);
1149 case EFI_INFO_INDEX_VOLUME_SIZE: return efiInfoNextByteU64(pThis, pThis->cbEfiRom);
1150 case EFI_INFO_INDEX_TEMPMEM_BASE: return efiInfoNextByteU32(pThis, VBOX_EFI_TOP_OF_STACK); /* just after stack */
1151 case EFI_INFO_INDEX_TEMPMEM_SIZE: return efiInfoNextByteU32(pThis, _512K);
1152 case EFI_INFO_INDEX_FSB_FREQUENCY: return efiInfoNextByteU64(pThis, pThis->u64FsbFrequency);
1153 case EFI_INFO_INDEX_TSC_FREQUENCY: return efiInfoNextByteU64(pThis, pThis->u64TscFrequency);
1154 case EFI_INFO_INDEX_CPU_FREQUENCY: return efiInfoNextByteU64(pThis, pThis->u64CpuFrequency);
1155 case EFI_INFO_INDEX_BOOT_ARGS: return efiInfoNextByteBuf(pThis, pThis->szBootArgs, sizeof(pThis->szBootArgs));
1156 case EFI_INFO_INDEX_DEVICE_PROPS: return efiInfoNextByteBuf(pThis, pThis->pbDeviceProps, pThis->cbDeviceProps);
1157 case EFI_INFO_INDEX_GOP_MODE: return efiInfoNextByteU32(pThis, pThis->u32GopMode);
1158 case EFI_INFO_INDEX_UGA_HORIZONTAL_RESOLUTION: return efiInfoNextByteU32(pThis, pThis->cxUgaResolution);
1159 case EFI_INFO_INDEX_UGA_VERTICAL_RESOLUTION: return efiInfoNextByteU32(pThis, pThis->cyUgaResolution);
1160
1161 /* Keep in sync with value in EfiThunk.asm */
1162 case EFI_INFO_INDEX_STACK_BASE: return efiInfoNextByteU32(pThis, VBOX_EFI_TOP_OF_STACK - _128K); /* 2M - 128 K */
1163 case EFI_INFO_INDEX_STACK_SIZE: return efiInfoNextByteU32(pThis, _128K);
1164
1165 default:
1166 PDMDevHlpDBGFStop(pThis->pDevIns, RT_SRC_POS, "%#x", pThis->iInfoSelector);
1167 return 0;
1168 }
1169}
1170
1171
1172#ifdef IN_RING3
1173static void efiVBoxDbgScript(PDEVEFI pThis, const char *pszFormat, ...)
1174{
1175# ifdef DEVEFI_WITH_VBOXDBG_SCRIPT
1176 PRTSTREAM pStrm;
1177 int rc2 = RTStrmOpen("./DevEFI.VBoxDbg", "a", &pStrm);
1178 if (RT_SUCCESS(rc2))
1179 {
1180 va_list va;
1181 va_start(va, pszFormat);
1182 RTStrmPrintfV(pStrm, pszFormat, va);
1183 va_end(va);
1184 RTStrmClose(pStrm);
1185 }
1186# endif
1187}
1188#endif /* IN_RING3 */
1189
1190
1191/**
1192 * Handles writes to the image event port.
1193 *
1194 * @returns VBox status suitable for I/O port write handler.
1195 *
1196 * @param pThis The EFI state.
1197 * @param u32 The value being written.
1198 * @param cb The size of the value.
1199 */
1200static int efiPortImageEventWrite(PDEVEFI pThis, uint32_t u32, unsigned cb)
1201{
1202 switch (u32 & EFI_IMAGE_EVT_CMD_MASK)
1203 {
1204 case EFI_IMAGE_EVT_CMD_START_LOAD32:
1205 case EFI_IMAGE_EVT_CMD_START_LOAD64:
1206 case EFI_IMAGE_EVT_CMD_START_UNLOAD32:
1207 case EFI_IMAGE_EVT_CMD_START_UNLOAD64:
1208 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) == 0);
1209
1210 /* Reset the state. */
1211 RT_ZERO(pThis->ImageEvt);
1212 pThis->ImageEvt.uEvt = (uint8_t)u32; Assert(pThis->ImageEvt.uEvt == u32);
1213 return VINF_SUCCESS;
1214
1215 case EFI_IMAGE_EVT_CMD_COMPLETE:
1216 {
1217#ifdef IN_RING3
1218 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) == 0);
1219
1220 /* For now, just log it. */
1221 static uint64_t s_cImageEvtLogged = 0;
1222 if (s_cImageEvtLogged < 2048)
1223 {
1224 s_cImageEvtLogged++;
1225 switch (pThis->ImageEvt.uEvt)
1226 {
1227 /* ASSUMES the name ends with .pdb and the image file ends with .efi! */
1228 case EFI_IMAGE_EVT_CMD_START_LOAD32:
1229 LogRel(("EFI: VBoxDbg> loadimage32 '%.*s.efi' %#llx LB %#llx\n",
1230 pThis->ImageEvt.offName - 4, pThis->ImageEvt.szName, pThis->ImageEvt.uAddr0, pThis->ImageEvt.cb0));
1231 if (pThis->ImageEvt.offName > 4)
1232 efiVBoxDbgScript(pThis, "loadimage32 '%.*s.efi' %#llx\n",
1233 pThis->ImageEvt.offName - 4, pThis->ImageEvt.szName, pThis->ImageEvt.uAddr0);
1234 break;
1235 case EFI_IMAGE_EVT_CMD_START_LOAD64:
1236 LogRel(("EFI: VBoxDbg> loadimage64 '%.*s.efi' %#llx LB %#llx\n",
1237 pThis->ImageEvt.offName - 4, pThis->ImageEvt.szName, pThis->ImageEvt.uAddr0, pThis->ImageEvt.cb0));
1238 if (pThis->ImageEvt.offName > 4)
1239 efiVBoxDbgScript(pThis, "loadimage64 '%.*s.efi' %#llx\n",
1240 pThis->ImageEvt.offName - 4, pThis->ImageEvt.szName, pThis->ImageEvt.uAddr0);
1241 break;
1242 case EFI_IMAGE_EVT_CMD_START_UNLOAD32:
1243 case EFI_IMAGE_EVT_CMD_START_UNLOAD64:
1244 {
1245 LogRel(("EFI: VBoxDbg> unload '%.*s.efi' # %#llx LB %#llx\n",
1246 pThis->ImageEvt.offName - 4 - pThis->ImageEvt.offNameLastComponent,
1247 &pThis->ImageEvt.szName[pThis->ImageEvt.offNameLastComponent],
1248 pThis->ImageEvt.uAddr0, pThis->ImageEvt.cb0));
1249 if (pThis->ImageEvt.offName > 4)
1250 efiVBoxDbgScript(pThis, "unload '%.*s.efi'\n",
1251 pThis->ImageEvt.offName - 4 - pThis->ImageEvt.offNameLastComponent,
1252 &pThis->ImageEvt.szName[pThis->ImageEvt.offNameLastComponent]);
1253 break;
1254 }
1255 }
1256 }
1257 return VINF_SUCCESS;
1258#else
1259 return VINF_IOM_R3_IOPORT_WRITE;
1260#endif
1261 }
1262
1263 case EFI_IMAGE_EVT_CMD_ADDR0:
1264 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) <= UINT16_MAX);
1265 pThis->ImageEvt.uAddr0 <<= 16;
1266 pThis->ImageEvt.uAddr0 |= EFI_IMAGE_EVT_GET_PAYLOAD_U16(u32);
1267 return VINF_SUCCESS;
1268
1269 case EFI_IMAGE_EVT_CMD_ADDR1:
1270 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) <= UINT16_MAX);
1271 pThis->ImageEvt.uAddr0 <<= 16;
1272 pThis->ImageEvt.uAddr0 |= EFI_IMAGE_EVT_GET_PAYLOAD_U16(u32);
1273 return VINF_SUCCESS;
1274
1275 case EFI_IMAGE_EVT_CMD_SIZE0:
1276 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) <= UINT16_MAX);
1277 pThis->ImageEvt.cb0 <<= 16;
1278 pThis->ImageEvt.cb0 |= EFI_IMAGE_EVT_GET_PAYLOAD_U16(u32);
1279 return VINF_SUCCESS;
1280
1281 case EFI_IMAGE_EVT_CMD_NAME:
1282 AssertBreak(EFI_IMAGE_EVT_GET_PAYLOAD(u32) <= 0x7f);
1283 if (pThis->ImageEvt.offName < sizeof(pThis->ImageEvt.szName) - 1)
1284 {
1285 char ch = EFI_IMAGE_EVT_GET_PAYLOAD_U8(u32);
1286 if (ch == '\\')
1287 ch = '/';
1288 pThis->ImageEvt.szName[pThis->ImageEvt.offName++] = ch;
1289 if (ch == '/' || ch == ':')
1290 pThis->ImageEvt.offNameLastComponent = pThis->ImageEvt.offName;
1291 }
1292 else
1293 Log(("EFI: Image name overflow\n"));
1294 return VINF_SUCCESS;
1295 }
1296
1297 Log(("EFI: Unknown image event: %#x (cb=%d)\n", u32, cb));
1298 return VINF_SUCCESS;
1299}
1300
1301
1302/**
1303 * Port I/O Handler for IN operations.
1304 *
1305 * @returns VBox status code.
1306 *
1307 * @param pDevIns The device instance.
1308 * @param pvUser User argument - ignored.
1309 * @param Port Port number used for the IN operation.
1310 * @param pu32 Where to store the result.
1311 * @param cb Number of bytes read.
1312 */
1313static DECLCALLBACK(int) efiIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1314{
1315 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1316 Log4(("EFI in: %x %x\n", Port, cb));
1317
1318 switch (Port)
1319 {
1320 case EFI_INFO_PORT:
1321 if (pThis->offInfo == -1 && cb == 4)
1322 {
1323 pThis->offInfo = 0;
1324 uint32_t cbInfo = *pu32 = efiInfoSize(pThis);
1325 if (cbInfo == UINT32_MAX)
1326 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "iInfoSelector=%#x (%d)\n",
1327 pThis->iInfoSelector, pThis->iInfoSelector);
1328 }
1329 else
1330 {
1331 if (cb != 1)
1332 return VERR_IOM_IOPORT_UNUSED;
1333 *pu32 = efiInfoNextByte(pThis);
1334 pThis->offInfo++;
1335 }
1336 return VINF_SUCCESS;
1337
1338 case EFI_PANIC_PORT:
1339#ifdef IN_RING3
1340 LogRel(("EFI panic port read!\n"));
1341 /* Insert special code here on panic reads */
1342 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "EFI Panic: panic port read!\n");
1343#else
1344 /* Reschedule to R3 */
1345 return VINF_IOM_R3_IOPORT_READ;
1346#endif
1347
1348 case EFI_PORT_VARIABLE_OP:
1349 return nvramReadVariableOp(pThis, pu32, cb);
1350
1351 case EFI_PORT_VARIABLE_PARAM:
1352 case EFI_PORT_DEBUG_POINT:
1353 case EFI_PORT_IMAGE_EVENT:
1354 *pu32 = UINT32_MAX;
1355 return VINF_SUCCESS;
1356 }
1357
1358 return VERR_IOM_IOPORT_UNUSED;
1359}
1360
1361
1362/**
1363 * Translates a debug point value into a string for logging.
1364 *
1365 * @returns read-only string
1366 * @param enmDbgPoint Valid debug point value.
1367 */
1368static const char *efiDbgPointName(EFIDBGPOINT enmDbgPoint)
1369{
1370 switch (enmDbgPoint)
1371 {
1372 case EFIDBGPOINT_SEC_PREMEM: return "SEC_PREMEM";
1373 case EFIDBGPOINT_SEC_POSTMEM: return "SEC_POSTMEM";
1374 case EFIDBGPOINT_DXE_CORE: return "DXE_CORE";
1375 case EFIDBGPOINT_SMM: return "SMM";
1376 case EFIDBGPOINT_SMI_ENTER: return "SMI_ENTER";
1377 case EFIDBGPOINT_SMI_EXIT: return "SMI_EXIT";
1378 case EFIDBGPOINT_GRAPHICS: return "GRAPHICS";
1379 case EFIDBGPOINT_DXE_AP: return "DXE_AP";
1380 default:
1381 AssertFailed();
1382 return "Unknown";
1383 }
1384}
1385
1386
1387/**
1388 * Port I/O Handler for OUT operations.
1389 *
1390 * @returns VBox status code.
1391 *
1392 * @param pDevIns The device instance.
1393 * @param pvUser User argument - ignored.
1394 * @param Port Port number used for the IN operation.
1395 * @param u32 The value to output.
1396 * @param cb The value size in bytes.
1397 */
1398static DECLCALLBACK(int) efiIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1399{
1400 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1401 int rc = VINF_SUCCESS;
1402 Log4(("efi: out %x %x %d\n", Port, u32, cb));
1403
1404 switch (Port)
1405 {
1406 case EFI_INFO_PORT:
1407 Log2(("EFI_INFO_PORT: iInfoSelector=%#x\n", u32));
1408 pThis->iInfoSelector = u32;
1409 pThis->offInfo = -1;
1410 break;
1411
1412 case EFI_DEBUG_PORT:
1413 {
1414 /* The raw version. */
1415 switch (u32)
1416 {
1417 case '\r': Log3(("efi: <return>\n")); break;
1418 case '\n': Log3(("efi: <newline>\n")); break;
1419 case '\t': Log3(("efi: <tab>\n")); break;
1420 default: Log3(("efi: %c (%02x)\n", u32, u32)); break;
1421 }
1422 /* The readable, buffered version. */
1423 if (u32 == '\n' || u32 == '\r')
1424 {
1425 pThis->szMsg[pThis->iMsg] = '\0';
1426 if (pThis->iMsg)
1427 Log(("efi: %s\n", pThis->szMsg));
1428 pThis->iMsg = 0;
1429 }
1430 else
1431 {
1432 if (pThis->iMsg >= sizeof(pThis->szMsg)-1)
1433 {
1434 pThis->szMsg[pThis->iMsg] = '\0';
1435 Log(("efi: %s\n", pThis->szMsg));
1436 pThis->iMsg = 0;
1437 }
1438 pThis->szMsg[pThis->iMsg] = (char )u32;
1439 pThis->szMsg[++pThis->iMsg] = '\0';
1440 }
1441 break;
1442 }
1443
1444 case EFI_PANIC_PORT:
1445 {
1446 switch (u32)
1447 {
1448 case EFI_PANIC_CMD_BAD_ORG: /* Legacy */
1449 case EFI_PANIC_CMD_THUNK_TRAP:
1450#ifdef IN_RING3
1451 LogRel(("EFI: Panic! Unexpected trap!!\n"));
1452# ifdef VBOX_STRICT
1453 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "EFI Panic: Unexpected trap during early bootstrap!\n");
1454# else
1455 AssertReleaseMsgFailed(("Unexpected trap during early EFI bootstrap!!\n"));
1456# endif
1457 break;
1458#else
1459 return VINF_IOM_R3_IOPORT_WRITE;
1460#endif
1461
1462 case EFI_PANIC_CMD_START_MSG:
1463 LogRel(("Receiving EFI panic...\n"));
1464 pThis->iPanicMsg = 0;
1465 pThis->szPanicMsg[0] = '\0';
1466 break;
1467
1468 case EFI_PANIC_CMD_END_MSG:
1469#ifdef IN_RING3
1470 LogRel(("EFI: Panic! %s\n", pThis->szPanicMsg));
1471# ifdef VBOX_STRICT
1472 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "EFI Panic: %s\n", pThis->szPanicMsg);
1473# else
1474 return VERR_INTERNAL_ERROR;
1475# endif
1476#else
1477 return VINF_IOM_R3_IOPORT_WRITE;
1478#endif
1479
1480
1481 default:
1482 if ( u32 >= EFI_PANIC_CMD_MSG_FIRST
1483 && u32 <= EFI_PANIC_CMD_MSG_LAST)
1484 {
1485 /* Add the message char to the buffer. */
1486 uint32_t i = pThis->iPanicMsg;
1487 if (i + 1 < sizeof(pThis->szPanicMsg))
1488 {
1489 char ch = EFI_PANIC_CMD_MSG_GET_CHAR(u32);
1490 if ( ch == '\n'
1491 && i > 0
1492 && pThis->szPanicMsg[i - 1] == '\r')
1493 i--;
1494 pThis->szPanicMsg[i] = ch;
1495 pThis->szPanicMsg[i + 1] = '\0';
1496 pThis->iPanicMsg = i + 1;
1497 }
1498 }
1499 else
1500 Log(("EFI: Unknown panic command: %#x (cb=%d)\n", u32, cb));
1501 break;
1502 }
1503 break;
1504 }
1505
1506 case EFI_PORT_VARIABLE_OP:
1507 {
1508 /* clear buffer index */
1509 if (u32 >= (uint32_t)EFI_VM_VARIABLE_OP_MAX)
1510 {
1511 Log(("EFI: Invalid variable op %#x\n", u32));
1512 u32 = EFI_VM_VARIABLE_OP_ERROR;
1513 }
1514 pThis->NVRAM.offOpBuffer = 0;
1515 pThis->NVRAM.enmOp = (EFIVAROP)u32;
1516 Log2(("EFI_VARIABLE_OP: enmOp=%#x (%d)\n", u32));
1517 break;
1518 }
1519
1520 case EFI_PORT_VARIABLE_PARAM:
1521 rc = nvramWriteVariableParam(pThis, u32);
1522 break;
1523
1524 case EFI_PORT_DEBUG_POINT:
1525#ifdef IN_RING3
1526 if (u32 > EFIDBGPOINT_INVALID && u32 < EFIDBGPOINT_END)
1527 {
1528 /* For now, just log it. */
1529 LogRelMax(1024, ("EFI: debug point %s\n", efiDbgPointName((EFIDBGPOINT)u32)));
1530 rc = VINF_SUCCESS;
1531 }
1532 else
1533 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "Invalid debug point %#x\n", u32);
1534 break;
1535#else
1536 return VINF_IOM_R3_IOPORT_WRITE;
1537#endif
1538
1539 case EFI_PORT_IMAGE_EVENT:
1540 rc = efiPortImageEventWrite(pThis, u32, cb);
1541 break;
1542
1543 default:
1544 Log(("EFI: Write to reserved port %RTiop: %#x (cb=%d)\n", Port, u32, cb));
1545 break;
1546 }
1547 return rc;
1548}
1549
1550static DECLCALLBACK(int) efiSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1551{
1552 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1553 LogFlow(("efiSaveExec:\n"));
1554
1555 /*
1556 * Set variables only used when saving state.
1557 */
1558 uint32_t idUniqueSavedState = 0;
1559 PEFIVAR pEfiVar;
1560 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
1561 {
1562 pEfiVar->idUniqueSavedState = idUniqueSavedState++;
1563 }
1564 Assert(idUniqueSavedState == pThis->NVRAM.cVariables);
1565
1566 pThis->NVRAM.idUniqueCurVar = pThis->NVRAM.pCurVar
1567 ? pThis->NVRAM.pCurVar->idUniqueSavedState
1568 : UINT32_MAX;
1569
1570 /*
1571 * Save the NVRAM state.
1572 */
1573 SSMR3PutStructEx(pSSM, &pThis->NVRAM, sizeof(NVRAMDESC), 0, g_aEfiNvramDescField, NULL);
1574 SSMR3PutStructEx(pSSM, &pThis->NVRAM.VarOpBuf, sizeof(EFIVAR), 0, g_aEfiVariableDescFields, NULL);
1575
1576 /*
1577 * Save the list variables (we saved the length above).
1578 */
1579 RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
1580 {
1581 SSMR3PutStructEx(pSSM, pEfiVar, sizeof(EFIVAR), 0, g_aEfiVariableDescFields, NULL);
1582 }
1583
1584 return VINF_SUCCESS; /* SSM knows */
1585}
1586
1587static DECLCALLBACK(int) efiLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1588{
1589 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1590 LogFlow(("efiLoadExec: uVersion=%d uPass=%d\n", uVersion, uPass));
1591
1592 /*
1593 * Validate input.
1594 */
1595 if (uPass != SSM_PASS_FINAL)
1596 return VERR_SSM_UNEXPECTED_PASS;
1597 if ( uVersion != EFI_SSM_VERSION
1598 && uVersion != EFI_SSM_VERSION_4_2
1599 )
1600 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1601
1602 /*
1603 * Kill the current variables before loading anything.
1604 */
1605 nvramFlushDeviceVariableList(pThis);
1606
1607 /*
1608 * Load the NVRAM state.
1609 */
1610 int rc = SSMR3GetStructEx(pSSM, &pThis->NVRAM, sizeof(NVRAMDESC), 0, g_aEfiNvramDescField, NULL);
1611 AssertRCReturn(rc, rc);
1612 pThis->NVRAM.pCurVar = NULL;
1613
1614 rc = SSMR3GetStructEx(pSSM, &pThis->NVRAM.VarOpBuf, sizeof(EFIVAR), 0, g_aEfiVariableDescFields, NULL);
1615 AssertRCReturn(rc, rc);
1616
1617 /*
1618 * Load variables.
1619 */
1620 pThis->NVRAM.pCurVar = NULL;
1621 Assert(RTListIsEmpty(&pThis->NVRAM.VarList));
1622 RTListInit(&pThis->NVRAM.VarList);
1623 for (uint32_t i = 0; i < pThis->NVRAM.cVariables; i++)
1624 {
1625 PEFIVAR pEfiVar = (PEFIVAR)RTMemAllocZ(sizeof(EFIVAR));
1626 AssertReturn(pEfiVar, VERR_NO_MEMORY);
1627
1628 rc = SSMR3GetStructEx(pSSM, pEfiVar, sizeof(EFIVAR), 0, g_aEfiVariableDescFields, NULL);
1629 if (RT_SUCCESS(rc))
1630 {
1631 if ( pEfiVar->cbValue > sizeof(pEfiVar->abValue)
1632 || pEfiVar->cbValue == 0)
1633 {
1634 rc = VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1635 LogRel(("EFI: Loaded invalid variable value length %#x\n", pEfiVar->cbValue));
1636 }
1637 uint32_t cchVarName = (uint32_t)RTStrNLen(pEfiVar->szName, sizeof(pEfiVar->szName));
1638 if (cchVarName >= sizeof(pEfiVar->szName))
1639 {
1640 rc = VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1641 LogRel(("EFI: Loaded variable name is unterminated.\n"));
1642 }
1643 if (pEfiVar->cchName > cchVarName) /* No check for 0 here, busted load code in 4.2, so now storing 0 here. */
1644 {
1645 rc = VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1646 LogRel(("EFI: Loaded invalid variable name length %#x (cchVarName=%#x)\n", pEfiVar->cchName, cchVarName));
1647 }
1648 if (RT_SUCCESS(rc))
1649 pEfiVar->cchName = cchVarName;
1650 }
1651 AssertRCReturnStmt(rc, RTMemFree(pEfiVar), rc);
1652
1653 /* Add it (not using nvramInsertVariable to preserve saved order),
1654 updating the current variable pointer while we're here. */
1655#if 1
1656 RTListAppend(&pThis->NVRAM.VarList, &pEfiVar->ListNode);
1657#else
1658 nvramInsertVariable(pThis, pEfiVar);
1659#endif
1660 if (pThis->NVRAM.idUniqueCurVar == pEfiVar->idUniqueSavedState)
1661 pThis->NVRAM.pCurVar = pEfiVar;
1662 }
1663
1664 return VINF_SUCCESS;
1665}
1666
1667
1668/**
1669 * @copydoc(PDMIBASE::pfnQueryInterface)
1670 */
1671static DECLCALLBACK(void *) devEfiQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1672{
1673 LogFlowFunc(("ENTER: pIBase: %p, pszIID:%p\n", __FUNCTION__, pInterface, pszIID));
1674 PDEVEFI pThis = RT_FROM_MEMBER(pInterface, DEVEFI, Lun0.IBase);
1675
1676 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
1677 return NULL;
1678}
1679
1680
1681/**
1682 * Write to CMOS memory.
1683 * This is used by the init complete code.
1684 */
1685static void cmosWrite(PPDMDEVINS pDevIns, unsigned off, uint32_t u32Val)
1686{
1687 Assert(off < 128);
1688 Assert(u32Val < 256);
1689
1690 int rc = PDMDevHlpCMOSWrite(pDevIns, off, u32Val);
1691 AssertRC(rc);
1692}
1693
1694/**
1695 * Init complete notification.
1696 *
1697 * @returns VBOX status code.
1698 * @param pDevIns The device instance.
1699 */
1700static DECLCALLBACK(int) efiInitComplete(PPDMDEVINS pDevIns)
1701{
1702 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1703
1704 /*
1705 * Memory sizes.
1706 */
1707 uint64_t const offRamHole = _4G - pThis->cbRamHole;
1708 uint32_t u32Low = 0;
1709 uint32_t u32Chunks = 0;
1710 if (pThis->cbRam > 16 * _1M)
1711 {
1712 u32Low = (uint32_t)RT_MIN(RT_MIN(pThis->cbRam, offRamHole), UINT32_C(0xffe00000));
1713 u32Chunks = (u32Low - 16U * _1M) / _64K;
1714 }
1715 cmosWrite(pDevIns, 0x34, RT_BYTE1(u32Chunks));
1716 cmosWrite(pDevIns, 0x35, RT_BYTE2(u32Chunks));
1717
1718 if (u32Low < pThis->cbRam)
1719 {
1720 uint64_t u64 = pThis->cbRam - u32Low;
1721 u32Chunks = (uint32_t)(u64 / _64K);
1722 cmosWrite(pDevIns, 0x5b, RT_BYTE1(u32Chunks));
1723 cmosWrite(pDevIns, 0x5c, RT_BYTE2(u32Chunks));
1724 cmosWrite(pDevIns, 0x5d, RT_BYTE3(u32Chunks));
1725 cmosWrite(pDevIns, 0x5e, RT_BYTE4(u32Chunks));
1726 }
1727
1728 /*
1729 * Number of CPUs.
1730 */
1731 cmosWrite(pDevIns, 0x60, pThis->cCpus & 0xff);
1732
1733 return VINF_SUCCESS;
1734}
1735
1736
1737/**
1738 * @interface_method_impl{PDMDEVREG,pfnMemSetup}
1739 */
1740static DECLCALLBACK(void) efiMemSetup(PPDMDEVINS pDevIns, PDMDEVMEMSETUPCTX enmCtx)
1741{
1742 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1743
1744 /*
1745 * Plan some structures in RAM.
1746 */
1747 FwCommonPlantSmbiosAndDmiHdrs(pDevIns, pThis->cbDmiTables, pThis->cNumDmiTables);
1748 if (pThis->u8IOAPIC)
1749 FwCommonPlantMpsFloatPtr(pDevIns);
1750
1751 /*
1752 * Re-shadow the Firmware Volume and make it RAM/RAM.
1753 */
1754 uint32_t cPages = RT_ALIGN_64(pThis->cbEfiRom, PAGE_SIZE) >> PAGE_SHIFT;
1755 RTGCPHYS GCPhys = pThis->GCLoadAddress;
1756 while (cPages > 0)
1757 {
1758 uint8_t abPage[PAGE_SIZE];
1759
1760 /* Read the (original) ROM page and write it back to the RAM page. */
1761 int rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, PAGE_SIZE, PGMROMPROT_READ_ROM_WRITE_RAM);
1762 AssertLogRelRC(rc);
1763
1764 rc = PDMDevHlpPhysRead(pDevIns, GCPhys, abPage, PAGE_SIZE);
1765 AssertLogRelRC(rc);
1766 if (RT_FAILURE(rc))
1767 memset(abPage, 0xcc, sizeof(abPage));
1768
1769 rc = PDMDevHlpPhysWrite(pDevIns, GCPhys, abPage, PAGE_SIZE);
1770 AssertLogRelRC(rc);
1771
1772 /* Switch to the RAM/RAM mode. */
1773 rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, PAGE_SIZE, PGMROMPROT_READ_RAM_WRITE_RAM);
1774 AssertLogRelRC(rc);
1775
1776 /* Advance */
1777 GCPhys += PAGE_SIZE;
1778 cPages--;
1779 }
1780}
1781
1782
1783/**
1784 * @interface_method_impl{PDMDEVREG,pfnReset}
1785 */
1786static DECLCALLBACK(void) efiReset(PPDMDEVINS pDevIns)
1787{
1788 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1789
1790 LogFlow(("efiReset\n"));
1791
1792 pThis->iInfoSelector = 0;
1793 pThis->offInfo = -1;
1794
1795 pThis->iMsg = 0;
1796 pThis->szMsg[0] = '\0';
1797 pThis->iPanicMsg = 0;
1798 pThis->szPanicMsg[0] = '\0';
1799
1800#ifdef DEVEFI_WITH_VBOXDBG_SCRIPT
1801 /*
1802 * Zap the debugger script
1803 */
1804 RTFileDelete("./DevEFI.VBoxDbg");
1805#endif
1806}
1807
1808
1809/**
1810 * Destruct a device instance.
1811 *
1812 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1813 * resources can be freed correctly.
1814 *
1815 * @param pDevIns The device instance data.
1816 */
1817static DECLCALLBACK(int) efiDestruct(PPDMDEVINS pDevIns)
1818{
1819 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
1820 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
1821
1822 if (pThis->Lun0.pNvramDrv)
1823 nvramStore(pThis);
1824 nvramFlushDeviceVariableList(pThis);
1825
1826 if (pThis->pu8EfiRom)
1827 {
1828 RTFileReadAllFree(pThis->pu8EfiRom, (size_t)pThis->cbEfiRom);
1829 pThis->pu8EfiRom = NULL;
1830 }
1831
1832 /*
1833 * Free MM heap pointers (waste of time, but whatever).
1834 */
1835 if (pThis->pszEfiRomFile)
1836 {
1837 MMR3HeapFree(pThis->pszEfiRomFile);
1838 pThis->pszEfiRomFile = NULL;
1839 }
1840
1841 if (pThis->pu8EfiThunk)
1842 {
1843 MMR3HeapFree(pThis->pu8EfiThunk);
1844 pThis->pu8EfiThunk = NULL;
1845 }
1846
1847 if (pThis->pbDeviceProps)
1848 {
1849 MMR3HeapFree(pThis->pbDeviceProps);
1850 pThis->pbDeviceProps = NULL;
1851 pThis->cbDeviceProps = 0;
1852 }
1853
1854 return VINF_SUCCESS;
1855}
1856
1857/**
1858 * Helper that searches for a FFS file of a given type.
1859 *
1860 * @returns Pointer to the FFS file header if found, NULL if not.
1861 *
1862 * @param pFfsFile Pointer to the FFS file header to start searching at.
1863 * @param pbEnd The end of the firmware volume.
1864 * @param FileType The file type to look for.
1865 * @param pcbFfsFile Where to store the FFS file size (includes header).
1866 */
1867DECLINLINE(EFI_FFS_FILE_HEADER const *)
1868efiFwVolFindFileByType(EFI_FFS_FILE_HEADER const *pFfsFile, uint8_t const *pbEnd, EFI_FV_FILETYPE FileType, uint32_t *pcbFile)
1869{
1870#define FFS_SIZE(hdr) RT_MAKE_U32_FROM_U8((hdr)->Size[0], (hdr)->Size[1], (hdr)->Size[2], 0)
1871 while ((uintptr_t)pFfsFile < (uintptr_t)pbEnd)
1872 {
1873 if (pFfsFile->Type == FileType)
1874 {
1875 *pcbFile = FFS_SIZE(pFfsFile);
1876 LogFunc(("Found %RTuuid of type:%d\n", &pFfsFile->Name, FileType));
1877 return pFfsFile;
1878 }
1879 pFfsFile = (EFI_FFS_FILE_HEADER *)((uintptr_t)pFfsFile + RT_ALIGN(FFS_SIZE(pFfsFile), 8));
1880 }
1881#undef FFS_SIZE
1882 return NULL;
1883}
1884
1885
1886/**
1887 * Parse EFI ROM headers and find entry points.
1888 *
1889 * @returns VBox status.
1890 * @param pThis The device instance data.
1891 */
1892static int efiParseFirmware(PDEVEFI pThis)
1893{
1894 EFI_FIRMWARE_VOLUME_HEADER const *pFwVolHdr = (EFI_FIRMWARE_VOLUME_HEADER const *)pThis->pu8EfiRom;
1895
1896 /*
1897 * Validate firmware volume header.
1898 */
1899 AssertLogRelMsgReturn(pFwVolHdr->Signature == RT_MAKE_U32_FROM_U8('_', 'F', 'V', 'H'),
1900 ("%#x, expected %#x\n", pFwVolHdr->Signature, RT_MAKE_U32_FROM_U8('_', 'F', 'V', 'H')),
1901 VERR_INVALID_MAGIC);
1902 AssertLogRelMsgReturn(pFwVolHdr->Revision == EFI_FVH_REVISION,
1903 ("%#x, expected %#x\n", pFwVolHdr->Signature, EFI_FVH_REVISION),
1904 VERR_VERSION_MISMATCH);
1905 /** @todo check checksum, see PE spec vol. 3 */
1906 AssertLogRelMsgReturn(pFwVolHdr->FvLength <= pThis->cbEfiRom,
1907 ("%#llx, expected %#llx\n", pFwVolHdr->FvLength, pThis->cbEfiRom),
1908 VERR_INVALID_PARAMETER);
1909 AssertLogRelMsgReturn( pFwVolHdr->BlockMap[0].Length > 0
1910 && pFwVolHdr->BlockMap[0].NumBlocks > 0,
1911 ("%#x, %x\n", pFwVolHdr->BlockMap[0].Length, pFwVolHdr->BlockMap[0].NumBlocks),
1912 VERR_INVALID_PARAMETER);
1913
1914 AssertLogRelMsgReturn(!(pThis->cbEfiRom & PAGE_OFFSET_MASK), ("%RX64\n", pThis->cbEfiRom), VERR_INVALID_PARAMETER);
1915
1916 uint8_t const * const pbFwVolEnd = pThis->pu8EfiRom + pFwVolHdr->FvLength;
1917 pThis->GCLoadAddress = UINT32_C(0xfffff000) - pThis->cbEfiRom + PAGE_SIZE;
1918
1919 return VINF_SUCCESS;
1920}
1921
1922/**
1923 * Load EFI ROM file into the memory.
1924 *
1925 * @returns VBox status.
1926 * @param pThis The device instance data.
1927 * @param pCfg Configuration node handle for the device.
1928 */
1929static int efiLoadRom(PDEVEFI pThis, PCFGMNODE pCfg)
1930{
1931 /*
1932 * Read the entire firmware volume into memory.
1933 */
1934 void *pvFile;
1935 size_t cbFile;
1936 int rc = RTFileReadAllEx(pThis->pszEfiRomFile,
1937 0 /*off*/,
1938 RTFOFF_MAX /*cbMax*/,
1939 RTFILE_RDALL_O_DENY_WRITE,
1940 &pvFile,
1941 &cbFile);
1942 if (RT_FAILURE(rc))
1943 return PDMDevHlpVMSetError(pThis->pDevIns, rc, RT_SRC_POS,
1944 N_("Loading the EFI firmware volume '%s' failed with rc=%Rrc"),
1945 pThis->pszEfiRomFile, rc);
1946 pThis->pu8EfiRom = (uint8_t *)pvFile;
1947 pThis->cbEfiRom = cbFile;
1948
1949 /*
1950 * Validate firmware volume and figure out the load address as well as the SEC entry point.
1951 */
1952 rc = efiParseFirmware(pThis);
1953 if (RT_FAILURE(rc))
1954 return PDMDevHlpVMSetError(pThis->pDevIns, rc, RT_SRC_POS,
1955 N_("Parsing the EFI firmware volume '%s' failed with rc=%Rrc"),
1956 pThis->pszEfiRomFile, rc);
1957
1958 /*
1959 * Map the firmware volume into memory as shadowed ROM.
1960 */
1961 /** @todo fix PGMR3PhysRomRegister so it doesn't mess up in SUPLib when mapping a big ROM image. */
1962 RTGCPHYS cbQuart = RT_ALIGN_64(pThis->cbEfiRom / 4, PAGE_SIZE);
1963 rc = PDMDevHlpROMRegister(pThis->pDevIns,
1964 pThis->GCLoadAddress,
1965 cbQuart,
1966 pThis->pu8EfiRom,
1967 cbQuart,
1968 PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY,
1969 "EFI Firmware Volume");
1970 AssertRCReturn(rc, rc);
1971 rc = PDMDevHlpROMProtectShadow(pThis->pDevIns, pThis->GCLoadAddress, (uint32_t)cbQuart, PGMROMPROT_READ_RAM_WRITE_IGNORE);
1972 AssertRCReturn(rc, rc);
1973 rc = PDMDevHlpROMRegister(pThis->pDevIns,
1974 pThis->GCLoadAddress + cbQuart,
1975 cbQuart,
1976 pThis->pu8EfiRom + cbQuart,
1977 cbQuart,
1978 PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY,
1979 "EFI Firmware Volume (Part 2)");
1980 if (RT_FAILURE(rc))
1981 return rc;
1982 rc = PDMDevHlpROMRegister(pThis->pDevIns,
1983 pThis->GCLoadAddress + cbQuart * 2,
1984 cbQuart,
1985 pThis->pu8EfiRom + cbQuart * 2,
1986 cbQuart,
1987 PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY,
1988 "EFI Firmware Volume (Part 3)");
1989 if (RT_FAILURE(rc))
1990 return rc;
1991 rc = PDMDevHlpROMRegister(pThis->pDevIns,
1992 pThis->GCLoadAddress + cbQuart * 3,
1993 pThis->cbEfiRom - cbQuart * 3,
1994 pThis->pu8EfiRom + cbQuart * 3,
1995 pThis->cbEfiRom - cbQuart * 3,
1996 PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY,
1997 "EFI Firmware Volume (Part 4)");
1998 if (RT_FAILURE(rc))
1999 return rc;
2000 return VINF_SUCCESS;
2001}
2002
2003static uint8_t efiGetHalfByte(char ch)
2004{
2005 uint8_t val;
2006
2007 if (ch >= '0' && ch <= '9')
2008 val = ch - '0';
2009 else if (ch >= 'A' && ch <= 'F')
2010 val = ch - 'A' + 10;
2011 else if(ch >= 'a' && ch <= 'f')
2012 val = ch - 'a' + 10;
2013 else
2014 val = 0xff;
2015
2016 return val;
2017
2018}
2019
2020
2021/**
2022 * Converts a hex string into a binary data blob located at
2023 * pThis->pbDeviceProps, size returned as pThis->cbDeviceProps.
2024 *
2025 * @returns VERR_NO_MEMORY or VINF_SUCCESS.
2026 * @param pThis The EFI instance data.
2027 * @param pszDeviceProps The device property hex string to decode.
2028 */
2029static int efiParseDeviceString(PDEVEFI pThis, const char *pszDeviceProps)
2030{
2031 uint32_t const cbOut = (uint32_t)RTStrNLen(pszDeviceProps, RTSTR_MAX) / 2 + 1;
2032 pThis->pbDeviceProps = (uint8_t *)PDMDevHlpMMHeapAlloc(pThis->pDevIns, cbOut);
2033 if (!pThis->pbDeviceProps)
2034 return VERR_NO_MEMORY;
2035
2036 uint32_t iHex = 0;
2037 bool fUpper = true;
2038 uint8_t u8Value = 0; /* (shut up gcc) */
2039 for (uint32_t iStr = 0; pszDeviceProps[iStr]; iStr++)
2040 {
2041 uint8_t u8Hb = efiGetHalfByte(pszDeviceProps[iStr]);
2042 if (u8Hb > 0xf)
2043 continue;
2044
2045 if (fUpper)
2046 u8Value = u8Hb << 4;
2047 else
2048 pThis->pbDeviceProps[iHex++] = u8Hb | u8Value;
2049
2050 Assert(iHex < cbOut);
2051 fUpper = !fUpper;
2052 }
2053
2054 Assert(iHex == 0 || fUpper);
2055 pThis->cbDeviceProps = iHex;
2056
2057 return VINF_SUCCESS;
2058}
2059
2060
2061/**
2062 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2063 */
2064static DECLCALLBACK(int) efiConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2065{
2066 PDEVEFI pThis = PDMINS_2_DATA(pDevIns, PDEVEFI);
2067 int rc;
2068 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2069
2070 Assert(iInstance == 0);
2071
2072 /*
2073 * Initalize the basic variables so that the destructor always works.
2074 */
2075 pThis->pDevIns = pDevIns;
2076 RTListInit(&pThis->NVRAM.VarList);
2077 pThis->Lun0.IBase.pfnQueryInterface = devEfiQueryInterface;
2078
2079
2080 /*
2081 * Validate and read the configuration.
2082 */
2083 if (!CFGMR3AreValuesValid(pCfg,
2084 "EfiRom\0"
2085 "RamSize\0"
2086 "RamHoleSize\0"
2087 "NumCPUs\0"
2088 "UUID\0"
2089 "IOAPIC\0"
2090 "DmiBIOSFirmwareMajor\0"
2091 "DmiBIOSFirmwareMinor\0"
2092 "DmiBIOSReleaseDate\0"
2093 "DmiBIOSReleaseMajor\0"
2094 "DmiBIOSReleaseMinor\0"
2095 "DmiBIOSVendor\0"
2096 "DmiBIOSVersion\0"
2097 "DmiSystemFamily\0"
2098 "DmiSystemProduct\0"
2099 "DmiSystemSerial\0"
2100 "DmiSystemSKU\0"
2101 "DmiSystemUuid\0"
2102 "DmiSystemVendor\0"
2103 "DmiSystemVersion\0"
2104 "DmiBoardAssetTag\0"
2105 "DmiBoardBoardType\0"
2106 "DmiBoardLocInChass\0"
2107 "DmiBoardProduct\0"
2108 "DmiBoardSerial\0"
2109 "DmiBoardVendor\0"
2110 "DmiBoardVersion\0"
2111 "DmiChassisAssetTag\0"
2112 "DmiChassisSerial\0"
2113 "DmiChassisType\0"
2114 "DmiChassisVendor\0"
2115 "DmiChassisVersion\0"
2116 "DmiProcManufacturer\0"
2117 "DmiProcVersion\0"
2118 "DmiOEMVBoxVer\0"
2119 "DmiOEMVBoxRev\0"
2120 "DmiUseHostInfo\0"
2121 "DmiExposeMemoryTable\0"
2122 "DmiExposeProcInf\0"
2123 "64BitEntry\0"
2124 "BootArgs\0"
2125 "DeviceProps\0"
2126 "GopMode\0"
2127 "UgaHorizontalResolution\0"
2128 "UgaVerticalResolution\0"))
2129 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
2130 N_("Configuration error: Invalid config value(s) for the EFI device"));
2131
2132 /* CPU count (optional). */
2133 rc = CFGMR3QueryU32Def(pCfg, "NumCPUs", &pThis->cCpus, 1);
2134 AssertLogRelRCReturn(rc, rc);
2135
2136 rc = CFGMR3QueryU8Def(pCfg, "IOAPIC", &pThis->u8IOAPIC, 1);
2137 if (RT_FAILURE (rc))
2138 return PDMDEV_SET_ERROR(pDevIns, rc,
2139 N_("Configuration error: Failed to read \"IOAPIC\""));
2140
2141 /*
2142 * Query the machine's UUID for SMBIOS/DMI use.
2143 */
2144 RTUUID uuid;
2145 rc = CFGMR3QueryBytes(pCfg, "UUID", &uuid, sizeof(uuid));
2146 if (RT_FAILURE(rc))
2147 return PDMDEV_SET_ERROR(pDevIns, rc,
2148 N_("Configuration error: Querying \"UUID\" failed"));
2149
2150 /*
2151 * Convert the UUID to network byte order. Not entirely straightforward as
2152 * parts are MSB already...
2153 */
2154 uuid.Gen.u32TimeLow = RT_H2BE_U32(uuid.Gen.u32TimeLow);
2155 uuid.Gen.u16TimeMid = RT_H2BE_U16(uuid.Gen.u16TimeMid);
2156 uuid.Gen.u16TimeHiAndVersion = RT_H2BE_U16(uuid.Gen.u16TimeHiAndVersion);
2157 memcpy(&pThis->aUuid, &uuid, sizeof pThis->aUuid);
2158
2159 /*
2160 * RAM sizes
2161 */
2162 rc = CFGMR3QueryU64(pCfg, "RamSize", &pThis->cbRam);
2163 AssertLogRelRCReturn(rc, rc);
2164 rc = CFGMR3QueryU64(pCfg, "RamHoleSize", &pThis->cbRamHole);
2165 AssertLogRelRCReturn(rc, rc);
2166 pThis->cbBelow4GB = RT_MIN(pThis->cbRam, _4G - pThis->cbRamHole);
2167 pThis->cbAbove4GB = pThis->cbRam - pThis->cbBelow4GB;
2168
2169 /*
2170 * Get the system EFI ROM file name.
2171 */
2172 rc = CFGMR3QueryStringAlloc(pCfg, "EfiRom", &pThis->pszEfiRomFile);
2173 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2174 {
2175 pThis->pszEfiRomFile = (char *)PDMDevHlpMMHeapAlloc(pDevIns, RTPATH_MAX);
2176 if (!pThis->pszEfiRomFile)
2177 return VERR_NO_MEMORY;
2178
2179 rc = RTPathAppPrivateArchTop(pThis->pszEfiRomFile, RTPATH_MAX);
2180 AssertRCReturn(rc, rc);
2181 rc = RTPathAppend(pThis->pszEfiRomFile, RTPATH_MAX, "VBoxEFI32.fd");
2182 AssertRCReturn(rc, rc);
2183 }
2184 else if (RT_FAILURE(rc))
2185 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2186 N_("Configuration error: Querying \"EfiRom\" as a string failed"));
2187 else if (!*pThis->pszEfiRomFile)
2188 {
2189 MMR3HeapFree(pThis->pszEfiRomFile);
2190 pThis->pszEfiRomFile = NULL;
2191 }
2192
2193 /*
2194 * NVRAM processing.
2195 */
2196 rc = PDMDevHlpSSMRegister(pDevIns, EFI_SSM_VERSION, sizeof(*pThis), efiSaveExec, efiLoadExec);
2197 AssertRCReturn(rc, rc);
2198
2199 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "NvramStorage");
2200 if (RT_FAILURE(rc))
2201 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, N_("Can't attach Nvram Storage driver"));
2202
2203 pThis->Lun0.pNvramDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMINVRAMCONNECTOR);
2204 AssertPtrReturn(pThis->Lun0.pNvramDrv, VERR_PDM_MISSING_INTERFACE_BELOW);
2205
2206 rc = nvramLoad(pThis);
2207 AssertRCReturn(rc, rc);
2208
2209 /*
2210 * Get boot args.
2211 */
2212 rc = CFGMR3QueryStringDef(pCfg, "BootArgs", pThis->szBootArgs, sizeof(pThis->szBootArgs), "");
2213 if (RT_FAILURE(rc))
2214 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2215 N_("Configuration error: Querying \"BootArgs\" as a string failed"));
2216
2217 //strcpy(pThis->szBootArgs, "-v keepsyms=1 io=0xf debug=0x2a");
2218 //strcpy(pThis->szBootArgs, "-v keepsyms=1 debug=0x2a");
2219 LogRel(("EFI: boot args = %s\n", pThis->szBootArgs));
2220
2221 /*
2222 * Get device props.
2223 */
2224 char *pszDeviceProps;
2225 rc = CFGMR3QueryStringAllocDef(pCfg, "DeviceProps", &pszDeviceProps, NULL);
2226 if (RT_FAILURE(rc))
2227 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2228 N_("Configuration error: Querying \"DeviceProps\" as a string failed"));
2229 if (pszDeviceProps)
2230 {
2231 LogRel(("EFI: device props = %s\n", pszDeviceProps));
2232 rc = efiParseDeviceString(pThis, pszDeviceProps);
2233 MMR3HeapFree(pszDeviceProps);
2234 if (RT_FAILURE(rc))
2235 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2236 N_("Configuration error: Cannot parse device properties"));
2237 }
2238 else
2239 {
2240 pThis->pbDeviceProps = NULL;
2241 pThis->cbDeviceProps = 0;
2242 }
2243
2244 /*
2245 * CPU frequencies.
2246 */
2247 pThis->u64TscFrequency = TMCpuTicksPerSecond(PDMDevHlpGetVM(pDevIns));
2248 pThis->u64CpuFrequency = pThis->u64TscFrequency;
2249 pThis->u64FsbFrequency = CPUMGetGuestScalableBusFrequency(PDMDevHlpGetVM(pDevIns));
2250
2251 /*
2252 * GOP graphics.
2253 */
2254 rc = CFGMR3QueryU32Def(pCfg, "GopMode", &pThis->u32GopMode, 2 /* 1024x768 */);
2255 if (RT_FAILURE(rc))
2256 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2257 N_("Configuration error: Querying \"GopMode\" as a 32-bit int failed"));
2258 if (pThis->u32GopMode == UINT32_MAX)
2259 pThis->u32GopMode = 2; /* 1024x768 */
2260
2261 /*
2262 * Uga graphics, default to 1024x768.
2263 */
2264 rc = CFGMR3QueryU32Def(pCfg, "UgaHorizontalResolution", &pThis->cxUgaResolution, 0);
2265 AssertRCReturn(rc, rc);
2266 rc = CFGMR3QueryU32Def(pCfg, "UgaVerticalResolution", &pThis->cyUgaResolution, 0);
2267 AssertRCReturn(rc, rc);
2268 if (pThis->cxUgaResolution == 0 || pThis->cyUgaResolution == 0)
2269 {
2270 pThis->cxUgaResolution = 1024;
2271 pThis->cyUgaResolution = 768;
2272 }
2273
2274 /*
2275 * Load firmware volume and thunk ROM.
2276 */
2277 rc = efiLoadRom(pThis, pCfg);
2278 if (RT_FAILURE(rc))
2279 return rc;
2280
2281 /*
2282 * Register our I/O ports.
2283 */
2284 rc = PDMDevHlpIOPortRegister(pDevIns, EFI_PORT_BASE, EFI_PORT_COUNT, NULL,
2285 efiIOPortWrite, efiIOPortRead,
2286 NULL, NULL, "EFI communication ports");
2287 if (RT_FAILURE(rc))
2288 return rc;
2289
2290 /*
2291 * Plant DMI and MPS tables.
2292 */
2293 /** @todo XXX I wonder if we really need these tables as there is no SMBIOS header... */
2294 rc = FwCommonPlantDMITable(pDevIns, pThis->au8DMIPage, VBOX_DMI_TABLE_SIZE, &pThis->aUuid,
2295 pDevIns->pCfg, pThis->cCpus, &pThis->cbDmiTables, &pThis->cNumDmiTables);
2296 AssertRCReturn(rc, rc);
2297 if (pThis->u8IOAPIC)
2298 FwCommonPlantMpsTable(pDevIns,
2299 pThis->au8DMIPage + VBOX_DMI_TABLE_SIZE,
2300 _4K - VBOX_DMI_TABLE_SIZE, pThis->cCpus);
2301 rc = PDMDevHlpROMRegister(pDevIns, VBOX_DMI_TABLE_BASE, _4K, pThis->au8DMIPage, _4K,
2302 PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "DMI tables");
2303
2304 AssertRCReturn(rc, rc);
2305
2306 /*
2307 * Register info handlers.
2308 */
2309 rc = PDMDevHlpDBGFInfoRegister(pDevIns, "nvram", "Dumps the NVRAM variables.\n", efiInfoNvram);
2310 AssertRCReturn(rc, rc);
2311
2312 /*
2313 * Call reset to set things up.
2314 */
2315 efiReset(pDevIns);
2316
2317 return VINF_SUCCESS;
2318}
2319
2320/**
2321 * The device registration structure.
2322 */
2323const PDMDEVREG g_DeviceEFI =
2324{
2325 /* u32Version */
2326 PDM_DEVREG_VERSION,
2327 /* szName */
2328 "efi",
2329 /* szRCMod */
2330 "",
2331 /* szR0Mod */
2332 "",
2333 /* pszDescription */
2334 "Extensible Firmware Interface Device. "
2335 "LUN#0 - NVRAM port",
2336 /* fFlags */
2337 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64,
2338 /* fClass */
2339 PDM_DEVREG_CLASS_ARCH_BIOS,
2340 /* cMaxInstances */
2341 1,
2342 /* cbInstance */
2343 sizeof(DEVEFI),
2344 /* pfnConstruct */
2345 efiConstruct,
2346 /* pfnDestruct */
2347 efiDestruct,
2348 /* pfnRelocate */
2349 NULL,
2350 /* pfnMemSetup */
2351 efiMemSetup,
2352 /* pfnPowerOn */
2353 NULL,
2354 /* pfnReset */
2355 efiReset,
2356 /* pfnSuspend */
2357 NULL,
2358 /* pfnResume */
2359 NULL,
2360 /* pfnAttach */
2361 NULL,
2362 /* pfnDetach */
2363 NULL,
2364 /* pfnQueryInterface. */
2365 NULL,
2366 /* pfnInitComplete. */
2367 efiInitComplete,
2368 /* pfnPowerOff */
2369 NULL,
2370 /* pfnSoftReset */
2371 NULL,
2372 /* u32VersionEnd */
2373 PDM_DEVREG_VERSION
2374};
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