bc092b
From d08c9685149e7788001ac9a209baa1d84c3628e0 Mon Sep 17 00:00:00 2001
bc092b
From: Vladimir Serbinenko <phcoder@gmail.com>
bc092b
Date: Mon, 8 May 2017 17:47:57 +0200
31cddd
Subject: [PATCH] Refactor arm-uboot code to make it genereic.
bc092b
bc092b
arm-coreboot startup code can be very similar to arm-uboot but current code has
bc092b
U-Boot specific references. So split U-Boot part from generic part.
bc092b
---
bc092b
 grub-core/Makefile.core.def              |  4 +-
bc092b
 grub-core/kern/arm/uboot/init.c          | 70 ++++++++++++++++++++++++++++++
bc092b
 grub-core/kern/uboot/init.c              | 16 -------
bc092b
 grub-core/kern/uboot/uboot.c             | 35 ---------------
bc092b
 include/grub/arm/startup.h               | 16 +++++++
ec4acb
 grub-core/kern/arm/{uboot => }/startup.S | 66 ++++-------------------------
ec4acb
 grub-core/kern/arm/uboot/uboot.S         | 73 ++++++++++++++++++++++++++++++++
ec4acb
 7 files changed, 171 insertions(+), 109 deletions(-)
bc092b
 create mode 100644 grub-core/kern/arm/uboot/init.c
bc092b
 create mode 100644 include/grub/arm/startup.h
ec4acb
 rename grub-core/kern/arm/{uboot => }/startup.S (77%)
ec4acb
 create mode 100644 grub-core/kern/arm/uboot/uboot.S
bc092b
bc092b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
ec4acb
index 8dcd0e5a998..fb59a7b1d6a 100644
bc092b
--- a/grub-core/Makefile.core.def
bc092b
+++ b/grub-core/Makefile.core.def
bc092b
@@ -105,7 +105,7 @@ kernel = {
bc092b
   mips_startup = kern/mips/startup.S;
bc092b
   sparc64_ieee1275_startup = kern/sparc64/ieee1275/crt0.S;
bc092b
   powerpc_ieee1275_startup = kern/powerpc/ieee1275/startup.S;
bc092b
-  arm_uboot_startup = kern/arm/uboot/startup.S;
bc092b
+  arm_uboot_startup = kern/arm/startup.S;
bc092b
   arm_efi_startup = kern/arm/efi/startup.S;
bc092b
   arm64_efi_startup = kern/arm64/efi/startup.S;
bc092b
 
bc092b
@@ -149,6 +149,8 @@ kernel = {
bc092b
   uboot = kern/uboot/init.c;
bc092b
   uboot = kern/uboot/hw.c;
bc092b
   uboot = term/uboot/console.c;
bc092b
+  arm_uboot = kern/arm/uboot/init.c;
bc092b
+  arm_uboot = kern/arm/uboot/uboot.S;
bc092b
 
bc092b
   terminfoinkernel = term/terminfo.c;
bc092b
   terminfoinkernel = term/tparm.c;
bc092b
diff --git a/grub-core/kern/arm/uboot/init.c b/grub-core/kern/arm/uboot/init.c
bc092b
new file mode 100644
ec4acb
index 00000000000..2a6aa3fdd3d
bc092b
--- /dev/null
bc092b
+++ b/grub-core/kern/arm/uboot/init.c
bc092b
@@ -0,0 +1,70 @@
bc092b
+/* init.c - generic U-Boot initialization and finalization */
bc092b
+/*
bc092b
+ *  GRUB  --  GRand Unified Bootloader
bc092b
+ *  Copyright (C) 2016  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/uboot/uboot.h>
bc092b
+#include <grub/arm/startup.h>
bc092b
+#include <grub/uboot/api_public.h>
bc092b
+
bc092b
+extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
bc092b
+
bc092b
+grub_uint32_t
bc092b
+grub_uboot_get_machine_type (void)
bc092b
+{
bc092b
+  return grub_arm_saved_registers.r[1];
bc092b
+}
bc092b
+
bc092b
+grub_addr_t
bc092b
+grub_uboot_get_boot_data (void)
bc092b
+{
bc092b
+  return grub_arm_saved_registers.r[2];
bc092b
+}
bc092b
+
bc092b
+int
bc092b
+grub_uboot_api_init (void)
bc092b
+{
bc092b
+  struct api_signature *start, *end;
bc092b
+  struct api_signature *p;
bc092b
+  grub_addr_t grub_uboot_search_hint = grub_arm_saved_registers.sp;
bc092b
+  if (grub_uboot_search_hint)
bc092b
+    {
bc092b
+      /* Extended search range to work around Trim Slice U-Boot issue */
bc092b
+      start = (struct api_signature *) ((grub_uboot_search_hint & ~0x000fffff)
bc092b
+					- 0x00500000);
bc092b
+      end =
bc092b
+	(struct api_signature *) ((grub_addr_t) start + UBOOT_API_SEARCH_LEN -
bc092b
+				  API_SIG_MAGLEN + 0x00500000);
bc092b
+    }
bc092b
+  else
bc092b
+    {
bc092b
+      start = 0;
bc092b
+      end = (struct api_signature *) (256 * 1024 * 1024);
bc092b
+    }
bc092b
+
bc092b
+  /* Structure alignment is (at least) 8 bytes */
bc092b
+  for (p = start; p < end; p = (void *) ((grub_addr_t) p + 8))
bc092b
+    {
bc092b
+      if (grub_memcmp (&(p->magic), API_SIG_MAGIC, API_SIG_MAGLEN) == 0)
bc092b
+	{
bc092b
+	  grub_uboot_syscall_ptr = p->syscall;
bc092b
+	  return p->version;
bc092b
+	}
bc092b
+    }
bc092b
+
bc092b
+  return 0;
bc092b
+}
bc092b
diff --git a/grub-core/kern/uboot/init.c b/grub-core/kern/uboot/init.c
ec4acb
index 5dcc106ed9b..3e338645c57 100644
bc092b
--- a/grub-core/kern/uboot/init.c
bc092b
+++ b/grub-core/kern/uboot/init.c
bc092b
@@ -36,30 +36,14 @@
bc092b
 extern char __bss_start[];
