]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiDriverLib/EfiDriverLib.c
9add9e7a56042482b903919a3d0a4c28b79d94b8
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Dxe / EfiDriverLib / EfiDriverLib.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 EfiDriverLib.c
15
16 Abstract:
17
18 Light weight lib to support EFI drivers.
19
20 --*/
21
22 #include "Tiano.h"
23 #include "EfiDriverLib.h"
24
25 //
26 // Global Interface for Debug Mask Protocol
27 //
28 EFI_DEBUG_MASK_PROTOCOL *gDebugMaskInterface = NULL;
29
30 EFI_STATUS
31 EfiInitializeDriverLib (
32 IN EFI_HANDLE ImageHandle,
33 IN EFI_SYSTEM_TABLE *SystemTable
34 )
35 /*++
36
37 Routine Description:
38
39 Intialize Driver Lib if it has not yet been initialized.
40
41 Arguments:
42
43 ImageHandle - Standard EFI Image entry parameter
44
45 SystemTable - Standard EFI Image entry parameter
46
47 Returns:
48
49 EFI_STATUS always returns EFI_SUCCESS
50
51 --*/
52 {
53 gST = SystemTable;
54
55 ASSERT (gST != NULL);
56
57 gBS = gST->BootServices;
58 gRT = gST->RuntimeServices;
59
60 ASSERT (gBS != NULL);
61 ASSERT (gRT != NULL);
62
63 //
64 // Get driver debug mask protocol interface
65 //
66 #ifdef EFI_DEBUG
67 gBS->HandleProtocol (
68 ImageHandle,
69 &gEfiDebugMaskProtocolGuid,
70 (VOID *) &gDebugMaskInterface
71 );
72 #endif
73 //
74 // Should be at EFI_D_INFO, but lets us know things are running
75 //
76 DEBUG ((EFI_D_INFO, "EfiInitializeDriverLib: Started\n"));
77
78 return EFI_SUCCESS;
79 }
80
81 BOOLEAN
82 EfiLibCompareLanguage (
83 IN CHAR8 *Language1,
84 IN CHAR8 *Language2
85 )
86 /*++
87
88 Routine Description:
89
90 Compare whether two names of languages are identical.
91
92 Arguments:
93
94 Language1 - Name of language 1
95 Language2 - Name of language 2
96
97 Returns:
98
99 TRUE - same
100 FALSE - not same
101
102 --*/
103 {
104 UINTN Index;
105
106 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
107 for (Index = 0; (Language1[Index] != 0) && (Language2[Index] != 0); Index++) {
108 if (Language1[Index] != Language2[Index]) {
109 return FALSE;
110 }
111 }
112
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] != ';'))) {
117 return TRUE;
118 }
119
120 return FALSE;
121 #else
122 for (Index = 0; Index < 3; Index++) {
123 if (Language1[Index] != Language2[Index]) {
124 return FALSE;
125 }
126 }
127
128 return TRUE;
129 #endif
130 }
131
132 STATIC
133 CHAR8 *
134 NextSupportedLanguage (
135 IN CHAR8 *Languages
136 )
137 {
138 #ifdef LANGUAGE_RFC_3066 // LANGUAGE_RFC_3066
139 for (; (*Languages != 0) && (*Languages != ';'); Languages++)
140 ;
141
142 if (*Languages == ';') {
143 Languages++;
144 }
145
146 return Languages;
147 #else // LANGUAGE_ISO_639_2
148 return (Languages + 3);
149 #endif
150 }
151
152 EFI_STATUS
153 EfiLibLookupUnicodeString (
154 IN CHAR8 *Language,
155 IN CHAR8 *SupportedLanguages,
156 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
157 OUT CHAR16 **UnicodeString
158 )
159 /*++
160
161 Routine Description:
162
163 Translate a unicode string to a specified language if supported.
164
165 Arguments:
166
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
171
172 Returns:
173
174 EFI_INVALID_PARAMETER - Invalid parameter
175 EFI_UNSUPPORTED - System not supported this language or this string translation
176 EFI_SUCCESS - String successfully translated
177
178 --*/
179 {
180 //
181 // Make sure the parameters are valid
182 //
183 if (Language == NULL || UnicodeString == NULL) {
184 return EFI_INVALID_PARAMETER;
185 }
186
187 //
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
190 //
191 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
192 return EFI_UNSUPPORTED;
193 }
194
195 //
196 // Make sure Language is in the set of Supported Languages
197 //
198 while (*SupportedLanguages != 0) {
199 if (EfiLibCompareLanguage (Language, SupportedLanguages)) {
200
201 //
202 // Search the Unicode String Table for the matching Language specifier
203 //
204 while (UnicodeStringTable->Language != NULL) {
205 if (EfiLibCompareLanguage (Language, UnicodeStringTable->Language)) {
206
207 //
208 // A matching string was found, so return it
209 //
210 *UnicodeString = UnicodeStringTable->UnicodeString;
211 return EFI_SUCCESS;
212 }
213
214 UnicodeStringTable++;
215 }
216
217 return EFI_UNSUPPORTED;
218 }
219
220 SupportedLanguages = NextSupportedLanguage(SupportedLanguages);
221 }
222
223 return EFI_UNSUPPORTED;
224 }
225
226 EFI_STATUS
227 EfiLibAddUnicodeString (
228 IN CHAR8 *Language,
229 IN CHAR8 *SupportedLanguages,
230 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
231 IN CHAR16 *UnicodeString
232 )
233 /*++
234
235 Routine Description:
236
237 Add an translation to the dictionary if this language if supported.
238
239 Arguments:
240
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
245
246 Returns:
247
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
253
254 --*/
255 {
256 UINTN NumberOfEntries;
257 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
258 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
259 UINTN UnicodeStringLength;
260
261 //
262 // Make sure the parameter are valid
263 //
264 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
265 return EFI_INVALID_PARAMETER;
266 }
267
268 //
269 // If there are no supported languages, then a Unicode String can not be added
270 //
271 if (SupportedLanguages == NULL) {
272 return EFI_UNSUPPORTED;
273 }
274
275 //
276 // If the Unicode String is empty, then a Unicode String can not be added
277 //
278 if (UnicodeString[0] == 0) {
279 return EFI_INVALID_PARAMETER;
280 }
281
282 //
283 // Make sure Language is a member of SupportedLanguages
284 //
285 while (*SupportedLanguages != 0) {
286 if (EfiLibCompareLanguage (Language, SupportedLanguages)) {
287
288 //
289 // Determine the size of the Unicode String Table by looking for a NULL Language entry
290 //
291 NumberOfEntries = 0;
292 if (*UnicodeStringTable != NULL) {
293 OldUnicodeStringTable = *UnicodeStringTable;
294 while (OldUnicodeStringTable->Language != NULL) {
295 if (EfiLibCompareLanguage (Language, OldUnicodeStringTable->Language)) {
296 return EFI_ALREADY_STARTED;
297 }
298
299 OldUnicodeStringTable++;
300 NumberOfEntries++;
301 }
302 }
303
304 //
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
307 // marker
308 //
309 NewUnicodeStringTable = EfiLibAllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
310 if (NewUnicodeStringTable == NULL) {
311 return EFI_OUT_OF_RESOURCES;
312 }
313
314 //
315 // If the current Unicode String Table contains any entries, then copy them to the
316 // newly allocated Unicode String Table.
317 //
318 if (*UnicodeStringTable != NULL) {
319 EfiCopyMem (
320 NewUnicodeStringTable,
321 *UnicodeStringTable,
322 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
323 );
324 }
325
326 //
327 // Allocate space for a copy of the Language specifier
328 //
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;
333 }
334
335 //
336 // Compute the length of the Unicode String
337 //
338 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
339 ;
340
341 //
342 // Allocate space for a copy of the Unicode String
343 //
344 NewUnicodeStringTable[NumberOfEntries].UnicodeString = EfiLibAllocateCopyPool (
345 (UnicodeStringLength + 1) * sizeof (CHAR16),
346 UnicodeString
347 );
348 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
349 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
350 gBS->FreePool (NewUnicodeStringTable);
351 return EFI_OUT_OF_RESOURCES;
352 }
353
354 //
355 // Mark the end of the Unicode String Table
356 //
357 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
358 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
359
360 //
361 // Free the old Unicode String Table
362 //
363 if (*UnicodeStringTable != NULL) {
364 gBS->FreePool (*UnicodeStringTable);
365 }
366
367 //
368 // Point UnicodeStringTable at the newly allocated Unicode String Table
369 //
370 *UnicodeStringTable = NewUnicodeStringTable;
371
372 return EFI_SUCCESS;
373 }
374
375 SupportedLanguages = NextSupportedLanguage(SupportedLanguages);
376 }
377
378 return EFI_UNSUPPORTED;
379 }
380
381 EFI_STATUS
382 EfiLibFreeUnicodeStringTable (
383 IN OUT EFI_UNICODE_STRING_TABLE *UnicodeStringTable
384 )
385 /*++
386
387 Routine Description:
388
389 Free a string table.
390
391 Arguments:
392
393 UnicodeStringTable - The string table to be freed.
394
395 Returns:
396
397 EFI_SUCCESS - The table successfully freed.
398
399 --*/
400 {
401 UINTN Index;
402
403 //
404 // If the Unicode String Table is NULL, then it is already freed
405 //
406 if (UnicodeStringTable == NULL) {
407 return EFI_SUCCESS;
408 }
409
410 //
411 // Loop through the Unicode String Table until we reach the end of table marker
412 //
413 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
414
415 //
416 // Free the Language string from the Unicode String Table
417 //
418 gBS->FreePool (UnicodeStringTable[Index].Language);
419
420 //
421 // Free the Unicode String from the Unicode String Table
422 //
423 if (UnicodeStringTable[Index].UnicodeString != NULL) {
424 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
425 }
426 }
427
428 //
429 // Free the Unicode String Table itself
430 //
431 gBS->FreePool (UnicodeStringTable);
432
433 return EFI_SUCCESS;
434 }