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