xref: /DragonStub/lib/init.c (revision e7db4418b1fe6ea0220974b77c3b10918ab9b7a0)
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         LibImageHandle = ImageHandle;
53 
54 
55         //
56         // Set up global pointer to the system table, boot services table,
57         // and runtime services table
58         //
59 
60         ST = SystemTable;
61         BS = SystemTable->BootServices;
62         RT = SystemTable->RuntimeServices;
63 //        ASSERT (CheckCrc(0, &ST->Hdr));
64 //        ASSERT (CheckCrc(0, &BS->Hdr));
65 //        ASSERT (CheckCrc(0, &RT->Hdr));
66 
67 
68         //
69         // Initialize pool allocation type
70         //
71 
72         if (ImageHandle) {
73             Status = uefi_call_wrapper(
74                 BS->HandleProtocol,
75                 3,
76                 ImageHandle,
77                 &LoadedImageProtocol,
78                 (VOID*)&LoadedImage
79             );
80 
81             if (!EFI_ERROR(Status)) {
82                 PoolAllocationType = LoadedImage->ImageDataType;
83             }
84             EFIDebugVariable ();
85         }
86 
87         //
88         // Initialize Guid table
89         //
90 
91         InitializeGuid();
92 
93         InitializeLibPlatform(ImageHandle,SystemTable);
94     }
95 
96     //
97     //
98     //
99 
100     if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
101         LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
102         InitializeUnicodeSupport (LangCode);
103         if (LangCode) {
104             FreePool (LangCode);
105         }
106     }
107 }
108 
109 VOID
110 InitializeUnicodeSupport (
111     CHAR8 *LangCode
112     )
113 {
114     EFI_UNICODE_COLLATION_INTERFACE *Ui;
115     EFI_STATUS                      Status;
116     CHAR8                           *Languages;
117     UINTN                           Index, Position, Length;
118     UINTN                           NoHandles;
119     EFI_HANDLE                      *Handles;
120 
121     //
122     // If we don't know it, lookup the current language code
123     //
124 
125     LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles);
126     if (!LangCode || !NoHandles) {
127         goto Done;
128     }
129 
130     //
131     // Check all driver's for a matching language code
132     //
133 
134     for (Index=0; Index < NoHandles; Index++) {
135         Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui);
136         if (EFI_ERROR(Status)) {
137             continue;
138         }
139 
140         //
141         // Check for a matching language code
142         //
143 
144         Languages = Ui->SupportedLanguages;
145         Length = strlena(Languages);
146         for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) {
147 
148             //
149             // If this code matches, use this driver
150             //
151 
152             if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) {
153                 UnicodeInterface = Ui;
154                 goto Done;
155             }
156         }
157     }
158 
159 Done:
160     //
161     // Cleanup
162     //
163 
164     if (Handles) {
165         FreePool (Handles);
166     }
167 }
168 
169 VOID
170 EFIDebugVariable (
171     VOID
172     )
173 {
174     EFI_STATUS      Status;
175     UINT32          Attributes;
176     UINTN           DataSize;
177     UINTN           NewEFIDebug;
178 
179     DataSize = sizeof(EFIDebug);
180     Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug);
181     if (!EFI_ERROR(Status)) {
182         EFIDebug = NewEFIDebug;
183     }
184 }
185 
186 /*
187  * Calls to memset/memcpy may be emitted implicitly by GCC or MSVC
188  * even when -ffreestanding or /NODEFAULTLIB are in effect.
189  */
190 
191 #ifndef __SIZE_TYPE__
192 #define __SIZE_TYPE__ UINTN
193 #endif
194 
195 void *memset(void *s, int c, __SIZE_TYPE__ n)
196 {
197     unsigned char *p = s;
198 
199     while (n--)
200         *p++ = c;
201 
202     return s;
203 }
204 
205 void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n)
206 {
207     const unsigned char *q = src;
208     unsigned char *p = dest;
209 
210     while (n--)
211         *p++ = *q++;
212 
213     return dest;
214 }
215