]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiHiiLib/HiiLib.c
b9323706a6f24f95462bb2101224e818d4381f29
[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 function shares the same logic to parse ConfigAltResp string
925 for setting default value and validating current setting.
926
927 @param ConfigResp
928 @param HiiPackageList
929 @param PackageListLength
930 @param VarGuid
931 @param VarName
932
933 @retval EFI_SUCCESS
934 **/
935 EFI_STATUS
936 EFIAPI
937 InternalHiiValidateCurrentSetting (
938 IN EFI_STRING ConfigResp,
939 IN EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList,
940 IN UINTN PackageListLength,
941 IN EFI_GUID *VarGuid,
942 IN CHAR16 *VarName
943 )
944 {
945 IFR_BLOCK_DATA *CurrentBlockArray;
946 IFR_BLOCK_DATA *BlockData;
947 IFR_BLOCK_DATA *NewBlockData;
948 IFR_BLOCK_DATA VarBlockData;
949 EFI_STRING StringPtr;
950 UINTN Length;
951 UINT8 *TmpBuffer;
952 UINT16 Offset;
953 UINT16 Width;
954 UINT64 VarValue;
955 LIST_ENTRY *Link;
956 UINT8 *VarBuffer;
957 UINTN MaxBufferSize;
958 EFI_STATUS Status;
959 EFI_HII_PACKAGE_HEADER PacakgeHeader;
960 UINT32 PackageOffset;
961 UINT8 *PackageData;
962 UINTN IfrOffset;
963 EFI_IFR_OP_HEADER *IfrOpHdr;
964 EFI_IFR_VARSTORE *IfrVarStore;
965 EFI_IFR_ONE_OF *IfrOneOf;
966 EFI_IFR_NUMERIC *IfrNumeric;
967 EFI_IFR_ONE_OF_OPTION *IfrOneOfOption;
968 EFI_IFR_CHECKBOX *IfrCheckBox;
969 EFI_IFR_STRING *IfrString;
970 CHAR8 *VarStoreName;
971 UINTN Index;
972
973 //
974 // 1. Get the current setting to current block data array and Convert them into VarBuffer
975 //
976
977 //
978 // Skip ConfigHdr string
979 //
980 StringPtr = ConfigResp;
981 StringPtr = StrStr (ConfigResp, L"&OFFSET");
982 if (StringPtr == NULL) {
983 //
984 // No ConfigBlock value is requied to be validated.
985 // EFI_SUCCESS directly return.
986 //
987 return EFI_SUCCESS;
988 }
989
990 //
991 // Initialize the local variables.
992 //
993 Index = 0;
994 VarStoreName = NULL;
995 Status = EFI_SUCCESS;
996 BlockData = NULL;
997 NewBlockData = NULL;
998 TmpBuffer = NULL;
999 MaxBufferSize = HII_LIB_DEFAULT_VARSTORE_SIZE;
1000 VarBuffer = AllocateZeroPool (MaxBufferSize);
1001 if (VarBuffer == NULL) {
1002 return EFI_OUT_OF_RESOURCES;
1003 }
1004
1005 //
1006 // Init CurrentBlockArray
1007 //
1008 CurrentBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1009 if (CurrentBlockArray == NULL) {
1010 Status = EFI_OUT_OF_RESOURCES;
1011 goto Done;
1012 }
1013 InitializeListHead (&CurrentBlockArray->Entry);
1014
1015 //
1016 // Parse each <RequestElement> if exists
1017 // Only <BlockName> format is supported by this help function.
1018 // <BlockName> ::= &'OFFSET='<Number>&'WIDTH='<Number>
1019 //
1020 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
1021 //
1022 // Skip the &OFFSET= string
1023 //
1024 StringPtr += StrLen (L"&OFFSET=");
1025
1026 //
1027 // Get Offset
1028 //
1029 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1030 if (EFI_ERROR (Status)) {
1031 goto Done;
1032 }
1033 Offset = 0;
1034 CopyMem (
1035 &Offset,
1036 TmpBuffer,
1037 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1038 );
1039 FreePool (TmpBuffer);
1040 TmpBuffer = NULL;
1041
1042 StringPtr += Length;
1043 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
1044 Status = EFI_INVALID_PARAMETER;
1045 goto Done;
1046 }
1047 StringPtr += StrLen (L"&WIDTH=");
1048
1049 //
1050 // Get Width
1051 //
1052 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1053 if (EFI_ERROR (Status)) {
1054 goto Done;
1055 }
1056 Width = 0;
1057 CopyMem (
1058 &Width,
1059 TmpBuffer,
1060 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1061 );
1062 FreePool (TmpBuffer);
1063 TmpBuffer = NULL;
1064
1065 StringPtr += Length;
1066 if (*StringPtr != 0 && *StringPtr != L'&') {
1067 Status = EFI_INVALID_PARAMETER;
1068 goto Done;
1069 }
1070
1071 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
1072 Status = EFI_INVALID_PARAMETER;
1073 goto Done;
1074 }
1075 StringPtr += StrLen (L"&VALUE=");
1076
1077 //
1078 // Get Value
1079 //
1080 Status = InternalHiiGetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1081 if (EFI_ERROR (Status)) {
1082 goto Done;
1083 }
1084
1085 StringPtr += Length;
1086 if (*StringPtr != 0 && *StringPtr != L'&') {
1087 Status = EFI_INVALID_PARAMETER;
1088 goto Done;
1089 }
1090
1091 //
1092 // Check whether VarBuffer is enough
1093 //
1094 if ((UINTN) (Offset + Width) > MaxBufferSize) {
1095 VarBuffer = ReallocatePool (
1096 MaxBufferSize,
1097 Offset + Width + HII_LIB_DEFAULT_VARSTORE_SIZE,
1098 VarBuffer
1099 );
1100 if (VarBuffer == NULL) {
1101 Status = EFI_OUT_OF_RESOURCES;
1102 goto Done;
1103 }
1104 MaxBufferSize = Offset + Width + HII_LIB_DEFAULT_VARSTORE_SIZE;
1105 }
1106
1107 //
1108 // Update the Block with configuration info
1109 //
1110 CopyMem (VarBuffer + Offset, TmpBuffer, Width);
1111 FreePool (TmpBuffer);
1112 TmpBuffer = NULL;
1113
1114 //
1115 // Set new Block Data
1116 //
1117 NewBlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1118 if (NewBlockData == NULL) {
1119 Status = EFI_OUT_OF_RESOURCES;
1120 goto Done;
1121 }
1122 NewBlockData->Offset = Offset;
1123 NewBlockData->Width = Width;
1124
1125 //
1126 // Insert the new block data into the block data array.
1127 //
1128 for (Link = CurrentBlockArray->Entry.ForwardLink; Link != &CurrentBlockArray->Entry; Link = Link->ForwardLink) {
1129 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1130 if (NewBlockData->Offset == BlockData->Offset) {
1131 if (NewBlockData->Width > BlockData->Width) {
1132 BlockData->Width = NewBlockData->Width;
1133 }
1134 FreePool (NewBlockData);
1135 break;
1136 } else if (NewBlockData->Offset < BlockData->Offset) {
1137 //
1138 // Insert new block data as the previous one of this link.
1139 //
1140 InsertTailList (Link, &NewBlockData->Entry);
1141 break;
1142 }
1143 }
1144
1145 //
1146 // Insert new block data into the array tail.
1147 //
1148 if (Link == &CurrentBlockArray->Entry) {
1149 InsertTailList (Link, &NewBlockData->Entry);
1150 }
1151
1152 //
1153 // If '\0', parsing is finished.
1154 //
1155 if (*StringPtr == 0) {
1156 break;
1157 }
1158 //
1159 // Go to next ConfigBlock
1160 //
1161 }
1162
1163 //
1164 // Merge the aligned block data into the single block data.
1165 //
1166 Link = CurrentBlockArray->Entry.ForwardLink;
1167 while ((Link != &CurrentBlockArray->Entry) && (Link->ForwardLink != &CurrentBlockArray->Entry)) {
1168 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1169 NewBlockData = BASE_CR (Link->ForwardLink, IFR_BLOCK_DATA, Entry);
1170 if ((NewBlockData->Offset >= BlockData->Offset) && (NewBlockData->Offset <= (BlockData->Offset + BlockData->Width))) {
1171 if ((NewBlockData->Offset + NewBlockData->Width) > (BlockData->Offset + BlockData->Width)) {
1172 BlockData->Width = (UINT16) (NewBlockData->Offset + NewBlockData->Width - BlockData->Offset);
1173 }
1174 RemoveEntryList (Link->ForwardLink);
1175 FreePool (NewBlockData);
1176 continue;
1177 }
1178 Link = Link->ForwardLink;
1179 }
1180
1181 if (IsListEmpty (&CurrentBlockArray->Entry)) {
1182 Status = EFI_SUCCESS;
1183 goto Done;
1184 }
1185
1186 //
1187 // 2. Check IFR value is in block data, then Validate Vaule
1188 //
1189 ZeroMem (&VarBlockData, sizeof (VarBlockData));
1190 VarValue = 0;
1191 IfrVarStore = NULL;
1192 PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
1193 while (PackageOffset < PackageListLength) {
1194 CopyMem (&PacakgeHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PacakgeHeader));
1195
1196 //
1197 // Parse IFR opcode from the form package.
1198 //
1199 if (PacakgeHeader.Type == EFI_HII_PACKAGE_FORMS) {
1200 IfrOffset = sizeof (PacakgeHeader);
1201 PackageData = (UINT8 *) HiiPackageList + PackageOffset;
1202 while (IfrOffset < PacakgeHeader.Length) {
1203 IfrOpHdr = (EFI_IFR_OP_HEADER *) (PackageData + IfrOffset);
1204 //
1205 // Validate current setting to the value built in IFR opcode
1206 //
1207 switch (IfrOpHdr->OpCode) {
1208 case EFI_IFR_VARSTORE_OP:
1209 //
1210 // VarStoreId has been found. No further found.
1211 //
1212 if (IfrVarStore != NULL) {
1213 break;
1214 }
1215 //
1216 // Find the matched VarStoreId to the input VarGuid and VarName
1217 //
1218 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
1219 if (CompareGuid ((EFI_GUID *) (VOID *) &IfrVarStore->Guid, VarGuid)) {
1220 VarStoreName = (CHAR8 *) IfrVarStore->Name;
1221 for (Index = 0; VarStoreName[Index] != 0; Index ++) {
1222 if ((CHAR16) VarStoreName[Index] != VarName[Index]) {
1223 break;
1224 }
1225 }
1226 //
1227 // The matched VarStore is found.
1228 //
1229 if ((VarStoreName[Index] != 0) || (VarName[Index] != 0)) {
1230 IfrVarStore = NULL;
1231 }
1232 } else {
1233 IfrVarStore = NULL;
1234 }
1235 break;
1236 case EFI_IFR_FORM_OP:
1237 //
1238 // Check the matched VarStoreId is found.
1239 //
1240 if (IfrVarStore == NULL) {
1241 Status = EFI_SUCCESS;
1242 goto Done;
1243 }
1244 break;
1245 case EFI_IFR_ONE_OF_OP:
1246 //
1247 // Check whether current value is the one of option.
1248 //
1249
1250 //
1251 // Check whether this question is for the requested varstore.
1252 //
1253 IfrOneOf = (EFI_IFR_ONE_OF *) IfrOpHdr;
1254 if (IfrOneOf->Question.VarStoreId != IfrVarStore->VarStoreId) {
1255 break;
1256 }
1257
1258 //
1259 // Get Offset by Question header and Width by DataType Flags
1260 //
1261 Offset = IfrOneOf->Question.VarStoreInfo.VarOffset;
1262 Width = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));
1263 //
1264 // Check whether this question is in current block array.
1265 //
1266 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1267 //
1268 // This question is not in the current configuration string. Skip it.
1269 //
1270 break;
1271 }
1272 //
1273 // Check this var question is in the var storage
1274 //
1275 if ((Offset + Width) > IfrVarStore->Size) {
1276 //
1277 // This question exceeds the var store size.
1278 //
1279 Status = EFI_INVALID_PARAMETER;
1280 goto Done;
1281 }
1282
1283 //
1284 // Get the current value for oneof opcode
1285 //
1286 VarValue = 0;
1287 CopyMem (&VarValue, VarBuffer + Offset, Width);
1288 //
1289 // Set Block Data, to be checked in the following Oneof option opcode.
1290 //
1291 VarBlockData.Offset = Offset;
1292 VarBlockData.Width = Width;
1293 VarBlockData.OpCode = IfrOpHdr->OpCode;
1294 VarBlockData.Scope = IfrOpHdr->Scope;
1295 break;
1296 case EFI_IFR_NUMERIC_OP:
1297 //
1298 // Check the current value is in the numeric range.
1299 //
1300
1301 //
1302 // Check whether this question is for the requested varstore.
1303 //
1304 IfrNumeric = (EFI_IFR_NUMERIC *) IfrOpHdr;
1305 if (IfrNumeric->Question.VarStoreId != IfrVarStore->VarStoreId) {
1306 break;
1307 }
1308
1309 //
1310 // Get Offset by Question header and Width by DataType Flags
1311 //
1312 Offset = IfrNumeric->Question.VarStoreInfo.VarOffset;
1313 Width = (UINT16) (1 << (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE));
1314 //
1315 // Check whether this question is in current block array.
1316 //
1317 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1318 //
1319 // This question is not in the current configuration string. Skip it.
1320 //
1321 break;
1322 }
1323 //
1324 // Check this var question is in the var storage
1325 //
1326 if ((Offset + Width) > IfrVarStore->Size) {
1327 //
1328 // This question exceeds the var store size.
1329 //
1330 Status = EFI_INVALID_PARAMETER;
1331 goto Done;
1332 }
1333
1334 //
1335 // Check the current value is in the numeric range.
1336 //
1337 VarValue = 0;
1338 CopyMem (&VarValue, VarBuffer + Offset, Width);
1339 switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) {
1340 case EFI_IFR_NUMERIC_SIZE_1:
1341 if ((UINT8) VarValue < IfrNumeric->data.u8.MinValue || (UINT8) VarValue > IfrNumeric->data.u8.MaxValue) {
1342 //
1343 // Not in the valid range.
1344 //
1345 Status = EFI_INVALID_PARAMETER;
1346 goto Done;
1347 }
1348 break;
1349 case EFI_IFR_NUMERIC_SIZE_2:
1350 if ((UINT16) VarValue < IfrNumeric->data.u16.MinValue || (UINT16) VarValue > IfrNumeric->data.u16.MaxValue) {
1351 //
1352 // Not in the valid range.
1353 //
1354 Status = EFI_INVALID_PARAMETER;
1355 goto Done;
1356 }
1357 break;
1358 case EFI_IFR_NUMERIC_SIZE_4:
1359 if ((UINT32) VarValue < IfrNumeric->data.u32.MinValue || (UINT32) VarValue > IfrNumeric->data.u32.MaxValue) {
1360 //
1361 // Not in the valid range.
1362 //
1363 Status = EFI_INVALID_PARAMETER;
1364 goto Done;
1365 }
1366 break;
1367 case EFI_IFR_NUMERIC_SIZE_8:
1368 if ((UINT64) VarValue < IfrNumeric->data.u64.MinValue || (UINT64) VarValue > IfrNumeric->data.u64.MaxValue) {
1369 //
1370 // Not in the valid range.
1371 //
1372 Status = EFI_INVALID_PARAMETER;
1373 goto Done;
1374 }
1375 break;
1376 }
1377
1378 break;
1379 case EFI_IFR_CHECKBOX_OP:
1380 //
1381 // Check value is BOOLEAN type, only 0 and 1 is valid.
1382 //
1383
1384 //
1385 // Check whether this question is for the requested varstore.
1386 //
1387 IfrCheckBox = (EFI_IFR_CHECKBOX *) IfrOpHdr;
1388 if (IfrCheckBox->Question.VarStoreId != IfrVarStore->VarStoreId) {
1389 break;
1390 }
1391
1392 //
1393 // Get Offset by Question header
1394 //
1395 Offset = IfrCheckBox->Question.VarStoreInfo.VarOffset;
1396 Width = sizeof (BOOLEAN);
1397 //
1398 // Check whether this question is in current block array.
1399 //
1400 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1401 //
1402 // This question is not in the current configuration string. Skip it.
1403 //
1404 break;
1405 }
1406 //
1407 // Check this var question is in the var storage
1408 //
1409 if ((Offset + Width) > IfrVarStore->Size) {
1410 //
1411 // This question exceeds the var store size.
1412 //
1413 Status = EFI_INVALID_PARAMETER;
1414 goto Done;
1415 }
1416
1417 //
1418 // Boolean type, only 1 and 0 is valid.
1419 //
1420 if (*(VarBuffer + Offset) > 1) {
1421 Status = EFI_INVALID_PARAMETER;
1422 goto Done;
1423 }
1424
1425 break;
1426 case EFI_IFR_STRING_OP:
1427 //
1428 // Check current string length is less than maxsize
1429 //
1430
1431 //
1432 // Check whether this question is for the requested varstore.
1433 //
1434 IfrString = (EFI_IFR_STRING *) IfrOpHdr;
1435 if (IfrString->Question.VarStoreId != IfrVarStore->VarStoreId) {
1436 break;
1437 }
1438
1439 //
1440 // Get Offset/Width by Question header and OneOf Flags
1441 //
1442 Offset = IfrString->Question.VarStoreInfo.VarOffset;
1443 Width = (UINT16) (IfrString->MaxSize * sizeof (UINT16));
1444 //
1445 // Check whether this question is in current block array.
1446 //
1447 if (!BlockArrayCheck (CurrentBlockArray, Offset, Width)) {
1448 //
1449 // This question is not in the current configuration string. Skip it.
1450 //
1451 break;
1452 }
1453 //
1454 // Check this var question is in the var storage
1455 //
1456 if ((Offset + Width) > IfrVarStore->Size) {
1457 //
1458 // This question exceeds the var store size.
1459 //
1460 Status = EFI_INVALID_PARAMETER;
1461 goto Done;
1462 }
1463
1464 //
1465 // Check current string length is less than maxsize
1466 //
1467 if (StrSize ((CHAR16 *) (VarBuffer + Offset)) > Width) {
1468 Status = EFI_INVALID_PARAMETER;
1469 goto Done;
1470 }
1471 break;
1472 case EFI_IFR_ONE_OF_OPTION_OP:
1473 //
1474 // Opcode Scope is zero. This one of option is not to be checked.
1475 //
1476 if (VarBlockData.Scope == 0) {
1477 break;
1478 }
1479
1480 //
1481 // Only check for OneOf and OrderList opcode
1482 //
1483 IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
1484 if (VarBlockData.OpCode == EFI_IFR_ONE_OF_OP) {
1485 //
1486 // Check current value is the value of one of option.
1487 //
1488 if (VarValue == IfrOneOfOption->Value.u64) {
1489 //
1490 // The value is one of option value.
1491 // Set OpCode to Zero, don't need check again.
1492 //
1493 VarBlockData.OpCode = 0;
1494 }
1495 }
1496
1497 break;
1498 case EFI_IFR_END_OP:
1499 //
1500 // Decrease opcode scope for the validated opcode
1501 //
1502 if (VarBlockData.Scope > 0) {
1503 VarBlockData.Scope --;
1504 }
1505
1506 //
1507 // OneOf value doesn't belong to one of option value.
1508 //
1509 if (VarBlockData.OpCode == EFI_IFR_ONE_OF_OP) {
1510 Status = EFI_INVALID_PARAMETER;
1511 goto Done;
1512 }
1513 break;
1514 default:
1515 //
1516 // Increase Scope for the validated opcode
1517 //
1518 if (VarBlockData.Scope > 0) {
1519 VarBlockData.Scope = (UINT8) (VarBlockData.Scope + IfrOpHdr->Scope);
1520 }
1521 break;
1522 }
1523 //
1524 // Go to the next opcode
1525 //
1526 IfrOffset += IfrOpHdr->Length;
1527 }
1528 //
1529 // Only one form is in a package list.
1530 //
1531 break;
1532 }
1533
1534 //
1535 // Go to next package.
1536 //
1537 PackageOffset += PacakgeHeader.Length;
1538 }
1539
1540 Done:
1541 if (VarBuffer != NULL) {
1542 FreePool (VarBuffer);
1543 }
1544
1545 if (CurrentBlockArray != NULL) {
1546 //
1547 // Free Link Array CurrentBlockArray
1548 //
1549 while (!IsListEmpty (&CurrentBlockArray->Entry)) {
1550 BlockData = BASE_CR (CurrentBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
1551 RemoveEntryList (&BlockData->Entry);
1552 FreePool (BlockData);
1553 }
1554 FreePool (CurrentBlockArray);
1555 }
1556
1557 return Status;
1558 }
1559
1560 /**
1561 This function shares the same logic to parse ConfigAltResp string
1562 for setting default value and validating current setting.
1563
1564 1. For setting default action, Reset the default value specified by DefaultId
1565 to the driver configuration got by Request string.
1566 2. For validating current setting, Validate the current configuration
1567 by parsing HII form IFR opcode.
1568
1569 NULL request string support depends on the ExportConfig interface of
1570 HiiConfigRouting protocol in UEFI specification.
1571
1572 @param Request A null-terminated Unicode string in
1573 <MultiConfigRequest> format. It can be NULL.
1574 If it is NULL, all current configuration for the
1575 entirety of the current HII database will be validated.
1576 If it is NULL, all configuration for the
1577 entirety of the current HII database will be reset.
1578 @param DefaultId Specifies the type of defaults to retrieve only for setting default action.
1579 @param ActionType Action supports setting defaults and validate current setting.
1580
1581 @retval TURE Action runs successfully.
1582 @retval FALSE Action is not valid or Action can't be executed successfully..
1583 **/
1584 BOOLEAN
1585 EFIAPI
1586 InternalHiiIfrValueAction (
1587 IN CONST EFI_STRING Request, OPTIONAL
1588 IN UINT16 DefaultId,
1589 IN UINT8 ActionType
1590 )
1591 {
1592 EFI_STRING ConfigAltResp;
1593 EFI_STRING ConfigAltHdr;
1594 EFI_STRING ConfigResp;
1595 EFI_STRING Progress;
1596 EFI_STRING StringPtr;
1597 EFI_STRING StringHdr;
1598 EFI_STATUS Status;
1599 EFI_HANDLE DriverHandle;
1600 EFI_HANDLE TempDriverHandle;
1601 EFI_HII_HANDLE *HiiHandleBuffer;
1602 EFI_HII_HANDLE HiiHandle;
1603 UINT32 Index;
1604 EFI_GUID *VarGuid;
1605 EFI_STRING VarName;
1606 EFI_STRING_ID DefaultName;
1607
1608 UINT8 *PackageData;
1609 UINTN IfrOffset;
1610 EFI_IFR_OP_HEADER *IfrOpHdr;
1611 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
1612 UINT32 PackageOffset;
1613 UINTN PackageListLength;
1614 EFI_HII_PACKAGE_HEADER PacakgeHeader;
1615 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1616 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1617
1618 ConfigAltResp = NULL;
1619 ConfigResp = NULL;
1620 VarGuid = NULL;
1621 VarName = NULL;
1622 DevicePath = NULL;
1623 ConfigAltHdr = NULL;
1624 HiiHandleBuffer = NULL;
1625 Index = 0;
1626 TempDriverHandle = NULL;
1627 HiiHandle = NULL;
1628 PackageData = NULL;
1629 HiiPackageList = NULL;
1630
1631 //
1632 // Only support set default and validate setting action.
1633 //
1634 if ((ActionType != ACTION_SET_DEFAUTL_VALUE) && (ActionType != ACTION_VALIDATE_SETTING)) {
1635 return FALSE;
1636 }
1637
1638 //
1639 // Get the full requested value and deault value string.
1640 //
1641 if (Request != NULL) {
1642 Status = gHiiConfigRouting->ExtractConfig (
1643 gHiiConfigRouting,
1644 Request,
1645 &Progress,
1646 &ConfigAltResp
1647 );
1648 } else {
1649 Status = gHiiConfigRouting->ExportConfig (
1650 gHiiConfigRouting,
1651 &ConfigAltResp
1652 );
1653 }
1654
1655 if (EFI_ERROR (Status)) {
1656 return FALSE;
1657 }
1658
1659 StringPtr = ConfigAltResp;
1660
1661 while (StringPtr != L'\0') {
1662 //
1663 // 1. Find <ConfigHdr> GUID=...&NAME=...&PATH=...
1664 //
1665 StringHdr = StringPtr;
1666
1667 //
1668 // Get Guid value
1669 //
1670 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
1671 Status = EFI_INVALID_PARAMETER;
1672 goto Done;
1673 }
1674 StringPtr += StrLen (L"GUID=");
1675 Status = InternalHiiGetBufferFromString (StringPtr, GUID_CONFIG_STRING_TYPE, (UINT8 **) &VarGuid);
1676 if (EFI_ERROR (Status)) {
1677 goto Done;
1678 }
1679
1680 //
1681 // Get Name value VarName
1682 //
1683 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
1684 StringPtr++;
1685 }
1686 if (*StringPtr == L'\0') {
1687 Status = EFI_INVALID_PARAMETER;
1688 goto Done;
1689 }
1690 StringPtr += StrLen (L"&NAME=");
1691 Status = InternalHiiGetBufferFromString (StringPtr, NAME_CONFIG_STRING_TYPE, (UINT8 **) &VarName);
1692 if (EFI_ERROR (Status)) {
1693 goto Done;
1694 }
1695
1696 //
1697 // Get Path value DevicePath
1698 //
1699 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
1700 StringPtr++;
1701 }
1702 if (*StringPtr == L'\0') {
1703 Status = EFI_INVALID_PARAMETER;
1704 goto Done;
1705 }
1706 StringPtr += StrLen (L"&PATH=");
1707 Status = InternalHiiGetBufferFromString (StringPtr, PATH_CONFIG_STRING_TYPE, (UINT8 **) &DevicePath);
1708 if (EFI_ERROR (Status)) {
1709 goto Done;
1710 }
1711
1712 //
1713 // Get the Driver handle by the got device path.
1714 //
1715 TempDevicePath = DevicePath;
1716 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDevicePath, &DriverHandle);
1717 if (EFI_ERROR (Status)) {
1718 goto Done;
1719 }
1720
1721 //
1722 // Find the matched Hii Handle for the found Driver handle
1723 //
1724 HiiHandleBuffer = HiiGetHiiHandles (NULL);
1725 if (HiiHandleBuffer == NULL) {
1726 Status = EFI_NOT_FOUND;
1727 goto Done;
1728 }
1729
1730 for (Index = 0; HiiHandleBuffer[Index] != NULL; Index ++) {
1731 gHiiDatabase->GetPackageListHandle (gHiiDatabase, HiiHandleBuffer[Index], &TempDriverHandle);
1732 if (TempDriverHandle == DriverHandle) {
1733 break;
1734 }
1735 }
1736
1737 HiiHandle = HiiHandleBuffer[Index];
1738 FreePool (HiiHandleBuffer);
1739
1740 if (HiiHandle == NULL) {
1741 //
1742 // This request string has no its Hii package.
1743 // Its default value and validating can't execute by parsing IFR data.
1744 // Directly jump into the next ConfigAltResp string for another pair Guid, Name, and Path.
1745 //
1746 Status = EFI_SUCCESS;
1747 goto NextConfigAltResp;
1748 }
1749
1750 //
1751 // 2. Get DefaultName string ID by parsing the PacakgeList
1752 //
1753
1754 //
1755 // Get HiiPackage by HiiHandle
1756 //
1757 PackageListLength = 0;
1758 HiiPackageList = NULL;
1759 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &PackageListLength, HiiPackageList);
1760
1761 //
1762 // The return status should always be EFI_BUFFER_TOO_SMALL as input buffer's size is 0.
1763 //
1764 if (Status != EFI_BUFFER_TOO_SMALL) {
1765 Status = EFI_INVALID_PARAMETER;
1766 goto Done;
1767 }
1768
1769 HiiPackageList = AllocatePool (PackageListLength);
1770 if (HiiPackageList == NULL) {
1771 Status = EFI_OUT_OF_RESOURCES;
1772 goto Done;
1773 }
1774
1775 //
1776 // Get PackageList on HiiHandle
1777 //
1778 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &PackageListLength, HiiPackageList);
1779 if (EFI_ERROR (Status)) {
1780 goto Done;
1781 }
1782
1783 //
1784 // Parse the form package and get the default name string ID.
1785 //
1786 if (ActionType == ACTION_SET_DEFAUTL_VALUE) {
1787 PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
1788 Status = EFI_NOT_FOUND;
1789 while (PackageOffset < PackageListLength) {
1790 CopyMem (&PacakgeHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PacakgeHeader));
1791
1792 //
1793 // Parse IFR opcode to get default store opcode
1794 //
1795 if (PacakgeHeader.Type == EFI_HII_PACKAGE_FORMS) {
1796 IfrOffset = sizeof (PacakgeHeader);
1797 PackageData = (UINT8 *) HiiPackageList + PackageOffset;
1798 while (IfrOffset < PacakgeHeader.Length) {
1799 IfrOpHdr = (EFI_IFR_OP_HEADER *) (PackageData + IfrOffset);
1800 //
1801 // Match DefaultId to find its DefaultName
1802 //
1803 if (IfrOpHdr->OpCode == EFI_IFR_DEFAULTSTORE_OP) {
1804 if (((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultId == DefaultId) {
1805 DefaultName = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultName;
1806 Status = EFI_SUCCESS;
1807 break;
1808 }
1809 }
1810 IfrOffset += IfrOpHdr->Length;
1811 }
1812 //
1813 // Only one form is in a package list.
1814 //
1815 break;
1816 }
1817
1818 //
1819 // Go to next package.
1820 //
1821 PackageOffset += PacakgeHeader.Length;
1822 }
1823
1824 //
1825 // Not found the matched default string ID
1826 //
1827 if (EFI_ERROR (Status)) {
1828 Status = EFI_SUCCESS;
1829 goto NextConfigAltResp;
1830 }
1831 }
1832
1833 //
1834 // 3. Call ConfigRouting GetAltCfg(ConfigRoute, <ConfigResponse>, Guid, Name, DevicePath, AltCfgId, AltCfgResp)
1835 // Get the default configuration string according to the found defaultname string ID.
1836 //
1837 Status = gHiiConfigRouting->GetAltConfig (
1838 gHiiConfigRouting,
1839 ConfigAltResp,
1840 VarGuid,
1841 VarName,
1842 DevicePath,
1843 (ActionType == ACTION_SET_DEFAUTL_VALUE) ? &DefaultName:NULL, // it can be NULL to get the current setting.
1844 &ConfigResp
1845 );
1846
1847 if (EFI_ERROR (Status)) {
1848 Status = EFI_SUCCESS;
1849 goto NextConfigAltResp;
1850 }
1851
1852 //
1853 // 4. Set the default configuration information or Validate current setting by parse IFR code.
1854 // Current Setting is in ConfigResp, will be set into buffer, then check it again.
1855 //
1856 if (ActionType == ACTION_SET_DEFAUTL_VALUE) {
1857 //
1858 // Set the default configuration information.
1859 //
1860 Status = gHiiConfigRouting->RouteConfig (gHiiConfigRouting, ConfigResp, &Progress);
1861 } else {
1862 //
1863 // Current Setting is in ConfigResp, will be set into buffer, then check it again.
1864 //
1865 Status = InternalHiiValidateCurrentSetting (ConfigResp, HiiPackageList, PackageListLength, VarGuid, VarName);
1866 }
1867
1868 if (EFI_ERROR (Status)) {
1869 goto Done;
1870 }
1871
1872 NextConfigAltResp:
1873 //
1874 // Free the allocated pacakge buffer and the got ConfigResp string.
1875 //
1876 if (HiiPackageList != NULL) {
1877 FreePool (HiiPackageList);
1878 HiiPackageList = NULL;
1879 }
1880
1881 if (ConfigResp != NULL) {
1882 FreePool (ConfigResp);
1883 ConfigResp = NULL;
1884 }
1885
1886 //
1887 // Free the allocated buffer.
1888 //
1889 FreePool (VarGuid);
1890 VarGuid = NULL;
1891
1892 FreePool (VarName);
1893 VarName = NULL;
1894
1895 FreePool (DevicePath);
1896 DevicePath = NULL;
1897
1898 //
1899 // 5. Jump to next ConfigAltResp for another Guid, Name, Path.
1900 //
1901
1902 //
1903 // Get and Skip ConfigHdr
1904 //
1905 while (*StringPtr != L'\0' && *StringPtr != L'&') {
1906 StringPtr++;
1907 }
1908 if (*StringPtr == L'\0') {
1909 break;
1910 }
1911
1912 //
1913 // Construct ConfigAltHdr string "&<ConfigHdr>&ALTCFG=\0"
1914 // | 1 | StrLen (ConfigHdr) | 8 | 1 |
1915 //
1916 ConfigAltHdr = AllocateZeroPool ((1 + StringPtr - StringHdr + 8 + 1) * sizeof (CHAR16));
1917 if (ConfigAltHdr == NULL) {
1918 Status = EFI_OUT_OF_RESOURCES;
1919 goto Done;
1920 }
1921 StrCpy (ConfigAltHdr, L"&");
1922 StrnCat (ConfigAltHdr, StringHdr, StringPtr - StringHdr);
1923 StrCat (ConfigAltHdr, L"&ALTCFG=");
1924
1925 //
1926 // Skip all AltResp (AltConfigHdr ConfigBody) for the same ConfigHdr
1927 //
1928 while ((StringHdr = StrStr (StringPtr, ConfigAltHdr)) != NULL) {
1929 StringPtr = StringHdr + StrLen (ConfigAltHdr);
1930 if (*StringPtr == L'\0') {
1931 break;
1932 }
1933 }
1934
1935 //
1936 // Free the allocated ConfigAltHdr string
1937 //
1938 FreePool (ConfigAltHdr);
1939 if (*StringPtr == L'\0') {
1940 break;
1941 }
1942
1943 //
1944 // Find &GUID as the next ConfigHdr
1945 //
1946 StringPtr = StrStr (StringPtr, L"&GUID");
1947 if (StringPtr == NULL) {
1948 break;
1949 }
1950
1951 //
1952 // Skip char '&'
1953 //
1954 StringPtr ++;
1955 }
1956
1957 Done:
1958 if (VarGuid != NULL) {
1959 FreePool (VarGuid);
1960 }
1961
1962 if (VarName != NULL) {
1963 FreePool (VarName);
1964 }
1965
1966 if (DevicePath != NULL) {
1967 FreePool (DevicePath);
1968 }
1969
1970 if (ConfigResp != NULL) {
1971 FreePool (ConfigResp);
1972 }
1973
1974 if (ConfigAltResp != NULL) {
1975 FreePool (ConfigAltResp);
1976 }
1977
1978 if (HiiPackageList != NULL) {
1979 FreePool (HiiPackageList);
1980 }
1981
1982 if (EFI_ERROR (Status)) {
1983 return FALSE;
1984 }
1985
1986 return TRUE;
1987 }
1988
1989 /**
1990 Validate the current configuration by parsing HII form IFR opcode.
1991
1992 NULL request string support depends on the ExtractConfig interface of
1993 HiiConfigRouting protocol in UEFI specification.
1994
1995 @param Request A null-terminated Unicode string in
1996 <MultiConfigRequest> format. It can be NULL.
1997 If it is NULL, all current configuration for the
1998 entirety of the current HII database will be validated.
1999
2000 @retval TURE Current configuration is valid.
2001 @retval FALSE Current configuration is invalid.
2002 **/
2003 BOOLEAN
2004 EFIAPI
2005 HiiValidateSettings (
2006 IN CONST EFI_STRING Request OPTIONAL
2007 )
2008 {
2009 return InternalHiiIfrValueAction (Request, 0, ACTION_VALIDATE_SETTING);
2010 }
2011
2012 /**
2013 Reset the default value specified by DefaultId to the driver
2014 configuration got by Request string.
2015
2016 NULL request string support depends on the ExportConfig interface of
2017 HiiConfigRouting protocol in UEFI specification.
2018
2019 @param Request A null-terminated Unicode string in
2020 <MultiConfigRequest> format. It can be NULL.
2021 If it is NULL, all configuration for the
2022 entirety of the current HII database will be reset.
2023 @param DefaultId Specifies the type of defaults to retrieve.
2024
2025 @retval TURE The default value is set successfully.
2026 @retval FALSE The default value can't be found and set.
2027 **/
2028 BOOLEAN
2029 EFIAPI
2030 HiiSetToDefaults (
2031 IN CONST EFI_STRING Request, OPTIONAL
2032 IN UINT16 DefaultId
2033 )
2034 {
2035 return InternalHiiIfrValueAction (Request, DefaultId, ACTION_SET_DEFAUTL_VALUE);
2036 }
2037
2038 /**
2039 Allocates and returns a Null-terminated Unicode <ConfigAltResp> string.
2040
2041 If Guid is NULL, then ASSERT().
2042 If Name is NULL, then ASSERT().
2043 If BlockNameArray is NULL, then ASSERT().
2044
2045 @param[in] Guid GUID of the buffer storage.
2046 @param[in] Name Name of the buffer storage.
2047 @param[in] DriverHandle The DriverHandle that support a Device Path
2048 Protocol.
2049 @param[in] BufferStorage Content of the buffer storage.
2050 @param[in] BufferStorageSize Length in bytes of the buffer storage.
2051 @param[in] BlockNameArray Array generated by VFR compiler. This array
2052 contains a UINT32 value that is the length
2053 of BlockNameArray in bytes, followed by pairs
2054 of 16-bit values that are the offset and length
2055 values used to contruct a <ConfigRequest> string.
2056 @param[in] ... A variable argument list that contains pairs of 16-bit
2057 ALTCFG identifiers and pointers to DefaultValueArrays.
2058 The variable argument list is terminated by a NULL
2059 DefaultValueArray pointer. A DefaultValueArray
2060 contains a UINT32 value that is the length, in bytes,
2061 of the DefaultValueArray. The UINT32 length value
2062 is followed by a series of records that contain
2063 a 16-bit WIDTH value followed by a byte array with
2064 WIDTH entries. The records must be parsed from
2065 beginning to end until the UINT32 length limit
2066 is reached.
2067
2068 @retval NULL There are not enough resources to process the request.
2069 @retval NULL A <ConfigResp> could not be retrieved from the Config
2070 Routing Protocol.
2071 @retval Other A pointer to the Null-terminate Unicode <ConfigAltResp>
2072 string.
2073
2074 **/
2075 EFI_STRING
2076 EFIAPI
2077 HiiConstructConfigAltResp (
2078 IN CONST EFI_GUID *Guid,
2079 IN CONST CHAR16 *Name,
2080 IN EFI_HANDLE DriverHandle,
2081 IN CONST VOID *BufferStorage,
2082 IN UINTN BufferStorageSize,
2083 IN CONST VOID *BlockNameArray,
2084 ...
2085 )
2086 {
2087 UINTN Length;
2088 CHAR16 *String;
2089 CHAR16 *ConfigHdr;
2090 UINT8 *Buffer;
2091 UINT8 *BufferEnd;
2092 CHAR16 *ConfigRequest;
2093 EFI_STRING ConfigResp;
2094 EFI_STRING ConfigAltResp;
2095 VA_LIST Args;
2096 UINTN AltCfgId;
2097 UINT16 Width;
2098 UINT16 OffsetValue;
2099 UINT16 WidthValue;
2100
2101 ASSERT (Guid != NULL);
2102 ASSERT (Name != NULL);
2103 ASSERT (BlockNameArray != NULL);
2104
2105 //
2106 // Initialize local variables
2107 //
2108 ConfigHdr = NULL;
2109 ConfigRequest = NULL;
2110 ConfigResp = NULL;
2111
2112 //
2113 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..."
2114 //
2115 ConfigHdr = HiiConstructConfigHdr (Guid, Name, DriverHandle);
2116 if (ConfigHdr == NULL) {
2117 goto Exit;
2118 }
2119
2120 //
2121 // Compute the length of the entire request starting with <ConfigHdr> and a
2122 // Null-terminator
2123 //
2124 Length = StrLen (ConfigHdr) + 1;
2125
2126 //
2127 // Determine the size <BlockName> Offset/Width pairs
2128 //
2129 Buffer = (UINT8 *)BlockNameArray;
2130 BufferEnd = Buffer + ReadUnaligned32 ((UINT32 *)Buffer);
2131 Buffer += sizeof (UINT32);
2132
2133 //
2134 // Add <BlockName> length that is composed of one or more Offset/Width pairs
2135 //
2136 // <BlockName> ::= &OFFSET=1234&WIDTH=1234
2137 // | 8 | 4 | 7 | 4 |
2138 //
2139 Length += (((BufferEnd - Buffer) / (sizeof (UINT16) + sizeof (UINT16))) * (8 + 4 + 7 + 4));
2140
2141 //
2142 // Allocate buffer for the entire <ConfigRequest>
2143 //
2144 ConfigRequest = AllocateZeroPool (Length * sizeof (CHAR16));
2145 if (ConfigRequest == NULL) {
2146 goto Exit;
2147 }
2148 String = ConfigRequest;
2149
2150 //
2151 // Start with <ConfigHdr>
2152 //
2153 StrCpy (String, ConfigHdr);
2154 String += StrLen (String);
2155
2156 //
2157 // Loop through all the Offset/Width pairs and append them to ConfigRequest
2158 //
2159 while (Buffer < BufferEnd) {
2160 //
2161 // Append &OFFSET=XXXX&WIDTH=YYYY
2162 //
2163 OffsetValue = ReadUnaligned16 ((UINT16 *)Buffer);
2164 WidthValue = ReadUnaligned16 ((UINT16 *)(Buffer + sizeof (UINT16)));
2165 UnicodeSPrint (
2166 String,
2167 (8 + 4 + 7 + 4) * sizeof (CHAR16),
2168 L"&OFFSET=%04X&WIDTH=%04X",
2169 OffsetValue,
2170 WidthValue
2171 );
2172
2173 String += StrLen (String);
2174 Buffer += (sizeof (UINT16) + sizeof (UINT16));
2175 }
2176
2177 //
2178 // Get the <ConfigResp>
2179 //
2180 ConfigResp = InternalHiiBlockToConfig (ConfigRequest, BufferStorage, BufferStorageSize);
2181 if (ConfigResp == NULL) {
2182 goto Exit;
2183 }
2184
2185 //
2186 // Compute the length of the entire response starting with <ConfigResp> and a
2187 // Null-terminator
2188 //
2189 Length = StrLen (ConfigResp) + 1;
2190
2191 //
2192 // Add the length associated with each pair of variable argument parameters
2193 //
2194 VA_START (Args, BlockNameArray);
2195 while (TRUE) {
2196 AltCfgId = VA_ARG (Args, UINTN);
2197 Buffer = VA_ARG (Args, UINT8 *);
2198 if (Buffer == NULL) {
2199 break;
2200 }
2201
2202 //
2203 // Add length for "&<ConfigHdr>&ALTCFG=XXXX"
2204 // |1| StrLen (ConfigHdr) | 8 | 4 |
2205 //
2206 Length += (1 + StrLen (ConfigHdr) + 8 + 4);
2207
2208 BufferEnd = Buffer + ReadUnaligned32 ((UINT32 *)Buffer);
2209 Buffer += sizeof (UINT32);
2210 while (Buffer < BufferEnd) {
2211 //
2212 // Extract Width field
2213 //
2214 Width = ReadUnaligned16 ((UINT16 *)(Buffer + sizeof (UINT16)));
2215
2216 //
2217 // Add length for "&OFFSET=XXXX&WIDTH=YYYY&VALUE=zzzzzzzzzzzz"
2218 // | 8 | 4 | 7 | 4 | 7 | Width * 2 |
2219 //
2220 Length += (8 + 4 + 7 + 4 + 7 + Width * 2);
2221
2222 //
2223 // Update Buffer to the next record
2224 //
2225 Buffer += (sizeof (UINT16) + sizeof (UINT16) + Width);
2226 }
2227 }
2228 VA_END (Args);
2229
2230 //
2231 // Allocate a buffer for the entire response
2232 //
2233 ConfigAltResp = AllocateZeroPool (Length * sizeof (CHAR16));
2234 if (ConfigAltResp == NULL) {
2235 goto Exit;
2236 }
2237 String = ConfigAltResp;
2238
2239 //
2240 // Add <ConfigResp>
2241 //
2242 StrCpy (String, ConfigResp);
2243 String += StrLen (String);
2244
2245 //
2246 // Add <AltResp> for each pair of variable argument parameters
2247 //
2248 VA_START (Args, BlockNameArray);
2249 while (TRUE) {
2250 AltCfgId = VA_ARG (Args, UINTN);
2251 Buffer = VA_ARG (Args, UINT8 *);
2252 if (Buffer == NULL) {
2253 break;
2254 }
2255
2256 //
2257 // Add <AltConfigHdr> of the form "&<ConfigHdr>&ALTCFG=XXXX"
2258 // |1| StrLen (ConfigHdr) | 8 | 4 |
2259 //
2260 UnicodeSPrint (
2261 String,
2262 (1 + StrLen (ConfigHdr) + 8 + 4) * sizeof (CHAR16),
2263 L"&%s&ALTCFG=%04X",
2264 ConfigHdr,
2265 AltCfgId
2266 );
2267 String += StrLen (String);
2268
2269 //
2270 // Add <ConfigBody> ::= <ConfigElement>*
2271 //
2272 BufferEnd = Buffer + ReadUnaligned32 ((UINT32 *)Buffer);
2273 Buffer += sizeof (UINT32);
2274 while (Buffer < BufferEnd) {
2275 //
2276 // Extract Width field
2277 //
2278 Width = ReadUnaligned16 ((UINT16 *)(Buffer + sizeof (UINT16)));
2279
2280 //
2281 // Add <BlockConfig>
2282 //
2283 UnicodeSPrint (
2284 String,
2285 (8 + 4 + 7 + 4 + 7 + Width * 2) * sizeof (CHAR16),
2286 L"&OFFSET=%04X&WIDTH=%04X&VALUE=",
2287 ReadUnaligned16 ((UINT16 *)Buffer),
2288 Width
2289 );
2290 String += StrLen (String);
2291
2292 //
2293 // Update Buffer to point to the value in the current record
2294 //
2295 Buffer += (sizeof (UINT16) + sizeof (UINT16));
2296
2297 //
2298 // Convert Value to a hex string in "%x" format
2299 // NOTE: This is in the opposite byte that GUID and PATH use
2300 //
2301 for (; Width > 0; Width--) {
2302 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, Buffer[Width - 1], 2);
2303 }
2304 //
2305 // Update Buffer to the next record
2306 //
2307 Buffer += Width;
2308 }
2309 }
2310 VA_END (Args);
2311
2312 //
2313 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
2314 //
2315 return InternalHiiLowerConfigString (ConfigAltResp);
2316
2317 Exit:
2318 if (ConfigHdr != NULL) {
2319 FreePool (ConfigHdr);
2320 }
2321 if (ConfigRequest != NULL) {
2322 FreePool (ConfigRequest);
2323 }
2324 if (ConfigResp != NULL) {
2325 FreePool (ConfigResp);
2326 }
2327
2328 return NULL;
2329 }
2330
2331 /**
2332 Determines if two values in config strings match.
2333
2334 Compares the substring between StartSearchString and StopSearchString in
2335 FirstString to the substring between StartSearchString and StopSearchString
2336 in SecondString. If the two substrings match, then TRUE is returned. If the
2337 two substrings do not match, then FALSE is returned.
2338
2339 If FirstString is NULL, then ASSERT().
2340 If SecondString is NULL, then ASSERT().
2341 If StartSearchString is NULL, then ASSERT().
2342 If StopSearchString is NULL, then ASSERT().
2343
2344 @param FirstString Pointer to the first Null-terminated Unicode string.
2345 @param SecondString Pointer to the second Null-terminated Unicode string.
2346 @param StartSearchString Pointer to the Null-terminated Unicode string that
2347 marks the start of the value string to compare.
2348 @param StopSearchString Pointer to the Null-terminated Unicode string that
2349 marks the end of the vakue string to compare.
2350
2351 @retval FALSE StartSearchString is not present in FirstString.
2352 @retval FALSE StartSearchString is not present in SecondString.
2353 @retval FALSE StopSearchString is not present in FirstString.
2354 @retval FALSE StopSearchString is not present in SecondString.
2355 @retval FALSE The length of the substring in FirstString is not the
2356 same length as the substring in SecondString.
2357 @retval FALSE The value string in FirstString does not matche the
2358 value string in SecondString.
2359 @retval TRUE The value string in FirstString matches the value
2360 string in SecondString.
2361
2362 **/
2363 BOOLEAN
2364 EFIAPI
2365 InternalHiiCompareSubString (
2366 IN CHAR16 *FirstString,
2367 IN CHAR16 *SecondString,
2368 IN CHAR16 *StartSearchString,
2369 IN CHAR16 *StopSearchString
2370 )
2371 {
2372 CHAR16 *EndFirstString;
2373 CHAR16 *EndSecondString;
2374
2375 ASSERT (FirstString != NULL);
2376 ASSERT (SecondString != NULL);
2377 ASSERT (StartSearchString != NULL);
2378 ASSERT (StopSearchString != NULL);
2379
2380 FirstString = StrStr (FirstString, StartSearchString);
2381 if (FirstString == NULL) {
2382 return FALSE;
2383 }
2384
2385 SecondString = StrStr (SecondString, StartSearchString);
2386 if (SecondString == NULL) {
2387 return FALSE;
2388 }
2389
2390 EndFirstString = StrStr (FirstString, StopSearchString);
2391 if (EndFirstString == NULL) {
2392 return FALSE;
2393 }
2394
2395 EndSecondString = StrStr (SecondString, StopSearchString);
2396 if (EndSecondString == NULL) {
2397 return FALSE;
2398 }
2399
2400 if ((EndFirstString - FirstString) != (EndSecondString - SecondString)) {
2401 return FALSE;
2402 }
2403
2404 return (BOOLEAN)(StrnCmp (FirstString, SecondString, EndFirstString - FirstString) == 0);
2405 }
2406
2407 /**
2408 Determines if the routing data specified by GUID and NAME match a <ConfigHdr>.
2409
2410 If ConfigHdr is NULL, then ASSERT().
2411
2412 @param[in] ConfigHdr Either <ConfigRequest> or <ConfigResp>.
2413 @param[in] Guid GUID of the storage.
2414 @param[in] Name NAME of the storage.
2415
2416 @retval TRUE Routing information matches <ConfigHdr>.
2417 @retval FALSE Routing information does not match <ConfigHdr>.
2418
2419 **/
2420 BOOLEAN
2421 EFIAPI
2422 HiiIsConfigHdrMatch (
2423 IN CONST EFI_STRING ConfigHdr,
2424 IN CONST EFI_GUID *Guid, OPTIONAL
2425 IN CONST CHAR16 *Name OPTIONAL
2426 )
2427 {
2428 EFI_STRING CompareConfigHdr;
2429 BOOLEAN Result;
2430
2431 ASSERT (ConfigHdr != NULL);
2432
2433 //
2434 // Use Guid and Name to generate a <ConfigHdr> string
2435 //
2436 CompareConfigHdr = HiiConstructConfigHdr (Guid, Name, NULL);
2437 if (CompareConfigHdr == NULL) {
2438 return FALSE;
2439 }
2440
2441 Result = TRUE;
2442 if (Guid != NULL) {
2443 //
2444 // Compare GUID value strings
2445 //
2446 Result = InternalHiiCompareSubString (ConfigHdr, CompareConfigHdr, L"GUID=", L"&NAME=");
2447 }
2448
2449 if (Result && Name != NULL) {
2450 //
2451 // Compare NAME value strings
2452 //
2453 Result = InternalHiiCompareSubString (ConfigHdr, CompareConfigHdr, L"&NAME=", L"&PATH=");
2454 }
2455
2456 //
2457 // Free the <ConfigHdr> string
2458 //
2459 FreePool (CompareConfigHdr);
2460
2461 return Result;
2462 }
2463
2464 /**
2465 Retrieves uncommited data from the Form Browser and converts it to a binary
2466 buffer.
2467
2468 @param[in] VariableName Pointer to a Null-terminated Unicode string. This
2469 is an optional parameter that may be NULL.
2470 @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional
2471 parameter that may be NULL.
2472 @param[in] BufferSize Length in bytes of buffer to hold retrived data.
2473 @param[out] Block Buffer of data to be updated.
2474
2475 @retval FALSE The uncommitted data could not be retrieved.
2476 @retval TRUE The uncommitted data was retrieved.
2477
2478 **/
2479 BOOLEAN
2480 EFIAPI
2481 HiiGetBrowserData (
2482 IN CONST EFI_GUID *VariableGuid, OPTIONAL
2483 IN CONST CHAR16 *VariableName, OPTIONAL
2484 IN UINTN BlockSize,
2485 OUT UINT8 *Block
2486 )
2487 {
2488 EFI_STRING ResultsData;
2489 UINTN Size;
2490 EFI_STRING ConfigResp;
2491 EFI_STATUS Status;
2492 CHAR16 *Progress;
2493
2494 //
2495 // Retrieve the results data from the Browser Callback
2496 //
2497 ResultsData = InternalHiiBrowserCallback (VariableGuid, VariableName, NULL);
2498 if (ResultsData == NULL) {
2499 return FALSE;
2500 }
2501
2502 //
2503 // Construct <ConfigResp> mConfigHdrTemplate L'&' ResultsData L'\0'
2504 //
2505 Size = (StrLen (mConfigHdrTemplate) + 1) * sizeof (CHAR16);
2506 Size = Size + (StrLen (ResultsData) + 1) * sizeof (CHAR16);
2507 ConfigResp = AllocateZeroPool (Size);
2508 UnicodeSPrint (ConfigResp, Size, L"%s&%s", mConfigHdrTemplate, ResultsData);
2509
2510 //
2511 // Free the allocated buffer
2512 //
2513 FreePool (ResultsData);
2514 if (ConfigResp == NULL) {
2515 return FALSE;
2516 }
2517
2518 //
2519 // Convert <ConfigResp> to a buffer
2520 //
2521 Status = gHiiConfigRouting->ConfigToBlock (
2522 gHiiConfigRouting,
2523 ConfigResp,
2524 Block,
2525 &BlockSize,
2526 &Progress
2527 );
2528 //
2529 // Free the allocated buffer
2530 //
2531 FreePool (ConfigResp);
2532
2533 if (EFI_ERROR (Status)) {
2534 return FALSE;
2535 }
2536
2537 return TRUE;
2538 }
2539
2540 /**
2541 Updates uncommitted data in the Form Browser.
2542
2543 If Buffer is NULL, then ASSERT().
2544
2545 @param[in] VariableName Pointer to a Null-terminated Unicode string. This
2546 is an optional parameter that may be NULL.
2547 @param[in] VariableGuid Pointer to an EFI_GUID structure. This is an optional
2548 parameter that may be NULL.
2549 @param[in] BufferSize Length, in bytes, of Buffer.
2550 @param[in] Buffer Buffer of data to commit.
2551 @param[in] RequestElement An optional field to specify which part of the
2552 buffer data will be send back to Browser. If NULL,
2553 the whole buffer of data will be committed to
2554 Browser.
2555 <RequestElement> ::= &OFFSET=<Number>&WIDTH=<Number>*
2556
2557 @retval FALSE The uncommitted data could not be updated.
2558 @retval TRUE The uncommitted data was updated.
2559
2560 **/
2561 BOOLEAN
2562 EFIAPI
2563 HiiSetBrowserData (
2564 IN CONST EFI_GUID *VariableGuid, OPTIONAL
2565 IN CONST CHAR16 *VariableName, OPTIONAL
2566 IN UINTN BufferSize,
2567 IN CONST UINT8 *Buffer,
2568 IN CONST CHAR16 *RequestElement OPTIONAL
2569 )
2570 {
2571 UINTN Size;
2572 EFI_STRING ConfigRequest;
2573 EFI_STRING ConfigResp;
2574 EFI_STRING ResultsData;
2575
2576 ASSERT (Buffer != NULL);
2577
2578 //
2579 // Construct <ConfigRequest>
2580 //
2581 if (RequestElement == NULL) {
2582 //
2583 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2584 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
2585 //
2586 Size = (StrLen (mConfigHdrTemplate) + 32 + 1) * sizeof (CHAR16);
2587 ConfigRequest = AllocateZeroPool (Size);
2588 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", mConfigHdrTemplate, (UINT64)BufferSize);
2589 } else {
2590 //
2591 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
2592 // followed by <RequestElement> followed by a Null-terminator
2593 //
2594 Size = StrLen (mConfigHdrTemplate) * sizeof (CHAR16);
2595 Size = Size + (StrLen (RequestElement) + 1) * sizeof (CHAR16);
2596 ConfigRequest = AllocateZeroPool (Size);
2597 UnicodeSPrint (ConfigRequest, Size, L"%s%s", mConfigHdrTemplate, RequestElement);
2598 }
2599 if (ConfigRequest == NULL) {
2600 return FALSE;
2601 }
2602
2603 //
2604 // Convert <ConfigRequest> to <ConfigResp>
2605 //
2606 ConfigResp = InternalHiiBlockToConfig (ConfigRequest, Buffer, BufferSize);
2607 FreePool (ConfigRequest);
2608 if (ConfigResp == NULL) {
2609 return FALSE;
2610 }
2611
2612 //
2613 // Set data in the uncommitted browser state information
2614 //
2615 ResultsData = InternalHiiBrowserCallback (VariableGuid, VariableName, ConfigResp + StrLen(mConfigHdrTemplate) + 1);
2616 FreePool (ConfigResp);
2617
2618 return (BOOLEAN)(ResultsData != NULL);
2619 }
2620
2621 /////////////////////////////////////////
2622 /////////////////////////////////////////
2623 /// IFR Functions
2624 /////////////////////////////////////////
2625 /////////////////////////////////////////
2626
2627 #define HII_LIB_OPCODE_ALLOCATION_SIZE 0x200
2628
2629 typedef struct {
2630 UINT8 *Buffer;
2631 UINTN BufferSize;
2632 UINTN Position;
2633 } HII_LIB_OPCODE_BUFFER;
2634
2635 ///
2636 /// Lookup table that converts EFI_IFR_TYPE_X enum values to a width in bytes
2637 ///
2638 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 mHiiDefaultTypeToWidth[] = {
2639 1, // EFI_IFR_TYPE_NUM_SIZE_8
2640 2, // EFI_IFR_TYPE_NUM_SIZE_16
2641 4, // EFI_IFR_TYPE_NUM_SIZE_32
2642 8, // EFI_IFR_TYPE_NUM_SIZE_64
2643 1, // EFI_IFR_TYPE_BOOLEAN
2644 3, // EFI_IFR_TYPE_TIME
2645 4, // EFI_IFR_TYPE_DATE
2646 2 // EFI_IFR_TYPE_STRING
2647 };
2648
2649 /**
2650 Allocates and returns a new OpCode Handle. OpCode Handles must be freed with
2651 HiiFreeOpCodeHandle().
2652
2653 @retval NULL There are not enough resources to allocate a new OpCode Handle.
2654 @retval Other A new OpCode handle.
2655
2656 **/
2657 VOID *
2658 EFIAPI
2659 HiiAllocateOpCodeHandle (
2660 VOID
2661 )
2662 {
2663 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2664
2665 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)AllocatePool (sizeof (HII_LIB_OPCODE_BUFFER));
2666 if (OpCodeBuffer == NULL) {
2667 return NULL;
2668 }
2669 OpCodeBuffer->Buffer = (UINT8 *)AllocatePool (HII_LIB_OPCODE_ALLOCATION_SIZE);
2670 if (OpCodeBuffer->Buffer == NULL) {
2671 FreePool (OpCodeBuffer);
2672 return NULL;
2673 }
2674 OpCodeBuffer->BufferSize = HII_LIB_OPCODE_ALLOCATION_SIZE;
2675 OpCodeBuffer->Position = 0;
2676 return (VOID *)OpCodeBuffer;
2677 }
2678
2679 /**
2680 Frees an OpCode Handle that was peviously allocated with HiiAllocateOpCodeHandle().
2681 When an OpCode Handle is freed, all of the opcodes associated with the OpCode
2682 Handle are also freed.
2683
2684 If OpCodeHandle is NULL, then ASSERT().
2685
2686 **/
2687 VOID
2688 EFIAPI
2689 HiiFreeOpCodeHandle (
2690 VOID *OpCodeHandle
2691 )
2692 {
2693 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2694
2695 ASSERT (OpCodeHandle != NULL);
2696
2697 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)OpCodeHandle;
2698 if (OpCodeBuffer->Buffer != NULL) {
2699 FreePool (OpCodeBuffer->Buffer);
2700 }
2701 FreePool (OpCodeBuffer);
2702 }
2703
2704 UINTN
2705 EFIAPI
2706 InternalHiiOpCodeHandlePosition (
2707 IN VOID *OpCodeHandle
2708 )
2709 {
2710 return ((HII_LIB_OPCODE_BUFFER *)OpCodeHandle)->Position;
2711 }
2712
2713 UINT8 *
2714 EFIAPI
2715 InternalHiiOpCodeHandleBuffer (
2716 IN VOID *OpCodeHandle
2717 )
2718 {
2719 return ((HII_LIB_OPCODE_BUFFER *)OpCodeHandle)->Buffer;
2720 }
2721
2722 UINT8 *
2723 EFIAPI
2724 InternalHiiGrowOpCodeHandle (
2725 VOID *OpCodeHandle,
2726 UINTN Size
2727 )
2728 {
2729 HII_LIB_OPCODE_BUFFER *OpCodeBuffer;
2730 UINT8 *Buffer;
2731
2732 ASSERT (OpCodeHandle != NULL);
2733
2734 OpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)OpCodeHandle;
2735 if (OpCodeBuffer->Position + Size > OpCodeBuffer->BufferSize) {
2736 Buffer = ReallocatePool (
2737 OpCodeBuffer->BufferSize,
2738 OpCodeBuffer->BufferSize + (Size + HII_LIB_OPCODE_ALLOCATION_SIZE),
2739 OpCodeBuffer->Buffer
2740 );
2741 if (Buffer == NULL) {
2742 return NULL;
2743 }
2744 OpCodeBuffer->Buffer = Buffer;
2745 OpCodeBuffer->BufferSize += (Size + HII_LIB_OPCODE_ALLOCATION_SIZE);
2746 }
2747 Buffer = OpCodeBuffer->Buffer + OpCodeBuffer->Position;
2748 OpCodeBuffer->Position += Size;
2749 return Buffer;
2750 }
2751
2752 UINT8 *
2753 EFIAPI
2754 InternalHiiCreateOpCodeExtended (
2755 IN VOID *OpCodeHandle,
2756 IN VOID *OpCodeTemplate,
2757 IN UINT8 OpCode,
2758 IN UINTN OpCodeSize,
2759 IN UINTN ExtensionSize,
2760 IN UINT8 Scope
2761 )
2762 {
2763 EFI_IFR_OP_HEADER *Header;
2764 UINT8 *Buffer;
2765
2766 ASSERT (OpCodeTemplate != NULL);
2767 ASSERT ((OpCodeSize + ExtensionSize) <= 0x7F);
2768
2769 Header = (EFI_IFR_OP_HEADER *)OpCodeTemplate;
2770 Header->OpCode = OpCode;
2771 Header->Scope = Scope;
2772 Header->Length = (UINT8)(OpCodeSize + ExtensionSize);
2773 Buffer = InternalHiiGrowOpCodeHandle (OpCodeHandle, Header->Length);
2774 return (UINT8 *)CopyMem (Buffer, Header, OpCodeSize);
2775 }
2776
2777 UINT8 *
2778 EFIAPI
2779 InternalHiiCreateOpCode (
2780 IN VOID *OpCodeHandle,
2781 IN VOID *OpCodeTemplate,
2782 IN UINT8 OpCode,
2783 IN UINTN OpCodeSize
2784 )
2785 {
2786 return InternalHiiCreateOpCodeExtended (OpCodeHandle, OpCodeTemplate, OpCode, OpCodeSize, 0, 0);
2787 }
2788
2789 /**
2790 Append raw opcodes to an OpCodeHandle.
2791
2792 If OpCodeHandle is NULL, then ASSERT().
2793 If RawBuffer is NULL, then ASSERT();
2794
2795 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2796 @param[in] RawBuffer Buffer of opcodes to append.
2797 @param[in] RawBufferSize The size, in bytes, of Buffer.
2798
2799 @retval NULL There is not enough space left in Buffer to add the opcode.
2800 @retval Other A pointer to the appended opcodes.
2801
2802 **/
2803 UINT8 *
2804 EFIAPI
2805 HiiCreateRawOpCodes (
2806 IN VOID *OpCodeHandle,
2807 IN UINT8 *RawBuffer,
2808 IN UINTN RawBufferSize
2809 )
2810 {
2811 UINT8 *Buffer;
2812
2813 ASSERT (RawBuffer != NULL);
2814
2815 Buffer = InternalHiiGrowOpCodeHandle (OpCodeHandle, RawBufferSize);
2816 return (UINT8 *)CopyMem (Buffer, RawBuffer, RawBufferSize);
2817 }
2818
2819 /**
2820 Append opcodes from one OpCode Handle to another OpCode handle.
2821
2822 If OpCodeHandle is NULL, then ASSERT().
2823 If RawOpCodeHandle is NULL, then ASSERT();
2824
2825 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2826 @param[in] RawOpCodeHandle Handle to the buffer of opcodes.
2827
2828 @retval NULL There is not enough space left in Buffer to add the opcode.
2829 @retval Other A pointer to the appended opcodes.
2830
2831 **/
2832 UINT8 *
2833 EFIAPI
2834 InternalHiiAppendOpCodes (
2835 IN VOID *OpCodeHandle,
2836 IN VOID *RawOpCodeHandle
2837 )
2838 {
2839 HII_LIB_OPCODE_BUFFER *RawOpCodeBuffer;
2840
2841 ASSERT (RawOpCodeHandle != NULL);
2842
2843 RawOpCodeBuffer = (HII_LIB_OPCODE_BUFFER *)RawOpCodeHandle;
2844 return HiiCreateRawOpCodes (OpCodeHandle, RawOpCodeBuffer->Buffer, RawOpCodeBuffer->Position);
2845 }
2846
2847 /**
2848 Create EFI_IFR_END_OP opcode.
2849
2850 If OpCodeHandle is NULL, then ASSERT().
2851
2852 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2853
2854 @retval NULL There is not enough space left in Buffer to add the opcode.
2855 @retval Other A pointer to the created opcode.
2856
2857 **/
2858 UINT8 *
2859 EFIAPI
2860 HiiCreateEndOpCode (
2861 IN VOID *OpCodeHandle
2862 )
2863 {
2864 EFI_IFR_END OpCode;
2865
2866 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_END_OP, sizeof (OpCode));
2867 }
2868
2869 /**
2870 Create EFI_IFR_ONE_OF_OPTION_OP opcode.
2871
2872 If OpCodeHandle is NULL, then ASSERT().
2873 If Type is invalid, then ASSERT().
2874 If Flags is invalid, then ASSERT().
2875
2876 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2877 @param[in] StringId StringId for the option
2878 @param[in] Flags Flags for the option
2879 @param[in] Type Type for the option
2880 @param[in] Value Value for the option
2881
2882 @retval NULL There is not enough space left in Buffer to add the opcode.
2883 @retval Other A pointer to the created opcode.
2884
2885 **/
2886 UINT8 *
2887 EFIAPI
2888 HiiCreateOneOfOptionOpCode (
2889 IN VOID *OpCodeHandle,
2890 IN UINT16 StringId,
2891 IN UINT8 Flags,
2892 IN UINT8 Type,
2893 IN UINT64 Value
2894 )
2895 {
2896 EFI_IFR_ONE_OF_OPTION OpCode;
2897
2898 ASSERT (Type < EFI_IFR_TYPE_OTHER);
2899
2900 ZeroMem (&OpCode, sizeof (OpCode));
2901 OpCode.Option = StringId;
2902 OpCode.Flags = (UINT8) (Flags & (EFI_IFR_OPTION_DEFAULT | EFI_IFR_OPTION_DEFAULT_MFG));
2903 OpCode.Type = Type;
2904 CopyMem (&OpCode.Value, &Value, mHiiDefaultTypeToWidth[Type]);
2905
2906 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_ONE_OF_OPTION_OP, sizeof (OpCode));
2907 }
2908
2909 /**
2910 Create EFI_IFR_DEFAULT_OP opcode.
2911
2912 If OpCodeHandle is NULL, then ASSERT().
2913 If Type is invalid, then ASSERT().
2914
2915 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2916 @param[in] DefaultId DefaultId for the default
2917 @param[in] Type Type for the default
2918 @param[in] Value Value for the default
2919
2920 @retval NULL There is not enough space left in Buffer to add the opcode.
2921 @retval Other A pointer to the created opcode.
2922
2923 **/
2924 UINT8 *
2925 EFIAPI
2926 HiiCreateDefaultOpCode (
2927 IN VOID *OpCodeHandle,
2928 IN UINT16 DefaultId,
2929 IN UINT8 Type,
2930 IN UINT64 Value
2931 )
2932 {
2933 EFI_IFR_DEFAULT OpCode;
2934
2935 ASSERT (Type < EFI_IFR_TYPE_OTHER);
2936
2937 ZeroMem (&OpCode, sizeof (OpCode));
2938 OpCode.Type = Type;
2939 OpCode.DefaultId = DefaultId;
2940 CopyMem (&OpCode.Value, &Value, mHiiDefaultTypeToWidth[Type]);
2941
2942 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_DEFAULT_OP, sizeof (OpCode));
2943 }
2944
2945 /**
2946 Create EFI_IFR_GUID opcode.
2947
2948 If OpCodeHandle is NULL, then ASSERT().
2949 If Guid is NULL, then ASSERT().
2950 If OpCodeSize < sizeof (EFI_IFR_GUID), then ASSERT().
2951
2952 @param[in] OpCodeHandle Handle to the buffer of opcodes.
2953 @param[in] Guid Pointer to EFI_GUID of this guided opcode.
2954 @param[in] GuidOpCode Pointer to an EFI_IFR_GUID opcode. This is an
2955 optional parameter that may be NULL. If this
2956 parameter is NULL, then the GUID extension
2957 region of the created opcode is filled with zeros.
2958 If this parameter is not NULL, then the GUID
2959 extension region of GuidData will be copied to
2960 the GUID extension region of the created opcode.
2961 @param[in] OpCodeSize The size, in bytes, of created opcode. This value
2962 must be >= sizeof(EFI_IFR_GUID).
2963
2964 @retval NULL There is not enough space left in Buffer to add the opcode.
2965 @retval Other A pointer to the created opcode.
2966
2967 **/
2968 UINT8 *
2969 EFIAPI
2970 HiiCreateGuidOpCode (
2971 IN VOID *OpCodeHandle,
2972 IN CONST EFI_GUID *Guid,
2973 IN CONST VOID *GuidOpCode, OPTIONAL
2974 IN UINTN OpCodeSize
2975 )
2976 {
2977 EFI_IFR_GUID OpCode;
2978 EFI_IFR_GUID *OpCodePointer;
2979
2980 ASSERT (Guid != NULL);
2981 ASSERT (OpCodeSize >= sizeof (OpCode));
2982
2983 ZeroMem (&OpCode, sizeof (OpCode));
2984 CopyGuid ((EFI_GUID *)(VOID *)&OpCode.Guid, Guid);
2985
2986 OpCodePointer = (EFI_IFR_GUID *)InternalHiiCreateOpCodeExtended (
2987 OpCodeHandle,
2988 &OpCode,
2989 EFI_IFR_GUID_OP,
2990 sizeof (OpCode),
2991 OpCodeSize - sizeof (OpCode),
2992 0
2993 );
2994 if (OpCodePointer != NULL && GuidOpCode != NULL) {
2995 CopyMem (OpCodePointer + 1, (EFI_IFR_GUID *)GuidOpCode + 1, OpCodeSize - sizeof (OpCode));
2996 }
2997 return (UINT8 *)OpCodePointer;
2998 }
2999
3000 /**
3001 Create EFI_IFR_ACTION_OP opcode.
3002
3003 If OpCodeHandle is NULL, then ASSERT().
3004 If any reserved bits are set in QuestionFlags, then ASSERT().
3005
3006 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3007 @param[in] QuestionId Question ID
3008 @param[in] Prompt String ID for Prompt
3009 @param[in] Help String ID for Help
3010 @param[in] QuestionFlags Flags in Question Header
3011 @param[in] QuestionConfig String ID for configuration
3012
3013 @retval NULL There is not enough space left in Buffer to add the opcode.
3014 @retval Other A pointer to the created opcode.
3015
3016 **/
3017 UINT8 *
3018 EFIAPI
3019 HiiCreateActionOpCode (
3020 IN VOID *OpCodeHandle,
3021 IN EFI_QUESTION_ID QuestionId,
3022 IN EFI_STRING_ID Prompt,
3023 IN EFI_STRING_ID Help,
3024 IN UINT8 QuestionFlags,
3025 IN EFI_STRING_ID QuestionConfig
3026 )
3027 {
3028 EFI_IFR_ACTION OpCode;
3029
3030 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3031
3032 ZeroMem (&OpCode, sizeof (OpCode));
3033 OpCode.Question.QuestionId = QuestionId;
3034 OpCode.Question.Header.Prompt = Prompt;
3035 OpCode.Question.Header.Help = Help;
3036 OpCode.Question.Flags = QuestionFlags;
3037 OpCode.QuestionConfig = QuestionConfig;
3038
3039 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_ACTION_OP, sizeof (OpCode));
3040 }
3041
3042 /**
3043 Create EFI_IFR_SUBTITLE_OP opcode.
3044
3045 If OpCodeHandle is NULL, then ASSERT().
3046 If any reserved bits are set in Flags, then ASSERT().
3047 If Scope > 1, then ASSERT().
3048
3049 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3050 @param[in] Prompt String ID for Prompt
3051 @param[in] Help String ID for Help
3052 @param[in] Flags Subtitle opcode flags
3053 @param[in] Scope 1 if this opcpde is the beginning of a new scope.
3054 0 if this opcode is within the current scope.
3055
3056 @retval NULL There is not enough space left in Buffer to add the opcode.
3057 @retval Other A pointer to the created opcode.
3058
3059 **/
3060 UINT8 *
3061 EFIAPI
3062 HiiCreateSubTitleOpCode (
3063 IN VOID *OpCodeHandle,
3064 IN EFI_STRING_ID Prompt,
3065 IN EFI_STRING_ID Help,
3066 IN UINT8 Flags,
3067 IN UINT8 Scope
3068 )
3069 {
3070 EFI_IFR_SUBTITLE OpCode;
3071
3072 ASSERT (Scope <= 1);
3073 ASSERT ((Flags & (~(EFI_IFR_FLAGS_HORIZONTAL))) == 0);
3074
3075 ZeroMem (&OpCode, sizeof (OpCode));
3076 OpCode.Statement.Prompt = Prompt;
3077 OpCode.Statement.Help = Help;
3078 OpCode.Flags = Flags;
3079
3080 return InternalHiiCreateOpCodeExtended (
3081 OpCodeHandle,
3082 &OpCode,
3083 EFI_IFR_SUBTITLE_OP,
3084 sizeof (OpCode),
3085 0,
3086 Scope
3087 );
3088 }
3089
3090 /**
3091 Create EFI_IFR_REF_OP opcode.
3092
3093 If OpCodeHandle is NULL, then ASSERT().
3094 If any reserved bits are set in QuestionFlags, then ASSERT().
3095
3096 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3097 @param[in] FormId Destination Form ID
3098 @param[in] Prompt String ID for Prompt
3099 @param[in] Help String ID for Help
3100 @param[in] QuestionFlags Flags in Question Header
3101 @param[in] QuestionId Question ID
3102
3103 @retval NULL There is not enough space left in Buffer to add the opcode.
3104 @retval Other A pointer to the created opcode.
3105
3106 **/
3107 UINT8 *
3108 EFIAPI
3109 HiiCreateGotoOpCode (
3110 IN VOID *OpCodeHandle,
3111 IN EFI_FORM_ID FormId,
3112 IN EFI_STRING_ID Prompt,
3113 IN EFI_STRING_ID Help,
3114 IN UINT8 QuestionFlags,
3115 IN EFI_QUESTION_ID QuestionId
3116 )
3117 {
3118 EFI_IFR_REF OpCode;
3119
3120 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3121
3122 ZeroMem (&OpCode, sizeof (OpCode));
3123 OpCode.Question.Header.Prompt = Prompt;
3124 OpCode.Question.Header.Help = Help;
3125 OpCode.Question.QuestionId = QuestionId;
3126 OpCode.Question.Flags = QuestionFlags;
3127 OpCode.FormId = FormId;
3128
3129 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_REF_OP, sizeof (OpCode));
3130 }
3131
3132 /**
3133 Create EFI_IFR_CHECKBOX_OP opcode.
3134
3135 If OpCodeHandle is NULL, then ASSERT().
3136 If any reserved bits are set in QuestionFlags, then ASSERT().
3137 If any reserved bits are set in CheckBoxFlags, then ASSERT().
3138
3139 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3140 @param[in] QuestionId Question ID
3141 @param[in] VarStoreId Storage ID
3142 @param[in] VarOffset Offset in Storage
3143 @param[in] Prompt String ID for Prompt
3144 @param[in] Help String ID for Help
3145 @param[in] QuestionFlags Flags in Question Header
3146 @param[in] CheckBoxFlags Flags for checkbox opcode
3147 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3148 is an optional parameter that may be NULL.
3149
3150 @retval NULL There is not enough space left in Buffer to add the opcode.
3151 @retval Other A pointer to the created opcode.
3152
3153 **/
3154 UINT8 *
3155 EFIAPI
3156 HiiCreateCheckBoxOpCode (
3157 IN VOID *OpCodeHandle,
3158 IN EFI_QUESTION_ID QuestionId,
3159 IN EFI_VARSTORE_ID VarStoreId,
3160 IN UINT16 VarOffset,
3161 IN EFI_STRING_ID Prompt,
3162 IN EFI_STRING_ID Help,
3163 IN UINT8 QuestionFlags,
3164 IN UINT8 CheckBoxFlags,
3165 IN VOID *DefaultsOpCodeHandle OPTIONAL
3166 )
3167 {
3168 EFI_IFR_CHECKBOX OpCode;
3169 UINTN Position;
3170
3171 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3172
3173 ZeroMem (&OpCode, sizeof (OpCode));
3174 OpCode.Question.QuestionId = QuestionId;
3175 OpCode.Question.VarStoreId = VarStoreId;
3176 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3177 OpCode.Question.Header.Prompt = Prompt;
3178 OpCode.Question.Header.Help = Help;
3179 OpCode.Question.Flags = QuestionFlags;
3180 OpCode.Flags = CheckBoxFlags;
3181
3182 if (DefaultsOpCodeHandle == NULL) {
3183 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_CHECKBOX_OP, sizeof (OpCode));
3184 }
3185
3186 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3187 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_CHECKBOX_OP, sizeof (OpCode), 0, 1);
3188 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3189 HiiCreateEndOpCode (OpCodeHandle);
3190 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3191 }
3192
3193 /**
3194 Create EFI_IFR_NUMERIC_OP opcode.
3195
3196 If OpCodeHandle is NULL, then ASSERT().
3197 If any reserved bits are set in QuestionFlags, then ASSERT().
3198 If any reserved bits are set in NumericFlags, then ASSERT().
3199
3200 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3201 @param[in] QuestionId Question ID
3202 @param[in] VarStoreId Storage ID
3203 @param[in] VarOffset Offset in Storage
3204 @param[in] Prompt String ID for Prompt
3205 @param[in] Help String ID for Help
3206 @param[in] QuestionFlags Flags in Question Header
3207 @param[in] NumericFlags Flags for numeric opcode
3208 @param[in] Minimum Numeric minimum value
3209 @param[in] Maximum Numeric maximum value
3210 @param[in] Step Numeric step for edit
3211 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3212 is an optional parameter that may be NULL.
3213
3214 @retval NULL There is not enough space left in Buffer to add the opcode.
3215 @retval Other A pointer to the created opcode.
3216
3217 **/
3218 UINT8 *
3219 EFIAPI
3220 HiiCreateNumericOpCode (
3221 IN VOID *OpCodeHandle,
3222 IN EFI_QUESTION_ID QuestionId,
3223 IN EFI_VARSTORE_ID VarStoreId,
3224 IN UINT16 VarOffset,
3225 IN EFI_STRING_ID Prompt,
3226 IN EFI_STRING_ID Help,
3227 IN UINT8 QuestionFlags,
3228 IN UINT8 NumericFlags,
3229 IN UINT64 Minimum,
3230 IN UINT64 Maximum,
3231 IN UINT64 Step,
3232 IN VOID *DefaultsOpCodeHandle OPTIONAL
3233 )
3234 {
3235 EFI_IFR_NUMERIC OpCode;
3236 UINTN Position;
3237
3238 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3239
3240 ZeroMem (&OpCode, sizeof (OpCode));
3241 OpCode.Question.QuestionId = QuestionId;
3242 OpCode.Question.VarStoreId = VarStoreId;
3243 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3244 OpCode.Question.Header.Prompt = Prompt;
3245 OpCode.Question.Header.Help = Help;
3246 OpCode.Question.Flags = QuestionFlags;
3247 OpCode.Flags = NumericFlags;
3248
3249 switch (NumericFlags & EFI_IFR_NUMERIC_SIZE) {
3250 case EFI_IFR_NUMERIC_SIZE_1:
3251 OpCode.data.u8.MinValue = (UINT8)Minimum;
3252 OpCode.data.u8.MaxValue = (UINT8)Maximum;
3253 OpCode.data.u8.Step = (UINT8)Step;
3254 break;
3255
3256 case EFI_IFR_NUMERIC_SIZE_2:
3257 OpCode.data.u16.MinValue = (UINT16)Minimum;
3258 OpCode.data.u16.MaxValue = (UINT16)Maximum;
3259 OpCode.data.u16.Step = (UINT16)Step;
3260 break;
3261
3262 case EFI_IFR_NUMERIC_SIZE_4:
3263 OpCode.data.u32.MinValue = (UINT32)Minimum;
3264 OpCode.data.u32.MaxValue = (UINT32)Maximum;
3265 OpCode.data.u32.Step = (UINT32)Step;
3266 break;
3267
3268 case EFI_IFR_NUMERIC_SIZE_8:
3269 OpCode.data.u64.MinValue = Minimum;
3270 OpCode.data.u64.MaxValue = Maximum;
3271 OpCode.data.u64.Step = Step;
3272 break;
3273 }
3274
3275 if (DefaultsOpCodeHandle == NULL) {
3276 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_NUMERIC_OP, sizeof (OpCode));
3277 }
3278
3279 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3280 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_NUMERIC_OP, sizeof (OpCode), 0, 1);
3281 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3282 HiiCreateEndOpCode (OpCodeHandle);
3283 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3284 }
3285
3286 /**
3287 Create EFI_IFR_STRING_OP opcode.
3288
3289 If OpCodeHandle is NULL, then ASSERT().
3290 If any reserved bits are set in QuestionFlags, then ASSERT().
3291 If any reserved bits are set in StringFlags, then ASSERT().
3292
3293 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3294 @param[in] QuestionId Question ID
3295 @param[in] VarStoreId Storage ID
3296 @param[in] VarOffset Offset in Storage
3297 @param[in] Prompt String ID for Prompt
3298 @param[in] Help String ID for Help
3299 @param[in] QuestionFlags Flags in Question Header
3300 @param[in] StringFlags Flags for string opcode
3301 @param[in] MinSize String minimum length
3302 @param[in] MaxSize String maximum length
3303 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3304 is an optional parameter that may be NULL.
3305
3306 @retval NULL There is not enough space left in Buffer to add the opcode.
3307 @retval Other A pointer to the created opcode.
3308
3309 **/
3310 UINT8 *
3311 EFIAPI
3312 HiiCreateStringOpCode (
3313 IN VOID *OpCodeHandle,
3314 IN EFI_QUESTION_ID QuestionId,
3315 IN EFI_VARSTORE_ID VarStoreId,
3316 IN UINT16 VarOffset,
3317 IN EFI_STRING_ID Prompt,
3318 IN EFI_STRING_ID Help,
3319 IN UINT8 QuestionFlags,
3320 IN UINT8 StringFlags,
3321 IN UINT8 MinSize,
3322 IN UINT8 MaxSize,
3323 IN VOID *DefaultsOpCodeHandle OPTIONAL
3324 )
3325 {
3326 EFI_IFR_STRING OpCode;
3327 UINTN Position;
3328
3329 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3330
3331 ZeroMem (&OpCode, sizeof (OpCode));
3332 OpCode.Question.Header.Prompt = Prompt;
3333 OpCode.Question.Header.Help = Help;
3334 OpCode.Question.QuestionId = QuestionId;
3335 OpCode.Question.VarStoreId = VarStoreId;
3336 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3337 OpCode.Question.Flags = QuestionFlags;
3338 OpCode.MinSize = MinSize;
3339 OpCode.MaxSize = MaxSize;
3340 OpCode.Flags = (UINT8) (StringFlags & EFI_IFR_STRING_MULTI_LINE);
3341
3342 if (DefaultsOpCodeHandle == NULL) {
3343 return InternalHiiCreateOpCode (OpCodeHandle, &OpCode, EFI_IFR_STRING_OP, sizeof (OpCode));
3344 }
3345
3346 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3347 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_STRING_OP, sizeof (OpCode), 0, 1);
3348 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3349 HiiCreateEndOpCode (OpCodeHandle);
3350 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3351 }
3352
3353 /**
3354 Create EFI_IFR_ONE_OF_OP opcode.
3355
3356 If OpCodeHandle is NULL, then ASSERT().
3357 If any reserved bits are set in QuestionFlags, then ASSERT().
3358 If any reserved bits are set in OneOfFlags, then ASSERT().
3359
3360 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3361 @param[in] QuestionId Question ID
3362 @param[in] VarStoreId Storage ID
3363 @param[in] VarOffset Offset in Storage
3364 @param[in] Prompt String ID for Prompt
3365 @param[in] Help String ID for Help
3366 @param[in] QuestionFlags Flags in Question Header
3367 @param[in] OneOfFlags Flags for oneof opcode
3368 @param[in] OptionsOpCodeHandle Handle for a buffer of ONE_OF_OPTION opcodes.
3369 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3370 is an optional parameter that may be NULL.
3371
3372 @retval NULL There is not enough space left in Buffer to add the opcode.
3373 @retval Other A pointer to the created opcode.
3374
3375 **/
3376 UINT8 *
3377 EFIAPI
3378 HiiCreateOneOfOpCode (
3379 IN VOID *OpCodeHandle,
3380 IN EFI_QUESTION_ID QuestionId,
3381 IN EFI_VARSTORE_ID VarStoreId,
3382 IN UINT16 VarOffset,
3383 IN EFI_STRING_ID Prompt,
3384 IN EFI_STRING_ID Help,
3385 IN UINT8 QuestionFlags,
3386 IN UINT8 OneOfFlags,
3387 IN VOID *OptionsOpCodeHandle,
3388 IN VOID *DefaultsOpCodeHandle OPTIONAL
3389 )
3390 {
3391 EFI_IFR_ONE_OF OpCode;
3392 UINTN Position;
3393
3394 ASSERT (OptionsOpCodeHandle != NULL);
3395 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3396
3397 ZeroMem (&OpCode, sizeof (OpCode));
3398 OpCode.Question.Header.Prompt = Prompt;
3399 OpCode.Question.Header.Help = Help;
3400 OpCode.Question.QuestionId = QuestionId;
3401 OpCode.Question.VarStoreId = VarStoreId;
3402 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3403 OpCode.Question.Flags = QuestionFlags;
3404 OpCode.Flags = OneOfFlags;
3405
3406 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3407 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_ONE_OF_OP, sizeof (OpCode), 0, 1);
3408 InternalHiiAppendOpCodes (OpCodeHandle, OptionsOpCodeHandle);
3409 if (DefaultsOpCodeHandle != NULL) {
3410 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3411 }
3412 HiiCreateEndOpCode (OpCodeHandle);
3413 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3414 }
3415
3416 /**
3417 Create EFI_IFR_ORDERED_LIST_OP opcode.
3418
3419 If OpCodeHandle is NULL, then ASSERT().
3420 If any reserved bits are set in QuestionFlags, then ASSERT().
3421 If any reserved bits are set in OrderedListFlags, then ASSERT().
3422
3423 @param[in] OpCodeHandle Handle to the buffer of opcodes.
3424 @param[in] QuestionId Question ID
3425 @param[in] VarStoreId Storage ID
3426 @param[in] VarOffset Offset in Storage
3427 @param[in] Prompt String ID for Prompt
3428 @param[in] Help String ID for Help
3429 @param[in] QuestionFlags Flags in Question Header
3430 @param[in] OrderedListFlags Flags for ordered list opcode
3431 @param[in] DataType Type for option value
3432 @param[in] MaxContainers Maximum count for options in this ordered list
3433 @param[in] OptionsOpCodeHandle Handle for a buffer of ONE_OF_OPTION opcodes.
3434 @param[in] DefaultsOpCodeHandle Handle for a buffer of DEFAULT opcodes. This
3435 is an optional parameter that may be NULL.
3436
3437 @retval NULL There is not enough space left in Buffer to add the opcode.
3438 @retval Other A pointer to the created opcode.
3439
3440 **/
3441 UINT8 *
3442 EFIAPI
3443 HiiCreateOrderedListOpCode (
3444 IN VOID *OpCodeHandle,
3445 IN EFI_QUESTION_ID QuestionId,
3446 IN EFI_VARSTORE_ID VarStoreId,
3447 IN UINT16 VarOffset,
3448 IN EFI_STRING_ID Prompt,
3449 IN EFI_STRING_ID Help,
3450 IN UINT8 QuestionFlags,
3451 IN UINT8 OrderedListFlags,
3452 IN UINT8 DataType,
3453 IN UINT8 MaxContainers,
3454 IN VOID *OptionsOpCodeHandle,
3455 IN VOID *DefaultsOpCodeHandle OPTIONAL
3456 )
3457 {
3458 EFI_IFR_ORDERED_LIST OpCode;
3459 UINTN Position;
3460
3461 ASSERT (OptionsOpCodeHandle != NULL);
3462 ASSERT ((QuestionFlags & (~(EFI_IFR_FLAG_READ_ONLY | EFI_IFR_FLAG_CALLBACK | EFI_IFR_FLAG_RESET_REQUIRED | EFI_IFR_FLAG_OPTIONS_ONLY))) == 0);
3463
3464 ZeroMem (&OpCode, sizeof (OpCode));
3465 OpCode.Question.Header.Prompt = Prompt;
3466 OpCode.Question.Header.Help = Help;
3467 OpCode.Question.QuestionId = QuestionId;
3468 OpCode.Question.VarStoreId = VarStoreId;
3469 OpCode.Question.VarStoreInfo.VarOffset = VarOffset;
3470 OpCode.Question.Flags = QuestionFlags;
3471 OpCode.MaxContainers = MaxContainers;
3472 OpCode.Flags = OrderedListFlags;
3473
3474 Position = InternalHiiOpCodeHandlePosition (OpCodeHandle);
3475 InternalHiiCreateOpCodeExtended (OpCodeHandle, &OpCode, EFI_IFR_ORDERED_LIST_OP, sizeof (OpCode), 0, 1);
3476 InternalHiiAppendOpCodes (OpCodeHandle, OptionsOpCodeHandle);
3477 if (DefaultsOpCodeHandle != NULL) {
3478 InternalHiiAppendOpCodes (OpCodeHandle, DefaultsOpCodeHandle);
3479 }
3480 HiiCreateEndOpCode (OpCodeHandle);
3481 return InternalHiiOpCodeHandleBuffer (OpCodeHandle) + Position;
3482 }
3483
3484 /**
3485 This is the internal worker function to update the data in
3486 a form specified by FormSetGuid, FormId and Label.
3487
3488 @param FormSetGuid The optional Formset GUID.
3489 @param FormId The Form ID.
3490 @param Package The package header.
3491
3492 @param TempPacakge The resultant package.
3493
3494 @retval EFI_SUCCESS The function completes successfully.
3495
3496 **/
3497 EFI_STATUS
3498 EFIAPI
3499 InternalHiiUpdateFormPackageData (
3500 IN EFI_GUID *FormSetGuid, OPTIONAL
3501 IN EFI_FORM_ID FormId,
3502 IN EFI_HII_PACKAGE_HEADER *Package,
3503 IN HII_LIB_OPCODE_BUFFER *OpCodeBufferStart,
3504 IN HII_LIB_OPCODE_BUFFER *OpCodeBufferEnd, OPTIONAL
3505 OUT EFI_HII_PACKAGE_HEADER *TempPackage
3506 )
3507 {
3508 UINTN AddSize;
3509 UINT8 *BufferPos;
3510 EFI_HII_PACKAGE_HEADER PackageHeader;
3511 UINTN Offset;
3512 EFI_IFR_OP_HEADER *IfrOpHdr;
3513 EFI_IFR_OP_HEADER *UpdateIfrOpHdr;
3514 BOOLEAN GetFormSet;
3515 BOOLEAN GetForm;
3516 BOOLEAN Updated;
3517 UINTN UpdatePackageLength;
3518
3519 CopyMem (TempPackage, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3520 UpdatePackageLength = sizeof (EFI_HII_PACKAGE_HEADER);
3521 BufferPos = (UINT8 *) (TempPackage + 1);
3522
3523 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3524 IfrOpHdr = (EFI_IFR_OP_HEADER *)((UINT8 *) Package + sizeof (EFI_HII_PACKAGE_HEADER));
3525 Offset = sizeof (EFI_HII_PACKAGE_HEADER);
3526 GetFormSet = (BOOLEAN) ((FormSetGuid == NULL) ? TRUE : FALSE);
3527 GetForm = FALSE;
3528 Updated = FALSE;
3529
3530 while (Offset < PackageHeader.Length) {
3531 CopyMem (BufferPos, IfrOpHdr, IfrOpHdr->Length);
3532 BufferPos += IfrOpHdr->Length;
3533 UpdatePackageLength += IfrOpHdr->Length;
3534
3535 //
3536 // Find the matched FormSet and Form
3537 //
3538 if ((IfrOpHdr->OpCode == EFI_IFR_FORM_SET_OP) && (FormSetGuid != NULL)) {
3539 if (CompareGuid((GUID *)(VOID *)&((EFI_IFR_FORM_SET *) IfrOpHdr)->Guid, FormSetGuid)) {
3540 GetFormSet = TRUE;
3541 } else {
3542 GetFormSet = FALSE;
3543 }
3544 } else if (IfrOpHdr->OpCode == EFI_IFR_FORM_OP) {
3545 if (CompareMem (&((EFI_IFR_FORM *) IfrOpHdr)->FormId, &FormId, sizeof (EFI_FORM_ID)) == 0) {
3546 GetForm = TRUE;
3547 } else {
3548 GetForm = FALSE;
3549 }
3550 }
3551
3552 //
3553 // The matched Form is found, and Update data in this form
3554 //
3555 if (GetFormSet && GetForm) {
3556 UpdateIfrOpHdr = (EFI_IFR_OP_HEADER *) OpCodeBufferStart->Buffer;
3557 if ((UpdateIfrOpHdr->Length == IfrOpHdr->Length) && \
3558 (CompareMem (IfrOpHdr, UpdateIfrOpHdr, UpdateIfrOpHdr->Length) == 0)) {
3559 //
3560 // Remove the original data when End OpCode buffer exist.
3561 //
3562 if (OpCodeBufferEnd != NULL) {
3563 Offset += IfrOpHdr->Length;
3564 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((UINT8 *) (IfrOpHdr) + IfrOpHdr->Length);
3565 UpdateIfrOpHdr = (EFI_IFR_OP_HEADER *) OpCodeBufferEnd->Buffer;
3566 while (Offset < PackageHeader.Length) {
3567 //
3568 // Search the matched end opcode
3569 //
3570 if ((UpdateIfrOpHdr->Length == IfrOpHdr->Length) && \
3571 (CompareMem (IfrOpHdr, UpdateIfrOpHdr, UpdateIfrOpHdr->Length) == 0)) {
3572 break;
3573 }
3574 //
3575 // Go to the next Op-Code
3576 //
3577 Offset += IfrOpHdr->Length;
3578 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((UINT8 *) (IfrOpHdr) + IfrOpHdr->Length);
3579 }
3580
3581 if (Offset >= PackageHeader.Length) {
3582 //
3583 // The end opcode is not found.
3584 //
3585 return EFI_NOT_FOUND;
3586 }
3587 }
3588
3589 //
3590 // Insert the updated data
3591 //
3592 AddSize = ((EFI_IFR_OP_HEADER *) OpCodeBufferStart->Buffer)->Length;
3593 CopyMem (BufferPos, OpCodeBufferStart->Buffer + AddSize, OpCodeBufferStart->Position - AddSize);
3594 BufferPos += OpCodeBufferStart->Position - AddSize;
3595 UpdatePackageLength += OpCodeBufferStart->Position - AddSize;
3596
3597 if (OpCodeBufferEnd != NULL) {
3598 //
3599 // Add the end opcode
3600 //
3601 CopyMem (BufferPos, IfrOpHdr, IfrOpHdr->Length);
3602 BufferPos += IfrOpHdr->Length;
3603 UpdatePackageLength += IfrOpHdr->Length;
3604 }
3605
3606 //
3607 // Copy the left package data.
3608 //
3609 Offset += IfrOpHdr->Length;
3610 CopyMem (BufferPos, (UINT8 *) Package + Offset, PackageHeader.Length - Offset);
3611 UpdatePackageLength += PackageHeader.Length - Offset;
3612
3613 //
3614 // Set update flag
3615 //
3616 Updated = TRUE;
3617 break;
3618 }
3619 }
3620
3621 //
3622 // Go to the next Op-Code
3623 //
3624 Offset += IfrOpHdr->Length;
3625 IfrOpHdr = (EFI_IFR_OP_HEADER *) ((CHAR8 *) (IfrOpHdr) + IfrOpHdr->Length);
3626 }
3627
3628 if (!Updated) {
3629 //
3630 // The updated opcode buffer is not found.
3631 //
3632 return EFI_NOT_FOUND;
3633 }
3634 //
3635 // Update the package length.
3636 //
3637 PackageHeader.Length = (UINT32) UpdatePackageLength;
3638 CopyMem (TempPackage, &PackageHeader, sizeof (EFI_HII_PACKAGE_HEADER));
3639
3640 return EFI_SUCCESS;
3641 }
3642
3643 /**
3644 This function updates a form that has previously been registered with the HII
3645 Database. This function will perform at most one update operation.
3646
3647 The form to update is specified by Handle, FormSetGuid, and FormId. Binary
3648 comparisons of IFR opcodes are performed from the beginning of the form being
3649 updated until an IFR opcode is found that exactly matches the first IFR opcode
3650 specifed by StartOpCodeHandle. The following rules are used to determine if
3651 an insert, replace, or delete operation is performed.
3652
3653 1) If no matches are found, then NULL is returned.
3654 2) If a match is found, and EndOpCodeHandle is NULL, then all of the IFR opcodes
3655 from StartOpcodeHandle except the first opcode are inserted immediately after
3656 the matching IFR opcode in the form beng updated.
3657 3) If a match is found, and EndOpCodeHandle is not NULL, then a search is made
3658 from the matching IFR opcode until an IFR opcode exatly matches the first
3659 IFR opcode specified by EndOpCodeHandle. If no match is found for the first
3660 IFR opcode specified by EndOpCodeHandle, then NULL is returned. If a match
3661 is found, then all of the IFR opcodes between the start match and the end
3662 match are deleted from the form being updated and all of the IFR opcodes
3663 from StartOpcodeHandle except the first opcode are inserted immediately after
3664 the matching start IFR opcode. If StartOpCcodeHandle only contains one
3665 IFR instruction, then the result of ths operation will delete all of the IFR
3666 opcodes between the start end matches.
3667
3668 If HiiHandle is NULL, then ASSERT().
3669 If StartOpCodeHandle is NULL, then ASSERT().
3670
3671 @param[in] HiiHandle The HII Handle of the form to update.
3672 @param[in] FormSetGuid The Formset GUID of the form to update. This
3673 is an optional parameter that may be NULL.
3674 If it is NULL, all FormSet will be updated.
3675 @param[in] FormId The ID of the form to update.
3676 @param[in] StartOpCodeHandle An OpCode Handle that contains the set of IFR
3677 opcodes to be inserted or replaced in the form.
3678 The first IFR instruction in StartOpCodeHandle
3679 is used to find matching IFR opcode in the
3680 form.
3681 @param[in] EndOpCodeHandle An OpCcode Handle that contains the IFR opcode
3682 that marks the end of a replace operation in
3683 the form. This is an optional parameter that
3684 may be NULL. If it is NULL, then an the IFR
3685 opcodes specified by StartOpCodeHandle are
3686 inserted into the form.
3687
3688 @retval EFI_OUT_OF_RESOURCES No enough memory resource is allocated.
3689 @retval EFI_NOT_FOUND The following cases will return EFI_NOT_FOUND.
3690 1) The form specified by HiiHandle, FormSetGuid,
3691 and FormId could not be found in the HII Database.
3692 2) No IFR opcodes in the target form match the first
3693 IFR opcode in StartOpCodeHandle.
3694 3) EndOpCOde is not NULL, and no IFR opcodes in the
3695 target form following a matching start opcode match
3696 the first IFR opcode in EndOpCodeHandle.
3697 @retval EFI_SUCCESS The matched form is updated by StartOpcode.
3698
3699 **/
3700 EFI_STATUS
3701 EFIAPI
3702 HiiUpdateForm (
3703 IN EFI_HII_HANDLE HiiHandle,
3704 IN EFI_GUID *FormSetGuid, OPTIONAL
3705 IN EFI_FORM_ID FormId,
3706 IN VOID *StartOpcodeHandle,
3707 IN VOID *EndOpcodeHandle OPTIONAL
3708 )
3709 {
3710 EFI_STATUS Status;
3711 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
3712 UINT32 PackageListLength;
3713 UINT32 Offset;
3714 EFI_HII_PACKAGE_LIST_HEADER *UpdatePackageList;
3715 UINTN BufferSize;
3716 UINT8 *UpdateBufferPos;
3717 EFI_HII_PACKAGE_HEADER *Package;
3718 EFI_HII_PACKAGE_HEADER *TempPacakge;
3719 EFI_HII_PACKAGE_HEADER PackageHeader;
3720 BOOLEAN Updated;
3721 HII_LIB_OPCODE_BUFFER *OpCodeBufferStart;
3722 HII_LIB_OPCODE_BUFFER *OpCodeBufferEnd;
3723
3724 //
3725 // Input update data can't be NULL.
3726 //
3727 ASSERT (HiiHandle != NULL);
3728 ASSERT (StartOpcodeHandle != NULL);
3729 UpdatePackageList = NULL;
3730 TempPacakge = NULL;
3731 HiiPackageList = NULL;
3732
3733 //
3734 // Restrive buffer data from Opcode Handle
3735 //
3736 OpCodeBufferStart = (HII_LIB_OPCODE_BUFFER *) StartOpcodeHandle;
3737 OpCodeBufferEnd = (HII_LIB_OPCODE_BUFFER *) EndOpcodeHandle;
3738
3739 //
3740 // Get the orginal package list
3741 //
3742 BufferSize = 0;
3743 HiiPackageList = NULL;
3744 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
3745 //
3746 // The return status should always be EFI_BUFFER_TOO_SMALL as input buffer's size is 0.
3747 //
3748 if (Status != EFI_BUFFER_TOO_SMALL) {
3749 return Status;
3750 }
3751
3752 HiiPackageList = AllocatePool (BufferSize);
3753 if (HiiPackageList == NULL) {
3754 Status = EFI_OUT_OF_RESOURCES;
3755 goto Finish;
3756 }
3757
3758 Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
3759 if (EFI_ERROR (Status)) {
3760 goto Finish;
3761 }
3762
3763 //
3764 // Calculate and allocate space for retrieval of IFR data
3765 //
3766 BufferSize += OpCodeBufferStart->Position;
3767 UpdatePackageList = AllocateZeroPool (BufferSize);
3768 if (UpdatePackageList == NULL) {
3769 Status = EFI_OUT_OF_RESOURCES;
3770 goto Finish;
3771 }
3772
3773 //
3774 // Allocate temp buffer to store the temp updated package buffer
3775 //
3776 TempPacakge = AllocateZeroPool (BufferSize);
3777 if (TempPacakge == NULL) {
3778 Status = EFI_OUT_OF_RESOURCES;
3779 goto Finish;
3780 }
3781
3782 UpdateBufferPos = (UINT8 *) UpdatePackageList;
3783
3784 //
3785 // Copy the package list header
3786 //
3787 CopyMem (UpdateBufferPos, HiiPackageList, sizeof (EFI_HII_PACKAGE_LIST_HEADER));
3788 UpdateBufferPos += sizeof (EFI_HII_PACKAGE_LIST_HEADER);
3789
3790 //
3791 // Go through each package to find the matched pacakge and update one by one
3792 //
3793 Updated = FALSE;
3794 Offset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
3795 PackageListLength = ReadUnaligned32 (&HiiPackageList->PackageLength);
3796 while (Offset < PackageListLength) {
3797 Package = (EFI_HII_PACKAGE_HEADER *) (((UINT8 *) HiiPackageList) + Offset);
3798 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3799 Offset += Package->Length;
3800
3801 if (Package->Type == EFI_HII_PACKAGE_FORMS) {
3802 //
3803 // Check this package is the matched package.
3804 //
3805 Status = InternalHiiUpdateFormPackageData (FormSetGuid, FormId, Package, OpCodeBufferStart, OpCodeBufferEnd, TempPacakge);
3806 //
3807 // The matched package is found. Its pacakge buffer will be updated by the input new data.
3808 //
3809 if (!EFI_ERROR(Status)) {
3810 //
3811 // Set Update Flag
3812 //
3813 Updated = TRUE;
3814 //
3815 // Add updated package buffer
3816 //
3817 Package = TempPacakge;
3818 }
3819 }
3820
3821 //
3822 // Add pacakge buffer
3823 //
3824 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
3825 CopyMem (UpdateBufferPos, Package, PackageHeader.Length);
3826 UpdateBufferPos += PackageHeader.Length;
3827 }
3828
3829 if (Updated) {
3830 //
3831 // Update package list length
3832 //
3833 BufferSize = UpdateBufferPos - (UINT8 *) UpdatePackageList;
3834 WriteUnaligned32 (&UpdatePackageList->PackageLength, (UINT32) BufferSize);
3835
3836 //
3837 // Update Pacakge to show form
3838 //
3839 Status = gHiiDatabase->UpdatePackageList (gHiiDatabase, HiiHandle, UpdatePackageList);
3840 } else {
3841 //
3842 // Not matched form is found and updated.
3843 //
3844 Status = EFI_NOT_FOUND;
3845 }
3846
3847 Finish:
3848 if (HiiPackageList != NULL) {
3849 FreePool (HiiPackageList);
3850 }
3851
3852 if (UpdatePackageList != NULL) {
3853 FreePool (UpdatePackageList);
3854 }
3855
3856 if (TempPacakge != NULL) {
3857 FreePool (TempPacakge);
3858 }
3859
3860 return Status;
3861 }
3862
3863 /**
3864 Configure the buffer accrording to ConfigBody strings in the format of
3865 <Length:4 bytes>, <Offset: 2 bytes>, <Width:2 bytes>, <Data:n bytes>.
3866 This ConfigBody strings is generated by UEFI VfrCompiler for the default
3867 values in a Form Set. The name of the ConfigBody strings is VfrMyIfrNVDataDefault0000
3868 constructed following this rule:
3869 "Vfr" + varstore.name + "Default" + defaultstore.attributes.
3870 Check the generated C file in Output for details.
3871
3872 @param Buffer The start address of buffer.
3873 @param BufferSize The size of buffer.
3874 @param Number The number of the strings.
3875 @param ... Variable argument list for default value in <AltResp> format
3876 generated by the tool.
3877
3878 @retval EFI_BUFFER_TOO_SMALL the BufferSize is too small to operate.
3879 @retval EFI_INVALID_PARAMETER Buffer is NULL or BufferSize is 0.
3880 @retval EFI_SUCCESS Operation successful.
3881
3882 **/
3883 EFI_STATUS
3884 EFIAPI
3885 HiiIfrLibExtractDefault(
3886 IN VOID *Buffer,
3887 IN UINTN *BufferSize,
3888 UINTN Number,
3889 ...
3890 )
3891 {
3892 VA_LIST Args;
3893 UINTN Index;
3894 UINT32 TotalLen;
3895 UINT8 *BufCfgArray;
3896 UINT8 *BufferPos;
3897 UINT16 Offset;
3898 UINT16 Width;
3899 UINT8 *Value;
3900
3901 if ((Buffer == NULL) || (BufferSize == NULL)) {
3902 return EFI_INVALID_PARAMETER;
3903 }
3904
3905 Offset = 0;
3906 Width = 0;
3907 Value = NULL;
3908
3909 VA_START (Args, Number);
3910 for (Index = 0; Index < Number; Index++) {
3911 BufCfgArray = (UINT8 *) VA_ARG (Args, VOID *);
3912 TotalLen = ReadUnaligned32 ((UINT32 *)BufCfgArray);
3913 BufferPos = BufCfgArray + sizeof (UINT32);
3914
3915 while ((UINT32)(BufferPos - BufCfgArray) < TotalLen) {
3916 Offset = ReadUnaligned16 ((UINT16 *)BufferPos);
3917 BufferPos += sizeof (UINT16);
3918 Width = ReadUnaligned16 ((UINT16 *)BufferPos);
3919 BufferPos += sizeof (UINT16);
3920 Value = BufferPos;
3921 BufferPos += Width;
3922
3923 if ((UINTN)(Offset + Width) > *BufferSize) {
3924 return EFI_BUFFER_TOO_SMALL;
3925 }
3926
3927 CopyMem ((UINT8 *)Buffer + Offset, Value, Width);
3928 }
3929 }
3930 VA_END (Args);
3931
3932 *BufferSize = (UINTN)Offset;
3933
3934 return EFI_SUCCESS;
3935 }
3936