bc092b
 extern char _end[];
bc092b
 extern grub_size_t grub_total_module_size;
bc092b
-extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
bc092b
 static unsigned long timer_start;
bc092b
 
bc092b
-extern grub_uint32_t grub_uboot_machine_type;
bc092b
-extern grub_addr_t grub_uboot_boot_data;
bc092b
-
bc092b
 void
bc092b
 grub_exit (void)
bc092b
 {
bc092b
   grub_uboot_return (0);
bc092b
 }
bc092b
 
bc092b
-grub_uint32_t
bc092b
-grub_uboot_get_machine_type (void)
bc092b
-{
bc092b
-  return grub_uboot_machine_type;
bc092b
-}
bc092b
-
bc092b
-grub_addr_t
bc092b
-grub_uboot_get_boot_data (void)
bc092b
-{
bc092b
-  return grub_uboot_boot_data;
bc092b
-}
bc092b
-
bc092b
 static grub_uint64_t
bc092b
 uboot_timer_ms (void)
bc092b
 {
bc092b
diff --git a/grub-core/kern/uboot/uboot.c b/grub-core/kern/uboot/uboot.c
ec4acb
index 6800a4beb1c..cf0168e62dd 100644
bc092b
--- a/grub-core/kern/uboot/uboot.c
bc092b
+++ b/grub-core/kern/uboot/uboot.c
bc092b
@@ -39,48 +39,13 @@
bc092b
  * returns:	0 if the call not found, 1 if serviced
bc092b
  */
bc092b
 
bc092b
-extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
bc092b
 extern int grub_uboot_syscall (int, int *, ...);
bc092b
-extern grub_addr_t grub_uboot_search_hint;
bc092b
 
bc092b
 static struct sys_info uboot_sys_info;
bc092b
 static struct mem_region uboot_mem_info[5];
bc092b
 static struct device_info * devices;
bc092b
 static int num_devices;
bc092b
 
bc092b
-int
bc092b
-grub_uboot_api_init (void)
bc092b
-{
bc092b
-  struct api_signature *start, *end;
bc092b
-  struct api_signature *p;
bc092b
-
bc092b
-  if (grub_uboot_search_hint)
bc092b
-    {
bc092b
-      /* Extended search range to work around Trim Slice U-Boot issue */
bc092b
-      start = (struct api_signature *) ((grub_uboot_search_hint & ~0x000fffff)
bc092b
-					- 0x00500000);
bc092b
-      end =
bc092b
-	(struct api_signature *) ((grub_addr_t) start + UBOOT_API_SEARCH_LEN -
bc092b
-				  API_SIG_MAGLEN + 0x00500000);
bc092b
-    }
bc092b
-  else
bc092b
-    {
bc092b
-      start = 0;
bc092b
-      end = (struct api_signature *) (256 * 1024 * 1024);
bc092b
-    }
bc092b
-
bc092b
-  /* Structure alignment is (at least) 8 bytes */
bc092b
-  for (p = start; p < end; p = (void *) ((grub_addr_t) p + 8))
bc092b
-    {
bc092b
-      if (grub_memcmp (&(p->magic), API_SIG_MAGIC, API_SIG_MAGLEN) == 0)
bc092b
-	{
bc092b
-	  grub_uboot_syscall_ptr = p->syscall;
bc092b
-	  return p->version;
bc092b
-	}
bc092b
-    }
bc092b
-
bc092b
-  return 0;
bc092b
-}
bc092b
 
bc092b
 /*
bc092b
  * All functions below are wrappers around the grub_uboot_syscall() function
bc092b
diff --git a/include/grub/arm/startup.h b/include/grub/arm/startup.h
bc092b
new file mode 100644
ec4acb
index 00000000000..9afb6c57c0b
bc092b
--- /dev/null
bc092b
+++ b/include/grub/arm/startup.h
bc092b
@@ -0,0 +1,16 @@
bc092b
+#ifndef GRUB_STARTUP_CPU_HEADER
bc092b
+#define GRUB_STARTUP_CPU_HEADER
bc092b
+
bc092b
+struct grub_arm_startup_registers
bc092b
+{
bc092b
+  /* registers 0-11 */
bc092b
+  /* for U-boot r[1] is machine type */
bc092b
+  /* for U-boot r[2] is boot data */
bc092b
+  grub_uint32_t r[12];
bc092b
+  grub_uint32_t sp;
bc092b
+  grub_uint32_t lr;
bc092b
+};
bc092b
+
bc092b
+extern struct grub_arm_startup_registers grub_arm_saved_registers;
bc092b
+
bc092b
+#endif
ec4acb
diff --git a/grub-core/kern/arm/uboot/startup.S b/grub-core/kern/arm/startup.S
ec4acb
similarity index 77%
ec4acb
rename from grub-core/kern/arm/uboot/startup.S
ec4acb
rename to grub-core/kern/arm/startup.S
ec4acb
index 5efaae16e83..640837cba51 100644
ec4acb
--- a/grub-core/kern/arm/uboot/startup.S
ec4acb
+++ b/grub-core/kern/arm/startup.S
ec4acb
@@ -86,7 +86,7 @@ FUNCTION(codestart)
ec4acb
 	@ Stack pointer used as start address for signature probing
