]>
Commit | Line | Data |
---|---|---|
08e4b3cf | 1 | /** @file\r |
2 | HII Library implementation that uses DXE protocols and services.\r | |
3 | \r | |
bf4a3dbd | 4 | Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r |
cd5ebaa0 | 5 | This program and the accompanying materials\r |
08e4b3cf | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | \r | |
16 | #include "InternalHiiLib.h"\r | |
17 | \r | |
08e4b3cf | 18 | /**\r |
cb7d01c0 | 19 | This function create a new string in String Package or updates an existing \r |
20 | string in a String Package. If StringId is 0, then a new string is added to\r | |
21 | a String Package. If StringId is not zero, then a string in String Package is\r | |
22 | updated. If SupportedLanguages is NULL, then the string is added or updated\r | |
23 | for all the languages that the String Package supports. If SupportedLanguages\r | |
24 | is not NULL, then the string is added or updated for the set of languages \r | |
25 | specified by SupportedLanguages.\r | |
26 | \r | |
27 | If HiiHandle is NULL, then ASSERT().\r | |
28 | If String is NULL, then ASSERT().\r | |
29 | \r | |
30 | @param[in] HiiHandle A handle that was previously registered in the \r | |
31 | HII Database.\r | |
32 | @param[in] StringId If zero, then a new string is created in the \r | |
33 | String Package associated with HiiHandle. If \r | |
34 | non-zero, then the string specified by StringId \r | |
35 | is updated in the String Package associated \r | |
36 | with HiiHandle. \r | |
37 | @param[in] String A pointer to the Null-terminated Unicode string \r | |
38 | to add or update in the String Package associated \r | |
39 | with HiiHandle.\r | |
40 | @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of \r | |
41 | language codes. If this parameter is NULL, then \r | |
42 | String is added or updated in the String Package \r | |
43 | associated with HiiHandle for all the languages \r | |
44 | that the String Package supports. If this \r | |
45 | parameter is not NULL, then then String is added \r | |
46 | or updated in the String Package associated with \r | |
47 | HiiHandle for the set oflanguages specified by \r | |
48 | SupportedLanguages. The format of \r | |
49 | SupportedLanguages must follow the language \r | |
50 | format assumed the HII Database.\r | |
51 | \r | |
52 | @retval 0 The string could not be added or updated in the String Package.\r | |
53 | @retval Other The EFI_STRING_ID of the newly added or updated string.\r | |
08e4b3cf | 54 | \r |
55 | **/\r | |
cb7d01c0 | 56 | EFI_STRING_ID\r |
08e4b3cf | 57 | EFIAPI\r |
cb7d01c0 | 58 | HiiSetString (\r |
59 | IN EFI_HII_HANDLE HiiHandle,\r | |
60 | IN EFI_STRING_ID StringId, OPTIONAL\r | |
61 | IN CONST EFI_STRING String,\r | |
62 | IN CONST CHAR8 *SupportedLanguages OPTIONAL\r | |
08e4b3cf | 63 | )\r |
64 | {\r | |
cb7d01c0 | 65 | EFI_STATUS Status;\r |
66 | CHAR8 *AllocatedLanguages;\r | |
67 | CHAR8 *Supported;\r | |
68 | CHAR8 *Language;\r | |
08e4b3cf | 69 | \r |
cb7d01c0 | 70 | ASSERT (HiiHandle != NULL);\r |
08e4b3cf | 71 | ASSERT (String != NULL);\r |
08e4b3cf | 72 | \r |
cb7d01c0 | 73 | if (SupportedLanguages == NULL) {\r |
74 | //\r | |
75 | // Retrieve the languages that the package specified by HiiHandle supports\r | |
76 | //\r | |
77 | AllocatedLanguages = HiiGetSupportedLanguages (HiiHandle);\r | |
78 | } else {\r | |
79 | //\r | |
80 | // Allocate a copy of the SupportLanguages string that passed in\r | |
81 | //\r | |
f580c1bd | 82 | AllocatedLanguages = AllocateCopyPool (AsciiStrSize (SupportedLanguages), SupportedLanguages);\r |
cb7d01c0 | 83 | }\r |
08e4b3cf | 84 | \r |
ea7cd3ec | 85 | //\r |
cb7d01c0 | 86 | // If there are not enough resources for the supported languages string, then return a StringId of 0\r |
ea7cd3ec | 87 | //\r |
cb7d01c0 | 88 | if (AllocatedLanguages == NULL) {\r |
89 | return (EFI_STRING_ID)(0);\r | |
90 | }\r | |
08e4b3cf | 91 | \r |
cb7d01c0 | 92 | Status = EFI_INVALID_PARAMETER;\r |
93 | //\r | |
94 | // Loop through each language that the string supports\r | |
95 | //\r | |
96 | for (Supported = AllocatedLanguages; *Supported != '\0'; ) {\r | |
97 | //\r | |
98 | // Cache a pointer to the beginning of the current language in the list of languages\r | |
99 | //\r | |
100 | Language = Supported;\r | |
08e4b3cf | 101 | \r |
102 | //\r | |
cb7d01c0 | 103 | // Search for the next language seperator and replace it with a Null-terminator\r |
08e4b3cf | 104 | //\r |
cb7d01c0 | 105 | for (; *Supported != 0 && *Supported != ';'; Supported++);\r |
106 | if (*Supported != 0) {\r | |
107 | *(Supported++) = '\0';\r | |
08e4b3cf | 108 | }\r |
08e4b3cf | 109 | \r |
cb7d01c0 | 110 | //\r |
111 | // If StringId is 0, then call NewString(). Otherwise, call SetString()\r | |
112 | //\r | |
113 | if (StringId == (EFI_STRING_ID)(0)) {\r | |
d6a82eaf | 114 | Status = gHiiString->NewString (gHiiString, HiiHandle, &StringId, Language, NULL, String, NULL);\r |
cb7d01c0 | 115 | } else {\r |
116 | Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);\r | |
117 | }\r | |
08e4b3cf | 118 | \r |
119 | //\r | |
cb7d01c0 | 120 | // If there was an error, then break out of the loop and return a StringId of 0\r |
08e4b3cf | 121 | //\r |
08e4b3cf | 122 | if (EFI_ERROR (Status)) {\r |
123 | break;\r | |
124 | }\r | |
125 | }\r | |
126 | \r | |
cb7d01c0 | 127 | //\r |
128 | // Free the buffer of supported languages\r | |
129 | //\r | |
130 | FreePool (AllocatedLanguages);\r | |
131 | \r | |
132 | if (EFI_ERROR (Status)) {\r | |
133 | return (EFI_STRING_ID)(0);\r | |
cb7d01c0 | 134 | } else {\r |
135 | return StringId;\r | |
136 | }\r | |
08e4b3cf | 137 | }\r |
138 | \r | |
139 | \r | |
140 | /**\r | |
cb7d01c0 | 141 | Retrieves a string from a string package names by GUID in a specific language. \r |
142 | If the language is not specified, then a string from a string package in the \r | |
143 | current platform language is retrieved. If the string can not be retrieved \r | |
144 | using the specified language or the current platform language, then the string \r | |
145 | is retrieved from the string package in the first language the string package \r | |
146 | supports. The returned string is allocated using AllocatePool(). The caller \r | |
147 | is responsible for freeing the allocated buffer using FreePool().\r | |
148 | \r | |
149 | If PackageListGuid is NULL, then ASSERT().\r | |
150 | If StringId is 0, then ASSERT.\r | |
151 | \r | |
152 | @param[in] PackageListGuid The GUID of a package list that was previously \r | |
153 | registered in the HII Database.\r | |
154 | @param[in] StringId The identifier of the string to retrieved from the \r | |
155 | string package associated with PackageListGuid.\r | |
156 | @param[in] Language The language of the string to retrieve. If this \r | |
157 | parameter is NULL, then the current platform \r | |
158 | language is used. The format of Language must \r | |
159 | follow the language format assumed the HII Database.\r | |
160 | \r | |
161 | @retval NULL The package list specified by PackageListGuid is not present in the\r | |
162 | HII Database.\r | |
163 | @retval NULL The string specified by StringId is not present in the string package.\r | |
164 | @retval Other The string was returned.\r | |
08e4b3cf | 165 | \r |
166 | **/\r | |
cb7d01c0 | 167 | EFI_STRING\r |
08e4b3cf | 168 | EFIAPI\r |
cb7d01c0 | 169 | HiiGetPackageString (\r |
170 | IN CONST EFI_GUID *PackageListGuid,\r | |
171 | IN EFI_STRING_ID StringId,\r | |
172 | IN CONST CHAR8 *Language OPTIONAL\r | |
08e4b3cf | 173 | )\r |
174 | {\r | |
cb7d01c0 | 175 | EFI_HANDLE *HiiHandleBuffer;\r |
176 | EFI_HANDLE HiiHandle;\r | |
08e4b3cf | 177 | \r |
cb7d01c0 | 178 | ASSERT (PackageListGuid != NULL);\r |
08e4b3cf | 179 | \r |
cb7d01c0 | 180 | HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);\r |
181 | if (HiiHandleBuffer == NULL) {\r | |
182 | return NULL;\r | |
08e4b3cf | 183 | }\r |
184 | \r | |
cb7d01c0 | 185 | HiiHandle = HiiHandleBuffer[0];\r |
4a1102c9 | 186 | FreePool (HiiHandleBuffer);\r |
187 | \r | |
cb7d01c0 | 188 | return HiiGetString (HiiHandle, StringId, Language);\r |
08e4b3cf | 189 | }\r |
190 | \r | |
191 | /**\r | |
cb7d01c0 | 192 | Retrieves a string from a string package in a specific language. If the language\r |
193 | is not specified, then a string from a string package in the current platform \r | |
194 | language is retrieved. If the string can not be retrieved using the specified \r | |
195 | language or the current platform language, then the string is retrieved from \r | |
196 | the string package in the first language the string package supports. The \r | |
197 | returned string is allocated using AllocatePool(). The caller is responsible \r | |
198 | for freeing the allocated buffer using FreePool().\r | |
199 | \r | |
200 | If HiiHandle is NULL, then ASSERT().\r | |
201 | If StringId is 0, then ASSET.\r | |
202 | \r | |
203 | @param[in] HiiHandle A handle that was previously registered in the HII Database.\r | |
204 | @param[in] StringId The identifier of the string to retrieved from the string \r | |
205 | package associated with HiiHandle.\r | |
206 | @param[in] Language The language of the string to retrieve. If this parameter \r | |
207 | is NULL, then the current platform language is used. The \r | |
208 | format of Language must follow the language format assumed \r | |
209 | the HII Database.\r | |
210 | \r | |
211 | @retval NULL The string specified by StringId is not present in the string package.\r | |
212 | @retval Other The string was returned.\r | |
08e4b3cf | 213 | \r |
214 | **/\r | |
cb7d01c0 | 215 | EFI_STRING\r |
08e4b3cf | 216 | EFIAPI\r |
cb7d01c0 | 217 | HiiGetString (\r |
218 | IN EFI_HII_HANDLE HiiHandle,\r | |
219 | IN EFI_STRING_ID StringId,\r | |
220 | IN CONST CHAR8 *Language OPTIONAL\r | |
08e4b3cf | 221 | )\r |
222 | {\r | |
223 | EFI_STATUS Status;\r | |
cb7d01c0 | 224 | UINTN StringSize;\r |
225 | CHAR16 TempString;\r | |
226 | EFI_STRING String;\r | |
227 | CHAR8 *SupportedLanguages;\r | |
228 | CHAR8 *PlatformLanguage;\r | |
ea7cd3ec | 229 | CHAR8 *BestLanguage;\r |
08e4b3cf | 230 | \r |
cb7d01c0 | 231 | ASSERT (HiiHandle != NULL);\r |
232 | ASSERT (StringId != 0);\r | |
08e4b3cf | 233 | \r |
cb7d01c0 | 234 | //\r |
235 | // Initialize all allocated buffers to NULL\r | |
236 | // \r | |
237 | SupportedLanguages = NULL;\r | |
238 | PlatformLanguage = NULL;\r | |
239 | BestLanguage = NULL;\r | |
240 | String = NULL;\r | |
08e4b3cf | 241 | \r |
cb7d01c0 | 242 | //\r |
243 | // Get the languages that the package specified by HiiHandle supports\r | |
244 | //\r | |
245 | SupportedLanguages = HiiGetSupportedLanguages (HiiHandle);\r | |
246 | if (SupportedLanguages == NULL) {\r | |
247 | goto Error;\r | |
08e4b3cf | 248 | }\r |
249 | \r | |
cb7d01c0 | 250 | //\r |
251 | // Get the current platform language setting\r | |
252 | //\r | |
f01b91ae | 253 | GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r |
08e4b3cf | 254 | \r |
cb7d01c0 | 255 | //\r |
256 | // If Languag is NULL, then set it to an empty string, so it will be \r | |
257 | // skipped by GetBestLanguage()\r | |
258 | //\r | |
259 | if (Language == NULL) {\r | |
260 | Language = "";\r | |
261 | }\r | |
08e4b3cf | 262 | \r |
cb7d01c0 | 263 | //\r |
264 | // Get the best matching language from SupportedLanguages\r | |
265 | //\r | |
266 | BestLanguage = GetBestLanguage (\r | |
267 | SupportedLanguages, \r | |
268 | FALSE, // RFC 4646 mode\r | |
269 | Language, // Highest priority \r | |
270 | PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority\r | |
271 | SupportedLanguages, // Lowest priority \r | |
272 | NULL\r | |
273 | );\r | |
274 | if (BestLanguage == NULL) {\r | |
275 | goto Error;\r | |
276 | }\r | |
08e4b3cf | 277 | \r |
cb7d01c0 | 278 | //\r |
279 | // Retrieve the size of the string in the string package for the BestLanguage\r | |
280 | //\r | |
281 | StringSize = 0;\r | |
282 | Status = gHiiString->GetString (\r | |
283 | gHiiString,\r | |
284 | BestLanguage,\r | |
285 | HiiHandle,\r | |
286 | StringId,\r | |
287 | &TempString,\r | |
288 | &StringSize,\r | |
289 | NULL\r | |
290 | );\r | |
291 | //\r | |
292 | // If GetString() returns EFI_SUCCESS for a zero size, \r | |
293 | // then there are no supported languages registered for HiiHandle. If GetString() \r | |
294 | // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r | |
295 | // in the HII Database\r | |
296 | //\r | |
297 | if (Status != EFI_BUFFER_TOO_SMALL) {\r | |
298 | goto Error;\r | |
299 | }\r | |
08e4b3cf | 300 | \r |
cb7d01c0 | 301 | //\r |
302 | // Allocate a buffer for the return string\r | |
303 | //\r | |
304 | String = AllocateZeroPool (StringSize);\r | |
305 | if (String == NULL) {\r | |
306 | goto Error;\r | |
307 | }\r | |
08e4b3cf | 308 | \r |
cb7d01c0 | 309 | //\r |
310 | // Retrieve the string from the string package\r | |
311 | //\r | |
312 | Status = gHiiString->GetString (\r | |
313 | gHiiString,\r | |
314 | BestLanguage,\r | |
315 | HiiHandle,\r | |
316 | StringId,\r | |
317 | String,\r | |
318 | &StringSize,\r | |
319 | NULL\r | |
320 | );\r | |
321 | if (EFI_ERROR (Status)) {\r | |
322 | //\r | |
323 | // Free the buffer and return NULL if the supported languages can not be retrieved.\r | |
324 | //\r | |
325 | FreePool (String);\r | |
326 | String = NULL;\r | |
08e4b3cf | 327 | }\r |
328 | \r | |
cb7d01c0 | 329 | Error:\r |
330 | //\r | |
331 | // Free allocated buffers\r | |
332 | //\r | |
333 | if (SupportedLanguages != NULL) {\r | |
334 | FreePool (SupportedLanguages);\r | |
335 | }\r | |
336 | if (PlatformLanguage != NULL) {\r | |
337 | FreePool (PlatformLanguage);\r | |
338 | }\r | |
339 | if (BestLanguage != NULL) {\r | |
340 | FreePool (BestLanguage);\r | |
08e4b3cf | 341 | }\r |
342 | \r | |
cb7d01c0 | 343 | //\r |
344 | // Return the Null-terminated Unicode string\r | |
345 | //\r | |
346 | return String;\r | |
08e4b3cf | 347 | }\r |
348 | \r |