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