Skip to contentSkip to conversation

x86: Improve support for chain-loading U-Boot

PATCH New Simon Glass 3 participants 22 messages Apr 26, 2020 – Apr 30, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover x86: Improve support for chain-loading U-Boot

This little series adds a few checks into the code to allow better operation when booting a build from a previous-state loader such as coreboot.

At present we have a 'coreboot' target but this runs very different code from the bare-metal targets, such as coral. There is very little in common between them.

It is useful to be able to boot the same U-Boot on a device, with or without a first-stage bootloader. For example, with chromebook_coral, it is helpful for testing to be able to boot the same U-Boot (complete with FSP) on bare metal and from coreboot. It allows checking of things like CPU speed, comparing registers, ACPI tables and the like.

This series allows U-Boot to detect that it ran from coreboot and automatically do the right thing.

This series makes the most important changes to allow the same u-boot.bin for coral to boot after coreboot (by itself) or bare metal (via TPL->SPL).

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT
- Add new patch to move coreboot-table detection into common code
- Rename flag to GD_FLG_SKIP_LL_INIT
- Split this patch into two
- Update to use locate_coreboot_table()

Changes in v3:
- Add a new patch with a gd flag for chain loading
- Add new patch to detect running from coreboot

Changes in v2:
- Drop the other check in interrupt_init() which is not needed now
- Drop patch 'dm: Avoid initing built-in devices when chain loading'

Simon Glass (9):
x86: fsp: Allow skipping init code when chain loading
x86: apl: Skip init code when chain loading
x86: cpu: Skip init code when chain loading
pci: Avoid auto-config when chain loading
board: Add a gd flag for chain loading
x86: Move coreboot-table detection into common code
x86: Add a way to detect running from coreboot
x86: Use the existing stack when chain-loading
x86: Add documentation for the chain-load feature

 arch/x86/cpu/apollolake/fsp_s.c        |  2 ++
 arch/x86/cpu/coreboot/tables.c         | 24 +++++++++-------------
 arch/x86/cpu/cpu.c                     |  4 +++-
 arch/x86/cpu/i386/cpu.c                | 27 +++++++++++++++++++++++++
 arch/x86/cpu/i386/interrupt.c          |  6 ++++--
 arch/x86/cpu/start_from_spl.S          | 16 +++++++++++++--
 arch/x86/include/asm/coreboot_tables.h |  7 +++++++
 arch/x86/lib/fsp/fsp_dram.c            |  8 ++++++++
 arch/x86/lib/fsp/fsp_graphics.c        |  3 +++
 arch/x86/lib/fsp2/fsp_dram.c           | 10 +++++++++
 arch/x86/lib/fsp2/fsp_init.c           |  2 +-
 arch/x86/lib/init_helpers.c            |  3 +++
 doc/arch/x86.rst                       | 28 ++++++++++++++++++++++++++
 drivers/pci/pci-uclass.c               |  4 ++--
 include/asm-generic/global_data.h      |  1 +
 include/init.h                         |  2 +-
 16 files changed, 123 insertions(+), 24 deletions(-)
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#1 lore raw

PATCH v4 1/9 x86: fsp: Allow skipping init code when chain loading

It is useful to be able to boot the same x86 image on a device with or without a first-stage bootloader. For example, with chromebook_coral, it is helpful for testing to be able to boot the same U-Boot (complete with FSP) on bare metal and from coreboot. It allows checking of things like CPU speed, comparing registers, ACPI tables and the like.

When U-Boot is not the first-stage bootloader much of this code is not needed and can break booting. Add checks for this to the FSP code.

Rather than checking for the amount of available SDRAM, just use 1GB in this situation, which should be safe. Using 2GB may run into a memory hole on some SoCs.

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 arch/x86/lib/fsp/fsp_dram.c     |  8 ++++++++
 arch/x86/lib/fsp/fsp_graphics.c |  3 +++
 arch/x86/lib/fsp2/fsp_dram.c    | 10 ++++++++++
 arch/x86/lib/fsp2/fsp_init.c    |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/lib/fsp/fsp_dram.c +8 -0
index 9ce0ddf0d3..15e82de2fe 100644
@@ -44,6 +44,14 @@ int dram_init_banksize(void)
 	phys_addr_t low_end;
 	uint bank;
 
