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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
|
/**@file
This file contains the form processing code to the HII database.
Copyright (c) 2006 - 2007 Intel Corporation. <BR>
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "HiiDatabase.h"
STATIC
CHAR16*
Ascii2Unicode (
OUT CHAR16 *UnicodeStr,
IN CHAR8 *AsciiStr
)
/*++
Routine Description:
This function converts ASCII string to Unicode string.
Arguments:
UnicodeStr - NULL terminated Unicode output string.
AsciieStr - NULL terminated ASCII input string.
Returns:
Start of the Unicode ouput string.
--*/
{
CHAR16 *Str = UnicodeStr;
while (TRUE) {
*(UnicodeStr++) = (CHAR16) *AsciiStr;
if (*(AsciiStr++) == '\0') {
return Str;
}
}
}
STATIC
CHAR8*
Unicode2Ascii (
OUT CHAR8 *AsciiStr,
IN CHAR16 *UnicodeStr
)
/*++
Routine Description:
This function converts Unicode string to ASCII string.
Arguments:
AsciieStr - NULL terminated ASCII output string.
UnicodeStr - NULL terminated Unicode input string.
Returns:
Start of the ASCII ouput string.
--*/
{
CHAR8 *Str = AsciiStr;
while (TRUE) {
*(AsciiStr++) = (CHAR8) *UnicodeStr;
if (*(UnicodeStr++) == '\0') {
return Str;
}
}
}
STATIC
VOID
ExtractDevicePathData (
IN EFI_HII_DATA_TABLE *DataTable,
IN UINT8 *IfrData,
IN OUT UINT8 **ExportBufferPtr
)
/*++
Routine Description:
Arguments:
Returns:
--*/
{
UINT8 *ExportBuffer;
ExportBuffer = *ExportBufferPtr;
//
// BUGBUG - don't have devicepath data yet, setting dummy value
//
DataTable++;
ExportBuffer = (UINT8 *) DataTable;
((EFI_HII_DEVICE_PATH_PACK *) ExportBuffer)->Header.Type = EFI_HII_DEVICE_PATH;
((EFI_HII_DEVICE_PATH_PACK *) ExportBuffer)->Header.Length = (UINT32) (sizeof (EFI_HII_DEVICE_PATH_PACK) + sizeof (EFI_DEVICE_PATH_PROTOCOL));
//
// BUGBUG - part of hack - skip the Device Path Pack.....place some data
//
ExportBuffer = ExportBuffer + sizeof (EFI_HII_DEVICE_PATH_PACK);
((EFI_DEVICE_PATH_PROTOCOL *) ExportBuffer)->Type = EFI_END_ENTIRE_DEVICE_PATH;
((EFI_DEVICE_PATH_PROTOCOL *) ExportBuffer)->SubType = EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
//
// BUGBUG - still part of hack....
//
ExportBuffer = ExportBuffer + sizeof (EFI_DEVICE_PATH_PROTOCOL);
*ExportBufferPtr = ExportBuffer;
}
STATIC
VOID
ExtractVariableData (
IN OUT EFI_HII_DATA_TABLE *DataTable,
IN UINT8 *IfrData,
IN OUT UINT8 **ExportBufferPtr
)
/*++
Routine Description:
This function extract the EFI_HII_VARIABLE_PACK portion from the
each of the EFI_HII_PACKAGE_INSTANCE in HII handle database.
Arguments:
DataTable - On input, this parameter point to the EFI_HII_DATA_TABLE structure
of the final data buffer for the EFI_HII_EXPORT interface. This function
update the NumberOfVariableData attribute.
IfrData - It points to a staring address of a EFI_HII_IFR_PACK structure.
ExportBufferPtr - On input, it points the starting address of the data buffer to
host the variable pack. On output, it is the starting address
of data buffer for the next extraction operation.
Returns:
VOID
--*/
{
EFI_HII_VARIABLE_PACK *VariableContents;
UINT8 *ExportBuffer;
UINTN Index;
UINTN Index2;
UINTN TempValue;
UINTN TempValue2;
EFI_FORM_CALLBACK_PROTOCOL *FormCallback;
EFI_PHYSICAL_ADDRESS CallbackHandle;
EFI_STATUS Status;
CHAR16 *String;
FormCallback = NULL;
CallbackHandle = 0;
ExportBuffer = *ExportBufferPtr;
for (Index = 0; IfrData[Index] != EFI_IFR_END_FORM_SET_OP;) {
VariableContents = (EFI_HII_VARIABLE_PACK *) ExportBuffer;
switch (IfrData[Index]) {
case EFI_IFR_FORM_SET_OP:
TempValue = EFI_HII_VARIABLE;
CopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
CopyMem (&TempValue, &((EFI_IFR_FORM_SET *) &IfrData[Index])->NvDataSize, sizeof (UINT16));
//
// If the variable has 0 size, do not process it
//
if (TempValue == 0) {
break;
}
//
// Add the size of the variable pack overhead. Later, will also add the size of the
// name of the variable.
//
TempValue = TempValue + sizeof (EFI_HII_VARIABLE_PACK);
CopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
CopyMem (
&CallbackHandle,
&((EFI_IFR_FORM_SET *) &IfrData[Index])->CallbackHandle,
sizeof (EFI_PHYSICAL_ADDRESS)
);
if (CallbackHandle != 0) {
Status = gBS->HandleProtocol (
(EFI_HANDLE) (UINTN) CallbackHandle,
&gEfiFormCallbackProtocolGuid,
(VOID *) &FormCallback
);
ASSERT_EFI_ERROR (Status);
}
//
// Since we have a "Setup" variable that wasn't specified by a variable op-code
// it will have a VariableId of 0. All other variable op-codes will have a designation
// of VariableId 1+
//
TempValue = 0;
CopyMem (&VariableContents->VariableId, &TempValue, sizeof (UINT16));
CopyMem (&VariableContents->VariableGuid, &((EFI_IFR_FORM_SET *) &IfrData[Index])->Guid, sizeof (EFI_GUID));
TempValue = sizeof (SETUP_MAP_NAME);
CopyMem (&VariableContents->VariableNameLength, &TempValue, sizeof (UINT32));
//
// Add the size of the name to the Header Length
//
TempValue2 = 0;
CopyMem (&TempValue2, &VariableContents->Header.Length, sizeof (UINT32));
TempValue2 = TempValue + TempValue2;
CopyMem (&VariableContents->Header.Length, &TempValue2, sizeof (UINT32));
ExportBuffer = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
CopyMem (ExportBuffer, SETUP_MAP_NAME, sizeof (SETUP_MAP_NAME));
ExportBuffer = ExportBuffer + sizeof (SETUP_MAP_NAME);
CopyMem (&TempValue, &((EFI_IFR_FORM_SET *) &IfrData[Index])->NvDataSize, sizeof (UINT16));
if ((FormCallback != NULL) && (FormCallback->NvRead != NULL)) {
Status = FormCallback->NvRead (
FormCallback,
(CHAR16 *) SETUP_MAP_NAME,
(EFI_GUID *)(UINTN)&VariableContents->VariableGuid,
NULL,
&TempValue,
ExportBuffer
);
ASSERT_EFI_ERROR (Status);
} else {
Status = gRT->GetVariable (
(CHAR16 *) SETUP_MAP_NAME,
(EFI_GUID *)(UINTN)&VariableContents->VariableGuid,
NULL,
&TempValue,
ExportBuffer
);
ASSERT_EFI_ERROR (Status);
}
ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + TempValue);
DataTable->NumberOfVariableData++;
break;
case EFI_IFR_VARSTORE_OP:
TempValue = EFI_HII_VARIABLE;
CopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
CopyMem (&TempValue, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Size, sizeof (UINT16));
//
// If the variable has 0 size, do not process it
//
if (TempValue == 0) {
break;
}
//
// Add the size of the variable pack overhead. Later, will also add the size of the
// name of the variable.
//
TempValue = TempValue + sizeof (EFI_HII_VARIABLE_PACK);
CopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
CopyMem (&VariableContents->VariableId, &((EFI_IFR_VARSTORE *) &IfrData[Index])->VarId, sizeof (UINT16));
CopyMem (&VariableContents->VariableGuid, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Guid, sizeof (EFI_GUID));
TempValue = (UINTN) ((EFI_IFR_VARSTORE *) &IfrData[Index])->Header.Length - sizeof (EFI_IFR_VARSTORE);
TempValue = TempValue * 2;
CopyMem (&VariableContents->VariableNameLength, &TempValue, sizeof (UINT32));
//
// Add the size of the name to the Header Length
//
TempValue2 = 0;
CopyMem (&TempValue2, &VariableContents->Header.Length, sizeof (UINT32));
TempValue2 = TempValue + TempValue2;
CopyMem (&VariableContents->Header.Length, &TempValue2, sizeof (UINT32));
ExportBuffer = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
String = (CHAR16 *) ExportBuffer;
for (Index2 = 0; Index2 < TempValue / 2; Index2++) {
ExportBuffer[Index2 * 2] = IfrData[Index + sizeof (EFI_IFR_VARSTORE) + Index2];
ExportBuffer[Index2 * 2 + 1] = 0;
}
ExportBuffer = ExportBuffer + TempValue;
CopyMem (&TempValue, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Size, sizeof (UINT16));
if ((FormCallback != NULL) && (FormCallback->NvRead != NULL)) {
Status = FormCallback->NvRead (
FormCallback,
String,
(EFI_GUID *)(UINTN)&VariableContents->VariableGuid,
NULL,
&TempValue,
ExportBuffer
);
ASSERT_EFI_ERROR (Status);
} else {
Status = gRT->GetVariable (
String,
(EFI_GUID *)(UINTN)&VariableContents->VariableGuid,
NULL,
&TempValue,
ExportBuffer
);
ASSERT_EFI_ERROR (Status);
}
ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + TempValue);
DataTable->NumberOfVariableData++;
break;
}
Index = IfrData[Index + 1] + Index;
}
//
// If we have added a variable pack, add a dummy empty one to signify the end
//
if (ExportBuffer != *ExportBufferPtr) {
VariableContents = (EFI_HII_VARIABLE_PACK *) ExportBuffer;
TempValue = EFI_HII_VARIABLE;
CopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
TempValue = sizeof (EFI_HII_VARIABLE_PACK);
CopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
ExportBuffer = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
}
*ExportBufferPtr = ExportBuffer;
}
EFI_STATUS
EFIAPI
HiiExportDatabase (
IN EFI_HII_PROTOCOL *This,
IN EFI_HII_HANDLE Handle,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
/*++
Routine Description:
This function allows a program to extract a form or form package that has
previously been registered with the EFI HII database.
Arguments:
Returns:
--*/
{
EFI_HII_PACKAGE_INSTANCE *PackageInstance;
EFI_HII_DATA *HiiData;
EFI_HII_HANDLE_DATABASE *HandleDatabase;
EFI_HII_IFR_PACK *FormPack;
UINT8 *RawData;
UINT8 *ExportBuffer;
EFI_HII_EXPORT_TABLE *ExportTable;
EFI_HII_DATA_TABLE *DataTable;
BOOLEAN VariableExist;
UINT16 NumberOfHiiDataTables;
UINTN SizeNeeded;
UINTN Index;
UINTN VariableSize;
UINTN TempValue;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
HiiData = EFI_HII_DATA_FROM_THIS (This);
HandleDatabase = HiiData->DatabaseHead;
FormPack = NULL;
RawData = NULL;
PackageInstance = NULL;
NumberOfHiiDataTables = 0;
VariableSize = 0;
TempValue = 0;
SizeNeeded = sizeof (EFI_HII_EXPORT_TABLE);
//
// How many total tables are there?
//
for (; HandleDatabase != NULL; HandleDatabase = HandleDatabase->NextHandleDatabase) {
if ((Handle != 0) && (Handle != HandleDatabase->Handle)) {
continue;
}
VariableExist = FALSE;
NumberOfHiiDataTables++;
PackageInstance = HandleDatabase->Buffer;
if (PackageInstance == NULL) {
continue;
}
//
// Extract Size of Export Package
//
SizeNeeded = SizeNeeded + PackageInstance->IfrSize
+ PackageInstance->StringSize
+ sizeof (EFI_HII_DATA_TABLE)
+ sizeof (EFI_HII_DEVICE_PATH_PACK);
//
// BUGBUG We aren't inserting Device path data yet
//
SizeNeeded = SizeNeeded + sizeof (EFI_DEVICE_PATH_PROTOCOL);
//
// Extract Size of Variable Data
//
if (PackageInstance->IfrSize > 0) {
FormPack = (EFI_HII_IFR_PACK *) ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER));
} else {
//
// No IFR? No variable information
//
continue;
}
RawData = (UINT8 *) FormPack;
for (Index = 0; RawData[Index] != EFI_IFR_END_FORM_SET_OP;) {
switch (RawData[Index]) {
case EFI_IFR_FORM_SET_OP:
CopyMem (&VariableSize, &((EFI_IFR_FORM_SET *) &RawData[Index])->NvDataSize, sizeof (UINT16));
SizeNeeded = SizeNeeded + VariableSize + sizeof (SETUP_MAP_NAME) + sizeof (EFI_HII_VARIABLE_PACK);
VariableExist = TRUE;
break;
case EFI_IFR_VARSTORE_OP:
CopyMem (&VariableSize, &((EFI_IFR_VARSTORE *) &RawData[Index])->Size, sizeof (UINT16));
SizeNeeded = SizeNeeded + VariableSize + sizeof (EFI_HII_VARIABLE_PACK);
//
// We will be expanding the stored ASCII name to a Unicode string. This will cause some memory overhead
// Since the VARSTORE size already takes in consideration the ASCII size, we need to size it and add another
// instance of it. Essentially, 2 ASCII strings == 1 Unicode string in size.
//
TempValue = (UINTN) ((EFI_IFR_VARSTORE *) &RawData[Index])->Header.Length - sizeof (EFI_IFR_VARSTORE);
SizeNeeded = SizeNeeded + TempValue * 2;
VariableExist = TRUE;
break;
}
Index = RawData[Index + 1] + Index;
}
//
// If a variable exists for this handle, add an additional variable pack overhead to
// indicate that we will have an extra null Variable Pack to signify the end of the Variable Packs
//
if (VariableExist) {
SizeNeeded = SizeNeeded + sizeof (EFI_HII_VARIABLE_PACK);
}
}
if (SizeNeeded > *BufferSize) {
*BufferSize = SizeNeeded;
return EFI_BUFFER_TOO_SMALL;
}
//
// Zero out the incoming buffer
//
ZeroMem (Buffer, *BufferSize);
//
// Cast the Buffer to EFI_HII_EXPORT_TABLE
//
ExportTable = (EFI_HII_EXPORT_TABLE *) Buffer;
//
// Set the Revision for the Export Table
//
CopyMem (&ExportTable->Revision, &gEfiHiiProtocolGuid, sizeof (EFI_GUID));
ExportBuffer = (UINT8 *) (UINTN) (((UINT8 *) ExportTable) + sizeof (EFI_HII_EXPORT_TABLE));
HandleDatabase = HiiData->DatabaseHead;
//
// Check numeric value against the head of the database
//
for (; HandleDatabase != NULL; HandleDatabase = HandleDatabase->NextHandleDatabase) {
DataTable = (EFI_HII_DATA_TABLE *) ExportBuffer;
PackageInstance = HandleDatabase->Buffer;
//
// If not asking for a specific handle, export the entire database
//
if (Handle == 0) {
ExportTable->NumberOfHiiDataTables = NumberOfHiiDataTables;
CopyMem (&DataTable->PackageGuid, &PackageInstance->Guid, sizeof (EFI_GUID));
DataTable->HiiHandle = PackageInstance->Handle;
DataTable->DevicePathOffset = (UINT32) (sizeof (EFI_HII_DATA_TABLE));
//
// Start Dumping DevicePath
//
ExtractDevicePathData (DataTable, RawData, &ExportBuffer);
if (((UINTN) ExportBuffer) == ((UINTN) DataTable)) {
//
// If there is no DevicePath information - set offset to 0 to signify the absence of data to parse
//
DataTable->DevicePathOffset = 0;
}
DataTable->VariableDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
if (PackageInstance->IfrSize > 0) {
FormPack = (EFI_HII_IFR_PACK *) ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER));
RawData = (UINT8 *) FormPack;
TempValue = 0;
//
// Start dumping the Variable Data
//
ExtractVariableData (DataTable, RawData, &ExportBuffer);
DataTable->IfrDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
if (DataTable->VariableDataOffset == DataTable->IfrDataOffset) {
DataTable->VariableDataOffset = 0;
}
//
// Start dumping the IFR data (Note: It is in an IFR PACK)
//
CopyMem (ExportBuffer, &PackageInstance->IfrData, PackageInstance->IfrSize);
ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + PackageInstance->IfrSize);
DataTable->StringDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
//
// Start dumping the String data (Note: It is in a String PACK)
//
if (PackageInstance->StringSize > 0) {
RawData = (UINT8 *) (((UINTN) &PackageInstance->IfrData) + PackageInstance->IfrSize);
CopyMem (ExportBuffer, RawData, PackageInstance->StringSize);
DataTable->DataTableSize = (UINT32) (DataTable->StringDataOffset + PackageInstance->StringSize);
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
for (; TempValue != 0;) {
DataTable->NumberOfLanguages++;
ExportBuffer = ExportBuffer + ((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length;
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
}
ExportBuffer = ExportBuffer + sizeof (EFI_HII_STRING_PACK);
} else {
DataTable->StringDataOffset = 0;
}
} else {
//
// No IFR? No variable information. If Offset is 0, means there is none. (Hmm - this might be prunable - no strings to export if no IFR - we always have a stub)
//
DataTable->VariableDataOffset = 0;
DataTable->IfrDataOffset = 0;
DataTable->StringDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
//
// Start dumping the String data - NOTE: It is in String Pack form
//
if (PackageInstance->StringSize > 0) {
RawData = (UINT8 *) (((UINTN) &PackageInstance->IfrData) + PackageInstance->IfrSize);
CopyMem (ExportBuffer, RawData, PackageInstance->StringSize);
DataTable->DataTableSize = (UINT32) (DataTable->StringDataOffset + PackageInstance->StringSize);
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
for (; TempValue != 0;) {
DataTable->NumberOfLanguages++;
ExportBuffer = ExportBuffer + ((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length;
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
}
ExportBuffer = ExportBuffer + sizeof (EFI_HII_STRING_PACK);
} else {
DataTable->StringDataOffset = 0;
}
}
} else {
//
// Match the numeric value with the database entry - if matched, extract PackageInstance
//
if (Handle == HandleDatabase->Handle) {
PackageInstance = HandleDatabase->Buffer;
ExportTable->NumberOfHiiDataTables = NumberOfHiiDataTables;
DataTable->HiiHandle = PackageInstance->Handle;
CopyMem (&DataTable->PackageGuid, &PackageInstance->Guid, sizeof (EFI_GUID));
//
// Start Dumping DevicePath
//
ExtractDevicePathData (DataTable, RawData, &ExportBuffer);
DataTable->VariableDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
if (PackageInstance->IfrSize > 0) {
FormPack = (EFI_HII_IFR_PACK *) ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER));
RawData = (UINT8 *) FormPack;
TempValue = 0;
//
// Start dumping the Variable Data
//
ExtractVariableData (DataTable, RawData, &ExportBuffer);
DataTable->IfrDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
if (DataTable->VariableDataOffset == DataTable->IfrDataOffset) {
DataTable->VariableDataOffset = 0;
}
//
// Start dumping the IFR data
//
CopyMem (ExportBuffer, &PackageInstance->IfrData, PackageInstance->IfrSize);
ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + PackageInstance->IfrSize);
DataTable->StringDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
//
// Start dumping the String data - NOTE: It is in String Pack form
//
if (PackageInstance->StringSize > 0) {
RawData = (UINT8 *) (((UINTN) &PackageInstance->IfrData) + PackageInstance->IfrSize);
CopyMem (ExportBuffer, RawData, PackageInstance->StringSize);
DataTable->DataTableSize = (UINT32) (DataTable->StringDataOffset + PackageInstance->StringSize);
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
for (; TempValue != 0;) {
DataTable->NumberOfLanguages++;
ExportBuffer = ExportBuffer + ((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length;
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
}
ExportBuffer = ExportBuffer + sizeof (EFI_HII_STRING_PACK);
} else {
DataTable->StringDataOffset = 0;
}
} else {
//
// No IFR? No variable information. If Offset is 0, means there is none.
//
DataTable->VariableDataOffset = 0;
DataTable->IfrDataOffset = 0;
DataTable->StringDataOffset = (UINT32) (((UINTN) ExportBuffer) - ((UINTN) DataTable));
//
// Start dumping the String data - Note: It is in String Pack form
//
if (PackageInstance->StringSize > 0) {
RawData = (UINT8 *) (((UINTN) &PackageInstance->IfrData) + PackageInstance->IfrSize);
CopyMem (ExportBuffer, RawData, PackageInstance->StringSize);
DataTable->DataTableSize = (UINT32) (DataTable->StringDataOffset + PackageInstance->StringSize);
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
for (; TempValue != 0;) {
DataTable->NumberOfLanguages++;
ExportBuffer = ExportBuffer + ((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length;
CopyMem (&TempValue, &((EFI_HII_STRING_PACK *) ExportBuffer)->Header.Length, sizeof (UINT32));
}
ExportBuffer = ExportBuffer + sizeof (EFI_HII_STRING_PACK);
} else {
DataTable->StringDataOffset = 0;
}
}
break;
}
}
}
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
HiiGetForms (
IN EFI_HII_PROTOCOL *This,
IN EFI_HII_HANDLE Handle,
IN EFI_FORM_ID FormId,
IN OUT UINTN *BufferLengthTemp,
OUT UINT8 *Buffer
)
/*++
Routine Description:
This function allows a program to extract a form or form package that has
previously been registered with the EFI HII database.
Arguments:
This - A pointer to the EFI_HII_PROTOCOL instance.
Handle - Handle on which the form resides. Type EFI_HII_HANDLE is defined in
EFI_HII_PROTOCOL.NewPack() in the Packages section.
FormId - The ID of the form to return. If the ID is zero, the entire form package is returned.
Type EFI_FORM_ID is defined in "Related Definitions" below.
BufferLength - On input, the length of the Buffer. On output, the length of the returned buffer, if
the length was sufficient and, if it was not, the length that is required to fit the
requested form(s).
Buffer - The buffer designed to receive the form(s).
Returns:
EFI_SUCCESS - Buffer filled with the requested forms. BufferLength
was updated.
EFI_INVALID_PARAMETER - The handle is unknown.
EFI_NOT_FOUND - A form on the requested handle cannot be found with the
requested FormId.
EFI_BUFFER_TOO_SMALL - The buffer provided was not large enough to allow the form to be stored.
--*/
{
EFI_HII_PACKAGE_INSTANCE *PackageInstance;
EFI_HII_DATA *HiiData;
EFI_HII_HANDLE_DATABASE *HandleDatabase;
EFI_HII_IFR_PACK *FormPack;
EFI_IFR_FORM *Form;
EFI_IFR_OP_HEADER *Location;
UINT16 *BufferLength = (UINT16 *) BufferLengthTemp;
UINTN FormLength;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
HiiData = EFI_HII_DATA_FROM_THIS (This);
HandleDatabase = HiiData->DatabaseHead;
PackageInstance = NULL;
FormLength = 0;
//
// Check numeric value against the head of the database
//
for (; HandleDatabase != NULL; HandleDatabase = HandleDatabase->NextHandleDatabase) {
//
// Match the numeric value with the database entry - if matched, extract PackageInstance
//
if (Handle == HandleDatabase->Handle) {
PackageInstance = HandleDatabase->Buffer;
break;
}
}
//
// No handle was found - error condition
//
if (PackageInstance == NULL) {
return EFI_NOT_FOUND;
}
//
// Based on if there is IFR data in this package instance, determine
// what the location is of the beginning of the string data.
//
if (PackageInstance->IfrSize > 0) {
FormPack = (EFI_HII_IFR_PACK *) (&PackageInstance->IfrData);
} else {
//
// If there is no IFR data return an error
//
return EFI_NOT_FOUND;
}
//
// If requesting the entire Form Package
//
if (FormId == 0) {
//
// Return an error if buffer is too small
//
if (PackageInstance->IfrSize > *BufferLength || Buffer == NULL) {
*BufferLength = (UINT16) PackageInstance->IfrSize;
return EFI_BUFFER_TOO_SMALL;
}
CopyMem (Buffer, FormPack, PackageInstance->IfrSize);
return EFI_SUCCESS;
} else {
FormPack = (EFI_HII_IFR_PACK *) ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER));
Location = (EFI_IFR_OP_HEADER *) FormPack;
//
// Look for the FormId requested
//
for (; Location->OpCode != EFI_IFR_END_FORM_SET_OP;) {
switch (Location->OpCode) {
case EFI_IFR_FORM_OP:
Form = (EFI_IFR_FORM *) Location;
//
// If we found a Form Op-code and it is of the correct Id, copy it and return
//
if (Form->FormId == FormId) {
//
// Calculate the total size of form
//
for (FormLength = 0; Location->OpCode != EFI_IFR_END_FORM_OP; ) {
FormLength += Location->Length;
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
}
FormLength += Location->Length;
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
if ((Buffer == NULL) || (FormLength > *BufferLength)) {
*BufferLengthTemp = FormLength;
return EFI_BUFFER_TOO_SMALL;
}
//
// Rewind to start offset of the found Form
//
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *)Location - FormLength);
CopyMem (Buffer, Location, FormLength);
return EFI_SUCCESS;
}
default:
break;
}
//
// Go to the next Op-Code
//
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
}
}
return EFI_NOT_FOUND;
}
//
// Helper functions to HiiGetDefaultImage()
//
STATIC
UINT8*
HiiGetDefaultImageInitPack (
IN OUT EFI_HII_VARIABLE_PACK_LIST *VariablePackItem,
IN EFI_IFR_VARSTORE *VarStore
)
/*++
Routine Description:
Initialize the EFI_HII_VARIABLE_PACK_LIST structure and
prepare it ready to be used by HiiGetDefaultImagePopulateMap ().
Arguments:
VariablePackItem - Variable Package List.
VarStore - IFR variable storage.
Returns:
Return the pointer to the Map space.
--*/
{
CHAR16 *Name16;
CHAR8 *Name8;
CHAR8 *Map;
EFI_HII_VARIABLE_PACK *VariablePack;
//
// Set pointer the pack right after the node
//
VariablePackItem->VariablePack = (EFI_HII_VARIABLE_PACK *) (VariablePackItem + 1);
VariablePack = VariablePackItem->VariablePack;
//
// Copy the var name to VariablePackItem from VarStore
// Needs ASCII->Unicode conversion.
//
ASSERT (VarStore->Header.Length > sizeof (*VarStore));
Name8 = (CHAR8 *) (VarStore + 1);
Name16 = (CHAR16 *) (VariablePack + 1);
Ascii2Unicode (Name16, Name8);
//
// Compute the other fields of the VariablePackItem
//
VariablePack->VariableId = VarStore->VarId;
CopyMem (&VariablePack->VariableGuid, &VarStore->Guid, sizeof (EFI_GUID));
VariablePack->VariableNameLength = (UINT32) ((StrLen (Name16) + 1) * 2);
VariablePack->Header.Length = sizeof (*VariablePack)
+ VariablePack->VariableNameLength
+ VarStore->Size;
//
// Return the pointer to the Map space.
//
Map = (CHAR8 *) Name16 + VariablePack->VariableNameLength;
return (UINT8 *)Map;
}
STATIC
VOID
HiiGetDefaultImagePopulateMap (
IN OUT UINT8 *Map,
IN EFI_IFR_OP_HEADER *FormSet,
IN EFI_IFR_VARSTORE *VarStore,
IN UINTN DefaultMask
)
/*++
Routine Description:
Fill the Map with all the default values either from NV or Hii database.
Arguments:
Map - Memory pointer to hold the default values.
FormSet - The starting EFI_IFR_OP_HEADER to begin retriving default values.
VarStore - IFR variable storage.
DefaultMask - The mask used to get the default variable.
Returns:
VOID
--*/
{
EFI_STATUS Status;
EFI_IFR_OP_HEADER *IfrItem;
UINT16 VarId;
EFI_IFR_VARSTORE_SELECT *VarSelect;
EFI_IFR_ONE_OF_OPTION *OneOfOpt;
EFI_IFR_CHECKBOX *CheckBox;
EFI_IFR_NUMERIC *Numeric;
UINTN Size;
UINTN SizeTmp;
EFI_IFR_NV_DATA *IfrNvData;
EFI_GUID Guid;
CHAR16 *Name16;
CHAR8 *Name8;
EFI_HANDLE CallbackHandle;
EFI_FORM_CALLBACK_PROTOCOL *FormCallbackProt;
//
// Get the Map's Name/Guid/Szie from the Varstore.
// VARSTORE contains the Name in ASCII format (@#$^&!), must convert it to Unicode.
//
ASSERT (VarStore->Header.Length >= sizeof (*VarStore));
Name8 = (CHAR8 *) (VarStore + 1);
Name16 = AllocateZeroPool ((VarStore->Header.Length - sizeof (*VarStore)) * sizeof (CHAR16));
Ascii2Unicode (Name16, Name8);
CopyMem (&Guid, &VarStore->Guid, sizeof(EFI_GUID));
Size = VarStore->Size;
//
// First, check if the map exists in the NV. If so, get it from NV and exit.
//
if (DefaultMask == EFI_IFR_FLAG_MANUFACTURING) {
//
// Check if Manufaturing Defaults exist in the NV.
//
Status = EfiLibHiiVariableOverrideBySuffix (
HII_VARIABLE_SUFFIX_MANUFACTURING_OVERRIDE,
Name16,
&Guid,
Size,
Map
);
} else {
//
// All other cases default to Defaults. Check if Defaults exist in the NV.
//
Status = EfiLibHiiVariableOverrideBySuffix (
HII_VARIABLE_SUFFIX_DEFAULT_OVERRIDE,
Name16,
&Guid,
Size,
Map
);
}
if (!EFI_ERROR (Status)) {
//
// Either Defaults/Manufacturing variable exists and appears to be valid.
// The map is read, exit w/ success now.
//
gBS->FreePool (Name16);
return;
}
//
// First, prime the map with what already is in the NV.
// This is needed to cover a situation where the IFR does not contain all the
// defaults; either deliberately not having appropriate IFR, or in case of IFR_STRING, there is no default.
// Ignore status. Either it gets read or not.
//
FormCallbackProt = NULL;
CopyMem (&CallbackHandle, &((EFI_IFR_FORM_SET*) FormSet)->CallbackHandle, sizeof (CallbackHandle));
if (CallbackHandle != NULL) {
Status = gBS->HandleProtocol (
(EFI_HANDLE) (UINTN) CallbackHandle,
&gEfiFormCallbackProtocolGuid,
(VOID *) &FormCallbackProt
);
}
if ((NULL != FormCallbackProt) && (NULL != FormCallbackProt->NvRead)) {
//
// Attempt to read using NvRead() callback. Probe first for existence and correct variable size.
//
SizeTmp = 0;
Status = FormCallbackProt->NvRead (
FormCallbackProt,
Name16,
&Guid,
0,
&SizeTmp,
NULL
);
if ((EFI_BUFFER_TOO_SMALL == Status) && (SizeTmp == Size)) {
Status = FormCallbackProt->NvRead (
FormCallbackProt,
Name16,
&Guid,
0,
&SizeTmp,
Map
);
ASSERT_EFI_ERROR (Status);
ASSERT (SizeTmp == Size);
}
} else {
//
// No callback available for this formset, read straight from NV. Deliberately ignore the Status.
// The buffer will only be written if variable exists nd has correct size.
//
Status = EfiLibHiiVariableRetrieveFromNv (
Name16,
&Guid,
Size,
(VOID **) &Map
);
}
//
// Iterate all IFR statements and for applicable, retrieve the default into the Map.
//
for (IfrItem = FormSet, VarId = 0;
IfrItem->OpCode != EFI_IFR_END_FORM_SET_OP;
IfrItem = (EFI_IFR_OP_HEADER *) ((UINT8*) IfrItem + IfrItem->Length)
) {
//
// Observe VarStore switch.
//
if (EFI_IFR_VARSTORE_SELECT_OP == IfrItem->OpCode) {
VarSelect = (EFI_IFR_VARSTORE_SELECT *) IfrItem;
VarId = VarSelect->VarId;
continue;
}
//
// Skip opcodes that reference other VarStore than that specific to current map.
//
if (VarId != VarStore->VarId) {
continue;
}
//
// Extract the default value from this opcode if applicable, and apply it to the map.
//
IfrNvData = (EFI_IFR_NV_DATA *) IfrItem;
switch (IfrItem->OpCode) {
case EFI_IFR_ONE_OF_OP:
ASSERT (IfrNvData->QuestionId + IfrNvData->StorageWidth <= VarStore->Size);
//
// Get to the first EFI_IFR_ONE_OF_OPTION_OP
//
IfrItem = (EFI_IFR_OP_HEADER *) ((UINT8*) IfrItem + IfrItem->Length);
ASSERT (EFI_IFR_ONE_OF_OPTION_OP == IfrItem->OpCode);
OneOfOpt = (EFI_IFR_ONE_OF_OPTION *)IfrItem;
//
// In the worst case, the first will be the default.
//
CopyMem (Map + IfrNvData->QuestionId, &OneOfOpt->Value, IfrNvData->StorageWidth);
while (EFI_IFR_ONE_OF_OPTION_OP == IfrItem->OpCode) {
OneOfOpt = (EFI_IFR_ONE_OF_OPTION *)IfrItem;
if (DefaultMask == EFI_IFR_FLAG_MANUFACTURING) {
if (0 != (OneOfOpt->Flags & EFI_IFR_FLAG_MANUFACTURING)) {
//
// In the worst case, the first will be the default.
//
CopyMem (Map + IfrNvData->QuestionId, &OneOfOpt->Value, IfrNvData->StorageWidth);
break;
}
} else {
if (OneOfOpt->Flags & EFI_IFR_FLAG_DEFAULT) {
//
// In the worst case, the first will be the default.
//
CopyMem (Map + IfrNvData->QuestionId, &OneOfOpt->Value, IfrNvData->StorageWidth);
break;
}
}
IfrItem = (EFI_IFR_OP_HEADER *)((UINT8*)IfrItem + IfrItem->Length);
}
continue;
break;
case EFI_IFR_CHECKBOX_OP:
ASSERT (IfrNvData->QuestionId + IfrNvData->StorageWidth <= VarStore->Size);
CheckBox = (EFI_IFR_CHECK_BOX *)IfrItem;
if (DefaultMask == EFI_IFR_FLAG_MANUFACTURING) {
if (0 != (CheckBox->Flags & EFI_IFR_FLAG_MANUFACTURING)) {
*(UINT8 *) (Map + IfrNvData->QuestionId) = TRUE;
}
} else {
if (CheckBox->Flags & EFI_IFR_FLAG_DEFAULT) {
*(UINT8 *) (Map + IfrNvData->QuestionId) = TRUE;
}
}
break;
case EFI_IFR_NUMERIC_OP:
ASSERT (IfrNvData->QuestionId + IfrNvData->StorageWidth <= VarStore->Size);
Numeric = (EFI_IFR_NUMERIC *) IfrItem;
CopyMem (Map + IfrNvData->QuestionId, &Numeric->Default, IfrNvData->StorageWidth);
break;
case EFI_IFR_ORDERED_LIST_OP:
case EFI_IFR_PASSWORD_OP:
case EFI_IFR_STRING_OP:
//
// No support for default value for these opcodes.
//
break;
}
}
gBS->FreePool (Name16);
}
EFI_STATUS
EFIAPI
HiiGetDefaultImage (
IN EFI_HII_PROTOCOL *This,
IN EFI_HII_HANDLE Handle,
IN UINTN DefaultMask,
OUT EFI_HII_VARIABLE_PACK_LIST **VariablePackList
)
/*++
Routine Description:
This function allows a program to extract the NV Image
that represents the default storage image
Arguments:
This - A pointer to the EFI_HII_PROTOCOL instance.
Handle - The HII handle from which will have default data retrieved.
UINTN - Mask used to retrieve the default image.
VariablePackList - Callee allocated, tightly-packed, link list data
structure that contain all default varaible packs
from the Hii Database.
Returns:
EFI_NOT_FOUND - If Hii database does not contain any default images.
EFI_INVALID_PARAMETER - Invalid input parameter.
EFI_SUCCESS - Operation successful.
--*/
{
EFI_HII_HANDLE_DATABASE *HandleDatabase;
EFI_HII_PACKAGE_INSTANCE *PackageInstance;
EFI_IFR_OP_HEADER *FormSet;
EFI_IFR_OP_HEADER *IfrItem;
EFI_IFR_VARSTORE *VarStore;
EFI_IFR_VARSTORE *VarStoreDefault;
UINTN SetupMapNameSize;
UINTN SizeOfMaps;
EFI_HII_VARIABLE_PACK_LIST *PackList;
EFI_HII_VARIABLE_PACK_LIST *PackListNext;
EFI_HII_VARIABLE_PACK_LIST *PackListLast;
UINT8 *Map;
//
// Find the IFR pack from the handle. Then get the formset from the pack.
//
PackageInstance = NULL;
HandleDatabase = (EFI_HII_DATA_FROM_THIS (This))->DatabaseHead;
for ( ; HandleDatabase != NULL; HandleDatabase = HandleDatabase->NextHandleDatabase) {
if (Handle == HandleDatabase->Handle) {
PackageInstance = HandleDatabase->Buffer;
break;
}
}
if (PackageInstance == NULL) {
return EFI_INVALID_PARAMETER;
}
FormSet = (EFI_IFR_OP_HEADER *) ((UINT8 *) &PackageInstance->IfrData + sizeof (EFI_HII_IFR_PACK));
//
// Get the sizes of all the VARSTOREs in this VFR.
// Then allocate enough space for all of them plus all maps
//
SizeOfMaps = 0;
IfrItem = FormSet;
while (EFI_IFR_END_FORM_SET_OP != IfrItem->OpCode) {
if (EFI_IFR_VARSTORE_OP == IfrItem->OpCode) {
VarStore = (EFI_IFR_VARSTORE *) IfrItem;
//
// Size of the map
//
SizeOfMaps += VarStore->Size;
//
// add the size of the string, in Unicode
//
SizeOfMaps += (VarStore->Header.Length - sizeof (*VarStore)) * 2;
//
// Space for node
//
SizeOfMaps += sizeof (EFI_HII_VARIABLE_PACK);
//
// Space for linked list node
//
SizeOfMaps += sizeof (EFI_HII_VARIABLE_PACK_LIST);
}
IfrItem = (EFI_IFR_OP_HEADER *) ((UINT8 *) IfrItem + IfrItem->Length);
}
//
// If the FormSet OpCode has a non-zero NvDataSize. There is a default
// NvMap with ID=0, GUID that of the formset itself and "Setup" as name.
//
SetupMapNameSize = StrLen (SETUP_MAP_NAME) + 1;
VarStoreDefault = AllocateZeroPool (sizeof (*VarStoreDefault) + SetupMapNameSize);
if (0 != ((EFI_IFR_FORM_SET*)FormSet)->NvDataSize) {
VarStoreDefault->Header.OpCode = EFI_IFR_VARSTORE_OP;
VarStoreDefault->Header.Length = (UINT8) (sizeof (*VarStoreDefault) + SetupMapNameSize);
Unicode2Ascii ((CHAR8 *) (VarStoreDefault + 1), SETUP_MAP_NAME);
CopyMem (&VarStoreDefault->Guid, &((EFI_IFR_FORM_SET*) FormSet)->Guid, sizeof (EFI_GUID));
VarStoreDefault->VarId = 0;
VarStoreDefault->Size = ((EFI_IFR_FORM_SET*) FormSet)->NvDataSize;
//
// Size of the map
//
SizeOfMaps += VarStoreDefault->Size;
//
// add the size of the string
//
SizeOfMaps += sizeof (SETUP_MAP_NAME);
//
// Space for node
//
SizeOfMaps += sizeof (EFI_HII_VARIABLE_PACK);
//
// Space for linked list node
//
SizeOfMaps += sizeof (EFI_HII_VARIABLE_PACK_LIST);
}
if (0 == SizeOfMaps) {
//
// The IFR does not have any explicit or default map(s).
//
return EFI_NOT_FOUND;
}
//
// Allocate the return buffer
//
PackList = AllocateZeroPool (SizeOfMaps);
ASSERT (NULL != PackList);
PackListNext = PackList;
PackListLast = PackList;
//
// Handle the default map first, if any.
//
if (0 != VarStoreDefault->Size) {
Map = HiiGetDefaultImageInitPack (PackListNext, VarStoreDefault);
HiiGetDefaultImagePopulateMap (Map, FormSet, VarStoreDefault, DefaultMask);
PackListNext->NextVariablePack = (EFI_HII_VARIABLE_PACK_LIST *) ((UINT8 *) PackListNext->VariablePack + PackListNext->VariablePack->Header.Length);
PackListLast = PackListNext;
PackListNext = PackListNext->NextVariablePack;
}
//
// Handle the explicit varstore(s)
//
IfrItem = FormSet;
while (EFI_IFR_END_FORM_SET_OP != IfrItem->OpCode) {
if (EFI_IFR_VARSTORE_OP == IfrItem->OpCode) {
Map = HiiGetDefaultImageInitPack (PackListNext, (EFI_IFR_VARSTORE *) IfrItem);
HiiGetDefaultImagePopulateMap (Map, FormSet, (EFI_IFR_VARSTORE *) IfrItem, DefaultMask);
PackListNext->NextVariablePack = (EFI_HII_VARIABLE_PACK_LIST *) ((UINT8 *) PackListNext->VariablePack + PackListNext->VariablePack->Header.Length);
PackListLast = PackListNext;
PackListNext = PackListNext->NextVariablePack;
}
IfrItem = (EFI_IFR_OP_HEADER *) ((UINT8 *) IfrItem + IfrItem->Length);
}
PackListLast->NextVariablePack = NULL;
*VariablePackList = PackList;
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
HiiUpdateForm (
IN EFI_HII_PROTOCOL *This,
IN EFI_HII_HANDLE Handle,
IN EFI_FORM_LABEL Label,
IN BOOLEAN AddData,
IN EFI_HII_UPDATE_DATA *Data
)
/*++
Routine Description:
This function allows the caller to update a form that has
previously been registered with the EFI HII database.
Arguments:
Handle - Hii Handle associated with the Formset to modify
Label - Update information starting immediately after this label in the IFR
AddData - If TRUE, add data. If FALSE, remove data
Data - If adding data, this is the pointer to the data to add
Returns:
EFI_SUCCESS - Update success.
Other - Update fail.
--*/
{
EFI_HII_PACKAGE_INSTANCE *PackageInstance;
EFI_HII_DATA *HiiData;
EFI_HII_HANDLE_DATABASE *HandleDatabase;
EFI_HII_IFR_PACK *FormPack;
EFI_IFR_OP_HEADER *Location;
EFI_IFR_OP_HEADER *DataLocation;
UINT8 *OtherBuffer;
UINT8 *TempBuffer;
UINT8 *OrigTempBuffer;
UINTN TempBufferSize;
UINTN Index;
OtherBuffer = NULL;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
HiiData = EFI_HII_DATA_FROM_THIS (This);
HandleDatabase = HiiData->DatabaseHead;
PackageInstance = NULL;
//
// Check numeric value against the head of the database
//
for (; HandleDatabase != NULL; HandleDatabase = HandleDatabase->NextHandleDatabase) {
//
// Match the numeric value with the database entry - if matched, extract PackageInstance
//
if (Handle == HandleDatabase->Handle) {
PackageInstance = HandleDatabase->Buffer;
break;
}
}
//
// No handle was found - error condition
//
if (PackageInstance == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// Calculate and allocate space for retrieval of IFR data
//
DataLocation = (EFI_IFR_OP_HEADER *) &Data->Data;
TempBufferSize = (CHAR8 *) (&PackageInstance->IfrData) - (CHAR8 *) (PackageInstance);
for (Index = 0; Index < Data->DataCount; Index++) {
TempBufferSize += DataLocation->Length;
DataLocation = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (DataLocation) + DataLocation->Length);
}
TempBufferSize += PackageInstance->IfrSize + PackageInstance->StringSize;
TempBuffer = AllocateZeroPool (TempBufferSize);
ASSERT (TempBuffer != NULL);
OrigTempBuffer = TempBuffer;
//
// We update only packages with IFR information in it
//
if (PackageInstance->IfrSize == 0) {
return EFI_INVALID_PARAMETER;
}
CopyMem (
TempBuffer,
PackageInstance,
((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER) - (CHAR8 *) (PackageInstance))
);
TempBuffer = TempBuffer + ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER) - (CHAR8 *) (PackageInstance));
//
// Based on if there is IFR data in this package instance, determine
// what the location is of the beginning of the string data.
//
FormPack = (EFI_HII_IFR_PACK *) ((CHAR8 *) (&PackageInstance->IfrData) + sizeof (EFI_HII_PACK_HEADER));
Location = (EFI_IFR_OP_HEADER *) FormPack;
//
// Look for the FormId requested
//
for (; Location->OpCode != EFI_IFR_END_FORM_SET_OP;) {
switch (Location->OpCode) {
case EFI_IFR_FORM_SET_OP:
//
// If the FormSet has an update pending, pay attention.
//
if (Data->FormSetUpdate) {
((EFI_IFR_FORM_SET *) Location)->CallbackHandle = Data->FormCallbackHandle;
}
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
break;
case EFI_IFR_FORM_OP:
//
// If the Form has an update pending, pay attention.
//
if (Data->FormUpdate) {
((EFI_IFR_FORM *) Location)->FormTitle = Data->FormTitle;
}
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
break;
case EFI_IFR_LABEL_OP:
//
// If the label does not match the requested update point, ignore it
//
if (((EFI_IFR_LABEL *) Location)->LabelId != Label) {
//
// Copy the label
//
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
//
// Go to the next Op-Code
//
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
continue;
}
if (AddData) {
//
// Copy the label
//
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
//
// Add the DataCount amount of opcodes to TempBuffer
//
DataLocation = (EFI_IFR_OP_HEADER *) &Data->Data;
for (Index = 0; Index < Data->DataCount; Index++) {
CopyMem (TempBuffer, DataLocation, DataLocation->Length);
((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->IfrSize += DataLocation->Length;
OtherBuffer = ((UINT8 *) &((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->StringSize + sizeof (UINTN));
CopyMem (OtherBuffer, &((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->IfrSize, 2);
TempBuffer = TempBuffer + DataLocation->Length;
DataLocation = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (DataLocation) + DataLocation->Length);
}
//
// Go to the next Op-Code
//
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
continue;
} else {
//
// Copy the label
//
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
//
// Remove the DataCount amount of opcodes unless we run into an end of form or a label
//
for (Index = 0; Index < Data->DataCount; Index++) {
//
// If we are about to skip an end form - bail out, since that is illegal
//
if ((Location->OpCode == EFI_IFR_END_FORM_OP) || (Location->OpCode == EFI_IFR_LABEL_OP)) {
break;
}
//
// By skipping Location entries, we are in effect not copying what was previously there
//
((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->IfrSize -= Location->Length;
OtherBuffer = ((UINT8 *) &((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->StringSize + sizeof (UINTN));
CopyMem (OtherBuffer, &((EFI_HII_PACKAGE_INSTANCE *) OrigTempBuffer)->IfrSize, 2);
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
}
}
default:
CopyMem (TempBuffer, Location, Location->Length);
TempBuffer = TempBuffer + Location->Length;
break;
}
//
// Go to the next Op-Code
//
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
}
//
// Copy the last op-code left behind from the for loop
//
CopyMem (TempBuffer, Location, Location->Length);
//
// Advance to beginning of strings and copy them
//
TempBuffer = TempBuffer + Location->Length;
Location = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (Location) + Location->Length);
CopyMem (TempBuffer, Location, PackageInstance->StringSize);
//
// Free the old buffer, and assign into our database the latest buffer
//
gBS->FreePool (HandleDatabase->Buffer);
HandleDatabase->Buffer = OrigTempBuffer;
return EFI_SUCCESS;
}
|