summaryrefslogtreecommitdiffstats
path: root/memtestEDK/Memtest/GraphicsOutput/screenmodes.c
blob: c329c8ffb26e5176845c2f1f866c14e4b56d5def (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
//  Copyright (c) 2015  Finnbarr P. Murphy.   All rights reserved.
//
//  Display TEXT and GRAPHIC screen mode information 
//
//  License: BSD License
//
 
 
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/PrintLib.h>
 
#include <Protocol/EfiShell.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/SimpleFileSystem.h>
#include <Protocol/GraphicsOutput.h>
#include "ConsoleControl.h"
#include "UgaDraw.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
PrintUGA(EFI_UGA_DRAW_PROTOCOL *Uga)
{
    EFI_STATUS Status = EFI_SUCCESS;
    UINT32 HorzResolution = 0;
    UINT32 VertResolution = 0;
    UINT32 ColorDepth = 0;
    UINT32 RefreshRate = 0;
 
 
    Status = Uga->GetMode( Uga, &HorzResolution, &VertResolution, 
                           &ColorDepth, &RefreshRate);
 
    if (EFI_ERROR (Status)) {
        Print(L"ERROR: UGA GetMode failed [%d]\n", Status );
    } else {
        Print(L"Horizontal Resolution: %d\n", HorzResolution);
        Print(L"Vertical Resolution: %d\n", VertResolution);
        Print(L"Color Depth: %d\n", ColorDepth);
        Print(L"Refresh Rate: %d\n", RefreshRate);
        Print(L"\n");
    }
 
    return Status;
}
 
 
EFI_STATUS
CheckUGA(BOOLEAN Verbose)
{
    EFI_HANDLE *HandleBuffer = NULL;
    UINTN HandleCount = 0;
    EFI_STATUS Status = EFI_SUCCESS;
    EFI_UGA_DRAW_PROTOCOL *Uga;
 
 
    // get from ConsoleOutHandle?
    Status = gBS->HandleProtocol( gST->ConsoleOutHandle, 
                                  &gEfiUgaDrawProtocolGuid, 
                                  (VOID **) &Uga);
    if (EFI_ERROR (Status)) {
        Print(L"No UGA handle found via HandleProtocol\n");
    } else {
        Print(L"UGA handle found via HandleProtocol\n");
        if (Verbose)
            PrintUGA(Uga);
    }
 
    // try locating directly
    Status = gBS->LocateProtocol( &gEfiUgaDrawProtocolGuid,
                                  NULL,
                                  (VOID **) &Uga);
    if (EFI_ERROR(Status) || Uga == NULL) {
        Print(L"No UGA handle found via LocateProtocol\n");
    } else {
        Print(L"Found UGA handle via LocateProtocol\n");
        if (Verbose)
            PrintUGA(Uga);
    }
 
    // try locating by handle
    Status = gBS->LocateHandleBuffer( ByProtocol,
                      &gEfiUgaDrawProtocolGuid,
                      NULL,
                      &HandleCount,
                      &HandleBuffer);
    if (EFI_ERROR (Status)) {
        Print(L"No UGA handles found via LocateHandleBuffer\n");
    } else {
        Print(L"Found %d UGA handles via LocateHandleBuffer\n", HandleCount);
        for (int i = 0; i < HandleCount; i++) {
            Status = gBS->HandleProtocol( HandleBuffer[i],
                                          &gEfiUgaDrawProtocolGuid,
                                          (VOID*) &Uga);
            if (!EFI_ERROR (Status)) { 
                if (Verbose)
                    PrintUGA(Uga);
            }
        }
        FreePool(HandleBuffer);
    }
 
    Print(L"\n");
 
    return Status;
}
 
 
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)
{
    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);
            }
        }
        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)
{
    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;
}
 
 
 
static void
Usage(void)
{
    Print(L"Usage: screenmodes [-v|--verbose]\n");
}
 
 
INTN
EFIAPI
ShellAppMain(UINTN Argc, CHAR16 **Argv)
{
    EFI_STATUS Status = EFI_SUCCESS;
    BOOLEAN Verbose = FALSE;
 
    if (Argc == 2) {
        if (!StrCmp(Argv[1], L"--verbose") ||
            !StrCmp(Argv[1], L"-v")) {
            Verbose = TRUE;
        }
        if (!StrCmp(Argv[1], L"--help") ||
            !StrCmp(Argv[1], L"-h") ||
            !StrCmp(Argv[1], L"-?")) {
            Usage();
            return Status;
        }
    }
 
 
    // First check for older EDK ConsoleControl protocol support
    CheckCCP(Verbose);
 
    // Next check for UGA support (probably none)
    CheckUGA(Verbose);
 
    // Finally check for GOP support 
    CheckGOP(Verbose);
 
    return Status;
}