+	if (!ll_boot_init()) {
+		gd->bd->bi_dram[0].start = 0;
+		gd->bd->bi_dram[0].size = gd->ram_size;
+
+		mtrr_add_request(MTRR_TYPE_WRBACK, 0, gd->ram_size);
+		return 0;
+	}
+
 	low_end = 0;
 	for (bank = 1, hdr = gd->arch.hob_list;
 	     bank < CONFIG_NR_DRAM_BANKS && !end_of_hob(hdr);
arch/x86/lib/fsp/fsp_graphics.c +3 -0
index 226c7e66b3..98b762209f 100644
@@ -78,6 +78,9 @@ static int fsp_video_probe(struct udevice *dev)
 	struct vesa_mode_info *vesa = &mode_info.vesa;
 	int ret;
 
+	if (!ll_boot_init())
+		return 0;
+
 	printf("Video: ");
 
 	/* Initialize vesa_mode_info structure */
arch/x86/lib/fsp2/fsp_dram.c +10 -0
index c8f2c09b6a..3869c53c5f 100644
@@ -12,11 +12,18 @@
 #include <asm/fsp/fsp_support.h>
 #include <asm/fsp2/fsp_api.h>
 #include <asm/fsp2/fsp_internal.h>
+#include <linux/sizes.h>
 
 int dram_init(void)
 {
 	int ret;
 
+	if (!ll_boot_init()) {
+		/* Use a small and safe amount of 1GB */
+		gd->ram_size = SZ_1G;
+
+		return 0;
+	}
 	if (spl_phase() == PHASE_SPL) {
 #ifdef CONFIG_HAVE_ACPI_RESUME
 		bool s3wake = gd->arch.prev_sleep_state == ACPI_S3;
@@ -68,6 +75,9 @@ int dram_init(void)
 
 ulong board_get_usable_ram_top(ulong total_size)
 {
+	if (!ll_boot_init())
+		return gd->ram_size;
+
 #if CONFIG_IS_ENABLED(HANDOFF)
 	struct spl_handoff *ho = gd->spl_handoff;
 
arch/x86/lib/fsp2/fsp_init.c +1 -1
index da9bd6b45c..c7dc2ea257 100644
@@ -23,7 +23,7 @@ int arch_cpu_init_dm(void)
 	int ret;
 
 	/* Make sure pads are set up early in U-Boot */
-	if (spl_phase() != PHASE_BOARD_F)
+	if (!ll_boot_init() || spl_phase() != PHASE_BOARD_F)
 		return 0;
 
 	/* Probe all pinctrl devices to set up the pads */
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#2 lore raw

PATCH v4 1/9 x86: fsp: Allow skipping init code when chain loading

Quoted 21 lines · Simon Glass

It is useful to be able to boot the same x86 image on a device with or without a first-stage bootloader. For example, with chromebook_coral, it is helpful for testing to be able to boot the same U-Boot (complete with FSP) on bare metal and from coreboot. It allows checking of things like CPU speed, comparing registers, ACPI tables and the like.

When U-Boot is not the first-stage bootloader much of this code is not needed and can break booting. Add checks for this to the FSP code.

Rather than checking for the amount of available SDRAM, just use 1GB in this situation, which should be safe. Using 2GB may run into a memory hole on some SoCs.

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

Quoted 6 lines
 arch/x86/lib/fsp/fsp_dram.c     |  8 ++++++++
 arch/x86/lib/fsp/fsp_graphics.c |  3 +++
 arch/x86/lib/fsp2/fsp_dram.c    | 10 ++++++++++
 arch/x86/lib/fsp2/fsp_init.c    |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)
#3 lore raw

PATCH v4 2/9 x86: apl: Skip init code when chain loading

In reply to Simon Glass

When U-Boot is not the first-stage bootloader the FSP-S init must be skipped. Update it to add a check.

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 arch/x86/cpu/apollolake/fsp_s.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/cpu/apollolake/fsp_s.c +2 -0
index 17cf1682ad..7ef169b147 100644
@@ -566,6 +566,8 @@ int arch_fsp_init_r(void)
 	struct udevice *dev, *itss;
 	int ret;
 
+	if (!ll_boot_init())
+		return 0;
 	/*
 	 * This must be called before any devices are probed. Put any probing
 	 * into arch_fsps_preinit() above.
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#4 lore raw

PATCH v4 2/9 x86: apl: Skip init code when chain loading

Quoted 5 lines · Simon Glass

When U-Boot is not the first-stage bootloader the FSP-S init must be skipped. Update it to add a check.

Quoted 9 lines

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 arch/x86/cpu/apollolake/fsp_s.c | 2 ++
 1 file changed, 2 insertions(+)
#5 lore raw

PATCH v4 3/9 x86: cpu: Skip init code when chain loading

In reply to Simon Glass

When U-Boot is not the first-stage bootloader the interrupt and cache init must be skipped, as well as init for various peripherals. Update the code to add checks for this.

---

Changes in v4: None
Changes in v3: None
Changes in v2:
- Drop the other check in interrupt_init() which is not needed now

 arch/x86/cpu/cpu.c            | 4 +++-
 arch/x86/cpu/i386/interrupt.c | 6 ++++--
 arch/x86/lib/init_helpers.c   | 3 +++
 3 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/cpu/cpu.c +3 -1
index cec04b481b..8526e856d7 100644
@@ -239,8 +239,10 @@ int cpu_init_r(void)
 	struct udevice *dev;
 	int ret;
 
-	if (!ll_boot_init())
+	if (!ll_boot_init()) {
+		uclass_first_device(UCLASS_PCI, &dev);
 		return 0;
+	}
 
 	ret = x86_init_cpus();
 	if (ret)
arch/x86/cpu/i386/interrupt.c +4 -2
index 4c7e9ea215..e67a116ac1 100644
@@ -264,6 +264,9 @@ int interrupt_init(void)
 	struct udevice *dev;
 	int ret;
 
+	if (!ll_boot_init())
+		return 0;
+
 	/* Try to set up the interrupt router, but don't require one */
 	ret = irq_first_device_type(X86_IRQT_BASE, &dev);
 	if (ret && ret != -ENODEV)
@@ -295,8 +298,7 @@ int interrupt_init(void)
 	 * TODO(sjg at chromium.org): But we don't handle these correctly when
 	 * booted from EFI.
 	 */
-	if (ll_boot_init())
-		enable_interrupts();
+	enable_interrupts();
 #endif
 
 	return 0;
arch/x86/lib/init_helpers.c +3 -0
index 5bb55e256f..d906b528b3 100644
@@ -30,6 +30,9 @@ int init_cache_f_r(void)
 			return ret;
 	}
 
+	if (!ll_boot_init())
+		return 0;
+
 	/* Initialise the CPU cache(s) */
 	return init_cache();
 }
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#6 lore raw

PATCH v4 3/9 x86: cpu: Skip init code when chain loading

Quoted 13 lines · Simon Glass

When U-Boot is not the first-stage bootloader the interrupt and cache init must be skipped, as well as init for various peripherals. Update the code to add checks for this.

---

Changes in v4: None
Changes in v3: None
Changes in v2:
- Drop the other check in interrupt_init() which is not needed now

Quoted 5 lines
 arch/x86/cpu/cpu.c            | 4 +++-
 arch/x86/cpu/i386/interrupt.c | 6 ++++--
 arch/x86/lib/init_helpers.c   | 3 +++
 3 files changed, 10 insertions(+), 3 deletions(-)
#7 lore raw

PATCH v4 4/9 pci: Avoid auto-config when chain loading

In reply to Simon Glass

When U-Boot is not the first-stage bootloader we don't want to re-configure the PCI devices, since this has already been done. Add a check to avoid this.

---

Changes in v4: None
Changes in v3: None
Changes in v2:
- Drop patch 'dm: Avoid initing built-in devices when chain loading'

 drivers/pci/pci-uclass.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/pci-uclass.c +2 -2
index d2e10d6868..7f46e901fb 100644
@@ -1009,7 +1009,7 @@ static int pci_uclass_post_probe(struct udevice *bus)
 	if (ret)
 		return ret;
 
-	if (CONFIG_IS_ENABLED(PCI_PNP) &&
+	if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
 	    (!hose->skip_auto_config_until_reloc ||
 	     (gd->flags & GD_FLG_RELOC))) {
 		ret = pci_auto_config_devices(bus);
@@ -1031,7 +1031,7 @@ static int pci_uclass_post_probe(struct udevice *bus)
 	 * Note we only call this 1) after U-Boot is relocated, and 2)
 	 * root bus has finished probing.
 	 */
