Skip to contentSkip to conversation

efi_loader: Handle GD_FLG_SKIP_RELOC

PATCH In review [email protected] 3 participants 5 messages Oct 10, 2021 – Oct 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi_loader: Handle GD_FLG_SKIP_RELOC

From: Marek Vasut <[email protected]>

In case U-Boot starts with GD_FLG_SKIP_RELOC, the efi loader relocation code breaks down because it assumes gd->relocaddr points to relocated U-Boot code, which is not the case. Add special case for handling GD_FLG_SKIP_RELOC, which uses the __image_copy_start instead of gd->relocaddr for efi loader code relocation source address.

Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/board_r.c             |  7 ++++++-
 lib/efi_loader/efi_runtime.c | 19 ++++++++++++++++---
 2 files changed, 22 insertions(+), 4 deletions(-)
common/board_r.c +6 -1
index 31a59c585a..3e95123f95 100644
@@ -178,7 +178,12 @@ static int initr_reloc_global_data(void)
 	 */
 	efi_save_gd();
 
-	efi_runtime_relocate(gd->relocaddr, NULL);
+#ifdef CONFIG_ARM
+	if (gd->flags & GD_FLG_SKIP_RELOC)
+		efi_runtime_relocate((ulong)__image_copy_start, NULL);
+	else
+#endif
+		efi_runtime_relocate(gd->relocaddr, NULL);
 #endif
 
 	return 0;
lib/efi_loader/efi_runtime.c +16 -3
index 93a695fc27..876ca09106 100644
@@ -17,6 +17,8 @@
 #include <asm/global_data.h>
 #include <u-boot/crc.h>
 
+#include <asm/sections.h>
+
 /* For manual relocation support */
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -629,13 +631,23 @@ out:
 	return ret;
 }
 
