/
DevEFI.cpp
2509 lines (2242 loc) · 88.9 KB
/
DevEFI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$ */
/** @file
* DevEFI - EFI <-> VirtualBox Integration Framework.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_EFI
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pgm.h>
#include <VBox/vmm/cpum.h>
#include <VBox/vmm/mm.h>
#include <VBox/log.h>
#include <VBox/err.h>
#include <VBox/param.h>
#include <VBox/vmm/dbgf.h>
#include <VBox/vmm/pdmnvram.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/uuid.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/mp.h>
#include <iprt/list.h>
#if defined(DEBUG) && defined(IN_RING3)
# include <iprt/stream.h>
# define DEVEFI_WITH_VBOXDBG_SCRIPT
#endif
#include <iprt/utf16.h>
#include "DevEFI.h"
#include "VBoxDD.h"
#include "VBoxDD2.h"
#include "../PC/DevFwCommon.h"
/* EFI includes */
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4668)
#endif
#include <ProcessorBind.h>
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#include <Common/UefiBaseTypes.h>
#include <Common/PiFirmwareVolume.h>
#include <Common/PiFirmwareFile.h>
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* EFI NVRAM variable.
*/
typedef struct EFIVAR
{
/** The list node for the variable. */
RTLISTNODE ListNode;
/** The unique sequence number of the variable.
* This is used to find pCurVar when restoring saved state and therefore only
* set when saving. */
uint32_t idUniqueSavedState;
/** The value attributess. */
uint32_t fAttributes;
/** The variable name length (not counting the terminator char). */
uint32_t cchName;
/** The size of the value. This cannot be zero. */
uint32_t cbValue;
/** The vendor UUID scoping the variable name. */
RTUUID uuid;
/** The variable name. */
char szName[EFI_VARIABLE_NAME_MAX];
/** The variable value bytes. */
uint8_t abValue[EFI_VARIABLE_VALUE_MAX];
} EFIVAR;
/** Pointer to an EFI NVRAM variable. */
typedef EFIVAR *PEFIVAR;
/** Pointer to a const EFI NVRAM variable. */
typedef EFIVAR const *PCEFIVAR;
/** Pointer to an EFI NVRAM variable pointer. */
typedef PEFIVAR *PPEFIVAR;
/**
* NVRAM state.
*/
typedef struct NVRAMDESC
{
/** The current operation. */
EFIVAROP enmOp;
/** The current status. */
uint32_t u32Status;
/** The current */
uint32_t offOpBuffer;
/** The current number of variables. */
uint32_t cVariables;
/** The list of variables. */
RTLISTANCHOR VarList;
/** The unique variable sequence ID, for the saved state only.
* @todo It's part of this structure for hysterical raisins, consider remove it
* when changing the saved state format the next time. */
uint32_t idUniqueCurVar;
/** Variable buffered used both when adding and querying NVRAM variables.
* When querying a variable, a copy of it is stored in this buffer and read
* from it. When adding, updating or deleting a variable, this buffer is used
* to set up the parameters before taking action. */
EFIVAR VarOpBuf;
/** The current variable. This is only used by EFI_VARIABLE_OP_QUERY_NEXT,
* the attribute readers work against the copy in VarOpBuf. */
PEFIVAR pCurVar;
} NVRAMDESC;
/**
* The EFI device state structure.
*/
typedef struct DEVEFI
{
/** Pointer back to the device instance. */
PPDMDEVINS pDevIns;
/** EFI message buffer. */
char szMsg[VBOX_EFI_DEBUG_BUFFER];
/** EFI message buffer index. */
uint32_t iMsg;
/** EFI panic message buffer. */
char szPanicMsg[2048];
/** EFI panic message buffer index. */
uint32_t iPanicMsg;
struct
{
/** The current/last image event. */
uint8_t uEvt;
/** Module path/name offset. */
uint8_t offName;
/** The offset of the last component in the module path/name. */
uint8_t offNameLastComponent;
/** Alignment padding. */
uint8_t abPadding[5];
/** First address associated with the event (image address). */
uint64_t uAddr0;
/** Second address associated with the event (old image address). */
uint64_t uAddr1;
/** The size associated with the event (0 if none). */
uint64_t cb0;
/** The module name. */
char szName[256];
} ImageEvt;
/** The system EFI ROM data. */
uint8_t *pu8EfiRom;
/** The size of the system EFI ROM. */
uint64_t cbEfiRom;
/** Offset into the actual ROM within EFI FW volume. */
uint64_t uEfiRomOfs;
/** The name of the EFI ROM file. */
char *pszEfiRomFile;
/** Thunk page pointer. */
uint8_t *pu8EfiThunk;
/** First entry point of the EFI firmware. */
RTGCPHYS GCEntryPoint0;
/** Second Entry Point (PeiCore)*/
RTGCPHYS GCEntryPoint1;
/** EFI firmware physical load address. */
RTGCPHYS GCLoadAddress;
/** Current info selector. */
uint32_t iInfoSelector;
/** Current info position. */
int32_t offInfo;
/** Number of virtual CPUs. (Config) */
uint32_t cCpus;
/** The size of the DMI tables. */
uint16_t cbDmiTables;
/** Number of the DMI tables. */
uint16_t cNumDmiTables;
/** The DMI tables. */
uint8_t au8DMIPage[0x1000];
/** Should NVRAM range be reserved for flash? */
bool fSkipNvramRange;
/** I/O-APIC enabled? */
uint8_t u8IOAPIC;
/** APIC mode to be set up by firmware. */
uint8_t u8APIC;
/** Boot parameters passed to the firmware. */
char szBootArgs[256];
/** Host UUID (for DMI). */
RTUUID aUuid;
/** Device properties buffer. */
R3PTRTYPE(uint8_t *) pbDeviceProps;
/** Device properties buffer size. */
uint32_t cbDeviceProps;
/** Virtual machine front side bus frequency. */
uint64_t u64FsbFrequency;
/** Virtual machine time stamp counter frequency. */
uint64_t u64TscFrequency;
/** Virtual machine CPU frequency. */
uint64_t u64CpuFrequency;
/** EFI Graphics mode (used as fallback if resolution is not known). */
uint32_t u32GraphicsMode;
/** EFI Graphics (GOP or UGA) horizontal resolution. */
uint32_t u32HorizontalResolution;
/** EFI Graphics (GOP or UGA) vertical resolution. */
uint32_t u32VerticalResolution;
/** Physical address of PCI config space MMIO region */
uint64_t u64McfgBase;
/** Length of PCI config space MMIO region */
uint64_t cbMcfgLength;
/** NVRAM state variables. */
NVRAMDESC NVRAM;
/**
* NVRAM port - LUN\#0.
*/
struct
{
/** The base interface we provide the NVRAM driver. */
PDMIBASE IBase;
/** The NVRAM driver base interface. */
PPDMIBASE pDrvBase;
/** The NVRAM interface provided by the driver. */
PPDMINVRAMCONNECTOR pNvramDrv;
} Lun0;
} DEVEFI;
typedef DEVEFI *PDEVEFI;
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** The saved state version. */
#define EFI_SSM_VERSION 2
/** The saved state version from VBox 4.2. */
#define EFI_SSM_VERSION_4_2 1
/** Non-volatile EFI variable. */
#define VBOX_EFI_VARIABLE_NON_VOLATILE UINT32_C(0x00000001)
/** Non-volatile EFI variable. */
#define VBOX_EFI_VARIABLE_READ_ONLY UINT32_C(0x00000008)
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Saved state NVRAMDESC field descriptors. */
static SSMFIELD const g_aEfiNvramDescField[] =
{
SSMFIELD_ENTRY( NVRAMDESC, enmOp),
SSMFIELD_ENTRY( NVRAMDESC, u32Status),
SSMFIELD_ENTRY( NVRAMDESC, offOpBuffer),
SSMFIELD_ENTRY_IGNORE(NVRAMDESC, VarOpBuf),
SSMFIELD_ENTRY( NVRAMDESC, cVariables),
SSMFIELD_ENTRY_OLD( idUnquireLast, 4),
SSMFIELD_ENTRY_IGNORE(NVRAMDESC, VarList),
SSMFIELD_ENTRY( NVRAMDESC, idUniqueCurVar),
SSMFIELD_ENTRY_IGNORE(NVRAMDESC, pCurVar),
SSMFIELD_ENTRY_TERM()
};
/** Saved state EFIVAR field descriptors. */
static SSMFIELD const g_aEfiVariableDescFields[] =
{
SSMFIELD_ENTRY_IGNORE(EFIVAR, ListNode),
SSMFIELD_ENTRY( EFIVAR, idUniqueSavedState),
SSMFIELD_ENTRY( EFIVAR, uuid),
SSMFIELD_ENTRY( EFIVAR, szName),
SSMFIELD_ENTRY_OLD( cchName, 4),
SSMFIELD_ENTRY( EFIVAR, abValue),
SSMFIELD_ENTRY( EFIVAR, cbValue),
SSMFIELD_ENTRY( EFIVAR, fAttributes),
SSMFIELD_ENTRY_TERM()
};
/**
* Flushes the variable list.
*
* @param pThis The EFI state.
*/
static void nvramFlushDeviceVariableList(PDEVEFI pThis)
{
while (!RTListIsEmpty(&pThis->NVRAM.VarList))
{
PEFIVAR pEfiVar = RTListNodeGetNext(&pThis->NVRAM.VarList, EFIVAR, ListNode);
RTListNodeRemove(&pEfiVar->ListNode);
RTMemFree(pEfiVar);
}
pThis->NVRAM.pCurVar = NULL;
}
/**
* This function looks up variable in NVRAM list.
*/
static int nvramLookupVariableByUuidAndName(PDEVEFI pThis, char *pszVariableName, PCRTUUID pUuid, PPEFIVAR ppEfiVar)
{
LogFlowFunc(("%RTuuid::'%s'\n", pUuid, pszVariableName));
size_t const cchVariableName = strlen(pszVariableName);
int rc = VERR_NOT_FOUND;
/*
* Start by checking the last variable queried.
*/
if ( pThis->NVRAM.pCurVar
&& pThis->NVRAM.pCurVar->cchName == cchVariableName
&& memcmp(pThis->NVRAM.pCurVar->szName, pszVariableName, cchVariableName + 1) == 0
&& RTUuidCompare(&pThis->NVRAM.pCurVar->uuid, pUuid) == 0
)
{
*ppEfiVar = pThis->NVRAM.pCurVar;
rc = VINF_SUCCESS;
}
else
{
/*
* Linear list search.
*/
PEFIVAR pEfiVar;
RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
{
Assert(strlen(pEfiVar->szName) == pEfiVar->cchName);
if ( pEfiVar->cchName == cchVariableName
&& memcmp(pEfiVar->szName, pszVariableName, cchVariableName + 1) == 0
&& RTUuidCompare(&pEfiVar->uuid, pUuid) == 0)
{
*ppEfiVar = pEfiVar;
rc = VINF_SUCCESS;
break;
}
}
}
LogFlowFunc(("rc=%Rrc pEfiVar=%p\n", rc, *ppEfiVar));
return rc;
}
/**
* Inserts the EFI variable into the list.
*
* This enforces the desired list ordering and/or insertion policy.
*
* @param pThis The EFI state.
* @param pEfiVar The variable to insert.
*/
static void nvramInsertVariable(PDEVEFI pThis, PEFIVAR pEfiVar)
{
#if 1
/*
* Sorted by UUID and name.
*/
PEFIVAR pCurVar;
RTListForEach(&pThis->NVRAM.VarList, pCurVar, EFIVAR, ListNode)
{
int iDiff = RTUuidCompare(&pEfiVar->uuid, &pCurVar->uuid);
if (!iDiff)
iDiff = strcmp(pEfiVar->szName, pCurVar->szName);
if (iDiff < 0)
{
RTListNodeInsertBefore(&pCurVar->ListNode, &pEfiVar->ListNode);
return;
}
}
#endif
/*
* Add it at the end.
*/
RTListAppend(&pThis->NVRAM.VarList, &pEfiVar->ListNode);
}
/**
* Creates an device internal list of variables.
*
* @returns VBox status code.
* @param pThis The EFI state.
*/
static int nvramLoad(PDEVEFI pThis)
{
int rc;
for (uint32_t iVar = 0; iVar < EFI_VARIABLE_MAX; iVar++)
{
PEFIVAR pEfiVar = (PEFIVAR)RTMemAllocZ(sizeof(EFIVAR));
AssertReturn(pEfiVar, VERR_NO_MEMORY);
pEfiVar->cchName = sizeof(pEfiVar->szName);
pEfiVar->cbValue = sizeof(pEfiVar->abValue);
rc = pThis->Lun0.pNvramDrv->pfnVarQueryByIndex(pThis->Lun0.pNvramDrv, iVar,
&pEfiVar->uuid, &pEfiVar->szName[0], &pEfiVar->cchName,
&pEfiVar->fAttributes, &pEfiVar->abValue[0], &pEfiVar->cbValue);
if (RT_SUCCESS(rc))
{
/* Some validations. */
rc = RTStrValidateEncoding(pEfiVar->szName);
size_t cchName = RTStrNLen(pEfiVar->szName, sizeof(pEfiVar->szName));
if (cchName != pEfiVar->cchName)
rc = VERR_INVALID_PARAMETER;
if (pEfiVar->cbValue == 0)
rc = VERR_NO_DATA;
if (RT_FAILURE(rc))
LogRel(("EFI/nvramLoad: Bad variable #%u: cbValue=%#x cchName=%#x (strlen=%#x) szName=%.*Rhxs\n",
iVar, pEfiVar->cbValue, pEfiVar->cchName, cchName, pEfiVar->cchName + 1, pEfiVar->szName));
}
if (RT_FAILURE(rc))
{
RTMemFree(pEfiVar);
if (rc == VERR_NOT_FOUND)
rc = VINF_SUCCESS;
AssertRC(rc);
return rc;
}
/* Append it. */
nvramInsertVariable(pThis, pEfiVar);
pThis->NVRAM.cVariables++;
}
AssertLogRelMsgFailed(("EFI: Too many variables.\n"));
return VERR_TOO_MUCH_DATA;
}
/**
* Let the NVRAM driver store the internal NVRAM variable list.
*
* @returns VBox status code.
* @param pThis The EFI state.
*/
static int nvramStore(PDEVEFI pThis)
{
/*
* Count the non-volatile variables and issue the begin call.
*/
PEFIVAR pEfiVar;
uint32_t cNonVolatile = 0;
RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
if (pEfiVar->fAttributes & VBOX_EFI_VARIABLE_NON_VOLATILE)
cNonVolatile++;
int rc = pThis->Lun0.pNvramDrv->pfnVarStoreSeqBegin(pThis->Lun0.pNvramDrv, cNonVolatile);
if (RT_SUCCESS(rc))
{
/*
* Store each non-volatile variable.
*/
uint32_t idxVar = 0;
RTListForEach(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode)
{
/* Skip volatile variables. */
if (!(pEfiVar->fAttributes & VBOX_EFI_VARIABLE_NON_VOLATILE))
continue;
int rc2 = pThis->Lun0.pNvramDrv->pfnVarStoreSeqPut(pThis->Lun0.pNvramDrv, idxVar,
&pEfiVar->uuid, pEfiVar->szName, pEfiVar->cchName,
pEfiVar->fAttributes, pEfiVar->abValue, pEfiVar->cbValue);
if (RT_FAILURE(rc2) && RT_SUCCESS_NP(rc))
{
LogRel(("EFI: pfnVarStoreVarByIndex failed: %Rrc\n", rc));
rc = rc2;
}
idxVar++;
}
Assert(idxVar == cNonVolatile);
/*
* Done.
*/
rc = pThis->Lun0.pNvramDrv->pfnVarStoreSeqEnd(pThis->Lun0.pNvramDrv, rc);
}
else
LogRel(("EFI: pfnVarStoreBegin failed: %Rrc\n", rc));
return rc;
}
/**
* EFI_VARIABLE_OP_QUERY and EFI_VARIABLE_OP_QUERY_NEXT worker that copies the
* variable into the VarOpBuf, set pCurVar and u32Status.
*
* @param pThis The EFI state.
* @param pEfiVar The resulting variable. NULL if not found / end.
* @param fEnumQuery Set if enumeration query, clear if specific.
*/
static void nvramWriteVariableOpQueryCopyResult(PDEVEFI pThis, PEFIVAR pEfiVar, bool fEnumQuery)
{
RT_ZERO(pThis->NVRAM.VarOpBuf.abValue);
if (pEfiVar)
{
RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
pThis->NVRAM.VarOpBuf.uuid = pEfiVar->uuid;
pThis->NVRAM.VarOpBuf.cchName = pEfiVar->cchName;
memcpy(pThis->NVRAM.VarOpBuf.szName, pEfiVar->szName, pEfiVar->cchName); /* no need for + 1. */
pThis->NVRAM.VarOpBuf.fAttributes = pEfiVar->fAttributes;
pThis->NVRAM.VarOpBuf.cbValue = pEfiVar->cbValue;
memcpy(pThis->NVRAM.VarOpBuf.abValue, pEfiVar->abValue, pEfiVar->cbValue);
pThis->NVRAM.pCurVar = pEfiVar;
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
LogFlow(("EFI: Variable query -> %RTuuid::'%s' (%d) abValue=%.*Rhxs\n", &pThis->NVRAM.VarOpBuf.uuid,
pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.cchName,
pThis->NVRAM.VarOpBuf.cbValue, pThis->NVRAM.VarOpBuf.abValue));
}
else
{
if (fEnumQuery)
LogFlow(("EFI: Variable query -> NOT_FOUND \n"));
else
LogFlow(("EFI: Variable query %RTuuid::'%s' -> NOT_FOUND \n",
&pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
pThis->NVRAM.VarOpBuf.fAttributes = 0;
pThis->NVRAM.VarOpBuf.cbValue = 0;
pThis->NVRAM.VarOpBuf.cchName = 0;
pThis->NVRAM.pCurVar = NULL;
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_NOT_FOUND;
}
}
/**
* Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_QUERY.
*
* @returns IOM strict status code.
* @param pThis The EFI state.
*/
static int nvramWriteVariableOpQuery(PDEVEFI pThis)
{
Log(("EFI_VARIABLE_OP_QUERY: %RTuuid::'%s'\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
PEFIVAR pEfiVar;
int rc = nvramLookupVariableByUuidAndName(pThis,
pThis->NVRAM.VarOpBuf.szName,
&pThis->NVRAM.VarOpBuf.uuid,
&pEfiVar);
nvramWriteVariableOpQueryCopyResult(pThis, RT_SUCCESS(rc) ? pEfiVar : NULL, false /*fEnumQuery*/);
return VINF_SUCCESS;
}
/**
* Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_QUERY_NEXT.
*
* This simply walks the list.
*
* @returns IOM strict status code.
* @param pThis The EFI state.
*/
static int nvramWriteVariableOpQueryNext(PDEVEFI pThis)
{
Log(("EFI_VARIABLE_OP_QUERY_NEXT: pCurVar=%p\n", pThis->NVRAM.pCurVar));
PEFIVAR pEfiVar = pThis->NVRAM.pCurVar;
if (pEfiVar)
pEfiVar = RTListGetNext(&pThis->NVRAM.VarList, pEfiVar, EFIVAR, ListNode);
else
pEfiVar = RTListGetFirst(&pThis->NVRAM.VarList, EFIVAR, ListNode);
nvramWriteVariableOpQueryCopyResult(pThis, pEfiVar, true /* fEnumQuery */);
return VINF_SUCCESS;
}
/**
* Implements EFI_VARIABLE_PARAM + EFI_VARIABLE_OP_ADD.
*
* @returns IOM strict status code.
* @param pThis The EFI state.
*/
static int nvramWriteVariableOpAdd(PDEVEFI pThis)
{
Log(("EFI_VARIABLE_OP_ADD: %RTuuid::'%s' fAttributes=%#x abValue=%.*Rhxs\n",
&pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes,
pThis->NVRAM.VarOpBuf.cbValue, pThis->NVRAM.VarOpBuf.abValue));
/*
* Validate and adjust the input a little before we start.
*/
int rc = RTStrValidateEncoding(pThis->NVRAM.VarOpBuf.szName);
if (RT_FAILURE(rc))
LogRel(("EFI: Badly encoded variable name: %.*Rhxs\n", pThis->NVRAM.VarOpBuf.cchName + 1, pThis->NVRAM.VarOpBuf.szName));
if (RT_FAILURE(rc))
{
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
return VINF_SUCCESS;
}
pThis->NVRAM.VarOpBuf.cchName = (uint32_t)RTStrNLen(pThis->NVRAM.VarOpBuf.szName, sizeof(pThis->NVRAM.VarOpBuf.szName));
/*
* Look it up and see what to do.
*/
PEFIVAR pEfiVar;
rc = nvramLookupVariableByUuidAndName(pThis,
pThis->NVRAM.VarOpBuf.szName,
&pThis->NVRAM.VarOpBuf.uuid,
&pEfiVar);
if (RT_SUCCESS(rc))
{
LogFlowFunc(("Old abValue=%.*Rhxs\n", pEfiVar->cbValue, pEfiVar->abValue));
#if 0 /** @todo Implement read-only EFI variables. */
if (pEfiVar->fAttributes & EFI_VARIABLE_XXXXXXX)
{
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_RO;
break;
}
#endif
if (pThis->NVRAM.VarOpBuf.cbValue == 0)
{
/*
* Delete it.
*/
LogRel(("EFI: Deleting variable %RTuuid::'%s'\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName));
RTListNodeRemove(&pEfiVar->ListNode);
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
pThis->NVRAM.cVariables--;
if (pThis->NVRAM.pCurVar == pEfiVar)
pThis->NVRAM.pCurVar = NULL;
RTMemFree(pEfiVar);
pEfiVar = NULL;
}
else
{
/*
* Update/replace it. (The name and UUID are unchanged, of course.)
*/
LogRel(("EFI: Replacing variable %RTuuid::'%s' fAttrib=%#x cbValue=%#x\n", &pThis->NVRAM.VarOpBuf.uuid,
pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
pEfiVar->fAttributes = pThis->NVRAM.VarOpBuf.fAttributes;
pEfiVar->cbValue = pThis->NVRAM.VarOpBuf.cbValue;
memcpy(pEfiVar->abValue, pThis->NVRAM.VarOpBuf.abValue, pEfiVar->cbValue);
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
}
}
else if (pThis->NVRAM.VarOpBuf.cbValue == 0)
{
/* delete operation, but nothing to delete. */
LogFlow(("nvramWriteVariableOpAdd: Delete (not found)\n"));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
}
else if (pThis->NVRAM.cVariables < EFI_VARIABLE_MAX)
{
/*
* Add a new variable.
*/
LogRel(("EFI: Adding variable %RTuuid::'%s' fAttrib=%#x cbValue=%#x\n", &pThis->NVRAM.VarOpBuf.uuid,
pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
pEfiVar = (PEFIVAR)RTMemAllocZ(sizeof(EFIVAR));
if (pEfiVar)
{
pEfiVar->uuid = pThis->NVRAM.VarOpBuf.uuid;
pEfiVar->cchName = pThis->NVRAM.VarOpBuf.cchName;
memcpy(pEfiVar->szName, pThis->NVRAM.VarOpBuf.szName, pEfiVar->cchName); /* The buffer is zeroed, so skip '\0'. */
pEfiVar->fAttributes = pThis->NVRAM.VarOpBuf.fAttributes;
pEfiVar->cbValue = pThis->NVRAM.VarOpBuf.cbValue;
memcpy(pEfiVar->abValue, pThis->NVRAM.VarOpBuf.abValue, pEfiVar->cbValue);
nvramInsertVariable(pThis, pEfiVar);
pThis->NVRAM.cVariables++;
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
}
else
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
else
{
/*
* Too many variables.
*/
LogRelMax(5, ("EFI: Too many variables (%RTuuid::'%s' fAttrib=%#x cbValue=%#x)\n", &pThis->NVRAM.VarOpBuf.uuid,
pThis->NVRAM.VarOpBuf.szName, pThis->NVRAM.VarOpBuf.fAttributes, pThis->NVRAM.VarOpBuf.cbValue));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
Log(("nvramWriteVariableOpAdd: Too many variabled.\n"));
}
/*
* Log the value of bugcheck variables.
*/
if ( ( pThis->NVRAM.VarOpBuf.cbValue == 4
|| pThis->NVRAM.VarOpBuf.cbValue == 8)
&& ( strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckCode") == 0
|| strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckParameter0") == 0
|| strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckParameter1") == 0
|| strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckParameter2") == 0
|| strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckParameter3") == 0
|| strcmp(pThis->NVRAM.VarOpBuf.szName, "BugCheckProgress") == 0 ) )
{
if (pThis->NVRAM.VarOpBuf.cbValue == 4)
LogRel(("EFI: %RTuuid::'%s' = %#010RX32\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName,
RT_MAKE_U32_FROM_U8(pThis->NVRAM.VarOpBuf.abValue[0], pThis->NVRAM.VarOpBuf.abValue[1],
pThis->NVRAM.VarOpBuf.abValue[2], pThis->NVRAM.VarOpBuf.abValue[3])));
else
LogRel(("EFI: %RTuuid::'%s' = %#018RX64\n", &pThis->NVRAM.VarOpBuf.uuid, pThis->NVRAM.VarOpBuf.szName,
RT_MAKE_U64_FROM_U8(pThis->NVRAM.VarOpBuf.abValue[0], pThis->NVRAM.VarOpBuf.abValue[1],
pThis->NVRAM.VarOpBuf.abValue[2], pThis->NVRAM.VarOpBuf.abValue[3],
pThis->NVRAM.VarOpBuf.abValue[4], pThis->NVRAM.VarOpBuf.abValue[5],
pThis->NVRAM.VarOpBuf.abValue[6], pThis->NVRAM.VarOpBuf.abValue[7])));
}
LogFunc(("cVariables=%u u32Status=%#x\n", pThis->NVRAM.cVariables, pThis->NVRAM.u32Status));
return VINF_SUCCESS;
}
/**
* Implements EFI_VARIABLE_PARAM writes.
*
* @returns IOM strict status code.
* @param pThis The EFI state.
* @param u32Value The value being written.
*/
static int nvramWriteVariableParam(PDEVEFI pThis, uint32_t u32Value)
{
int rc = VINF_SUCCESS;
switch (pThis->NVRAM.enmOp)
{
case EFI_VM_VARIABLE_OP_START:
switch (u32Value)
{
case EFI_VARIABLE_OP_QUERY:
rc = nvramWriteVariableOpQuery(pThis);
break;
case EFI_VARIABLE_OP_QUERY_NEXT:
rc = nvramWriteVariableOpQueryNext(pThis);
break;
case EFI_VARIABLE_OP_QUERY_REWIND:
Log2(("EFI_VARIABLE_OP_QUERY_REWIND\n"));
pThis->NVRAM.pCurVar = NULL;
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_OK;
break;
case EFI_VARIABLE_OP_ADD:
rc = nvramWriteVariableOpAdd(pThis);
break;
default:
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
LogRel(("EFI: Unknown EFI_VM_VARIABLE_OP_START value %#x\n", u32Value));
break;
}
break;
case EFI_VM_VARIABLE_OP_GUID:
Log2(("EFI_VM_VARIABLE_OP_GUID[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
if (pThis->NVRAM.offOpBuffer < sizeof(pThis->NVRAM.VarOpBuf.uuid))
pThis->NVRAM.VarOpBuf.uuid.au8[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_GUID write (%#x).\n", u32Value));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
break;
case EFI_VM_VARIABLE_OP_ATTRIBUTE:
Log2(("EFI_VM_VARIABLE_OP_ATTRIBUTE=%#x\n", u32Value));
pThis->NVRAM.VarOpBuf.fAttributes = u32Value;
break;
case EFI_VM_VARIABLE_OP_NAME:
Log2(("EFI_VM_VARIABLE_OP_NAME[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cchName)
pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
else if (u32Value == 0)
Assert(pThis->NVRAM.VarOpBuf.szName[sizeof(pThis->NVRAM.VarOpBuf.szName) - 1] == 0);
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME write (%#x).\n", u32Value));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
break;
case EFI_VM_VARIABLE_OP_NAME_LENGTH:
Log2(("EFI_VM_VARIABLE_OP_NAME_LENGTH=%#x\n", u32Value));
RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
if (u32Value < sizeof(pThis->NVRAM.VarOpBuf.szName))
pThis->NVRAM.VarOpBuf.cchName = u32Value;
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_LENGTH write (%#x, max %#x).\n",
u32Value, sizeof(pThis->NVRAM.VarOpBuf.szName) - 1));
pThis->NVRAM.VarOpBuf.cchName = sizeof(pThis->NVRAM.VarOpBuf.szName) - 1;
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
Assert(pThis->NVRAM.offOpBuffer == 0);
break;
case EFI_VM_VARIABLE_OP_NAME_UTF16:
{
Log2(("EFI_VM_VARIABLE_OP_NAME_UTF16[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
/* Currently simplifying this to UCS2, i.e. no surrogates. */
if (pThis->NVRAM.offOpBuffer == 0)
RT_ZERO(pThis->NVRAM.VarOpBuf.szName);
uint32_t cbUtf8 = (uint32_t)RTStrCpSize(u32Value);
if (pThis->NVRAM.offOpBuffer + cbUtf8 < sizeof(pThis->NVRAM.VarOpBuf.szName))
{
RTStrPutCp(&pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer], u32Value);
pThis->NVRAM.offOpBuffer += cbUtf8;
}
else if (u32Value == 0)
Assert(pThis->NVRAM.VarOpBuf.szName[sizeof(pThis->NVRAM.VarOpBuf.szName) - 1] == 0);
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_UTF16 write (%#x).\n", u32Value));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
break;
}
case EFI_VM_VARIABLE_OP_VALUE:
Log2(("EFI_VM_VARIABLE_OP_VALUE[%#x]=%#x\n", pThis->NVRAM.offOpBuffer, u32Value));
if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cbValue)
pThis->NVRAM.VarOpBuf.abValue[pThis->NVRAM.offOpBuffer++] = (uint8_t)u32Value;
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE write (%#x).\n", u32Value));
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
break;
case EFI_VM_VARIABLE_OP_VALUE_LENGTH:
Log2(("EFI_VM_VARIABLE_OP_VALUE_LENGTH=%#x\n", u32Value));
RT_ZERO(pThis->NVRAM.VarOpBuf.abValue);
if (u32Value <= sizeof(pThis->NVRAM.VarOpBuf.abValue))
pThis->NVRAM.VarOpBuf.cbValue = u32Value;
else
{
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE_LENGTH write (%#x, max %#x).\n",
u32Value, sizeof(pThis->NVRAM.VarOpBuf.abValue)));
pThis->NVRAM.VarOpBuf.cbValue = sizeof(pThis->NVRAM.VarOpBuf.abValue);
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
}
Assert(pThis->NVRAM.offOpBuffer == 0);
break;
default:
pThis->NVRAM.u32Status = EFI_VARIABLE_OP_STATUS_ERROR;
LogRel(("EFI: Unexpected variable operation %#x\n", pThis->NVRAM.enmOp));
break;
}
return VINF_SUCCESS;
}
/**
* Implements EFI_VARIABLE_OP reads.
*
* @returns IOM strict status code.
* @param pThis The EFI state.
* @param u32Value The value being written.
*/
static int nvramReadVariableOp(PDEVEFI pThis, uint32_t *pu32, unsigned cb)
{
switch (pThis->NVRAM.enmOp)
{
case EFI_VM_VARIABLE_OP_START:
*pu32 = pThis->NVRAM.u32Status;
break;
case EFI_VM_VARIABLE_OP_GUID:
if (pThis->NVRAM.offOpBuffer < sizeof(pThis->NVRAM.VarOpBuf.uuid) && cb == 1)
*pu32 = pThis->NVRAM.VarOpBuf.uuid.au8[pThis->NVRAM.offOpBuffer++];
else
{
if (cb == 1)
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_GUID read.\n"));
else
LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_GUID read size (%d).\n", cb));
*pu32 = UINT32_MAX;
}
break;
case EFI_VM_VARIABLE_OP_ATTRIBUTE:
*pu32 = pThis->NVRAM.VarOpBuf.fAttributes;
break;
case EFI_VM_VARIABLE_OP_NAME:
/* allow reading terminator char */
if (pThis->NVRAM.offOpBuffer <= pThis->NVRAM.VarOpBuf.cchName && cb == 1)
*pu32 = pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer++];
else
{
if (cb == 1)
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME read.\n"));
else
LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_NAME read size (%d).\n", cb));
*pu32 = UINT32_MAX;
}
break;
case EFI_VM_VARIABLE_OP_NAME_LENGTH:
*pu32 = pThis->NVRAM.VarOpBuf.cchName;
break;
case EFI_VM_VARIABLE_OP_NAME_UTF16:
/* Lazy bird: ASSUME no surrogate pairs. */
if (pThis->NVRAM.offOpBuffer <= pThis->NVRAM.VarOpBuf.cchName && cb == 2)
{
char const *psz1 = &pThis->NVRAM.VarOpBuf.szName[pThis->NVRAM.offOpBuffer];
char const *psz2 = psz1;
RTUNICP Cp;
RTStrGetCpEx(&psz2, &Cp);
*pu32 = Cp;
Log2(("EFI_VM_VARIABLE_OP_NAME_UTF16[%u] => %#x (+%d)\n", pThis->NVRAM.offOpBuffer, *pu32, psz2 - psz1));
pThis->NVRAM.offOpBuffer += psz2 - psz1;
}
else
{
if (cb == 2)
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_NAME_UTF16 read.\n"));
else
LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_NAME_UTF16 read size (%d).\n", cb));
*pu32 = UINT32_MAX;
}
break;
case EFI_VM_VARIABLE_OP_NAME_LENGTH_UTF16:
/* Lazy bird: ASSUME no surrogate pairs. */
*pu32 = (uint32_t)RTStrUniLen(pThis->NVRAM.VarOpBuf.szName);
break;
case EFI_VM_VARIABLE_OP_VALUE:
if (pThis->NVRAM.offOpBuffer < pThis->NVRAM.VarOpBuf.cbValue && cb == 1)
*pu32 = pThis->NVRAM.VarOpBuf.abValue[pThis->NVRAM.offOpBuffer++];
else
{
if (cb == 1)
LogRel(("EFI: Out of bounds EFI_VM_VARIABLE_OP_VALUE read.\n"));
else
LogRel(("EFI: Invalid EFI_VM_VARIABLE_OP_VALUE read size (%d).\n", cb));
*pu32 = UINT32_MAX;
}
break;
case EFI_VM_VARIABLE_OP_VALUE_LENGTH:
*pu32 = pThis->NVRAM.VarOpBuf.cbValue;
break;
default:
*pu32 = UINT32_MAX;
break;
}
return VINF_SUCCESS;
}
/**
* Checks if the EFI variable value looks like a printable UTF-8 string.
*
* @returns true if it is, false if not.
* @param pEfiVar The variable.
* @param pfZeroTerm Where to return whether the string is zero
* terminated.
*/
static bool efiInfoNvramIsUtf8(PCEFIVAR pEfiVar, bool *pfZeroTerm)
{
if (pEfiVar->cbValue < 2)
return false;
const char *pachValue = (const char *)&pEfiVar->abValue[0];
*pfZeroTerm = pachValue[pEfiVar->cbValue - 1] == 0;
/* Check the length. */
size_t cchValue = RTStrNLen((const char *)pEfiVar->abValue, pEfiVar->cbValue);
if (cchValue != pEfiVar->cbValue - *pfZeroTerm)
return false; /* stray zeros in the value, forget it. */
/* Check that the string is valid UTF-8 and printable. */
const char *pchCur = pachValue;
while ((uintptr_t)(pchCur - pachValue) < cchValue)
{
RTUNICP uc;
int rc = RTStrGetCpEx(&pachValue, &uc);