-	if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
+	if ((gd->flags & GD_FLG_RELOC) && bus->seq == 0 && ll_boot_init()) {
 		ret = fsp_init_phase_pci();
 		if (ret)
 			return ret;
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#8 lore raw

PATCH v4 4/9 pci: Avoid auto-config when chain loading

Quoted 8 lines · Simon Glass

When U-Boot is not the first-stage bootloader we don't want to re-configure the PCI devices, since this has already been done. Add a check to avoid this.

---

Quoted 8 lines

Changes in v4: None
Changes in v3: None
Changes in v2:
- Drop patch 'dm: Avoid initing built-in devices when chain loading'

 drivers/pci/pci-uclass.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#9 lore raw

PATCH v4 5/9 board: Add a gd flag for chain loading

In reply to Simon Glass

When U-Boot is run from another boot loader, much of the low-level init needs to be skipped.

Add a flag for this and adjust ll_boot_init() to use it.

---

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT

Changes in v3:
- Add a new patch with a gd flag for chain loading

Changes in v2: None

 include/asm-generic/global_data.h | 1 +
 include/init.h                    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/global_data.h +1 -0
index d9e220cfe3..8c78792cc9 100644
@@ -166,5 +166,6 @@ typedef struct global_data {
 #define GD_FLG_SPL_EARLY_INIT	0x04000 /* Early SPL init is done	   */
 #define GD_FLG_LOG_READY	0x08000 /* Log system is ready for use	   */
 #define GD_FLG_WDT_READY	0x10000 /* Watchdog is ready for use	   */
+#define GD_FLG_SKIP_LL_INIT	0x20000	/* Don't perform low-level init	   */
 
 #endif /* __ASM_GENERIC_GBL_DATA_H */
include/init.h +1 -1
index 2a33a3fd1e..f84a1fc0e0 100644
@@ -20,7 +20,7 @@ struct global_data;
 #ifdef CONFIG_EFI_STUB
 #define ll_boot_init()	false
 #else
-#define ll_boot_init()	true
+#define ll_boot_init()	(!(gd->flags & GD_FLG_SKIP_LL_INIT))
 #endif
 
 /*
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#10 lore raw

PATCH v4 5/9 board: Add a gd flag for chain loading

Quoted 43 lines

Subject: [PATCH v4 5/9] board: Add a gd flag for chain loading

When U-Boot is run from another boot loader, much of the low-level init needs to be skipped.

Add a flag for this and adjust ll_boot_init() to use it.

---

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT

Changes in v3:
- Add a new patch with a gd flag for chain loading

Changes in v2: None

 include/asm-generic/global_data.h | 1 +
 include/init.h                    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/global_data.h +0 -0
index d9e220cfe3..8c78792cc9 100644
@@ -166,5 +166,6 @@ typedef struct global_data {
 #define GD_FLG_SPL_EARLY_INIT	0x04000 /* Early SPL init is done
 */
 #define GD_FLG_LOG_READY	0x08000 /* Log system is ready for use
 */
 #define GD_FLG_WDT_READY	0x10000 /* Watchdog is ready for use

*/
+#define GD_FLG_SKIP_LL_INIT 0x20000 /* Don't perform low-level init
*/

#endif /* __ASM_GENERIC_GBL_DATA_H */

include/init.h index 2a33a3fd1e..f84a1fc0e0 renamed from include/init.h +0 -0

100644

include/init.h struct global_data; · line 24

Quoted 6 lines
include/init.h +1 -1
@@ -20,7 +20,7 @@ struct global_data;
 #ifdef CONFIG_EFI_STUB
 #define ll_boot_init()	false
 #else
-#define ll_boot_init()	true
+#define ll_boot_init()	(!(gd->flags & GD_FLG_SKIP_LL_INIT))
 #endif
#11 lore raw

PATCH v4 5/9 board: Add a gd flag for chain loading

In reply to Simon Glass
Quoted 12 lines · Simon Glass

When U-Boot is run from another boot loader, much of the low-level init needs to be skipped.

Add a flag for this and adjust ll_boot_init() to use it.

---

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT

Quoted 9 lines

Changes in v3:
- Add a new patch with a gd flag for chain loading

Changes in v2: None

 include/asm-generic/global_data.h | 1 +
 include/init.h                    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
#12 lore raw

PATCH v4 6/9 x86: Move coreboot-table detection into common code

In reply to Simon Glass

To support detecting booting from coreboot, move the code which locates the coreboot tables into a common place. Adjust the algorithm slightly to use a word comparison instead of string, since it is faster.

---

Changes in v4:
- Add new patch to move coreboot-table detection into common code

Changes in v3: None
Changes in v2: None

 arch/x86/cpu/coreboot/tables.c         | 24 +++++++++---------------
 arch/x86/cpu/i386/cpu.c                | 25 +++++++++++++++++++++++++
 arch/x86/include/asm/coreboot_tables.h |  7 +++++++
 3 files changed, 41 insertions(+), 15 deletions(-)
arch/x86/cpu/coreboot/tables.c +9 -15
index 37e0424b5e..0f04c4f8e9 100644
@@ -115,20 +115,11 @@ __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr)
 
 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 {
+	unsigned char *ptr = addr;
 	struct cb_header *header;
-	unsigned char *ptr = (unsigned char *)addr;
 	int i;
 
-	for (i = 0; i < len; i += 16, ptr += 16) {
-		header = (struct cb_header *)ptr;
-		if (!strncmp((const char *)header->signature, "LBIO", 4))
-			break;
-	}
-
-	/* We walked the entire space and didn't find anything. */
-	if (i >= len)
-		return -1;
-
+	header = (struct cb_header *)ptr;
 	if (!header->table_bytes)
 		return 0;
 
@@ -231,10 +222,13 @@ static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 
 int get_coreboot_info(struct sysinfo_t *info)
 {
-	int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
+	long addr;
+	int ret;
 
-	if (ret != 1)
-		ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
+	addr = locate_coreboot_table();
+	if (addr < 0)
+		return addr;
+	ret = cb_parse_header((void *)addr, 0x1000, info);
 
-	return (ret == 1) ? 0 : -1;
+	return ret == 1 ? 0 : -ENOENT;
 }
arch/x86/cpu/i386/cpu.c +25 -0
index c8da7f10e9..e52fd686d9 100644
@@ -447,6 +447,31 @@ int x86_cpu_init_f(void)
 	return 0;
 }
 
+long detect_coreboot_table_at(ulong start, ulong size)
+{
+	u32 *ptr, *end;
+
+	size /= 4;
+	for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
+		if (*ptr == 0x4f49424c) /* "LBIO" */
+			return (long)ptr;
+	}
+
+	return -ENOENT;
+}
+
+long locate_coreboot_table(void)
+{
+	long addr;
+
+	/* We look for LBIO in the first 4K of RAM and again at 60KB */
+	addr = detect_coreboot_table_at(0x0, 0x1000);
+	if (addr < 0)
+		addr = detect_coreboot_table_at(0xf0000, 0x1000);
+
+	return addr;
+}
+
 int x86_cpu_reinit_f(void)
 {
 	setup_identity();
arch/x86/include/asm/coreboot_tables.h +7 -0
index 61de0077d7..268284f43c 100644
@@ -343,4 +343,11 @@ void *high_table_malloc(size_t bytes);
  */
 void write_coreboot_table(u32 addr, struct memory_area *cfg_tables);
 
+/**
+ * locate_coreboot_table() - Try to find coreboot tables@standard locations
+ *
+ * @return address of table that was found, or -ve error number
+ */
+long locate_coreboot_table(void);
+
 #endif
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#13 lore raw

PATCH v4 6/9 x86: Move coreboot-table detection into common code

Hi Simon,

Quoted 24 lines · Simon Glass

To support detecting booting from coreboot, move the code which locates the coreboot tables into a common place. Adjust the algorithm slightly to use a word comparison instead of string, since it is faster.

---

Changes in v4:
- Add new patch to move coreboot-table detection into common code

Changes in v3: None
Changes in v2: None

 arch/x86/cpu/coreboot/tables.c         | 24 +++++++++---------------
 arch/x86/cpu/i386/cpu.c                | 25 +++++++++++++++++++++++++
 arch/x86/include/asm/coreboot_tables.h |  7 +++++++
 3 files changed, 41 insertions(+), 15 deletions(-)
arch/x86/cpu/coreboot/tables.c +0 -0
index 37e0424b5e..0f04c4f8e9 100644

arch/x86/cpu/coreboot/tables.c __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr) · line 118