ec4acb
 	mov	r12, sp
ec4acb
 	adr	sp, entry_state
ec4acb
-	push	{r1-r12,lr}	@ store U-Boot context (sp in r12)
ec4acb
+	push	{r0-r12,lr}	@ store U-Boot context (sp in r12)
ec4acb
 
ec4acb
 	adr     r1, _start
ec4acb
 	ldr	r0, bss_start_ptr		@ src
ec4acb
@@ -153,69 +153,21 @@ reloc_done:
ec4acb
 	
ec4acb
 	b	EXT_C(grub_main)
ec4acb
 
ec4acb
-	/*
ec4acb
-	 * uboot_syscall():
ec4acb
-	 *   This function is effectively a veneer, so it cannot
ec4acb
-	 *   modify the stack or corrupt any registers other than
ec4acb
-	 *   r12 (ip). Furthermore it needs to restore r8 for
ec4acb
-	 *   U-Boot (Global Data Pointer) and preserve it for Grub.
ec4acb
-	 */
ec4acb
-FUNCTION(grub_uboot_syscall)
ec4acb
-	str     r8, transition_space
ec4acb
-	str     lr, transition_space + 4
ec4acb
-	str     r9, transition_space + 8
ec4acb
-
ec4acb
-	ldr	r8, gd_backup
ec4acb
-	ldr	r9, gd_backup + 4
ec4acb
-
ec4acb
-	bl	do_syscall
ec4acb
-
ec4acb
-	ldr     r8, transition_space
ec4acb
-	ldr     lr, transition_space + 4
ec4acb
-	ldr     r9, transition_space + 8
ec4acb
-
ec4acb
-	bx	lr
ec4acb
-do_syscall:
ec4acb
-
ec4acb
-	ldr	ip, grub_uboot_syscall_ptr
ec4acb
-	bx	ip
ec4acb
-	
ec4acb
-FUNCTION(grub_uboot_return)
ec4acb
-	adr	sp, entry_state_end
ec4acb
-	pop	{r4-r12, lr}
ec4acb
-	mov	sp, r12
ec4acb
-	bx	lr
ec4acb
-
ec4acb
-	
ec4acb
 	.align	3
ec4acb
-@ U-boot context stack space
ec4acb
-entry_state_end:
ec4acb
-VARIABLE(grub_uboot_machine_type)
ec4acb
+@ U-boot/coreboot context stack space
ec4acb
+VARIABLE(grub_arm_saved_registers)
ec4acb
+	.long	0	@ r0
ec4acb
 	.long	0	@ r1
ec4acb
-VARIABLE(grub_uboot_boot_data)
ec4acb
 	.long	0	@ r2
ec4acb
 	.long	0	@ r3
ec4acb
 	.long	0	@ r4
ec4acb
 	.long	0	@ r5
ec4acb
 	.long	0	@ r6
ec4acb
 	.long	0	@ r7
