Blame SOURCES/0021-apps-Add-bltgrid-and-lfbgrid-and-add-error-checks-to.patch

67650e
From 5ec879ace760faacfa940a1933e0339e8f3811bd Mon Sep 17 00:00:00 2001
67650e
From: Peter Jones <pjones@redhat.com>
67650e
Date: Tue, 13 Mar 2018 15:20:31 -0400
67650e
Subject: [PATCH 21/25] apps: Add bltgrid and lfbgrid, and add error checks to
67650e
 modelist
67650e
67650e
This adds bltgrid and lfbgrid, which draw checkerboards using GOP's
67650e
Blt() and linear framebuffer, respectively, and adds some error checks
67650e
to modelist.efi.
67650e
67650e
Signed-off-by: Peter Jones <pjones@redhat.com>
67650e
Signed-off-by: Nigel Croxon <ncroxon@redhat.com>
67650e
---
67650e
 apps/bltgrid.c  | 131 +++++++++++++++++++++++++++++++++++++++++++
67650e
 apps/lfbgrid.c  | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
67650e
 apps/modelist.c |  56 +++++++++----------
67650e
 apps/Makefile   |   3 +-
67650e
 4 files changed, 328 insertions(+), 32 deletions(-)
67650e
 create mode 100644 apps/bltgrid.c
67650e
 create mode 100644 apps/lfbgrid.c