Quoted 4 lines
arch/x86/cpu/coreboot/tables.c +1 -0
@@ -115,20 +115,11 @@ __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr)
 
 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 {
+       unsigned char *ptr = addr;

This variable is not necessary

Quoted 5 lines
arch/x86/cpu/coreboot/tables.c +0 -2
        struct cb_header *header;
-       unsigned char *ptr = (unsigned char *)addr;
        int i;
 
-       for (i = 0; i < len; i += 16, ptr += 16) {

arch/x86/cpu/coreboot/tables.c __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr) · line 122

Quoted 10 lines
arch/x86/cpu/coreboot/tables.c +1 -9
-               header = (struct cb_header *)ptr;
-               if (!strncmp((const char *)header->signature, "LBIO", 4))
-                       break;
-       }
-
-       /* We walked the entire space and didn't find anything. */
-       if (i >= len)
-               return -1;
-
+       header = (struct cb_header *)ptr;

and assign addr to header directly

Quoted 37 lines
arch/x86/cpu/coreboot/tables.c +7 -4
        if (!header->table_bytes)
                return 0;
 
@@ -231,10 +222,13 @@ static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 
 int get_coreboot_info(struct sysinfo_t *info)
 {
-       int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
+       long addr;
+       int ret;
 
-       if (ret != 1)
-               ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
+       addr = locate_coreboot_table();
+       if (addr < 0)
+               return addr;
+       ret = cb_parse_header((void *)addr, 0x1000, info);
 
-       return (ret == 1) ? 0 : -1;
+       return ret == 1 ? 0 : -ENOENT;
 }
