Imported Upstream version 3.0
[platform/upstream/gnu-efi.git] / lib / init.c
1 /*++
2
3 Copyright (c) 1998  Intel Corporation
4
5 Module Name:
6
7
8 Abstract:
9
10
11
12
13 Revision History
14
15 --*/
16
17 #include "lib.h"
18
19 VOID
20 EFIDebugVariable (
21     VOID
22     );
23
24 VOID
25 InitializeLib (
26     IN EFI_HANDLE           ImageHandle,
27     IN EFI_SYSTEM_TABLE     *SystemTable
28     )
29 /*++
30
31 Routine Description:
32
33     Initializes EFI library for use
34     
35 Arguments:
36
37     Firmware's EFI system table
38     
39 Returns:
40
41     None
42
43 --*/ 
44 {
45     EFI_LOADED_IMAGE        *LoadedImage;
46     EFI_STATUS              Status;
47     CHAR8                   *LangCode;
48
49     if (!LibInitialized) {
50         LibInitialized = TRUE;
51         LibFwInstance = FALSE;
52
53         //
54         // Set up global pointer to the system table, boot services table,
55         // and runtime services table
56         //
57
58         ST = SystemTable;
59         BS = SystemTable->BootServices;
60         RT = SystemTable->RuntimeServices;
61 //        ASSERT (CheckCrc(0, &ST->Hdr));
62 //        ASSERT (CheckCrc(0, &BS->Hdr));
63 //        ASSERT (CheckCrc(0, &RT->Hdr));
64
65
66         //
67         // Initialize pool allocation type
68         //
69
70         if (ImageHandle) {
71             Status = uefi_call_wrapper(
72                             BS->HandleProtocol,
73                                 3,
74                             ImageHandle, 
75                             &LoadedImageProtocol,
76                             (VOID*)&LoadedImage
77                             );
78
79             if (!EFI_ERROR(Status)) {
80                 PoolAllocationType = LoadedImage->ImageDataType;
81             }
82             
83             EFIDebugVariable ();
84         }
85
86         //
87         // Initialize Guid table
88         //
89
90         InitializeGuid();
91
92         InitializeLibPlatform(ImageHandle,SystemTable);
93     }
94
95     //
96     // 
97     //
98
99     if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
100         LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
101         InitializeUnicodeSupport (LangCode);
102         if (LangCode) {
103             FreePool (LangCode);
104         }
105     }
106 }
107
108 VOID
109 InitializeUnicodeSupport (
110     CHAR8 *LangCode
111     )
112 {
113     EFI_UNICODE_COLLATION_INTERFACE *Ui;
114     EFI_STATUS                      Status;
115     CHAR8                           *Languages;
116     UINTN                           Index, Position, Length;
117     UINTN                           NoHandles;
118     EFI_HANDLE                      *Handles;
119
120     //
121     // If we don't know it, lookup the current language code
122     //
123
124     LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles);
125     if (!LangCode || !NoHandles) {
126         goto Done;
127     }
128
129     //
130     // Check all driver's for a matching language code
131     //
132
133     for (Index=0; Index < NoHandles; Index++) {
134         Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui);
135         if (EFI_ERROR(Status)) {
136             continue;
137         }
138
139         //
140         // Check for a matching language code
141         //
142
143         Languages = Ui->SupportedLanguages;
144         Length = strlena(Languages);
145         for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) {
146
147             //
148             // If this code matches, use this driver
149             //
150
151             if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) {
152                 UnicodeInterface = Ui;
153                 goto Done;
154             }
155         }
156     }
157
158 Done:
159     //
160     // Cleanup
161     //
162
163     if (Handles) {
164         FreePool (Handles);
165     }
166 }
167
168 VOID
169 EFIDebugVariable (
170     VOID
171     )
172 {
173     EFI_STATUS      Status;
174     UINT32          Attributes;
175     UINTN           DataSize;
176     UINTN           NewEFIDebug;
177
178     DataSize = sizeof(EFIDebug);
179     Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug);
180     if (!EFI_ERROR(Status)) {
181         EFIDebug = NewEFIDebug;
182     } 
183 }