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