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 19*78b790faSLoGin VOID EFIDebugVariable(VOID); 20530d68baSNigel Croxon 21*78b790faSLoGin VOID InitializeLib(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) 22530d68baSNigel Croxon /*++ 23530d68baSNigel Croxon 24530d68baSNigel Croxon Routine Description: 25530d68baSNigel Croxon 26530d68baSNigel Croxon Initializes EFI library for use 27530d68baSNigel Croxon 28530d68baSNigel Croxon Arguments: 29530d68baSNigel Croxon 30530d68baSNigel Croxon Firmware's EFI system table 31530d68baSNigel Croxon 32530d68baSNigel Croxon Returns: 33530d68baSNigel Croxon 34530d68baSNigel Croxon None 35530d68baSNigel Croxon 36530d68baSNigel Croxon --*/ 37530d68baSNigel Croxon { 38530d68baSNigel Croxon EFI_LOADED_IMAGE *LoadedImage; 39530d68baSNigel Croxon EFI_STATUS Status; 40530d68baSNigel Croxon CHAR8 *LangCode; 41530d68baSNigel Croxon 424f8b339fSPeter Jones if (LibInitialized) 434f8b339fSPeter Jones return; 444f8b339fSPeter Jones 45530d68baSNigel Croxon LibInitialized = TRUE; 46530d68baSNigel Croxon LibFwInstance = FALSE; 473f8935c3SNigel Croxon LibImageHandle = ImageHandle; 483f8935c3SNigel Croxon 49530d68baSNigel Croxon // 50530d68baSNigel Croxon // Set up global pointer to the system table, boot services table, 51530d68baSNigel Croxon // and runtime services table 52530d68baSNigel Croxon // 53530d68baSNigel Croxon 54530d68baSNigel Croxon ST = SystemTable; 55530d68baSNigel Croxon BS = SystemTable->BootServices; 56530d68baSNigel Croxon RT = SystemTable->RuntimeServices; 57530d68baSNigel Croxon // ASSERT (CheckCrc(0, &ST->Hdr)); 58530d68baSNigel Croxon // ASSERT (CheckCrc(0, &BS->Hdr)); 59530d68baSNigel Croxon // ASSERT (CheckCrc(0, &RT->Hdr)); 60530d68baSNigel Croxon 61530d68baSNigel Croxon // 62530d68baSNigel Croxon // Initialize pool allocation type 63530d68baSNigel Croxon // 64530d68baSNigel Croxon 65530d68baSNigel Croxon if (ImageHandle) { 66*78b790faSLoGin Status = uefi_call_wrapper(BS->HandleProtocol, 3, ImageHandle, 67530d68baSNigel Croxon &LoadedImageProtocol, 68*78b790faSLoGin (VOID *)&LoadedImage); 69530d68baSNigel Croxon 70530d68baSNigel Croxon if (!EFI_ERROR(Status)) { 71530d68baSNigel Croxon PoolAllocationType = LoadedImage->ImageDataType; 72530d68baSNigel Croxon } 73530d68baSNigel Croxon EFIDebugVariable(); 74530d68baSNigel Croxon } 75530d68baSNigel Croxon 76530d68baSNigel Croxon // 77530d68baSNigel Croxon // Initialize Guid table 78530d68baSNigel Croxon // 79530d68baSNigel Croxon 80530d68baSNigel Croxon InitializeGuid(); 81530d68baSNigel Croxon 82530d68baSNigel Croxon InitializeLibPlatform(ImageHandle, SystemTable); 83530d68baSNigel Croxon 84530d68baSNigel Croxon if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) { 85530d68baSNigel Croxon LangCode = LibGetVariable(VarLanguage, &EfiGlobalVariable); 86530d68baSNigel Croxon InitializeUnicodeSupport(LangCode); 87530d68baSNigel Croxon if (LangCode) { 88530d68baSNigel Croxon FreePool(LangCode); 89530d68baSNigel Croxon } 90530d68baSNigel Croxon } 91530d68baSNigel Croxon } 92530d68baSNigel Croxon 93*78b790faSLoGin VOID InitializeUnicodeSupport(CHAR8 *LangCode) 94530d68baSNigel Croxon { 95530d68baSNigel Croxon EFI_UNICODE_COLLATION_INTERFACE *Ui; 96530d68baSNigel Croxon EFI_STATUS Status; 97530d68baSNigel Croxon CHAR8 *Languages; 98530d68baSNigel Croxon UINTN Index, Position, Length; 99530d68baSNigel Croxon UINTN NoHandles; 100530d68baSNigel Croxon EFI_HANDLE *Handles; 101530d68baSNigel Croxon 102530d68baSNigel Croxon // 103530d68baSNigel Croxon // If we don't know it, lookup the current language code 104530d68baSNigel Croxon // 105530d68baSNigel Croxon 106*78b790faSLoGin LibLocateHandle(ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, 107*78b790faSLoGin &Handles); 108530d68baSNigel Croxon if (!LangCode || !NoHandles) { 109530d68baSNigel Croxon goto Done; 110530d68baSNigel Croxon } 111530d68baSNigel Croxon 112530d68baSNigel Croxon // 113530d68baSNigel Croxon // Check all driver's for a matching language code 114530d68baSNigel Croxon // 115530d68baSNigel Croxon 116530d68baSNigel Croxon for (Index = 0; Index < NoHandles; Index++) { 117*78b790faSLoGin Status = uefi_call_wrapper(BS->HandleProtocol, 3, 118*78b790faSLoGin Handles[Index], 119*78b790faSLoGin &UnicodeCollationProtocol, 120*78b790faSLoGin (VOID *)&Ui); 121530d68baSNigel Croxon if (EFI_ERROR(Status)) { 122530d68baSNigel Croxon continue; 123530d68baSNigel Croxon } 124530d68baSNigel Croxon 125530d68baSNigel Croxon // 126530d68baSNigel Croxon // Check for a matching language code 127530d68baSNigel Croxon // 128530d68baSNigel Croxon 129530d68baSNigel Croxon Languages = Ui->SupportedLanguages; 130530d68baSNigel Croxon Length = strlena(Languages); 131*78b790faSLoGin for (Position = 0; Position < Length; 132*78b790faSLoGin Position += ISO_639_2_ENTRY_SIZE) { 133530d68baSNigel Croxon // 134530d68baSNigel Croxon // If this code matches, use this driver 135530d68baSNigel Croxon // 136530d68baSNigel Croxon 137*78b790faSLoGin if (CompareMem(Languages + Position, LangCode, 138*78b790faSLoGin ISO_639_2_ENTRY_SIZE) == 0) { 139530d68baSNigel Croxon UnicodeInterface = Ui; 140530d68baSNigel Croxon goto Done; 141530d68baSNigel Croxon } 142530d68baSNigel Croxon } 143530d68baSNigel Croxon } 144530d68baSNigel Croxon 145530d68baSNigel Croxon Done: 146530d68baSNigel Croxon // 147530d68baSNigel Croxon // Cleanup 148530d68baSNigel Croxon // 149530d68baSNigel Croxon 150530d68baSNigel Croxon if (Handles) { 151530d68baSNigel Croxon FreePool(Handles); 152530d68baSNigel Croxon } 153530d68baSNigel Croxon } 154530d68baSNigel Croxon 155*78b790faSLoGin VOID EFIDebugVariable(VOID) 156530d68baSNigel Croxon { 157530d68baSNigel Croxon EFI_STATUS Status; 158530d68baSNigel Croxon UINT32 Attributes; 159530d68baSNigel Croxon UINTN DataSize; 160530d68baSNigel Croxon UINTN NewEFIDebug; 161530d68baSNigel Croxon 162530d68baSNigel Croxon DataSize = sizeof(EFIDebug); 163*78b790faSLoGin Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", 164*78b790faSLoGin &EfiGlobalVariable, &Attributes, &DataSize, 165*78b790faSLoGin &NewEFIDebug); 166530d68baSNigel Croxon if (!EFI_ERROR(Status)) { 167530d68baSNigel Croxon EFIDebug = NewEFIDebug; 168530d68baSNigel Croxon } 169530d68baSNigel Croxon } 170b1d426ceSNigel Croxon 171b1d426ceSNigel Croxon /* 172b1d426ceSNigel Croxon * Calls to memset/memcpy may be emitted implicitly by GCC or MSVC 173b1d426ceSNigel Croxon * even when -ffreestanding or /NODEFAULTLIB are in effect. 174b1d426ceSNigel Croxon */ 175b1d426ceSNigel Croxon 176b1d426ceSNigel Croxon #ifndef __SIZE_TYPE__ 177b1d426ceSNigel Croxon #define __SIZE_TYPE__ UINTN 178b1d426ceSNigel Croxon #endif 179b1d426ceSNigel Croxon 180b1d426ceSNigel Croxon void *memset(void *s, int c, __SIZE_TYPE__ n) 181b1d426ceSNigel Croxon { 182b1d426ceSNigel Croxon unsigned char *p = s; 183b1d426ceSNigel Croxon 184b1d426ceSNigel Croxon while (n--) 185b1d426ceSNigel Croxon *p++ = c; 186b1d426ceSNigel Croxon 187b1d426ceSNigel Croxon return s; 188b1d426ceSNigel Croxon } 189b1d426ceSNigel Croxon 190b1d426ceSNigel Croxon void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n) 191b1d426ceSNigel Croxon { 192b1d426ceSNigel Croxon const unsigned char *q = src; 193b1d426ceSNigel Croxon unsigned char *p = dest; 194b1d426ceSNigel Croxon 195b1d426ceSNigel Croxon while (n--) 196b1d426ceSNigel Croxon *p++ = *q++; 197b1d426ceSNigel Croxon 198b1d426ceSNigel Croxon return dest; 199b1d426ceSNigel Croxon } 200f412fd2aSLoGin 201f412fd2aSLoGin /** 202f412fd2aSLoGin * @brief 将数据从src搬运到dst,并能正确处理地址重叠的问题 203f412fd2aSLoGin * 204f412fd2aSLoGin * @param dst 目标地址指针 205f412fd2aSLoGin * @param src 源地址指针 206f412fd2aSLoGin * @param size 大小 207f412fd2aSLoGin * @return void* 指向目标地址的指针 208f412fd2aSLoGin */ 209f412fd2aSLoGin void *memmove(void *dst, const void *src, uint64_t size) 210f412fd2aSLoGin { 211f412fd2aSLoGin const char *_src = src; 212f412fd2aSLoGin char *_dst = dst; 213f412fd2aSLoGin 214f412fd2aSLoGin if (!size) 215f412fd2aSLoGin return dst; 216f412fd2aSLoGin 217f412fd2aSLoGin // 当源地址大于目标地址时,使用memcpy来完成 218f412fd2aSLoGin if (dst <= src) 219f412fd2aSLoGin return memcpy(dst, src, size); 220f412fd2aSLoGin 221f412fd2aSLoGin // 当源地址小于目标地址时,为防止重叠覆盖,因此从后往前拷贝 222f412fd2aSLoGin _src += size; 223f412fd2aSLoGin _dst += size; 224f412fd2aSLoGin 225f412fd2aSLoGin // 逐字节拷贝 226f412fd2aSLoGin while (size--) 227f412fd2aSLoGin *--_dst = *--_src; 228f412fd2aSLoGin 229f412fd2aSLoGin return dst; 230f412fd2aSLoGin } 231