arch/x86/cpu/i386/cpu.c +8 -0
index c8da7f10e9..e52fd686d9 100644
@@ -447,6 +447,31 @@ int x86_cpu_init_f(void)
        return 0;
 }
 
+long detect_coreboot_table_at(ulong start, ulong size)
+{
+       u32 *ptr, *end;
+
+       size /= 4;
+       for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
+               if (*ptr == 0x4f49424c) /* "LBIO" */
+                       return (long)ptr;

arch/x86/cpu/i386/cpu.c int x86_cpu_init_f(void) · line 467

Quoted 10 lines
arch/x86/cpu/i386/cpu.c +10 -0
+       }
+
+       return -ENOENT;
+}
+
+long locate_coreboot_table(void)
+{
+       long addr;
+
+       /* We look for LBIO in the first 4K of RAM and again at 60KB */

It should be 960KB

arch/x86/cpu/i386/cpu.c int x86_cpu_init_f(void) · line 474

Quoted 7 lines
arch/x86/cpu/i386/cpu.c +7 -0
+       addr = detect_coreboot_table_at(0x0, 0x1000);
+       if (addr < 0)
+               addr = detect_coreboot_table_at(0xf0000, 0x1000);
+
+       return addr;
+}
+

Looks good otherwise:

Regards,
Bin

