linux_trampoline: Handle 64-bit framebuffer addresses

To support full 64-bit addresses, there is a new field `ext_lfb_base`
since Linux 4.1. It is unclear, however, how a loader is supposed to
know if the kernel is compatible with this. Filling these previously
reserved bits doesn't hurt, but an old kernel would probably ignore
them and not know that it's handling a clipped, invalid address. So
we play safe, and only allow 64-bit addresses for kernels after the
2.15 version bump of the boot protocol.

Change-Id: Ib20184cf207f092062a91ac3e6aa819b956efd33
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/+/76479
Reviewed-by: Bill XIE <persmule@hardenedlinux.org>
Reviewed-by: Tim Wawrzynczak <inforichland@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/util/cbfstool/cbfs-payload-linux.c b/util/cbfstool/cbfs-payload-linux.c
index f9d0d11..53d455c 100644
--- a/util/cbfstool/cbfs-payload-linux.c
+++ b/util/cbfstool/cbfs-payload-linux.c
@@ -265,6 +265,10 @@
 	 * this information for its jump to real Linux. */
 	params.kernel_start = kernel_base;
 
+	/* To make decisions based on the protocol version,
+	   copy that as well. */
+	params.param_block_version = hdr->protocol_version;
+
 	if (bzp_add_kernel(&bzp, input, setup_size) != 0)
 		return -1;
 
diff --git a/util/cbfstool/linux_trampoline.S b/util/cbfstool/linux_trampoline.S
index d36e60d..941ab3f 100644
--- a/util/cbfstool/linux_trampoline.S
+++ b/util/cbfstool/linux_trampoline.S
@@ -17,6 +17,7 @@
 
 #define ACPI_RSDP_ADDR 0x70
 #define E820_NR_OFFSET 0x1e8
+#define PROTOCOL_VERSION 0x206
 #define LINUX_ENTRY_OFFSET 0x214
 #define E820_OFFSET 0x2d0
 
@@ -103,12 +104,17 @@
 cmp $CB_TAG_FRAMEBUFFER, (%ebx)
 jne .endScan
 
+cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION)
+jge .framebufferSetup  /* protocol version >= 2.15 can handle 64-bit address */
 cmpl $0, 0x0c(%ebx)    /* check if upper 32-bit of framebuffer address are 0 */
 jne .endScan
 
+.framebufferSetup:
 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 0x0c(%ebx), %eax   /* physical_address   */
+mov %eax, 0x3a(%edi)   /* -> ext_lfb_base    */
 mov 0x10(%ebx), %eax   /* x_resolution       */
 mov %ax,  0x12(%edi)   /* -> lfb_width       */
 mov 0x14(%ebx), %eax   /* y_resolution       */
@@ -130,6 +136,9 @@
 dec %esi
 jnz 1b
 
+#define VIDEO_CAPABILITY_64BIT_BASE (1 << 1)
+movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi)
+
 #define LFB_EFI_SIMPLE 0x70      /* VIDEO_TYPE_EFI in Linux */
 movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA     */
 
diff --git a/util/cbfstool/linux_trampoline.c b/util/cbfstool/linux_trampoline.c
index 87c595a..794b245 100644
--- a/util/cbfstool/linux_trampoline.c
+++ b/util/cbfstool/linux_trampoline.c
@@ -3,23 +3,25 @@
 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, 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,
+  0xcb, 0x0f, 0x84, 0x2e, 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, 0x57, 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, 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, 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
+  0xe8, 0x01, 0x09, 0x00, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0x83, 0xf8, 0x00, 0x0f, 0x84, 0x94, 0x00,
+  0x00, 0x00, 0x83, 0x7f, 0x10, 0x0c, 0x7e, 0x07, 0xc7, 0x47, 0x10, 0x02, 0x00, 0x00, 0x00, 0x48,
+  0x83, 0xc7, 0x14, 0xeb, 0xe4, 0x83, 0x3b, 0x43, 0x75, 0x12, 0x8b, 0x43, 0x08, 0xa3, 0x70, 0x00,
+  0x09, 0x00, 0x8b, 0x43, 0x0c, 0xa3, 0x74, 0x00, 0x09, 0x00, 0xeb, 0x6a, 0x83, 0x3b, 0x12, 0x75,
+  0x65, 0x66, 0x81, 0x3d, 0x06, 0x02, 0x09, 0x00, 0x0f, 0x02, 0x7d, 0x06, 0x83, 0x7b, 0x0c, 0x00,
+  0x75, 0x54, 0xbf, 0x00, 0x00, 0x09, 0x00, 0x8b, 0x43, 0x08, 0x89, 0x47, 0x18, 0x8b, 0x43, 0x0c,
+  0x89, 0x47, 0x3a, 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, 0xc7, 0x47, 0x36, 0x02, 0x00,
+  0x00, 0x00, 0xc6, 0x47, 0x0f, 0x70, 0x03, 0x5b, 0x04, 0x49, 0x0f, 0x85, 0x0f, 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 = 316;
+unsigned int trampoline_len = 344;