bc092b
From 461bfab7b7e47fd0a76bf6076a438af1b7e2184a Mon Sep 17 00:00:00 2001
bc092b
From: Vladimir Serbinenko <phcoder@gmail.com>
bc092b
Date: Mon, 8 May 2017 19:06:22 +0200
31cddd
Subject: [PATCH] coreboot: Split parts that are platform-independent.
bc092b
bc092b
We currently assume that coreboot is always i386, it's no longer the case,
bc092b
so split i386-coreboot parts from generic coreboot code.
bc092b
---
bc092b
 grub-core/Makefile.core.def                      |  6 +-
bc092b
 grub-core/commands/i386/coreboot/cb_timestamps.c |  2 +-
bc092b
 grub-core/commands/i386/coreboot/cbls.c          |  2 +-
bc092b
 grub-core/kern/coreboot/cbtable.c                | 70 ++++++++++++++++++++++++
bc092b
 grub-core/kern/{i386 => }/coreboot/mmap.c        |  6 +-
bc092b
 grub-core/kern/i386/coreboot/cbtable.c           | 53 +++---------------
bc092b
 grub-core/term/i386/coreboot/cbmemc.c            |  2 +-
bc092b
 grub-core/video/{i386 => }/coreboot/cbfb.c       |  2 +-
bc092b
 include/grub/{i386 => }/coreboot/lbio.h          |  9 +++
ec4acb
 grub-core/Makefile.am                            |  2 +-
bc092b
 10 files changed, 99 insertions(+), 55 deletions(-)
bc092b
 create mode 100644 grub-core/kern/coreboot/cbtable.c
bc092b
 rename grub-core/kern/{i386 => }/coreboot/mmap.c (97%)
bc092b
 rename grub-core/video/{i386 => }/coreboot/cbfb.c (99%)
bc092b
 rename include/grub/{i386 => }/coreboot/lbio.h (93%)
