Refix the previous issue.
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
1 /** @file
2 Mde UEFI library functions.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: UefiLib.c
14
15 **/
16
17 /**
18 Compare whether two names of languages are identical.
19
20 @param Language1 Name of language 1.
21 @param Language2 Name of language 2.
22
23 @retval TRUE Language 1 and language 2 are the same.
24 @retval FALSE Language 1 and language 2 are not the same.
25
26 **/
27 BOOLEAN
28 CompareIso639LanguageCode (
29 IN CONST CHAR8 *Language1,
30 IN CONST CHAR8 *Language2
31 )
32 {
33 return (BOOLEAN) (ReadUnaligned24 ((CONST UINT32 *) Language1) == ReadUnaligned24 ((CONST UINT32 *) Language2));
34 }
35
36 /**
37 This function searches the list of configuration tables stored in the EFI System
38 Table for a table with a GUID that matches TableGuid. If a match is found,
39 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
40 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
41
42 @param TableGuid Pointer to table's GUID type..
43 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
44
45 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
46 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 EfiGetSystemConfigurationTable (
52 IN EFI_GUID *TableGuid,
53 OUT VOID **Table
54 )
55 {
56 EFI_SYSTEM_TABLE *SystemTable;
57 UINTN Index;
58
59 ASSERT (TableGuid != NULL);
60 ASSERT (Table != NULL);
61
62 SystemTable = gST;
63 *Table = NULL;
64 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {
65 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {
66 *Table = SystemTable->ConfigurationTable[Index].VendorTable;
67 return EFI_SUCCESS;
68 }
69 }
70
71 return EFI_NOT_FOUND;
72 }
73
74 /**
75 This function causes the notification function to be executed for every protocol
76 of type ProtocolGuid instance that exists in the system when this function is
77 invoked. In addition, every time a protocol of type ProtocolGuid instance is
78 installed or reinstalled, the notification function is also executed.
79
80 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
81 @param NotifyTpl Supplies the task priority level of the event notifications.
82 @param NotifyFunction Supplies the function to notify when the event is signaled.
83 @param NotifyContext The context parameter to pass to NotifyFunction.
84 @param Registration A pointer to a memory location to receive the registration value.
85
86 @return The notification event that was created.
87
88 **/
89 EFI_EVENT
90 EFIAPI
91 EfiCreateProtocolNotifyEvent(
92 IN EFI_GUID *ProtocolGuid,
93 IN EFI_TPL NotifyTpl,
94 IN EFI_EVENT_NOTIFY NotifyFunction,
95 IN VOID *NotifyContext, OPTIONAL
96 OUT VOID **Registration
97 )
98 {
99 EFI_STATUS Status;
100 EFI_EVENT Event;
101
102 //
103 // Create the event
104 //
105
106 Status = gBS->CreateEvent (
107 EFI_EVENT_NOTIFY_SIGNAL,
108 NotifyTpl,
109 NotifyFunction,
110 NotifyContext,
111 &Event
112 );
113 ASSERT_EFI_ERROR (Status);
114
115 //
116 // Register for protocol notifactions on this event
117 //
118
119 Status = gBS->RegisterProtocolNotify (
120 ProtocolGuid,
121 Event,
122 Registration
123 );
124
125 ASSERT_EFI_ERROR (Status);
126
127 //
128 // Kick the event so we will perform an initial pass of
129 // current installed drivers
130 //
131
132 gBS->SignalEvent (Event);
133 return Event;
134 }
135
136 /**
137 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
138 This event is signaled with EfiNamedEventSignal(). This provide the ability for
139 one or more listeners on the same event named by the GUID specified by Name.
140
141 @param Name Supplies GUID name of the event.
142 @param NotifyTpl Supplies the task priority level of the event notifications.
143 @param NotifyFunction Supplies the function to notify when the event is signaled.
144 @param NotifyContext The context parameter to pass to NotifyFunction.
145 @param Registration A pointer to a memory location to receive the registration value.
146
147 @retval EFI_SUCCESS A named event was created.
148 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
149
150 **/
151 EFI_STATUS
152 EFIAPI
153 EfiNamedEventListen (
154 IN CONST EFI_GUID *Name,
155 IN EFI_TPL NotifyTpl,
156 IN EFI_EVENT_NOTIFY NotifyFunction,
157 IN CONST VOID *NotifyContext, OPTIONAL
158 OUT VOID *Registration OPTIONAL
159 )
160 {
161 EFI_STATUS Status;
162 EFI_EVENT Event;
163 VOID *RegistrationLocal;
164
165 //
166 // Create event
167 //
168 Status = gBS->CreateEvent (
169 EFI_EVENT_NOTIFY_SIGNAL,
170 NotifyTpl,
171 NotifyFunction,
172 (VOID *) NotifyContext,
173 &Event
174 );
175 ASSERT_EFI_ERROR (Status);
176
177 //
178 // The Registration is not optional to RegisterProtocolNotify().
179 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
180 //
181 if (Registration != NULL) {
182 RegistrationLocal = Registration;
183 } else {
184 RegistrationLocal = &RegistrationLocal;
185 }
186
187 //
188 // Register for an installation of protocol interface
189 //
190
191 Status = gBS->RegisterProtocolNotify (
192 (EFI_GUID *) Name,
193 Event,
194 RegistrationLocal
195 );
196 ASSERT_EFI_ERROR (Status);
197
198 return EFI_SUCCESS;
199 }
200
201 /**
202 This function signals the named event specified by Name. The named event must
203 have been created with EfiNamedEventListen().
204
205 @param Name Supplies GUID name of the event.
206
207 @retval EFI_SUCCESS A named event was signaled.
208 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
209
210 **/
211 EFI_STATUS
212 EFIAPI
213 EfiNamedEventSignal (
214 IN CONST EFI_GUID *Name
215 )
216 {
217 EFI_STATUS Status;
218 EFI_HANDLE Handle;
219
220 Handle = NULL;
221 Status = gBS->InstallProtocolInterface (
222 &Handle,
223 (EFI_GUID *) Name,
224 EFI_NATIVE_INTERFACE,
225 NULL
226 );
227 ASSERT_EFI_ERROR (Status);
228
229 Status = gBS->UninstallProtocolInterface (
230 Handle,
231 (EFI_GUID *) Name,
232 NULL
233 );
234 ASSERT_EFI_ERROR (Status);
235
236 return EFI_SUCCESS;
237 }
238
239
240 /**
241 This function initializes a basic mutual exclusion lock to the released state
242 and returns the lock. Each lock provides mutual exclusion access at its task
243 priority level. Since there is no preemption or multiprocessor support in EFI,
244 acquiring the lock only consists of raising to the locks TPL.
245
246 @param Lock A pointer to the lock data structure to initialize.
247 @param Priority EFI TPL associated with the lock.
248
249 @return The lock.
250
251 **/
252 EFI_LOCK *
253 EFIAPI
254 EfiInitializeLock (
255 IN OUT EFI_LOCK *Lock,
256 IN EFI_TPL Priority
257 )
258 {
259 ASSERT (Lock != NULL);
260 ASSERT (Priority <= EFI_TPL_HIGH_LEVEL);
261
262 Lock->Tpl = Priority;
263 Lock->OwnerTpl = EFI_TPL_APPLICATION;
264 Lock->Lock = EfiLockReleased ;
265 return Lock;
266 }
267
268 /**
269 This function raises the system's current task priority level to the task
270 priority level of the mutual exclusion lock. Then, it places the lock in the
271 acquired state.
272
273 @param Priority The task priority level of the lock.
274
275 **/
276 VOID
277 EFIAPI
278 EfiAcquireLock (
279 IN EFI_LOCK *Lock
280 )
281 {
282 ASSERT (Lock != NULL);
283 ASSERT (Lock->Lock == EfiLockReleased);
284
285 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
286 Lock->Lock = EfiLockAcquired;
287 }
288
289 /**
290 This function raises the system's current task priority level to the task
291 priority level of the mutual exclusion lock. Then, it attempts to place the
292 lock in the acquired state.
293
294 @param Lock A pointer to the lock to acquire.
295
296 @retval EFI_SUCCESS The lock was acquired.
297 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
298
299 **/
300 EFI_STATUS
301 EFIAPI
302 EfiAcquireLockOrFail (
303 IN EFI_LOCK *Lock
304 )
305 {
306
307 ASSERT (Lock != NULL);
308 ASSERT (Lock->Lock != EfiLockUninitialized);
309
310 if (Lock->Lock == EfiLockAcquired) {
311 //
312 // Lock is already owned, so bail out
313 //
314 return EFI_ACCESS_DENIED;
315 }
316
317 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
318
319 Lock->Lock = EfiLockAcquired;
320
321 return EFI_SUCCESS;
322 }
323
324 /**
325 This function transitions a mutual exclusion lock from the acquired state to
326 the released state, and restores the system's task priority level to its
327 previous level.
328
329 @param Lock A pointer to the lock to release.
330
331 **/
332 VOID
333 EFIAPI
334 EfiReleaseLock (
335 IN EFI_LOCK *Lock
336 )
337 {
338 EFI_TPL Tpl;
339
340 ASSERT (Lock != NULL);
341 ASSERT (Lock->Lock == EfiLockAcquired);
342
343 Tpl = Lock->OwnerTpl;
344
345 Lock->Lock = EfiLockReleased;
346
347 gBS->RestoreTPL (Tpl);
348 }
349
350 /**
351 This function looks up a Unicode string in UnicodeStringTable. If Language is
352 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
353 that matches the language code specified by Language, then it is returned in
354 UnicodeString.
355
356 @param Language A pointer to the ISO 639-2 language code for the
357 Unicode string to look up and return.
358 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
359 that the Unicode string table supports. Language
360 must be a member of this set.
361 @param UnicodeStringTable A pointer to the table of Unicode strings.
362 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
363 that matches the language specified by Language.
364
365 @retval EFI_SUCCESS The Unicode string that matches the language
366 specified by Language was found
367 in the table of Unicoide strings UnicodeStringTable,
368 and it was returned in UnicodeString.
369 @retval EFI_INVALID_PARAMETER Language is NULL.
370 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
371 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
372 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
373 @retval EFI_UNSUPPORTED The language specified by Language is not a
374 member of SupportedLanguages.
375 @retval EFI_UNSUPPORTED The language specified by Language is not
376 supported by UnicodeStringTable.
377
378 **/
379 EFI_STATUS
380 EFIAPI
381 LookupUnicodeString (
382 IN CONST CHAR8 *Language,
383 IN CONST CHAR8 *SupportedLanguages,
384 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
385 OUT CHAR16 **UnicodeString
386 )
387 {
388 //
389 // Make sure the parameters are valid
390 //
391 if (Language == NULL || UnicodeString == NULL) {
392 return EFI_INVALID_PARAMETER;
393 }
394
395 //
396 // If there are no supported languages, or the Unicode String Table is empty, then the
397 // Unicode String specified by Language is not supported by this Unicode String Table
398 //
399 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
400 return EFI_UNSUPPORTED;
401 }
402
403 //
404 // Make sure Language is in the set of Supported Languages
405 //
406 while (*SupportedLanguages != 0) {
407 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
408
409 //
410 // Search the Unicode String Table for the matching Language specifier
411 //
412 while (UnicodeStringTable->Language != NULL) {
413 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {
414
415 //
416 // A matching string was found, so return it
417 //
418 *UnicodeString = UnicodeStringTable->UnicodeString;
419 return EFI_SUCCESS;
420 }
421
422 UnicodeStringTable++;
423 }
424
425 return EFI_UNSUPPORTED;
426 }
427
428 SupportedLanguages += 3;
429 }
430
431 return EFI_UNSUPPORTED;
432 }
433
434 /**
435 This function adds a Unicode string to UnicodeStringTable.
436 If Language is a member of SupportedLanguages then UnicodeString is added to
437 UnicodeStringTable. New buffers are allocated for both Language and
438 UnicodeString. The contents of Language and UnicodeString are copied into
439 these new buffers. These buffers are automatically freed when
440 FreeUnicodeStringTable() is called.
441
442 @param Language A pointer to the ISO 639-2 language code for the Unicode
443 string to add.
444 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
445 that the Unicode string table supports.
446 Language must be a member of this set.
447 @param UnicodeStringTable A pointer to the table of Unicode strings.
448 @param UnicodeString A pointer to the Unicode string to add.
449
450 @retval EFI_SUCCESS The Unicode string that matches the language
451 specified by Language was found in the table of
452 Unicode strings UnicodeStringTable, and it was
453 returned in UnicodeString.
454 @retval EFI_INVALID_PARAMETER Language is NULL.
455 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
456 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
457 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
458 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
459 already present in UnicodeStringTable.
460 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
461 Unicode string to UnicodeStringTable.
462 @retval EFI_UNSUPPORTED The language specified by Language is not a
463 member of SupportedLanguages.
464
465 **/
466 EFI_STATUS
467 EFIAPI
468 AddUnicodeString (
469 IN CONST CHAR8 *Language,
470 IN CONST CHAR8 *SupportedLanguages,
471 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
472 IN CONST CHAR16 *UnicodeString
473 )
474 {
475 UINTN NumberOfEntries;
476 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
477 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
478 UINTN UnicodeStringLength;
479
480 //
481 // Make sure the parameter are valid
482 //
483 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
484 return EFI_INVALID_PARAMETER;
485 }
486
487 //
488 // If there are no supported languages, then a Unicode String can not be added
489 //
490 if (SupportedLanguages == NULL) {
491 return EFI_UNSUPPORTED;
492 }
493
494 //
495 // If the Unicode String is empty, then a Unicode String can not be added
496 //
497 if (UnicodeString[0] == 0) {
498 return EFI_INVALID_PARAMETER;
499 }
500
501 //
502 // Make sure Language is a member of SupportedLanguages
503 //
504 while (*SupportedLanguages != 0) {
505 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
506
507 //
508 // Determine the size of the Unicode String Table by looking for a NULL Language entry
509 //
510 NumberOfEntries = 0;
511 if (*UnicodeStringTable != NULL) {
512 OldUnicodeStringTable = *UnicodeStringTable;
513 while (OldUnicodeStringTable->Language != NULL) {
514 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {
515 return EFI_ALREADY_STARTED;
516 }
517
518 OldUnicodeStringTable++;
519 NumberOfEntries++;
520 }
521 }
522
523 //
524 // Allocate space for a new Unicode String Table. It must hold the current number of
525 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
526 // marker
527 //
528 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
529 if (NewUnicodeStringTable == NULL) {
530 return EFI_OUT_OF_RESOURCES;
531 }
532
533 //
534 // If the current Unicode String Table contains any entries, then copy them to the
535 // newly allocated Unicode String Table.
536 //
537 if (*UnicodeStringTable != NULL) {
538 CopyMem (
539 NewUnicodeStringTable,
540 *UnicodeStringTable,
541 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
542 );
543 }
544
545 //
546 // Allocate space for a copy of the Language specifier
547 //
548 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);
549 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
550 gBS->FreePool (NewUnicodeStringTable);
551 return EFI_OUT_OF_RESOURCES;
552 }
553
554 //
555 // Compute the length of the Unicode String
556 //
557 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
558 ;
559
560 //
561 // Allocate space for a copy of the Unicode String
562 //
563 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (
564 (UnicodeStringLength + 1) * sizeof (CHAR16),
565 UnicodeString
566 );
567 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
568 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
569 gBS->FreePool (NewUnicodeStringTable);
570 return EFI_OUT_OF_RESOURCES;
571 }
572
573 //
574 // Mark the end of the Unicode String Table
575 //
576 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
577 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
578
579 //
580 // Free the old Unicode String Table
581 //
582 if (*UnicodeStringTable != NULL) {
583 gBS->FreePool (*UnicodeStringTable);
584 }
585
586 //
587 // Point UnicodeStringTable at the newly allocated Unicode String Table
588 //
589 *UnicodeStringTable = NewUnicodeStringTable;
590
591 return EFI_SUCCESS;
592 }
593
594 SupportedLanguages += 3;
595 }
596
597 return EFI_UNSUPPORTED;
598 }
599
600 /**
601 This function frees the table of Unicode strings in UnicodeStringTable.
602 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
603 Otherwise, each language code, and each Unicode string in the Unicode string
604 table are freed, and EFI_SUCCESS is returned.
605
606 @param UnicodeStringTable A pointer to the table of Unicode strings.
607
608 @retval EFI_SUCCESS The Unicode string table was freed.
609
610 **/
611 EFI_STATUS
612 EFIAPI
613 FreeUnicodeStringTable (
614 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable
615 )
616 {
617 UINTN Index;
618
619 //
620 // If the Unicode String Table is NULL, then it is already freed
621 //
622 if (UnicodeStringTable == NULL) {
623 return EFI_SUCCESS;
624 }
625
626 //
627 // Loop through the Unicode String Table until we reach the end of table marker
628 //
629 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
630
631 //
632 // Free the Language string from the Unicode String Table
633 //
634 gBS->FreePool (UnicodeStringTable[Index].Language);
635
636 //
637 // Free the Unicode String from the Unicode String Table
638 //
639 if (UnicodeStringTable[Index].UnicodeString != NULL) {
640 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
641 }
642 }
643
644 //
645 // Free the Unicode String Table itself
646 //
647 gBS->FreePool (UnicodeStringTable);
648
649 return EFI_SUCCESS;
650 }