+static unsigned long efi_get_reloc_start(void)
+{
+#ifdef CONFIG_ARM
+	if (gd->flags & GD_FLG_SKIP_RELOC)
+		return (unsigned long)__image_copy_start;
+	else
+#endif
+		return gd->relocaddr;
+}
+
 static __efi_runtime void efi_relocate_runtime_table(ulong offset)
 {
 	ulong patchoff;
 	void **pos;
 
 	/* Relocate the runtime services pointers */
-	patchoff = offset - gd->relocaddr;
+	patchoff = offset - efi_get_reloc_start();
 	for (pos = (void **)&efi_runtime_services.get_time;
 	     pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
 		if (*pos)
@@ -681,7 +693,7 @@ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
 		ulong *p;
 		ulong newaddr;
 
-		p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
+		p = (void*)((ulong)rel->offset - base) + efi_get_reloc_start();
 
 		/*
 		 * The runtime services table is updated in
@@ -852,7 +864,8 @@ static efi_status_t EFIAPI efi_set_virtual_address_map(
 		map = (void*)virtmap + (descriptor_size * i);
 		if (map->type == EFI_RUNTIME_SERVICES_CODE) {
 			ulong new_offset = map->virtual_start -
-					   map->physical_start + gd->relocaddr;
+					   map->physical_start +
+					   efi_get_reloc_start();
 
 			efi_relocate_runtime_table(new_offset);
 			efi_runtime_relocate(new_offset, map);
Signature · 1 line
2.33.0
#1 lore raw
Quoted 8 lines · On 10/10/21 23:48, [email protected] wrote:

From: Marek Vasut <[email protected]>

In case U-Boot starts with GD_FLG_SKIP_RELOC, the efi loader relocation code breaks down because it assumes gd->relocaddr points to relocated U-Boot code, which is not the case. Add special case for handling GD_FLG_SKIP_RELOC, which uses the __image_copy_start instead of gd->relocaddr for efi loader code relocation source address.

GD_FLG_SKIP_RELOC is only to be used by the x86 EFI app which is incompatible with CONFIG_EFI_LOADER=y. lib/efi/efi_app.c:131: board_init_f(GD_FLG_SKIP_RELOC);

Why do we need this patch?

Quoted 15 lines
Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
  common/board_r.c             |  7 ++++++-
  lib/efi_loader/efi_runtime.c | 19 ++++++++++++++++---
  2 files changed, 22 insertions(+), 4 deletions(-)
common/board_r.c +0 -0
index 31a59c585a..3e95123f95 100644

common/board_r.c static int initr_reloc_global_data(void) · line 181

Quoted 5 lines
common/board_r.c +1 -1
@@ -178,7 +178,12 @@ static int initr_reloc_global_data(void)
  	 */
  	efi_save_gd();
 
-	efi_runtime_relocate(gd->relocaddr, NULL);
+#ifdef CONFIG_ARM

Why should this be ARM specific?

Best regards

Heinrich

Quoted 66 lines
common/board_r.c +5 -0
+	if (gd->flags & GD_FLG_SKIP_RELOC)
+		efi_runtime_relocate((ulong)__image_copy_start, NULL);
+	else
+#endif
+		efi_runtime_relocate(gd->relocaddr, NULL);
  #endif
 
  	return 0;
lib/efi_loader/efi_runtime.c +16 -3
index 93a695fc27..876ca09106 100644
@@ -17,6 +17,8 @@
  #include <asm/global_data.h>
  #include <u-boot/crc.h>
 
+#include <asm/sections.h>
+
  /* For manual relocation support */
  DECLARE_GLOBAL_DATA_PTR;
 
@@ -629,13 +631,23 @@ out:
  	return ret;
  }
 
+static unsigned long efi_get_reloc_start(void)
+{
+#ifdef CONFIG_ARM
+	if (gd->flags & GD_FLG_SKIP_RELOC)
+		return (unsigned long)__image_copy_start;
+	else
+#endif
+		return gd->relocaddr;
+}
+
  static __efi_runtime void efi_relocate_runtime_table(ulong offset)
  {
  	ulong patchoff;
  	void **pos;
 
  	/* Relocate the runtime services pointers */
-	patchoff = offset - gd->relocaddr;
+	patchoff = offset - efi_get_reloc_start();
  	for (pos = (void **)&efi_runtime_services.get_time;
  	     pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
  		if (*pos)
@@ -681,7 +693,7 @@ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  		ulong *p;
  		ulong newaddr;
 
-		p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
+		p = (void*)((ulong)rel->offset - base) + efi_get_reloc_start();
 
  		/*
  		 * The runtime services table is updated in
@@ -852,7 +864,8 @@ static efi_status_t EFIAPI efi_set_virtual_address_map(
  		map = (void*)virtmap + (descriptor_size * i);
  		if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  			ulong new_offset = map->virtual_start -
-					   map->physical_start + gd->relocaddr;
+					   map->physical_start +
+					   efi_get_reloc_start();
 
  			efi_relocate_runtime_table(new_offset);
  			efi_runtime_relocate(new_offset, map);
#2 lore raw

On 10/11/21 1:36 PM, Heinrich Schuchardt wrote:
Hi,

Quoted 7 lines
Quoted 6 lines

In case U-Boot starts with GD_FLG_SKIP_RELOC, the efi loader relocation code breaks down because it assumes gd->relocaddr points to relocated U-Boot code, which is not the case. Add special case for handling GD_FLG_SKIP_RELOC, which uses the __image_copy_start instead of gd->relocaddr for efi loader code relocation source address.

Quoted 5 lines

GD_FLG_SKIP_RELOC is only to be used by the x86 EFI app which is incompatible with CONFIG_EFI_LOADER=y. lib/efi/efi_app.c:131:  board_init_f(GD_FLG_SKIP_RELOC);

Why do we need this patch?

GD_FLG_SKIP_RELOC is set when U-Boot doesn't perform relocation, it has nothing to do with EFI. That case is currently broken, try setting GD_FLG_SKIP_RELOC early on in U-Boot (somewhere early, e.g. in mach_cpu_init()) and the system crashes on board_init_r when initializing EFI.

#3 lore raw
Quoted 14 lines · Marek Vasut

On 10/11/21 1:36 PM, Heinrich Schuchardt wrote:
Hi,

Quoted 11 lines
Quoted 6 lines

In case U-Boot starts with GD_FLG_SKIP_RELOC, the efi loader relocation code breaks down because it assumes gd->relocaddr points to relocated U-Boot code, which is not the case. Add special case for handling GD_FLG_SKIP_RELOC, which uses the __image_copy_start instead of gd->relocaddr for efi loader code relocation source address.

GD_FLG_SKIP_RELOC is only to be used by the x86 EFI app which is incompatible with CONFIG_EFI_LOADER=y. lib/efi/efi_app.c:131:  board_init_f(GD_FLG_SKIP_RELOC);

Quoted 7 lines
Quoted 1 line

Why do we need this patch?

GD_FLG_SKIP_RELOC is set when U-Boot doesn't perform relocation, it has nothing to do with EFI. That case is currently broken, try setting GD_FLG_SKIP_RELOC early on in U-Boot (somewhere early, e.g. in mach_cpu_init()) and the system crashes on board_init_r when initializing EFI.

You missed the second question:

Why should the change be ARM specific?

Best regards

Heinrich

#4 lore raw
Quoted 25 lines · Heinrich Schuchardt
Quoted 14 lines · Marek Vasut

On 10/11/21 1:36 PM, Heinrich Schuchardt wrote:
Hi,

Quoted 11 lines
Quoted 6 lines

In case U-Boot starts with GD_FLG_SKIP_RELOC, the efi loader relocation code breaks down because it assumes gd->relocaddr points to relocated U-Boot code, which is not the case. Add special case for handling GD_FLG_SKIP_RELOC, which uses the __image_copy_start instead of gd->relocaddr for efi loader code relocation source address.

GD_FLG_SKIP_RELOC is only to be used by the x86 EFI app which is incompatible with CONFIG_EFI_LOADER=y. lib/efi/efi_app.c:131:  board_init_f(GD_FLG_SKIP_RELOC);

Quoted 7 lines
Quoted 1 line

Why do we need this patch?

GD_FLG_SKIP_RELOC is set when U-Boot doesn't perform relocation, it has nothing to do with EFI. That case is currently broken, try setting GD_FLG_SKIP_RELOC early on in U-Boot (somewhere early, e.g. in mach_cpu_init()) and the system crashes on board_init_r when initializing EFI.

Quoted 3 lines

You missed the second question:

Why should the change be ARM specific?

It shouldn't, do you have a more generic idea ?