bc092b
bc092b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
ec4acb
index fb59a7b1d6a..45ed0fa6b1c 100644
bc092b
--- a/grub-core/Makefile.core.def
bc092b
+++ b/grub-core/Makefile.core.def
bc092b
@@ -166,7 +166,7 @@ kernel = {
bc092b
   i386_multiboot = kern/i386/coreboot/init.c;
bc092b
   i386_qemu = kern/i386/qemu/init.c;
bc092b
   i386_coreboot_multiboot_qemu = term/i386/pc/vga_text.c;
bc092b
-  i386_coreboot = video/i386/coreboot/cbfb.c;
bc092b
+  i386_coreboot = video/coreboot/cbfb.c;
bc092b
 
bc092b
   efi = disk/efi/efidisk.c;
bc092b
   efi = kern/efi/efi.c;
bc092b
@@ -227,8 +227,9 @@ kernel = {
bc092b
   i386_qemu = kern/vga_init.c;
bc092b
   i386_qemu = kern/i386/qemu/mmap.c;
bc092b
 
bc092b
-  i386_coreboot = kern/i386/coreboot/mmap.c;
bc092b
+  i386_coreboot = kern/coreboot/mmap.c;
bc092b
   i386_coreboot = kern/i386/coreboot/cbtable.c;
bc092b
+  i386_coreboot = kern/coreboot/cbtable.c;
bc092b
 
bc092b
   i386_multiboot = kern/i386/multiboot_mmap.c;
bc092b
 
bc092b
@@ -643,6 +644,7 @@ module = {
bc092b
 module = {
bc092b
   name = cbtable;
bc092b
   common = kern/i386/coreboot/cbtable.c;
bc092b
+  common = kern/coreboot/cbtable.c;
bc092b
   enable = i386_pc;
bc092b
   enable = i386_efi;
bc092b
   enable = i386_qemu;
bc092b
diff --git a/grub-core/commands/i386/coreboot/cb_timestamps.c b/grub-core/commands/i386/coreboot/cb_timestamps.c
ec4acb
index e72f38d6e05..e97ea6bed98 100644
bc092b
--- a/grub-core/commands/i386/coreboot/cb_timestamps.c
bc092b
+++ b/grub-core/commands/i386/coreboot/cb_timestamps.c
bc092b
@@ -20,7 +20,7 @@
bc092b
 #include <grub/misc.h>
bc092b
 #include <grub/command.h>
bc092b
 #include <grub/i18n.h>
bc092b
-#include <grub/i386/coreboot/lbio.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/i386/tsc.h>
bc092b
 
bc092b
 GRUB_MOD_LICENSE ("GPLv3+");
bc092b
diff --git a/grub-core/commands/i386/coreboot/cbls.c b/grub-core/commands/i386/coreboot/cbls.c
ec4acb
index e0a10596fe2..102291f424a 100644
bc092b
--- a/grub-core/commands/i386/coreboot/cbls.c
bc092b
+++ b/grub-core/commands/i386/coreboot/cbls.c
bc092b
@@ -20,7 +20,7 @@
bc092b
 #include <grub/misc.h>
bc092b
 #include <grub/command.h>
bc092b
 #include <grub/i18n.h>
bc092b
-#include <grub/i386/coreboot/lbio.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/i386/tsc.h>
bc092b
 
bc092b
 GRUB_MOD_LICENSE ("GPLv3+");
bc092b
diff --git a/grub-core/kern/coreboot/cbtable.c b/grub-core/kern/coreboot/cbtable.c
bc092b
new file mode 100644
ec4acb
index 00000000000..996d3f40738
bc092b
--- /dev/null
bc092b
+++ b/grub-core/kern/coreboot/cbtable.c
bc092b
@@ -0,0 +1,70 @@
bc092b
+/*
bc092b
+ *  GRUB  --  GRand Unified Bootloader
bc092b
+ *  Copyright (C) 2007,2008,2013  Free Software Foundation, Inc.
bc092b
+ *
bc092b
+ *  GRUB is free software: you can redistribute it and/or modify
bc092b
+ *  it under the terms of the GNU General Public License as published by
bc092b
+ *  the Free Software Foundation, either version 3 of the License, or
bc092b
+ *  (at your option) any later version.
bc092b
+ *
bc092b
+ *  GRUB is distributed in the hope that it will be useful,
bc092b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
bc092b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bc092b
+ *  GNU General Public License for more details.
bc092b
+ *
bc092b
+ *  You should have received a copy of the GNU General Public License
bc092b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
bc092b
+ */
bc092b
+
bc092b
+#include <grub/i386/coreboot/memory.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
+#include <grub/types.h>
bc092b
+#include <grub/err.h>
bc092b
+#include <grub/misc.h>
bc092b
+#include <grub/dl.h>
bc092b
+
bc092b
+/* Helper for grub_linuxbios_table_iterate.  */
bc092b
+int
bc092b
+grub_linuxbios_check_signature (grub_linuxbios_table_header_t tbl_header)
bc092b
+{
bc092b
+  if (! grub_memcmp (tbl_header->signature, "LBIO", 4))
bc092b
+    return 1;
bc092b
+
bc092b
+  return 0;
bc092b
+}
bc092b
+
bc092b
+grub_err_t
bc092b
+grub_linuxbios_table_iterate (int (*hook) (grub_linuxbios_table_item_t,
bc092b
+					   void *),
bc092b
+			      void *hook_data)
bc092b
+{
bc092b
+  grub_linuxbios_table_header_t table_header = grub_linuxbios_get_tables ();
bc092b
+  grub_linuxbios_table_item_t table_item;
bc092b
+
bc092b
+  if (!table_header)
bc092b
+    return 0;
bc092b
+
bc092b
+signature_found:
bc092b
+
bc092b
+  table_item =
bc092b
+    (grub_linuxbios_table_item_t) ((char *) table_header +
bc092b
+				   table_header->header_size);
bc092b
+  for (; table_item < (grub_linuxbios_table_item_t) ((char *) table_header
bc092b
+						     + table_header->header_size
bc092b
+						     + table_header->table_size);
bc092b
+       table_item = (grub_linuxbios_table_item_t) ((char *) table_item + table_item->size))
bc092b
+    {
bc092b
+      if (table_item->tag == GRUB_LINUXBIOS_MEMBER_LINK
bc092b
+         && grub_linuxbios_check_signature ((grub_linuxbios_table_header_t) (grub_addr_t)
bc092b
+                             *(grub_uint64_t *) (table_item + 1)))
bc092b
+       {
bc092b
+         table_header = (grub_linuxbios_table_header_t) (grub_addr_t)
bc092b
+           *(grub_uint64_t *) (table_item + 1);
bc092b
+         goto signature_found;   
bc092b
+       }
bc092b
+      if (hook (table_item, hook_data))
bc092b
+       return 1;
bc092b
+    }
bc092b
+
bc092b
+  return 0;
bc092b
+}
bc092b
diff --git a/grub-core/kern/i386/coreboot/mmap.c b/grub-core/kern/coreboot/mmap.c
bc092b
similarity index 97%
bc092b
rename from grub-core/kern/i386/coreboot/mmap.c
bc092b
rename to grub-core/kern/coreboot/mmap.c
ec4acb
index 4d29f6b7d90..caf8f7cef1b 100644
bc092b
--- a/grub-core/kern/i386/coreboot/mmap.c
bc092b
+++ b/grub-core/kern/coreboot/mmap.c
bc092b
@@ -16,8 +16,8 @@
bc092b
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
bc092b
  */
bc092b
 
bc092b
-#include <grub/machine/memory.h>
bc092b
-#include <grub/machine/lbio.h>
bc092b
+#include <grub/memory.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/types.h>
bc092b
 #include <grub/err.h>
bc092b
 #include <grub/misc.h>
bc092b
@@ -49,6 +49,7 @@ iterate_linuxbios_table (grub_linuxbios_table_item_t table_item, void *data)
bc092b
     {
bc092b
       grub_uint64_t start = mem_region->addr;
bc092b
       grub_uint64_t end = mem_region->addr + mem_region->size;
bc092b
+#ifdef __i386__
bc092b
       /* Mark region 0xa0000 - 0x100000 as reserved.  */
bc092b
       if (start < 0x100000 && end >= 0xa0000
bc092b
 	  && mem_region->type == GRUB_MACHINE_MEMORY_AVAILABLE)
bc092b
@@ -75,6 +76,7 @@ iterate_linuxbios_table (grub_linuxbios_table_item_t table_item, void *data)
bc092b
 	  if (end <= start)
bc092b
 	    continue;
bc092b
 	}
bc092b
+#endif
bc092b
       if (ctx->hook (start, end - start,
bc092b
 		     /* Multiboot mmaps match with the coreboot mmap
bc092b
 		        definition.  Therefore, we can just pass type
bc092b
diff --git a/grub-core/kern/i386/coreboot/cbtable.c b/grub-core/kern/i386/coreboot/cbtable.c
ec4acb
index 1669bc0ca23..34a2b59be1f 100644
bc092b
--- a/grub-core/kern/i386/coreboot/cbtable.c
bc092b
+++ b/grub-core/kern/i386/coreboot/cbtable.c
bc092b
@@ -17,7 +17,7 @@
bc092b
  */
bc092b
 
bc092b
 #include <grub/i386/coreboot/memory.h>
bc092b
-#include <grub/i386/coreboot/lbio.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/types.h>
bc092b
 #include <grub/err.h>
bc092b
 #include <grub/misc.h>
bc092b
@@ -25,59 +25,20 @@
bc092b
 
bc092b
 GRUB_MOD_LICENSE ("GPLv3+");
bc092b
 
bc092b
-/* Helper for grub_linuxbios_table_iterate.  */
bc092b
-static int
bc092b
-check_signature (grub_linuxbios_table_header_t tbl_header)
bc092b
-{
bc092b
-  if (! grub_memcmp (tbl_header->signature, "LBIO", 4))
bc092b
-    return 1;
bc092b
-
bc092b
-  return 0;
bc092b
-}
bc092b
-
bc092b
-grub_err_t
bc092b
-grub_linuxbios_table_iterate (int (*hook) (grub_linuxbios_table_item_t,
bc092b
-					   void *),
bc092b
-			      void *hook_data)
bc092b
+grub_linuxbios_table_header_t
bc092b
+grub_linuxbios_get_tables (void)
bc092b
 {
bc092b
   grub_linuxbios_table_header_t table_header;
bc092b
-  grub_linuxbios_table_item_t table_item;
bc092b
-
bc092b
   /* Assuming table_header is aligned to its size (8 bytes).  */
bc092b
-
bc092b
   for (table_header = (grub_linuxbios_table_header_t) 0x500;
bc092b
        table_header < (grub_linuxbios_table_header_t) 0x1000; table_header++)
bc092b
-    if (check_signature (table_header))
bc092b
-      goto signature_found;
bc092b
+    if (grub_linuxbios_check_signature (table_header))
bc092b
+      return table_header;
bc092b
 
bc092b
   for (table_header = (grub_linuxbios_table_header_t) 0xf0000;
bc092b
        table_header < (grub_linuxbios_table_header_t) 0x100000; table_header++)
bc092b
-    if (check_signature (table_header))
bc092b
-      goto signature_found;
bc092b
-
bc092b
-  return 0;
bc092b
-
bc092b
-signature_found:
bc092b
-
bc092b
-  table_item =
bc092b
-    (grub_linuxbios_table_item_t) ((char *) table_header +
bc092b
-				   table_header->header_size);
bc092b
-  for (; table_item < (grub_linuxbios_table_item_t) ((char *) table_header
bc092b
-						     + table_header->header_size
bc092b
-						     + table_header->table_size);
bc092b
-       table_item = (grub_linuxbios_table_item_t) ((char *) table_item + table_item->size))
bc092b
-    {
bc092b
-      if (table_item->tag == GRUB_LINUXBIOS_MEMBER_LINK
bc092b
-         && check_signature ((grub_linuxbios_table_header_t) (grub_addr_t)
bc092b
-                             *(grub_uint64_t *) (table_item + 1)))
bc092b
-       {
bc092b
-         table_header = (grub_linuxbios_table_header_t) (grub_addr_t)
bc092b
-           *(grub_uint64_t *) (table_item + 1);
bc092b
-         goto signature_found;   
bc092b
-       }
bc092b
-      if (hook (table_item, hook_data))
bc092b
-       return 1;
bc092b
-    }
bc092b
+    if (grub_linuxbios_check_signature (table_header))
bc092b
+      return table_header;
bc092b
 
bc092b
   return 0;
bc092b
 }
bc092b
diff --git a/grub-core/term/i386/coreboot/cbmemc.c b/grub-core/term/i386/coreboot/cbmemc.c
ec4acb
index 25e64a05c03..129248c7f47 100644
bc092b
--- a/grub-core/term/i386/coreboot/cbmemc.c
bc092b
+++ b/grub-core/term/i386/coreboot/cbmemc.c
bc092b
@@ -23,7 +23,7 @@
bc092b
 #include <grub/time.h>
bc092b
 #include <grub/terminfo.h>
bc092b
 #include <grub/dl.h>
bc092b
-#include <grub/i386/coreboot/lbio.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/command.h>
bc092b
 #include <grub/normal.h>
bc092b
 
bc092b
diff --git a/grub-core/video/i386/coreboot/cbfb.c b/grub-core/video/coreboot/cbfb.c
bc092b
similarity index 99%
bc092b
rename from grub-core/video/i386/coreboot/cbfb.c
bc092b
rename to grub-core/video/coreboot/cbfb.c
ec4acb
index dede0c37ea3..9af81fa5b01 100644
bc092b
--- a/grub-core/video/i386/coreboot/cbfb.c
bc092b
+++ b/grub-core/video/coreboot/cbfb.c
bc092b
@@ -25,7 +25,7 @@
bc092b
 #include <grub/mm.h>
bc092b
 #include <grub/video.h>
bc092b
 #include <grub/video_fb.h>
bc092b
-#include <grub/machine/lbio.h>
bc092b
+#include <grub/coreboot/lbio.h>
bc092b
 #include <grub/machine/console.h>
bc092b
 
bc092b
 struct grub_linuxbios_table_framebuffer *grub_video_coreboot_fbtable;
bc092b
diff --git a/include/grub/i386/coreboot/lbio.h b/include/grub/coreboot/lbio.h
bc092b
similarity index 93%
bc092b
rename from include/grub/i386/coreboot/lbio.h
bc092b
rename to include/grub/coreboot/lbio.h
ec4acb
index 1c3fa6f1953..5076d36c71b 100644
bc092b
--- a/include/grub/i386/coreboot/lbio.h
bc092b
+++ b/include/grub/coreboot/lbio.h
bc092b
@@ -20,6 +20,9 @@
bc092b
 #ifndef _GRUB_MACHINE_LBIO_HEADER
bc092b
 #define _GRUB_MACHINE_LBIO_HEADER      1
bc092b
 
bc092b
+#include <grub/types.h>
bc092b
+#include <grub/err.h>
bc092b
+
bc092b
 struct grub_linuxbios_table_header
bc092b
 {
bc092b
   grub_uint8_t signature[4];
bc092b
@@ -102,4 +105,10 @@ EXPORT_FUNC(grub_linuxbios_table_iterate) (int (*hook) (grub_linuxbios_table_ite
bc092b
 					   void *),
bc092b
 					   void *hook_data);
bc092b
 
bc092b
+grub_linuxbios_table_header_t
bc092b
+grub_linuxbios_get_tables (void);
bc092b
+
bc092b
+int
bc092b
+grub_linuxbios_check_signature (grub_linuxbios_table_header_t tbl_header);
bc092b
+
bc092b
 #endif
ec4acb
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
ec4acb
index 809a8aa26bf..781d0ffbf75 100644
ec4acb
--- a/grub-core/Makefile.am
ec4acb
+++ b/grub-core/Makefile.am
ec4acb
@@ -112,7 +112,7 @@ endif
ec4acb
 
ec4acb
 if COND_i386_coreboot
ec4acb
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
ec4acb
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/coreboot/lbio.h
ec4acb
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/coreboot/lbio.h
ec4acb
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/video.h
ec4acb
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/video_fb.h
ec4acb
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/gfxterm.h