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