67650e
67650e
diff --git a/apps/bltgrid.c b/apps/bltgrid.c
67650e
new file mode 100644
67650e
index 00000000000..2adde6a3211
67650e
--- /dev/null
67650e
+++ b/apps/bltgrid.c
67650e
@@ -0,0 +1,131 @@
67650e
+#include <efi.h>
67650e
+#include <efilib.h>
67650e
+
67650e
+extern EFI_GUID GraphicsOutputProtocol;
67650e
+
67650e
+static void
67650e
+fill_boxes(UINT32 *PixelBuffer, UINT32 Width, UINT32 Height)
67650e
+{
67650e
+	UINT32 y, x = 0;
67650e
+	/*
67650e
+	 * This assums BGRR, but it doesn't really matter; we pick red and
67650e
+	 * green so it'll just be blue/green if the pixel format is backwards.
67650e
+	 */
67650e
+	EFI_GRAPHICS_OUTPUT_BLT_PIXEL Red = {0, 0, 0xff, 0},
67650e
+				      Green = {0, 0xff, 0, 0},
67650e
+				      *Color;
67650e
+
67650e
+	for (y = 0; y < Height; y++) {
67650e
+		Color = ((y / 32) % 2 == 0) ? &Red : &Gree;;
67650e
+		for (x = 0; x < Width; x++) {
67650e
+			if (x % 32 == 0 && x != 0)
67650e
+				Color = (Color == &Red) ? &Green : &Red;
67650e
+			PixelBuffer[y * Width + x] = *(UINT32 *)Color;
67650e
+		}
67650e
+	}
67650e
+}
67650e
+
67650e
+static void
67650e
+draw_boxes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
67650e
+{
67650e
+	int i, imax;
67650e
+	EFI_STATUS rc;
67650e
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
67650e
+	UINTN NumPixels;
67650e
+	UINT32 *PixelBuffer;
67650e
+	UINT32 BufferSize;
67650e
+
67650e
+	if (gop->Mode) {
67650e
+		imax = gop->Mode->MaxMode;
67650e
+	} else {
67650e
+		Print(L"gop->Mode is NULL\n");
67650e
+		return;
67650e
+	}
67650e
+
67650e
+	for (i = 0; i < imax; i++) {
67650e
+		UINTN SizeOfInfo;
67650e
+		rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
67650e
+					&info;;
67650e
+		if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
67650e
+			Print(L"gop->QueryMode() returned %r\n", rc);
67650e
+			Print(L"Trying to start GOP with SetMode().\n");
67650e
+			rc = uefi_call_wrapper(gop->SetMode, 2, gop,
67650e
+				gop->Mode ? gop->Mode->Mode : 0);
67650e
+			rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
67650e
+				&SizeOfInfo, &info;;
67650e
+		}
67650e
+
67650e
+		if (EFI_ERROR(rc)) {
67650e
+			Print(L"%d: Bad response from QueryMode: %r (%d)\n",
67650e
+			      i, rc, rc);
67650e
+			continue;
67650e
+		}
67650e
+
67650e
+		if (CompareMem(info, gop->Mode->Info, sizeof (*info)))
67650e
+			continue;
67650e
+
67650e
+		NumPixels = info->VerticalResolution * info->HorizontalResolution;
67650e
+		BufferSize = NumPixels * sizeof(UINT32);
67650e
+
67650e
+		PixelBuffer = AllocatePool(BufferSize);
67650e
+		if (!PixelBuffer) {
67650e
+			Print(L"Allocation of 0x%08lx bytes failed.\n",
67650e
+			      sizeof(UINT32) * NumPixels);
67650e
+			return;
67650e
+		}
67650e
+
67650e
+		fill_boxes(PixelBuffer,
67650e
+			   info->HorizontalResolution, info->VerticalResolution);
67650e
+
67650e
+		uefi_call_wrapper(gop->Blt, 10, gop,
67650e
+				  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)PixelBuffer,
67650e
+				  EfiBltBufferToVideo,
67650e
+				  0, 0, 0, 0,
67650e
+				  info->HorizontalResolution,
67650e
+				  info->VerticalResolution,
67650e
+				  0);
67650e
+		return;
67650e
+	}
67650e
+	Print(L"Never found the active video mode?\n");
67650e
+}
67650e
+
67650e
+static EFI_STATUS
67650e
+SetWatchdog(UINTN seconds)
67650e
+{
67650e
+	EFI_STATUS rc;
67650e
+	rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
67650e
+				0, NULL);
67650e
+	if (EFI_ERROR(rc)) {
67650e
+		CHAR16 Buffer[64];
67650e
+		StatusToString(Buffer, rc);
67650e
+		Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
67650e
+	}
67650e
+	return rc;
67650e
+}
67650e
+
67650e
+EFI_STATUS
67650e
+efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
67650e
+{
67650e
+	EFI_STATUS rc;
67650e
+	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
67650e
+
67650e
+	InitializeLib(image_handle, systab);
67650e
+
67650e
+	SetWatchdog(10);
67650e
+
67650e
+	rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
67650e
+	if (EFI_ERROR(rc)) {
67650e
+		Print(L"Could not locate GOP: %r\n", rc);
67650e
+		return rc;
67650e
+	}
67650e
+
67650e
+	if (!gop) {
67650e
+		Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
67650e
+		return EFI_UNSUPPORTED;
67650e
+	}
67650e
+
67650e
+	draw_boxes(gop);
67650e
+
67650e
+	SetWatchdog(0);
67650e
+	return EFI_SUCCESS;
67650e
+}
67650e
diff --git a/apps/lfbgrid.c b/apps/lfbgrid.c
67650e
new file mode 100644
67650e
index 00000000000..53a255afbb9
67650e
--- /dev/null
67650e
+++ b/apps/lfbgrid.c
67650e
@@ -0,0 +1,170 @@
67650e
+#include <efi.h>
67650e
+#include <efilib.h>
67650e
+
67650e
+extern EFI_GUID GraphicsOutputProtocol;
67650e
+
67650e
+#define be32_to_cpu(x) __builtin_bswap32(x)
67650e
+
67650e
+static void
67650e
+fill_boxes(UINT32 *PixelBuffer, UINT32 Width, UINT32 Height, UINT32 Pitch,
67650e
+	   EFI_GRAPHICS_PIXEL_FORMAT Format, EFI_PIXEL_BITMASK Info )
67650e
+{
67650e
+	UINT32 Red, Green;
67650e
+	UINT32 y, x, color;
67650e
+
67650e
+	switch(Format) {
67650e
+	case PixelRedGreenBlueReserved8BitPerColor:
67650e
+		Red = be32_to_cpu(0xff000000);
67650e
+		Green = be32_to_cpu(0x00ff0000);
67650e
+		break;
67650e
+	case PixelBlueGreenRedReserved8BitPerColor:
67650e
+		Red = be32_to_cpu(0x0000ff00);
67650e
+		Green = be32_to_cpu(0x00ff0000);
67650e
+		break;
67650e
+	case PixelBitMask:
67650e
+		Red = Info.RedMask;
67650e
+		Green = Info.GreenMask;
67650e
+		break;
67650e
+	case PixelBltOnly:
67650e
+		return;
67650e
+	default:
67650e
+		Print(L"Invalid pixel format\n");
67650e
+		return;
67650e
+	}
67650e
+
67650e
+	for (y = 0; y < Height; y++) {
67650e
+		color = ((y / 32) % 2 == 0) ? Red : Green;
67650e
+		for (x = 0; x < Width; x++) {
67650e
+			if (x % 32 == 0 && x != 0)
67650e
+				color = (color == Red) ? Green : Red;
67650e
+			PixelBuffer[y * Pitch + x] = color;
67650e
+		}
67650e
+	}
67650e
+}
67650e
+
67650e
+static void
67650e
+draw_boxes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
67650e
+{
67650e
+	int i, imax;
67650e
+	EFI_STATUS rc;
67650e
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
67650e
+	UINTN NumPixels;
67650e
+	UINT32 *PixelBuffer;
67650e
+	UINT32 CopySize, BufferSize;
67650e
+#if defined(__x86_64__) || defined(__aarch64__)
67650e
+	UINT64 FrameBufferAddr;
67650e
+#elif defined(__i386__) || defined(__arm__)
67650e
+	UINT32 FrameBufferAddr;
67650e
+#else
67650e
+#error YOUR ARCH HERE
67650e
+#endif
67650e
+
67650e
+	if (gop->Mode) {
67650e
+		imax = gop->Mode->MaxMode;
67650e
+	} else {
67650e
+		Print(L"gop->Mode is NULL\n");
67650e
+		return;
67650e
+	}
67650e
+
67650e
+	for (i = 0; i < imax; i++) {
67650e
+		UINTN SizeOfInfo;
67650e
+		rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
67650e
+					&info;;
67650e
+		if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
67650e
+			Print(L"gop->QueryMode() returned %r\n", rc);
67650e
+			Print(L"Trying to start GOP with SetMode().\n");
67650e
+			rc = uefi_call_wrapper(gop->SetMode, 2, gop,
67650e
+				gop->Mode ? gop->Mode->Mode : 0);
67650e
+			rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
67650e
+				&SizeOfInfo, &info;;
67650e
+		}
67650e
+
67650e
+		if (EFI_ERROR(rc)) {
67650e
+			Print(L"%d: Bad response from QueryMode: %r (%d)\n",
67650e
+			      i, rc, rc);
67650e
+			continue;
67650e
+		}
67650e
+
67650e
+		if (CompareMem(info, gop->Mode->Info, sizeof (*info)))
67650e
+			continue;
67650e
+
67650e
+		NumPixels = info->VerticalResolution * info->PixelsPerScanLine;
67650e
+		BufferSize = NumPixels * sizeof(UINT32);
67650e
+		if (BufferSize == gop->Mode->FrameBufferSize) {
67650e
+			CopySize = BufferSize;
67650e
+		} else {
67650e
+			CopySize = BufferSize < gop->Mode->FrameBufferSize ?
67650e
+				BufferSize : gop->Mode->FrameBufferSize;
67650e
+			Print(L"height * pitch * pixelsize = %lu buf fb size is %lu; using %lu\n",
67650e
+			      BufferSize, gop->Mode->FrameBufferSize, CopySize);
67650e
+		}
67650e
+
67650e
+		PixelBuffer = AllocatePool(BufferSize);
67650e
+		if (!PixelBuffer) {
67650e
+			Print(L"Allocation of 0x%08lx bytes failed.\n",
67650e
+			      sizeof(UINT32) * NumPixels);
67650e
+			return;
67650e
+		}
67650e
+
67650e
+		fill_boxes(PixelBuffer, info->HorizontalResolution,
67650e
+			   info->VerticalResolution, info->PixelsPerScanLine,
67650e
+			   info->PixelFormat, info->PixelInformation);
67650e
+
67650e
+		if (info->PixelFormat == PixelBltOnly) {
67650e
+			Print(L"No linear framebuffer on this device.\n");
67650e
+			return;
67650e
+		}
67650e
+#if defined(__x86_64__) || defined(__aarch64__)
67650e
+		FrameBufferAddr = (UINT64)gop->Mode->FrameBufferBase;
67650e
+#elif defined(__i386__) || defined(__arm__)
67650e
+		FrameBufferAddr = (UINT32)(UINT64)gop->Mode->FrameBufferBase;
67650e
+#else
67650e
+#error YOUR ARCH HERE
67650e
+#endif
67650e
+
67650e
+		CopyMem((VOID *)FrameBufferAddr, PixelBuffer, CopySize);
67650e
+		return;
67650e
+	}
67650e
+	Print(L"Never found the active video mode?\n");
67650e
+}
67650e
+
67650e
+static EFI_STATUS
67650e
+SetWatchdog(UINTN seconds)
67650e
+{
67650e
+	EFI_STATUS rc;
67650e
+	rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
67650e
+				0, NULL);
67650e
+	if (EFI_ERROR(rc)) {
67650e
+		CHAR16 Buffer[64];
67650e
+		StatusToString(Buffer, rc);
67650e
+		Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
67650e
+	}
67650e
+	return rc;
67650e
+}
67650e
+
67650e
+EFI_STATUS
67650e
+efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
67650e
+{
67650e
+	EFI_STATUS rc;
67650e
+	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
67650e
+
67650e
+	InitializeLib(image_handle, systab);
67650e
+
67650e
+	SetWatchdog(10);
67650e
+
67650e
+	rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
67650e
+	if (EFI_ERROR(rc)) {
67650e
+		Print(L"Could not locate GOP: %r\n", rc);
67650e
+		return rc;
67650e
+	}
67650e
+
67650e
+	if (!gop) {
67650e
+		Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
67650e
+		return EFI_UNSUPPORTED;
67650e
+	}
67650e
+
67650e
+	draw_boxes(gop);
67650e
+
67650e
+	SetWatchdog(0);
67650e
+	return EFI_SUCCESS;
67650e
+}
67650e
diff --git a/apps/modelist.c b/apps/modelist.c
67650e
index 8d816d1d9da..26892e1d5f5 100644
67650e
--- a/apps/modelist.c
67650e
+++ b/apps/modelist.c
67650e
@@ -3,57 +3,44 @@
67650e
 
