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