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