67650e
 extern EFI_GUID GraphicsOutputProtocol;
67650e
 
67650e
-static int memcmp(const void *s1, const void *s2, UINTN n)
67650e
-{
67650e
-	const unsigned char *c1 = s1, *c2 = s2;
67650e
-	int d = 0;
67650e
-
67650e
-	if (!s1 && !s2)
67650e
-		return 0;
67650e
-	if (s1 && !s2)
67650e
-		return 1;
67650e
-	if (!s1 && s2)
67650e
-		return -1;
67650e
-
67650e
-	while (n--) {
67650e
-		d = (int)*c1++ - (int)*c2++;
67650e
-		if (d)
67650e
-			break;
67650e
-	}
67650e
-	return d;
67650e
-}
67650e
-
67650e
 static void
67650e
 print_modes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
67650e
 {
67650e
 	int i, imax;
67650e
 	EFI_STATUS rc;
67650e
 
67650e
-	imax = gop->Mode->MaxMode;
67650e
+	if (gop->Mode) {
67650e
+		imax = gop->Mode->MaxMode;
67650e
+		Print(L"GOP reports MaxMode %d\n", imax);
67650e
+	} else {
67650e
+		Print(L"gop->Mode is NULL\n");
67650e
+		imax = 1;
67650e
+	}
67650e
 
67650e
-	Print(L"GOP reports MaxMode %d\n", imax);
67650e
 	for (i = 0; i < imax; i++) {
67650e
 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
67650e
 		UINTN SizeOfInfo;
67650e
 		rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
67650e
 					&info;;
67650e
 		if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
67650e
+			Print(L"gop->QueryMode() returned %r\n", rc);
67650e
+			Print(L"Trying to start GOP with SetMode().\n");
67650e
 			rc = uefi_call_wrapper(gop->SetMode, 2, gop,
67650e
-				gop->Mode->Mode);
67650e
+				gop->Mode ? gop->Mode->Mode : 0);
67650e
 			rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
67650e
 				&SizeOfInfo, &info;;
67650e
 		}
