xref: /netbsd-src/sys/external/bsd/gnu-efi/dist/apps/modelist.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: modelist.c,v 1.1.1.1 2014/04/01 16:16:06 jakllsch Exp $	*/
2 
3 #include <efi.h>
4 #include <efilib.h>
5 
6 extern EFI_GUID GraphicsOutputProtocol;
7 
8 static int memcmp(const void *s1, const void *s2, UINTN n)
9 {
10 	const unsigned char *c1 = s1, *c2 = s2;
11 	int d = 0;
12 
13 	if (!s1 && !s2)
14 		return 0;
15 	if (s1 && !s2)
16 		return 1;
17 	if (!s1 && s2)
18 		return -1;
19 
20 	while (n--) {
21 		d = (int)*c1++ - (int)*c2++;
22 		if (d)
23 			break;
24 	}
25 	return d;
26 }
27 
28 static void
29 print_modes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
30 {
31 	int i, imax;
32 	EFI_STATUS rc;
33 
34 	imax = gop->Mode->MaxMode;
35 
36 	Print(L"GOP reports MaxMode %d\n", imax);
37 	for (i = 0; i < imax; i++) {
38 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
39 		UINTN SizeOfInfo;
40 		rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
41 					&info);
42 		if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
43 			rc = uefi_call_wrapper(gop->SetMode, 2, gop,
44 				gop->Mode->Mode);
45 			rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
46 				&SizeOfInfo, &info);
47 		}
48 
49 		if (EFI_ERROR(rc)) {
50 			CHAR16 Buffer[64];
51 			StatusToString(Buffer, rc);
52 			Print(L"%d: Bad response from QueryMode: %s (%d)\n",
53 				i, Buffer, rc);
54 			continue;
55 		}
56 		Print(L"%c%d: %dx%d ", memcmp(info,gop->Mode->Info,sizeof(*info)) == 0 ? '*' : ' ', i,
57 			info->HorizontalResolution,
58 			info->VerticalResolution);
59 		switch(info->PixelFormat) {
60 			case PixelRedGreenBlueReserved8BitPerColor:
61 				Print(L"RGBR");
62 				break;
63 			case PixelBlueGreenRedReserved8BitPerColor:
64 				Print(L"BGRR");
65 				break;
66 			case PixelBitMask:
67 				Print(L"R:%08x G:%08x B:%08x X:%08x",
68 					info->PixelInformation.RedMask,
69 					info->PixelInformation.GreenMask,
70 					info->PixelInformation.BlueMask,
71 					info->PixelInformation.ReservedMask);
72 				break;
73 			case PixelBltOnly:
74 				Print(L"(blt only)");
75 				break;
76 			default:
77 				Print(L"(Invalid pixel format)");
78 				break;
79 		}
80 		Print(L" pitch %d\n", info->PixelsPerScanLine);
81 	}
82 }
83 
84 static EFI_STATUS
85 SetWatchdog(UINTN seconds)
86 {
87 	EFI_STATUS rc;
88 	rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
89 				0, NULL);
90 	if (EFI_ERROR(rc)) {
91 		CHAR16 Buffer[64];
92 		StatusToString(Buffer, rc);
93 		Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
94 	}
95 	return rc;
96 }
97 
98 EFI_STATUS
99 efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
100 {
101 	EFI_STATUS rc;
102 	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
103 
104 	InitializeLib(image_handle, systab);
105 
106 	SetWatchdog(10);
107 
108 	rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
109 	if (EFI_ERROR(rc))
110 		return rc;
111 
112 	print_modes(gop);
113 
114 	SetWatchdog(0);
115 	return EFI_SUCCESS;
116 }
117