From 295f6bf8d4bc93d1aedef218f14c8f20f101a1af Mon Sep 17 00:00:00 2001
From: Nico Huber <nico.h@gmx.de>
Date: Fri, 14 Jul 2023 14:20:39 +0200
Subject: [PATCH] linux_trampoline: Handle coreboot framebuffer

Translate the coreboot framebuffer info from coreboot tables to
the Linux zero page.

Tested in QEMU/Q35 with a kernel w/ efifb enabled.

Change-Id: I2447b2366df8dd8ffe741c943de544d8b4d02dff
Signed-off-by: Nico Huber <nico.h@gmx.de>
Co-authored-by: Bill XIE <persmule@hardenedlinux.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/76431
Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Tim Wawrzynczak <inforichland@gmail.com>
Reviewed-by: Samuel Holland <samuel@sholland.org>
Reviewed-by: Bill XIE <persmule@hardenedlinux.org>
---

diff --git a/util/cbfstool/linux_trampoline.S b/util/cbfstool/linux_trampoline.S
index 767c8be..d36e60d 100644
--- a/util/cbfstool/linux_trampoline.S
+++ b/util/cbfstool/linux_trampoline.S
@@ -102,7 +102,36 @@
 .testFramebuffer:
 cmp $CB_TAG_FRAMEBUFFER, (%ebx)
 jne .endScan
-/* TODO: handle framebuffer tag */
+
+cmpl $0, 0x0c(%ebx)    /* check if upper 32-bit of framebuffer address are 0 */
+jne .endScan
+
+mov $LINUX_PARAM_LOC, %edi   /* translate the framebuffer entry into Linux' struct screen_info */
+mov 0x08(%ebx), %eax   /* physical_address   */
+mov %eax, 0x18(%edi)   /* -> lfb_base        */
+mov 0x10(%ebx), %eax   /* x_resolution       */
+mov %ax,  0x12(%edi)   /* -> lfb_width       */
+mov 0x14(%ebx), %eax   /* y_resolution       */
+mov %ax,  0x14(%edi)   /* -> lfb_height      */
+mov 0x18(%ebx), %edx   /* bytes_per_line     */
+mov %dx,  0x24(%edi)   /* -> lfb_linelength  */
+
+mul %edx    /* bytes_per_line * y_resolution */
+mov %eax, 0x1c(%edi)   /* -> lfb_size        */
+
+movzbw 0x1c(%ebx), %ax /* bits_per_pixel     */
+mov %ax,  0x16(%edi)   /* -> lfb_depth       */
+
+mov $4, %esi           /* Copy 4 color components' pos and size, each 1 byte. */
+1:
+mov 0x1b(%ebx, %esi, 2), %ax
+rol %ax                /* Order is reversed for Linux, hence swap. */
+mov %ax, 0x24(%edi, %esi, 2)
+dec %esi
+jnz 1b
+
+#define LFB_EFI_SIMPLE 0x70      /* VIDEO_TYPE_EFI in Linux */
+movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA     */
 
 .endScan:
 add 4(%ebx), %ebx
