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

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