diff options
Diffstat (limited to 'memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c')
-rw-r--r-- | memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c b/memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c new file mode 100644 index 0000000..926849c --- /dev/null +++ b/memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c @@ -0,0 +1,172 @@ +#include <stdio.h> +//#include <stdlib.h> + + +#include <Uefi.h> +#include <Library/UefiLib.h> +#include <Library/UefiApplicationEntryPoint.h> + +#include "../SingleComponents/environment.h" + + +#define DATA_SIZE 1000 + +EFI_MEMORY_DESCRIPTOR getNextElement(EFI_MEMORY_DESCRIPTOR *memoryMap) { // TODO + return *memoryMap; +} + +const CHAR16 *memory_types[] = +{ + L"EfiReservedMemoryType", + L"EfiLoaderCode", + L"EfiLoaderData", + L"EfiBootServicesCode", + L"EfiBootServicesData", + L"EfiRuntimeServicesCode", + L"EfiRuntimeServicesData", + L"EfiConventionalMemory", + L"EfiUnusableMemory", + L"EfiACPIReclaimMemory", + L"EfiACPIMemoryNVS", + L"EfiMemoryMappedIO", + L"EfiMemoryMappedIOPortSpace", + L"EfiPalCode", +}; + +void printToFile() { + /* Variable to store user content */ + char data[DATA_SIZE]; + + /* File pointer to hold reference to our file */ + FILE * fPtr; + + + /* + * Open file in w (write) mode. + * "data/file1.txt" is complete path to create file + */ + fPtr = fopen("data/file1.txt", "w"); + + + /* fopen() return NULL if last operation was unsuccessful */ + if(fPtr == NULL) + { + /* File not created hence exit */ + printf("Unable to create file.\n"); + } + + + /* Write data to file */ + fputs(data, fPtr); + + + /* Close file to save file data */ + fclose(fPtr); + + + /* Success message */ + printf("File created and saved successfully. :) \n"); +} + +EFI_STATUS +EFIAPI +UefiMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + Print(L"Application to get Memory Map\n"); + + UINTN MemoryMapSize = 0; + EFI_MEMORY_DESCRIPTOR *memoryMap = NULL; + UINTN LocalMapKey; + UINTN DescriptorSize; + UINT32 DescriptorVersion; + EFI_STATUS Status; + + EFI_BOOT_SERVICES *bs = SystemTable->BootServices; + + Status = bs->GetMemoryMap(&MemoryMapSize, memoryMap, &LocalMapKey, &DescriptorSize, &DescriptorVersion); +/* Print(L"MemoryMapSize: %d\n", MemoryMapSize); // size needed if the buffer was to small + Print(L"memoryMap pointer: %p\n", memoryMap); + Print(L"This time through the memory map loop, status = %r\n",Status);*/ + do { +/* Print(L"Buffer was too small!\n"); + Print(L"Trying with size %d\n", (UINTN) (MemoryMapSize + 1) ); + Print(L"MemoryMapSize: %d\n", MemoryMapSize); // size needed if the buffer was to small + Print(L"MemoryMap pointer: %p\n", memoryMap);*/ + + Status = bs->AllocatePool(EfiLoaderData, (UINTN) (MemoryMapSize + 1) , (void **)&memoryMap); +/* + Print(L"Status = %r\n", Status); + Print(L"MemoryMap pointer: %p\n", memoryMap); +*/ + Status = bs->GetMemoryMap(&MemoryMapSize, memoryMap, &LocalMapKey, &DescriptorSize, &DescriptorVersion); +/* Print(L"Status = %r\n", Status); + Print(L"After GetMemoryMap\n"); + Print(L"MemoryMapSize: %d\n", MemoryMapSize); // size needed if the buffer was to small + Print(L"memoryMap pointer: %p\n", memoryMap); + Print(L"This time through the memory map loop, status = %r\n",Status);*/ + } while (Status == EFI_BUFFER_TOO_SMALL); + + if (Status != EFI_SUCCESS) { + Print(L"Status = %r\n", Status); + } + + + Print(L"After GetMemoryMap\n"); + Print(L"MemoryMapSize: %d\n", MemoryMapSize); // size needed if the buffer was to small + Print(L"MemoryMap pointer: %p\n", memoryMap); + + EFI_MEMORY_DESCRIPTOR firstElement = *memoryMap; + Print(L"memoryMap location: %p\n", memoryMap); + Print(L"Size of first element: %d\n", sizeof(firstElement)); + Print(L"Type: %d\n", firstElement.Type); + Print(L"PhysicalStart: %p\n", firstElement.PhysicalStart); + Print(L"VirtualStart: %p\n", firstElement.VirtualStart); + Print(L"NumberOfPages: %d\n", firstElement.NumberOfPages); + Print(L"Attribute: %d\n", firstElement.Attribute); + + + EFI_MEMORY_DESCRIPTOR secondElement = *(memoryMap + 40); + Print(L"memoryMap location: %p\n", memoryMap); + Print(L"Size of first element: %d\n", sizeof(secondElement)); + Print(L"Type: %d\n", secondElement.Type); + Print(L"PhysicalStart: %p\n", secondElement.PhysicalStart); + Print(L"VirtualStart: %p\n", secondElement.VirtualStart); + Print(L"NumberOfPages: %d\n", secondElement.NumberOfPages); + Print(L"Attribute: %d\n", secondElement.Attribute); + + uint8_t *startOfMemoryMap = (uint8_t *)memoryMap; + uint8_t *endOfMemoryMap = startOfMemoryMap + MemoryMapSize; + + uint8_t *offset = startOfMemoryMap; + + uint32_t counter = 0; + uint64_t totalPages = 0; + + EFI_MEMORY_DESCRIPTOR *desc = NULL; + + while(offset < endOfMemoryMap) { + desc = (EFI_MEMORY_DESCRIPTOR *)offset; + + Print(L"Map %d:\n", counter); + Print(L" Type: %X, %s\n", desc->Type, memory_types[desc->Type]); + Print(L" PhysicalStart: %X\n", desc->PhysicalStart); + Print(L" VirtualStart: %X\n", desc->VirtualStart); + Print(L" NumberOfPages: %X (4k)\n", desc->NumberOfPages); + Print(L" Attribute: %X\n", desc->Attribute); + + totalPages += desc->NumberOfPages; + + offset += DescriptorSize; + counter++; + + uint64_t MemoryMapSize = totalPages * 4096; + Print(L"Memory detected: %d MB\n", MemoryMapSize / 1024 / 1024); + } + + + return EFI_SUCCESS; +} + |