summaryrefslogtreecommitdiffstats
path: root/memtestEDK/Memtest/GraphicsOutput/GraphicsOutputProtocol.c
blob: 2198553a213c38e6b8d241424e4445ad2a53dec8 (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
#include <stdio.h>

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




static int
memcmp(const void *s1, const void *s2, UINTN n)
{
    const unsigned char *c1 = s1, *c2 = s2;
    int d = 0;
 
    if (!s1 && !s2)
       return 0;
    if (s1 && !s2)
       return 1;
    if (!s1 && s2)
       return -1;
 
    while (n--) {
        d = (int)*c1++ - (int)*c2++;
        if (d)
             break;
    }
 
    return d;
}
 
EFI_STATUS
PrintGOP(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
{
    int i, imax;
    EFI_STATUS Status;
 
    imax = gop->Mode->MaxMode;
 
    Print(L"GOP reports MaxMode %d\n", imax);
 
    for (i = 0; i < imax; i++) {
         EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
         UINTN SizeOfInfo;
 
         Status = gop->QueryMode(gop, i, &SizeOfInfo, &Info);
         if (EFI_ERROR(Status) && Status == EFI_NOT_STARTED) {
             gop->SetMode(gop, gop->Mode->Mode);
             Status = gop->QueryMode(gop, i, &SizeOfInfo, &Info);
         }
 
         if (EFI_ERROR(Status)) {
             Print(L"ERROR: Bad response from QueryMode: %d\n", Status);
             continue;
         }
         Print(L"%c%d: %dx%d ", memcmp(Info,gop->Mode->Info,sizeof(*Info)) == 0 ? '*' : ' ', i,
                        Info->HorizontalResolution,
                        Info->VerticalResolution);
         switch(Info->PixelFormat) {
             case PixelRedGreenBlueReserved8BitPerColor:
                  Print(L"RGBRerserved");
                  break;
             case PixelBlueGreenRedReserved8BitPerColor:
                  Print(L"BGRReserved");
                  break;
             case PixelBitMask:
                  Print(L"Red:%08x Green:%08x Blue:%08x Reserved:%08x",
                          Info->PixelInformation.RedMask,
                          Info->PixelInformation.GreenMask,
                          Info->PixelInformation.BlueMask,
                          Info->PixelInformation.ReservedMask);
                          break;
             case PixelBltOnly:
                  Print(L"(blt only)");
                  break;
             default:
                  Print(L"(Invalid pixel format)");
                 break;
        }
        Print(L" Pixels %d\n", Info->PixelsPerScanLine);
    }
    Print(L"\n");
 
    return EFI_SUCCESS;
}
 
 
EFI_STATUS
CheckGOP(BOOLEAN Verbose, IN EFI_SYSTEM_TABLE  *gST, IN EFI_BOOT_SERVICES *gBS)
{
    EFI_HANDLE *HandleBuffer = NULL;
    UINTN HandleCount = 0;
    EFI_STATUS Status = EFI_SUCCESS;
    EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
 
 
    // get from ConsoleOutHandle?
    Status = gBS->HandleProtocol( gST->ConsoleOutHandle, 
                                  &gEfiGraphicsOutputProtocolGuid, 
                                  (VOID **) &Gop);
    if (EFI_ERROR (Status)) {
        Print(L"No GOP handle found via HandleProtocol\n");
    } else {
        Print(L"GOP handle found via HandleProtocol\n");
        if (Verbose)
            PrintGOP(Gop);
    }
 
    // try locating directly
    Status = gBS->LocateProtocol( &gEfiGraphicsOutputProtocolGuid,
                                  NULL,
                                  (VOID **) &Gop);
    if (EFI_ERROR(Status) || Gop == NULL) {
        Print(L"No GOP handle found via LocateProtocol\n");
    } else {
        Print(L"Found GOP handle via LocateProtocol\n");
        if (Verbose)
            PrintGOP(Gop);
    }
 
    // try locating by handle
    Status = gBS->LocateHandleBuffer( ByProtocol,
                      &gEfiGraphicsOutputProtocolGuid,
                      NULL,
                      &HandleCount,
                      &HandleBuffer);
    if (EFI_ERROR (Status)) {
        Print(L"No GOP handles found via LocateHandleBuffer\n");
    } else {
        Print(L"Found %d GOP handles via LocateHandleBuffer\n", HandleCount);
        for (int i = 0; i < HandleCount; i++) {
            Status = gBS->HandleProtocol( HandleBuffer[i],
                                          &gEfiGraphicsOutputProtocolGuid,
                                          (VOID*) &Gop);
            if (!EFI_ERROR (Status)) { 
                if (Verbose)
                    PrintGOP(Gop);
            }
        }
        gBS->FreePool(HandleBuffer);
    }
 
    Print(L"\n");
 
    return Status;
}

EFI_STATUS
PrintCCP(EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl)
{
    EFI_STATUS Status = EFI_SUCCESS;
    EFI_CONSOLE_CONTROL_SCREEN_MODE Mode;
    BOOLEAN GopUgaExists;
    BOOLEAN StdInLocked;
 
    Status = ConsoleControl->GetMode(ConsoleControl, &Mode, &GopUgaExists, &StdInLocked);
    if (EFI_ERROR (Status)) {
        Print(L"ERROR: ConsoleControl GetMode failed [%d]\n", Status );
        return Status;
    }
 
    Print(L"Screen mode: ");
    switch (Mode) {
       case EfiConsoleControlScreenText:
              Print(L"Text");
              break;
       case EfiConsoleControlScreenGraphics:
              Print(L"Graphics");
              break;
       case EfiConsoleControlScreenMaxValue:
              Print(L"MaxValue");
              break;
    }
    Print(L"\n");
    Print(L"Graphics Support Avalaiable: ");
    if (GopUgaExists) 
        Print(L"Yes");
    else
        Print(L"No");
    Print(L"\n");
 
    Print(L"\n");
 
    return EFI_SUCCESS;
}
 
 
EFI_STATUS
CheckCCP(BOOLEAN Verbose, IN EFI_SYSTEM_TABLE  *gST, IN EFI_BOOT_SERVICES *gBS)
{
    EFI_GUID gEfiConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
    EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
    EFI_HANDLE *HandleBuffer = NULL;
    UINTN HandleCount = 0;
    EFI_STATUS Status = EFI_SUCCESS;
 
    // get from ConsoleOutHandle?
    Status = gBS->HandleProtocol( gST->ConsoleOutHandle,
                                  &gEfiConsoleControlProtocolGuid,
                                  (VOID **) &ConsoleControl);
    if (EFI_ERROR (Status)) {
        Print(L"No ConsoleControl handle found via HandleProtocol\n");
    } else {
        Print(L"ConsoleControl handle found via HandleProtocol\n");
        if (Verbose)
            PrintCCP(ConsoleControl);
    }
 
    // try locating directly
    Status = gBS->LocateProtocol( &gEfiConsoleControlProtocolGuid,
                                  NULL,
                                  (VOID **) &ConsoleControl);
    if (EFI_ERROR(Status) || ConsoleControl == NULL) {
        Print(L"No ConsoleControl handle found via LocateProtocol\n");
    } else {
        Print(L"Found ConsoleControl handle via LocateProtocol\n");
        if (Verbose)
            PrintCCP(ConsoleControl);
    }
 
    // try locating by handle
    Status = gBS->LocateHandleBuffer( ByProtocol,
                                      &gEfiConsoleControlProtocolGuid,
                                      NULL,
                                      &HandleCount,
                                      &HandleBuffer);
    if (EFI_ERROR (Status)) {
        Print(L"No ConsoleControl handles found via LocateHandleBuffer\n");
    } else {
        Print(L"Found %d ConsoleControl handles via LocateHandleBuffer\n", HandleCount);
        for (int i = 0; i < HandleCount; i++) {
            Status = gBS->HandleProtocol( HandleBuffer[i],
                                          &gEfiConsoleControlProtocolGuid,
                                          (VOID*) &ConsoleControl);
            if (!EFI_ERROR (Status)) 
                if (Verbose)
                    PrintCCP(ConsoleControl);
        }
        FreePool(HandleBuffer);
    }
 
    Print(L"\n");
 
    return Status;
}

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

	CheckGOP(FALSE, SystemTable, SystemTable->BootServices);

	CheckCCP(FALSE, SystemTable, SystemTable->BootServices);

	return EFI_SUCCESS;

}