summaryrefslogtreecommitdiffstats
path: root/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c
blob: 235d0b03330dd9264482b29f89b922b83af5983b (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
TODO Description


*/

#include <stdio.h>

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Include/Guid/FileSystemInfo.h>


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, char *msg) {
  if (es != EFI_SUCCESS) {
    Print(L"Point of failure: %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 (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  bs = SystemTable->BootServices;
  Console = SystemTable->ConOut;

  //
  // Get all Handles
  //
  efiStatus = bs->LocateHandleBuffer (
    AllHandles,
    NULL,
    NULL,
    &handleCount,
    &handleBuffer
  );
  checkStatus(efiStatus, "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, "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_TEST_PROTOCOL
		    );
        checkStatus(efiStatus, "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, "OpenProtocolInformation");

        EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fs = NULL;
   
        efiStatus = bs->HandleProtocol( // TODO Should use OpenProtocol() in new implementations
          handleBuffer[handleIndex],
          &sfspGuid,
          (void**)&fs
        );
        checkStatus(efiStatus, "HandleProtocol");

        EFI_FILE_PROTOCOL* root = NULL;
        efiStatus = fs->OpenVolume(fs, &root);
        checkStatus(efiStatus, "OpenVolume");

        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 = bs->AllocatePool(EfiLoaderData, (UINTN) (bufSize) , (void **)&fileSystemInfo);
        checkStatus(efiStatus, "AllocatePool");

        efiStatus = root->GetInfo(
          root,
          &fsiGuid,
          &bufSize,
          fileSystemInfo);
        checkStatus(efiStatus, "GetInfo");
        //printFileSystemInfo(fileSystemInfo);

        // TODO first check if it is the right filesystem. If yes, leave the for loop

        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);
        checkStatus(efiStatus, "Open");

        UINTN fileSize = 500; // TODO get the actual size
        CHAR16* fileBuffer = NULL;
        efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (fileSize) , (void **)&fileBuffer);
        checkStatus(efiStatus, "AllocatePool for file");

        efiStatus = token->Read(token, &fileSize, fileBuffer);
        checkStatus(efiStatus, "Read");
        printFileContent(fileSize, fileBuffer);


        UINT64 currPos;
        efiStatus = token->GetPosition(token, &currPos);
        checkStatus(efiStatus, "GetPosition");
        Print(L"Current File Position: %d\n", currPos);

        char *message = "\nAdding a new line";
        UINTN length = 19;
        Print(L"Lengh: %d\n", length);

        token->SetPosition(token, fileSize - 1);

        efiStatus = token->Write(token, &length, message);
        checkStatus(efiStatus, "Write");

        fileSize += length;

        token->SetPosition(token, 0);

        efiStatus = token->Read(token, &fileSize, fileBuffer);
        printFileContent(fileSize, 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++) {
						//
						// HandleBuffer[handleIndex] is the handle
						// ProtocolGuidArray[ProtocolIndex] is the protocol GUID
						// Instance is the protocol instance for the protocol
						// OpenInfo[OpenInfoIndex] is an agent that has opened a protocol
						//
				}
				/*if (OpenInfo != NULL) { // TODO Why is it crashing when using FreePool?
					bs->FreePool(OpenInfo);
          Print(L"EFI_STATUS after FreePool(OpenInfo): %r\n", efiStatus);
				}*/
 	 		}

  		/*if (ProtocolGuidArray != NULL) {
				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);
    }*/

  efiStatus = bs->LocateHandleBuffer(ByProtocol, 
                                   &sfspGuid, 
                                   NULL, 
                                   &handleCount2, 
                                   &handleBuffer2);

  Print(L"Handle count: %d\n", handleCount2);


  return EFI_SUCCESS;
}