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 494f8b339fSPeter Jones if (LibInitialized) 504f8b339fSPeter Jones return; 514f8b339fSPeter Jones 52530d68baSNigel Croxon LibInitialized = TRUE; 53530d68baSNigel Croxon LibFwInstance = FALSE; 543f8935c3SNigel Croxon LibImageHandle = ImageHandle; 553f8935c3SNigel Croxon 56530d68baSNigel Croxon // 57530d68baSNigel Croxon // Set up global pointer to the system table, boot services table, 58530d68baSNigel Croxon // and runtime services table 59530d68baSNigel Croxon // 60530d68baSNigel Croxon 61530d68baSNigel Croxon ST = SystemTable; 62530d68baSNigel Croxon BS = SystemTable->BootServices; 63530d68baSNigel Croxon RT = SystemTable->RuntimeServices; 64530d68baSNigel Croxon // ASSERT (CheckCrc(0, &ST->Hdr)); 65530d68baSNigel Croxon // ASSERT (CheckCrc(0, &BS->Hdr)); 66530d68baSNigel Croxon // ASSERT (CheckCrc(0, &RT->Hdr)); 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 if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) { 96530d68baSNigel Croxon LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable); 97530d68baSNigel Croxon InitializeUnicodeSupport (LangCode); 98530d68baSNigel Croxon if (LangCode) { 99530d68baSNigel Croxon FreePool (LangCode); 100530d68baSNigel Croxon } 101530d68baSNigel Croxon } 102530d68baSNigel Croxon } 103530d68baSNigel Croxon 104530d68baSNigel Croxon VOID 105530d68baSNigel Croxon InitializeUnicodeSupport ( 106530d68baSNigel Croxon CHAR8 *LangCode 107530d68baSNigel Croxon ) 108530d68baSNigel Croxon { 109530d68baSNigel Croxon EFI_UNICODE_COLLATION_INTERFACE *Ui; 110530d68baSNigel Croxon EFI_STATUS Status; 111530d68baSNigel Croxon CHAR8 *Languages; 112530d68baSNigel Croxon UINTN Index, Position, Length; 113530d68baSNigel Croxon UINTN NoHandles; 114530d68baSNigel Croxon EFI_HANDLE *Handles; 115530d68baSNigel Croxon 116530d68baSNigel Croxon // 117530d68baSNigel Croxon // If we don't know it, lookup the current language code 118530d68baSNigel Croxon // 119530d68baSNigel Croxon 120530d68baSNigel Croxon LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles); 121530d68baSNigel Croxon if (!LangCode || !NoHandles) { 122530d68baSNigel Croxon goto Done; 123530d68baSNigel Croxon } 124530d68baSNigel Croxon 125530d68baSNigel Croxon // 126530d68baSNigel Croxon // Check all driver's for a matching language code 127530d68baSNigel Croxon // 128530d68baSNigel Croxon 129530d68baSNigel Croxon for (Index=0; Index < NoHandles; Index++) { 130530d68baSNigel Croxon Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui); 131530d68baSNigel Croxon if (EFI_ERROR(Status)) { 132530d68baSNigel Croxon continue; 133530d68baSNigel Croxon } 134530d68baSNigel Croxon 135530d68baSNigel Croxon // 136530d68baSNigel Croxon // Check for a matching language code 137530d68baSNigel Croxon // 138530d68baSNigel Croxon 139530d68baSNigel Croxon Languages = Ui->SupportedLanguages; 140530d68baSNigel Croxon Length = strlena(Languages); 141530d68baSNigel Croxon for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) { 142530d68baSNigel Croxon 143530d68baSNigel Croxon // 144530d68baSNigel Croxon // If this code matches, use this driver 145530d68baSNigel Croxon // 146530d68baSNigel Croxon 147530d68baSNigel Croxon if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) { 148530d68baSNigel Croxon UnicodeInterface = Ui; 149530d68baSNigel Croxon goto Done; 150530d68baSNigel Croxon } 151530d68baSNigel Croxon } 152530d68baSNigel Croxon } 153530d68baSNigel Croxon 154530d68baSNigel Croxon Done: 155530d68baSNigel Croxon // 156530d68baSNigel Croxon // Cleanup 157530d68baSNigel Croxon // 158530d68baSNigel Croxon 159530d68baSNigel Croxon if (Handles) { 160530d68baSNigel Croxon FreePool (Handles); 161530d68baSNigel Croxon } 162530d68baSNigel Croxon } 163530d68baSNigel Croxon 164530d68baSNigel Croxon VOID 165530d68baSNigel Croxon EFIDebugVariable ( 166530d68baSNigel Croxon VOID 167530d68baSNigel Croxon ) 168530d68baSNigel Croxon { 169530d68baSNigel Croxon EFI_STATUS Status; 170530d68baSNigel Croxon UINT32 Attributes; 171530d68baSNigel Croxon UINTN DataSize; 172530d68baSNigel Croxon UINTN NewEFIDebug; 173530d68baSNigel Croxon 174530d68baSNigel Croxon DataSize = sizeof(EFIDebug); 175530d68baSNigel Croxon Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug); 176530d68baSNigel Croxon if (!EFI_ERROR(Status)) { 177530d68baSNigel Croxon EFIDebug = NewEFIDebug; 178530d68baSNigel Croxon } 179530d68baSNigel Croxon } 180b1d426ceSNigel Croxon 181b1d426ceSNigel Croxon /* 182b1d426ceSNigel Croxon * Calls to memset/memcpy may be emitted implicitly by GCC or MSVC 183b1d426ceSNigel Croxon * even when -ffreestanding or /NODEFAULTLIB are in effect. 184b1d426ceSNigel Croxon */ 185b1d426ceSNigel Croxon 186b1d426ceSNigel Croxon #ifndef __SIZE_TYPE__ 187b1d426ceSNigel Croxon #define __SIZE_TYPE__ UINTN 188b1d426ceSNigel Croxon #endif 189b1d426ceSNigel Croxon 190b1d426ceSNigel Croxon void *memset(void *s, int c, __SIZE_TYPE__ n) 191b1d426ceSNigel Croxon { 192b1d426ceSNigel Croxon unsigned char *p = s; 193b1d426ceSNigel Croxon 194b1d426ceSNigel Croxon while (n--) 195b1d426ceSNigel Croxon *p++ = c; 196b1d426ceSNigel Croxon 197b1d426ceSNigel Croxon return s; 198b1d426ceSNigel Croxon } 199b1d426ceSNigel Croxon 200b1d426ceSNigel Croxon void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n) 201b1d426ceSNigel Croxon { 202b1d426ceSNigel Croxon const unsigned char *q = src; 203b1d426ceSNigel Croxon unsigned char *p = dest; 204b1d426ceSNigel Croxon 205b1d426ceSNigel Croxon while (n--) 206b1d426ceSNigel Croxon *p++ = *q++; 207b1d426ceSNigel Croxon 208b1d426ceSNigel Croxon return dest; 209b1d426ceSNigel Croxon } 210*f412fd2aSLoGin 211*f412fd2aSLoGin 212*f412fd2aSLoGin /** 213*f412fd2aSLoGin * @brief 将数据从src搬运到dst,并能正确处理地址重叠的问题 214*f412fd2aSLoGin * 215*f412fd2aSLoGin * @param dst 目标地址指针 216*f412fd2aSLoGin * @param src 源地址指针 217*f412fd2aSLoGin * @param size 大小 218*f412fd2aSLoGin * @return void* 指向目标地址的指针 219*f412fd2aSLoGin */ 220*f412fd2aSLoGin void *memmove(void *dst, const void *src, uint64_t size) 221*f412fd2aSLoGin { 222*f412fd2aSLoGin const char *_src = src; 223*f412fd2aSLoGin char *_dst = dst; 224*f412fd2aSLoGin 225*f412fd2aSLoGin if (!size) 226*f412fd2aSLoGin return dst; 227*f412fd2aSLoGin 228*f412fd2aSLoGin // 当源地址大于目标地址时,使用memcpy来完成 229*f412fd2aSLoGin if (dst <= src) 230*f412fd2aSLoGin return memcpy(dst, src, size); 231*f412fd2aSLoGin 232*f412fd2aSLoGin // 当源地址小于目标地址时,为防止重叠覆盖,因此从后往前拷贝 233*f412fd2aSLoGin _src += size; 234*f412fd2aSLoGin _dst += size; 235*f412fd2aSLoGin 236*f412fd2aSLoGin // 逐字节拷贝 237*f412fd2aSLoGin while (size--) 238*f412fd2aSLoGin *--_dst = *--_src; 239*f412fd2aSLoGin 240*f412fd2aSLoGin return dst; 241*f412fd2aSLoGin } 242*f412fd2aSLoGin 243