2 Implement protocol interface related to package registrations.
4 Copyright (c) 2006 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
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.
16 #include "HiiDatabase.h"
17 #include "HiiHandle.h"
20 STATIC BOOLEAN mInFrameworkHiiNewPack
= FALSE
;
21 STATIC BOOLEAN mInFrameworkHiiRemovePack
= FALSE
;
22 BOOLEAN mInFrameworkUpdatePakcage
= FALSE
;
27 IN CONST EFI_HII_PACKAGES
*Packages
,
28 UINTN
*IfrPackageCount
,
29 UINTN
*StringPackageCount
33 TIANO_AUTOGEN_PACKAGES_HEADER
**TianoAutogenPackageHdrArray
;
35 ASSERT (Packages
!= NULL
);
36 ASSERT (IfrPackageCount
!= NULL
);
37 ASSERT (StringPackageCount
!= NULL
);
40 *StringPackageCount
= 0;
42 TianoAutogenPackageHdrArray
= (TIANO_AUTOGEN_PACKAGES_HEADER
**) (((UINT8
*) &Packages
->GuidId
) + sizeof (Packages
->GuidId
));
44 for (Index
= 0; Index
< Packages
->NumberOfPackages
; Index
++) {
46 // BugBug: The current UEFI HII build tool generate a binary in the format defined in:
47 // TIANO_AUTOGEN_PACKAGES_HEADER. We assume that all packages generated in
48 // this binary is with same package type. So the returned IfrPackageCount and StringPackageCount
49 // may not be the exact number of valid package number in the binary generated
52 switch (TianoAutogenPackageHdrArray
[Index
]->PackageHeader
.Type
) {
53 case EFI_HII_PACKAGE_FORM
:
54 *IfrPackageCount
+= 1;
56 case EFI_HII_PACKAGE_STRINGS
:
57 *StringPackageCount
+= 1;
60 case EFI_HII_PACKAGE_SIMPLE_FONTS
:
64 // The following fonts are invalid for a module that using Framework to UEFI thunk layer.
66 case EFI_HII_PACKAGE_KEYBOARD_LAYOUT
:
67 case EFI_HII_PACKAGE_FONTS
:
68 case EFI_HII_PACKAGE_IMAGES
:
71 return EFI_INVALID_PARAMETER
;
80 Removes a node from a doubly linked list, and returns the node that follows
83 Removes the node Entry from a doubly linked list. It is up to the caller of
84 this function to release the memory used by this node if that is required. On
85 exit, the node following Entry in the doubly linked list is returned. If
86 Entry is the only node in the linked list, then the head node of the linked
89 If Entry is NULL, then ASSERT().
90 If Entry is the head node of an empty list, then ASSERT().
91 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
92 linked list containing Entry, including the Entry node, is greater than
93 or equal to PcdMaximumLinkedListLength, then ASSERT().
95 @param Entry A pointer to a node in a linked list
101 FindPackListWithOnlyIfrPackAndAddStringPack (
102 IN HII_THUNK_PRIVATE_DATA
*Private
,
103 IN HII_THUNK_CONTEXT
*StringPackageThunkContext
,
104 IN CONST EFI_HII_PACKAGE_LIST_HEADER
*StringPackageListHeader
109 HII_THUNK_CONTEXT
*ThunkContext
;
111 Link
= GetFirstNode (&Private
->ThunkContextListHead
);
112 while (!IsNull (&Private
->ThunkContextListHead
, Link
)) {
114 ThunkContext
= HII_THUNK_CONTEXT_FROM_LINK (Link
);
116 if (StringPackageThunkContext
!= ThunkContext
) {
118 // Skip the String Package Thunk Entry itself.
121 if (CompareGuid (&StringPackageListHeader
->PackageListGuid
, &ThunkContext
->TagGuid
)) {
123 ASSERT (ThunkContext
->StringPackageCount
== 0 && ThunkContext
->IfrPackageCount
== 1);
125 ThunkContext
->StringPackageCount
= GetPackageCountByType (StringPackageListHeader
, EFI_HII_PACKAGE_STRINGS
);
127 Status
= mHiiDatabase
->UpdatePackageList (
129 ThunkContext
->UefiHiiHandle
,
130 StringPackageListHeader
132 ASSERT (Status
!= EFI_NOT_FOUND
);
134 if (EFI_ERROR (Status
)) {
140 Link
= GetNextNode (&Private
->ThunkContextListHead
, Link
);
144 return EFI_NOT_FOUND
;
148 EFI_HII_PACKAGE_LIST_HEADER
*
149 PrepareUefiPackageListFromFrameworkHiiPackages (
150 IN CONST EFI_HII_PACKAGES
*Packages
,
151 IN CONST EFI_GUID
*PackageListGuid
154 UINTN NumberOfPackages
;
155 EFI_HII_PACKAGE_LIST_HEADER
*PackageListHeader
;
156 UINT8
*PackageListData
;
157 UINT32 PackageListLength
;
158 UINT32 PackageLength
;
159 EFI_HII_PACKAGE_HEADER PackageHeader
;
161 TIANO_AUTOGEN_PACKAGES_HEADER
**TianoAutogenPackageHdrArray
;
163 ASSERT (Packages
!= NULL
);
164 ASSERT (PackageListGuid
!= NULL
);
166 TianoAutogenPackageHdrArray
= (TIANO_AUTOGEN_PACKAGES_HEADER
**) ((UINT8
*) &Packages
->GuidId
+ sizeof (Packages
->GuidId
));
167 NumberOfPackages
= Packages
->NumberOfPackages
;
169 PackageListLength
= sizeof (EFI_HII_PACKAGE_LIST_HEADER
);
171 for (Index
= 0; Index
< NumberOfPackages
; Index
++) {
172 CopyMem (&PackageLength
, &TianoAutogenPackageHdrArray
[Index
]->BinaryLength
, sizeof (UINT32
));
174 //TIANO_AUTOGEN_PACKAGES_HEADER.BinaryLength include the BinaryLength itself.
176 PackageListLength
+= (PackageLength
- sizeof(UINT32
));
180 // Include the lenght of EFI_HII_PACKAGE_END
182 PackageListLength
+= sizeof (EFI_HII_PACKAGE_HEADER
);
183 PackageListHeader
= AllocateZeroPool (PackageListLength
);
184 ASSERT (PackageListHeader
!= NULL
);
186 CopyMem (&PackageListHeader
->PackageListGuid
, PackageListGuid
, sizeof (EFI_GUID
));
187 PackageListHeader
->PackageLength
= PackageListLength
;
189 PackageListData
= ((UINT8
*) PackageListHeader
) + sizeof (EFI_HII_PACKAGE_LIST_HEADER
);
191 for (Index
= 0; Index
< NumberOfPackages
; Index
++) {
192 CopyMem (&PackageLength
, &(TianoAutogenPackageHdrArray
[Index
]->BinaryLength
), sizeof (UINT32
));
193 PackageLength
-= sizeof (UINT32
);
194 CopyMem (PackageListData
, &(TianoAutogenPackageHdrArray
[Index
]->PackageHeader
), PackageLength
);
195 PackageListData
+= PackageLength
;
199 // Append EFI_HII_PACKAGE_END
201 PackageHeader
.Type
= EFI_HII_PACKAGE_END
;
202 PackageHeader
.Length
= sizeof (EFI_HII_PACKAGE_HEADER
);
203 CopyMem (PackageListData
, &PackageHeader
, PackageHeader
.Length
);
205 return PackageListHeader
;
210 IN CONST EFI_GUID
* InGuid
,
211 OUT EFI_GUID
* OutGuid
214 UINT64 MonotonicCount
;
216 CopyMem (OutGuid
, InGuid
, sizeof (EFI_GUID
));
218 gBS
->GetNextMonotonicCount (&MonotonicCount
);
220 // Use Monotonic Count as a psedo random number generator.
222 *((UINT64
*) OutGuid
) = *((UINT64
*) OutGuid
) + MonotonicCount
;
226 FindStringPackAndAddToPackListWithOnlyIfrPack(
227 IN HII_THUNK_PRIVATE_DATA
*Private
,
228 IN HII_THUNK_CONTEXT
*IfrThunkContext
233 EFI_HII_PACKAGE_LIST_HEADER
*StringPackageListHeader
;
235 HII_THUNK_CONTEXT
*ThunkContext
;
238 Link
= GetFirstNode (&Private
->ThunkContextListHead
);
240 while (!IsNull (&Private
->ThunkContextListHead
, Link
)) {
242 ThunkContext
= HII_THUNK_CONTEXT_FROM_LINK (Link
);
244 if (ThunkContext
!= IfrThunkContext
) {
245 if (CompareGuid (&IfrThunkContext
->TagGuid
, &ThunkContext
->TagGuid
) && (ThunkContext
->IfrPackageCount
== 0)) {
246 Status
= HiiLibExportPackageLists (ThunkContext
->UefiHiiHandle
, &StringPackageListHeader
, &Size
);
247 ASSERT_EFI_ERROR (Status
);
249 IfrThunkContext
->StringPackageCount
= GetPackageCountByType (StringPackageListHeader
, EFI_HII_PACKAGE_STRINGS
);
251 // Add Function to only get only String Packages from the Package List
253 Status
= mHiiDatabase
->UpdatePackageList (
255 IfrThunkContext
->UefiHiiHandle
,
256 StringPackageListHeader
258 ASSERT_EFI_ERROR (Status
);
260 FreePool (StringPackageListHeader
);
265 Link
= GetNextNode (&Private
->ThunkContextListHead
, Link
);
268 return EFI_NOT_FOUND
;
274 CONST EFI_GUID mAGuid
=
275 { 0x14f95e01, 0xd562, 0x432e, { 0x84, 0x4a, 0x95, 0xa4, 0x39, 0x5, 0x10, 0x7e } };
278 UefiRegisterPackageList(
279 IN HII_THUNK_PRIVATE_DATA
*Private
,
280 IN EFI_HII_PACKAGES
*Packages
,
281 OUT FRAMEWORK_EFI_HII_HANDLE
*Handle
285 UINTN StringPackageCount
;
286 UINTN IfrPackageCount
;
287 EFI_HII_PACKAGE_LIST_HEADER
*PackageListHeader
;
288 HII_THUNK_CONTEXT
*ThunkContext
;
291 PackageListHeader
= NULL
;
293 Status
= GetPackageCount (Packages
, &IfrPackageCount
, &StringPackageCount
);
294 ASSERT_EFI_ERROR (Status
);
296 if (IfrPackageCount
> 1) {
298 // HII Thunk only handle package with 0 or 1 IFR package.
300 return EFI_UNSUPPORTED
;
303 ThunkContext
= CreateThunkContext (Private
, StringPackageCount
, IfrPackageCount
);
304 if (ThunkContext
== NULL
) {
305 return EFI_OUT_OF_RESOURCES
;
307 ThunkContext
->ByFrameworkHiiNewPack
= TRUE
;
309 if (Packages
->GuidId
== NULL
) {
311 // UEFI HII Database require Package List GUID must be unique.
313 // If Packages->GuidId is NULL, the caller of FramworkHii->NewPack is registering
314 // packages with at least 1 StringPack and 1 IfrPack. Therefore, Packages->GuidId is
315 // not used as the name of the package list. A GUID is generated as a Package List
318 ASSERT (StringPackageCount
>=1 && IfrPackageCount
== 1);
319 Packages
->GuidId
= &GuidId
;
320 GenerateGuidId (&mAGuid
, Packages
->GuidId
);
322 //BugBug We need fix this.
323 //ASSERT (StringPackageCount == 0 || IfrPackageCount == 0);
324 CopyGuid (&GuidId
, Packages
->GuidId
);
328 // Record the Package List GUID, it is used as a name for the package list by Framework HII.
330 CopyGuid (&ThunkContext
->TagGuid
, Packages
->GuidId
);
332 if ((StringPackageCount
== 0) && (IfrPackageCount
!= 0)) {
334 // UEFI HII database does not allow two package list with the same GUID.
335 // In Framework HII implementation, Packages->GuidId is used as an identifier to associate
336 // a PackageList with only IFR to a Package list the with String package.
338 GenerateGuidId (Packages
->GuidId
, &GuidId
);
342 // UEFI HII require EFI_HII_CONFIG_ACCESS_PROTOCOL to be installed on a EFI_HANDLE, so
343 // that Setup Utility can load the Buffer Storage using this protocol.
345 if (IfrPackageCount
!= 0) {
346 InstallDefaultConfigAccessProtocol (Packages
, ThunkContext
);
348 PackageListHeader
= PrepareUefiPackageListFromFrameworkHiiPackages (Packages
, &GuidId
);
349 Status
= mHiiDatabase
->NewPackageList (
352 ThunkContext
->UefiHiiDriverHandle
,
353 &ThunkContext
->UefiHiiHandle
357 // BUGBUG: Remove when development is done
359 ASSERT_EFI_ERROR (Status
);
360 if (EFI_ERROR (Status
)) {
364 if (IfrPackageCount
== 0) {
365 if (StringPackageCount
!= 0) {
367 // Find if there is Package List with only IFR Package in the databasee with the same
368 // tag. If found, add the String Package to this Package List.
370 Status
= FindPackListWithOnlyIfrPackAndAddStringPack (
376 if (Status
== EFI_NOT_FOUND
) {
377 Status
= EFI_SUCCESS
;
381 CreateQuestionIdMap (ThunkContext
);
383 if (StringPackageCount
== 0) {
385 // Register the Package List to UEFI HII first.
387 Status
= FindStringPackAndAddToPackListWithOnlyIfrPack (
391 ASSERT_EFI_ERROR (Status
);
396 if (EFI_ERROR (Status
)) {
397 DestroyThunkContext (ThunkContext
);
399 InsertTailList (&Private
->ThunkContextListHead
, &ThunkContext
->Link
);
400 *Handle
= ThunkContext
->FwHiiHandle
;
403 SafeFreePool (PackageListHeader
);
412 IN EFI_HII_PROTOCOL
*This
,
413 IN EFI_HII_PACKAGES
*Packages
,
414 OUT FRAMEWORK_EFI_HII_HANDLE
*Handle
420 Extracts the various packs from a package list.
424 This - Pointer of HII protocol.
425 Packages - Pointer of HII packages.
426 Handle - Handle value to be returned.
430 EFI_SUCCESS - Pacakges has added to HII database successfully.
431 EFI_INVALID_PARAMETER - Invalid parameter.
436 HII_THUNK_PRIVATE_DATA
*Private
;
439 if (Handle
== NULL
) {
440 return EFI_INVALID_PARAMETER
;
443 if (Packages
== NULL
) {
444 return EFI_INVALID_PARAMETER
;
447 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
450 // We use a simple Global variable to inform NewPackNotify
451 // that the package list registered here is already registered
452 // in the HII Thunk Layer. So NewPackNotify does not need to
453 // call RegisterUefiHiiHandle () to registered it.
455 mInFrameworkHiiNewPack
= TRUE
;
457 Private
= HII_THUNK_PRIVATE_DATA_FROM_THIS(This
);
459 Status
= UefiRegisterPackageList (
465 mInFrameworkHiiNewPack
= FALSE
;
467 gBS
->RestoreTPL (OldTpl
);
475 IN EFI_HII_PROTOCOL
*This
,
476 IN FRAMEWORK_EFI_HII_HANDLE Handle
481 Removes the various packs from a Handle
490 HII_THUNK_PRIVATE_DATA
*Private
;
491 HII_THUNK_CONTEXT
*ThunkContext
;
494 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
496 mInFrameworkHiiRemovePack
= TRUE
;
498 Private
= HII_THUNK_PRIVATE_DATA_FROM_THIS(This
);
500 ThunkContext
= FwHiiHandleToThunkContext (Private
, Handle
);
502 if (ThunkContext
!= NULL
) {
503 Status
= mHiiDatabase
->RemovePackageList (
505 ThunkContext
->UefiHiiHandle
507 ASSERT_EFI_ERROR (Status
);
509 if (ThunkContext
->IfrPackageCount
!= 0) {
510 UninstallDefaultConfigAccessProtocol (ThunkContext
);
513 RemoveEntryList (&ThunkContext
->Link
);
515 DestroyThunkContext (ThunkContext
);
517 Status
= EFI_NOT_FOUND
;
520 mInFrameworkHiiRemovePack
= FALSE
;
522 gBS
->RestoreTPL (OldTpl
);
530 IN UINT8 PackageType
,
531 IN CONST EFI_GUID
*PackageGuid
,
532 IN CONST EFI_HII_PACKAGE_HEADER
*Package
,
533 IN EFI_HII_HANDLE Handle
,
534 IN EFI_HII_DATABASE_NOTIFY_TYPE NotifyType
538 HII_THUNK_PRIVATE_DATA
*Private
;
539 HII_THUNK_CONTEXT
*ThunkContext
;
541 ASSERT (PackageType
== EFI_HII_PACKAGE_STRINGS
|| PackageType
== EFI_HII_PACKAGE_FORM
);
542 ASSERT (NotifyType
== EFI_HII_DATABASE_NOTIFY_ADD_PACK
|| NotifyType
== EFI_HII_DATABASE_NOTIFY_NEW_PACK
);
544 Status
= EFI_SUCCESS
;
545 Private
= mHiiThunkPrivateData
;
547 if (mInFrameworkHiiNewPack
|| mInFrameworkUpdatePakcage
) {
552 // We only create a ThunkContext if the Uefi Hii Handle is only already registered
553 // by the HII Thunk Layer.
555 ThunkContext
= UefiHiiHandleToThunkContext (Private
, Handle
);
556 if (ThunkContext
== NULL
) {
557 ThunkContext
= CreateThunkContextForUefiHiiHandle (Handle
);
558 ASSERT (ThunkContext
!= NULL
);
560 InsertTailList (&Private
->ThunkContextListHead
, &ThunkContext
->Link
);
564 if (PackageType
== EFI_HII_PACKAGE_FORM
) {
565 Status
= CreateQuestionIdMap (ThunkContext
);
573 IsRemovingLastStringPack (
574 IN EFI_HII_HANDLE Handle
577 EFI_HII_PACKAGE_LIST_HEADER
*HiiPackageList
;
580 EFI_HII_PACKAGE_HEADER
*PackageHeader
;
581 UINTN StringPackageCount
;
583 HiiPackageList
= NULL
;
585 StringPackageCount
= 0;
587 Status
= HiiLibExportPackageLists (Handle
, &HiiPackageList
, &BufferSize
);
588 ASSERT_EFI_ERROR (Status
);
590 PackageHeader
= (EFI_HII_PACKAGE_HEADER
*) ((UINT8
*) HiiPackageList
+ sizeof (EFI_HII_PACKAGE_LIST_HEADER
));
592 while (PackageHeader
->Type
!= EFI_HII_PACKAGE_END
) {
593 switch (PackageHeader
->Type
) {
594 case EFI_HII_PACKAGE_STRINGS
:
595 StringPackageCount
++;
597 if (StringPackageCount
> 1) {
599 // More than one String Pack in the package list
601 FreePool (HiiPackageList
);
610 // goto header of next package
612 PackageHeader
= (EFI_HII_PACKAGE_HEADER
*) ((UINT8
*) PackageHeader
+ PackageHeader
->Length
);
617 // We will always be notified for the removal of String Pack from a package list.
618 // So StringPackageCount must be one at this point.
620 ASSERT (StringPackageCount
== 1);
622 FreePool (HiiPackageList
);
628 // Framework HII module may cache a GUID as the name of the package list.
629 // Then search for the Framework HII handle database for the handle matching
635 IN UINT8 PackageType
,
636 IN CONST EFI_GUID
*PackageGuid
,
637 IN CONST EFI_HII_PACKAGE_HEADER
*Package
,
638 IN EFI_HII_HANDLE Handle
,
639 IN EFI_HII_DATABASE_NOTIFY_TYPE NotifyType
643 HII_THUNK_PRIVATE_DATA
*Private
;
644 HII_THUNK_CONTEXT
* ThunkContext
;
646 Status
= EFI_SUCCESS
;
648 ASSERT (PackageType
== EFI_HII_PACKAGE_STRINGS
);
649 ASSERT (NotifyType
== EFI_HII_DATABASE_NOTIFY_REMOVE_PACK
);
651 if (mInFrameworkHiiRemovePack
|| mInFrameworkUpdatePakcage
) {
655 Private
= mHiiThunkPrivateData
;
657 ThunkContext
= UefiHiiHandleToThunkContext (Private
, Handle
);
659 if (!ThunkContext
->ByFrameworkHiiNewPack
) {
660 if (IsRemovingLastStringPack (Handle
)) {
662 // If the string package will be removed is the last string package
663 // in the package list, we will remove the HII Thunk entry from the
666 DestroyThunkContextForUefiHiiHandle (Private
, Handle
);