#14 lore raw

PATCH v4 6/9 x86: Move coreboot-table detection into common code

Hi Simon,

Quoted 29 lines · Bin Meng

Hi Simon,

Quoted 25 lines · Simon Glass

To support detecting booting from coreboot, move the code which locates the coreboot tables into a common place. Adjust the algorithm slightly to use a word comparison instead of string, since it is faster.

---

Changes in v4:
- Add new patch to move coreboot-table detection into common code

Changes in v3: None
Changes in v2: None

 arch/x86/cpu/coreboot/tables.c         | 24 +++++++++---------------
 arch/x86/cpu/i386/cpu.c                | 25 +++++++++++++++++++++++++
 arch/x86/include/asm/coreboot_tables.h |  7 +++++++
 3 files changed, 41 insertions(+), 15 deletions(-)
arch/x86/cpu/coreboot/tables.c +0 -0
index 37e0424b5e..0f04c4f8e9 100644
@@ -115,20 +115,11 @@ __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr)
 
Quoted 5 lines

arch/x86/cpu/coreboot/tables.c __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr) · line 118

Quoted 3 lines
arch/x86/cpu/coreboot/tables.c +1 -0
 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 {
+       unsigned char *ptr = addr;

This variable is not necessary

No, I was wrong.

Quoted 5 lines
Quoted 5 lines
arch/x86/cpu/coreboot/tables.c +0 -1
        struct cb_header *header;
-       unsigned char *ptr = (unsigned char *)addr;
        int i;
 
Quoted 13 lines

arch/x86/cpu/coreboot/tables.c __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr) · line 122

Quoted 11 lines
arch/x86/cpu/coreboot/tables.c +1 -10
-       for (i = 0; i < len; i += 16, ptr += 16) {
-               header = (struct cb_header *)ptr;
-               if (!strncmp((const char *)header->signature, "LBIO", 4))
-                       break;
-       }
-
-       /* We walked the entire space and didn't find anything. */
-       if (i >= len)
-               return -1;
-
+       header = (struct cb_header *)ptr;

and assign addr to header directly

Ignore this.

Quoted 42 lines
Quoted 42 lines
arch/x86/cpu/coreboot/tables.c +7 -4
        if (!header->table_bytes)
                return 0;
 
@@ -231,10 +222,13 @@ static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 
 int get_coreboot_info(struct sysinfo_t *info)
 {
-       int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
+       long addr;
+       int ret;
 
-       if (ret != 1)
-               ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
+       addr = locate_coreboot_table();
+       if (addr < 0)
+               return addr;
+       ret = cb_parse_header((void *)addr, 0x1000, info);
 
-       return (ret == 1) ? 0 : -1;
+       return ret == 1 ? 0 : -ENOENT;
 }
