VirtualBox

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

Last change on this file since 66058 was 65850, checked in by vboxsync, 8 years ago

Devices/Main: don't set RamSize and RamHole explicitly for DevACPI, DevEFI, DevPcBios and VMMDev but use the MMR3Phys* API for that. This simplifies and unifies the calculation of RAM below 4GB and RAM above 4GB. Also renamed PciPref64Limit to PciPref64LimitGB to show that the limit is in GB.

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