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