From b0eee679a93f30576d1f5354e9e912e56596bec5 Mon Sep 17 00:00:00 2001 From: Regina König Date: Thu, 20 Aug 2020 20:55:09 +0200 Subject: refactoring ProtocolInformation.c --- .../ProtocolInformation/ProtocolInformation.c | 368 +++++++++++---------- 1 file changed, 191 insertions(+), 177 deletions(-) (limited to 'memtestEDK') diff --git a/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c b/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c index ddc6e82..b32a97d 100644 --- a/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c +++ b/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c @@ -1,3 +1,9 @@ +/* +TODO Description + + +*/ + #include #include @@ -6,6 +12,69 @@ #include +EFI_STATUS efiStatus; +EFI_BOOT_SERVICES* bs; +EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Console; + +EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; + +EFI_HANDLE* handleBuffer = NULL; +EFI_HANDLE* handleBuffer2 = NULL; // TODO remove +UINTN handleCount = 0; +UINTN handleCount2 = 0; +UINTN handleIndex; + +EFI_GUID **ProtocolGuidArray; +UINTN ArrayCount; +UINTN ProtocolIndex; + +EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo; +UINTN OpenInfoIndex; +UINTN OpenInfoCount; + +void printFileSystemInfo(EFI_FILE_SYSTEM_INFO* fileSystemInfo) { + + Print(L"fileSystemInfo pointer %p\n", fileSystemInfo); + + CHAR16* volumeLabel = fileSystemInfo->VolumeLabel; + Print(L"VolumeLabel: %a\n", volumeLabel); + Print(L"Size of volume Label: %d\n", sizeof(fileSystemInfo->VolumeLabel)); + Print(L"Size: %d\n", fileSystemInfo->Size); + Print(L"ReadOnly: %d\n", fileSystemInfo->ReadOnly); + Print(L"VolumeSize: %d\n", fileSystemInfo->VolumeSize); + Print(L"FreeSpace: %d\n", fileSystemInfo->FreeSpace); + Print(L"BlockSize: %d\n", fileSystemInfo->BlockSize); +} + +void printFileContent(UINTN fileSize, CHAR16* fileBuffer) { + Print(L"File Size: %d\n", fileSize); + Print(L"File Content: %a\n", fileBuffer); +} + +void checkStatus(EFI_STATUS es, CHAR16* msg) { + if (es != EFI_SUCCESS) { + Print(L"%a\n", msg); + Print(L"Exit with status: %r\n", es); + Exit(1); + } +} + +BOOLEAN compareGuids(EFI_GUID* sfspGuid, EFI_GUID* guid) { + if (guid->Data1 == sfspGuid->Data1 && guid->Data2 == sfspGuid->Data2 && guid->Data3 == sfspGuid->Data3 + && guid->Data4[0] == sfspGuid->Data4[0] && guid->Data4[1] == sfspGuid->Data4[1] + && guid->Data4[2] == sfspGuid->Data4[2] && guid->Data4[3] == sfspGuid->Data4[3] + && guid->Data4[4] == sfspGuid->Data4[4] && guid->Data4[5] == sfspGuid->Data4[5] + && guid->Data4[6] == sfspGuid->Data4[6] && guid->Data4[7] == sfspGuid->Data4[7]) { + + Print(L"GUID: %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x\n", + guid->Data1, guid->Data2, guid->Data3, + guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3], + guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]); + return TRUE; + } + return FALSE; +} + EFI_STATUS EFIAPI UefiMain ( @@ -13,28 +82,12 @@ UefiMain ( IN EFI_SYSTEM_TABLE *SystemTable ) { + bs = SystemTable->BootServices; + Console = SystemTable->ConOut; - EFI_STATUS efiStatus; - EFI_BOOT_SERVICES* bs = SystemTable->BootServices; - EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; - - EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Console = SystemTable->ConOut; - - - EFI_HANDLE* handleBuffer = NULL; - EFI_HANDLE* handleBuffer2 = NULL; - UINTN handleCount = 0; - UINTN handleCount2 = 0; - UINTN handleIndex; - - EFI_GUID **ProtocolGuidArray; - UINTN ArrayCount; - UINTN ProtocolIndex; - - EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo; - UINTN OpenInfoIndex; - UINTN OpenInfoCount; - + // + // Get all Handles + // efiStatus = bs->LocateHandleBuffer ( AllHandles, NULL, @@ -43,179 +96,140 @@ UefiMain ( &handleBuffer ); - UINT32 sfspData1 = sfspGuid.Data1; - - //Print(L"EFI_STATUS after LocateHandleBuffer: %r\n", efiStatus); - //Print(L"Number of handles found: %d\n", handleCount); // Last time: 182 - - if (!EFI_ERROR (efiStatus)) { - for (handleIndex = 0; handleIndex < handleCount; handleIndex++) { - // - // Retrieve the list of all the protocols on each handle - // - efiStatus = bs->ProtocolsPerHandle ( - handleBuffer[handleIndex], - &ProtocolGuidArray, - &ArrayCount - ); - - //Print(L"EFI_STATUS after ProtocolsPerHandle: %r\n", efiStatus); - //Print(L"Number of Protocols per Handle: %d\n", ArrayCount); - - if (!EFI_ERROR (efiStatus)) { - for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) { - - EFI_GUID *guid = ProtocolGuidArray[ProtocolIndex]; - UINT32 Data1; - UINT16 Data2; - UINT32 Data3; - UINT8 Data4[8]; - Data1 = guid->Data1; - Data2 = guid->Data2; - Data3 = guid->Data3; - for (int i = 0; i < 8; i++) { - Data4[i] = guid->Data4[i]; - } - - // TODO compare the other values - if (Data1 == sfspData1) { - Print(L"GUID: %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x\n", Data1, Data2, Data3, Data4[0], Data4[1], - Data4[2], Data4[3], Data4[4], Data4[5], Data4[6], Data4[7]); - Print(L"handleIndex: %d\n", handleIndex); - Print(L"ProtocolIndex: %d\n", ProtocolIndex); - - // - // Retrieve the protocol instance for each protocol - // - efiStatus = bs->OpenProtocol ( - handleBuffer[handleIndex], - ProtocolGuidArray[ProtocolIndex], - NULL, - ImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - - // - // Retrieve the list of agents that have opened each protocol - // - efiStatus = bs->OpenProtocolInformation ( - handleBuffer[handleIndex], - ProtocolGuidArray[ProtocolIndex], - &OpenInfo, - &OpenInfoCount - ); - - Print(L"Number of agents which have opened the protocol: %d\n", OpenInfoCount); - - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fs = NULL; + checkStatus(efiStatus, L"LocateHandleBuffer"); + + for (handleIndex = 0; handleIndex < handleCount; handleIndex++) { + // + // Retrieve the list of all the protocols on each handle + // + efiStatus = bs->ProtocolsPerHandle ( + handleBuffer[handleIndex], + &ProtocolGuidArray, + &ArrayCount + ); + + checkStatus(efiStatus, L"ProtocolsPerHandle"); + + // + // Search for protocol with EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID + // + for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) { + + EFI_GUID *guid = ProtocolGuidArray[ProtocolIndex]; + + if (compareGuids(&sfspGuid, guid)) { + + // + // Retrieve the protocol instance for each protocol + // + efiStatus = bs->OpenProtocol ( + handleBuffer[handleIndex], + ProtocolGuidArray[ProtocolIndex], + NULL, + ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + checkStatus(efiStatus, L"OpenProtocol"); + + // + // Retrieve the list of agents that have opened each protocol + // + efiStatus = bs->OpenProtocolInformation ( + handleBuffer[handleIndex], + ProtocolGuidArray[ProtocolIndex], + &OpenInfo, + &OpenInfoCount // Number of agents that have opened the protocol + ); + + checkStatus(efiStatus, L"OpenProtocolInformation"); + + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fs = NULL; - efiStatus = bs->HandleProtocol( // TODO Should use OpenProtocol() in new implementations - handleBuffer[handleIndex], - &sfspGuid, - (void**)&fs - ); - - Print(L"Status after HandleProtocol: %r\n", efiStatus); - Print(L"fs pointer: %p\n", fs); - - - EFI_FILE_PROTOCOL* root = NULL; - - efiStatus = fs->OpenVolume(fs, &root); - Print(L"Status after open volume: %r\n", efiStatus); - - EFI_GUID fsiGuid = EFI_FILE_SYSTEM_INFO_ID; - - UINTN bufSize = 0; - - EFI_FILE_SYSTEM_INFO* fileSystemInfo = NULL; - efiStatus = root->GetInfo( - root, - &fsiGuid, - &bufSize, - &fileSystemInfo); - - Print(L"Status: %r\n", efiStatus); - Print(L"Required buffer size: %d\n", bufSize ); - - efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (bufSize) , (void **)&fileSystemInfo); - - efiStatus = root->GetInfo( - root, - &fsiGuid, - &bufSize, - fileSystemInfo); - - Print(L"Status: %r\n", efiStatus); - - Print(L"fileSystemInfo pointer %p\n", fileSystemInfo); - - CHAR16* volumeLabel = fileSystemInfo->VolumeLabel; - Print(L"VolumeLabel: %a\n", volumeLabel); - Print(L"Size of volume Label: %d\n", sizeof(fileSystemInfo->VolumeLabel)); - Print(L"Size: %d\n", fileSystemInfo->Size); - Print(L"ReadOnly: %d\n", fileSystemInfo->ReadOnly); - Print(L"VolumeSize: %d\n", fileSystemInfo->VolumeSize); - Print(L"FreeSpace: %d\n", fileSystemInfo->FreeSpace); - Print(L"BlockSize: %d\n", fileSystemInfo->BlockSize); -/* - efiStatus = bs->FreePool((void **)&buffer); - Print(L"efiStatus after FreePool: %r\n", efiStatus);*/ + efiStatus = bs->HandleProtocol( // TODO Should use OpenProtocol() in new implementations + handleBuffer[handleIndex], + &sfspGuid, + (void**)&fs + ); + checkStatus(efiStatus, L"HandleProtocol"); - EFI_FILE_PROTOCOL* token = NULL; - efiStatus = root->Open( - root, - &token, - L"log", - EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, - 0); + EFI_FILE_PROTOCOL* root = NULL; + efiStatus = fs->OpenVolume(fs, &root); + checkStatus(efiStatus, L"OpenVolume"); - UINTN fileSize = 50; // TODO get the actual size - CHAR16* fileBuffer = NULL; - efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (fileSize) , (void **)&fileBuffer); + EFI_GUID fsiGuid = EFI_FILE_SYSTEM_INFO_ID; + UINTN bufSize = 0; + EFI_FILE_SYSTEM_INFO* fileSystemInfo = NULL; + // First time it is expected to fail + efiStatus = root->GetInfo( + root, + &fsiGuid, + &bufSize, + &fileSystemInfo); - efiStatus = token->Read(token, &fileSize, fileBuffer); - Print(L"Status after file read: %r\n", efiStatus); - Print(L"File Size: %d\n", fileSize); - Print(L"File Buffer Pointer: %p\n", fileBuffer); - Print(L"File Content: %a\n", fileBuffer); + efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (bufSize) , (void **)&fileSystemInfo); + checkStatus(efiStatus, L"AllocatePool"); + efiStatus = root->GetInfo( + root, + &fsiGuid, + &bufSize, + fileSystemInfo); + checkStatus(efiStatus, L"GetInfo"); + printFileSystemInfo(fileSystemInfo); - UINT64 currPos; + // TODO first check if it is the right filesystem. If yes, leave the for loop - token->GetPosition(token, &currPos); - Print(L"Current File Position: %d\n", currPos); + EFI_FILE_PROTOCOL* token = NULL; + efiStatus = root->Open( // TODO do I need that to read? + root, + &token, + L"log", + EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, + 0); + checkStatus(efiStatus, L"Open"); + UINTN fileSize = 50; // TODO get the actual size + CHAR16* fileBuffer = NULL; + efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (fileSize) , (void **)&fileBuffer); + checkStatus(efiStatus, L"AllocatePool for file"); + efiStatus = token->Read(token, &fileSize, fileBuffer); + checkStatus(efiStatus, L"Read"); + printFileContent(fileSize, fileBuffer); - fileBuffer[0] = (CHAR16) 'C'; - fileBuffer[1] = (CHAR16) '\0'; - Console->OutputString(Console, fileBuffer); - UINTN length = 2; - fileSize += 2; - token->SetPosition(token, 35); + UINT64 currPos; + efiStatus = token->GetPosition(token, &currPos); + checkStatus(efiStatus, L"GetPosition"); + Print(L"Current File Position: %d\n", currPos); - efiStatus = token->Write(token, &length, fileBuffer); + fileBuffer[0] = (CHAR16) 'C'; // TODO create new buffer. What's the best practice? Extra function + fileBuffer[1] = (CHAR16) '\0'; + Console->OutputString(Console, fileBuffer); + UINTN length = 2; - Print(L"Status after file write: %r\n", efiStatus); + token->SetPosition(token, fileSize - 1); - token->SetPosition(token, fileSize - 2); + efiStatus = token->Write(token, &length, fileBuffer); + checkStatus(efiStatus, L"Write"); - efiStatus = token->Read(token, &fileSize, fileBuffer); - Print(L"File Content after write: %a\n", fileBuffer); + fileSize += length; + token->SetPosition(token, 0); - token->SetPosition(token, 0); + efiStatus = token->Read(token, &fileSize, fileBuffer); + Print(L"File Content after write: %a\n", fileBuffer); - efiStatus = token->Read(token, &fileSize, fileBuffer); - Print(L"File Content after write: %a\n", fileBuffer); + } // end if (compareGuids(&sfspGuid, guid)) + } // end for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) - } - } - } + +/* + efiStatus = bs->FreePool((void **)&buffer); + Print(L"efiStatus after FreePool: %r\n", efiStatus);*/ if (!EFI_ERROR (efiStatus)) { for (OpenInfoIndex=0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) { @@ -236,13 +250,13 @@ UefiMain ( bs->FreePool(ProtocolGuidArray); Print(L"EFI_STATUS after FreePool(ProtocolGuidArray): %r\n", efiStatus); }*/ - } + + } // end "for (handleIndex = 0; handleIndex < handleCount; handleIndex++)" + /*if (handleBuffer != NULL) { efiStatus = bs->FreePool(handleBuffer); Print(L"EFI_STATUS after FreePool(handleBuffer): %r\n", efiStatus); }*/ - } - Print(L"sfps: %x\n", sfspData1); efiStatus = bs->LocateHandleBuffer(ByProtocol, &sfspGuid, -- cgit v1.2.3-55-g7522