1530d68baSNigel Croxon /*++ 2530d68baSNigel Croxon 3530d68baSNigel Croxon Copyright (c) 1998 Intel Corporation 4530d68baSNigel Croxon 5530d68baSNigel Croxon Module Name: 6530d68baSNigel Croxon 7530d68baSNigel Croxon 8530d68baSNigel Croxon Abstract: 9530d68baSNigel Croxon 10530d68baSNigel Croxon 11530d68baSNigel Croxon 12530d68baSNigel Croxon 13530d68baSNigel Croxon Revision History 14530d68baSNigel Croxon 15530d68baSNigel Croxon --*/ 16530d68baSNigel Croxon 17530d68baSNigel Croxon #include "lib.h" 18530d68baSNigel Croxon 19530d68baSNigel Croxon VOID 20530d68baSNigel Croxon EFIDebugVariable ( 21530d68baSNigel Croxon VOID 22530d68baSNigel Croxon ); 23530d68baSNigel Croxon 24530d68baSNigel Croxon VOID 25530d68baSNigel Croxon InitializeLib ( 26530d68baSNigel Croxon IN EFI_HANDLE ImageHandle, 27530d68baSNigel Croxon IN EFI_SYSTEM_TABLE *SystemTable 28530d68baSNigel Croxon ) 29530d68baSNigel Croxon /*++ 30530d68baSNigel Croxon 31530d68baSNigel Croxon Routine Description: 32530d68baSNigel Croxon 33530d68baSNigel Croxon Initializes EFI library for use 34530d68baSNigel Croxon 35530d68baSNigel Croxon Arguments: 36530d68baSNigel Croxon 37530d68baSNigel Croxon Firmware's EFI system table 38530d68baSNigel Croxon 39530d68baSNigel Croxon Returns: 40530d68baSNigel Croxon 41530d68baSNigel Croxon None 42530d68baSNigel Croxon 43530d68baSNigel Croxon --*/ 44530d68baSNigel Croxon { 45530d68baSNigel Croxon EFI_LOADED_IMAGE *LoadedImage; 46530d68baSNigel Croxon EFI_STATUS Status; 47530d68baSNigel Croxon CHAR8 *LangCode; 48530d68baSNigel Croxon 49530d68baSNigel Croxon if (!LibInitialized) { 50530d68baSNigel Croxon LibInitialized = TRUE; 51530d68baSNigel Croxon LibFwInstance = FALSE; 523f8935c3SNigel Croxon LibImageHandle = ImageHandle; 533f8935c3SNigel Croxon 54530d68baSNigel Croxon 55530d68baSNigel Croxon // 56530d68baSNigel Croxon // Set up global pointer to the system table, boot services table, 57530d68baSNigel Croxon // and runtime services table 58530d68baSNigel Croxon // 59530d68baSNigel Croxon 60530d68baSNigel Croxon ST = SystemTable; 61530d68baSNigel Croxon BS = SystemTable->BootServices; 62530d68baSNigel Croxon RT = SystemTable->RuntimeServices; 63530d68baSNigel Croxon // ASSERT (CheckCrc(0, &ST->Hdr)); 64530d68baSNigel Croxon // ASSERT (CheckCrc(0, &BS->Hdr)); 65530d68baSNigel Croxon // ASSERT (CheckCrc(0, &RT->Hdr)); 66530d68baSNigel Croxon 67530d68baSNigel Croxon 68530d68baSNigel Croxon // 69530d68baSNigel Croxon // Initialize pool allocation type 70530d68baSNigel Croxon // 71530d68baSNigel Croxon 72530d68baSNigel Croxon if (ImageHandle) { 73530d68baSNigel Croxon Status = uefi_call_wrapper( 74530d68baSNigel Croxon BS->HandleProtocol, 75530d68baSNigel Croxon 3, 76530d68baSNigel Croxon ImageHandle, 77530d68baSNigel Croxon &LoadedImageProtocol, 78530d68baSNigel Croxon (VOID*)&LoadedImage 79530d68baSNigel Croxon ); 80530d68baSNigel Croxon 81530d68baSNigel Croxon if (!EFI_ERROR(Status)) { 82530d68baSNigel Croxon PoolAllocationType = LoadedImage->ImageDataType; 83530d68baSNigel Croxon } 84530d68baSNigel Croxon EFIDebugVariable (); 85530d68baSNigel Croxon } 86530d68baSNigel Croxon 87530d68baSNigel Croxon // 88530d68baSNigel Croxon // Initialize Guid table 89530d68baSNigel Croxon // 90530d68baSNigel Croxon 91530d68baSNigel Croxon InitializeGuid(); 92530d68baSNigel Croxon 93530d68baSNigel Croxon InitializeLibPlatform(ImageHandle,SystemTable); 94530d68baSNigel Croxon } 95530d68baSNigel Croxon 96530d68baSNigel Croxon // 97530d68baSNigel Croxon // 98530d68baSNigel Croxon // 99530d68baSNigel Croxon 100530d68baSNigel Croxon if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) { 101530d68baSNigel Croxon LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable); 102530d68baSNigel Croxon InitializeUnicodeSupport (LangCode); 103530d68baSNigel Croxon if (LangCode) { 104530d68baSNigel Croxon FreePool (LangCode); 105530d68baSNigel Croxon } 106530d68baSNigel Croxon } 107530d68baSNigel Croxon } 108530d68baSNigel Croxon 109530d68baSNigel Croxon VOID 110530d68baSNigel Croxon InitializeUnicodeSupport ( 111530d68baSNigel Croxon CHAR8 *LangCode 112530d68baSNigel Croxon ) 113530d68baSNigel Croxon { 114530d68baSNigel Croxon EFI_UNICODE_COLLATION_INTERFACE *Ui; 115530d68baSNigel Croxon EFI_STATUS Status; 116530d68baSNigel Croxon CHAR8 *Languages; 117530d68baSNigel Croxon UINTN Index, Position, Length; 118530d68baSNigel Croxon UINTN NoHandles; 119530d68baSNigel Croxon EFI_HANDLE *Handles; 120530d68baSNigel Croxon 121530d68baSNigel Croxon // 122530d68baSNigel Croxon // If we don't know it, lookup the current language code 123530d68baSNigel Croxon // 124530d68baSNigel Croxon 125530d68baSNigel Croxon LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles); 126530d68baSNigel Croxon if (!LangCode || !NoHandles) { 127530d68baSNigel Croxon goto Done; 128530d68baSNigel Croxon } 129530d68baSNigel Croxon 130530d68baSNigel Croxon // 131530d68baSNigel Croxon // Check all driver's for a matching language code 132530d68baSNigel Croxon // 133530d68baSNigel Croxon 134530d68baSNigel Croxon for (Index=0; Index < NoHandles; Index++) { 135530d68baSNigel Croxon Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui); 136530d68baSNigel Croxon if (EFI_ERROR(Status)) { 137530d68baSNigel Croxon continue; 138530d68baSNigel Croxon } 139530d68baSNigel Croxon 140530d68baSNigel Croxon // 141530d68baSNigel Croxon // Check for a matching language code 142530d68baSNigel Croxon // 143530d68baSNigel Croxon 144530d68baSNigel Croxon Languages = Ui->SupportedLanguages; 145530d68baSNigel Croxon Length = strlena(Languages); 146530d68baSNigel Croxon for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) { 147530d68baSNigel Croxon 148530d68baSNigel Croxon // 149530d68baSNigel Croxon // If this code matches, use this driver 150530d68baSNigel Croxon // 151530d68baSNigel Croxon 152530d68baSNigel Croxon if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) { 153530d68baSNigel Croxon UnicodeInterface = Ui; 154530d68baSNigel Croxon goto Done; 155530d68baSNigel Croxon } 156530d68baSNigel Croxon } 157530d68baSNigel Croxon } 158530d68baSNigel Croxon 159530d68baSNigel Croxon Done: 160530d68baSNigel Croxon // 161530d68baSNigel Croxon // Cleanup 162530d68baSNigel Croxon // 163530d68baSNigel Croxon 164530d68baSNigel Croxon if (Handles) { 165530d68baSNigel Croxon FreePool (Handles); 166530d68baSNigel Croxon } 167530d68baSNigel Croxon } 168530d68baSNigel Croxon 169530d68baSNigel Croxon VOID 170530d68baSNigel Croxon EFIDebugVariable ( 171530d68baSNigel Croxon VOID 172530d68baSNigel Croxon ) 173530d68baSNigel Croxon { 174530d68baSNigel Croxon EFI_STATUS Status; 175530d68baSNigel Croxon UINT32 Attributes; 176530d68baSNigel Croxon UINTN DataSize; 177530d68baSNigel Croxon UINTN NewEFIDebug; 178530d68baSNigel Croxon 179530d68baSNigel Croxon DataSize = sizeof(EFIDebug); 180530d68baSNigel Croxon Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug); 181530d68baSNigel Croxon if (!EFI_ERROR(Status)) { 182530d68baSNigel Croxon EFIDebug = NewEFIDebug; 183530d68baSNigel Croxon } 184530d68baSNigel Croxon } 185*b1d426ceSNigel Croxon 186*b1d426ceSNigel Croxon /* 187*b1d426ceSNigel Croxon * Calls to memset/memcpy may be emitted implicitly by GCC or MSVC 188*b1d426ceSNigel Croxon * even when -ffreestanding or /NODEFAULTLIB are in effect. 189*b1d426ceSNigel Croxon */ 190*b1d426ceSNigel Croxon 191*b1d426ceSNigel Croxon #ifndef __SIZE_TYPE__ 192*b1d426ceSNigel Croxon #define __SIZE_TYPE__ UINTN 193*b1d426ceSNigel Croxon #endif 194*b1d426ceSNigel Croxon 195*b1d426ceSNigel Croxon void *memset(void *s, int c, __SIZE_TYPE__ n) 196*b1d426ceSNigel Croxon { 197*b1d426ceSNigel Croxon unsigned char *p = s; 198*b1d426ceSNigel Croxon 199*b1d426ceSNigel Croxon while (n--) 200*b1d426ceSNigel Croxon *p++ = c; 201*b1d426ceSNigel Croxon 202*b1d426ceSNigel Croxon return s; 203*b1d426ceSNigel Croxon } 204*b1d426ceSNigel Croxon 205*b1d426ceSNigel Croxon void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n) 206*b1d426ceSNigel Croxon { 207*b1d426ceSNigel Croxon const unsigned char *q = src; 208*b1d426ceSNigel Croxon unsigned char *p = dest; 209*b1d426ceSNigel Croxon 210*b1d426ceSNigel Croxon while (n--) 211*b1d426ceSNigel Croxon *p++ = *q++; 212*b1d426ceSNigel Croxon 213*b1d426ceSNigel Croxon return dest; 214*b1d426ceSNigel Croxon } 215