]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiToUefiHiiThunk/Package.c
1) Add in fix to handle the case when a form is not related to a buffer storage.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FrameworkHiiToUefiHiiThunk / Package.c
1 /**@file
2 Implement protocol interface related to package registrations.
3
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
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
16 #include "HiiDatabase.h"
17 #include "HiiHandle.h"
18
19
20 STATIC BOOLEAN mInFrameworkHiiNewPack = FALSE;
21 STATIC BOOLEAN mInFrameworkHiiRemovePack = FALSE;
22 BOOLEAN mInFrameworkUpdatePakcage = FALSE;
23
24
25 EFI_STATUS
26 GetPackageCount (
27 IN CONST EFI_HII_PACKAGES *Packages,
28 UINTN *IfrPackageCount,
29 UINTN *StringPackageCount
30 )
31 {
32 UINTN Index;
33 TIANO_AUTOGEN_PACKAGES_HEADER **TianoAutogenPackageHdrArray;
34
35 ASSERT (Packages != NULL);
36 ASSERT (IfrPackageCount != NULL);
37 ASSERT (StringPackageCount != NULL);
38
39 *IfrPackageCount = 0;
40 *StringPackageCount = 0;
41
42 TianoAutogenPackageHdrArray = (TIANO_AUTOGEN_PACKAGES_HEADER **) (((UINT8 *) &Packages->GuidId) + sizeof (Packages->GuidId));
43
44 for (Index = 0; Index < Packages->NumberOfPackages; Index++) {
45 //
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
50 // by HII Build tool.
51 //
52 switch (TianoAutogenPackageHdrArray[Index]->PackageHeader.Type) {
53 case EFI_HII_PACKAGE_FORM:
54 *IfrPackageCount += 1;
55 break;
56 case EFI_HII_PACKAGE_STRINGS:
57 *StringPackageCount += 1;
58 break;
59
60 case EFI_HII_PACKAGE_SIMPLE_FONTS:
61 break;
62
63 //
64 // The following fonts are invalid for a module that using Framework to UEFI thunk layer.
65 //
66 case EFI_HII_PACKAGE_KEYBOARD_LAYOUT:
67 case EFI_HII_PACKAGE_FONTS:
68 case EFI_HII_PACKAGE_IMAGES:
69 default:
70 ASSERT (FALSE);
71 return EFI_INVALID_PARAMETER;
72 break;
73 }
74 }
75
76 return EFI_SUCCESS;
77 }
78
79 /**
80 Removes a node from a doubly linked list, and returns the node that follows
81 the removed node.
82
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
87 list is returned.
88
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().
94
95 @param Entry A pointer to a node in a linked list
96
97 @return Entry
98
99 **/
100 EFI_STATUS
101 FindPackListWithOnlyIfrPackAndAddStringPack (
102 IN HII_THUNK_PRIVATE_DATA *Private,
103 IN HII_THUNK_CONTEXT *StringPackageThunkContext,
104 IN CONST EFI_HII_PACKAGE_LIST_HEADER *StringPackageListHeader
105 )
106 {
107 EFI_STATUS Status;
108 LIST_ENTRY *Link;
109 HII_THUNK_CONTEXT *ThunkContext;
110
111 Link = GetFirstNode (&Private->ThunkContextListHead);
112 while (!IsNull (&Private->ThunkContextListHead, Link)) {
113
114 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
115
116 if (StringPackageThunkContext != ThunkContext) {
117 //
118 // Skip the String Package Thunk Entry itself.
119 //
120
121 if (CompareGuid (&StringPackageListHeader->PackageListGuid, &ThunkContext->TagGuid)) {
122
123 ASSERT (ThunkContext->StringPackageCount == 0 && ThunkContext->IfrPackageCount == 1);
124
125 ThunkContext->StringPackageCount = GetPackageCountByType (StringPackageListHeader, EFI_HII_PACKAGE_STRINGS);
126
127 Status = mHiiDatabase->UpdatePackageList (
128 mHiiDatabase,
129 ThunkContext->UefiHiiHandle,
130 StringPackageListHeader
131 );
132 ASSERT (Status != EFI_NOT_FOUND);
133
134 if (EFI_ERROR (Status)) {
135 return Status;
136 }
137 }
138 }
139
140 Link = GetNextNode (&Private->ThunkContextListHead, Link);
141 }
142
143
144 return EFI_NOT_FOUND;
145 }
146
147
148 EFI_HII_PACKAGE_LIST_HEADER *
149 PrepareUefiPackageListFromFrameworkHiiPackages (
150 IN CONST EFI_HII_PACKAGES *Packages,
151 IN CONST EFI_GUID *PackageListGuid
152 )
153 {
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;
160 UINTN Index;
161 TIANO_AUTOGEN_PACKAGES_HEADER **TianoAutogenPackageHdrArray;
162
163 ASSERT (Packages != NULL);
164 ASSERT (PackageListGuid != NULL);
165
166 TianoAutogenPackageHdrArray = (TIANO_AUTOGEN_PACKAGES_HEADER **) ((UINT8 *) &Packages->GuidId + sizeof (Packages->GuidId));
167 NumberOfPackages = Packages->NumberOfPackages;
168
169 PackageListLength = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
170
171 for (Index = 0; Index < NumberOfPackages; Index++) {
172 CopyMem (&PackageLength, &TianoAutogenPackageHdrArray[Index]->BinaryLength, sizeof (UINT32));
173 //
174 //TIANO_AUTOGEN_PACKAGES_HEADER.BinaryLength include the BinaryLength itself.
175 //
176 PackageListLength += (PackageLength - sizeof(UINT32));
177 }
178
179 //
180 // Include the lenght of EFI_HII_PACKAGE_END
181 //
182 PackageListLength += sizeof (EFI_HII_PACKAGE_HEADER);
183 PackageListHeader = AllocateZeroPool (PackageListLength);
184 ASSERT (PackageListHeader != NULL);
185
186 CopyMem (&PackageListHeader->PackageListGuid, PackageListGuid, sizeof (EFI_GUID));
187 PackageListHeader->PackageLength = PackageListLength;
188
189 PackageListData = ((UINT8 *) PackageListHeader) + sizeof (EFI_HII_PACKAGE_LIST_HEADER);
190
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;
196 }
197
198 //
199 // Append EFI_HII_PACKAGE_END
200 //
201 PackageHeader.Type = EFI_HII_PACKAGE_END;
202 PackageHeader.Length = sizeof (EFI_HII_PACKAGE_HEADER);
203 CopyMem (PackageListData, &PackageHeader, PackageHeader.Length);
204
205 return PackageListHeader;
206 }
207
208 VOID
209 GenerateGuidId (
210 IN CONST EFI_GUID * InGuid,
211 OUT EFI_GUID * OutGuid
212 )
213 {
214 UINT64 MonotonicCount;
215
216 CopyMem (OutGuid, InGuid, sizeof (EFI_GUID));
217
218 gBS->GetNextMonotonicCount (&MonotonicCount);
219 //
220 // Use Monotonic Count as a psedo random number generator.
221 //
222 *((UINT64 *) OutGuid) = *((UINT64 *) OutGuid) + MonotonicCount;
223 }
224
225 EFI_STATUS
226 FindStringPackAndAddToPackListWithOnlyIfrPack(
227 IN HII_THUNK_PRIVATE_DATA *Private,
228 IN HII_THUNK_CONTEXT *IfrThunkContext
229 )
230 {
231 EFI_STATUS Status;
232 LIST_ENTRY *Link;
233 EFI_HII_PACKAGE_LIST_HEADER *StringPackageListHeader;
234 UINTN Size;
235 HII_THUNK_CONTEXT *ThunkContext;
236
237
238 Link = GetFirstNode (&Private->ThunkContextListHead);
239
240 while (!IsNull (&Private->ThunkContextListHead, Link)) {
241
242 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
243
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);
248
249 IfrThunkContext->StringPackageCount = GetPackageCountByType (StringPackageListHeader, EFI_HII_PACKAGE_STRINGS);
250 //
251 // Add Function to only get only String Packages from the Package List
252 //
253 Status = mHiiDatabase->UpdatePackageList (
254 mHiiDatabase,
255 IfrThunkContext->UefiHiiHandle,
256 StringPackageListHeader
257 );
258 ASSERT_EFI_ERROR (Status);
259
260 FreePool (StringPackageListHeader);
261 return EFI_SUCCESS;
262 }
263 }
264
265 Link = GetNextNode (&Private->ThunkContextListHead, Link);
266 }
267
268 return EFI_NOT_FOUND;
269
270 }
271
272
273
274 CONST EFI_GUID mAGuid =
275 { 0x14f95e01, 0xd562, 0x432e, { 0x84, 0x4a, 0x95, 0xa4, 0x39, 0x5, 0x10, 0x7e } };
276
277 EFI_STATUS
278 UefiRegisterPackageList(
279 IN HII_THUNK_PRIVATE_DATA *Private,
280 IN EFI_HII_PACKAGES *Packages,
281 OUT FRAMEWORK_EFI_HII_HANDLE *Handle
282 )
283 {
284 EFI_STATUS Status;
285 UINTN StringPackageCount;
286 UINTN IfrPackageCount;
287 EFI_HII_PACKAGE_LIST_HEADER *PackageListHeader;
288 HII_THUNK_CONTEXT *ThunkContext;
289 EFI_GUID GuidId;
290
291 PackageListHeader = NULL;
292
293 Status = GetPackageCount (Packages, &IfrPackageCount, &StringPackageCount);
294 ASSERT_EFI_ERROR (Status);
295
296 if (IfrPackageCount > 1) {
297 //
298 // HII Thunk only handle package with 0 or 1 IFR package.
299 //
300 return EFI_UNSUPPORTED;
301 }
302
303 ThunkContext = CreateThunkContext (Private, StringPackageCount, IfrPackageCount);
304 if (ThunkContext == NULL) {
305 return EFI_OUT_OF_RESOURCES;
306 }
307 ThunkContext->ByFrameworkHiiNewPack = TRUE;
308
309 if (Packages->GuidId == NULL) {
310 //
311 // UEFI HII Database require Package List GUID must be unique.
312 //
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
316 // GUID.
317 //
318 ASSERT (StringPackageCount >=1 && IfrPackageCount == 1);
319 Packages->GuidId = &GuidId;
320 GenerateGuidId (&mAGuid, Packages->GuidId);
321 } else {
322 //BugBug We need fix this.
323 //ASSERT (StringPackageCount == 0 || IfrPackageCount == 0);
324 CopyGuid (&GuidId, Packages->GuidId);
325 }
326
327 //
328 // Record the Package List GUID, it is used as a name for the package list by Framework HII.
329 //
330 CopyGuid (&ThunkContext->TagGuid, Packages->GuidId);
331
332 if ((StringPackageCount == 0) && (IfrPackageCount != 0)) {
333 //
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.
337 //
338 GenerateGuidId (Packages->GuidId, &GuidId);
339 }
340
341 //
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.
344 //
345 if (IfrPackageCount != 0) {
346 InstallDefaultConfigAccessProtocol (Packages, ThunkContext);
347 }
348 PackageListHeader = PrepareUefiPackageListFromFrameworkHiiPackages (Packages, &GuidId);
349 Status = mHiiDatabase->NewPackageList (
350 mHiiDatabase,
351 PackageListHeader,
352 ThunkContext->UefiHiiDriverHandle,
353 &ThunkContext->UefiHiiHandle
354 );
355
356 //
357 // BUGBUG: Remove when development is done
358 //
359 ASSERT_EFI_ERROR (Status);
360 if (EFI_ERROR (Status)) {
361 goto Done;
362 }
363
364 if (IfrPackageCount == 0) {
365 if (StringPackageCount != 0) {
366 //
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.
369 //
370 Status = FindPackListWithOnlyIfrPackAndAddStringPack (
371 Private,
372 ThunkContext,
373 PackageListHeader
374 );
375
376 if (Status == EFI_NOT_FOUND) {
377 Status = EFI_SUCCESS;
378 }
379 }
380 } else {
381 CreateQuestionIdMap (ThunkContext);
382
383 if (StringPackageCount == 0) {
384 //
385 // Register the Package List to UEFI HII first.
386 //
387 Status = FindStringPackAndAddToPackListWithOnlyIfrPack (
388 Private,
389 ThunkContext
390 );
391 ASSERT_EFI_ERROR (Status);
392 }
393 }
394
395 Done:
396 if (EFI_ERROR (Status)) {
397 DestroyThunkContext (ThunkContext);
398 } else {
399 InsertTailList (&Private->ThunkContextListHead, &ThunkContext->Link);
400 *Handle = ThunkContext->FwHiiHandle;
401 }
402
403 SafeFreePool (PackageListHeader);
404
405 return Status;
406 }
407
408
409 EFI_STATUS
410 EFIAPI
411 HiiNewPack (
412 IN EFI_HII_PROTOCOL *This,
413 IN EFI_HII_PACKAGES *Packages,
414 OUT FRAMEWORK_EFI_HII_HANDLE *Handle
415 )
416 /*++
417
418 Routine Description:
419
420 Extracts the various packs from a package list.
421
422 Arguments:
423
424 This - Pointer of HII protocol.
425 Packages - Pointer of HII packages.
426 Handle - Handle value to be returned.
427
428 Returns:
429
430 EFI_SUCCESS - Pacakges has added to HII database successfully.
431 EFI_INVALID_PARAMETER - Invalid parameter.
432
433 --*/
434 {
435 EFI_STATUS Status;
436 HII_THUNK_PRIVATE_DATA *Private;
437 EFI_TPL OldTpl;
438
439 if (Handle == NULL) {
440 return EFI_INVALID_PARAMETER;
441 }
442
443 if (Packages == NULL) {
444 return EFI_INVALID_PARAMETER;
445 }
446
447 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
448
449 //
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.
454 //
455 mInFrameworkHiiNewPack = TRUE;
456
457 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
458
459 Status = UefiRegisterPackageList (
460 Private,
461 Packages,
462 Handle
463 );
464
465 mInFrameworkHiiNewPack = FALSE;
466
467 gBS->RestoreTPL (OldTpl);
468
469 return Status;
470 }
471
472 EFI_STATUS
473 EFIAPI
474 HiiRemovePack (
475 IN EFI_HII_PROTOCOL *This,
476 IN FRAMEWORK_EFI_HII_HANDLE Handle
477 )
478 /*++
479
480 Routine Description:
481 Removes the various packs from a Handle
482
483 Arguments:
484
485 Returns:
486
487 --*/
488 {
489 EFI_STATUS Status;
490 HII_THUNK_PRIVATE_DATA *Private;
491 HII_THUNK_CONTEXT *ThunkContext;
492 EFI_TPL OldTpl;
493
494 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
495
496 mInFrameworkHiiRemovePack = TRUE;
497
498 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
499
500 ThunkContext = FwHiiHandleToThunkContext (Private, Handle);
501
502 if (ThunkContext != NULL) {
503 Status = mHiiDatabase->RemovePackageList (
504 mHiiDatabase,
505 ThunkContext->UefiHiiHandle
506 );
507 ASSERT_EFI_ERROR (Status);
508
509 if (ThunkContext->IfrPackageCount != 0) {
510 UninstallDefaultConfigAccessProtocol (ThunkContext);
511 }
512
513 RemoveEntryList (&ThunkContext->Link);
514
515 DestroyThunkContext (ThunkContext);
516 }else {
517 Status = EFI_NOT_FOUND;
518 }
519
520 mInFrameworkHiiRemovePack = FALSE;
521
522 gBS->RestoreTPL (OldTpl);
523
524 return Status;
525 }
526
527 EFI_STATUS
528 EFIAPI
529 NewOrAddPackNotify (
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
535 )
536 {
537 EFI_STATUS Status;
538 HII_THUNK_PRIVATE_DATA *Private;
539 HII_THUNK_CONTEXT *ThunkContext;
540
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);
543
544 Status = EFI_SUCCESS;
545 Private = mHiiThunkPrivateData;
546
547 if (mInFrameworkHiiNewPack || mInFrameworkUpdatePakcage) {
548 return EFI_SUCCESS;
549 }
550
551 //
552 // We only create a ThunkContext if the Uefi Hii Handle is only already registered
553 // by the HII Thunk Layer.
554 //
555 ThunkContext = UefiHiiHandleToThunkContext (Private, Handle);
556 if (ThunkContext == NULL) {
557 ThunkContext = CreateThunkContextForUefiHiiHandle (Handle);
558 ASSERT (ThunkContext != NULL);
559
560 InsertTailList (&Private->ThunkContextListHead, &ThunkContext->Link);
561 }
562
563
564 if (PackageType == EFI_HII_PACKAGE_FORM) {
565 Status = CreateQuestionIdMap (ThunkContext);
566 }
567
568 return Status;
569 }
570
571
572 BOOLEAN
573 IsRemovingLastStringPack (
574 IN EFI_HII_HANDLE Handle
575 )
576 {
577 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
578 UINTN BufferSize;
579 EFI_STATUS Status;
580 EFI_HII_PACKAGE_HEADER *PackageHeader;
581 UINTN StringPackageCount;
582
583 HiiPackageList = NULL;
584 BufferSize = 0;
585 StringPackageCount = 0;
586
587 Status = HiiLibExportPackageLists (Handle, &HiiPackageList, &BufferSize);
588 ASSERT_EFI_ERROR (Status);
589
590 PackageHeader = (EFI_HII_PACKAGE_HEADER *) ((UINT8 *) HiiPackageList + sizeof (EFI_HII_PACKAGE_LIST_HEADER));
591
592 while (PackageHeader->Type != EFI_HII_PACKAGE_END) {
593 switch (PackageHeader->Type) {
594 case EFI_HII_PACKAGE_STRINGS:
595 StringPackageCount++;
596
597 if (StringPackageCount > 1) {
598 //
599 // More than one String Pack in the package list
600 //
601 FreePool (HiiPackageList);
602 return FALSE;
603 }
604 break;
605
606 default:
607 break;
608 }
609 //
610 // goto header of next package
611 //
612 PackageHeader = (EFI_HII_PACKAGE_HEADER *) ((UINT8 *) PackageHeader + PackageHeader->Length);
613 }
614
615
616 //
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.
619 //
620 ASSERT (StringPackageCount == 1);
621
622 FreePool (HiiPackageList);
623 return TRUE;
624 }
625
626
627
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
630 // this GUID
631
632 EFI_STATUS
633 EFIAPI
634 RemovePackNotify (
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
640 )
641 {
642 EFI_STATUS Status;
643 HII_THUNK_PRIVATE_DATA *Private;
644 HII_THUNK_CONTEXT * ThunkContext;
645
646 Status = EFI_SUCCESS;
647
648 ASSERT (PackageType == EFI_HII_PACKAGE_STRINGS);
649 ASSERT (NotifyType == EFI_HII_DATABASE_NOTIFY_REMOVE_PACK);
650
651 if (mInFrameworkHiiRemovePack || mInFrameworkUpdatePakcage) {
652 return EFI_SUCCESS;
653 }
654
655 Private = mHiiThunkPrivateData;
656
657 ThunkContext = UefiHiiHandleToThunkContext (Private, Handle);
658
659 if (!ThunkContext->ByFrameworkHiiNewPack) {
660 if (IsRemovingLastStringPack (Handle)) {
661 //
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
664 // database.
665 //
666 DestroyThunkContextForUefiHiiHandle (Private, Handle);
667 }
668 }
669
670 return Status;
671 }
672
673
674