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