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