diff --git a/util/cbfstool/linux_trampoline.c b/util/cbfstool/linux_trampoline.c
index 46bb800..87c595a 100644
--- a/util/cbfstool/linux_trampoline.c
+++ b/util/cbfstool/linux_trampoline.c
@@ -3,18 +3,23 @@
 unsigned char trampoline[] = {
   0xfc, 0x31, 0xd2, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x01, 0x00, 0x01, 0xcb, 0x8b,
   0x01, 0x3d, 0x4c, 0x42, 0x49, 0x4f, 0x74, 0x07, 0x83, 0xc1, 0x10, 0x39, 0xcb, 0x75, 0xe9, 0x39,
-  0xcb, 0x0f, 0x84, 0xc5, 0x00, 0x00, 0x00, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83,
+  0xcb, 0x0f, 0x84, 0x12, 0x01, 0x00, 0x00, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83,
   0x3b, 0x11, 0x75, 0x05, 0x8b, 0x4b, 0x08, 0xeb, 0xcf, 0x83, 0x3b, 0x01, 0x75, 0x53, 0x8b, 0x43,
   0x04, 0x83, 0xe8, 0x08, 0xc1, 0xe8, 0x02, 0x3d, 0xa0, 0x00, 0x00, 0x00, 0x7e, 0x05, 0xb8, 0xa0,
   0x00, 0x00, 0x00, 0x89, 0xc6, 0xbf, 0x05, 0x00, 0x00, 0x00, 0xf7, 0xf7, 0xa3, 0xe8, 0x01, 0x09,
   0x00, 0x89, 0xf0, 0x91, 0x8d, 0x73, 0x08, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0xf3, 0xa5, 0x91, 0xa1,
-  0xe8, 0x01, 0x09, 0x00, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0x83, 0xf8, 0x00, 0x74, 0x2f, 0x83, 0x7f,
+  0xe8, 0x01, 0x09, 0x00, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0x83, 0xf8, 0x00, 0x74, 0x7c, 0x83, 0x7f,
   0x10, 0x0c, 0x7e, 0x07, 0xc7, 0x47, 0x10, 0x02, 0x00, 0x00, 0x00, 0x48, 0x83, 0xc7, 0x14, 0xeb,
   0xe8, 0x83, 0x3b, 0x43, 0x75, 0x12, 0x8b, 0x43, 0x08, 0xa3, 0x70, 0x00, 0x09, 0x00, 0x8b, 0x43,
-  0x0c, 0xa3, 0x74, 0x00, 0x09, 0x00, 0xeb, 0x05, 0x83, 0x3b, 0x12, 0x75, 0x00, 0x03, 0x5b, 0x04,
-  0x49, 0x0f, 0x85, 0x78, 0xff, 0xff, 0xff, 0xb8, 0x00, 0x00, 0x04, 0x00, 0xc7, 0x40, 0x10, 0xff,
-  0xff, 0x00, 0x00, 0xc7, 0x40, 0x14, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x40, 0x18, 0xff, 0xff, 0x00,
-  0x00, 0xc7, 0x40, 0x1c, 0x00, 0x93, 0xcf, 0x00, 0xc6, 0x00, 0x2b, 0x89, 0x40, 0x02, 0x0f, 0x01,
-  0x10, 0xbe, 0x00, 0x00, 0x09, 0x00, 0xff, 0x25, 0x14, 0x02, 0x09, 0x00, 0xf4, 0xeb, 0xfd
+  0x0c, 0xa3, 0x74, 0x00, 0x09, 0x00, 0xeb, 0x52, 0x83, 0x3b, 0x12, 0x75, 0x4d, 0x83, 0x7b, 0x0c,
+  0x00, 0x75, 0x47, 0xbf, 0x00, 0x00, 0x09, 0x00, 0x8b, 0x43, 0x08, 0x89, 0x47, 0x18, 0x8b, 0x43,
+  0x10, 0x66, 0x89, 0x47, 0x12, 0x8b, 0x43, 0x14, 0x66, 0x89, 0x47, 0x14, 0x8b, 0x53, 0x18, 0x66,
+  0x89, 0x57, 0x24, 0xf7, 0xe2, 0x89, 0x47, 0x1c, 0x66, 0x0f, 0xb6, 0x43, 0x1c, 0x66, 0x89, 0x47,
+  0x16, 0xbe, 0x04, 0x00, 0x00, 0x00, 0x66, 0x8b, 0x44, 0x73, 0x1b, 0x66, 0xd1, 0xc0, 0x66, 0x89,
+  0x44, 0x77, 0x24, 0x4e, 0x75, 0xf0, 0xc6, 0x47, 0x0f, 0x70, 0x03, 0x5b, 0x04, 0x49, 0x0f, 0x85,
+  0x2b, 0xff, 0xff, 0xff, 0xb8, 0x00, 0x00, 0x04, 0x00, 0xc7, 0x40, 0x10, 0xff, 0xff, 0x00, 0x00,
+  0xc7, 0x40, 0x14, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x40, 0x18, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x40,
+  0x1c, 0x00, 0x93, 0xcf, 0x00, 0xc6, 0x00, 0x2b, 0x89, 0x40, 0x02, 0x0f, 0x01, 0x10, 0xbe, 0x00,
+  0x00, 0x09, 0x00, 0xff, 0x25, 0x14, 0x02, 0x09, 0x00, 0xf4, 0xeb, 0xfd
 };
-unsigned int trampoline_len = 239;
+unsigned int trampoline_len = 316;
