2 HII Library implementation that uses DXE protocols and services.
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "InternalHiiLib.h"
14 This function create a new string in String Package or updates an existing
15 string in a String Package. If StringId is 0, then a new string is added to
16 a String Package. If StringId is not zero, then a string in String Package is
17 updated. If SupportedLanguages is NULL, then the string is added or updated
18 for all the languages that the String Package supports. If SupportedLanguages
19 is not NULL, then the string is added or updated for the set of languages
20 specified by SupportedLanguages.
22 If HiiHandle is NULL, then ASSERT().
23 If String is NULL, then ASSERT().
25 @param[in] HiiHandle A handle that was previously registered in the
27 @param[in] StringId If zero, then a new string is created in the
28 String Package associated with HiiHandle. If
29 non-zero, then the string specified by StringId
30 is updated in the String Package associated
32 @param[in] String A pointer to the Null-terminated Unicode string
33 to add or update in the String Package associated
35 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of
36 language codes. If this parameter is NULL, then
37 String is added or updated in the String Package
38 associated with HiiHandle for all the languages
39 that the String Package supports. If this
40 parameter is not NULL, then then String is added
41 or updated in the String Package associated with
42 HiiHandle for the set oflanguages specified by
43 SupportedLanguages. The format of
44 SupportedLanguages must follow the language
45 format assumed the HII Database.
47 @retval 0 The string could not be added or updated in the String Package.
48 @retval Other The EFI_STRING_ID of the newly added or updated string.
54 IN EFI_HII_HANDLE HiiHandle
,
55 IN EFI_STRING_ID StringId OPTIONAL
,
56 IN CONST EFI_STRING String
,
57 IN CONST CHAR8
*SupportedLanguages OPTIONAL
61 CHAR8
*AllocatedLanguages
;
65 ASSERT (HiiHandle
!= NULL
);
66 ASSERT (String
!= NULL
);
68 if (SupportedLanguages
== NULL
) {
70 // Retrieve the languages that the package specified by HiiHandle supports
72 AllocatedLanguages
= HiiGetSupportedLanguages (HiiHandle
);
75 // Allocate a copy of the SupportLanguages string that passed in
77 AllocatedLanguages
= AllocateCopyPool (AsciiStrSize (SupportedLanguages
), SupportedLanguages
);
81 // If there are not enough resources for the supported languages string, then return a StringId of 0
83 if (AllocatedLanguages
== NULL
) {
84 return (EFI_STRING_ID
)(0);
87 Status
= EFI_INVALID_PARAMETER
;
89 // Loop through each language that the string supports
91 for (Supported
= AllocatedLanguages
; *Supported
!= '\0'; ) {
93 // Cache a pointer to the beginning of the current language in the list of languages
98 // Search for the next language separator and replace it with a Null-terminator
100 for (; *Supported
!= 0 && *Supported
!= ';'; Supported
++);
101 if (*Supported
!= 0) {
102 *(Supported
++) = '\0';
105 if ((SupportedLanguages
== NULL
) && AsciiStrnCmp (Language
, UEFI_CONFIG_LANG
, AsciiStrLen (UEFI_CONFIG_LANG
)) == 0) {
107 // Skip string package used for keyword protocol.
113 // If StringId is 0, then call NewString(). Otherwise, call SetString()
115 if (StringId
== (EFI_STRING_ID
)(0)) {
116 Status
= gHiiString
->NewString (gHiiString
, HiiHandle
, &StringId
, Language
, NULL
, String
, NULL
);
118 Status
= gHiiString
->SetString (gHiiString
, HiiHandle
, StringId
, Language
, String
, NULL
);
122 // If there was an error, then break out of the loop and return a StringId of 0
124 if (EFI_ERROR (Status
)) {
130 // Free the buffer of supported languages
132 FreePool (AllocatedLanguages
);
134 if (EFI_ERROR (Status
)) {
135 return (EFI_STRING_ID
)(0);
143 Retrieves a string from a string package names by GUID in a specific language.
144 If the language is not specified, then a string from a string package in the
145 current platform language is retrieved. If the string can not be retrieved
146 using the specified language or the current platform language, then the string
147 is retrieved from the string package in the first language the string package
148 supports. The returned string is allocated using AllocatePool(). The caller
149 is responsible for freeing the allocated buffer using FreePool().
151 If PackageListGuid is NULL, then ASSERT().
152 If StringId is 0, then ASSERT.
154 @param[in] PackageListGuid The GUID of a package list that was previously
155 registered in the HII Database.
156 @param[in] StringId The identifier of the string to retrieved from the
157 string package associated with PackageListGuid.
158 @param[in] Language The language of the string to retrieve. If this
159 parameter is NULL, then the current platform
160 language is used. The format of Language must
161 follow the language format assumed the HII Database.
163 @retval NULL The package list specified by PackageListGuid is not present in the
165 @retval NULL The string specified by StringId is not present in the string package.
166 @retval Other The string was returned.
171 HiiGetPackageString (
172 IN CONST EFI_GUID
*PackageListGuid
,
173 IN EFI_STRING_ID StringId
,
174 IN CONST CHAR8
*Language OPTIONAL
177 EFI_HII_HANDLE
*HiiHandleBuffer
;
178 EFI_HII_HANDLE HiiHandle
;
180 ASSERT (PackageListGuid
!= NULL
);
182 HiiHandleBuffer
= HiiGetHiiHandles (PackageListGuid
);
183 if (HiiHandleBuffer
== NULL
) {
187 HiiHandle
= HiiHandleBuffer
[0];
188 FreePool (HiiHandleBuffer
);
190 return HiiGetString (HiiHandle
, StringId
, Language
);
194 Retrieves a string from a string package in a specific language specified in Language
195 or in the best lanaguage. See HiiGetStringEx () for the details.
197 @param[in] HiiHandle A handle that was previously registered in the HII Database.
198 @param[in] StringId The identifier of the string to retrieved from the string
199 package associated with HiiHandle.
200 @param[in] Language The language of the string to retrieve. If this parameter
201 is NULL, then the current platform language is used. The
202 format of Language must follow the language format assumed
205 @retval NULL The string specified by StringId is not present in the string package.
206 @retval Other The string was returned.
212 IN EFI_HII_HANDLE HiiHandle
,
213 IN EFI_STRING_ID StringId
,
214 IN CONST CHAR8
*Language OPTIONAL
217 return HiiGetStringEx (HiiHandle
, StringId
, Language
, TRUE
);
221 Retrieves a string from a string package in a specific language or in the best
222 language at discretion of this function according to the priority of languages.
223 TryBestLanguage is used to get the string in the best language or in the language
224 specified in Language parameter. The behavior is,
225 If TryBestLanguage is TRUE, this function looks for the best language for the string.
226 - If the string can not be retrieved using the specified language or the current
227 platform language, then the string is retrieved from the string package in the
228 first language the string package supports.
229 If TryBestLanguage is FALSE, Language must be specified for retrieving the string.
231 The returned string is allocated using AllocatePool(). The caller is responsible
232 for freeing the allocated buffer using FreePool().
234 If HiiHandle is NULL, then ASSERT().
235 If StringId is 0, then ASSET.
236 If TryBestLanguage is FALE and Language is NULL, then ASSERT().
238 @param[in] HiiHandle A handle that was previously registered in the HII Database.
239 @param[in] StringId The identifier of the string to retrieved from the string
240 package associated with HiiHandle.
241 @param[in] Language The language of the string to retrieve. If this parameter
242 is NULL, then the current platform language is used. The
243 format of Language must follow the language format assumed
245 @param[in] TryBestLanguage If TRUE, try to get the best matching language from all
246 supported languages.If FALSE, the Language must be assigned
249 @retval NULL The string specified by StringId is not present in the string package.
250 @retval Other The string was returned.
256 IN EFI_HII_HANDLE HiiHandle
,
257 IN EFI_STRING_ID StringId
,
258 IN CONST CHAR8
*Language OPTIONAL
,
259 IN BOOLEAN TryBestLanguage
266 CHAR8
*SupportedLanguages
;
267 CHAR8
*PlatformLanguage
;
270 ASSERT (HiiHandle
!= NULL
);
271 ASSERT (StringId
!= 0);
273 // Language must be specified if TryBestLanguage = FALSE.
275 ASSERT (!(!TryBestLanguage
&& Language
== NULL
));
277 // Initialize all allocated buffers to NULL
279 SupportedLanguages
= NULL
;
280 PlatformLanguage
= NULL
;
285 // Get the languages that the package specified by HiiHandle supports
287 SupportedLanguages
= HiiGetSupportedLanguages (HiiHandle
);
288 if (SupportedLanguages
== NULL
) {
293 // Get the current platform language setting
295 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatformLanguage
, NULL
);
298 // If Languag is NULL, then set it to an empty string, so it will be
299 // skipped by GetBestLanguage()
301 if (Language
== NULL
) {
305 if (TryBestLanguage
) {
307 // Get the best matching language from SupportedLanguages
309 BestLanguage
= GetBestLanguage (
311 FALSE
, // RFC 4646 mode
312 Language
, // Highest priority
313 PlatformLanguage
!= NULL
? PlatformLanguage
: "", // Next highest priority
314 SupportedLanguages
, // Lowest priority
317 if (BestLanguage
== NULL
) {
321 BestLanguage
= (CHAR8
*) Language
;
326 // Retrieve the size of the string in the string package for the BestLanguage
329 Status
= gHiiString
->GetString (
339 // If GetString() returns EFI_SUCCESS for a zero size,
340 // then there are no supported languages registered for HiiHandle. If GetString()
341 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
342 // in the HII Database
344 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
349 // Allocate a buffer for the return string
351 String
= AllocateZeroPool (StringSize
);
352 if (String
== NULL
) {
357 // Retrieve the string from the string package
359 Status
= gHiiString
->GetString (
368 if (EFI_ERROR (Status
)) {
370 // Free the buffer and return NULL if the supported languages can not be retrieved.
378 // Free allocated buffers
380 if (SupportedLanguages
!= NULL
) {
381 FreePool (SupportedLanguages
);
383 if (PlatformLanguage
!= NULL
) {
384 FreePool (PlatformLanguage
);
386 if (TryBestLanguage
&& BestLanguage
!= NULL
) {
387 FreePool (BestLanguage
);
391 // Return the Null-terminated Unicode string