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 if (*Languages
== ';') {
147 #else // LANGUAGE_ISO_639_2
148 return (Languages
+ 3);
153 EfiLibLookupUnicodeString (
155 IN CHAR8
*SupportedLanguages
,
156 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
157 OUT CHAR16
**UnicodeString
163 Translate a unicode string to a specified language if supported.
167 Language - The name of language to translate to
168 SupportedLanguages - Supported languages set
169 UnicodeStringTable - Pointer of one item in translation dictionary
170 UnicodeString - The translated string
174 EFI_INVALID_PARAMETER - Invalid parameter
175 EFI_UNSUPPORTED - System not supported this language or this string translation
176 EFI_SUCCESS - String successfully translated
181 // Make sure the parameters are valid
183 if (Language
== NULL
|| UnicodeString
== NULL
) {
184 return EFI_INVALID_PARAMETER
;
188 // If there are no supported languages, or the Unicode String Table is empty, then the
189 // Unicode String specified by Language is not supported by this Unicode String Table
191 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
192 return EFI_UNSUPPORTED
;
196 // Make sure Language is in the set of Supported Languages
198 while (*SupportedLanguages
!= 0) {
199 if (EfiLibCompareLanguage (Language
, SupportedLanguages
)) {
202 // Search the Unicode String Table for the matching Language specifier
204 while (UnicodeStringTable
->Language
!= NULL
) {
205 if (EfiLibCompareLanguage (Language
, UnicodeStringTable
->Language
)) {
208 // A matching string was found, so return it
210 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
214 UnicodeStringTable
++;
217 return EFI_UNSUPPORTED
;
220 SupportedLanguages
= NextSupportedLanguage(SupportedLanguages
);
223 return EFI_UNSUPPORTED
;
227 EfiLibAddUnicodeString (
229 IN CHAR8
*SupportedLanguages
,
230 IN OUT EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
231 IN CHAR16
*UnicodeString
237 Add an translation to the dictionary if this language if supported.
241 Language - The name of language to translate to
242 SupportedLanguages - Supported languages set
243 UnicodeStringTable - Translation dictionary
244 UnicodeString - The corresponding string for the language to be translated to
248 EFI_INVALID_PARAMETER - Invalid parameter
249 EFI_UNSUPPORTED - System not supported this language
250 EFI_ALREADY_STARTED - Already has a translation item of this language
251 EFI_OUT_OF_RESOURCES - No enough buffer to be allocated
252 EFI_SUCCESS - String successfully translated
256 UINTN NumberOfEntries
;
257 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
258 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
259 UINTN UnicodeStringLength
;
262 // Make sure the parameter are valid
264 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
265 return EFI_INVALID_PARAMETER
;
269 // If there are no supported languages, then a Unicode String can not be added
271 if (SupportedLanguages
== NULL
) {
272 return EFI_UNSUPPORTED
;
276 // If the Unicode String is empty, then a Unicode String can not be added
278 if (UnicodeString
[0] == 0) {
279 return EFI_INVALID_PARAMETER
;
283 // Make sure Language is a member of SupportedLanguages
285 while (*SupportedLanguages
!= 0) {
286 if (EfiLibCompareLanguage (Language
, SupportedLanguages
)) {
289 // Determine the size of the Unicode String Table by looking for a NULL Language entry
292 if (*UnicodeStringTable
!= NULL
) {
293 OldUnicodeStringTable
= *UnicodeStringTable
;
294 while (OldUnicodeStringTable
->Language
!= NULL
) {
295 if (EfiLibCompareLanguage (Language
, OldUnicodeStringTable
->Language
)) {
296 return EFI_ALREADY_STARTED
;
299 OldUnicodeStringTable
++;
305 // Allocate space for a new Unicode String Table. It must hold the current number of
306 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
309 NewUnicodeStringTable
= EfiLibAllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
310 if (NewUnicodeStringTable
== NULL
) {
311 return EFI_OUT_OF_RESOURCES
;
315 // If the current Unicode String Table contains any entries, then copy them to the
316 // newly allocated Unicode String Table.
318 if (*UnicodeStringTable
!= NULL
) {
320 NewUnicodeStringTable
,
322 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
327 // Allocate space for a copy of the Language specifier
329 NewUnicodeStringTable
[NumberOfEntries
].Language
= EfiLibAllocateCopyPool (EfiAsciiStrLen(Language
) + 1, Language
);
330 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
331 gBS
->FreePool (NewUnicodeStringTable
);
332 return EFI_OUT_OF_RESOURCES
;
336 // Compute the length of the Unicode String
338 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
342 // Allocate space for a copy of the Unicode String
344 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= EfiLibAllocateCopyPool (
345 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
348 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
349 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
350 gBS
->FreePool (NewUnicodeStringTable
);
351 return EFI_OUT_OF_RESOURCES
;
355 // Mark the end of the Unicode String Table
357 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
358 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
361 // Free the old Unicode String Table
363 if (*UnicodeStringTable
!= NULL
) {
364 gBS
->FreePool (*UnicodeStringTable
);
368 // Point UnicodeStringTable at the newly allocated Unicode String Table
370 *UnicodeStringTable
= NewUnicodeStringTable
;
375 SupportedLanguages
= NextSupportedLanguage(SupportedLanguages
);
378 return EFI_UNSUPPORTED
;
382 EfiLibFreeUnicodeStringTable (
383 IN OUT EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
393 UnicodeStringTable - The string table to be freed.
397 EFI_SUCCESS - The table successfully freed.
404 // If the Unicode String Table is NULL, then it is already freed
406 if (UnicodeStringTable
== NULL
) {
411 // Loop through the Unicode String Table until we reach the end of table marker
413 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
416 // Free the Language string from the Unicode String Table
418 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
421 // Free the Unicode String from the Unicode String Table
423 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
424 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
429 // Free the Unicode String Table itself
431 gBS
->FreePool (UnicodeStringTable
);