summaryrefslogtreecommitdiffstats
path: root/memtestEDK/Memtest/GetMemoryMap/GetMemoryMap.c
blob: 926849caa053f4d5641e65c6d1b8b3108ca530aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
}