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

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