]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/FrameworkHiiOnUefiHiiThunk/Strings.c
05a885cc8ffc0ad14fc7d38d9bf0464f7f8c1f5a
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FrameworkHiiOnUefiHiiThunk / Strings.c
1 /**@file
2 This file implements the protocol functions related to string package.
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
18 typedef struct {
19 CHAR8 *Iso639;
20 CHAR8 *Rfc3066;
21 } ISO639TORFC3066MAP;
22
23 ISO639TORFC3066MAP Iso639ToRfc3066Map [] = {
24 {"eng", "en-US"},
25 {"fra", "fr-FR"},
26 };
27
28 CHAR8 *
29 ConvertIso639ToRfc3066 (
30 CHAR8 *Iso638Lang
31 )
32 {
33 UINTN Index;
34 CHAR8 AsciiLanguage[ISO_639_2_ENTRY_SIZE + 1];
35
36 AsciiStrnCpy (AsciiLanguage, Iso638Lang, sizeof (AsciiLanguage));
37 for (Index = 0; Index < ISO_639_2_ENTRY_SIZE + 1; Index ++) {
38 if (AsciiLanguage [Index] == 0) {
39 break;
40 } else if (AsciiLanguage [Index] >= 'A' && AsciiLanguage [Index] <= 'Z') {
41 AsciiLanguage [Index] = (CHAR8) (AsciiLanguage [Index] - 'A' + 'a');
42 }
43 }
44
45 for (Index = 0; Index < sizeof (Iso639ToRfc3066Map) / sizeof (Iso639ToRfc3066Map[0]); Index++) {
46 if (AsciiStrnCmp (AsciiLanguage, Iso639ToRfc3066Map[Index].Iso639, AsciiStrSize (AsciiLanguage)) == 0) {
47 return Iso639ToRfc3066Map[Index].Rfc3066;
48 }
49 }
50
51 return (CHAR8 *) NULL;
52 }
53
54 /**
55 Test if all of the characters in a string have corresponding font characters.
56
57 This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
58 return EFI_UNSUPPORTED.
59
60 @param This A pointer to the EFI_HII_PROTOCOL instance.
61 @param StringToTest A pointer to a Unicode string.
62 @param FirstMissing A pointer to an index into the string. On input, the index of
63 the first character in the StringToTest to examine. On exit, the index
64 of the first character encountered for which a glyph is unavailable.
65 If all glyphs in the string are available, the index is the index of the terminator
66 of the string.
67 @param GlyphBufferSize A pointer to a value. On output, if the function returns EFI_SUCCESS,
68 it contains the amount of memory that is required to store the string? glyph equivalent.
69
70 @retval EFI_UNSUPPORTED The function performs nothing and return EFI_UNSUPPORTED.
71 **/
72 EFI_STATUS
73 EFIAPI
74 HiiTestString (
75 IN EFI_HII_PROTOCOL *This,
76 IN CHAR16 *StringToTest,
77 IN OUT UINT32 *FirstMissing,
78 OUT UINT32 *GlyphBufferSize
79 )
80 {
81 ASSERT (FALSE);
82
83 return EFI_UNSUPPORTED;
84 }
85
86
87 /**
88 Find the corressponding TAG GUID from a Framework HII Handle given.
89
90 @param Private The HII Thunk Module Private context.
91 @param FwHiiHandle The Framemwork HII Handle.
92 @param TagGuid The output of TAG GUID found.
93
94 @return NULL If Framework HII Handle is invalid.
95 @return The corresponding HII Thunk Context.
96 **/
97 EFI_STATUS
98 GetTagGuidByFwHiiHandle (
99 IN CONST HII_THUNK_PRIVATE_DATA *Private,
100 IN FRAMEWORK_EFI_HII_HANDLE FwHiiHandle,
101 OUT EFI_GUID *TagGuid
102 )
103 {
104 LIST_ENTRY *Link;
105 HII_THUNK_CONTEXT *ThunkContext;
106
107 ASSERT (TagGuid != NULL);
108
109 Link = GetFirstNode (&Private->ThunkContextListHead);
110 while (!IsNull (&Private->ThunkContextListHead, Link)) {
111
112 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
113
114 if (FwHiiHandle == ThunkContext->FwHiiHandle) {
115 CopyGuid (TagGuid, &ThunkContext->TagGuid);
116 return EFI_SUCCESS;
117 }
118
119 Link = GetNextNode (&Private->ThunkContextListHead, Link);
120 }
121
122 return EFI_NOT_FOUND;
123 }
124
125 /**
126 Create or update the String given a new string and String ID.
127
128 @param ThunkContext The Thunk Context.
129 @param Rfc3066AsciiLanguage The RFC 3066 Language code in ASCII string format.
130 @param NewString The new string.
131 @param StringId The String ID. If StringId is 0, a new String Token
132 is created. Otherwise, the String Token StringId is
133 updated.
134
135
136 @retval EFI_SUCCESS The new string is created or updated successfully.
137 The new String Token ID is returned in StringId if
138 *StringId is 0 on input.
139 @return Others The update of string failed.
140
141 **/
142 EFI_STATUS
143 UpdateString (
144 IN CONST HII_THUNK_CONTEXT *ThunkContext,
145 IN CONST CHAR8 *Rfc3066AsciiLanguage,
146 IN CHAR16 *NewString,
147 IN OUT STRING_REF *StringId
148 )
149 {
150 EFI_STRING_ID NewStringId;
151
152 NewStringId = HiiSetString (ThunkContext->UefiHiiHandle, *StringId, NewString, Rfc3066AsciiLanguage);
153 *StringId = NewStringId;
154 if (NewStringId == 0) {
155 //
156 // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
157 //
158 return EFI_INVALID_PARAMETER;
159 } else {
160 return EFI_SUCCESS;
161 }
162 }
163
164 /**
165 Create or update a String Token in a String Package.
166
167 If *Reference == 0, a new String Token is created.
168
169 @param This A pointer to the EFI_HII_PROTOCOL instance.
170 @param Language Pointer to a NULL-terminated string containing a single ISO 639-2 language
171 identifier, indicating the language to print. A string consisting of
172 all spaces indicates that the string is applicable to all languages.
173 @param Handle The handle of the language pack to which the string is to be added.
174 @param Token The string token assigned to the string.
175 @param NewString The string to be added.
176
177
178 @retval EFI_SUCCESS The string was effectively registered.
179 @retval EFI_INVALID_PARAMETER The Handle was unknown. The string is not created or updated in the
180 the string package.
181 **/
182
183 EFI_STATUS
184 EFIAPI
185 HiiNewString (
186 IN EFI_HII_PROTOCOL *This,
187 IN CHAR16 *Language,
188 IN FRAMEWORK_EFI_HII_HANDLE Handle,
189 IN OUT STRING_REF *Reference,
190 IN CHAR16 *NewString
191 )
192 {
193 EFI_STATUS Status;
194 HII_THUNK_PRIVATE_DATA *Private;
195 EFI_GUID TagGuid;
196 LIST_ENTRY *Link;
197 HII_THUNK_CONTEXT *ThunkContext;
198 HII_THUNK_CONTEXT *StringPackThunkContext;
199 EFI_STRING_ID StringId;
200 EFI_STRING_ID LastStringId;
201 CHAR8 AsciiLanguage[ISO_639_2_ENTRY_SIZE + 1];
202 CHAR16 LanguageCopy[ISO_639_2_ENTRY_SIZE + 1];
203 CHAR8 *Rfc3066AsciiLanguage;
204
205 LastStringId = (EFI_STRING_ID) 0;
206 StringId = (EFI_STRING_ID) 0;
207 Rfc3066AsciiLanguage = NULL;
208
209 if (Language != NULL) {
210 ZeroMem (AsciiLanguage, sizeof (AsciiLanguage));;
211 ZeroMem (LanguageCopy, sizeof (LanguageCopy));
212 CopyMem (LanguageCopy, Language, ISO_639_2_ENTRY_SIZE * sizeof (CHAR16));
213 UnicodeStrToAsciiStr (LanguageCopy, AsciiLanguage);
214 Rfc3066AsciiLanguage = ConvertIso639ToRfc3066 (AsciiLanguage);
215 ASSERT (Rfc3066AsciiLanguage != NULL);
216 }
217
218 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
219
220 StringPackThunkContext = FwHiiHandleToThunkContext (Private, Handle);
221 if (StringPackThunkContext == NULL) {
222 return EFI_INVALID_PARAMETER;
223 }
224
225 if (StringPackThunkContext->SharingStringPack) {
226 Status = GetTagGuidByFwHiiHandle (Private, Handle, &TagGuid);
227 ASSERT_EFI_ERROR (Status);
228
229 Link = GetFirstNode (&Private->ThunkContextListHead);
230 while (!IsNull (&Private->ThunkContextListHead, Link)) {
231 ThunkContext = HII_THUNK_CONTEXT_FROM_LINK (Link);
232
233 if (CompareGuid (&TagGuid, &ThunkContext->TagGuid)) {
234 if (ThunkContext->SharingStringPack) {
235 StringId = *Reference;
236 Status = UpdateString (ThunkContext, Rfc3066AsciiLanguage, NewString, &StringId);
237 if (EFI_ERROR (Status)) {
238 break;
239 }
240
241 DEBUG_CODE_BEGIN ();
242 if (*Reference == 0) {
243 //
244 // When creating new string token, make sure all created token is the same
245 // for all string packages registered using FW HII interface.
246 //
247 if (LastStringId == (EFI_STRING_ID) 0) {
248 LastStringId = StringId;
249 } else {
250 if (LastStringId != StringId) {
251 ASSERT(FALSE);
252 }
253 }
254 }
255 DEBUG_CODE_END ();
256
257 }
258 }
259
260 Link = GetNextNode (&Private->ThunkContextListHead, Link);
261 }
262 } else {
263 StringId = *Reference;
264 Status = UpdateString (StringPackThunkContext, Rfc3066AsciiLanguage, NewString, &StringId);
265 }
266
267 if (!EFI_ERROR (Status)) {
268 if (*Reference == 0) {
269 *Reference = StringId;
270 }
271 } else {
272 //
273 // Only EFI_INVALID_PARAMETER is defined in HII 0.92 specification.
274 //
275 Status = EFI_INVALID_PARAMETER;
276 }
277
278 return Status;
279 }
280
281 /**
282 This function removes any new strings that were added after the initial string export for this handle.
283 UEFI HII String Protocol does not have Reset String function. This function perform nothing.
284
285 @param This A pointer to the EFI_HII_PROTOCOL instance.
286 @param Handle The HII handle on which the string resides.
287
288 @retval EFI_SUCCESS This function is a NOP and always return EFI_SUCCESS.
289
290 **/
291 EFI_STATUS
292 EFIAPI
293 HiiResetStrings (
294 IN EFI_HII_PROTOCOL *This,
295 IN FRAMEWORK_EFI_HII_HANDLE Handle
296 )
297 {
298 return EFI_SUCCESS;
299 }
300
301 /**
302 This function extracts a string from a package already registered with the EFI HII database.
303
304 @param This A pointer to the EFI_HII_PROTOCOL instance.
305 @param Handle The HII handle on which the string resides.
306 @param Token The string token assigned to the string.
307 @param Raw If TRUE, the string is returned unedited in the internal storage format described
308 above. If false, the string returned is edited by replacing <cr> with <space>
309 and by removing special characters such as the <wide> prefix.
310 @param LanguageString Pointer to a NULL-terminated string containing a single ISO 639-2 language
311 identifier, indicating the language to print. If the LanguageString is empty (starts
312 with a NULL), the default system language will be used to determine the language.
313 @param BufferLength Length of the StringBuffer. If the status reports that the buffer width is too
314 small, this parameter is filled with the length of the buffer needed.
315 @param StringBuffer The buffer designed to receive the characters in the string. Type EFI_STRING is
316 defined in String.
317
318 @retval EFI_INVALID_PARAMETER If input parameter is invalid.
319 @retval EFI_BUFFER_TOO_SMALL If the *BufferLength is too small.
320 @retval EFI_SUCCESS Operation is successful.
321
322 **/
323 EFI_STATUS
324 EFIAPI
325 HiiThunkGetString (
326 IN EFI_HII_PROTOCOL *This,
327 IN FRAMEWORK_EFI_HII_HANDLE Handle,
328 IN STRING_REF Token,
329 IN BOOLEAN Raw,
330 IN CHAR16 *LanguageString,
331 IN OUT UINTN *BufferLengthTemp,
332 OUT EFI_STRING StringBuffer
333 )
334 {
335 HII_THUNK_PRIVATE_DATA *Private;
336 CHAR8 *Iso639AsciiLanguage;
337 CHAR8 *Rfc3066AsciiLanguage;
338 CHAR8 *SupportedLanguages;
339 CHAR8 *PlatformLanguage;
340 CHAR8 *BestLanguage;
341 EFI_HII_HANDLE UefiHiiHandle;
342 EFI_STATUS Status;
343
344 Private = HII_THUNK_PRIVATE_DATA_FROM_THIS(This);
345
346 Iso639AsciiLanguage = NULL;
347 Rfc3066AsciiLanguage = NULL;
348
349 if (LanguageString != NULL) {
350 Iso639AsciiLanguage = AllocateZeroPool (StrLen (LanguageString) + 1);
351 if (Iso639AsciiLanguage == NULL) {
352 return EFI_OUT_OF_RESOURCES;
353 }
354 UnicodeStrToAsciiStr (LanguageString, Iso639AsciiLanguage);
355
356 //
357 // Caller of Framework HII Interface uses the Language Identification String defined
358 // in Iso639. So map it to the Language Identifier defined in RFC3066.
359 //
360 Rfc3066AsciiLanguage = ConvertIso639ToRfc3066 (Iso639AsciiLanguage);
361
362 //
363 // If Rfc3066AsciiLanguage is NULL, more language mapping must be added to
364 // Iso639ToRfc3066Map.
365 //
366 ASSERT (Rfc3066AsciiLanguage != NULL);
367
368 }
369
370 UefiHiiHandle = FwHiiHandleToUefiHiiHandle (Private, Handle);
371 if (UefiHiiHandle == NULL) {
372 Status = EFI_NOT_FOUND;
373 goto Done;
374 }
375
376 if (Rfc3066AsciiLanguage == NULL) {
377 //
378 // Get the languages that the package specified by HiiHandle supports
379 //
380 SupportedLanguages = HiiGetSupportedLanguages (UefiHiiHandle);
381 if (SupportedLanguages == NULL) {
382 goto Error2;
383 }
384
385 //
386 // Get the current platform language setting
387 //
388 PlatformLanguage = GetEfiGlobalVariable (L"PlatformLang");
389 if (PlatformLanguage == NULL) {
390 goto Error1;
391 }
392
393 //
394 // Get the best matching language from SupportedLanguages
395 //
396 BestLanguage = GetBestLanguage (
397 SupportedLanguages,
398 FALSE, // RFC 4646 mode
399 PlatformLanguage, // Next highest priority
400 SupportedLanguages, // Lowest priority
401 NULL
402 );
403 if (BestLanguage == NULL) {
404 FreePool (PlatformLanguage);
405 Error1:
406 FreePool (SupportedLanguages);
407 Error2:
408 Status = EFI_INVALID_PARAMETER;
409 goto Done;
410 }
411
412 Status = mHiiStringProtocol->GetString (
413 mHiiStringProtocol,
414 BestLanguage,
415 UefiHiiHandle,
416 Token,
417 StringBuffer,
418 BufferLengthTemp,
419 NULL
420 );
421 FreePool (BestLanguage);
422 } else {
423 Status = mHiiStringProtocol->GetString (
424 mHiiStringProtocol,
425 Rfc3066AsciiLanguage,
426 UefiHiiHandle,
427 Token,
428 StringBuffer,
429 BufferLengthTemp,
430 NULL
431 );
432 }
433
434 Done:
435 if (Iso639AsciiLanguage != NULL) {
436 FreePool (Iso639AsciiLanguage);
437 }
438
439 return Status;
440 }
441
442 /**
443
444 This function allows a program to extract a part of a string of not more than a given width.
445 With repeated calls, this allows a calling program to extract "lines" of text that fit inside
446 columns. The effort of measuring the fit of strings inside columns is localized to this call.
447
448 This is a deprecated API. No Framework HII module is calling it. This function will ASSERT and
449 return EFI_UNSUPPORTED.
450
451 @param This A pointer to the EFI_HII_PROTOCOL instance.
452 @param Handle The HII handle on which the string resides.
453 @param Token The string token assigned to the string.
454 @param Raw If TRUE, the string is returned unedited in the internal storage format described
455 above. If false, the string returned is edited by replacing <cr> with <space>
456 and by removing special characters such as the <wide> prefix.
457 @param LanguageString Pointer to a NULL-terminated string containing a single ISO 639-2 language
458 identifier, indicating the language to print. If the LanguageString is empty (starts
459 with a NULL), the default system language will be used to determine the language.
460 @param BufferLength Length of the StringBuffer. If the status reports that the buffer width is too
461 small, this parameter is filled with the length of the buffer needed.
462 @param StringBuffer The buffer designed to receive the characters in the string. Type EFI_STRING is
463 defined in String.
464
465 @retval EFI_UNSUPPORTED.
466 **/
467 EFI_STATUS
468 EFIAPI
469 HiiGetLine (
470 IN EFI_HII_PROTOCOL *This,
471 IN FRAMEWORK_EFI_HII_HANDLE Handle,
472 IN STRING_REF Token,
473 IN OUT UINT16 *Index,
474 IN UINT16 LineWidth,
475 IN CHAR16 *LanguageString,
476 IN OUT UINT16 *BufferLength,
477 OUT EFI_STRING StringBuffer
478 )
479 {
480 ASSERT (FALSE);
481 return EFI_UNSUPPORTED;
482 }
483
484