arch/x86/cpu/i386/cpu.c +12 -0
index c8da7f10e9..e52fd686d9 100644
@@ -447,6 +447,31 @@ int x86_cpu_init_f(void)
        return 0;
 }
 
+long detect_coreboot_table_at(ulong start, ulong size)
+{
+       u32 *ptr, *end;
+
+       size /= 4;
+       for (ptr = (void *)start, end = ptr + size; ptr < end; ptr += 4) {
+               if (*ptr == 0x4f49424c) /* "LBIO" */
+                       return (long)ptr;
+       }
+
+       return -ENOENT;
+}
Quoted 8 lines

arch/x86/cpu/i386/cpu.c int x86_cpu_init_f(void) · line 467

Quoted 6 lines
arch/x86/cpu/i386/cpu.c +6 -0
+
+long locate_coreboot_table(void)
+{
+       long addr;
+
+       /* We look for LBIO in the first 4K of RAM and again at 60KB */

It should be 960KB

I can fix this comments when applying.

Quoted 11 lines

arch/x86/cpu/i386/cpu.c int x86_cpu_init_f(void) · line 474

Quoted 8 lines
arch/x86/cpu/i386/cpu.c +7 -0
+       addr = detect_coreboot_table_at(0x0, 0x1000);
+       if (addr < 0)
+               addr = detect_coreboot_table_at(0xf0000, 0x1000);
+
+       return addr;
+}
+

Looks good otherwise:

Quoted 1 line

Regards,
Bin

#15 lore raw

PATCH v4 7/9 x86: Add a way to detect running from coreboot

In reply to Simon Glass

If U-Boot is running from coreboot we need to skip low-level init. Add an way to detect this and to set the gd flag.

---

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT
- Split this patch into two

Changes in v3:
- Add new patch to detect running from coreboot

Changes in v2: None

 arch/x86/cpu/i386/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/cpu/i386/cpu.c +2 -0
index e52fd686d9..8a458bb5ff 100644
@@ -476,6 +476,8 @@ int x86_cpu_reinit_f(void)
 {
 	setup_identity();
 	setup_pci_ram_top();
+	if (locate_coreboot_table() >= 0)
+		gd->flags |= GD_FLG_SKIP_LL_INIT;
 
 	return 0;
 }
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#16 lore raw

PATCH v4 7/9 x86: Add a way to detect running from coreboot

Quoted 11 lines · Simon Glass

If U-Boot is running from coreboot we need to skip low-level init. Add an way to detect this and to set the gd flag.

---

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT
- Split this patch into two

Quoted 8 lines

Changes in v3:
- Add new patch to detect running from coreboot

Changes in v2: None

 arch/x86/cpu/i386/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
#17 lore raw

PATCH v4 8/9 x86: Use the existing stack when chain-loading

In reply to Simon Glass

With chromebook_coral we normally run TPL->SPL->U-Boot. This is the 'bare metal' case.

When running from coreboot we put u-boot.bin in the RW_LEGACY portion of the image, e.g. with:

   cbfstool image-coral.serial.bin add-flat-binary -r RW_LEGACY \
	-f /tmp/b/chromebook_coral/u-boot.bin -n altfw/u-boot \
	-c lzma -l 0x1110000 -e 0x1110000

In this case U-Boot is run from coreboot (actually Depthcharge, its payload) so we cannot access CAR. Use the existing stack instead.

---

Changes in v4:
- Update to use locate_coreboot_table()

Changes in v3: None
Changes in v2: None

 arch/x86/cpu/start_from_spl.S | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
arch/x86/cpu/start_from_spl.S +14 -2
index 22cab2dd6c..905c825cdc 100644
@@ -14,18 +14,30 @@
 .globl _start
 .type _start, @function
 _start:
-	/* Set up memory using the existing stack */
+	/*
+	 * If running from coreboot, CAR is no-longer available. Use the
+	 * existing stack, which is large enough.
+	 */
+	call	locate_coreboot_table
+	cmp	$0, %eax
+	jge	use_existing_stack
+
 	movl	$(CONFIG_SYS_CAR_ADDR + CONFIG_SYS_CAR_SIZE - 4), %eax
 #ifdef CONFIG_DCACHE_RAM_MRC_VAR_SIZE
 	subl	$CONFIG_DCACHE_RAM_MRC_VAR_SIZE, %eax
 #endif
+	jmp	2f
 	/*
-	 * We don't subject CONFIG_DCACHE_RAM_MRC_VAR_SIZE since memory is
+	 * We don't subtract CONFIG_DCACHE_RAM_MRC_VAR_SIZE since memory is
 	 * already set up. This has the happy side-effect of putting gd in a
 	 * new place separate from SPL, so the memset() in
 	 * board_init_f_init_reserve() does not cause any problems (otherwise
 	 * it would zero out the gd and crash)
 	 */
