summaryrefslogtreecommitdiffstats
path: root/memtestEDK/Memtest/ProtocolInformation/ProtocolInformation.c
blob: 4a953c312984889c907f96fb0db47dc957e0f4b4 (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
#include <stdio.h>

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>


EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{

  EFI_STATUS efiStatus;
  EFI_BOOT_SERVICES* bs = SystemTable->BootServices;
  EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;

  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;

  efiStatus = bs->LocateHandleBuffer (
    AllHandles,
    NULL,
    NULL,
    &handleCount,
    &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
				    );
          }
        }
      }

			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);
  		}*/
		}
		/*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, 
                                   NULL, 
                                   &handleCount2, 
                                   &handleBuffer2);

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

  
  for (int index = 0; index < (int)handleCount; ++ index)
  {
    //EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fs = NULL;
   
    /*efiStatus = bs->HandleProtocol( // TODO Should use OpenProtocol() in new implementations
        handles[index],
        &sfspGuid,
        (void**)&fs
        );*/
    	/*Print(L"Status after HandleProtocol: %r\n", efiStatus);
        Print(L"Volume %d found\n", index);
        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;
        VOID* buffer = NULL;*/

        /*efiStatus = root->GetInfo(
        	root,
        	&fsiGuid,
        	&bufSize,
        	&buffer);

        Print(L"Status: %r\n", efiStatus);
        Print(L"Required buffer size: %d\n", bufSize );


        efiStatus = bs->AllocatePool(EfiLoaderData, (UINTN) (bufSize) , (void **)&buffer);


        efiStatus = root->GetInfo(
        	root,
        	&fsiGuid,
        	&bufSize,
        	&buffer);*/
/*
        Print(L"Status: %r\n", efiStatus);

        EFI_FILE_SYSTEM_INFO* fhgf = buffer;

        Print(L"buffer pointer %p\n", buffer);
        Print(L"fhgf pointer %p\n", fhgf);

        Print(L"VolumeLabel: %s\n", fhgf->VolumeLabel);
        Print(L"Size: %d\n", fhgf->Size);
        Print(L"ReadOnly: %d\n", fhgf->ReadOnly);
        Print(L"VolumeSize: %d\n", fhgf->VolumeSize);
        Print(L"FreeSpace: %d\n", fhgf->FreeSpace);
        Print(L"BlockSize: %d\n", fhgf->BlockSize);
        Print(L"Number of handle found %d\n", (index + 1));

        efiStatus = bs->FreePool((void **)&buffer);
        Print(L"efiStatus after FreePool: %r\n", efiStatus);*/
   }
/*    
...




        EFI_FILE_PROTOCOL* token = NULL;
efiStatus = root->Open(
        root, 
        &token,
        L"myfolder\\token.bin",
        EFI_FILE_MODE_READ,
        EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);*/

/*
          EFI_FILE_OPEN         Open;
  EFI_FILE_CLOSE        Close;
  EFI_FILE_DELETE       Delete;
  EFI_FILE_READ         Read;
  EFI_FILE_WRITE        Write;
  EFI_FILE_GET_POSITION GetPosition;
  EFI_FILE_SET_POSITION SetPosition;
  EFI_FILE_GET_INFO     GetInfo;
  EFI_FILE_SET_INFO     SetInfo;
  EFI_FILE_FLUSH        Flush;*/

  return EFI_SUCCESS;
}