3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Light weight lib to support EFI drivers.
23 #include "EfiDriverLib.h"
26 // Global Interface for Debug Mask Protocol
28 EFI_DEBUG_MASK_PROTOCOL
*gDebugMaskInterface
= NULL
;
31 EfiInitializeDriverLib (
32 IN EFI_HANDLE ImageHandle
,
33 IN EFI_SYSTEM_TABLE
*SystemTable
39 Intialize Driver Lib if it has not yet been initialized.
43 ImageHandle - Standard EFI Image entry parameter
45 SystemTable - Standard EFI Image entry parameter
49 EFI_STATUS always returns EFI_SUCCESS
57 gBS
= gST
->BootServices
;
58 gRT
= gST
->RuntimeServices
;
64 // Get driver debug mask protocol interface
69 &gEfiDebugMaskProtocolGuid
,
70 (VOID
*) &gDebugMaskInterface
74 // Should be at EFI_D_INFO, but lets us know things are running
76 DEBUG ((EFI_D_INFO
, "EfiInitializeDriverLib: Started\n"));
82 EfiLibCompareLanguage (
90 Compare whether two names of languages are identical.
94 Language1 - Name of language 1
95 Language2 - Name of language 2
106 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
107 for (Index
= 0; (Language1
[Index
] != 0) && (Language2
[Index
] != 0); Index
++) {
108 if (Language1
[Index
] != Language2
[Index
]) {
113 if (((Language1
[Index
] == 0) && (Language2
[Index
] == 0)) ||
114 ((Language1
[Index
] == 0) && (Language2
[Index
] != ';')) ||
115 ((Language1
[Index
] == ';') && (Language2
[Index
] != 0)) ||
116 ((Language1
[Index
] == ';') && (Language2
[Index
] != ';'))) {
122 for (Index
= 0; Index
< 3; Index
++) {
123 if (Language1
[Index
] != Language2
[Index
]) {
134 NextSupportedLanguage (
138 #ifdef LANGUAGE_RFC_3066 // LANGUAGE_RFC_3066
139 for (; (*Languages
!= 0) && (*Languages
!= ';'); Languages
++)
142 #else // LANGUAGE_ISO_639_2
143 return (Languages
+ 3);
148 EfiLibLookupUnicodeString (
150 IN CHAR8
*SupportedLanguages
,
151 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
152 OUT CHAR16
**UnicodeString
158 Translate a unicode string to a specified language if supported.
162 Language - The name of language to translate to
163 SupportedLanguages - Supported languages set
164 UnicodeStringTable - Pointer of one item in translation dictionary
165 UnicodeString - The translated string
169 EFI_INVALID_PARAMETER - Invalid parameter
170 EFI_UNSUPPORTED - System not supported this language or this string translation
171 EFI_SUCCESS - String successfully translated
176 // Make sure the parameters are valid
178 if (Language
== NULL
|| UnicodeString
== NULL
) {
179 return EFI_INVALID_PARAMETER
;
183 // If there are no supported languages, or the Unicode String Table is empty, then the
184 // Unicode String specified by Language is not supported by this Unicode String Table
186 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
187 return EFI_UNSUPPORTED
;
191 // Make sure Language is in the set of Supported Languages
193 while (*SupportedLanguages
!= 0) {
194 if (EfiLibCompareLanguage (Language
, SupportedLanguages
)) {
197 // Search the Unicode String Table for the matching Language specifier
199 while (UnicodeStringTable
->Language
!= NULL
) {
200 if (EfiLibCompareLanguage (Language
, UnicodeStringTable
->Language
)) {
203 // A matching string was found, so return it
205 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
209 UnicodeStringTable
++;
212 return EFI_UNSUPPORTED
;
215 SupportedLanguages
= NextSupportedLanguage(SupportedLanguages
);
218 return EFI_UNSUPPORTED
;
222 EfiLibAddUnicodeString (
224 IN CHAR8
*SupportedLanguages
,
225 IN OUT EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
226 IN CHAR16
*UnicodeString
232 Add an translation to the dictionary if this language if supported.
236 Language - The name of language to translate to
237 SupportedLanguages - Supported languages set
238 UnicodeStringTable - Translation dictionary
239 UnicodeString - The corresponding string for the language to be translated to
243 EFI_INVALID_PARAMETER - Invalid parameter
244 EFI_UNSUPPORTED - System not supported this language
245 EFI_ALREADY_STARTED - Already has a translation item of this language
246 EFI_OUT_OF_RESOURCES - No enough buffer to be allocated
247 EFI_SUCCESS - String successfully translated
251 UINTN NumberOfEntries
;
252 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
253 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
254 UINTN UnicodeStringLength
;
257 // Make sure the parameter are valid
259 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
260 return EFI_INVALID_PARAMETER
;
264 // If there are no supported languages, then a Unicode String can not be added
266 if (SupportedLanguages
== NULL
) {
267 return EFI_UNSUPPORTED
;
271 // If the Unicode String is empty, then a Unicode String can not be added
273 if (UnicodeString
[0] == 0) {
274 return EFI_INVALID_PARAMETER
;
278 // Make sure Language is a member of SupportedLanguages
280 while (*SupportedLanguages
!= 0) {
281 if (EfiLibCompareLanguage (Language
, SupportedLanguages
)) {
284 // Determine the size of the Unicode String Table by looking for a NULL Language entry
287 if (*UnicodeStringTable
!= NULL
) {
288 OldUnicodeStringTable
= *UnicodeStringTable
;
289 while (OldUnicodeStringTable
->Language
!= NULL
) {
290 if (EfiLibCompareLanguage (Language
, OldUnicodeStringTable
->Language
)) {
291 return EFI_ALREADY_STARTED
;
294 OldUnicodeStringTable
++;
300 // Allocate space for a new Unicode String Table. It must hold the current number of
301 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
304 NewUnicodeStringTable
= EfiLibAllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
305 if (NewUnicodeStringTable
== NULL
) {
306 return EFI_OUT_OF_RESOURCES
;
310 // If the current Unicode String Table contains any entries, then copy them to the
311 // newly allocated Unicode String Table.
313 if (*UnicodeStringTable
!= NULL
) {
315 NewUnicodeStringTable
,
317 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
322 // Allocate space for a copy of the Language specifier
324 NewUnicodeStringTable
[NumberOfEntries
].Language
= EfiLibAllocateCopyPool (EfiAsciiStrLen(Language
) + 1, Language
);
325 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
326 gBS
->FreePool (NewUnicodeStringTable
);
327 return EFI_OUT_OF_RESOURCES
;
331 // Compute the length of the Unicode String
333 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
337 // Allocate space for a copy of the Unicode String
339 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= EfiLibAllocateCopyPool (
340 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
343 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
344 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
345 gBS
->FreePool (NewUnicodeStringTable
);
346 return EFI_OUT_OF_RESOURCES
;
350 // Mark the end of the Unicode String Table
352 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
353 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
356 // Free the old Unicode String Table
358 if (*UnicodeStringTable
!= NULL
) {
359 gBS
->FreePool (*UnicodeStringTable
);
363 // Point UnicodeStringTable at the newly allocated Unicode String Table
365 *UnicodeStringTable
= NewUnicodeStringTable
;
370 SupportedLanguages
= NextSupportedLanguage(SupportedLanguages
);
373 return EFI_UNSUPPORTED
;
377 EfiLibFreeUnicodeStringTable (
378 IN OUT EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
388 UnicodeStringTable - The string table to be freed.
392 EFI_SUCCESS - The table successfully freed.
399 // If the Unicode String Table is NULL, then it is already freed
401 if (UnicodeStringTable
== NULL
) {
406 // Loop through the Unicode String Table until we reach the end of table marker
408 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
411 // Free the Language string from the Unicode String Table
413 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
416 // Free the Unicode String from the Unicode String Table
418 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
419 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
424 // Free the Unicode String Table itself
426 gBS
->FreePool (UnicodeStringTable
);