+	/* Set up memory using the existing stack */
+use_existing_stack:
+	mov	%esp, %eax
+2:
 	call	board_init_f_alloc_reserve
 	mov	%eax, %esp
 
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#18 lore raw

PATCH v4 8/9 x86: Use the existing stack when chain-loading

Quoted 17 lines · Simon Glass

With chromebook_coral we normally run TPL->SPL->U-Boot. This is the 'bare metal' case.

When running from coreboot we put u-boot.bin in the RW_LEGACY portion of the image, e.g. with:

   cbfstool image-coral.serial.bin add-flat-binary -r RW_LEGACY \
        -f /tmp/b/chromebook_coral/u-boot.bin -n altfw/u-boot \
        -c lzma -l 0x1110000 -e 0x1110000

In this case U-Boot is run from coreboot (actually Depthcharge, its payload) so we cannot access CAR. Use the existing stack instead.

---

Quoted 9 lines

Changes in v4:
- Update to use locate_coreboot_table()

Changes in v3: None
Changes in v2: None

 arch/x86/cpu/start_from_spl.S | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
#19 lore raw

PATCH v4 9/9 x86: Add documentation for the chain-load feature

In reply to Simon Glass

Add a few notes about this feature, which is aimed for development.

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 doc/arch/x86.rst | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
doc/arch/x86.rst +28 -0
index a441738ad1..c6b70ce61a 100644
@@ -712,6 +712,34 @@ to load a 'u-boot-payload.efi', see below test logs on QEMU.
 See :doc:`../uefi/u-boot_on_efi` and :doc:`../uefi/uefi` for details of
 EFI support in U-Boot.
 
+Chain-loading
+-------------
+U-Boot can be chain-loaded from another bootloader, such as coreboot or
+Slim Bootloader. Typically this is done by building for targets 'coreboot' or
+'slimbootloader'.
+
+For example, at present we have a 'coreboot' target but this runs very
+different code from the bare-metal targets, such as coral. There is very little
+in common between them.
+
+It is useful to be able to boot the same U-Boot on a device, with or without a
+first-stage bootloader. For example, with chromebook_coral, it is helpful for
+testing to be able to boot the same U-Boot (complete with FSP) on bare metal
+and from coreboot. It allows checking of things like CPU speed, comparing
+registers, ACPI tables and the like.
+
+To do this you can use ll_boot_init() in appropriate places to skip init that
+has already been done by the previous stage. This works by setting a
+GD_FLG_NO_LL_INIT flag when U-Boot detects that it is running from another
+bootloader.
+
+With this feature, you can build a bare-metal target and boot it from
+coreboot, for example.
+
+Note that this is a development feature only. It is not intended for use in
+production environments. Also it is not currently part of the automated tests
+so may break in the future.
+
 TODO List
 ---------
 - Audio
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#20 lore raw

PATCH v4 9/9 x86: Add documentation for the chain-load feature

Quoted 4 lines · Simon Glass

Add a few notes about this feature, which is aimed for development.

Quoted 9 lines

---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 doc/arch/x86.rst | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
#21 lore raw

v4 cover x86: Improve support for chain-loading U-Boot

In reply to Simon Glass

Hi Simon,

Quoted 21 lines · Simon Glass

This little series adds a few checks into the code to allow better operation when booting a build from a previous-state loader such as coreboot.

At present we have a 'coreboot' target but this runs very different code from the bare-metal targets, such as coral. There is very little in common between them.

It is useful to be able to boot the same U-Boot on a device, with or without a first-stage bootloader. For example, with chromebook_coral, it is helpful for testing to be able to boot the same U-Boot (complete with FSP) on bare metal and from coreboot. It allows checking of things like CPU speed, comparing registers, ACPI tables and the like.

This series allows U-Boot to detect that it ran from coreboot and automatically do the right thing.

This series makes the most important changes to allow the same u-boot.bin for coral to boot after coreboot (by itself) or bare metal (via TPL->SPL).

Quoted 6 lines

Changes in v4:
- Rename flag to GD_FLG_SKIP_LL_INIT
- Add new patch to move coreboot-table detection into common code
- Rename flag to GD_FLG_SKIP_LL_INIT
- Split this patch into two
- Update to use locate_coreboot_table()

v4 series applied to u-boot-x86/master, thanks!

Regards,
Bin