xref: /netbsd-src/sys/external/bsd/gnu-efi/dist/lib/init.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: init.c,v 1.1.1.1 2014/04/01 16:16:06 jakllsch Exp $	*/
2 
3 /*++
4 
5 Copyright (c) 1998  Intel Corporation
6 
7 Module Name:
8 
9 
10 Abstract:
11 
12 
13 
14 
15 Revision History
16 
17 --*/
18 
19 #include "lib.h"
20 
21 VOID
22 EFIDebugVariable (
23     VOID
24     );
25 
26 VOID
27 InitializeLib (
28     IN EFI_HANDLE           ImageHandle,
29     IN EFI_SYSTEM_TABLE     *SystemTable
30     )
31 /*++
32 
33 Routine Description:
34 
35     Initializes EFI library for use
36 
37 Arguments:
38 
39     Firmware's EFI system table
40 
41 Returns:
42 
43     None
44 
45 --*/
46 {
47     EFI_LOADED_IMAGE        *LoadedImage;
48     EFI_STATUS              Status;
49     CHAR8                   *LangCode;
50 
51     if (!LibInitialized) {
52         LibInitialized = TRUE;
53         LibFwInstance = FALSE;
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 
85             EFIDebugVariable ();
86         }
87 
88         //
89         // Initialize Guid table
90         //
91 
92         InitializeGuid();
93 
94         InitializeLibPlatform(ImageHandle,SystemTable);
95     }
96 
97     //
98     //
99     //
100 
101     if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
102         LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
103         InitializeUnicodeSupport (LangCode);
104         if (LangCode) {
105             FreePool (LangCode);
106         }
107     }
108 }
109 
110 VOID
111 InitializeUnicodeSupport (
112     CHAR8 *LangCode
113     )
114 {
115     EFI_UNICODE_COLLATION_INTERFACE *Ui;
116     EFI_STATUS                      Status;
117     CHAR8                           *Languages;
118     UINTN                           Index, Position, Length;
119     UINTN                           NoHandles;
120     EFI_HANDLE                      *Handles;
121 
122     //
123     // If we don't know it, lookup the current language code
124     //
125 
126     LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles);
127     if (!LangCode || !NoHandles) {
128         goto Done;
129     }
130 
131     //
132     // Check all driver's for a matching language code
133     //
134 
135     for (Index=0; Index < NoHandles; Index++) {
136         Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui);
137         if (EFI_ERROR(Status)) {
138             continue;
139         }
140 
141         //
142         // Check for a matching language code
143         //
144 
145         Languages = Ui->SupportedLanguages;
146         Length = strlena(Languages);
147         for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) {
148 
149             //
150             // If this code matches, use this driver
151             //
152 
153             if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) {
154                 UnicodeInterface = Ui;
155                 goto Done;
156             }
157         }
158     }
159 
160 Done:
161     //
162     // Cleanup
163     //
164 
165     if (Handles) {
166         FreePool (Handles);
167     }
168 }
169 
170 VOID
171 EFIDebugVariable (
172     VOID
173     )
174 {
175     EFI_STATUS      Status;
176     UINT32          Attributes;
177     UINTN           DataSize;
178     UINTN           NewEFIDebug;
179 
180     DataSize = sizeof(EFIDebug);
181     Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug);
182     if (!EFI_ERROR(Status)) {
183         EFIDebug = NewEFIDebug;
184     }
185 }
186