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