67650e
 
67650e
 		if (EFI_ERROR(rc)) {
67650e
-			CHAR16 Buffer[64];
67650e
-			StatusToString(Buffer, rc);
67650e
-			Print(L"%d: Bad response from QueryMode: %s (%d)\n",
67650e
-				i, Buffer, rc);
67650e
+			Print(L"%d: Bad response from QueryMode: %r (%d)\n",
67650e
+			      i, rc, rc);
67650e
 			continue;
67650e
 		}
67650e
-		Print(L"%c%d: %dx%d ", memcmp(info,gop->Mode->Info,sizeof(*info)) == 0 ? '*' : ' ', i,
67650e
-			info->HorizontalResolution,
67650e
-			info->VerticalResolution);
67650e
+		Print(L"%c%d: %dx%d ",
67650e
+		      (gop->Mode &&
67650e
+		       CompareMem(info,gop->Mode->Info,sizeof(*info)) == 0
67650e
+		       ) ? '*' : ' ',
67650e
+		      i, info->HorizontalResolution, info->VerticalResolution);
67650e
 		switch(info->PixelFormat) {
67650e
 			case PixelRedGreenBlueReserved8BitPerColor:
67650e
 				Print(L"RGBR");
67650e
@@ -104,8 +91,15 @@ efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
67650e
 	SetWatchdog(10);
67650e
 
67650e
 	rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
67650e
-	if (EFI_ERROR(rc))
67650e
+	if (EFI_ERROR(rc)) {
67650e
+		Print(L"Could not locate GOP: %r\n", rc);
67650e
 		return rc;
67650e
+	}
67650e
+
67650e
+	if (!gop) {
67650e
+		Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
67650e
+		return EFI_UNSUPPORTED;
67650e
+	}
67650e
 
67650e
 	print_modes(gop);
67650e
 
67650e
diff --git a/apps/Makefile b/apps/Makefile
67650e
index 5fffd4254c8..4e26444c469 100644
67650e
--- a/apps/Makefile
67650e
+++ b/apps/Makefile
67650e
@@ -61,7 +61,8 @@ LOADLIBES	+= -T $(LDSCRIPT)
67650e
 TARGET_APPS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi \
67650e
 	      printenv.efi t7.efi t8.efi tcc.efi modelist.efi \
67650e
 	      route80h.efi drv0_use.efi AllocPages.efi exit.efi \
67650e
-	      FreePages.efi setjmp.efi debughook.efi debughook.efi.debug
67650e
+	      FreePages.efi setjmp.efi debughook.efi debughook.efi.debug \
67650e
+	      bltgrid.efi lfbgrid.efi
67650e
 TARGET_BSDRIVERS = drv0.efi
67650e
 TARGET_RTDRIVERS =
67650e
 
67650e
-- 
67650e
2.15.0
67650e