]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiHiiLib/HiiLib.c
87aeb947ae50134543b879e34b7516d3c58d4a3c
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiLib.c
1 /** @file
2 HII Library implementation that uses DXE protocols and services.
3
4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalHiiLib.h"
16
17 #define GUID_CONFIG_STRING_TYPE 0x00
18 #define NAME_CONFIG_STRING_TYPE 0x01
19 #define PATH_CONFIG_STRING_TYPE 0x02
20
21 #define ACTION_SET_DEFAUTL_VALUE 0x01
22 #define ACTION_VALIDATE_SETTING 0x02
23
24 #define HII_LIB_DEFAULT_VARSTORE_SIZE 0x200
25
26 typedef struct {
27 LIST_ENTRY Entry; // Link to Block array
28 UINT16 Offset;
29 UINT16 Width;
30 UINT8 OpCode;
31 UINT8 Scope;
32 } IFR_BLOCK_DATA;
33
34 typedef struct {
35 EFI_VARSTORE_ID VarStoreId;
36 UINT16 Size;
37 } IFR_VARSTORAGE_DATA;
38
39 //
40 // <ConfigHdr> Template
41 //
42 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR16 mConfigHdrTemplate[] = L"GUID=00000000000000000000000000000000&NAME=0000&PATH=00";
43
44 EFI_FORM_BROWSER2_PROTOCOL *mUefiFormBrowser2 = NULL;
45
46 //
47 // Template used to mark the end of a list of packages
48 //
49 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_HII_PACKAGE_HEADER mEndOfPakageList = {
50 sizeof (EFI_HII_PACKAGE_HEADER),
51 EFI_HII_PACKAGE_END
52 };
53
54 /**
55 Extract Hii package list GUID for given HII handle.
56
57 If HiiHandle could not be found in the HII database, then ASSERT.
58 If Guid is NULL, then ASSERT.
59
60 @param Handle Hii handle
61 @param Guid Package list GUID
62
63 @retval EFI_SUCCESS Successfully extract GUID from Hii database.
64
65 **/
66 EFI_STATUS
67 EFIAPI
68 InternalHiiExtractGuidFromHiiHandle (
69 IN EFI_HII_HANDLE Handle,
70 OUT EFI_GUID *Guid
71 )
72 {
73 EFI_STATUS Status;
74 UINTN BufferSize;
75 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
76
77 ASSERT (Guid != NULL);
78 ASSERT (Handle != NULL);
79
80 //
81 // Get HII PackageList
82 //
83 BufferSize = 0;
84 HiiPackageList = NULL;
85
86 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &BufferSize, HiiPackageList);
87 ASSERT (Status != EFI_NOT_FOUND);
88
89 if (Status == EFI_BUFFER_TOO_SMALL) {
90 HiiPackageList = AllocatePool (BufferSize);
91 ASSERT (HiiPackageList != NULL);
92
93 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &BufferSize, HiiPackageList);
94 }
95 if (EFI_ERROR (Status)) {
96 FreePool (HiiPackageList);
97 return Status;
98 }
99
100 //
101 // Extract GUID
102 //
103 CopyGuid (Guid, &HiiPackageList->PackageListGuid);
104
105 FreePool (HiiPackageList);
106
107 return EFI_SUCCESS;
108 }
109
110 /**
111 Registers a list of packages in the HII Database and returns the HII Handle
112 associated with that registration. If an HII Handle has already been registered
113 with the same PackageListGuid and DeviceHandle, then NULL is returned. If there
114 are not enough resources to perform the registration, then NULL is returned.
115 If an empty list of packages is passed in, then NULL is returned. If the size of
116 the list of package is 0, then NULL is returned.
117
118 The variable arguments are pointers which point to package header that defined
119 by UEFI VFR compiler and StringGather tool.
120
121 #pragma pack (push, 1)
122 typedef struct {
123 UINT32 BinaryLength;
124 EFI_HII_PACKAGE_HEADER PackageHeader;
125 } EDKII_AUTOGEN_PACKAGES_HEADER;
126 #pragma pack (pop)
127
128 @param[in] PackageListGuid The GUID of the package list.
129 @param[in] DeviceHandle If not NULL, the Device Handle on which
130 an instance of DEVICE_PATH_PROTOCOL is installed.
131 This Device Handle uniquely defines the device that
132 the added packages are associated with.
133 @param[in] ... The variable argument list that contains pointers
134 to packages terminated by a NULL.
135
136 @retval NULL A HII Handle has already been registered in the HII Database with
137 the same PackageListGuid and DeviceHandle.
138 @retval NULL The HII Handle could not be created.
139 @retval NULL An empty list of packages was passed in.
140 @retval NULL All packages are empty.
141 @retval Other The HII Handle associated with the newly registered package list.
142
143 **/
144 EFI_HII_HANDLE
145 EFIAPI
146 HiiAddPackages (
147 IN CONST EFI_GUID *PackageListGuid,
148 IN EFI_HANDLE DeviceHandle OPTIONAL,
149 ...
150 )
151 {
152 EFI_STATUS Status;
153 VA_LIST Args;
154 UINT32 *Package;
155 EFI_HII_PACKAGE_LIST_HEADER *PackageListHeader;
156 EFI_HII_HANDLE HiiHandle;
157 UINT32 Length;
158 UINT8 *Data;
159
160 ASSERT (PackageListGuid != NULL);
161
162 //
163 // Calculate the length of all the packages in the variable argument list
164 //
165 for (Length = 0, VA_START (Args, DeviceHandle); (Package = VA_ARG (Args, UINT32 *)) != NULL; ) {
166 Length += (ReadUnaligned32 (Package) - sizeof (UINT32));
167 }
168 VA_END (Args);
169
170 //
171 // If there are no packages in the variable argument list or all the packages
172 // are empty, then return a NULL HII Handle
173 //
174 if (Length == 0) {
175 return NULL;
176 }
177
178 //
179 // Add the length of the Package List Header and the terminating Package Header
180 //
181 Length += sizeof (EFI_HII_PACKAGE_LIST_HEADER) + sizeof (EFI_HII_PACKAGE_HEADER);
182
183 //
184 // Allocate the storage for the entire Package List
185 //
186 PackageListHeader = AllocateZeroPool (Length);
187
188 //
189 // If the Package List can not be allocated, then return a NULL HII Handle
190 //
191 if (PackageListHeader == NULL) {
192 return NULL;
193 }
194
195 //
196 // Fill in the GUID and Length of the Package List Header
197 //
198 CopyGuid (&PackageListHeader->PackageListGuid, PackageListGuid);
199 PackageListHeader->PackageLength = Length;
200
201 //
202 // Initialize a pointer to the beginning if the Package List data
203 //
204 Data = (UINT8 *)(PackageListHeader + 1);
205
206 //
207 // Copy the data from each package in the variable argument list
208 //
209 for (VA_START (Args, DeviceHandle); (Package = VA_ARG (Args, UINT32 *)) != NULL; ) {
210 Length = ReadUnaligned32 (Package) - sizeof (UINT32);
211 CopyMem (Data, Package + 1, Length);
212 Data += Length;
213 }
214 VA_END (Args);
215
216 //
217 // Append a package of type EFI_HII_PACKAGE_END to mark the end of the package list
218 //
219 CopyMem (Data, &mEndOfPakageList, sizeof (mEndOfPakageList));
220
221 //
222 // Register the package list with the HII Database
223 //
224 Status = gHiiDatabase->NewPackageList (
225 gHiiDatabase,
226 PackageListHeader,
227 DeviceHandle,
228 &HiiHandle
229 );
230 if (EFI_ERROR (Status)) {
231 HiiHandle = NULL;
232 }
233
234 //
235 // Free the allocated package list
236 //
237 FreePool (PackageListHeader);
238
239 //
240 // Return the new HII Handle
241 //
242 return HiiHandle;
243 }
244
245 /**
246 Removes a package list from the HII database.
247
248 If HiiHandle is NULL, then ASSERT.
249 If HiiHandle is not a valid EFI_HII_HANDLE in the HII database, then ASSERT.
250
251 @param[in] HiiHandle The handle that was previously registered in the HII database
252
253 **/
254 VOID
255 EFIAPI
256 HiiRemovePackages (
257 IN EFI_HII_HANDLE HiiHandle
258 )
259 {
260 EFI_STATUS Status;
261
262 ASSERT (HiiHandle != NULL);
263 Status = gHiiDatabase->RemovePackageList (gHiiDatabase, HiiHandle);
264 ASSERT_EFI_ERROR (Status);
265 }
266
267
268 /**
269 Retrieves the array of all the HII Handles or the HII handles of a specific
270 package list GUID in the HII Database.
271 This array is terminated with a NULL HII Handle.
272 This function allocates the returned array using AllocatePool().
273 The caller is responsible for freeing the array with FreePool().
274
275 @param[in] PackageListGuid An optional parameter that is used to request
276 HII Handles associated with a specific
277 Package List GUID. If this parameter is NULL,
278 then all the HII Handles in the HII Database
279 are returned. If this parameter is not NULL,
280 then zero or more HII Handles associated with
281 PackageListGuid are returned.
282
283 @retval NULL No HII handles were found in the HII database
284 @retval NULL The array of HII Handles could not be retrieved
285 @retval Other A pointer to the NULL terminated array of HII Handles
286
287 **/
288 EFI_HII_HANDLE *
289 EFIAPI
290 HiiGetHiiHandles (
291 IN CONST EFI_GUID *PackageListGuid OPTIONAL
292 )
293 {
294 EFI_STATUS Status;
295 UINTN HandleBufferLength;
296 EFI_HII_HANDLE TempHiiHandleBuffer;
297 EFI_HII_HANDLE *HiiHandleBuffer;
298 EFI_GUID Guid;
299 UINTN Index1;
300 UINTN Index2;
301
302 //
303 // Retrieve the size required for the buffer of all HII handles.
304 //
305 HandleBufferLength = 0;
306 Status = gHiiDatabase->ListPackageLists (
307 gHiiDatabase,
308 EFI_HII_PACKAGE_TYPE_ALL,
309 NULL,
310 &HandleBufferLength,
311 &TempHiiHandleBuffer
312 );
313
314 //
315 // If ListPackageLists() returns EFI_SUCCESS for a zero size,
316 // then there are no HII handles in the HII database. If ListPackageLists()
317 // returns an error other than EFI_BUFFER_TOO_SMALL, then there are no HII
318 // handles in the HII database.
319 //
320 if (Status != EFI_BUFFER_TOO_SMALL) {
321 //
322 // Return NULL if the size can not be retrieved, or if there are no HII
323 // handles in the HII Database
324 //
325 return NULL;
326 }
327
328 //
329 // Allocate the array of HII handles to hold all the HII Handles and a NULL terminator
330 //
331 HiiHandleBuffer = AllocateZeroPool (HandleBufferLength + sizeof (EFI_HII_HANDLE));
332 if (HiiHandleBuffer == NULL) {
333 //
334 // Return NULL if allocation fails.
335 //
336 return NULL;
337 }
338
339 //
340 // Retrieve the array of HII Handles in the HII Database
341 //
342 Status = gHiiDatabase->ListPackageLists (
343 gHiiDatabase,
344 EFI_HII_PACKAGE_TYPE_ALL,
345 NULL,
346 &HandleBufferLength,
347 HiiHandleBuffer
348 );
349 if (EFI_ERROR (Status)) {
350 //
351 // Free the buffer and return NULL if the HII handles can not be retrieved.
352 //
353 FreePool (HiiHandleBuffer);
354 return NULL;
355 }
356
357 if (PackageListGuid == NULL) {
358 //
359 // Return the NULL terminated array of HII handles in the HII Database
360 //
361 return HiiHandleBuffer;
362 } else {
363 for (Index1 = 0, Index2 = 0; HiiHandleBuffer[Index1] != NULL; Index1++) {
364 Status = InternalHiiExtractGuidFromHiiHandle (HiiHandleBuffer[Index1], &Guid);
365 ASSERT_EFI_ERROR (Status);
366 if (CompareGuid (&Guid, PackageListGuid)) {
367 HiiHandleBuffer[Index2++] = HiiHandleBuffer[Index1];
368 }
369 }
370 if (Index2 > 0) {
371 HiiHandleBuffer[Index2] = NULL;
372 return HiiHandleBuffer;
373 } else {
374 FreePool (HiiHandleBuffer);
375 return NULL;
376 }
377 }
378 }
379
380 /**
381 Converts all hex dtring characters in range ['A'..'F'] to ['a'..'f'] for
382 hex digits that appear between a '=' and a '&' in a config string.
383
384 If ConfigString is NULL, then ASSERT().
385
386 @param[in] ConfigString Pointer to a Null-terminated Unicode string.
387
388 @return Pointer to the Null-terminated Unicode result string.
389
390 **/
391 EFI_STRING
392 EFIAPI
393 InternalHiiLowerConfigString (
394 IN EFI_STRING ConfigString
395 )
396 {
397 EFI_STRING String;
398 BOOLEAN Lower;
399
400 ASSERT (ConfigString != NULL);
401
402 //
403 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
404 //
405 for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) {
406 if (*String == L'=') {
407 Lower = TRUE;
408 } else if (*String == L'&') {
409 Lower = FALSE;
410 } else if (Lower && *String >= L'A' && *String <= L'F') {
411 *String = (CHAR16) (*String - L'A' + L'a');
412 }
413 }
414
415 return ConfigString;
416 }
417
418 /**
419 Uses the BlockToConfig() service of the Config Routing Protocol to
420 convert <ConfigRequest> and a buffer to a <ConfigResp>
421
422 If ConfigRequest is NULL, then ASSERT().
423 If Block is NULL, then ASSERT().
424
425 @param[in] ConfigRequest Pointer to a Null-terminated Unicode string.
426 @param[in] Block Pointer to a block of data.
427 @param[in] BlockSize The zie, in bytes, of Block.
428
429 @retval NULL The <ConfigResp> string could not be generated.
430 @retval Other Pointer to the Null-terminated Unicode <ConfigResp> string.
431
432 **/
433 EFI_STRING
434 EFIAPI
435 InternalHiiBlockToConfig (
436 IN CONST EFI_STRING ConfigRequest,
437 IN CONST UINT8 *Block,
438 IN UINTN BlockSize
439 )
440 {
441 EFI_STATUS Status;
442 EFI_STRING ConfigResp;
443 CHAR16 *Progress;
444
445 ASSERT (ConfigRequest != NULL);
446 ASSERT (Block != NULL);
447
448 //
449 // Convert <ConfigRequest> to <ConfigResp>
450 //
451 Status = gHiiConfigRouting->BlockToConfig (
452 gHiiConfigRouting,
453 ConfigRequest,
454 Block,
455 BlockSize,
456 &ConfigResp,
457 &Progress
458 );
459 if (EFI_ERROR (Status)) {
460 return NULL;
461 }
462 return ConfigResp;
463 }
464
465 /**
466 Uses the BrowserCallback() service of the Form Browser Protocol to retrieve
467 or set uncommitted data. If sata i being retrieved, then the buffer is
468 allocated using AllocatePool(). The caller is then responsible for freeing
469 the buffer using FreePool().
470
471 @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional
472 parameter that may be NULL.
473 @param[in] VariableName Pointer to a Null-terminated Unicode string. This
474 is an optional parameter that may be NULL.
475 @param[in] SetResultsData If not NULL, then this parameter specified the buffer
476 of uncommited data to set. If this parameter is NULL,
477 then the caller is requesting to get the uncommited data
478 from the Form Browser.
479
480 @retval NULL The uncommitted data could not be retrieved.
481 @retval Other A pointer to a buffer containing the uncommitted data.
482
483 **/
484 EFI_STRING
485 EFIAPI
486 InternalHiiBrowserCallback (
487 IN CONST EFI_GUID *VariableGuid, OPTIONAL
488 IN CONST CHAR16 *VariableName, OPTIONAL
489 IN CONST EFI_STRING SetResultsData OPTIONAL
490 )
491 {
492 EFI_STATUS Status;
493 UINTN ResultsDataSize;
494 EFI_STRING ResultsData;
495 CHAR16 TempResultsData;
496
497 //
498 // Locate protocols
499 //
500 if (mUefiFormBrowser2 == NULL) {
501 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &mUefiFormBrowser2);
502 if (EFI_ERROR (Status) || mUefiFormBrowser2 == NULL) {
503 return NULL;
504 }
505 }
506
507 ResultsDataSize = 0;
508
509 if (SetResultsData != NULL) {
510 //
511 // Request to to set data in the uncommitted browser state information
512 //
513 ResultsData = SetResultsData;
514 } else {
515 //
516 // Retrieve the length of the buffer required ResultsData from the Browser Callback
517 //
518 Status = mUefiFormBrowser2->BrowserCallback (
519 mUefiFormBrowser2,
520 &ResultsDataSize,
521 &TempResultsData,
522 TRUE,
523 VariableGuid,
524 VariableName
525 );
526
527 if (!EFI_ERROR (Status)) {
528 //
529 // No Resluts Data, only allocate one char for '\0'
530 //
531 ResultsData = AllocateZeroPool (sizeof (CHAR16));
532 return ResultsData;
533 }
534
535 if (Status != EFI_BUFFER_TOO_SMALL) {
536 return NULL;
537 }
538
539 //
540 // Allocate the ResultsData buffer
541 //
542 ResultsData = AllocateZeroPool (ResultsDataSize);
543 if (ResultsData == NULL) {
544 return NULL;
545 }
546 }
547
548 //
549 // Retrieve or set the ResultsData from the Browser Callback
550 //
551 Status = mUefiFormBrowser2->BrowserCallback (
552 mUefiFormBrowser2,
553 &ResultsDataSize,
554 ResultsData,
555 (BOOLEAN)(SetResultsData == NULL),
556 VariableGuid,
557 VariableName
558 );
559 if (EFI_ERROR (Status)) {
560 return NULL;
561 }
562
563 return ResultsData;
564 }
565
566 /**
567 Allocates and returns a Null-terminated Unicode <ConfigHdr> string using routing
568 information that includes a GUID, an optional Unicode string name, and a device
569 path. The string returned is allocated with AllocatePool(). The caller is
570 responsible for freeing the allocated string with FreePool().
571
572 The format of a <ConfigHdr> is as follows:
573
574 GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize<Null>
575
576 @param[in] Guid Pointer to an EFI_GUID that is the routing information
577 GUID. Each of the 16 bytes in Guid is converted to
578 a 2 Unicode character hexidecimal string. This is
579 an optional parameter that may be NULL.
580 @param[in] Name Pointer to a Null-terminated Unicode string that is
581 the routing information NAME. This is an optional
582 parameter that may be NULL. Each 16-bit Unicode
583 character in Name is converted to a 4 character Unicode
584 hexidecimal string.
585 @param[in] DriverHandle The driver handle which supports a Device Path Protocol
586 that is the routing information PATH. Each byte of
587 the Device Path associated with DriverHandle is converted
588 to a 2 Unicode character hexidecimal string.
589
590 @retval NULL DriverHandle does not support the Device Path Protocol.
591 @retval Other A pointer to the Null-terminate Unicode <ConfigHdr> string
592
593 **/
594 EFI_STRING
595 EFIAPI
596 HiiConstructConfigHdr (
597 IN CONST EFI_GUID *Guid, OPTIONAL
598 IN CONST CHAR16 *Name, OPTIONAL
599 IN EFI_HANDLE DriverHandle
600 )
601 {
602 UINTN NameLength;
603 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
604 UINTN DevicePathSize;
605 CHAR16 *String;
606 CHAR16 *ReturnString;
607 UINTN Index;
608 UINT8 *Buffer;
609
610 //
611 // Compute the length of Name in Unicode characters.
612 // If Name is NULL, then the length is 0.
613 //
614 NameLength = 0;
615 if (Name != NULL) {
616 NameLength = StrLen (Name);
617 }
618
619 DevicePath = NULL;
620 DevicePathSize = 0;
621 //
622 // Retrieve DevicePath Protocol associated with DriverHandle
623 //
624 if (DriverHandle != NULL) {
625 DevicePath = DevicePathFromHandle (DriverHandle);
626 if (DevicePath == NULL) {
627 return NULL;
628 }
629 //
630 // Compute the size of the device path in bytes
631 //
632 DevicePathSize = GetDevicePathSize (DevicePath);
633 }
634
635 //
636 // GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize <Null>
637 // | 5 | sizeof (EFI_GUID) * 2 | 6 | NameStrLen*4 | 6 | DevicePathSize * 2 | 1 |
638 //
639 String = AllocateZeroPool ((5 + sizeof (EFI_GUID) * 2 + 6 + NameLength * 4 + 6 + DevicePathSize * 2 + 1) * sizeof (CHAR16));
640 if (String == NULL) {
641 return NULL;
642 }
643
644 //
645 // Start with L"GUID="
646 //
647 ReturnString = StrCpy (String, L"GUID=");
648 String += StrLen (String);
649
650 if (Guid != NULL) {
651 //
652 // Append Guid converted to <HexCh>32
653 //
654 for (Index = 0, Buffer = (UINT8 *)Guid; Index < sizeof (EFI_GUID); Index++) {
655 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);
656 }
657 }
658
659 //
660 // Append L"&NAME="
661 //
662 StrCpy (String, L"&NAME=");
663 String += StrLen (String);
664
665 if (Name != NULL) {
666 //
667 // Append Name converted to <Char>NameLength
668 //
669 for (; *Name != L'\0'; Name++) {
670 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *Name, 4);
671 }
672 }
673
674 //
675 // Append L"&PATH="
676 //
677 StrCpy (String, L"&PATH=");
678 String += StrLen (String);
679
680 //
681 // Append the device path associated with DriverHandle converted to <HexChar>DevicePathSize
682 //
683 for (Index = 0, Buffer = (UINT8 *)DevicePath; Index < DevicePathSize; Index++) {
684 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);
685 }
686
687 //
688 // Null terminate the Unicode string
689 //
690 *String = L'\0';
691
692 //
693 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
694 //
695 return InternalHiiLowerConfigString (ReturnString);
696 }
697
698 /**
699 Convert the hex UNICODE encoding string of UEFI GUID, NAME or device path
700 to binary buffer from <ConfigHdr>.
701
702 This is a internal function.
703
704 @param String UEFI configuration string.
705 @param Flag Flag specifies what type buffer will be retrieved.
706 @param Buffer Binary of Guid, Name or Device path.
707
708 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
709 @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures.
710 @retval EFI_SUCCESS The buffer data is retrieved and translated to
711 binary format.
712
713 **/
714 EFI_STATUS
715 InternalHiiGetBufferFromString (
716 IN EFI_STRING String,
717 IN UINT8 Flag,
718 OUT UINT8 **Buffer
719 )
720 {
721 UINTN Length;
722 EFI_STRING ConfigHdr;
723 CHAR16 *StringPtr;
724 UINT8 *DataBuffer;
725 CHAR16 TemStr[5];
726 UINTN Index;
727 UINT8 DigitUint8;
728
729 if (String == NULL || Buffer == NULL) {
730 return EFI_INVALID_PARAMETER;
731 }
732
733 DataBuffer = NULL;
734 StringPtr = NULL;
735 ConfigHdr = String;
736 //
737 // The content between 'GUID', 'NAME', 'PATH' of <ConfigHdr> and '&' of next element
738 // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding string.
739 //
740 for (Length = 0; *String != 0 && *String != L'&'; String++, Length++);
741
742 switch (Flag) {
743 case GUID_CONFIG_STRING_TYPE:
744 case PATH_CONFIG_STRING_TYPE:
745 //
746 // The data in <ConfigHdr> is encoded as hex UNICODE %02x bytes in the same order
747 // as the device path and Guid resides in RAM memory.
748 // Translate the data into binary.
749 //
750 DataBuffer = (UINT8 *) AllocateZeroPool ((Length + 1) / 2);
751 if (DataBuffer == NULL) {
752 return EFI_OUT_OF_RESOURCES;
753 }
754 //
755 // Convert binary byte one by one
756 //
757 ZeroMem (TemStr, sizeof (TemStr));
758 for (Index = 0; Index < Length; Index ++) {
759 TemStr[0] = ConfigHdr[Index];
760 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
761 if ((Index & 1) == 0) {
762 DataBuffer [Index/2] = DigitUint8;
763 } else {
764 DataBuffer [Index/2] = (UINT8) ((DataBuffer [Index/2] << 4) + DigitUint8);
765 }
766 }
767
768 *Buffer = DataBuffer;
769 break;
770
771 case NAME_CONFIG_STRING_TYPE:
772 //
773 // Convert Config String to Unicode String, e.g. "0041004200430044" => "ABCD"
774 //
775
776 //
777 // Add the tailling char L'\0'
778 //
779 DataBuffer = (UINT8 *) AllocateZeroPool ((Length/4 + 1) * sizeof (CHAR16));
780 if (DataBuffer == NULL) {
781 return EFI_OUT_OF_RESOURCES;
782 }
783 //
784 // Convert character one by one
785 //
786 StringPtr = (CHAR16 *) DataBuffer;
787 ZeroMem (TemStr, sizeof (TemStr));
788 for (Index = 0; Index < Length; Index += 4) {
789 StrnCpy (TemStr, ConfigHdr + Index, 4);
790 StringPtr[Index/4] = (CHAR16) StrHexToUint64 (TemStr);
791 }
792 //
793 // Add tailing L'\0' character
794 //
795 StringPtr[Index/4] = L'\0';
796
797 *Buffer = DataBuffer;
798 break;
799
800 default:
801 return EFI_INVALID_PARAMETER;
802 }
803
804 return EFI_SUCCESS;
805 }
806
807 /**
808 This function checks VarOffset and VarWidth is in the block range.
809
810 @param BlockArray The block array is to be checked.
811 @param VarOffset Offset of var to the structure
812 @param VarWidth Width of var.
813
814 @retval TRUE This Var is in the block range.
815 @retval FALSE This Var is not in the block range.
816 **/
817 BOOLEAN
818 BlockArrayCheck (
819 IN IFR_BLOCK_DATA *BlockArray,
820 IN UINT16 VarOffset,
821 IN UINT16 VarWidth
822 )
823 {
824 LIST_ENTRY *Link;
825 IFR_BLOCK_DATA *BlockData;
826
827 //
828 // No Request Block array, all vars are got.
829 //
830 if (BlockArray == NULL) {
831 return TRUE;
832 }
833
834 //
835 // Check the input var is in the request block range.
836 //
837 for (Link = BlockArray->Entry.ForwardLink; Link != &BlockArray->Entry; Link = Link->ForwardLink) {
838 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
839 if ((VarOffset >= BlockData->Offset) && ((VarOffset + VarWidth) <= (BlockData->Offset + BlockData->Width))) {
840 return TRUE;
841 }
842 }
843
844 return FALSE;
845 }
846
847 /**
848 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
849 or WIDTH or VALUE.
850 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
851
852 @param ValueString String in <BlockConfig> format and points to the
853 first character of <Number>.
854 @param ValueData The output value. Caller takes the responsibility
855 to free memory.
856 @param ValueLength Length of the <Number>, in characters.
857
858 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
859 structures.
860 @retval EFI_SUCCESS Value of <Number> is outputted in Number
861 successfully.
862
863 **/
864 EFI_STATUS
865 EFIAPI
866 InternalHiiGetValueOfNumber (
867 IN EFI_STRING ValueString,
868 OUT UINT8 **ValueData,
869 OUT UINTN *ValueLength
870 )
871 {
872 EFI_STRING StringPtr;
873 UINTN Length;
874 UINT8 *Buf;
875 UINT8 DigitUint8;
876 UINTN Index;
877 CHAR16 TemStr[2];
878
879 ASSERT (ValueString != NULL && ValueData != NULL && ValueLength != NULL);
880 ASSERT (*ValueString != L'\0');
881
882 //
883 // Get the length of value string
884 //
885 StringPtr = ValueString;
886 while (*StringPtr != L'\0' && *StringPtr != L'&') {
887 StringPtr++;
888 }
889 Length = StringPtr - ValueString;
890
891 //
892 // Allocate buffer to store the value
893 //
894 Buf = (UINT8 *) AllocateZeroPool ((Length + 1) / 2);
895 if (Buf == NULL) {
896 return EFI_OUT_OF_RESOURCES;
897 }
898
899 //
900 // Convert character one by one to the value buffer
901 //
902 ZeroMem (TemStr, sizeof (TemStr));
903 for (Index = 0; Index < Length; Index ++) {
904 TemStr[0] = ValueString[Length - Index - 1];
905 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
906 if ((Index & 1) == 0) {
907 Buf [Index/2] = DigitUint8;
908 } else {
909 Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);
910 }
911 }
912
913 //
914 // Set the converted value and string length.
915 //
916 *ValueData = Buf;
917 *ValueLength = Length;
918 return EFI_SUCCESS;
919 }
920
921 /**
922 Get value from config request resp string.
923
924 @param ConfigElement ConfigResp string contains the current setting.
925 @param VarName The variable name which need to get value.
926 @param VarValue The return value.
927
928 @retval EFI_SUCCESS Get the value for the VarName
929 @retval EFI_OUT_OF_RESOURCES The memory is not enough.
930 **/
931 EFI_STATUS
932 GetValueFromRequest (
933 IN CHAR16 *ConfigElement,
934 IN CHAR16 *VarName,
935 OUT UINT64 *VarValue
936 )
937 {
938 UINT8 *TmpBuffer;
939 CHAR16 *StringPtr;
940 UINTN Length;
941 EFI_STATUS Status;
942
943 //
944 // Find VarName related string.
945 //
946 StringPtr = StrStr (ConfigElement, VarName);
947 ASSERT (StringPtr != NULL);
948
949 //
950 // Skip the "VarName=" string
951 //
952 StringPtr += StrLen (VarName) + 1;
953
954 //
955 // Get Offset
956 //
957 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
958 if (EFI_ERROR (Status)) {
959 return Status;
960 }
961
962 *VarValue = 0;
963 CopyMem (VarValue, TmpBuffer, (((Length + 1) / 2) < sizeof (UINT64)) ? ((Length + 1) / 2) : sizeof (UINT64));
964
965 FreePool (TmpBuffer);
966
967 return EFI_SUCCESS;
968 }
969
970 /**
971 This internal function parses IFR data to validate current setting.
972
973 Base on the NameValueType, if it is TRUE, RequestElement and HiiHandle is valid;
974 else the VarBuffer and CurrentBlockArray is valid.
975
976 @param HiiPackageList Point to Hii package list.
977 @param PackageListLength The length of the pacakge.
978 @param VarGuid Guid of the buffer storage.
979 @param VarName Name of the buffer storage.
980 @param VarBuffer The data buffer for the storage.
981 @param CurrentBlockArray The block array from the config Requst string.
982 @param RequestElement The config string for this storage.
983 @param HiiHandle The HiiHandle for this formset.
984 @param NameValueType Whether current storage is name/value varstore or not.
985
986 @retval EFI_SUCCESS The current setting is valid.
987 @retval EFI_OUT_OF_RESOURCES The memory is not enough.
988 @retval EFI_INVALID_PARAMETER The config string or the Hii package is invalid.
989 **/
990 EFI_STATUS
991 ValidateQuestionFromVfr (
992 IN EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList,
993 IN UINTN PackageListLength,
994 IN EFI_GUID *VarGuid,
995 IN CHAR16 *VarName,
996 IN UINT8 *VarBuffer,
997 IN IFR_BLOCK_DATA *CurrentBlockArray,
998 IN CHAR16 *RequestElement,
999 IN EFI_HII_HANDLE HiiHandle,
1000 IN BOOLEAN NameValueType
1001 )
1002 {
1003 IFR_BLOCK_DATA VarBlockData;
1004 UINT8 *TmpBuffer;
1005 UINT16 Offset;
1006 UINT16 Width;
1007 UINT64 VarValue;
1008 EFI_IFR_TYPE_VALUE TmpValue;
1009 EFI_STATUS Status;
1010 EFI_HII_PACKAGE_HEADER PacakgeHeader;
1011 UINT32 PackageOffset;
1012 UINT8 *PackageData;
1013 UINTN IfrOffset;
1014 EFI_IFR_OP_HEADER *IfrOpHdr;
1015 EFI_IFR_VARSTORE *IfrVarStore;
1016 EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueStore;
1017 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1018 IFR_VARSTORAGE_DATA VarStoreData;
1019 EFI_IFR_ONE_OF *IfrOneOf;
1020 EFI_IFR_NUMERIC *IfrNumeric;
1021 EFI_IFR_ONE_OF_OPTION *IfrOneOfOption;
1022 EFI_IFR_CHECKBOX *IfrCheckBox;
1023 EFI_IFR_STRING *IfrString;
1024 CHAR8 *VarStoreName;
1025 UINTN Index;
1026 CHAR16 *QuestionName;
1027 CHAR16 *StringPtr;
1028
1029 //
1030 // Initialize the local variables.
1031 //
1032 Index = 0;
1033 VarStoreName = NULL;
1034 Status = EFI_SUCCESS;
1035 TmpBuffer = NULL;
1036 VarValue = 0;
1037 IfrVarStore = NULL;
1038 IfrNameValueStore = NULL;
1039 IfrEfiVarStore = NULL;
1040 ZeroMem (&VarStoreData, sizeof (IFR_VARSTORAGE_DATA));
1041 ZeroMem (&VarBlockData, sizeof (VarBlockData));
1042
1043 //
1044 // Check IFR value is in block data, then Validate Value
1045 //
1046 PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
1047 while (PackageOffset < PackageListLength) {
1048 CopyMem (&PacakgeHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PacakgeHeader));
1049
1050 //
1051 // Parse IFR opcode from the form package.
1052 //
1053 if (PacakgeHeader.Type == EFI_HII_PACKAGE_FORMS) {
1054 IfrOffset = sizeof (PacakgeHeader);
1055 PackageData = (UINT8 *) HiiPackageList + PackageOffset;
1056 while (IfrOffset < PacakgeHeader.Length) {
1057 IfrOpHdr = (EFI_IFR_OP_HEADER *) (PackageData + IfrOffset);
1058 //
1059 // Validate current setting to the value built in IFR opcode
1060 //
1061 switch (IfrOpHdr->OpCode) {
1062 case EFI_IFR_VARSTORE_OP:
1063 //
1064 // VarStoreId has been found. No further found.
1065 //
1066 if (VarStoreData.VarStoreId != 0) {
1067 break;
1068 }
1069 //
1070 // Find the matched VarStoreId to the input VarGuid and VarName
1071 //
1072 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
1073 if (CompareGuid ((EFI_GUID *) (VOID *) &IfrVarStore->Guid, VarGuid)) {
1074 VarStoreName = (CHAR8 *) IfrVarStore->Name;
1075 for (Index = 0; VarStoreName[Index] != 0; Index ++) {
1076 if ((CHAR16) VarStoreName[Index] != VarName[Index]) {
1077 break;
1078 }
1079 }
1080 //
1081 // The matched VarStore is found.
1082 //
1083 if ((VarStoreName[Index] != 0) || (VarName[Index] != 0)) {
1084 IfrVarStore = NULL;
1085 }
1086 } else {
1087 IfrVarStore = NULL;
1088 }
1089
1090 if (IfrVarStore != NULL) {
1091 VarStoreData.VarStoreId = IfrVarStore->VarStoreId;
1092 VarStoreData.Size = IfrVarStore->Size;
1093 }
1094 break;
1095 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1096 //
1097 // VarStoreId has been found. No further found.
1098 //
1099 if (VarStoreData.VarStoreId != 0) {
1100 break;
1101 }
1102 //
1103 // Find the matched VarStoreId to the input VarGuid
1104 //
1105 IfrNameValueStore = (EFI_IFR_VARSTORE_NAME_VALUE *) IfrOpHdr;
1106 if (!CompareGuid ((EFI_GUID *) (VOID *) &IfrNameValueStore->Guid, VarGuid)) {
1107 IfrNameValueStore = NULL;
1108 }
1109
1110 if (IfrNameValueStore != NULL) {
1111 VarStoreData.VarStoreId = IfrNameValueStore->VarStoreId;
1112 }
1113 break;
1114 case EFI_IFR_VARSTORE_EFI_OP:
1115 //
1116 // VarStore is found. Don't need to search any more.
1117 //
1118 if (VarStoreData.VarStoreId != 0) {
1119 break;
1120 }
1121
1122 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1123
1124 //
1125 // If the length is small than the structure, this is from old efi
1126 // varstore definition. Old efi varstore get config directly from
1127 // GetVariable function.
1128 //
1129 if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {
1130 break;
1131 }
1132
1133 if (CompareGuid ((EFI_GUID *) (VOID *) &IfrEfiVarStore->Guid, VarGuid)) {
1134 VarStoreName = (CHAR8 *) IfrEfiVarStore->Name;
1135 for (Index = 0; VarStoreName[Index] != 0; Index ++) {
1136 if ((CHAR16) VarStoreName[Index] != VarName[Index]) {
1137 break;
1138 }
1139 }
1140 //
1141 // The matched VarStore is found.
1142 //
1143 if ((VarStoreName[Index] != 0) || (VarName[Index] != 0)) {
1144 IfrEfiVarStore = NULL;
1145 }
1146 } else {
1147 IfrEfiVarStore = NULL;
1148 }
1149
1150 if (IfrEfiVarStore != NULL) {
1151 //
1152 // Find the matched VarStore
1153 //
1154 VarStoreData.VarStoreId = IfrEfiVarStore->VarStoreId;
1155 VarStoreData.Size = IfrEfiVarStore->Size;
1156 }
1157 break;
1158 case EFI_IFR_FORM_OP:
1159 case EFI_IFR_FORM_MAP_OP:
1160 //
1161 // Check the matched VarStoreId is found.
1162 //
1163 if (VarStoreData.VarStoreId == 0) {
1164 return EFI_SUCCESS;
1165 }
1166 break;
1167 case EFI_IFR_ONE_OF_OP:
1168 //
1169 // Check whether current value is the one of option.
1170 //
1171
1172 //
1173 // OneOf question is not in IFR Form. This IFR form is not valid.
1174 //
1175 if (VarStoreData.VarStoreId == 0) {
1176 return EFI_INVALID_PARAMETER;
1177 }
1178 //
1179 // Check whether this question is for the requested varstore.
1180 //
1181 IfrOneOf = (EFI_IFR_ONE_OF *) IfrOpHdr;
1182 if (IfrOneOf->Question.VarStoreId != VarStoreData.VarStoreId) {
1183 break;
1184 }
1185
1186 if (NameValueType) {
1187 QuestionName = HiiGetString (HiiHandle, IfrOneOf->Question.VarStoreInfo.VarName, NULL);
1188 ASSERT (QuestionName != NULL);
1189
1190 if (StrStr (RequestElement, QuestionName) == NULL) {
1191 //
1192 // This question is not in the current configuration string. Skip it.
1193 //
1194 break;
1195 }
1196
1197 Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue);
1198 if (EFI_ERROR (Status)) {
1199 return Status;
1200 }
1201 } else {
1202 //
1203 // Get Offset by Question header and Width by DataType Flags
1204 //
1205 Offset = IfrOneOf->Question.VarStoreInfo.VarOffset;
1206 Width = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));
1207 //
1208 // Check whether this question is in current block array.
1209 //
1210 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1211 //
1212 // This question is not in the current configuration string. Skip it.
1213 //
1214 break;
1215 }
1216 //
1217 // Check this var question is in the var storage
1218 //
1219 if ((Offset + Width) > VarStoreData.Size) {
1220 //
1221 // This question exceeds the var store size.
1222 //
1223 return EFI_INVALID_PARAMETER;
1224 }
1225
1226 //
1227 // Get the current value for oneof opcode
1228 //
1229 VarValue = 0;
1230 CopyMem (&VarValue, VarBuffer + Offset, Width);
1231 }
1232 //
1233 // Set Block Data, to be checked in the following Oneof option opcode.
1234 //
1235 VarBlockData.OpCode = IfrOpHdr->OpCode;
1236 VarBlockData.Scope = IfrOpHdr->Scope;
1237 break;
1238 case EFI_IFR_NUMERIC_OP:
1239 //
1240 // Check the current value is in the numeric range.
1241 //
1242
1243 //
1244 // Numeric question is not in IFR Form. This IFR form is not valid.
1245 //
1246 if (VarStoreData.VarStoreId == 0) {
1247 return EFI_INVALID_PARAMETER;
1248 }
1249 //
1250 // Check whether this question is for the requested varstore.
1251 //
1252 IfrNumeric = (EFI_IFR_NUMERIC *) IfrOpHdr;
1253 if (IfrNumeric->Question.VarStoreId != VarStoreData.VarStoreId) {
1254 break;
1255 }
1256
1257 if (NameValueType) {
1258 QuestionName = HiiGetString (HiiHandle, IfrNumeric->Question.VarStoreInfo.VarName, NULL);
1259 ASSERT (QuestionName != NULL);
1260
1261 if (StrStr (RequestElement, QuestionName) == NULL) {
1262 //
1263 // This question is not in the current configuration string. Skip it.
1264 //
1265 break;
1266 }
1267
1268 Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue);
1269 if (EFI_ERROR (Status)) {
1270 return Status;
1271 }
1272 } else {
1273 //
1274 // Get Offset by Question header and Width by DataType Flags
1275 //
1276 Offset = IfrNumeric->Question.VarStoreInfo.VarOffset;
1277 Width = (UINT16) (1 << (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE));
1278 //
1279 // Check whether this question is in current block array.
1280 //
1281 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1282 //
1283 // This question is not in the current configuration string. Skip it.
1284 //
1285 break;
1286 }
1287 //
1288 // Check this var question is in the var storage
1289 //
1290 if ((Offset + Width) > VarStoreData.Size) {
1291 //
1292 // This question exceeds the var store size.
1293 //
1294 return EFI_INVALID_PARAMETER;
1295 }
1296
1297 //
1298 // Check the current value is in the numeric range.
1299 //
1300 VarValue = 0;
1301 CopyMem (&VarValue, VarBuffer + Offset, Width);
1302 }
1303 switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) {
1304 case EFI_IFR_NUMERIC_SIZE_1:
1305 if ((UINT8) VarValue < IfrNumeric->data.u8.MinValue || (UINT8) VarValue > IfrNumeric->data.u8.MaxValue) {
1306 //
1307 // Not in the valid range.
1308 //
1309 return EFI_INVALID_PARAMETER;
1310 }
1311 break;
1312 case EFI_IFR_NUMERIC_SIZE_2:
1313 if ((UINT16) VarValue < IfrNumeric->data.u16.MinValue || (UINT16) VarValue > IfrNumeric->data.u16.MaxValue) {
1314 //
1315 // Not in the valid range.
1316 //
1317 return EFI_INVALID_PARAMETER;
1318 }
1319 break;
1320 case EFI_IFR_NUMERIC_SIZE_4:
1321 if ((UINT32) VarValue < IfrNumeric->data.u32.MinValue || (UINT32) VarValue > IfrNumeric->data.u32.MaxValue) {
1322 //
1323 // Not in the valid range.
1324 //
1325 return EFI_INVALID_PARAMETER;
1326 }
1327 break;
1328 case EFI_IFR_NUMERIC_SIZE_8:
1329 if ((UINT64) VarValue < IfrNumeric->data.u64.MinValue || (UINT64) VarValue > IfrNumeric->data.u64.MaxValue) {
1330 //
1331 // Not in the valid range.
1332 //
1333 return EFI_INVALID_PARAMETER;
1334 }
1335 break;
1336 }
1337
1338 break;
1339 case EFI_IFR_CHECKBOX_OP:
1340 //
1341 // Check value is BOOLEAN type, only 0 and 1 is valid.
1342 //
1343
1344 //
1345 // CheckBox question is not in IFR Form. This IFR form is not valid.
1346 //
1347 if (VarStoreData.VarStoreId == 0) {
1348 return EFI_INVALID_PARAMETER;
1349 }
1350
1351 //
1352 // Check whether this question is for the requested varstore.
1353 //
1354 IfrCheckBox = (EFI_IFR_CHECKBOX *) IfrOpHdr;
1355 if (IfrCheckBox->Question.VarStoreId != VarStoreData.VarStoreId) {
1356 break;
1357 }
1358
1359 if (NameValueType) {
1360 QuestionName = HiiGetString (HiiHandle, IfrCheckBox->Question.VarStoreInfo.VarName, NULL);
1361 ASSERT (QuestionName != NULL);
1362
1363 if (StrStr (RequestElement, QuestionName) == NULL) {
1364 //
1365 // This question is not in the current configuration string. Skip it.
1366 //
1367 break;
1368 }
1369
1370 Status = GetValueFromRequest (RequestElement, QuestionName, &VarValue);
1371 if (EFI_ERROR (Status)) {
1372 return Status;
1373 }
1374 } else {
1375 //
1376 // Get Offset by Question header
1377 //
1378 Offset = IfrCheckBox->Question.VarStoreInfo.VarOffset;
1379 Width = (UINT16) sizeof (BOOLEAN);
1380 //
1381 // Check whether this question is in current block array.
1382 //
1383 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1384 //
1385 // This question is not in the current configuration string. Skip it.
1386 //
1387 break;
1388 }
1389 //
1390 // Check this var question is in the var storage
1391 //
1392 if ((Offset + Width) > VarStoreData.Size) {
1393 //
1394 // This question exceeds the var store size.
1395 //
1396 return EFI_INVALID_PARAMETER;
1397 }
1398 //
1399 // Check the current value is in the numeric range.
1400 //
1401 VarValue = 0;
1402 CopyMem (&VarValue, VarBuffer + Offset, Width);
1403 }
1404 //
1405 // Boolean type, only 1 and 0 is valid.
1406 //
1407 if (VarValue > 1) {
1408 return EFI_INVALID_PARAMETER;
1409 }
1410 break;
1411 case EFI_IFR_STRING_OP:
1412 //
1413 // Check current string length is less than maxsize
1414 //
1415
1416 //
1417 // CheckBox question is not in IFR Form. This IFR form is not valid.
1418 //
1419 if (VarStoreData.VarStoreId == 0) {
1420 return EFI_INVALID_PARAMETER;
1421 }
1422
1423 //
1424 // Check whether this question is for the requested varstore.
1425 //
1426 IfrString = (EFI_IFR_STRING *) IfrOpHdr;
1427 if (IfrString->Question.VarStoreId != VarStoreData.VarStoreId) {
1428 break;
1429 }
1430 //
1431 // Get Width by OneOf Flags
1432 //
1433 Width = (UINT16) (IfrString->MaxSize * sizeof (UINT16));
1434 if (NameValueType) {
1435 QuestionName = HiiGetString (HiiHandle, IfrString->Question.VarStoreInfo.VarName, NULL);
1436 ASSERT (QuestionName != NULL);
1437
1438 StringPtr = StrStr (RequestElement, QuestionName);
1439 if (StringPtr == NULL) {
1440 //
1441 // This question is not in the current configuration string. Skip it.
1442 //
1443 break;
1444 }
1445
1446 //
1447 // Skip the "=".
1448 //
1449 StringPtr += 1;
1450
1451 //
1452 // Check current string length is less than maxsize
1453 //
1454 if (StrSize (StringPtr) > Width) {
1455 return EFI_INVALID_PARAMETER;
1456 }
1457 } else {
1458 //
1459 // Get Offset/Width by Question header and OneOf Flags
1460 //
1461 Offset = IfrString->Question.VarStoreInfo.VarOffset;
1462 //
1463 // Check whether this question is in current block array.
1464 //
1465 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1466 //
1467 // This question is not in the current configuration string. Skip it.
1468 //
1469 break;
1470 }
1471 //
1472 // Check this var question is in the var storage
1473 //
1474 if ((Offset + Width) > VarStoreData.Size) {
1475 //
1476 // This question exceeds the var store size.
1477 //
1478 return EFI_INVALID_PARAMETER;
1479 }
1480
1481 //
1482 // Check current string length is less than maxsize
1483 //
1484 if (StrSize ((CHAR16 *) (VarBuffer + Offset)) > Width) {
1485 return EFI_INVALID_PARAMETER;
1486 }
1487 }
1488 break;
1489 case EFI_IFR_ONE_OF_OPTION_OP:
1490 //
1491 // Opcode Scope is zero. This one of option is not to be checked.
1492 //
1493 if (VarBlockData.Scope == 0) {
1494 break;
1495 }
1496
1497 //
1498 // Only check for OneOf and OrderList opcode
1499 //
1500 IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
1501 if (VarBlockData.OpCode == EFI_IFR_ONE_OF_OP) {
1502 //
1503 // Check current value is the value of one of option.
1504 //
1505 ASSERT (IfrOneOfOption->Type <= EFI_IFR_TYPE_NUM_SIZE_64);
1506 ZeroMem (&TmpValue, sizeof (EFI_IFR_TYPE_VALUE));
1507 CopyMem (&TmpValue, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));
1508 if (VarValue == TmpValue.u64) {
1509 //
1510 // The value is one of option value.
1511 // Set OpCode to Zero, don't need check again.
1512 //
1513 VarBlockData.OpCode = 0;
1514 }
1515 }
1516 break;
1517 case EFI_IFR_END_OP:
1518 //
1519 // Decrease opcode scope for the validated opcode
1520 //
1521 if (VarBlockData.Scope > 0) {
1522 VarBlockData.Scope --;
1523 }
1524
1525 //
1526 // OneOf value doesn't belong to one of option value.
1527 //
1528 if ((VarBlockData.Scope == 0) && (VarBlockData.OpCode == EFI_IFR_ONE_OF_OP)) {
1529 return EFI_INVALID_PARAMETER;
1530 }
1531 break;
1532 default:
1533 //
1534 // Increase Scope for the validated opcode
1535 //
1536 if (VarBlockData.Scope > 0) {
1537 VarBlockData.Scope = (UINT8) (VarBlockData.Scope + IfrOpHdr->Scope);
1538 }
1539 break;
1540 }
1541 //
1542 // Go to the next opcode
1543 //
1544 IfrOffset += IfrOpHdr->Length;
1545 }
1546 //
1547 // Only one form is in a package list.
1548 //
1549 break;
1550 }
1551
1552 //
1553 // Go to next package.
1554 //
1555 PackageOffset += PacakgeHeader.Length;
1556 }
1557
1558 return EFI_SUCCESS;
1559 }
1560
1561 /**
1562 This internal function parses IFR data to validate current setting.
1563
1564 @param ConfigElement ConfigResp element string contains the current setting.
1565 @param CurrentBlockArray Current block array.
1566 @param VarBuffer Data buffer for this varstore.
1567
1568 @retval EFI_SUCCESS The current setting is valid.
1569 @retval EFI_OUT_OF_RESOURCES The memory is not enough.
1570 @retval EFI_INVALID_PARAMETER The config string or the Hii package is invalid.
1571 **/
1572 EFI_STATUS
1573 GetBlockDataInfo (
1574 IN CHAR16 *ConfigElement,
1575 OUT IFR_BLOCK_DATA **CurrentBlockArray,
1576 OUT UINT8 **VarBuffer
1577 )
1578 {
1579 IFR_BLOCK_DATA *BlockData;
1580 IFR_BLOCK_DATA *NewBlockData;
1581 EFI_STRING StringPtr;
1582 UINTN Length;
1583 UINT8 *TmpBuffer;
1584 UINT16 Offset;
1585 UINT16 Width;
1586 LIST_ENTRY *Link;
1587 UINTN MaxBufferSize;
1588 EFI_STATUS Status;
1589 CHAR8 *VarStoreName;
1590 UINTN Index;
1591 IFR_BLOCK_DATA *BlockArray;
1592 UINT8 *DataBuffer;
1593
1594 //
1595 // Initialize the local variables.
1596 //
1597 Index = 0;
1598 VarStoreName = NULL;
1599 Status = EFI_SUCCESS;
1600 BlockData = NULL;
1601 NewBlockData = NULL;
1602 TmpBuffer = NULL;
1603 BlockArray = NULL;
1604 MaxBufferSize = HII_LIB_DEFAULT_VARSTORE_SIZE;
1605 DataBuffer = AllocateZeroPool (MaxBufferSize);
1606 if (DataBuffer == NULL) {
1607 return EFI_OUT_OF_RESOURCES;
1608 }
1609
1610 //
1611 // Init BlockArray
1612 //
1613 BlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1614 if (BlockArray == NULL) {
1615 Status = EFI_OUT_OF_RESOURCES;
1616 goto Done;
1617 }
1618 InitializeListHead (&BlockArray->Entry);
1619
1620 StringPtr = StrStr (ConfigElement, L"&OFFSET=");
1621 ASSERT (StringPtr != NULL);
1622
1623 //
1624 // Parse each <RequestElement> if exists
1625 // Only <BlockName> format is supported by this help function.
1626 // <BlockName> ::= &'OFFSET='<Number>&'WIDTH='<Number>
1627 //
1628 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
1629 //
1630 // Skip the &OFFSET= string
1631 //
1632 StringPtr += StrLen (L"&OFFSET=");
1633
1634 //
1635 // Get Offset
1636 //
1637 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1638 if (EFI_ERROR (Status)) {
1639 goto Done;
1640 }
1641 Offset = 0;
1642 CopyMem (
1643 &Offset,
1644 TmpBuffer,
1645 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1646 );
1647 FreePool (TmpBuffer);
1648 TmpBuffer = NULL;
1649
1650 StringPtr += Length;
1651 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
1652 Status = EFI_INVALID_PARAMETER;
1653 goto Done;
1654 }
1655 StringPtr += StrLen (L"&WIDTH=");
1656
1657 //
1658 // Get Width
1659 //
1660 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1661 if (EFI_ERROR (Status)) {
1662 goto Done;
1663 }
1664 Width = 0;
1665 CopyMem (
1666 &Width,
1667 TmpBuffer,
1668 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1669 );
1670 FreePool (TmpBuffer);
1671 TmpBuffer = NULL;
1672
1673 StringPtr += Length;
1674 if (*StringPtr != 0 && *StringPtr != L'&') {
1675 Status = EFI_INVALID_PARAMETER;
1676 goto Done;
1677 }
1678
1679 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
1680 Status = EFI_INVALID_PARAMETER;
1681 goto Done;
1682 }
1683 StringPtr += StrLen (L"&VALUE=");
1684
1685 //
1686 // Get Value
1687 //
1688 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1689 if (EFI_ERROR (Status)) {
1690 goto Done;
1691 }
1692
1693 StringPtr += Length;
1694 if (*StringPtr != 0 && *StringPtr != L'&') {
1695 Status = EFI_INVALID_PARAMETER;
1696 goto Done;
1697 }
1698
1699 //
1700 // Check whether VarBuffer is enough
1701 //
1702 if ((UINTN) (Offset + Width) > MaxBufferSize) {
1703 DataBuffer = ReallocatePool (
1704 MaxBufferSize,
1705 Offset + Width + HII_LIB_DEFAULT_VARSTORE_SIZE,
1706 DataBuffer
1707 );
1708 if (DataBuffer == NULL) {
1709 Status = EFI_OUT_OF_RESOURCES;
1710 goto Done;
1711 }
1712 MaxBufferSize = Offset + Width + HII_LIB_DEFAULT_VARSTORE_SIZE;
1713 }
1714
1715 //
1716 // Update the Block with configuration info
1717 //
1718 CopyMem (DataBuffer + Offset, TmpBuffer, Width);
1719 FreePool (TmpBuffer);
1720 TmpBuffer = NULL;
1721
1722 //
1723 // Set new Block Data
1724 //
1725 NewBlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1726 if (NewBlockData == NULL) {
1727 Status = EFI_OUT_OF_RESOURCES;
1728 goto Done;
1729 }
1730 NewBlockData->Offset = Offset;
1731 NewBlockData->Width = Width;
1732
1733 //
1734 // Insert the new block data into the block data array.
1735 //
1736 for (Link = BlockArray->Entry.ForwardLink; Link != &BlockArray->Entry; Link = Link->ForwardLink) {
1737 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1738 if (NewBlockData->Offset == BlockData->Offset) {
1739 if (NewBlockData->Width > BlockData->Width) {
1740 BlockData->Width = NewBlockData->Width;
1741 }
1742 FreePool (NewBlockData);
1743 break;
1744 } else if (NewBlockData->Offset < BlockData->Offset) {
1745 //
1746 // Insert new block data as the previous one of this link.
1747 //
1748 InsertTailList (Link, &NewBlockData->Entry);
1749 break;
1750 }
1751 }
1752
1753 //
1754 // Insert new block data into the array tail.
1755 //
1756 if (Link == &BlockArray->Entry) {
1757 InsertTailList (Link, &NewBlockData->Entry);
1758 }
1759
1760 //
1761 // If '\0', parsing is finished.
1762 //
1763 if (*StringPtr == 0) {
1764 break;
1765 }
1766 //
1767 // Go to next ConfigBlock
1768 //
1769 }
1770
1771 //
1772 // Merge the aligned block data into the single block data.
1773 //
1774 Link = BlockArray->Entry.ForwardLink;
1775 while ((Link != &BlockArray->Entry) && (Link->ForwardLink != &BlockArray->Entry)) {
1776 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1777 NewBlockData = BASE_CR (Link->ForwardLink, IFR_BLOCK_DATA, Entry);
1778 if ((NewBlockData->Offset >= BlockData->Offset) && (NewBlockData->Offset <= (BlockData->Offset + BlockData->Width))) {
1779 if ((NewBlockData->Offset + NewBlockData->Width) > (BlockData->Offset + BlockData->Width)) {
1780 BlockData->Width = (UINT16) (NewBlockData->Offset + NewBlockData->Width - BlockData->Offset);
1781 }
1782 RemoveEntryList (Link->ForwardLink);
1783 FreePool (NewBlockData);
1784 continue;
1785 }
1786 Link = Link->ForwardLink;
1787 }
1788
1789 *VarBuffer = DataBuffer;
1790 *CurrentBlockArray = BlockArray;
1791 return EFI_SUCCESS;
1792
1793 Done:
1794 if (DataBuffer != NULL) {
1795 FreePool (DataBuffer);
1796 }
1797
1798 if (BlockArray != NULL) {
1799 //
1800 // Free Link Array CurrentBlockArray
1801 //
1802 while (!IsListEmpty (&BlockArray->Entry)) {
1803 BlockData = BASE_CR (BlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
1804 RemoveEntryList (&BlockData->Entry);
1805 FreePool (BlockData);
1806 }
1807 FreePool (BlockArray);
1808 }
1809
1810 return Status;
1811 }
1812
1813 /**
1814 This internal function parses IFR data to validate current setting.
1815
1816 @param ConfigResp ConfigResp string contains the current setting.
1817 @param HiiPackageList Point to Hii package list.
1818 @param PackageListLength The length of the pacakge.
1819 @param VarGuid Guid of the buffer storage.
1820 @param VarName Name of the buffer storage.
1821 @param HiiHandle The HiiHandle for this package.
1822
1823 @retval EFI_SUCCESS The current setting is valid.
1824 @retval EFI_OUT_OF_RESOURCES The memory is not enough.
1825 @retval EFI_INVALID_PARAMETER The config string or the Hii package is invalid.
1826 **/
1827 EFI_STATUS
1828 EFIAPI
1829 InternalHiiValidateCurrentSetting (
1830 IN EFI_STRING ConfigResp,
1831 IN EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList,
1832 IN UINTN PackageListLength,
1833 IN EFI_GUID *VarGuid,
1834 IN CHAR16 *VarName,
1835 IN EFI_HII_HANDLE HiiHandle
1836 )
1837 {
1838 CHAR16 *StringPtr;
1839 EFI_STATUS Status;
1840 IFR_BLOCK_DATA *CurrentBlockArray;
1841 IFR_BLOCK_DATA *BlockData;
1842 UINT8 *VarBuffer;
1843 BOOLEAN NameValueType;
1844
1845 CurrentBlockArray = NULL;
1846 VarBuffer = NULL;
1847 StringPtr = NULL;
1848 Status = EFI_SUCCESS;
1849
1850 //
1851 // If StringPtr != NULL, get the request elements.
1852 //
1853 if (StrStr (ConfigResp, L"&OFFSET=") != NULL) {
1854 Status = GetBlockDataInfo(ConfigResp, &CurrentBlockArray, &VarBuffer);
1855 if (EFI_ERROR (Status)) {
1856 return Status;
1857 }
1858 NameValueType = FALSE;
1859 } else {
1860 //
1861 // Skip header part.
1862 //
1863 StringPtr = StrStr (ConfigResp, L"PATH=");
1864 ASSERT (StringPtr != NULL);
1865
1866 if (StrStr (StringPtr, L"&") != NULL) {
1867 NameValueType = TRUE;
1868 } else {
1869 //
1870 // Not found Request element, return success.
1871 //
1872 return EFI_SUCCESS;
1873 }
1874 }
1875
1876 Status = ValidateQuestionFromVfr(
1877 HiiPackageList,
1878 PackageListLength,
1879 VarGuid,
1880 VarName,
1881 VarBuffer,
1882 CurrentBlockArray,
1883 ConfigResp,
1884 HiiHandle,
1885 NameValueType
1886 );
1887
1888 if (VarBuffer != NULL) {
1889 FreePool (VarBuffer);
1890 }
1891
1892 if (CurrentBlockArray != NULL) {
1893 //
1894 // Free Link Array CurrentBlockArray
1895 //
1896 while (!IsListEmpty (&CurrentBlockArray->Entry)) {
1897 BlockData = BASE_CR (CurrentBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
1898 RemoveEntryList (&BlockData->Entry);
1899 FreePool (BlockData);
1900 }
1901 FreePool (CurrentBlockArray);
1902 }
1903
1904 return Status;
1905 }
1906
1907 /**
1908 Check whether the ConfigRequest string has the request elements.
1909 For EFI_HII_VARSTORE_BUFFER type, the request has "&OFFSET=****&WIDTH=****..." format.
1910 For EFI_HII_VARSTORE_NAME_VALUE type, the request has "&NAME1**&NAME2..." format.
1911
1912 @param ConfigRequest The input config request string.
1913
1914 @retval TRUE The input include config request elements.
1915 @retval FALSE The input string not includes.
1916
1917 **/
1918 BOOLEAN
1919 GetElementsFromRequest (
1920 IN EFI_STRING ConfigRequest
1921 )
1922 {
1923 EFI_STRING TmpRequest;
1924
1925 TmpRequest = StrStr (ConfigRequest, L"PATH=");
1926 ASSERT (TmpRequest != NULL);
1927
1928 if ((StrStr (TmpRequest, L"&OFFSET=") != NULL) || (StrStr (TmpRequest, L"&") != NULL)) {
1929 return TRUE;
1930 }
1931
1932 return FALSE;
1933 }
1934
1935 /**
1936 This function parses the input ConfigRequest string and its matched IFR code
1937 string for setting default value and validating current setting.
1938
1939 1. For setting default action, Reset the default value specified by DefaultId
1940 to the driver configuration got by Request string.
1941 2. For validating current setting, Validate the current configuration
1942 by parsing HII form IFR opcode.
1943
1944 NULL request string support depends on the ExportConfig interface of
1945 HiiConfigRouting protocol in UEFI specification.
1946
1947 @param Request A null-terminated Unicode string in
1948 <MultiConfigRequest> format. It can be NULL.
1949 If it is NULL, all current configuration for the
1950 entirety of the current HII database will be validated.
1951 If it is NULL, all configuration for the
1952 entirety of the current HII database will be reset.
1953 @param DefaultId Specifies the type of defaults to retrieve only for setting default action.
1954 @param ActionType Action supports setting defaults and validate current setting.
1955
1956 @retval TURE Action runs successfully.
1957 @retval FALSE Action is not valid or Action can't be executed successfully..
1958 **/
1959 BOOLEAN
1960 EFIAPI
1961 InternalHiiIfrValueAction (
1962 IN CONST EFI_STRING Request, OPTIONAL
1963 IN UINT16 DefaultId,
1964 IN UINT8 ActionType
1965 )
1966 {
1967 EFI_STRING ConfigAltResp;
1968 EFI_STRING ConfigAltHdr;
1969 EFI_STRING ConfigResp;
1970 EFI_STRING Progress;
1971 EFI_STRING StringPtr;
1972 EFI_STRING StringHdr;
1973 EFI_STATUS Status;
1974 EFI_HANDLE DriverHandle;
1975 EFI_HANDLE TempDriverHandle;
1976 EFI_HII_HANDLE *HiiHandleBuffer;
1977 EFI_HII_HANDLE HiiHandle;
1978 UINT32 Index;
1979 EFI_GUID *VarGuid;
1980 EFI_STRING VarName;
1981
1982 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
1983 UINTN PackageListLength;
1984 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1985 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1986
1987 ConfigAltResp = NULL;
1988 ConfigResp = NULL;
1989 VarGuid = NULL;
1990 VarName = NULL;
1991 DevicePath = NULL;
1992 ConfigAltHdr = NULL;
1993 HiiHandleBuffer = NULL;
1994 Index = 0;
1995 TempDriverHandle = NULL;
1996 HiiHandle = NULL;
1997 HiiPackageList = NULL;
1998
1999 //
2000 // Only support set default and validate setting action.
2001 //
2002 if ((ActionType != ACTION_SET_DEFAUTL_VALUE) && (ActionType != ACTION_VALIDATE_SETTING)) {
2003 return FALSE;
2004 }
2005
2006 //
2007 // Get the full requested value and deault value string.
2008 //
2009 if (Request != NULL) {
2010 Status = gHiiConfigRouting->ExtractConfig (
2011 gHiiConfigRouting,
2012 Request,
2013 &Progress,
2014 &ConfigAltResp
2015 );
2016 } else {
2017 Status = gHiiConfigRouting->ExportConfig (
2018 gHiiConfigRouting,
2019 &ConfigAltResp
2020 );
2021 }
2022
2023 if (EFI_ERROR (Status)) {
2024 return FALSE;
2025 }
2026
2027 StringPtr = ConfigAltResp;
2028
2029 while (StringPtr != L'\0') {
2030 //
2031 // 1. Find <ConfigHdr> GUID=...&NAME=...&PATH=...
2032 //
2033 StringHdr = StringPtr;
2034
2035 //
2036 // Get Guid value
2037 //
2038 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
2039 Status = EFI_INVALID_PARAMETER;
2040 goto Done;
2041 }
2042 StringPtr += StrLen (L"GUID=");
2043 Status = InternalHiiGetBufferFromString (StringPtr, GUID_CONFIG_STRING_TYPE, (UINT8 **) &VarGuid);
2044 if (EFI_ERROR (Status)) {
2045 goto Done;
2046 }
2047
2048 //
2049 // Get Name value VarName
2050 //
2051 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
2052 StringPtr++;
2053 }
2054 if (*StringPtr == L'\0') {
2055 Status = EFI_INVALID_PARAMETER;
2056 goto Done;
2057 }
2058 StringPtr += StrLen (L"&NAME=");
2059 Status = InternalHiiGetBufferFromString (StringPtr, NAME_CONFIG_STRING_TYPE, (UINT8 **) &VarName);
2060 if (EFI_ERROR (Status)) {
2061 goto Done;
2062 }
2063
2064 //
2065 // Get Path value DevicePath
2066 //
2067 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
2068 StringPtr++;
2069 }
2070 if (*StringPtr == L'\0') {
2071 Status = EFI_INVALID_PARAMETER;
2072 goto Done;
2073 }
2074 StringPtr += StrLen (L"&PATH=");
2075 Status = InternalHiiGetBufferFromString (StringPtr, PATH_CONFIG_STRING_TYPE, (UINT8 **) &DevicePath);
2076 if (EFI_ERROR (Status)) {
2077 goto Done;
2078 }
2079
2080 //
2081 // Get the Driver handle by the got device path.
2082 //
2083 TempDevicePath = DevicePath;
2084 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDevicePath, &DriverHandle);
2085 if (EFI_ERROR (Status)) {
2086 goto Done;
2087 }
2088
2089 //
2090 // Find the matched Hii Handle for the found Driver handle
2091 //
2092 HiiHandleBuffer = HiiGetHiiHandles (NULL);
2093 if (HiiHandleBuffer == NULL) {
2094 Status = EFI_NOT_FOUND;
2095 goto Done;
2096 }
2097
2098 for (Index = 0; HiiHandleBuffer[Index] != NULL; Index ++) {
2099 gHiiDatabase->GetPackageListHandle (gHiiDatabase, HiiHandleBuffer[Index], &TempDriverHandle);
2100 if (TempDriverHandle == DriverHandle) {
2101 break;
2102 }
2103 }
2104
2105 HiiHandle = HiiHandleBuffer[Index];
2106 FreePool (HiiHandleBuffer);
2107
2108 if (HiiHandle == NULL) {
2109 //
2110 // This request string has no its Hii package.
2111 // Its default value and validating can't execute by parsing IFR data.
2112 // Directly jump into the next ConfigAltResp string for another pair Guid, Name, and Path.
2113 //
2114 Status = EFI_SUCCESS;
2115 goto NextConfigAltResp;
2116 }
2117
2118 //
2119 // 2. Get HiiPackage by HiiHandle
2120 //
2121 PackageListLength = 0;
2122 HiiPackageList = NULL;
2123 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &PackageListLength, HiiPackageList);
2124
2125 //
2126 // The return status should always be EFI_BUFFER_TOO_SMALL as input buffer's size is 0.
2127 //
2128 if (Status != EFI_BUFFER_TOO_SMALL) {
2129 Status = EFI_INVALID_PARAMETER;
2130 goto Done;
2131 }
2132
2133 HiiPackageList = AllocatePool (PackageListLength);
2134 if (HiiPackageList == NULL) {
2135 Status = EFI_OUT_OF_RESOURCES;
2136 goto Done;
2137 }
2138
2139 //
2140 // Get PackageList on HiiHandle
2141 //
2142 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &PackageListLength, HiiPackageList);
2143 if (EFI_ERROR (Status)) {
2144 goto Done;
2145 }
2146
2147 //
2148 // 3. Call ConfigRouting GetAltCfg(ConfigRoute, <ConfigResponse>, Guid, Name, DevicePath, AltCfgId, AltCfgResp)
2149 // Get the default configuration string according to the default ID.
2150 //
2151 Status = gHiiConfigRouting->GetAltConfig (
2152 gHiiConfigRouting,
2153 ConfigAltResp,
2154 VarGuid,
2155 VarName,
2156 DevicePath,
2157 (ActionType == ACTION_SET_DEFAUTL_VALUE) ? &DefaultId:NULL, // it can be NULL to get the current setting.
2158 &ConfigResp
2159 );
2160
2161 //
2162 // The required setting can't be found. So, it is not required to be validated and set.
2163 //
2164 if (EFI_ERROR (Status)) {
2165 Status = EFI_SUCCESS;
2166 goto NextConfigAltResp;
2167 }
2168 //
2169 // Only the ConfigHdr is found. Not any block data is found. No data is required to be validated and set.
2170 //
2171 if (!GetElementsFromRequest (ConfigResp)) {
2172 goto NextConfigAltResp;
2173 }
2174
2175 //
2176 // 4. Set the default configuration information or Validate current setting by parse IFR code.
2177 // Current Setting is in ConfigResp, will be set into buffer, then check it again.
2178 //
2179 if (ActionType == ACTION_SET_DEFAUTL_VALUE) {
2180 //
2181 // Set the default configuration information.
2182 //
2183 Status = gHiiConfigRouting->RouteConfig (gHiiConfigRouting, ConfigResp, &Progress);
2184 } else {
2185 //
2186 // Current Setting is in ConfigResp, will be set into buffer, then check it again.
2187 //
2188 Status = InternalHiiValidateCurrentSetting (ConfigResp, HiiPackageList, PackageListLength, VarGuid, VarName, HiiHandle);
2189 }
2190
2191 if (EFI_ERROR (Status)) {
2192 goto Done;
2193 }
2194
2195 NextConfigAltResp:
2196 //
2197 // Free the allocated pacakge buffer and the got ConfigResp string.
2198 //
2199 if (HiiPackageList != NULL) {
2200 FreePool (HiiPackageList);
2201 HiiPackageList = NULL;
2202 }
2203
2204 if (ConfigResp != NULL) {
2205 FreePool (ConfigResp);
2206 ConfigResp = NULL;
2207 }
2208
2209 //
2210 // Free the allocated buffer.
2211 //
2212 FreePool (VarGuid);
2213 VarGuid = NULL;
2214
2215 FreePool (VarName);
2216 VarName = NULL;
2217
2218 FreePool (DevicePath);
2219 DevicePath = NULL;
2220
2221 //
2222 // 5. Jump to next ConfigAltResp for another Guid, Name, Path.
2223 //
2224
2225 //
2226 // Get and Skip ConfigHdr
2227 //
2228 while (*StringPtr != L'\0' && *StringPtr != L'&') {
2229 StringPtr++;
2230 }
2231 if (*StringPtr == L'\0') {
2232 break;
2233 }
2234
2235 //
2236 // Construct ConfigAltHdr string "&<ConfigHdr>&ALTCFG=\0"
2237 // | 1 | StrLen (ConfigHdr) | 8 | 1 |
2238 //
2239 ConfigAltHdr = AllocateZeroPool ((1 + StringPtr - StringHdr + 8 + 1) * sizeof (CHAR16));
2240 if (ConfigAltHdr == NULL) {
2241 Status = EFI_OUT_OF_RESOURCES;
2242 goto Done;
2243 }
2244 StrCpy (ConfigAltHdr, L"&");
2245 StrnCat (ConfigAltHdr, StringHdr, StringPtr - StringHdr);
2246 StrCat (ConfigAltHdr, L"&ALTCFG=");
2247
2248 //
2249 // Skip all AltResp (AltConfigHdr ConfigBody) for the same ConfigHdr
2250 //
2251 while ((StringHdr = StrStr (StringPtr, ConfigAltHdr)) != NULL) {
2252 StringPtr = StringHdr + StrLen (ConfigAltHdr);
2253 if (*StringPtr == L'\0') {
2254 break;
2255 }
2256 }
2257
2258 //
2259 // Free the allocated ConfigAltHdr string
2260 //
2261 FreePool (ConfigAltHdr);
2262 if (*StringPtr == L'\0') {
2263 break;
2264 }
2265
2266 //
2267 // Find &GUID as the next ConfigHdr
2268 //
2269 StringPtr = StrStr (StringPtr, L"&GUID");
2270 if (StringPtr == NULL) {
2271 break;
2272 }
2273
2274 //
2275 // Skip char '&'
2276 //
2277 StringPtr ++;
2278 }
2279
2280 Done:
2281 if (VarGuid != NULL) {
2282 FreePool (VarGuid);
2283 }
2284
2285 if (VarName != NULL) {
2286 FreePool (VarName);
2287 }
2288
2289 if (DevicePath != NULL) {
2290 FreePool (DevicePath);
2291 }
2292
2293 if (ConfigResp != NULL) {
2294 FreePool (ConfigResp);
2295 }
2296
2297 if (ConfigAltResp != NULL) {
2298 FreePool (ConfigAltResp);
2299 }
2300
2301 if (HiiPackageList != NULL) {
2302 FreePool (HiiPackageList);
2303 }
2304
2305 if (EFI_ERROR (Status)) {
2306 return FALSE;
2307 }
2308
2309 return TRUE;
2310 }
2311
2312 /**
2313 Validate the current configuration by parsing HII form IFR opcode.
2314
2315 NULL request string support depends on the ExportConfig interface of
2316 HiiConfigRouting protocol in UEFI specification.
2317
2318 @param Request A null-terminated Unicode string in
2319 <MultiConfigRequest> format. It can be NULL.
2320 If it is NULL, all current configuration for the
2321 entirety of the current HII database will be validated.
2322
2323 @retval TRUE Current configuration is valid.
2324 @retval FALSE Current configuration is invalid.
2325 **/
2326 BOOLEAN
2327 EFIAPI
2328 HiiValidateSettings (
2329 IN CONST EFI_STRING Request OPTIONAL
2330 )
2331 {
2332 return InternalHiiIfrValueAction (Request, 0, ACTION_VALIDATE_SETTING);
2333 }
2334
2335 /**
2336 Reset the default value specified by DefaultId to the driver
2337 configuration got by Request string.
2338
2339 NULL request string support depends on the ExportConfig interface of
2340 HiiConfigRouting protocol in UEFI specification.
2341
2342 @param Request A null-terminated Unicode string in
2343 <MultiConfigRequest> format. It can be NULL.
2344 If it is NULL, all configuration for the
2345 entirety of the current HII database will be reset.
2346 @param DefaultId Specifies the type of defaults to retrieve.
2347
2348 @retval TURE The default value is set successfully.
2349 @retval FALSE The default value can't be found and set.
2350 **/
2351 BOOLEAN
2352 EFIAPI
2353 HiiSetToDefaults (
2354 IN CONST EFI_STRING Request, OPTIONAL
2355 IN UINT16 DefaultId
2356 )
2357 {
2358 return InternalHiiIfrValueAction (Request, DefaultId, ACTION_SET_DEFAUTL_VALUE);
2359 }
2360
2361 /**
2362 Determines if two values in config strings match.
2363
2364 Compares the substring between StartSearchString and StopSearchString in
2365 FirstString to the substring between StartSearchString and StopSearchString
2366 in SecondString. If the two substrings match, then TRUE is returned. If the
2367 two substrings do not match, then FALSE is returned.
2368
2369 If FirstString is NULL, then ASSERT().
2370 If SecondString is NULL, then ASSERT().
2371 If StartSearchString is NULL, then ASSERT().
2372 If StopSearchString is NULL, then ASSERT().
2373
2374 @param FirstString Pointer to the first Null-terminated Unicode string.
2375 @param SecondString Pointer to the second Null-terminated Unicode string.
2376 @param StartSearchString Pointer to the Null-terminated Unicode string that
2377 marks the start of the value string to compare.
2378 @param StopSearchString Pointer to the Null-terminated Unicode string that
2379 marks the end of the value string to compare.
2380
2381 @retval FALSE StartSearchString is not present in FirstString.
2382 @retval FALSE StartSearchString is not present in SecondString.
2383 @retval FALSE StopSearchString is not present in FirstString.
2384 @retval FALSE StopSearchString is not present in SecondString.
2385 @retval FALSE The length of the substring in FirstString is not the
2386 same length as the substring in SecondString.
2387 @retval FALSE The value string in FirstString does not matche the
2388 value string in SecondString.
2389 @retval TRUE The value string in FirstString matches the value
2390 string in SecondString.
2391
2392 **/
2393 BOOLEAN
2394 EFIAPI
2395 InternalHiiCompareSubString (
2396 IN CHAR16 *FirstString,
2397 IN CHAR16 *SecondString,
2398 IN CHAR16 *StartSearchString,
2399 IN CHAR16 *StopSearchString
2400 )
2401 {
2402 CHAR16 *EndFirstString;
2403 CHAR16 *EndSecondString;
2404
2405 ASSERT (FirstString != NULL);
2406 ASSERT (SecondString != NULL);
2407 ASSERT (StartSearchString != NULL);
2408 ASSERT (StopSearchString != NULL);
2409
2410 FirstString = StrStr (FirstString, StartSearchString);
2411 if (FirstString == NULL) {
2412 return FALSE;
2413 }
2414
2415 SecondString = StrStr (SecondString, StartSearchString);
2416 if (SecondString == NULL) {
2417 return FALSE;
2418 }
2419
2420 EndFirstString = StrStr (FirstString, StopSearchString);
2421 if (EndFirstString == NULL) {
2422 return FALSE;
2423 }
2424
2425 EndSecondString = StrStr (SecondString, StopSearchString);
2426 if (EndSecondString == NULL) {
2427 return FALSE;
2428 }
2429
2430 if ((EndFirstString - FirstString) != (EndSecondString - SecondString)) {
2431 return FALSE;
2432 }
2433
2434 return (BOOLEAN)(StrnCmp (FirstString, SecondString, EndFirstString - FirstString) == 0);
2435 }
2436
2437 /**
2438 Determines if the routing data specified by GUID and NAME match a <ConfigHdr>.
2439
2440 If ConfigHdr is NULL, then ASSERT().
2441
2442 @param[in] ConfigHdr Either <ConfigRequest> or <ConfigResp>.
2443 @param[in] Guid GUID of the storage.
2444 @param[in] Name NAME of the storage.
2445
2446 @retval TRUE Routing information matches <ConfigHdr>.
2447 @retval FALSE Routing information does not match <ConfigHdr>.
2448
2449 **/
2450 BOOLEAN
2451 EFIAPI
2452 HiiIsConfigHdrMatch (
2453 IN CONST EFI_STRING ConfigHdr,
2454 IN CONST EFI_GUID *Guid, OPTIONAL
2455 IN CONST CHAR16 *Name OPTIONAL
2456 )
2457 {
2458 EFI_STRING CompareConfigHdr;
2459 BOOLEAN Result;
2460
2461 ASSERT (ConfigHdr != NULL);
2462
2463 //
2464 // Use Guid and Name to generate a <ConfigHdr> string
2465 //
2466 CompareConfigHdr = HiiConstructConfigHdr (Guid, Name, NULL);
2467 if (CompareConfigHdr == NULL) {
2468 return FALSE;
2469 }
2470
2471 Result = TRUE;
2472 if (Guid != NULL) {
2473 //
2474 // Compare GUID value strings
2475 //
2476 Result = InternalHiiCompareSubString (ConfigHdr, CompareConfigHdr, L"GUID=", L"&NAME=");
2477 }
2478
2479 if (Result && Name != NULL) {
2480 //
2481 // Compare NAME value strings
2482 //
2483 Result = InternalHiiCompareSubString (ConfigHdr, CompareConfigHdr, L"&NAME=", L"&PATH=");
2484 }
2485
2486 //
2487 // Free the <ConfigHdr> string
2488 //
2489 FreePool (CompareConfigHdr);
2490
2491 return Result;
2492 }
2493
2494 /**
2495 Retrieves uncommitted data from the Form Browser and converts it to a binary
2496 buffer.
2497
2498 @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional
2499 parameter that may be NULL.
2500 @param[in] VariableName Pointer to a Null-terminated Unicode string. This
2501 is an optional parameter that may be NULL.
2502 @param[in] BufferSize Length in bytes of buffer to hold retrieved data.
2503 @param[out] Buffer Buffer of data to be updated.
2504
2505 @retval FALSE The uncommitted data could not be retrieved.
2506 @retval TRUE The uncommitted data was retrieved.
2507
2508 **/
2509 BOOLEAN
2510 EFIAPI
2511 HiiGetBrowserData (
2512 IN CONST EFI_GUID *VariableGuid, OPTIONAL
2513 IN CONST CHAR16 *VariableName, OPTIONAL
2514 IN UINTN BufferSize,
2515 OUT UINT8 *Buffer
2516 )
2517 {
2518 EFI_STRING ResultsData;
2519 UINTN Size;
2520 EFI_STRING ConfigResp;
2521 EFI_STATUS Status;
2522 CHAR16 *Progress;
2523
2524 //
2525 // Retrieve the results data from the Browser Callback
2526 //
2527 ResultsData = InternalHiiBrowserCallback (VariableGuid, VariableName, NULL);
2528 if (ResultsData == NULL) {
2529 return FALSE;
2530 }
2531
2532 //
2533 // Construct <ConfigResp> mConfigHdrTemplate L'&' ResultsData L'\0'
2534 //
2535 Size = (StrLen (mConfigHdrTemplate) + 1) * sizeof (CHAR16);
2536 Size = Size + (StrLen (ResultsData) + 1) * sizeof (CHAR16);
2537 ConfigResp = AllocateZeroPool (Size);
2538 UnicodeSPrint (ConfigResp, Size, L"%s&%s", mConfigHdrTemplate, ResultsData);
2539
2540 //
2541 // Free the allocated buffer
2542 //
2543 FreePool (ResultsData);
2544 if (ConfigResp == NULL) {
2545 return FALSE;
2546 }
2547
2548 //
2549 // Convert <ConfigResp> to a buffer
2550 //
2551 Status = gHiiConfigRouting->ConfigToBlock (
2552 gHiiConfigRouting,
2553 ConfigResp,
2554 Buffer,
2555 &BufferSize,
2556 &Progress
2557 );
2558 //
2559 // Free the allocated buffer
2560 //
2561 FreePool (ConfigResp);
2562
2563 if (EFI_ERROR (Status)) {
2564 return FALSE;
2565 }
2566
2567 return TRUE;
2568 }
2569
2570 /**
2571 Updates uncommitted data in the Form Browser.
2572
2573 If Buffer is NULL, then ASSERT().
2574
2575 @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional
2576 parameter that may be NULL.
2577 @param[in] VariableName Pointer to a Null-terminated Unicode string. This
2578 is an optional parameter that may be NULL.
2579 @param[in] BufferSize Length, in bytes, of Buffer.
2580 @param[in] Buffer Buffer of data to commit.
2581 @param[in] RequestElement An optional field to specify which part of the
2582 buffer data will be send back to Browser. If NULL,
2583 the whole buffer of data will be committed to
2584 Browser.
2585 <RequestElement> ::= &OFFSET=<Number>&WIDTH=<Number>*
2586
2587 @retval FALSE The uncommitted data could not be updated.
2588 @retval TRUE The uncommitted data was updated.
2589
2590 **/
2591 BOOLEAN
2592 EFIAPI
2593 HiiSetBrowserData (
2594 IN CONST EFI_GUID *VariableGuid, OPTIONAL
2595 IN CONST CHAR16 *VariableName, OPTIONAL
2596 IN UINTN BufferSize,
2597 IN CONST UINT8 *Buffer,
2598 IN CONST CHAR16 *RequestElement OPTIONAL
2599 )
2600 {
2601 UINTN Size;
2602 EFI_STRING ConfigRequest;
2603 EFI_STRING ConfigResp;
2604 EFI_STRING ResultsData;
2605
2606 ASSERT (Buffer != NULL);
2607
2608 //
2609 // Construct <ConfigRequest>
2610 //
2611 if (RequestElement == NULL) {
2612 //
2613 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2614 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
2615 //
2616 Size = (StrLen (mConfigHdrTemplate) + 32 + 1) * sizeof (CHAR16);
2617 ConfigRequest = AllocateZeroPool (Size);
2618 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", mConfigHdrTemplate, (UINT64)BufferSize);
2619 } else {
2620 //
2621 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2622 // followed by <RequestElement> followed by a Null-terminator
2623 //
2624 Size = StrLen (mConfigHdrTemplate) * sizeof (CHAR16);
2625 Size = Size + (StrLen (RequestElement) + 1) * sizeof (CHAR16);
2626 ConfigRequest = AllocateZeroPool (Size);
2627 UnicodeSPrint (ConfigRequest, Size, L"%s%s", mConfigHdrTemplate, RequestElement);
2628 }
2629 if (ConfigRequest == NULL) {
2630 return FALSE;
2631 }
2632
2633 //
2634 // Convert <ConfigRequest> to <ConfigResp>
2635 //
2636 ConfigResp = InternalHiiBlockToConfig (ConfigRequest, Buffer, BufferSize);
2637 FreePool (ConfigRequest);
2638 if (ConfigResp == NULL) {
2639 return FALSE;
2640 }
2641
2642 //
2643 // Set data in the uncommitted browser state information
2644 //
2645 ResultsData = InternalHiiBrowserCallback (VariableGuid, VariableName, ConfigResp + StrLen(mConfigHdrTemplate) + 1);
2646 FreePool (ConfigResp);
2647
2648 return (BOOLEAN)(ResultsData != NULL);
2649 }
2650
2651 /////////////////////////////////////////
2652 /////////////////////////////////////////
2653 /// IFR Functions
2654 /////////////////////////////////////////
2655 /////////////////////////////////////////
2656
2657 #define HII_LIB_OPCODE_ALLOCATION_SIZE 0x200
2658
2659 typedef struct {
2660 UINT8 *Buffer;
2661 UINTN BufferSize;
2662 UINTN Position;
2663 } HII_LIB_OPCODE_BUFFER;
2664
2665 ///
2666 /// Lookup table that converts EFI_IFR_TYPE_X enum values to a width in bytes
2667 ///
2668 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 mHiiDefaultTypeToWidth[] = {
2669 1, // EFI_IFR_TYPE_NUM_SIZE_8
2670 2, // EFI_IFR_TYPE_NUM_SIZE_16
2671 4, // EFI_IFR_TYPE_NUM_SIZE_32
2672 8, // EFI_IFR_TYPE_NUM_SIZE_64
2673 1, // EFI_IFR_TYPE_BOOLEAN
2674 3, // EFI_IFR_TYPE_TIME
2675 4, // EFI_IFR_TYPE_DATE
2676 2 // EFI_IFR_TYPE_STRING
2677 };
2678
2679 /**
2680 Allocates and returns a new OpCode Handle. OpCode Handles must be freed with
2681 HiiFreeOpCodeHandle().
2682
2683 @retval NULL There are not enough resources to allocate a new OpCode Handle.
2684 @retval Other A new OpCode handle.
2685
2686 **/
2687 VOID *
2688 EFIAPI
2689 HiiAllocateOpCodeHandle (
2690 VOID
2691 )
2692 {
2693 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2694
2695 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)AllocatePool (sizeof (HII_LIB_OPCODE_BUFFER));
2696 if (OpCodeBuffer == NULL) {
2697 return NULL;
2698 }
2699 OpCodeBuffer->Buffer = (UINT8 *)AllocatePool (HII_LIB_OPCODE_ALLOCATION_SIZE);
2700 if (OpCodeBuffer->Buffer == NULL) {
2701 FreePool (OpCodeBuffer);
2702 return NULL;
2703 }
2704 OpCodeBuffer->BufferSize = HII_LIB_OPCODE_ALLOCATION_SIZE;
2705 OpCodeBuffer->Position = 0;
2706 return (VOID *)OpCodeBuffer;
2707 }
2708
2709 /**
2710 Frees an OpCode Handle that was previously allocated with HiiAllocateOpCodeHandle().
2711 When an OpCode Handle is freed, all of the opcodes associated with the OpCode
2712 Handle are also freed.
2713
2714 If OpCodeHandle is NULL, then ASSERT().
2715
2716 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2717
2718 **/
2719 VOID
2720 EFIAPI
2721 HiiFreeOpCodeHandle (
2722 VOID *OpCodeHandle
2723 )
2724 {
2725 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2726
2727 ASSERT (OpCodeHandle != NULL);
2728
2729 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)OpCodeHandle;
2730 if (OpCodeBuffer->Buffer != NULL) {
2731 FreePool (OpCodeBuffer->Buffer);
2732 }
2733 FreePool (OpCodeBuffer);
2734 }
2735
2736 /**
2737 Internal function gets the current position of opcode buffer.
2738
2739 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2740
2741 @return Current position of opcode buffer.
2742 **/
2743 UINTN
2744 EFIAPI
2745 InternalHiiOpCodeHandlePosition (
2746 IN VOID *OpCodeHandle
2747 )
2748 {
2749 return ((HII_LIB_OPCODE_BUFFER *)OpCodeHandle)->Position;
2750 }
2751
2752 /**
2753 Internal function gets the start pointer of opcode buffer.
2754
2755 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2756
2757 @return Pointer to the opcode buffer base.
2758 **/
2759 UINT8 *
2760 EFIAPI
2761 InternalHiiOpCodeHandleBuffer (
2762 IN VOID *OpCodeHandle
2763 )
2764 {
2765 return ((HII_LIB_OPCODE_BUFFER *)OpCodeHandle)->Buffer;
2766 }
2767
2768 /**
2769 Internal function reserves the enough buffer for current opcode.
2770 When the buffer is not enough, Opcode buffer will be extended.
2771
2772 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2773 @param[in] Size Size of current opcode.
2774
2775 @return Pointer to the current opcode.
2776 **/
2777 UINT8 *
2778 EFIAPI
2779 InternalHiiGrowOpCodeHandle (
2780 IN VOID *OpCodeHandle,
2781 IN UINTN Size
2782 )
2783 {
2784 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2785 UINT8 *Buffer;
2786
2787 ASSERT (OpCodeHandle != NULL);
2788
2789 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)OpCodeHandle;
2790 if (OpCodeBuffer->Position + Size > OpCodeBuffer->BufferSize) {
2791 Buffer = ReallocatePool (
2792 OpCodeBuffer->BufferSize,
2793 OpCodeBuffer->BufferSize + (Size + HII_LIB_OPCODE_ALLOCATION_SIZE),
2794 OpCodeBuffer->Buffer
2795 );
2796 ASSERT (Buffer != NULL);
2797 OpCodeBuffer->Buffer = Buffer;
2798 OpCodeBuffer->BufferSize += (Size + HII_LIB_OPCODE_ALLOCATION_SIZE);
2799 }
2800 Buffer = OpCodeBuffer->Buffer + OpCodeBuffer->Position;
2801 OpCodeBuffer->Position += Size;
2802 return Buffer;
2803 }
2804
2805 /**
2806 Internal function creates opcode based on the template opcode.
2807
2808 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2809 @param[in] OpCodeTemplate Pointer to the template buffer of opcode.
2810 @param[in] OpCode OpCode IFR value.
2811 @param[in] OpCodeSize Size of opcode.
2812 @param[in] ExtensionSize Size of extended opcode.
2813 @param[in] Scope Scope bit of opcode.
2814
2815 @return Pointer to the current opcode with opcode data.
2816 **/
2817 UINT8 *
2818 EFIAPI
2819 InternalHiiCreateOpCodeExtended (
2820 IN VOID *OpCodeHandle,
2821 IN VOID *OpCodeTemplate,
2822 IN UINT8 OpCode,
2823 IN UINTN OpCodeSize,
2824 IN UINTN ExtensionSize,
2825 IN UINT8 Scope
2826 )
2827 {
2828 EFI_IFR_OP_HEADER *Header;
2829 UINT8 *Buffer;
2830
2831 ASSERT (OpCodeTemplate != NULL);
2832 ASSERT ((OpCodeSize + ExtensionSize) <= 0x7F);
2833
2834 Header = (EFI_IFR_OP_HEADER *)OpCodeTemplate;
2835 Header->OpCode = OpCode;
2836 Header->Scope = Scope;
2837 Header->Length = (UINT8)(OpCodeSize + ExtensionSize);
2838 Buffer = InternalHiiGrowOpCodeHandle (OpCodeHandle, Header->Length);
2839 return (UINT8 *)CopyMem (Buffer, Header, OpCodeSize);
2840 }
2841
2842 /**
2843 Internal function creates opcode based on the template opcode for the normal opcode.
2844
2845 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2846 @param[in] OpCodeTemplate Pointer to the template buffer of opcode.
2847 @param[in] OpCode OpCode IFR value.
2848 @param[in] OpCodeSize Size of opcode.
2849
2850 @return Pointer to the current opcode with opcode data.
2851 **/
2852 UINT8 *
2853 EFIAPI
2854 InternalHiiCreateOpCode (
2855 IN VOID *OpCodeHandle,
2856 IN VOID *OpCodeTemplate,
2857 IN UINT8 OpCode,
2858 IN UINTN OpCodeSize
2859 )
2860 {
2861 return InternalHiiCreateOpCodeExtended (OpCodeHandle, OpCodeTemplate, OpCode, OpCodeSize, 0, 0);
2862 }
2863
2864 /**
2865 Append raw opcodes to an OpCodeHandle.
2866
2867 If OpCodeHandle is NULL, then ASSERT().
2868 If RawBuffer is NULL, then ASSERT();
2869
2870 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2871 @param[in] RawBuffer Buffer of opcodes to append.
2872 @param[in] RawBufferSize The size, in bytes, of Buffer.
2873
2874 @retval NULL There is not enough space left in Buffer to add the opcode.
2875 @retval Other A pointer to the appended opcodes.
2876
2877 **/
2878 UINT8 *
2879 EFIAPI
2880 HiiCreateRawOpCodes (
2881 IN VOID *OpCodeHandle,
2882 IN UINT8 *RawBuffer,
2883 IN UINTN RawBufferSize
2884 )
2885 {
2886 UINT8 *Buffer;
2887
2888 ASSERT (RawBuffer != NULL);
2889
2890 Buffer = InternalHiiGrowOpCodeHandle (OpCodeHandle, RawBufferSize);
2891 return (UINT8 *)CopyMem (Buffer, RawBuffer, RawBufferSize);
2892 }
2893
2894 /**
2895 Append opcodes from one OpCode Handle to another OpCode handle.
2896
2897 If OpCodeHandle is NULL, then ASSERT().
2898 If RawOpCodeHandle is NULL, then ASSERT();
2899
2900 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2901 @param[in] RawOpCodeHandle Handle to the buffer of opcodes.
2902
2903 @retval NULL There is not enough space left in Buffer to add the opcode.
2904 @retval Other A pointer to the appended opcodes.
2905
2906 **/
2907 UINT8 *
2908 EFIAPI
2909 InternalHiiAppendOpCodes (
2910 IN VOID *OpCodeHandle,
2911 IN VOID *RawOpCodeHandle
2912 )
2913 {
2914 HII_LIB_OPCODE_BUFFER *RawOpCodeBuffer;
2915
2916 ASSERT (RawOpCodeHandle != NULL);
2917
2918 RawOpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)RawOpCodeHandle;
2919 return HiiCreateRawOpCodes (OpCodeHandle, RawOpCodeBuffer->Buffer, RawOpCodeBuffer->Position);
2920 }
2921
2922 /**
2923 Create EFI_IFR_END_OP opcode.
2924
2925 If OpCodeHandle is NULL, then ASSERT().
2926
2927 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2928
2929 @retval NULL There is not enough space left in Buffer to add the opcode.
2930 @retval Other A pointer to the created opcode.
2931
2932 **/
2933 UINT8 *
2934 EFIAPI
2935 HiiCreateEndOpCode (
2936 IN VOID *OpCodeHandle
2937 )
2938 {
2939 EFI_IFR_END OpCode;
2940
2941 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_END_OP, sizeof (OpCode));
2942 }
2943
2944 /**
2945 Create EFI_IFR_ONE_OF_OPTION_OP opcode.
2946
2947 If OpCodeHandle is NULL, then ASSERT().
2948 If Type is invalid, then ASSERT().
2949 If Flags is invalid, then ASSERT().
2950
2951 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2952 @param[in] StringId StringId for the option
2953 @param[in] Flags Flags for the option
2954 @param[in] Type Type for the option
2955 @param[in] Value Value for the option
2956
2957 @retval NULL There is not enough space left in Buffer to add the opcode.
2958 @retval Other A pointer to the created opcode.
2959
2960 **/
2961 UINT8 *
2962 EFIAPI
2963 HiiCreateOneOfOptionOpCode (
2964 IN VOID *OpCodeHandle,
2965 IN UINT16 StringId,
2966 IN UINT8 Flags,
2967 IN UINT8 Type,
2968 IN UINT64 Value
2969 )
2970 {
2971 EFI_IFR_ONE_OF_OPTION OpCode;
2972
2973 ASSERT (Type < EFI_IFR_TYPE_OTHER);
2974
2975 ZeroMem (&OpCode, sizeof (OpCode));
2976 OpCode.Option = StringId;
2977 OpCode.Flags = (UINT8) (Flags & (EFI_IFR_OPTION_DEFAULT | EFI_IFR_OPTION_DEFAULT_MFG));
2978 OpCode.Type = Type;
2979 CopyMem (&OpCode.Value, &Value, mHiiDefaultTypeToWidth[Type]);
2980
2981 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_ONE_OF_OPTION_OP, OFFSET_OF(EFI_IFR_ONE_OF_OPTION, Value) + mHiiDefaultTypeToWidth[Type]);
2982 }
2983
2984 /**
2985 Create EFI_IFR_DEFAULT_OP opcode.
2986
2987 If OpCodeHandle is NULL, then ASSERT().
2988 If Type is invalid, then ASSERT().
2989
2990 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2991 @param[in] DefaultId DefaultId for the default
2992 @param[in] Type Type for the default
2993 @param[in] Value Value for the default
2994
2995 @retval NULL There is not enough space left in Buffer to add the opcode.
2996 @retval Other A pointer to the created opcode.
2997
2998 **/
2999 UINT8 *
3000 EFIAPI
3001 HiiCreateDefaultOpCode (
3002 IN VOID *OpCodeHandle,
3003 IN UINT16 DefaultId,
3004 IN UINT8 Type,
3005 IN UINT64 Value
3006 )
3007 {
3008 EFI_IFR_DEFAULT OpCode;
3009
3010 ASSERT (Type < EFI_IFR_TYPE_OTHER);
3011
3012 ZeroMem (&OpCode, sizeof (OpCode));
3013 OpCode.Type = Type;
3014 OpCode.DefaultId = DefaultId;
3015 CopyMem (&OpCode.Value, &Value, mHiiDefaultTypeToWidth[Type]);
3016
3017 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_DEFAULT_OP, OFFSET_OF(EFI_IFR_DEFAULT, Value) + mHiiDefaultTypeToWidth[Type]);
3018 }
3019
3020 /**
3021 Create EFI_IFR_GUID opcode.
3022
3023 If OpCodeHandle is NULL, then ASSERT().
3024 If Guid is NULL, then ASSERT().
3025 If OpCodeSize < sizeof (EFI_IFR_GUID), then ASSERT().
3026
3027 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3028 @param[in] Guid Pointer to EFI_GUID of this guided opcode.
3029 @param[in] GuidOpCode Pointer to an EFI_IFR_GUID opcode. This is an
3030 optional parameter that may be NULL. If this
3031 parameter is NULL, then the GUID extension
3032 region of the created opcode is filled with zeros.
3033 If this parameter is not NULL, then the GUID
3034 extension region of GuidData will be copied to
3035 the GUID extension region of the created opcode.
3036 @param[in] OpCodeSize The size, in bytes, of created opcode. This value
3037 must be >= sizeof(EFI_IFR_GUID).
3038
3039 @retval NULL There is not enough space left in Buffer to add the opcode.
3040 @retval Other A pointer to the created opcode.
3041
3042 **/
3043 UINT8 *
3044 EFIAPI
3045 HiiCreateGuidOpCode (
3046 IN VOID *OpCodeHandle,
3047 IN CONST EFI_GUID *Guid,
3048 IN CONST VOID *GuidOpCode, OPTIONAL
3049 IN UINTN OpCodeSize
3050 )
3051 {
3052 EFI_IFR_GUID OpCode;
3053 EFI_IFR_GUID *OpCodePointer;
3054
3055 ASSERT (Guid != NULL);
3056 ASSERT (OpCodeSize >= sizeof (OpCode));
3057
3058 ZeroMem (&OpCode, sizeof (OpCode));
3059 CopyGuid ((EFI_GUID *)(VOID *)&OpCode.Guid, Guid);
3060
3061 OpCodePointer = (EFI_IFR_GUID *)InternalHiiCreateOpCodeExtended (
3062 OpCodeHandle,
3063 &OpCode,
3064 EFI_IFR_GUID_OP,
3065 sizeof (OpCode),
3066 OpCodeSize - sizeof (OpCode),
3067 0
3068 );
3069 if (OpCodePointer != NULL && GuidOpCode != NULL) {
3070 CopyMem (OpCodePointer + 1, (EFI_IFR_GUID *)GuidOpCode + 1, OpCodeSize - sizeof (OpCode));
3071 }
3072 return (UINT8 *)OpCodePointer;
3073 }
3074
3075 /**
3076 Create EFI_IFR_ACTION_OP opcode.
3077
3078 If OpCodeHandle is NULL, then ASSERT().
3079 If any reserved bits are set in QuestionFlags, then ASSERT().
3080
3081 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3082 @param[in] QuestionId Question ID
3083 @param[in] Prompt String ID for Prompt
3084 @param[in] Help String ID for Help
3085 @param[in] QuestionFlags Flags in Question Header
3086 @param[in] QuestionConfig String ID for configuration
3087
3088 @retval NULL There is not enough space left in Buffer to add the opcode.
3089 @retval Other A pointer to the created opcode.
3090
3091 **/
3092 UINT8 *
3093 EFIAPI
3094 HiiCreateActionOpCode (
3095 IN VOID *OpCodeHandle,
3096 IN EFI_QUESTION_ID QuestionId,
3097 IN EFI_STRING_ID Prompt,
3098 IN EFI_STRING_ID Help,
3099 IN UINT8 QuestionFlags,
3100 IN EFI_STRING_ID QuestionConfig
3101 )
3102 {
3103 EFI_IFR_ACTION OpCode;
3104
3105 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3106
3107 ZeroMem (&OpCode, sizeof (OpCode));
3108 OpCode.Question.QuestionId = QuestionId;
3109 OpCode.Question.Header.Prompt = Prompt;
3110 OpCode.Question.Header.Help = Help;
3111 OpCode.Question.Flags = QuestionFlags;
3112 OpCode.QuestionConfig = QuestionConfig;
3113
3114 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_ACTION_OP, sizeof (OpCode));
3115 }
3116
3117 /**
3118 Create EFI_IFR_SUBTITLE_OP opcode.
3119
3120 If OpCodeHandle is NULL, then ASSERT().
3121 If any reserved bits are set in Flags, then ASSERT().
3122 If Scope > 1, then ASSERT().
3123
3124 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3125 @param[in] Prompt String ID for Prompt
3126 @param[in] Help String ID for Help
3127 @param[in] Flags Subtitle opcode flags
3128 @param[in] Scope 1 if this opcpde is the beginning of a new scope.
3129 0 if this opcode is within the current scope.
3130
3131 @retval NULL There is not enough space left in Buffer to add the opcode.
3132 @retval Other A pointer to the created opcode.
3133
3134 **/
3135 UINT8 *
3136 EFIAPI
3137 HiiCreateSubTitleOpCode (
3138 IN VOID *OpCodeHandle,
3139 IN EFI_STRING_ID Prompt,
3140 IN EFI_STRING_ID Help,
3141 IN UINT8 Flags,
3142 IN UINT8 Scope
3143 )
3144 {
3145 EFI_IFR_SUBTITLE OpCode;
3146
3147 ASSERT (Scope <= 1);
3148 ASSERT ((Flags & (~(EFI_IFR_FLAGS_HORIZONTAL))) == 0);
3149
3150 ZeroMem (&OpCode, sizeof (OpCode));
3151 OpCode.Statement.Prompt = Prompt;
3152 OpCode.Statement.Help = Help;
3153 OpCode.Flags = Flags;
3154
3155 return InternalHiiCreateOpCodeExtended (
3156 OpCodeHandle,
3157 &OpCode,
3158 EFI_IFR_SUBTITLE_OP,
3159 sizeof (OpCode),
3160 0,
3161 Scope
3162 );
3163 }
3164
3165 /**
3166 Create EFI_IFR_REF_OP opcode.
3167
3168 If OpCodeHandle is NULL, then ASSERT().
3169 If any reserved bits are set in QuestionFlags, then ASSERT().
3170
3171 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3172 @param[in] FormId Destination Form ID
3173 @param[in] Prompt String ID for Prompt
3174 @param[in] Help String ID for Help
3175 @param[in] QuestionFlags Flags in Question Header
3176 @param[in] QuestionId Question ID
3177
3178 @retval NULL There is not enough space left in Buffer to add the opcode.
3179 @retval Other A pointer to the created opcode.
3180
3181 **/
3182 UINT8 *
3183 EFIAPI
3184 HiiCreateGotoOpCode (
3185 IN VOID *OpCodeHandle,
3186 IN EFI_FORM_ID FormId,
3187 IN EFI_STRING_ID Prompt,
3188 IN EFI_STRING_ID Help,
3189 IN UINT8 QuestionFlags,
3190 IN EFI_QUESTION_ID QuestionId
3191 )
3192 {
3193 EFI_IFR_REF OpCode;
3194
3195 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3196
3197 ZeroMem (&OpCode, sizeof (OpCode));
3198 OpCode.Question.Header.Prompt = Prompt;
3199 OpCode.Question.Header.Help = Help;
3200 OpCode.Question.QuestionId = QuestionId;
3201 OpCode.Question.Flags = QuestionFlags;
3202 OpCode.FormId = FormId;
3203
3204 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_REF_OP, sizeof (OpCode));
3205 }
3206
3207 /**
3208 Create EFI_IFR_REF_OP, EFI_IFR_REF2_OP, EFI_IFR_REF3_OP and EFI_IFR_REF4_OP opcode.
3209
3210 When RefDevicePath is not zero, EFI_IFR_REF4 opcode will be created.
3211 When RefDevicePath is zero and RefFormSetId is not NULL, EFI_IFR_REF3 opcode will be created.
3212 When RefDevicePath is zero, RefFormSetId is NULL and RefQuestionId is not zero, EFI_IFR_REF2 opcode will be created.
3213 When RefDevicePath is zero, RefFormSetId is NULL and RefQuestionId is zero, EFI_IFR_REF opcode will be created.
3214
3215 If OpCodeHandle is NULL, then ASSERT().
3216 If any reserved bits are set in QuestionFlags, then ASSERT().
3217
3218 @param[in] OpCodeHandle The handle to the buffer of opcodes.
3219 @param[in] RefFormId The Destination Form ID.
3220 @param[in] Prompt The string ID for Prompt.
3221 @param[in] Help The string ID for Help.
3222 @param[in] QuestionFlags The flags in Question Header
3223 @param[in] QuestionId Question ID.
3224 @param[in] RefQuestionId The question on the form to which this link is referring.
3225 If its value is zero, then the link refers to the top of the form.
3226 @param[in] RefFormSetId The form set to which this link is referring. If its value is NULL, and RefDevicePath is
3227 zero, then the link is to the current form set.
3228 @param[in] RefDevicePath The string identifier that specifies the string containing the text representation of
3229 the device path to which the form set containing the form specified by FormId.
3230 If its value is zero, then the link refers to the current page.
3231
3232 @retval NULL There is not enough space left in Buffer to add the opcode.
3233 @retval Other A pointer to the created opcode.
3234
3235 **/
3236 UINT8 *
3237 EFIAPI
3238 HiiCreateGotoExOpCode (
3239 IN VOID *OpCodeHandle,
3240 IN EFI_FORM_ID RefFormId,
3241 IN EFI_STRING_ID Prompt,
3242 IN EFI_STRING_ID Help,
3243 IN UINT8 QuestionFlags,
3244 IN EFI_QUESTION_ID QuestionId,
3245 IN EFI_QUESTION_ID RefQuestionId,
3246 IN EFI_GUID *RefFormSetId, OPTIONAL
3247 IN EFI_STRING_ID RefDevicePath
3248 )
3249 {
3250 EFI_IFR_REF4 OpCode;
3251 UINTN OpCodeSize;
3252
3253 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3254
3255 ZeroMem (&OpCode, sizeof (OpCode));
3256 OpCode.Question.Header.Prompt = Prompt;
3257 OpCode.Question.Header.Help = Help;
3258 OpCode.Question.QuestionId = QuestionId;
3259 OpCode.Question.Flags = QuestionFlags;
3260 OpCode.FormId = RefFormId;
3261 OpCode.QuestionId = RefQuestionId;
3262 OpCode.DevicePath = RefDevicePath;
3263 if (RefFormSetId != NULL) {
3264 CopyMem (&OpCode.FormSetId, RefFormSetId, sizeof (OpCode.FormSetId));
3265 }
3266
3267 //
3268 // Cacluate OpCodeSize based on the input Ref value.
3269 // Try to use the small OpCode to save size.
3270 //
3271 OpCodeSize = sizeof (EFI_IFR_REF);
3272 if (RefDevicePath != 0) {
3273 OpCodeSize = sizeof (EFI_IFR_REF4);
3274 } else if (RefFormSetId != NULL) {
3275 OpCodeSize = sizeof (EFI_IFR_REF3);
3276 } else if (RefQuestionId != 0) {
3277 OpCodeSize = sizeof (EFI_IFR_REF2);
3278 }
3279
3280 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_REF_OP, OpCodeSize);
3281 }
3282
3283 /**
3284 Create EFI_IFR_CHECKBOX_OP opcode.
3285
3286 If OpCodeHandle is NULL, then ASSERT().
3287 If any reserved bits are set in QuestionFlags, then ASSERT().
3288 If any reserved bits are set in CheckBoxFlags, then ASSERT().
3289
3290 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3291 @param[in] QuestionId Question ID
3292 @param[in] VarStoreId Storage ID
3293 @param[in] VarOffset Offset in Storage
3294 @param[in] Prompt String ID for Prompt
3295 @param[in] Help String ID for Help
3296 @param[in] QuestionFlags Flags in Question Header
3297 @param[in] CheckBoxFlags Flags for checkbox opcode
3298 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3299 is an optional parameter that may be NULL.
3300
3301 @retval NULL There is not enough space left in Buffer to add the opcode.
3302 @retval Other A pointer to the created opcode.
3303
3304 **/
3305 UINT8 *
3306 EFIAPI
3307 HiiCreateCheckBoxOpCode (
3308 IN VOID *OpCodeHandle,
3309 IN EFI_QUESTION_ID QuestionId,
3310 IN EFI_VARSTORE_ID VarStoreId,
3311 IN UINT16 VarOffset,
3312 IN EFI_STRING_ID Prompt,
3313 IN EFI_STRING_ID Help,
3314 IN UINT8 QuestionFlags,
3315 IN UINT8 CheckBoxFlags,
3316 IN VOID *DefaultsOpCodeHandle OPTIONAL
3317 )
3318 {
3319 EFI_IFR_CHECKBOX OpCode;
3320 UINTN Position;
3321
3322 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3323
3324 ZeroMem (&OpCode, sizeof (OpCode));
3325 OpCode.Question.QuestionId = QuestionId;
3326 OpCode.Question.VarStoreId = VarStoreId;
3327 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3328 OpCode.Question.Header.Prompt = Prompt;
3329 OpCode.Question.Header.Help = Help;
3330 OpCode.Question.Flags = QuestionFlags;
3331 OpCode.Flags = CheckBoxFlags;
3332
3333 if (DefaultsOpCodeHandle == NULL) {
3334 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_CHECKBOX_OP, sizeof (OpCode));
3335 }
3336
3337 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3338 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_CHECKBOX_OP, sizeof (OpCode), 0, 1);
3339 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3340 HiiCreateEndOpCode (OpCodeHandle);
3341 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3342 }
3343
3344 /**
3345 Create EFI_IFR_NUMERIC_OP opcode.
3346
3347 If OpCodeHandle is NULL, then ASSERT().
3348 If any reserved bits are set in QuestionFlags, then ASSERT().
3349 If any reserved bits are set in NumericFlags, then ASSERT().
3350
3351 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3352 @param[in] QuestionId Question ID
3353 @param[in] VarStoreId Storage ID
3354 @param[in] VarOffset Offset in Storage
3355 @param[in] Prompt String ID for Prompt
3356 @param[in] Help String ID for Help
3357 @param[in] QuestionFlags Flags in Question Header
3358 @param[in] NumericFlags Flags for numeric opcode
3359 @param[in] Minimum Numeric minimum value
3360 @param[in] Maximum Numeric maximum value
3361 @param[in] Step Numeric step for edit
3362 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3363 is an optional parameter that may be NULL.
3364
3365 @retval NULL There is not enough space left in Buffer to add the opcode.
3366 @retval Other A pointer to the created opcode.
3367
3368 **/
3369 UINT8 *
3370 EFIAPI
3371 HiiCreateNumericOpCode (
3372 IN VOID *OpCodeHandle,
3373 IN EFI_QUESTION_ID QuestionId,
3374 IN EFI_VARSTORE_ID VarStoreId,
3375 IN UINT16 VarOffset,
3376 IN EFI_STRING_ID Prompt,
3377 IN EFI_STRING_ID Help,
3378 IN UINT8 QuestionFlags,
3379 IN UINT8 NumericFlags,
3380 IN UINT64 Minimum,
3381 IN UINT64 Maximum,
3382 IN UINT64 Step,
3383 IN VOID *DefaultsOpCodeHandle OPTIONAL
3384 )
3385 {
3386 EFI_IFR_NUMERIC OpCode;
3387 UINTN Position;
3388 UINTN Length;
3389
3390 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3391
3392 Length = 0;
3393 ZeroMem (&OpCode, sizeof (OpCode));
3394 OpCode.Question.QuestionId = QuestionId;
3395 OpCode.Question.VarStoreId = VarStoreId;
3396 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3397 OpCode.Question.Header.Prompt = Prompt;
3398 OpCode.Question.Header.Help = Help;
3399 OpCode.Question.Flags = QuestionFlags;
3400 OpCode.Flags = NumericFlags;
3401
3402 switch (NumericFlags & EFI_IFR_NUMERIC_SIZE) {
3403 case EFI_IFR_NUMERIC_SIZE_1:
3404 OpCode.data.u8.MinValue = (UINT8)Minimum;
3405 OpCode.data.u8.MaxValue = (UINT8)Maximum;
3406 OpCode.data.u8.Step = (UINT8)Step;
3407 Length = 3;
3408 break;
3409
3410 case EFI_IFR_NUMERIC_SIZE_2:
3411 OpCode.data.u16.MinValue = (UINT16)Minimum;
3412 OpCode.data.u16.MaxValue = (UINT16)Maximum;
3413 OpCode.data.u16.Step = (UINT16)Step;
3414 Length = 6;
3415 break;
3416
3417 case EFI_IFR_NUMERIC_SIZE_4:
3418 OpCode.data.u32.MinValue = (UINT32)Minimum;
3419 OpCode.data.u32.MaxValue = (UINT32)Maximum;
3420 OpCode.data.u32.Step = (UINT32)Step;
3421 Length = 12;
3422 break;
3423
3424 case EFI_IFR_NUMERIC_SIZE_8:
3425 OpCode.data.u64.MinValue = Minimum;
3426 OpCode.data.u64.MaxValue = Maximum;
3427 OpCode.data.u64.Step = Step;
3428 Length = 24;
3429 break;
3430 }
3431
3432 Length += OFFSET_OF (EFI_IFR_NUMERIC, data);
3433
3434 if (DefaultsOpCodeHandle == NULL) {
3435 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_NUMERIC_OP, Length);
3436 }
3437
3438 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3439 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_NUMERIC_OP, Length, 0, 1);
3440 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3441 HiiCreateEndOpCode (OpCodeHandle);
3442 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3443 }
3444
3445 /**
3446 Create EFI_IFR_STRING_OP opcode.
3447
3448 If OpCodeHandle is NULL, then ASSERT().
3449 If any reserved bits are set in QuestionFlags, then ASSERT().
3450 If any reserved bits are set in StringFlags, then ASSERT().
3451
3452 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3453 @param[in] QuestionId Question ID
3454 @param[in] VarStoreId Storage ID
3455 @param[in] VarOffset Offset in Storage
3456 @param[in] Prompt String ID for Prompt
3457 @param[in] Help String ID for Help
3458 @param[in] QuestionFlags Flags in Question Header
3459 @param[in] StringFlags Flags for string opcode
3460 @param[in] MinSize String minimum length
3461 @param[in] MaxSize String maximum length
3462 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3463 is an optional parameter that may be NULL.
3464
3465 @retval NULL There is not enough space left in Buffer to add the opcode.
3466 @retval Other A pointer to the created opcode.
3467
3468 **/
3469 UINT8 *
3470 EFIAPI
3471 HiiCreateStringOpCode (
3472 IN VOID *OpCodeHandle,
3473 IN EFI_QUESTION_ID QuestionId,
3474 IN EFI_VARSTORE_ID VarStoreId,
3475 IN UINT16 VarOffset,
3476 IN EFI_STRING_ID Prompt,
3477 IN EFI_STRING_ID Help,
3478 IN UINT8 QuestionFlags,
3479 IN UINT8 StringFlags,
3480 IN UINT8 MinSize,
3481 IN UINT8 MaxSize,
3482 IN VOID *DefaultsOpCodeHandle OPTIONAL
3483 )
3484 {
3485 EFI_IFR_STRING OpCode;
3486 UINTN Position;
3487
3488 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3489
3490 ZeroMem (&OpCode, sizeof (OpCode));
3491 OpCode.Question.Header.Prompt = Prompt;
3492 OpCode.Question.Header.Help = Help;
3493 OpCode.Question.QuestionId = QuestionId;
3494 OpCode.Question.VarStoreId = VarStoreId;
3495 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3496 OpCode.Question.Flags = QuestionFlags;
3497 OpCode.MinSize = MinSize;
3498 OpCode.MaxSize = MaxSize;
3499 OpCode.Flags = (UINT8) (StringFlags & EFI_IFR_STRING_MULTI_LINE);
3500
3501 if (DefaultsOpCodeHandle == NULL) {
3502 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_STRING_OP, sizeof (OpCode));
3503 }
3504
3505 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3506 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_STRING_OP, sizeof (OpCode), 0, 1);
3507 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3508 HiiCreateEndOpCode (OpCodeHandle);
3509 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3510 }
3511
3512 /**
3513 Create EFI_IFR_ONE_OF_OP opcode.
3514
3515 If OpCodeHandle is NULL, then ASSERT().
3516 If any reserved bits are set in QuestionFlags, then ASSERT().
3517 If any reserved bits are set in OneOfFlags, then ASSERT().
3518
3519 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3520 @param[in] QuestionId Question ID
3521 @param[in] VarStoreId Storage ID
3522 @param[in] VarOffset Offset in Storage
3523 @param[in] Prompt String ID for Prompt
3524 @param[in] Help String ID for Help
3525 @param[in] QuestionFlags Flags in Question Header
3526 @param[in] OneOfFlags Flags for oneof opcode
3527 @param[in] OptionsOpCodeHandle Handle for a buffer of ONE_OF_OPTION opcodes.
3528 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3529 is an optional parameter that may be NULL.
3530
3531 @retval NULL There is not enough space left in Buffer to add the opcode.
3532 @retval Other A pointer to the created opcode.
3533
3534 **/
3535 UINT8 *
3536 EFIAPI
3537 HiiCreateOneOfOpCode (
3538 IN VOID *OpCodeHandle,
3539 IN EFI_QUESTION_ID QuestionId,
3540 IN EFI_VARSTORE_ID VarStoreId,
3541 IN UINT16 VarOffset,
3542 IN EFI_STRING_ID Prompt,
3543 IN EFI_STRING_ID Help,
3544 IN UINT8 QuestionFlags,
3545 IN UINT8 OneOfFlags,
3546 IN VOID *OptionsOpCodeHandle,
3547 IN VOID *DefaultsOpCodeHandle OPTIONAL
3548 )
3549 {
3550 EFI_IFR_ONE_OF OpCode;
3551 UINTN Position;
3552 UINTN Length;
3553
3554 ASSERT (OptionsOpCodeHandle != NULL);
3555 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3556
3557 ZeroMem (&OpCode, sizeof (OpCode));
3558 OpCode.Question.Header.Prompt = Prompt;
3559 OpCode.Question.Header.Help = Help;
3560 OpCode.Question.QuestionId = QuestionId;
3561 OpCode.Question.VarStoreId = VarStoreId;
3562 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3563 OpCode.Question.Flags = QuestionFlags;
3564 OpCode.Flags = OneOfFlags;
3565
3566 Length = OFFSET_OF (EFI_IFR_ONE_OF, data);
3567 Length += (1 << (OneOfFlags & EFI_IFR_NUMERIC_SIZE)) * 3;
3568
3569 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3570 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_ONE_OF_OP, Length, 0, 1);
3571 InternalHiiAppendOpCodes (OpCodeHandle, OptionsOpCodeHandle);
3572 if (DefaultsOpCodeHandle != NULL) {
3573 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3574 }
3575 HiiCreateEndOpCode (OpCodeHandle);
3576 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3577 }
3578
3579 /**
3580 Create EFI_IFR_ORDERED_LIST_OP opcode.
3581
3582 If OpCodeHandle is NULL, then ASSERT().
3583 If any reserved bits are set in QuestionFlags, then ASSERT().
3584 If any reserved bits are set in OrderedListFlags, then ASSERT().
3585
3586 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3587 @param[in] QuestionId Question ID
3588 @param[in] VarStoreId Storage ID
3589 @param[in] VarOffset Offset in Storage
3590 @param[in] Prompt String ID for Prompt
3591 @param[in] Help String ID for Help
3592 @param[in] QuestionFlags Flags in Question Header
3593 @param[in] OrderedListFlags Flags for ordered list opcode
3594 @param[in] DataType Type for option value
3595 @param[in] MaxContainers Maximum count for options in this ordered list
3596 @param[in] OptionsOpCodeHandle Handle for a buffer of ONE_OF_OPTION opcodes.
3597 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3598 is an optional parameter that may be NULL.
3599
3600 @retval NULL There is not enough space left in Buffer to add the opcode.
3601 @retval Other A pointer to the created opcode.
3602
3603 **/
3604 UINT8 *
3605 EFIAPI
3606 HiiCreateOrderedListOpCode (
3607 IN VOID *OpCodeHandle,
3608 IN EFI_QUESTION_ID QuestionId,
3609 IN EFI_VARSTORE_ID VarStoreId,
3610 IN UINT16 VarOffset,
3611 IN EFI_STRING_ID Prompt,
3612 IN EFI_STRING_ID Help,
3613 IN UINT8 QuestionFlags,
3614 IN UINT8 OrderedListFlags,
3615 IN UINT8 DataType,
3616 IN UINT8 MaxContainers,
3617 IN VOID *OptionsOpCodeHandle,
3618 IN VOID *DefaultsOpCodeHandle OPTIONAL
3619 )
3620 {
3621 EFI_IFR_ORDERED_LIST OpCode;
3622 UINTN Position;
3623
3624 ASSERT (OptionsOpCodeHandle != NULL);
3625 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3626
3627 ZeroMem (&OpCode, sizeof (OpCode));
3628 OpCode.Question.Header.Prompt = Prompt;
3629 OpCode.Question.Header.Help = Help;
3630 OpCode.Question.QuestionId = QuestionId;
3631 OpCode.Question.VarStoreId = VarStoreId;
3632 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3633 OpCode.Question.Flags = QuestionFlags;
3634 OpCode.MaxContainers = MaxContainers;
3635 OpCode.Flags = OrderedListFlags;
3636
3637 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3638 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_ORDERED_LIST_OP, sizeof (OpCode), 0, 1);
3639 InternalHiiAppendOpCodes (OpCodeHandle, OptionsOpCodeHandle);
3640 if (DefaultsOpCodeHandle != NULL) {
3641 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3642 }
3643 HiiCreateEndOpCode (OpCodeHandle);
3644 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3645 }
3646
3647 /**
3648 Create EFI_IFR_TEXT_OP opcode.
3649
3650 If OpCodeHandle is NULL, then ASSERT().
3651
3652 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3653 @param[in] Prompt String ID for Prompt.
3654 @param[in] Help String ID for Help.
3655 @param[in] TextTwo String ID for TextTwo.
3656
3657 @retval NULL There is not enough space left in Buffer to add the opcode.
3658 @retval Other A pointer to the created opcode.
3659
3660 **/
3661 UINT8 *
3662 EFIAPI
3663 HiiCreateTextOpCode (
3664 IN VOID *OpCodeHandle,
3665 IN EFI_STRING_ID Prompt,
3666 IN EFI_STRING_ID Help,
3667 IN EFI_STRING_ID TextTwo
3668 )
3669 {
3670 EFI_IFR_TEXT OpCode;
3671
3672 ZeroMem (&OpCode, sizeof (OpCode));
3673 OpCode.Statement.Prompt = Prompt;
3674 OpCode.Statement.Help = Help;
3675 OpCode.TextTwo = TextTwo;
3676
3677 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_TEXT_OP, sizeof (OpCode));
3678 }
3679
3680 /**
3681 Create EFI_IFR_DATE_OP opcode.
3682
3683 If OpCodeHandle is NULL, then ASSERT().
3684 If any reserved bits are set in QuestionFlags, then ASSERT().
3685 If any reserved bits are set in DateFlags, then ASSERT().
3686
3687 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3688 @param[in] QuestionId Question ID
3689 @param[in] VarStoreId Storage ID, optional. If DateFlags is not
3690 QF_DATE_STORAGE_NORMAL, this parameter is ignored.
3691 @param[in] VarOffset Offset in Storage, optional. If DateFlags is not
3692 QF_DATE_STORAGE_NORMAL, this parameter is ignored.
3693 @param[in] Prompt String ID for Prompt
3694 @param[in] Help String ID for Help
3695 @param[in] QuestionFlags Flags in Question Header
3696 @param[in] DateFlags Flags for date opcode
3697 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3698 is an optional parameter that may be NULL.
3699
3700 @retval NULL There is not enough space left in Buffer to add the opcode.
3701 @retval Other A pointer to the created opcode.
3702
3703 **/
3704 UINT8 *
3705 EFIAPI
3706 HiiCreateDateOpCode (
3707 IN VOID *OpCodeHandle,
3708 IN EFI_QUESTION_ID QuestionId,
3709 IN EFI_VARSTORE_ID VarStoreId, OPTIONAL
3710 IN UINT16 VarOffset, OPTIONAL
3711 IN EFI_STRING_ID Prompt,
3712 IN EFI_STRING_ID Help,
3713 IN UINT8 QuestionFlags,
3714 IN UINT8 DateFlags,
3715 IN VOID *DefaultsOpCodeHandle OPTIONAL
3716 )
3717 {
3718 EFI_IFR_DATE OpCode;
3719 UINTN Position;
3720
3721 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3722 ASSERT ((DateFlags & (~(EFI_QF_DATE_YEAR_SUPPRESS | EFI_QF_DATE_MONTH_SUPPRESS | EFI_QF_DATE_DAY_SUPPRESS | EFI_QF_DATE_STORAGE))) == 0);
3723
3724 ZeroMem (&OpCode, sizeof (OpCode));
3725 OpCode.Question.Header.Prompt = Prompt;
3726 OpCode.Question.Header.Help = Help;
3727 OpCode.Question.QuestionId = QuestionId;
3728 OpCode.Question.VarStoreId = VarStoreId;
3729 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3730 OpCode.Question.Flags = QuestionFlags;
3731 OpCode.Flags = DateFlags;
3732
3733 if (DefaultsOpCodeHandle == NULL) {
3734 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_DATE_OP, sizeof (OpCode));
3735 }
3736
3737 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3738 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_DATE_OP, sizeof (OpCode), 0, 1);
3739 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3740 HiiCreateEndOpCode (OpCodeHandle);
3741 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3742 }
3743
3744 /**
3745 Create EFI_IFR_TIME_OP opcode.
3746
3747 If OpCodeHandle is NULL, then ASSERT().
3748 If any reserved bits are set in QuestionFlags, then ASSERT().
3749 If any reserved bits are set in TimeFlags, then ASSERT().
3750
3751 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3752 @param[in] QuestionId Question ID
3753 @param[in] VarStoreId Storage ID, optional. If TimeFlags is not
3754 QF_TIME_STORAGE_NORMAL, this parameter is ignored.
3755 @param[in] VarOffset Offset in Storage, optional. If TimeFlags is not
3756 QF_TIME_STORAGE_NORMAL, this parameter is ignored.
3757 @param[in] Prompt String ID for Prompt
3758 @param[in] Help String ID for Help
3759 @param[in] QuestionFlags Flags in Question Header
3760 @param[in] TimeFlags Flags for time opcode
3761 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3762 is an optional parameter that may be NULL.
3763
3764 @retval NULL There is not enough space left in Buffer to add the opcode.
3765 @retval Other A pointer to the created opcode.
3766
3767 **/
3768 UINT8 *
3769 EFIAPI
3770 HiiCreateTimeOpCode (
3771 IN VOID *OpCodeHandle,
3772 IN EFI_QUESTION_ID QuestionId,
3773 IN EFI_VARSTORE_ID VarStoreId, OPTIONAL
3774 IN UINT16 VarOffset, OPTIONAL
3775 IN EFI_STRING_ID Prompt,
3776 IN EFI_STRING_ID Help,
3777 IN UINT8 QuestionFlags,
3778 IN UINT8 TimeFlags,
3779 IN VOID *DefaultsOpCodeHandle OPTIONAL
3780 )
3781 {
3782 EFI_IFR_TIME OpCode;
3783 UINTN Position;
3784
3785 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED))) == 0);
3786 ASSERT ((TimeFlags & (~(QF_TIME_HOUR_SUPPRESS | QF_TIME_MINUTE_SUPPRESS | QF_TIME_SECOND_SUPPRESS | QF_TIME_STORAGE))) == 0);
3787
3788 ZeroMem (&OpCode, sizeof (OpCode));
3789 OpCode.Question.Header.Prompt = Prompt;
3790 OpCode.Question.Header.Help = Help;
3791 OpCode.Question.QuestionId = QuestionId;
3792 OpCode.Question.VarStoreId = VarStoreId;
3793 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3794 OpCode.Question.Flags = QuestionFlags;
3795 OpCode.Flags = TimeFlags;
3796
3797 if (DefaultsOpCodeHandle == NULL) {
3798 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_TIME_OP, sizeof (OpCode));
3799 }
3800
3801 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3802 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_TIME_OP, sizeof (OpCode), 0, 1);
3803 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3804 HiiCreateEndOpCode (OpCodeHandle);
3805 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3806 }
3807
3808 /**
3809 This is the internal worker function to update the data in
3810 a form specified by FormSetGuid, FormId and Label.
3811
3812 @param[in] FormSetGuid The optional Formset GUID.
3813 @param[in] FormId The Form ID.
3814 @param[in] Package The package header.
3815 @param[in] OpCodeBufferStart An OpCode buffer that contains the set of IFR
3816 opcodes to be inserted or replaced in the form.
3817 @param[in] OpCodeBufferEnd An OpCcode buffer that contains the IFR opcode
3818 that marks the end of a replace operation in the form.
3819 @param[out] TempPackage The resultant package.
3820
3821 @retval EFI_SUCCESS The function completes successfully.
3822 @retval EFI_NOT_FOUND The updated opcode or endopcode is not found.
3823
3824 **/
3825 EFI_STATUS
3826 EFIAPI
3827 InternalHiiUpdateFormPackageData (
3828 IN EFI_GUID *FormSetGuid, OPTIONAL
3829 IN EFI_FORM_ID FormId,
3830 IN EFI_HII_PACKAGE_HEADER *Package,
3831 IN HII_LIB_OPCODE_BUFFER *OpCodeBufferStart,
3832 IN HII_LIB_OPCODE_BUFFER *OpCodeBufferEnd, OPTIONAL
3833 OUT EFI_HII_PACKAGE_HEADER *TempPackage
3834 )
3835 {
3836 UINTN AddSize;
3837 UINT8 *BufferPos;
3838 EFI_HII_PACKAGE_HEADER PackageHeader;
3839 UINTN Offset;
3840 EFI_IFR_OP_HEADER *IfrOpHdr;
3841 EFI_IFR_OP_HEADER *UpdateIfrOpHdr;
3842 BOOLEAN GetFormSet;
3843 BOOLEAN GetForm;
3844 BOOLEAN Updated;
3845 UINTN UpdatePackageLength;
3846
3847 CopyMem (TempPackage, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3848 UpdatePackageLength = sizeof (EFI_HII_PACKAGE_HEADER);
3849 BufferPos = (UINT8 *) (TempPackage + 1);
3850
3851 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3852 IfrOpHdr = (EFI_IFR_OP_HEADER *)((UINT8 *) Package + sizeof (EFI_HII_PACKAGE_HEADER));
3853 Offset = sizeof (EFI_HII_PACKAGE_HEADER);
3854 GetFormSet = (BOOLEAN) ((FormSetGuid == NULL) ? TRUE : FALSE);
3855 GetForm = FALSE;
3856 Updated = FALSE;
3857
3858 while (Offset < PackageHeader.Length) {
3859 CopyMem (BufferPos, IfrOpHdr, IfrOpHdr->Length);
3860 BufferPos += IfrOpHdr->Length;
3861 UpdatePackageLength += IfrOpHdr->Length;
3862
3863 //
3864 // Find the matched FormSet and Form
3865 //
3866 if ((IfrOpHdr->OpCode == EFI_IFR_FORM_SET_OP) && (FormSetGuid != NULL)) {
3867 if (CompareGuid((GUID *)(VOID *)&((EFI_IFR_FORM_SET *) IfrOpHdr)->Guid, FormSetGuid)) {
3868 GetFormSet = TRUE;
3869 } else {
3870 GetFormSet = FALSE;
3871 }
3872 } else if (IfrOpHdr->OpCode == EFI_IFR_FORM_OP || IfrOpHdr->OpCode == EFI_IFR_FORM_MAP_OP) {
3873 if (CompareMem (&((EFI_IFR_FORM *) IfrOpHdr)->FormId, &FormId, sizeof (EFI_FORM_ID)) == 0) {
3874 GetForm = TRUE;
3875 } else {
3876 GetForm = FALSE;
3877 }
3878 }
3879
3880 //
3881 // The matched Form is found, and Update data in this form
3882 //
3883 if (GetFormSet && GetForm) {
3884 UpdateIfrOpHdr = (EFI_IFR_OP_HEADER *) OpCodeBufferStart->Buffer;
3885 if ((UpdateIfrOpHdr->Length == IfrOpHdr->Length) && \
3886 (CompareMem (IfrOpHdr, UpdateIfrOpHdr, UpdateIfrOpHdr->Length) == 0)) {
3887 //
3888 // Remove the original data when End OpCode buffer exist.
3889 //
3890 if (OpCodeBufferEnd != NULL) {
3891 Offset += IfrOpHdr->Length;
3892 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((UINT8 *) (IfrOpHdr) + IfrOpHdr->Length);
3893 UpdateIfrOpHdr = (EFI_IFR_OP_HEADER *) OpCodeBufferEnd->Buffer;
3894 while (Offset < PackageHeader.Length) {
3895 //
3896 // Search the matched end opcode
3897 //
3898 if ((UpdateIfrOpHdr->Length == IfrOpHdr->Length) && \
3899 (CompareMem (IfrOpHdr, UpdateIfrOpHdr, UpdateIfrOpHdr->Length) == 0)) {
3900 break;
3901 }
3902 //
3903 // Go to the next Op-Code
3904 //
3905 Offset += IfrOpHdr->Length;
3906 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((UINT8 *) (IfrOpHdr) + IfrOpHdr->Length);
3907 }
3908
3909 if (Offset >= PackageHeader.Length) {
3910 //
3911 // The end opcode is not found.
3912 //
3913 return EFI_NOT_FOUND;
3914 }
3915 }
3916
3917 //
3918 // Insert the updated data
3919 //
3920 AddSize = ((EFI_IFR_OP_HEADER *) OpCodeBufferStart->Buffer)->Length;
3921 CopyMem (BufferPos, OpCodeBufferStart->Buffer + AddSize, OpCodeBufferStart->Position - AddSize);
3922 BufferPos += OpCodeBufferStart->Position - AddSize;
3923 UpdatePackageLength += OpCodeBufferStart->Position - AddSize;
3924
3925 if (OpCodeBufferEnd != NULL) {
3926 //
3927 // Add the end opcode
3928 //
3929 CopyMem (BufferPos, IfrOpHdr, IfrOpHdr->Length);
3930 BufferPos += IfrOpHdr->Length;
3931 UpdatePackageLength += IfrOpHdr->Length;
3932 }
3933
3934 //
3935 // Copy the left package data.
3936 //
3937 Offset += IfrOpHdr->Length;
3938 CopyMem (BufferPos, (UINT8 *) Package + Offset, PackageHeader.Length - Offset);
3939 UpdatePackageLength += PackageHeader.Length - Offset;
3940
3941 //
3942 // Set update flag
3943 //
3944 Updated = TRUE;
3945 break;
3946 }
3947 }
3948
3949 //
3950 // Go to the next Op-Code
3951 //
3952 Offset += IfrOpHdr->Length;
3953 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (IfrOpHdr) + IfrOpHdr->Length);
3954 }
3955
3956 if (!Updated) {
3957 //
3958 // The updated opcode buffer is not found.
3959 //
3960 return EFI_NOT_FOUND;
3961 }
3962 //
3963 // Update the package length.
3964 //
3965 PackageHeader.Length = (UINT32) UpdatePackageLength;
3966 CopyMem (TempPackage, &PackageHeader, sizeof (EFI_HII_PACKAGE_HEADER));
3967
3968 return EFI_SUCCESS;
3969 }
3970
3971 /**
3972 This function updates a form that has previously been registered with the HII
3973 Database. This function will perform at most one update operation.
3974
3975 The form to update is specified by Handle, FormSetGuid, and FormId. Binary
3976 comparisons of IFR opcodes are performed from the beginning of the form being
3977 updated until an IFR opcode is found that exactly matches the first IFR opcode
3978 specified by StartOpCodeHandle. The following rules are used to determine if
3979 an insert, replace, or delete operation is performed.
3980
3981 1) If no matches are found, then NULL is returned.
3982 2) If a match is found, and EndOpCodeHandle is NULL, then all of the IFR opcodes
3983 from StartOpCodeHandle except the first opcode are inserted immediately after
3984 the matching IFR opcode in the form to be updated.
3985 3) If a match is found, and EndOpCodeHandle is not NULL, then a search is made
3986 from the matching IFR opcode until an IFR opcode exactly matches the first
3987 IFR opcode specified by EndOpCodeHandle. If no match is found for the first
3988 IFR opcode specified by EndOpCodeHandle, then NULL is returned. If a match
3989 is found, then all of the IFR opcodes between the start match and the end
3990 match are deleted from the form being updated and all of the IFR opcodes
3991 from StartOpCodeHandle except the first opcode are inserted immediately after
3992 the matching start IFR opcode. If StartOpCcodeHandle only contains one
3993 IFR instruction, then the result of this operation will delete all of the IFR
3994 opcodes between the start end matches.
3995
3996 If HiiHandle is NULL, then ASSERT().
3997 If StartOpCodeHandle is NULL, then ASSERT().
3998
3999 @param[in] HiiHandle The HII Handle of the form to update.
4000 @param[in] FormSetGuid The Formset GUID of the form to update. This
4001 is an optional parameter that may be NULL.
4002 If it is NULL, all FormSet will be updated.
4003 @param[in] FormId The ID of the form to update.
4004 @param[in] StartOpCodeHandle An OpCode Handle that contains the set of IFR
4005 opcodes to be inserted or replaced in the form.
4006 The first IFR instruction in StartOpCodeHandle
4007 is used to find matching IFR opcode in the
4008 form.
4009 @param[in] EndOpCodeHandle An OpCcode Handle that contains the IFR opcode
4010 that marks the end of a replace operation in
4011 the form. This is an optional parameter that
4012 may be NULL. If it is NULL, then an the IFR
4013 opcodes specified by StartOpCodeHandle are
4014 inserted into the form.
4015
4016 @retval EFI_OUT_OF_RESOURCES No enough memory resource is allocated.
4017 @retval EFI_NOT_FOUND The following cases will return EFI_NOT_FOUND.
4018 1) The form specified by HiiHandle, FormSetGuid,
4019 and FormId could not be found in the HII Database.
4020 2) No IFR opcodes in the target form match the first
4021 IFR opcode in StartOpCodeHandle.
4022 3) EndOpCOde is not NULL, and no IFR opcodes in the
4023 target form following a matching start opcode match
4024 the first IFR opcode in EndOpCodeHandle.
4025 @retval EFI_SUCCESS The matched form is updated by StartOpcode.
4026
4027 **/
4028 EFI_STATUS
4029 EFIAPI
4030 HiiUpdateForm (
4031 IN EFI_HII_HANDLE HiiHandle,
4032 IN EFI_GUID *FormSetGuid, OPTIONAL
4033 IN EFI_FORM_ID FormId,
4034 IN VOID *StartOpCodeHandle,
4035 IN VOID *EndOpCodeHandle OPTIONAL
4036 )
4037 {
4038 EFI_STATUS Status;
4039 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
4040 UINT32 PackageListLength;
4041 UINT32 Offset;
4042 EFI_HII_PACKAGE_LIST_HEADER *UpdatePackageList;
4043 UINTN BufferSize;
4044 UINT8 *UpdateBufferPos;
4045 EFI_HII_PACKAGE_HEADER *Package;
4046 EFI_HII_PACKAGE_HEADER *TempPacakge;
4047 EFI_HII_PACKAGE_HEADER PackageHeader;
4048 BOOLEAN Updated;
4049 HII_LIB_OPCODE_BUFFER *OpCodeBufferStart;
4050 HII_LIB_OPCODE_BUFFER *OpCodeBufferEnd;
4051
4052 //
4053 // Input update data can't be NULL.
4054 //
4055 ASSERT (HiiHandle != NULL);
4056 ASSERT (StartOpCodeHandle != NULL);
4057 UpdatePackageList = NULL;
4058 TempPacakge = NULL;
4059 HiiPackageList = NULL;
4060
4061 //
4062 // Retrieve buffer data from Opcode Handle
4063 //
4064 OpCodeBufferStart = (HII_LIB_OPCODE_BUFFER *) StartOpCodeHandle;
4065 OpCodeBufferEnd = (HII_LIB_OPCODE_BUFFER *) EndOpCodeHandle;
4066
4067 //
4068 // Get the original package list
4069 //
4070 BufferSize = 0;
4071 HiiPackageList = NULL;
4072 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
4073 //
4074 // The return status should always be EFI_BUFFER_TOO_SMALL as input buffer's size is 0.
4075 //
4076 if (Status != EFI_BUFFER_TOO_SMALL) {
4077 return Status;
4078 }
4079
4080 HiiPackageList = AllocatePool (BufferSize);
4081 if (HiiPackageList == NULL) {
4082 Status = EFI_OUT_OF_RESOURCES;
4083 goto Finish;
4084 }
4085
4086 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
4087 if (EFI_ERROR (Status)) {
4088 goto Finish;
4089 }
4090
4091 //
4092 // Calculate and allocate space for retrieval of IFR data
4093 //
4094 BufferSize += OpCodeBufferStart->Position;
4095 UpdatePackageList = AllocateZeroPool (BufferSize);
4096 if (UpdatePackageList == NULL) {
4097 Status = EFI_OUT_OF_RESOURCES;
4098 goto Finish;
4099 }
4100
4101 //
4102 // Allocate temp buffer to store the temp updated package buffer
4103 //
4104 TempPacakge = AllocateZeroPool (BufferSize);
4105 if (TempPacakge == NULL) {
4106 Status = EFI_OUT_OF_RESOURCES;
4107 goto Finish;
4108 }
4109
4110 UpdateBufferPos = (UINT8 *) UpdatePackageList;
4111
4112 //
4113 // Copy the package list header
4114 //
4115 CopyMem (UpdateBufferPos, HiiPackageList, sizeof (EFI_HII_PACKAGE_LIST_HEADER));
4116 UpdateBufferPos += sizeof (EFI_HII_PACKAGE_LIST_HEADER);
4117
4118 //
4119 // Go through each package to find the matched package and update one by one
4120 //
4121 Updated = FALSE;
4122 Offset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
4123 PackageListLength = ReadUnaligned32 (&HiiPackageList->PackageLength);
4124 while (Offset < PackageListLength) {
4125 Package = (EFI_HII_PACKAGE_HEADER *) (((UINT8 *) HiiPackageList) + Offset);
4126 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
4127 Offset += Package->Length;
4128
4129 if (Package->Type == EFI_HII_PACKAGE_FORMS) {
4130 //
4131 // Check this package is the matched package.
4132 //
4133 Status = InternalHiiUpdateFormPackageData (FormSetGuid, FormId, Package, OpCodeBufferStart, OpCodeBufferEnd, TempPacakge);
4134 //
4135 // The matched package is found. Its package buffer will be updated by the input new data.
4136 //
4137 if (!EFI_ERROR(Status)) {
4138 //
4139 // Set Update Flag
4140 //
4141 Updated = TRUE;
4142 //
4143 // Add updated package buffer
4144 //
4145 Package = TempPacakge;
4146 }
4147 }
4148
4149 //
4150 // Add pacakge buffer
4151 //
4152 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
4153 CopyMem (UpdateBufferPos, Package, PackageHeader.Length);
4154 UpdateBufferPos += PackageHeader.Length;
4155 }
4156
4157 if (Updated) {
4158 //
4159 // Update package list length
4160 //
4161 BufferSize = UpdateBufferPos - (UINT8 *) UpdatePackageList;
4162 WriteUnaligned32 (&UpdatePackageList->PackageLength, (UINT32) BufferSize);
4163
4164 //
4165 // Update Package to show form
4166 //
4167 Status = gHiiDatabase->UpdatePackageList (gHiiDatabase, HiiHandle, UpdatePackageList);
4168 } else {
4169 //
4170 // Not matched form is found and updated.
4171 //
4172 Status = EFI_NOT_FOUND;
4173 }
4174
4175 Finish:
4176 if (HiiPackageList != NULL) {
4177 FreePool (HiiPackageList);
4178 }
4179
4180 if (UpdatePackageList != NULL) {
4181 FreePool (UpdatePackageList);
4182 }
4183
4184 if (TempPacakge != NULL) {
4185 FreePool (TempPacakge);
4186 }
4187
4188 return Status;
4189 }