ec4acb
-gd_backup:	
ec4acb
-	.long	0	@ r8 - U-Boot global data pointer up to 2013-09-21
ec4acb
-	.long	0	@ r9 - U-Boot global data pointer 2013-09-21 onwards
ec4acb
-	.long	0	@ r10
ec4acb
-	.long	0	@ r11
ec4acb
-VARIABLE(grub_uboot_search_hint)@ U-Boot stack pointer - 
ec4acb
-	.long	0	@ also API signature address hint.
ec4acb
-	.long	0	@ lr
ec4acb
-entry_state:		@ backup for U-Boot context
ec4acb
-
ec4acb
-@ GRUB context stack space
ec4acb
-transition_space:	
ec4acb
 	.long	0	@ r8
ec4acb
-	.long	0	@ lr
ec4acb
 	.long	0	@ r9
ec4acb
-
ec4acb
-VARIABLE(grub_uboot_syscall_ptr)
ec4acb
-	.long	0	@
ec4acb
-
ec4acb
-	END
ec4acb
+	.long	0	@ r10
ec4acb
+	.long	0	@ r11
ec4acb
+	.long	0	@ sp
ec4acb
+	.long	0	@ lr
ec4acb
+entry_state:
ec4acb
diff --git a/grub-core/kern/arm/uboot/uboot.S b/grub-core/kern/arm/uboot/uboot.S
ec4acb
new file mode 100644
ec4acb
index 00000000000..d128775f19e
ec4acb
--- /dev/null
ec4acb
+++ b/grub-core/kern/arm/uboot/uboot.S
ec4acb
@@ -0,0 +1,73 @@
ec4acb
+/*
ec4acb
+ *  GRUB  --  GRand Unified Bootloader
ec4acb
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
ec4acb
+ *
ec4acb
+ *  GRUB is free software: you can redistribute it and/or modify
ec4acb
+ *  it under the terms of the GNU General Public License as published by
ec4acb
+ *  the Free Software Foundation, either version 3 of the License, or
ec4acb
+ *  (at your option) any later version.
ec4acb
+ *
ec4acb
+ *  GRUB is distributed in the hope that it will be useful,
ec4acb
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ec4acb
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ec4acb
+ *  GNU General Public License for more details.
ec4acb
+ *
ec4acb
+ *  You should have received a copy of the GNU General Public License
ec4acb
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
ec4acb
+ */
ec4acb
+
ec4acb
+#include <grub/offsets.h>
ec4acb
+#include <grub/symbol.h>
ec4acb
+#include <grub/machine/kernel.h>
ec4acb
+
ec4acb
+	/*
ec4acb
+	 * uboot_syscall():
ec4acb
+	 *   This function is effectively a veneer, so it cannot
ec4acb
+	 *   modify the stack or corrupt any registers other than
ec4acb
+	 *   r12 (ip). Furthermore it needs to restore r8 for
ec4acb
+	 *   U-Boot (Global Data Pointer) and preserve it for Grub.
ec4acb
+	 */
ec4acb
+FUNCTION(grub_uboot_syscall)
ec4acb
+	str     r8, transition_space
ec4acb
+	str     lr, transition_space + 4
ec4acb
+	str     r9, transition_space + 8
ec4acb
+
ec4acb
+	ldr	ip, saved_registers_ptr
ec4acb
+	ldr	r8, [ip, #4 * 8]
ec4acb
+	ldr	r9, [ip, #4 * 9]
ec4acb
+
ec4acb
+	bl	do_syscall
ec4acb
+
ec4acb
+	ldr     r8, transition_space
ec4acb
+	ldr     lr, transition_space + 4
ec4acb
+	ldr     r9, transition_space + 8
ec4acb
+
ec4acb
+	bx	lr
ec4acb
+do_syscall:
ec4acb
+
ec4acb
+	ldr	ip, grub_uboot_syscall_ptr
ec4acb
+	bx	ip
ec4acb
+	
ec4acb
+FUNCTION(grub_uboot_return)
ec4acb
+	ldr	ip, saved_registers_ptr
ec4acb
+	ldr	sp, [ip, #4 * 4]
ec4acb
+	pop	{r4-r12, lr}
ec4acb
+	mov	sp, r12
ec4acb
+	bx	lr
ec4acb
+
ec4acb
+	
ec4acb
+	.align	3
ec4acb
+
ec4acb
+@ GRUB context stack space
ec4acb
+transition_space:	
ec4acb
+	.long	0	@ r8
ec4acb
+	.long	0	@ lr
ec4acb
+	.long	0	@ r9
ec4acb
+
ec4acb
+saved_registers_ptr:
ec4acb
+	.long EXT_C(grub_arm_saved_registers)
ec4acb
+
ec4acb
+VARIABLE(grub_uboot_syscall_ptr)
ec4acb
+	.long	0	@
ec4acb
+
ec4acb
+	END