]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiToUefiHiiThunk/Utility.c
1) StrGather (Build.exe) in compatible mode add a Framework Package Header to replace...
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FrameworkHiiToUefiHiiThunk / Utility.c
1 /**@file
2
3 This file contains the keyboard processing code to the HII database.
4
5 Copyright (c) 2006 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16
17 #include "HiiDatabase.h"
18 #include "HiiHandle.h"
19 #include <Library/DebugLib.h>
20
21 CONST EFI_GUID gZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
22
23 /**
24 Find the corressponding UEFI HII Handle from a Framework HII Handle given.
25
26 @param Private The HII Thunk Module Private context.
27 @param FwHiiHandle The Framemwork HII Handle.
28
29 @return NULL If Framework HII Handle is invalid.
30 @return The corresponding UEFI HII Handle.
31 **/
32 EFI_HII_HANDLE
33 FwHiiHandleToUefiHiiHandle (
34 IN CONST HII_THUNK_PRIVATE_DATA *Private,
35 IN FRAMEWORK_EFI_HII_HANDLE FwHiiHandle
36 )
37 {
38 HII_THUNK_CONTEXT *ThunkContext;
39
40 ASSERT (FwHiiHandle != (FRAMEWORK_EFI_HII_HANDLE) 0);
41 ASSERT (Private != NULL);
42
43 ThunkContext = FwHiiHandleToThunkContext (Private, FwHiiHandle);
44
45 if (ThunkContext != NULL) {
46 return ThunkContext->UefiHiiHandle;
47 }
48
49 return (EFI_HII_HANDLE) NULL;
50 }
51
52
53 /**
54 Find the corressponding HII Thunk Context from a Framework HII Handle given.
55
56 @param Private The HII Thunk Module Private context.
57 @param FwHiiHandle The Framemwork HII Handle.
58
59 @return NULL If Framework HII Handle is invalid.
60 @return The corresponding HII Thunk Context.
61 **/
62 HII_THUNK_CONTEXT *
63 FwHiiHandleToThunkContext (
64 IN CONST HII_THUNK_PRIVATE_DATA *Private,
65 IN FRAMEWORK_EFI_HII_HANDLE FwHiiHandle
66 )
67 {
68 LIST_ENTRY *Link;
69 HII_THUNK_CONTEXT *ThunkContext;
70
71
72 Link = GetFirstNode (&Private->ThunkContextListHead);
73
74 while (!IsNull (&Private->ThunkContextListHead, Link)) {
75 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
76
77 if (FwHiiHandle == ThunkContext->FwHiiHandle) {
78 return ThunkContext;
79 }
80
81 Link = GetNextNode (&Private->ThunkContextListHead, Link);
82 }
83
84 return NULL;
85 }
86
87 /**
88 Find the corressponding HII Thunk Context from a UEFI HII Handle given.
89
90 @param Private The HII Thunk Module Private context.
91 @param UEFIHiiHandle The UEFI HII Handle.
92
93 @return NULL If UEFI HII Handle is invalid.
94 @return The corresponding HII Thunk Context.
95 **/
96 HII_THUNK_CONTEXT *
97 UefiHiiHandleToThunkContext (
98 IN CONST HII_THUNK_PRIVATE_DATA *Private,
99 IN EFI_HII_HANDLE UefiHiiHandle
100 )
101 {
102 LIST_ENTRY *Link;
103 HII_THUNK_CONTEXT *ThunkContext;
104
105 Link = GetFirstNode (&Private->ThunkContextListHead);
106
107 while (!IsNull (&Private->ThunkContextListHead, Link)) {
108 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
109
110 if (UefiHiiHandle == ThunkContext->UefiHiiHandle) {
111 return ThunkContext;
112 }
113 Link = GetNextNode (&Private->ThunkContextListHead, Link);
114 }
115
116 return NULL;
117 }
118
119 /**
120 Find the corressponding HII Thunk Context from a Tag GUID.
121
122 @param Private The HII Thunk Module Private context.
123 @param Guid The Tag GUID.
124
125 @return NULL No HII Thunk Context matched the Tag GUID.
126 @return The corresponding HII Thunk Context.
127 **/
128 HII_THUNK_CONTEXT *
129 TagGuidToIfrPackThunkContext (
130 IN CONST HII_THUNK_PRIVATE_DATA *Private,
131 IN CONST EFI_GUID *Guid
132 )
133 {
134 LIST_ENTRY *Link;
135 HII_THUNK_CONTEXT *ThunkContext;
136
137 Link = GetFirstNode (&Private->ThunkContextListHead);
138
139 while (!IsNull (&Private->ThunkContextListHead, Link)) {
140 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
141
142 if (CompareGuid (Guid, &ThunkContext->TagGuid) && (ThunkContext->IfrPackageCount != 0)) {
143 return ThunkContext;
144 }
145
146 Link = GetNextNode (&Private->ThunkContextListHead, Link);
147 }
148
149 return NULL;
150
151 }
152
153 /**
154 Clean up the HII Thunk Context for a UEFI HII Handle.
155
156 @param Private The HII Thunk Module Private context.
157 @param UEFIHiiHandle The UEFI HII Handle.
158
159 **/
160 VOID
161 DestroyThunkContextForUefiHiiHandle (
162 IN HII_THUNK_PRIVATE_DATA *Private,
163 IN EFI_HII_HANDLE UefiHiiHandle
164 )
165 {
166 HII_THUNK_CONTEXT *ThunkContext;
167
168 ThunkContext = UefiHiiHandleToThunkContext (Private, UefiHiiHandle);
169 ASSERT (ThunkContext != NULL);
170
171 DestroyThunkContext (ThunkContext);
172 }
173
174
175 /**
176 This function create a HII_THUNK_CONTEXT for a package list registered
177 by a module calling EFI_HII_DATABASE_PROTOCOL.NewPackageList. It records
178 the PackageListGuid in EFI_HII_PACKAGE_LIST_HEADER in the TagGuid in
179 HII_THUNK_CONTEXT created. This TagGuid will be used as a key to s
180
181 **/
182 HII_THUNK_CONTEXT *
183 CreateThunkContextForUefiHiiHandle (
184 IN EFI_HII_HANDLE UefiHiiHandle
185 )
186 {
187 EFI_STATUS Status;
188 EFI_GUID PackageGuid;
189 HII_THUNK_CONTEXT *ThunkContext;
190
191 ThunkContext = AllocateZeroPool (sizeof (*ThunkContext));
192 ASSERT (ThunkContext != NULL);
193
194 ThunkContext->Signature = HII_THUNK_CONTEXT_SIGNATURE;
195
196 Status = AllocateHiiHandle (&ThunkContext->FwHiiHandle);
197 if (EFI_ERROR (Status)) {
198 return NULL;
199 }
200
201 ThunkContext->UefiHiiHandle = UefiHiiHandle;
202
203 Status = HiiLibExtractGuidFromHiiHandle (UefiHiiHandle, &PackageGuid);
204 ASSERT_EFI_ERROR (Status);
205
206 CopyGuid(&ThunkContext->TagGuid, &PackageGuid);
207
208 return ThunkContext;
209 }
210
211
212 /**
213 Get the number of HII Package for a Package type.
214
215 @param PackageListHeader The Package List.
216 @param PackageType The Package Type.
217
218 @return The number of Package for given type.
219 **/
220 UINTN
221 GetPackageCountByType (
222 IN CONST EFI_HII_PACKAGE_LIST_HEADER *PackageListHeader,
223 IN UINT8 PackageType
224 )
225 {
226 UINTN Count;
227 EFI_HII_PACKAGE_HEADER *PackageHeader;
228
229 PackageHeader = (EFI_HII_PACKAGE_HEADER *) ((UINT8 *) PackageListHeader + sizeof (EFI_HII_PACKAGE_LIST_HEADER));
230 Count = 0;
231
232 while (PackageHeader->Type != EFI_HII_PACKAGE_END) {
233 if (PackageHeader->Type == PackageType ) {
234 Count++;
235 }
236 PackageHeader = (EFI_HII_PACKAGE_HEADER *) ((UINT8 *) PackageHeader + PackageHeader->Length);
237 }
238
239
240 return Count;
241 }
242
243 /**
244 Get the Form Package from a Framework Package List.
245
246 @param Packages Framework Package List.
247
248 @return The Form Package Header found.
249 **/
250 EFI_HII_PACKAGE_HEADER *
251 GetIfrPackage (
252 IN CONST EFI_HII_PACKAGES *Packages
253 )
254 {
255 UINTN Index;
256 TIANO_AUTOGEN_PACKAGES_HEADER **TianoAutogenPackageHdrArray;
257
258 ASSERT (Packages != NULL);
259
260 TianoAutogenPackageHdrArray = (TIANO_AUTOGEN_PACKAGES_HEADER **) (((UINT8 *) &Packages->GuidId) + sizeof (Packages->GuidId));
261
262 for (Index = 0; Index < Packages->NumberOfPackages; Index++) {
263 //
264 // The current UEFI HII build tool generate a binary in the format defined by
265 // TIANO_AUTOGEN_PACKAGES_HEADER. We assume that all packages generated in
266 // this binary is with same package type. So the returned IfrPackageCount and StringPackageCount
267 // may not be the exact number of valid package number in the binary generated
268 // by HII Build tool.
269 //
270 switch (TianoAutogenPackageHdrArray[Index]->FrameworkPackageHeader.Type) {
271 case EFI_HII_IFR:
272 return &TianoAutogenPackageHdrArray[Index]->PackageHeader;
273 break;
274 case EFI_HII_STRING:
275 case EFI_HII_FONT:
276 break;
277
278 default:
279 ASSERT (FALSE);
280 return NULL;
281 break;
282 }
283 }
284
285 return NULL;
286 }
287
288 /**
289 Get FormSet GUID.
290
291 ASSERT if no FormSet Opcode is found.
292
293 @param Packages Form Framework Package.
294 @param FormSetGuid Return the FormSet Guid.
295
296 **/
297 VOID
298 GetFormSetGuid (
299 IN EFI_HII_PACKAGE_HEADER *Package,
300 OUT EFI_GUID *FormSetGuid
301 )
302 {
303 UINTN Offset;
304 EFI_IFR_OP_HEADER *OpCode;
305 EFI_IFR_FORM_SET *FormSet;
306
307 Offset = sizeof (EFI_HII_PACKAGE_HEADER);
308 while (Offset < Package->Length) {
309 OpCode = (EFI_IFR_OP_HEADER *)((UINT8 *) Package + Offset);
310
311 switch (OpCode->OpCode) {
312 case EFI_IFR_FORM_SET_OP:
313 FormSet = (EFI_IFR_FORM_SET *) OpCode;
314 CopyGuid (FormSetGuid, (EFI_GUID *)(VOID *)&FormSet->Guid);
315 return;
316
317 default:
318 break;
319
320 }
321 Offset += OpCode->Length;
322 }
323
324 //
325 // A proper IFR must have a formset opcode.
326 //
327 ASSERT (FALSE);
328
329 }
330
331 /**
332 Creat a Thunk Context.
333
334 ASSERT if no FormSet Opcode is found.
335
336 @param Private The HII Thunk Private Context.
337 @param StringPackageCount The String package count.
338 @param FormSetGuid The IFR Package count.
339
340 @return A newly created Thunk Context.
341 @retval NULL No resource to create a new Thunk Context.
342 **/
343 HII_THUNK_CONTEXT *
344 CreateThunkContext (
345 IN HII_THUNK_PRIVATE_DATA *Private,
346 IN UINTN StringPackageCount,
347 IN UINTN IfrPackageCount
348 )
349 {
350 EFI_STATUS Status;
351 HII_THUNK_CONTEXT *ThunkContext;
352
353 ThunkContext = AllocateZeroPool (sizeof (HII_THUNK_CONTEXT));
354 ASSERT (ThunkContext != NULL);
355
356 ThunkContext->Signature = HII_THUNK_CONTEXT_SIGNATURE;
357 ThunkContext->IfrPackageCount = IfrPackageCount;
358 ThunkContext->StringPackageCount = StringPackageCount;
359 Status = AllocateHiiHandle (&ThunkContext->FwHiiHandle);
360 if (EFI_ERROR (Status)) {
361 return NULL;
362 }
363
364 return ThunkContext;
365
366 }
367
368 /**
369 Destroy the Thunk Context and free up all resource.
370
371 @param ThunkContext The HII Thunk Private Context to be freed.
372
373 **/
374 VOID
375 DestroyThunkContext (
376 IN HII_THUNK_CONTEXT *ThunkContext
377 )
378 {
379 ASSERT (ThunkContext != NULL);
380
381 FreeHiiHandle (ThunkContext->FwHiiHandle);
382
383 RemoveEntryList (&ThunkContext->Link);
384
385 if (ThunkContext->FormSet != NULL) {
386 DestroyFormSet (ThunkContext->FormSet);
387 }
388
389 FreePool (ThunkContext);
390 }
391
392 /**
393 Get the FormSet's Default Varstore ID based on the rule (Descending Priority):
394
395 1) Var Store ID of FRAMEWORK_RESERVED_VARSTORE_ID (0x01).
396 2) First Var Store ID.
397
398 @param FormSet The Form Set.
399
400 **/
401 VOID
402 GetFormsetDefaultVarstoreId (
403 IN OUT FORM_BROWSER_FORMSET * FormSet
404 )
405 {
406 LIST_ENTRY *StorageList;
407 FORMSET_STORAGE *Storage;
408
409 //
410 // VarStoreId 0 is invalid in UEFI IFR.
411 //
412 FormSet->DefaultVarStoreId = 0;
413 StorageList = GetFirstNode (&FormSet->StorageListHead);
414
415 while (!IsNull (&FormSet->StorageListHead, StorageList)) {
416 Storage = FORMSET_STORAGE_FROM_LINK (StorageList);
417
418 DEBUG ((EFI_D_INFO, "FormSet %g: Found Varstore ID %x\n", &FormSet->Guid, Storage->VarStoreId));
419
420 if (Storage->VarStoreId == FRAMEWORK_RESERVED_VARSTORE_ID) {
421 FormSet->DefaultVarStoreId = FRAMEWORK_RESERVED_VARSTORE_ID;
422 break;
423 }
424
425 StorageList = GetNextNode (&FormSet->StorageListHead, StorageList);
426 }
427
428 if (FormSet->DefaultVarStoreId != FRAMEWORK_RESERVED_VARSTORE_ID) {
429 StorageList = GetFirstNode (&FormSet->StorageListHead);
430 if (!IsNull (&FormSet->StorageListHead, StorageList)) {
431 Storage = FORMSET_STORAGE_FROM_LINK (StorageList);
432 FormSet->DefaultVarStoreId = Storage->VarStoreId;
433 }
434
435 }
436
437 DEBUG_CODE_BEGIN ();
438 if (FormSet->DefaultVarStoreId == 0) {
439 DEBUG ((EFI_D_INFO, "FormSet %g: No Varstore Found\n", &FormSet->Guid));
440 } else {
441 DEBUG ((EFI_D_INFO, "FormSet %g: Default Varstore ID is %x\n", &FormSet->Guid, FormSet->DefaultVarStoreId));
442 }
443 DEBUG_CODE_END ();
444
445 return;
446 }
447
448 /**
449 Fetch the Ifr binary data of a FormSet.
450
451 @param Handle PackageList Handle
452 @param FormSetGuid GUID of a formset. If not specified (NULL or zero
453 GUID), take the first FormSet found in package
454 list.
455 @param BinaryLength The length of the FormSet IFR binary.
456 @param BinaryData The buffer designed to receive the FormSet.
457
458 @retval EFI_SUCCESS Buffer filled with the requested FormSet.
459 BufferLength was updated.
460 @retval EFI_INVALID_PARAMETER The handle is unknown.
461 @retval EFI_NOT_FOUND A form or FormSet on the requested handle cannot
462 be found with the requested FormId.
463
464 **/
465 EFI_STATUS
466 GetIfrBinaryData (
467 IN EFI_HII_HANDLE Handle,
468 IN OUT EFI_GUID *FormSetGuid,
469 OUT UINTN *BinaryLength,
470 OUT UINT8 **BinaryData
471 )
472 {
473 EFI_STATUS Status;
474 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
475 UINTN BufferSize;
476 UINT8 *Package;
477 UINT8 *OpCodeData;
478 UINT32 Offset;
479 UINT32 Offset2;
480 BOOLEAN ReturnDefault;
481 UINT32 PackageListLength;
482 EFI_HII_PACKAGE_HEADER PackageHeader;
483
484 OpCodeData = NULL;
485 Package = NULL;
486 ZeroMem (&PackageHeader, sizeof (EFI_HII_PACKAGE_HEADER));;
487
488 //
489 // if FormSetGuid is NULL or zero GUID, return first FormSet in the package list
490 //
491 if (FormSetGuid == NULL || CompareGuid (FormSetGuid, &gZeroGuid)) {
492 ReturnDefault = TRUE;
493 } else {
494 ReturnDefault = FALSE;
495 }
496
497 //
498 // Get HII PackageList
499 //
500 BufferSize = 0;
501 HiiPackageList = NULL;
502 Status = mHiiDatabase->ExportPackageLists (mHiiDatabase, Handle, &BufferSize, HiiPackageList);
503 if (Status == EFI_BUFFER_TOO_SMALL) {
504 HiiPackageList = AllocatePool (BufferSize);
505 ASSERT (HiiPackageList != NULL);
506
507 Status = mHiiDatabase->ExportPackageLists (mHiiDatabase, Handle, &BufferSize, HiiPackageList);
508 }
509 if (EFI_ERROR (Status)) {
510 return Status;
511 }
512
513 //
514 // Get Form package from this HII package List
515 //
516 Offset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
517 Offset2 = 0;
518 CopyMem (&PackageListLength, &HiiPackageList->PackageLength, sizeof (UINT32));
519
520 while (Offset < PackageListLength) {
521 Package = ((UINT8 *) HiiPackageList) + Offset;
522 CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));
523
524 if (PackageHeader.Type == EFI_HII_PACKAGE_FORMS) {
525 //
526 // Search FormSet in this Form Package
527 //
528 Offset2 = sizeof (EFI_HII_PACKAGE_HEADER);
529 while (Offset2 < PackageHeader.Length) {
530 OpCodeData = Package + Offset2;
531
532 if (((EFI_IFR_OP_HEADER *) OpCodeData)->OpCode == EFI_IFR_FORM_SET_OP) {
533 //
534 // Check whether return default FormSet
535 //
536 if (ReturnDefault) {
537 break;
538 }
539
540 //
541 // FormSet GUID is specified, check it
542 //
543 if (CompareGuid (FormSetGuid, (EFI_GUID *)(OpCodeData + sizeof (EFI_IFR_OP_HEADER)))) {
544 break;
545 }
546 }
547
548 Offset2 += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length;
549 }
550
551 if (Offset2 < PackageHeader.Length) {
552 //
553 // Target formset found
554 //
555 break;
556 }
557 }
558
559 Offset += PackageHeader.Length;
560 }
561
562 if (Offset >= PackageListLength) {
563 //
564 // Form package not found in this Package List
565 //
566 gBS->FreePool (HiiPackageList);
567 return EFI_NOT_FOUND;
568 }
569
570 if (ReturnDefault && FormSetGuid != NULL) {
571 //
572 // Return the default FormSet GUID
573 //
574 CopyMem (FormSetGuid, &((EFI_IFR_FORM_SET *) OpCodeData)->Guid, sizeof (EFI_GUID));
575 }
576
577 //
578 // To determine the length of a whole FormSet IFR binary, one have to parse all the Opcodes
579 // in this FormSet; So, here just simply copy the data from start of a FormSet to the end
580 // of the Form Package.
581 //
582 *BinaryLength = PackageHeader.Length - Offset2;
583 *BinaryData = AllocateCopyPool (*BinaryLength, OpCodeData);
584
585 gBS->FreePool (HiiPackageList);
586
587 if (*BinaryData == NULL) {
588 return EFI_OUT_OF_RESOURCES;
589 }
590
591 return EFI_SUCCESS;
592 }
593
594 /**
595 Initialize the internal data structure of a FormSet.
596
597 @param Handle PackageList Handle
598 @param FormSetGuid GUID of a formset. If not specified (NULL or zero
599 GUID), take the first FormSet found in package
600 list.
601 @param FormSet FormSet data structure.
602
603 @retval EFI_SUCCESS The function completed successfully.
604 @retval EFI_NOT_FOUND The specified FormSet could not be found.
605
606 **/
607 EFI_STATUS
608 InitializeFormSet (
609 IN EFI_HII_HANDLE Handle,
610 IN OUT EFI_GUID *FormSetGuid,
611 OUT FORM_BROWSER_FORMSET *FormSet
612 )
613 {
614 EFI_STATUS Status;
615
616 Status = GetIfrBinaryData (Handle, FormSetGuid, &FormSet->IfrBinaryLength, &FormSet->IfrBinaryData);
617 if (EFI_ERROR (Status)) {
618 return Status;
619 }
620
621 FormSet->HiiHandle = Handle;
622 CopyMem (&FormSet->Guid, FormSetGuid, sizeof (EFI_GUID));
623
624 //
625 // Parse the IFR binary OpCodes
626 //
627 Status = ParseOpCodes (FormSet);
628 if (EFI_ERROR (Status)) {
629 return Status;
630 }
631
632 GetFormsetDefaultVarstoreId (FormSet);
633 return Status;
634 }
635
636 /**
637 Parse the Form Package and build a FORM_BROWSER_FORMSET structure.
638
639 @param UefiHiiHandle PackageList Handle
640
641 @return A pointer to FORM_BROWSER_FORMSET.
642
643 **/
644 FORM_BROWSER_FORMSET *
645 ParseFormSet (
646 IN EFI_HII_HANDLE UefiHiiHandle
647 )
648 {
649 FORM_BROWSER_FORMSET *FormSet;
650 EFI_GUID FormSetGuid;
651 EFI_STATUS Status;
652
653 FormSet = AllocateZeroPool (sizeof (FORM_BROWSER_FORMSET));
654 ASSERT (FormSet != NULL);
655
656 CopyGuid (&FormSetGuid, &gZeroGuid);
657 Status = InitializeFormSet (UefiHiiHandle, &FormSetGuid, FormSet);
658 ASSERT_EFI_ERROR (Status);
659
660 return FormSet;
661 }
662