]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiOnUefiHiiThunk/HiiDatabase.c
Refine language conversion in ECP. Create a new library LanguageLib providing functio...
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FrameworkHiiOnUefiHiiThunk / HiiDatabase.c
1 /**@file
2 Framework to UEFI 2.1 HII Thunk. The driver consume UEFI HII protocols
3 to produce a Framework HII protocol.
4
5 Copyright (c) 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 #include "HiiDatabase.h"
17 #include "HiiHandle.h"
18
19 HII_THUNK_PRIVATE_DATA *mHiiThunkPrivateData;
20
21 HII_THUNK_PRIVATE_DATA mHiiThunkPrivateDataTempate = {
22 HII_THUNK_PRIVATE_DATA_SIGNATURE,
23 (EFI_HANDLE) NULL,
24 {
25 HiiNewPack,
26 HiiRemovePack,
27 HiiFindHandles,
28 HiiExportDatabase,
29
30 HiiTestString,
31 HiiGetGlyph,
32 HiiGlyphToBlt,
33
34 HiiNewString,
35 HiiGetPrimaryLanguages,
36 HiiGetSecondaryLanguages,
37 HiiThunkGetString,
38 HiiResetStrings,
39 HiiGetLine,
40 HiiGetForms,
41 HiiGetDefaultImage,
42 HiiThunkUpdateForm,
43
44 HiiGetKeyboardLayout
45 },
46
47 {
48 ///
49 /// HiiHandleLinkList
50 ///
51 NULL, NULL
52 },
53 };
54
55 EFI_FORMBROWSER_THUNK_PRIVATE_DATA mBrowserThunkPrivateDataTemplate = {
56 EFI_FORMBROWSER_THUNK_PRIVATE_DATA_SIGNATURE,
57 (EFI_HANDLE) NULL,
58 (HII_THUNK_PRIVATE_DATA *) NULL,
59 {
60 ThunkSendForm,
61 ThunkCreatePopUp
62 }
63 };
64
65
66 CONST EFI_HII_DATABASE_PROTOCOL *mHiiDatabase;
67 CONST EFI_HII_IMAGE_PROTOCOL *mHiiImageProtocol;
68 CONST EFI_HII_STRING_PROTOCOL *mHiiStringProtocol;
69 CONST EFI_HII_FONT_PROTOCOL *mHiiFontProtocol;
70 CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *mHiiConfigRoutingProtocol;
71 CONST EFI_FORM_BROWSER2_PROTOCOL *mFormBrowser2Protocol;
72
73
74 /**
75 This routine initializes the HII Database.
76
77 @param ImageHandle Image handle for PCD DXE driver.
78 @param SystemTable Pointer to SystemTable.
79
80 @retval EFI_SUCCESS The entry point alwasy return successfully.
81 **/
82 EFI_STATUS
83 EFIAPI
84 InitializeHiiDatabase (
85 IN EFI_HANDLE ImageHandle,
86 IN EFI_SYSTEM_TABLE *SystemTable
87 )
88 {
89 HII_THUNK_PRIVATE_DATA *Private;
90 EFI_HANDLE Handle;
91 EFI_STATUS Status;
92 UINTN BufferLength;
93 EFI_HII_HANDLE *Buffer;
94 UINTN Index;
95 HII_THUNK_CONTEXT *ThunkContext;
96
97
98 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiHiiCompatibilityProtocolGuid);
99 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiFormBrowserCompatibilityProtocolGuid);
100
101 Private = AllocateCopyPool (sizeof (HII_THUNK_PRIVATE_DATA), &mHiiThunkPrivateDataTempate);
102 ASSERT (Private != NULL);
103 InitializeListHead (&Private->ThunkContextListHead);
104
105 InitHiiHandleDatabase ();
106
107 mHiiThunkPrivateData = Private;
108
109 Status = gBS->LocateProtocol (
110 &gEfiHiiDatabaseProtocolGuid,
111 NULL,
112 (VOID **) &mHiiDatabase
113 );
114 ASSERT_EFI_ERROR (Status);
115
116 Status = gBS->LocateProtocol (
117 &gEfiHiiStringProtocolGuid,
118 NULL,
119 (VOID **) &mHiiStringProtocol
120 );
121 ASSERT_EFI_ERROR (Status);
122
123 Status = gBS->LocateProtocol (
124 &gEfiHiiFontProtocolGuid,
125 NULL,
126 (VOID **) &mHiiFontProtocol
127 );
128 ASSERT_EFI_ERROR (Status);
129
130 Status = gBS->LocateProtocol (
131 &gEfiHiiConfigRoutingProtocolGuid,
132 NULL,
133 (VOID **) &mHiiConfigRoutingProtocol
134 );
135 ASSERT_EFI_ERROR (Status);
136
137
138 Status = gBS->LocateProtocol (
139 &gEfiFormBrowser2ProtocolGuid,
140 NULL,
141 (VOID **) &mFormBrowser2Protocol
142 );
143 ASSERT_EFI_ERROR (Status);
144
145
146
147
148 //
149 // Install protocol interface
150 //
151 Status = gBS->InstallProtocolInterface (
152 &Private->Handle,
153 &gEfiHiiCompatibilityProtocolGuid,
154 EFI_NATIVE_INTERFACE,
155 (VOID *) &Private->Hii
156 );
157 ASSERT_EFI_ERROR (Status);
158
159 Status = ListPackageLists (EFI_HII_PACKAGE_STRINGS, NULL, &BufferLength, &Buffer);
160 if (Status == EFI_SUCCESS) {
161 for (Index = 0; Index < BufferLength / sizeof (EFI_HII_HANDLE); Index++) {
162 ThunkContext = CreateThunkContextForUefiHiiHandle (Buffer[Index]);
163 ASSERT (ThunkContext!= NULL);
164
165 InsertTailList (&Private->ThunkContextListHead, &ThunkContext->Link);
166 }
167
168 FreePool (Buffer);
169 }
170
171 Status = mHiiDatabase->RegisterPackageNotify (
172 mHiiDatabase,
173 EFI_HII_PACKAGE_STRINGS,
174 NULL,
175 NewOrAddPackNotify,
176 EFI_HII_DATABASE_NOTIFY_NEW_PACK,
177 &Handle
178 );
179 ASSERT_EFI_ERROR (Status);
180
181 Status = mHiiDatabase->RegisterPackageNotify (
182 mHiiDatabase,
183 EFI_HII_PACKAGE_STRINGS,
184 NULL,
185 NewOrAddPackNotify,
186 EFI_HII_DATABASE_NOTIFY_ADD_PACK,
187 &Handle
188 );
189 ASSERT_EFI_ERROR (Status);
190
191 Status = mHiiDatabase->RegisterPackageNotify (
192 mHiiDatabase,
193 EFI_HII_PACKAGE_FORMS,
194 NULL,
195 NewOrAddPackNotify,
196 EFI_HII_DATABASE_NOTIFY_NEW_PACK,
197 &Handle
198 );
199 ASSERT_EFI_ERROR (Status);
200
201 Status = mHiiDatabase->RegisterPackageNotify (
202 mHiiDatabase,
203 EFI_HII_PACKAGE_FORMS,
204 NULL,
205 NewOrAddPackNotify,
206 EFI_HII_DATABASE_NOTIFY_ADD_PACK,
207 &Handle
208 );
209 ASSERT_EFI_ERROR (Status);
210
211 Status = mHiiDatabase->RegisterPackageNotify (
212 mHiiDatabase,
213 EFI_HII_PACKAGE_STRINGS,
214 NULL,
215 RemovePackNotify,
216 EFI_HII_DATABASE_NOTIFY_REMOVE_PACK,
217 &Handle
218 );
219 ASSERT_EFI_ERROR (Status);
220
221 InitSetBrowserStrings ();
222
223 mBrowserThunkPrivateDataTemplate.ThunkPrivate = Private;
224 Status = gBS->InstallProtocolInterface (
225 &mBrowserThunkPrivateDataTemplate.Handle,
226 &gEfiFormBrowserCompatibilityProtocolGuid,
227 EFI_NATIVE_INTERFACE,
228 (VOID *) &mBrowserThunkPrivateDataTemplate.FormBrowser
229 );
230 ASSERT_EFI_ERROR (Status);
231
232 return Status;
233 }
234
235 /**
236 Determines the handles that are currently active in the database.
237
238 This function determines the handles that are currently active in the database.
239 For example, a program wishing to create a Setup-like configuration utility would use this call
240 to determine the handles that are available. It would then use calls defined in the forms section
241 below to extract forms and then interpret them.
242
243 @param This A pointer to the EFI_HII_PROTOCOL instance.
244 @param HandleBufferLength On input, a pointer to the length of the handle buffer.
245 On output, the length of the handle buffer that is required for the handles found.
246 @param Handle An array of EFI_HII_HANDLE instances returned.
247 Type EFI_HII_HANDLE is defined in EFI_HII_PROTOCOL.NewPack() in the Packages section.
248
249 @retval EFI_SUCCESS Handle was updated successfully.
250
251 @retval EFI_BUFFER_TOO_SMALL The HandleBufferLength parameter indicates that Handle is too small
252 to support the number of handles. HandleBufferLength is updated with a value that
253 will enable the data to fit.
254 **/
255 EFI_STATUS
256 EFIAPI
257 HiiFindHandles (
258 IN EFI_HII_PROTOCOL *This,
259 IN OUT UINT16 *HandleBufferLength,
260 OUT FRAMEWORK_EFI_HII_HANDLE Handle[1]
261 )
262 {
263 UINT16 Count;
264 LIST_ENTRY *Link;
265 HII_THUNK_CONTEXT *ThunkContext;
266 HII_THUNK_PRIVATE_DATA *Private;
267
268 if (HandleBufferLength == NULL) {
269 return EFI_INVALID_PARAMETER;
270 }
271
272 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
273
274 //
275 // Count the number of handles.
276 //
277 Count = 0;
278 Link = GetFirstNode (&Private->ThunkContextListHead);
279 while (!IsNull (&Private->ThunkContextListHead, Link)) {
280 Count++;
281 Link = GetNextNode (&Private->ThunkContextListHead, Link);
282 }
283
284 if (Count > *HandleBufferLength) {
285 *HandleBufferLength = (UINT16) (Count * sizeof (FRAMEWORK_EFI_HII_HANDLE));
286 return EFI_BUFFER_TOO_SMALL;
287 }
288
289 //
290 // Output the handles.
291 //
292 Count = 0;
293 Link = GetFirstNode (&Private->ThunkContextListHead);
294 while (!IsNull (&Private->ThunkContextListHead, Link)) {
295
296 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
297 Handle[Count] = ThunkContext->FwHiiHandle;
298
299 Count++;
300 Link = GetNextNode (&Private->ThunkContextListHead, Link);
301
302 }
303
304 *HandleBufferLength = (UINT16) (Count * sizeof (FRAMEWORK_EFI_HII_HANDLE));
305 return EFI_SUCCESS;
306 }
307
308 /**
309 Allows a program to determine the primary languages that are supported on a given handle.
310
311 This routine is intended to be used by drivers to query the interface database for supported languages.
312 This routine returns a string of concatenated 3-byte language identifiers, one per string package associated with the handle.
313
314 @param This A pointer to the EFI_HII_PROTOCOL instance.
315 @param Handle The handle on which the strings reside. Type EFI_HII_HANDLE is defined in EFI_HII_PROTOCOL.NewPack()
316 in the Packages section.
317 @param LanguageString A string allocated by GetPrimaryLanguages() that contains a list of all primary languages
318 registered on the handle. The routine will not return the three-spaces language identifier used in
319 other functions to indicate non-language-specific strings.
320
321 @reval EFI_SUCCESS LanguageString was correctly returned.
322
323 @reval EFI_INVALID_PARAMETER The Handle was unknown.
324 **/
325 EFI_STATUS
326 EFIAPI
327 HiiGetPrimaryLanguages (
328 IN EFI_HII_PROTOCOL *This,
329 IN FRAMEWORK_EFI_HII_HANDLE Handle,
330 OUT EFI_STRING *LanguageString
331 )
332 {
333 HII_THUNK_PRIVATE_DATA *Private;
334 EFI_HII_HANDLE UefiHiiHandle;
335 CHAR8 *LangCodes4646;
336 CHAR16 *UnicodeLangCodes639;
337 CHAR8 *LangCodes639;
338 EFI_STATUS Status;
339
340 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
341
342 UefiHiiHandle = FwHiiHandleToUefiHiiHandle (Private, Handle);
343 if (UefiHiiHandle == NULL) {
344 return EFI_INVALID_PARAMETER;
345 }
346
347 LangCodes4646 = HiiGetSupportedLanguages (UefiHiiHandle);
348
349 if (LangCodes4646 == NULL) {
350 return EFI_INVALID_PARAMETER;
351 }
352
353 LangCodes639 = ConvertLanguagesRfc4646ToIso639 (LangCodes4646);
354 if (LangCodes639 == NULL) {
355 Status = EFI_INVALID_PARAMETER;
356 goto Done;
357 }
358
359 UnicodeLangCodes639 = AllocateZeroPool (AsciiStrSize (LangCodes639) * sizeof (CHAR16));
360 if (UnicodeLangCodes639 == NULL) {
361 Status = EFI_OUT_OF_RESOURCES;
362 goto Done;
363 }
364
365 //
366 // The language returned is in RFC 639-2 format.
367 //
368 AsciiStrToUnicodeStr (LangCodes639, UnicodeLangCodes639);
369 *LanguageString = UnicodeLangCodes639;
370 Status = EFI_SUCCESS;
371
372 Done:
373 FreePool (LangCodes4646);
374 if (LangCodes639 != NULL) {
375 FreePool (LangCodes639);
376 }
377
378 return Status;
379 }
380
381 /**
382 This function returns the list of supported 2nd languages, in the format specified
383 in UEFI specification Appendix M.
384
385 If HiiHandle is not a valid Handle in the HII database, then ASSERT.
386 If not enough resource to complete the operation, then ASSERT.
387
388 @param HiiHandle The HII package list handle.
389 @param FirstLanguage Pointer to language name buffer.
390
391 @return The supported languages.
392
393 **/
394 CHAR8 *
395 EFIAPI
396 HiiGetSupportedSecondaryLanguages (
397 IN EFI_HII_HANDLE HiiHandle,
398 IN CONST CHAR8 *FirstLanguage
399 )
400 {
401 EFI_STATUS Status;
402 UINTN BufferSize;
403 CHAR8 *LanguageString;
404
405 ASSERT (HiiHandle != NULL);
406
407 //
408 // Collect current supported 2nd Languages for given HII handle
409 // First try allocate 4K buffer to store the current supported 2nd languages.
410 //
411 BufferSize = 0x1000;
412 LanguageString = AllocateZeroPool (BufferSize);
413 if (LanguageString == NULL) {
414 return NULL;
415 }
416
417 Status = mHiiStringProtocol->GetSecondaryLanguages (mHiiStringProtocol, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
418 ASSERT (Status != EFI_NOT_FOUND);
419
420 if (Status == EFI_BUFFER_TOO_SMALL) {
421 FreePool (LanguageString);
422 LanguageString = AllocateZeroPool (BufferSize);
423 if (LanguageString == NULL) {
424 return NULL;
425 }
426
427 Status = mHiiStringProtocol->GetSecondaryLanguages (mHiiStringProtocol, HiiHandle, FirstLanguage, LanguageString, &BufferSize);
428 }
429
430 if (EFI_ERROR (Status)) {
431 LanguageString = NULL;
432 }
433
434 return LanguageString;
435 }
436
437 /**
438 Allows a program to determine which secondary languages are supported on a given handle for a given primary language
439
440 This routine is intended to be used by drivers to query the interface database for supported languages.
441 This routine returns a string of concatenated 3-byte language identifiers, one per string package associated with the handle.
442
443 @param This A pointer to the EFI_HII_PROTOCOL instance.
444 @param Handle The handle on which the strings reside. Type EFI_HII_HANDLE is defined in EFI_HII_PROTOCOL.NewPack()
445 in the Packages section.
446 @param PrimaryLanguage Pointer to a NULL-terminated string containing a single ISO 639-2 language identifier, indicating
447 the primary language.
448 @param LanguageString A string allocated by GetSecondaryLanguages() containing a list of all secondary languages registered
449 on the handle. The routine will not return the three-spaces language identifier used in other functions
450 to indicate non-language-specific strings, nor will it return the primary language. This function succeeds
451 but returns a NULL LanguageString if there are no secondary languages associated with the input Handle and
452 PrimaryLanguage pair. Type EFI_STRING is defined in String.
453
454 @reval EFI_SUCCESS LanguageString was correctly returned.
455 @reval EFI_INVALID_PARAMETER The Handle was unknown.
456 **/
457 EFI_STATUS
458 EFIAPI
459 HiiGetSecondaryLanguages (
460 IN EFI_HII_PROTOCOL *This,
461 IN FRAMEWORK_EFI_HII_HANDLE Handle,
462 IN CHAR16 *PrimaryLanguage,
463 OUT EFI_STRING *LanguageString
464 )
465 {
466 HII_THUNK_PRIVATE_DATA *Private;
467 EFI_HII_HANDLE UefiHiiHandle;
468 CHAR8 *PrimaryLang4646;
469 CHAR8 *PrimaryLang639;
470 CHAR8 *SecLangCodes4646;
471 CHAR8 *SecLangCodes639;
472 CHAR16 *UnicodeSecLangCodes639;
473 EFI_STATUS Status;
474
475 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
476
477 SecLangCodes639 = NULL;
478 SecLangCodes4646 = NULL;
479 PrimaryLang4646 = NULL;
480 UnicodeSecLangCodes639 = NULL;
481
482 UefiHiiHandle = FwHiiHandleToUefiHiiHandle (Private, Handle);
483 if (UefiHiiHandle == NULL) {
484 return EFI_INVALID_PARAMETER;
485 }
486
487 PrimaryLang639 = AllocateZeroPool (StrLen (PrimaryLanguage) + 1);
488 if (PrimaryLang639 == NULL) {
489 Status = EFI_OUT_OF_RESOURCES;
490 goto Done;
491 }
492
493 UnicodeStrToAsciiStr (PrimaryLanguage, PrimaryLang639);
494
495 PrimaryLang4646 = ConvertLanguagesIso639ToRfc4646 (PrimaryLang639);
496 ASSERT_EFI_ERROR (PrimaryLang4646 != NULL);
497
498 SecLangCodes4646 = HiiGetSupportedSecondaryLanguages (UefiHiiHandle, PrimaryLang4646);
499
500 if (SecLangCodes4646 == NULL) {
501 Status = EFI_INVALID_PARAMETER;
502 goto Done;
503 }
504
505 SecLangCodes639 = ConvertLanguagesIso639ToRfc4646 (SecLangCodes4646);
506 if (SecLangCodes639 == NULL) {
507 Status = EFI_INVALID_PARAMETER;
508 goto Done;
509 }
510
511 UnicodeSecLangCodes639 = AllocateZeroPool (AsciiStrSize (SecLangCodes639) * sizeof (CHAR16));
512 if (UnicodeSecLangCodes639 == NULL) {
513 Status = EFI_OUT_OF_RESOURCES;
514 goto Done;
515 }
516
517 //
518 // The language returned is in RFC 4646 format.
519 //
520 *LanguageString = AsciiStrToUnicodeStr (SecLangCodes639, UnicodeSecLangCodes639);
521 Status = EFI_SUCCESS;
522
523 Done:
524 if (PrimaryLang639 != NULL) {
525 FreePool (PrimaryLang639);
526 }
527
528 if (SecLangCodes639 != NULL) {
529 FreePool (SecLangCodes639);
530 }
531
532 if (PrimaryLang4646 != NULL) {
533 FreePool (PrimaryLang4646);
534 }
535
536 if (SecLangCodes4646 != NULL) {
537 FreePool (SecLangCodes4646);
538 }
539 if (UnicodeSecLangCodes639 != NULL) {
540 FreePool (UnicodeSecLangCodes639);
541 }
542
543 return Status;
544 }
545
546