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