Allow to build romstage sources inside the bootblock

Having a separate romstage is only desirable:
 - with advanced setups like vboot or normal/fallback
 - boot medium is slow at startup (some ARM SOCs)
 - bootblock is limited in size (Intel APL 32K)

When this is not the case there is no need for the extra complexity
that romstage brings. Including the romstage sources inside the
bootblock substantially reduces the total code footprint. Often the
resulting code is 10-20k smaller.

This is controlled via a Kconfig option.

TESTED: works on qemu x86, arm and aarch64 with and without VBOOT.

Change-Id: Id68390edc1ba228b121cca89b80c64a92553e284
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/55068
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Felix Held <felix-coreboot@felixheld.de>
diff --git a/Makefile.inc b/Makefile.inc
index 22a4646..e382518 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -1250,6 +1250,7 @@
 	fi
 endif # CONFIG_CBFS_VERIFICATION
 
+ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y)
 cbfs-files-y += $(CONFIG_CBFS_PREFIX)/romstage
 $(CONFIG_CBFS_PREFIX)/romstage-file := $(objcbfs)/romstage.elf
 $(CONFIG_CBFS_PREFIX)/romstage-type := stage
@@ -1288,6 +1289,9 @@
 ifeq ($(CONFIG_VBOOT_STARTS_IN_ROMSTAGE),y)
 $(CONFIG_CBFS_PREFIX)/romstage-options += $(TXTIBB)
 endif
+else # CONFIG_SEPARATE_ROMSTAGE
+postinclude-hooks += $$(eval bootblock-srcs += $$(romstage-srcs))
+endif
 
 cbfs-files-$(CONFIG_HAVE_RAMSTAGE) += $(CONFIG_CBFS_PREFIX)/ramstage
 $(CONFIG_CBFS_PREFIX)/ramstage-file := $(RAMSTAGE)
diff --git a/src/Kconfig b/src/Kconfig
index 02d5d77..5cb9a1a 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -243,6 +243,14 @@
 	  user-selectable. (There's no real point in offering this to the user
 	  anyway... if it works and saves boot time, you would always want it.)
 
+config SEPARATE_ROMSTAGE
+	bool "Build a separate romstage"
+	default y
+	help
+	  Build a separate romstage that is loaded by bootblock. With this
+	  option disabled the romstage sources are linked inside the bootblock
+	  as a single stage.
+
 config INCLUDE_CONFIG_FILE
 	bool "Include the coreboot .config file into the ROM image"
 	# Default value set at the end of the file
@@ -1523,6 +1531,7 @@
 
 config HAVE_ROMSTAGE
 	bool
+	depends on SEPARATE_ROMSTAGE
 	default y
 
 config HAVE_RAMSTAGE
diff --git a/src/arch/arm64/romstage.c b/src/arch/arm64/romstage.c
index 654ba5a..0c37711 100644
--- a/src/arch/arm64/romstage.c
+++ b/src/arch/arm64/romstage.c
@@ -11,6 +11,7 @@
 __weak void platform_romstage_main(void) { /* no-op, for bring-up */ }
 __weak void platform_romstage_postram(void) { /* no-op */ }
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_add_now(TS_ROMSTAGE_START);
@@ -20,6 +21,7 @@
 	exception_init();
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/arch/x86/Makefile.inc b/src/arch/x86/Makefile.inc
index 384eacd..75b9d3d 100644
--- a/src/arch/x86/Makefile.inc
+++ b/src/arch/x86/Makefile.inc
@@ -161,12 +161,12 @@
 
 ifeq ($(CONFIG_ARCH_ROMSTAGE_X86_32)$(CONFIG_ARCH_ROMSTAGE_X86_64),y)
 
-romstage-y += assembly_entry.S
-romstage-y += romstage.c
+romstage-$(CONFIG_SEPARATE_ROMSTAGE) += assembly_entry.S
+romstage-$(CONFIG_SEPARATE_ROMSTAGE) += romstage.c
 romstage-y += boot.c
 romstage-$(CONFIG_DEBUG_HW_BREAKPOINTS_IN_ALL_STAGES) += breakpoint.c
 romstage-y += post.c
-romstage-y += gdt_init.S
+romstage-$(CONFIG_SEPARATE_ROMSTAGE) += gdt_init.S
 romstage-y += cpu_common.c
 romstage-$(CONFIG_IDT_IN_EVERY_STAGE) += exception.c
 romstage-$(CONFIG_IDT_IN_EVERY_STAGE) += idt.S
diff --git a/src/arch/x86/assembly_entry.S b/src/arch/x86/assembly_entry.S
index 9a9a046..7f19e21 100644
--- a/src/arch/x86/assembly_entry.S
+++ b/src/arch/x86/assembly_entry.S
@@ -53,7 +53,7 @@
 #endif
 
 #if ((ENV_SEPARATE_VERSTAGE && CONFIG(VERSTAGE_DEBUG_SPINLOOP)) \
-	|| (ENV_ROMSTAGE && CONFIG(ROMSTAGE_DEBUG_SPINLOOP)))
+	|| (ENV_SEPARATE_ROMSTAGE && CONFIG(ROMSTAGE_DEBUG_SPINLOOP)))
 
 	/* Wait for a JTAG debugger to break in and set EBX non-zero */
 	xor	%ebx, %ebx
diff --git a/src/arch/x86/car.ld b/src/arch/x86/car.ld
index 2ad1ca2..eb75981 100644
--- a/src/arch/x86/car.ld
+++ b/src/arch/x86/car.ld
@@ -75,7 +75,7 @@
 	RECORD_SIZE(bss)
 #endif
 
-#if ENV_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)
+#if ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)
 	_shadow_size = (_ebss - _car_region_start) >> 3;
 	REGION(asan_shadow, ., _shadow_size, ARCH_POINTER_ALIGN_SIZE)
 #endif
@@ -144,7 +144,7 @@
 _bogus2 = ASSERT(_pagetables == ALIGN(_pagetables, 4096), "_pagetables aren't 4KiB aligned");
 #endif
 _bogus3 = ASSERT(CONFIG_DCACHE_BSP_STACK_SIZE > 0x0, "BSP stack size not configured");
-#if CONFIG(NO_XIP_EARLY_STAGES) && (ENV_ROMSTAGE || ENV_SEPARATE_VERSTAGE)
+#if CONFIG(NO_XIP_EARLY_STAGES) && (ENV_SEPARATE_ROMSTAGE || ENV_SEPARATE_VERSTAGE)
 _bogus4 = ASSERT(_eprogram <= _car_region_end, "Stage end too high !");
 _bogus5 = ASSERT(_program >= _car_unallocated_start, "Stage start too low!");
 #endif
diff --git a/src/arch/x86/memcpy.c b/src/arch/x86/memcpy.c
index d96a93c..9da2a75 100644
--- a/src/arch/x86/memcpy.c
+++ b/src/arch/x86/memcpy.c
@@ -8,7 +8,7 @@
 {
 	unsigned long d0, d1, d2;
 
-#if (ENV_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
+#if (ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
 		(ENV_RAMSTAGE && CONFIG(ASAN_IN_RAMSTAGE))
 	check_memory_region((unsigned long)src, n, false, _RET_IP_);
 	check_memory_region((unsigned long)dest, n, true, _RET_IP_);
diff --git a/src/arch/x86/memlayout.ld b/src/arch/x86/memlayout.ld
index f448bf8..c199118 100644
--- a/src/arch/x86/memlayout.ld
+++ b/src/arch/x86/memlayout.ld
@@ -26,7 +26,7 @@
 	/* Relocated at runtime in cbmem so the address does not matter. */
 	RAMSTAGE(64M, 8M)
 
-#elif ENV_ROMSTAGE
+#elif ENV_SEPARATE_ROMSTAGE
 	/* The 1M size is not allocated. It's just for basic size checking.
 	 * Link at 32MiB address and rely on cbfstool to relocate to XIP. */
 	ROMSTAGE(CONFIG_ROMSTAGE_ADDR, 1M)
diff --git a/src/arch/x86/memmove_32.c b/src/arch/x86/memmove_32.c
index 3ec50b2..387a77e 100644
--- a/src/arch/x86/memmove_32.c
+++ b/src/arch/x86/memmove_32.c
@@ -12,7 +12,7 @@
 	int d0, d1, d2, d3, d4, d5;
 	char *ret = dest;
 
-#if (ENV_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
+#if (ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
 		(ENV_RAMSTAGE && CONFIG(ASAN_IN_RAMSTAGE))
 	check_memory_region((unsigned long)src, n, false, _RET_IP_);
 	check_memory_region((unsigned long)dest, n, true, _RET_IP_);
diff --git a/src/arch/x86/memset.c b/src/arch/x86/memset.c
index 8a0165b..142dda3 100644
--- a/src/arch/x86/memset.c
+++ b/src/arch/x86/memset.c
@@ -14,7 +14,7 @@
 	int d0;
 	unsigned long int dstp = (unsigned long int)dstpp;
 
-#if (ENV_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
+#if (ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
 		(ENV_RAMSTAGE && CONFIG(ASAN_IN_RAMSTAGE))
 	check_memory_region((unsigned long)dstpp, len, true, _RET_IP_);
 #endif
diff --git a/src/commonlib/storage/sdhci.c b/src/commonlib/storage/sdhci.c
index 882920d..410d4fc 100644
--- a/src/commonlib/storage/sdhci.c
+++ b/src/commonlib/storage/sdhci.c
@@ -18,7 +18,7 @@
 
 #define DMA_AVAILABLE	((CONFIG(SDHCI_ADMA_IN_BOOTBLOCK) && ENV_BOOTBLOCK) \
 			|| (CONFIG(SDHCI_ADMA_IN_VERSTAGE) && ENV_SEPARATE_VERSTAGE) \
-			|| (CONFIG(SDHCI_ADMA_IN_ROMSTAGE) && ENV_ROMSTAGE) \
+			|| (CONFIG(SDHCI_ADMA_IN_ROMSTAGE) && ENV_SEPARATE_ROMSTAGE) \
 			|| ENV_POSTCAR || ENV_RAMSTAGE)
 
 __weak void *dma_malloc(size_t length_in_bytes)
diff --git a/src/console/Kconfig b/src/console/Kconfig
index 8efe2e47..84eb9f9 100644
--- a/src/console/Kconfig
+++ b/src/console/Kconfig
@@ -2,6 +2,7 @@
 
 config NO_BOOTBLOCK_CONSOLE
 	bool
+	depends on SEPARATE_ROMSTAGE
 
 config BOOTBLOCK_CONSOLE
 	bool "Enable early (bootblock) console output."
diff --git a/src/console/init.c b/src/console/init.c
index 8918dcf..3b89326 100644
--- a/src/console/init.c
+++ b/src/console/init.c
@@ -9,7 +9,7 @@
 #include <option.h>
 #include <version.h>
 
-#define FIRST_CONSOLE (ENV_BOOTBLOCK || (CONFIG(NO_BOOTBLOCK_CONSOLE) && ENV_ROMSTAGE))
+#define FIRST_CONSOLE (ENV_BOOTBLOCK || (CONFIG(NO_BOOTBLOCK_CONSOLE) && ENV_SEPARATE_ROMSTAGE))
 
 static int console_inited;
 static int console_loglevel;
@@ -53,7 +53,7 @@
 	if (CONFIG(DEBUG_CONSOLE_INIT))
 		console_inited = 1;
 
-	if (CONFIG(EARLY_PCI_BRIDGE) && (ENV_BOOTBLOCK || ENV_ROMSTAGE))
+	if (CONFIG(EARLY_PCI_BRIDGE) && (ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE))
 		pci_early_bridge_init();
 
 	console_hw_init();
diff --git a/src/cpu/x86/cache/cache.c b/src/cpu/x86/cache/cache.c
index 6413660..11524e6 100644
--- a/src/cpu/x86/cache/cache.c
+++ b/src/cpu/x86/cache/cache.c
@@ -33,7 +33,7 @@
 	   to make sure that our code hits dram during romstage. */
 	if (!ENV_CACHE_AS_RAM)
 		return;
-	if (!ENV_ROMSTAGE)
+	if (!ENV_RAMINIT)
 		return;
 	if (!CONFIG(POSTCAR_STAGE))
 		return;
diff --git a/src/drivers/siemens/nc_fpga/nc_fpga_early.c b/src/drivers/siemens/nc_fpga/nc_fpga_early.c
index 284ec8a..830291c 100644
--- a/src/drivers/siemens/nc_fpga/nc_fpga_early.c
+++ b/src/drivers/siemens/nc_fpga/nc_fpga_early.c
@@ -42,7 +42,7 @@
 	/* The function pci_early_device_probe is called in bootblock and romstage. Make sure
 	   that in these stages the initialization code was successful before the POST code
 	   value is written to the register. */
-	if ((ENV_BOOTBLOCK || ENV_ROMSTAGE) && nc_fpga_present == false)
+	if ((ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE) && nc_fpga_present == false)
 		return;
 	write32p(fpga_bar + NC_FPGA_POST_OFFSET, value);
 }
diff --git a/src/drivers/usb/ehci_debug.c b/src/drivers/usb/ehci_debug.c
index 178877b..4ddefcc 100644
--- a/src/drivers/usb/ehci_debug.c
+++ b/src/drivers/usb/ehci_debug.c
@@ -53,7 +53,7 @@
 {
 	if (glob_dbg_info_p == NULL) {
 		struct ehci_debug_info *info;
-		if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE || ENV_ROMSTAGE) {
+		if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE || ENV_SEPARATE_ROMSTAGE) {
 			/* The message likely does not show if we hit this. */
 			if (sizeof(*info) > _car_ehci_dbg_info_size)
 				die("BUG: Increase ehci_dbg_info reserve in CAR");
@@ -730,7 +730,7 @@
 	 * from CBMEM.
 	 */
 	if (CONFIG(USBDEBUG_IN_PRE_RAM)
-	    && (ENV_ROMSTAGE || ENV_BOOTBLOCK))
+	    && (ENV_SEPARATE_ROMSTAGE || ENV_BOOTBLOCK))
 		usbdebug_hw_init(false);
 
 	/* USB console init is done early in ramstage if it was
diff --git a/src/include/console/cbmem_console.h b/src/include/console/cbmem_console.h
index 9a814b9..84a95dd 100644
--- a/src/include/console/cbmem_console.h
+++ b/src/include/console/cbmem_console.h
@@ -10,7 +10,7 @@
 
 #define __CBMEM_CONSOLE_ENABLE__	(CONFIG(CONSOLE_CBMEM) && \
 	(ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE || ENV_POSTCAR  || \
-	 ENV_ROMSTAGE || (ENV_BOOTBLOCK && CONFIG(BOOTBLOCK_CONSOLE)) || \
+	 ENV_SEPARATE_ROMSTAGE || (ENV_BOOTBLOCK && CONFIG(BOOTBLOCK_CONSOLE)) || \
 	 (ENV_SMM && CONFIG(DEBUG_SMI))))
 
 #if __CBMEM_CONSOLE_ENABLE__
diff --git a/src/include/console/console.h b/src/include/console/console.h
index fb257ba1..7e9a439 100644
--- a/src/include/console/console.h
+++ b/src/include/console/console.h
@@ -44,7 +44,7 @@
 #define __CONSOLE_ENABLE__ \
 	((ENV_BOOTBLOCK && CONFIG(BOOTBLOCK_CONSOLE)) || \
 	 (ENV_POSTCAR && CONFIG(POSTCAR_CONSOLE)) || \
-	 ENV_SEPARATE_VERSTAGE || ENV_ROMSTAGE || ENV_RAMSTAGE || \
+	 ENV_SEPARATE_VERSTAGE || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || \
 	 ENV_LIBAGESA || (ENV_SMM && CONFIG(DEBUG_SMI)))
 
 #if __CONSOLE_ENABLE__
diff --git a/src/include/console/qemu_debugcon.h b/src/include/console/qemu_debugcon.h
index 155bfda..ed1d8e0 100644
--- a/src/include/console/qemu_debugcon.h
+++ b/src/include/console/qemu_debugcon.h
@@ -9,7 +9,7 @@
 void qemu_debugcon_tx_byte(unsigned char data);
 
 #if CONFIG(CONSOLE_QEMU_DEBUGCON) && \
-	(ENV_ROMSTAGE || ENV_RAMSTAGE || ENV_POSTCAR || ENV_BOOTBLOCK)
+	(ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || ENV_POSTCAR || ENV_BOOTBLOCK)
 static inline void __qemu_debugcon_init(void)	{ qemu_debugcon_init(); }
 static inline void __qemu_debugcon_tx_byte(u8 data)
 {
diff --git a/src/include/console/system76_ec.h b/src/include/console/system76_ec.h
index 2aa265a..6e9311a 100644
--- a/src/include/console/system76_ec.h
+++ b/src/include/console/system76_ec.h
@@ -11,7 +11,7 @@
 void system76_ec_print(uint8_t byte);
 
 #define __CONSOLE_SYSTEM76_EC_ENABLE__	(CONFIG(CONSOLE_SYSTEM76_EC) && \
-	(ENV_BOOTBLOCK || ENV_ROMSTAGE || ENV_RAMSTAGE \
+	(ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE \
 	 || ENV_SEPARATE_VERSTAGE || ENV_POSTCAR \
 	 || (ENV_SMM && CONFIG(DEBUG_SMI))))
 
diff --git a/src/include/console/uart.h b/src/include/console/uart.h
index ca03ecb..3f9e5b0 100644
--- a/src/include/console/uart.h
+++ b/src/include/console/uart.h
@@ -62,7 +62,7 @@
 void oxford_remap(unsigned int new_base);
 
 #define __CONSOLE_SERIAL_ENABLE__	(CONFIG(CONSOLE_SERIAL) && \
-	(ENV_BOOTBLOCK || ENV_ROMSTAGE || ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE \
+	(ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE \
 	 || ENV_POSTCAR || (ENV_SMM && CONFIG(DEBUG_SMI))))
 
 #if __CONSOLE_SERIAL_ENABLE__
diff --git a/src/include/console/usb.h b/src/include/console/usb.h
index 30591c2..fa8d511 100644
--- a/src/include/console/usb.h
+++ b/src/include/console/usb.h
@@ -15,7 +15,7 @@
 
 #define __CONSOLE_USB_ENABLE__	(CONFIG(CONSOLE_USB) && \
 	((ENV_BOOTBLOCK && CONFIG(USBDEBUG_IN_PRE_RAM)) || \
-	 (ENV_ROMSTAGE && CONFIG(USBDEBUG_IN_PRE_RAM)) || \
+	 (ENV_SEPARATE_ROMSTAGE && CONFIG(USBDEBUG_IN_PRE_RAM)) || \
 	 (ENV_POSTCAR && CONFIG(USBDEBUG_IN_PRE_RAM)) || \
 	 (ENV_SEPARATE_VERSTAGE && CONFIG(USBDEBUG_IN_PRE_RAM)) || \
 	 ENV_RAMSTAGE))
diff --git a/src/include/memlayout.h b/src/include/memlayout.h
index 0d84ec1..6280b20 100644
--- a/src/include/memlayout.h
+++ b/src/include/memlayout.h
@@ -134,7 +134,7 @@
 		REGION(bootblock, addr, sz, 1)
 #endif
 
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	#define ROMSTAGE(addr, sz) \
 		SYMBOL(romstage, addr) \
 		_eromstage = ABSOLUTE(_romstage + sz); \
diff --git a/src/include/rules.h b/src/include/rules.h
index b30b21d..7d32c77 100644
--- a/src/include/rules.h
+++ b/src/include/rules.h
@@ -22,7 +22,7 @@
 #if defined(__DECOMPRESSOR__)
 #define ENV_DECOMPRESSOR 1
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -34,7 +34,7 @@
 #elif defined(__BOOTBLOCK__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 1
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -46,7 +46,7 @@
 #elif defined(__ROMSTAGE__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 1
+#define ENV_SEPARATE_ROMSTAGE 1
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -58,7 +58,7 @@
 #elif defined(__SMM__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 1
 #define ENV_SEPARATE_VERSTAGE 0
@@ -72,13 +72,13 @@
  * bootblock/romstage, depending on the setting of the VBOOT_SEPARATE_VERSTAGE
  * kconfig option. The ENV_SEPARATE_VERSTAGE macro will only return true for
  * "verstage" code when CONFIG(VBOOT_SEPARATE_VERSTAGE) is true, otherwise that
- * code will have ENV_BOOTBLOCK or ENV_ROMSTAGE set (depending on the
+ * code will have ENV_BOOTBLOCK or ENV_SEPARATE_ROMSTAGE set (depending on the
  * "VBOOT_STARTS_IN_"... kconfig options).
  */
 #elif defined(__VERSTAGE__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 1
@@ -94,7 +94,7 @@
 #elif defined(__RAMSTAGE__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 1
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -106,7 +106,7 @@
 #elif defined(__RMODULE__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -118,7 +118,7 @@
 #elif defined(__POSTCAR__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -130,7 +130,7 @@
 #elif defined(__LIBAGESA__)
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -146,7 +146,7 @@
  */
 #define ENV_DECOMPRESSOR 0
 #define ENV_BOOTBLOCK 0
-#define ENV_ROMSTAGE 0
+#define ENV_SEPARATE_ROMSTAGE 0
 #define ENV_RAMSTAGE 0
 #define ENV_SMM 0
 #define ENV_SEPARATE_VERSTAGE 0
@@ -268,7 +268,7 @@
 #endif
 
 #define ENV_ROMSTAGE_OR_BEFORE \
-	(ENV_DECOMPRESSOR || ENV_BOOTBLOCK || ENV_ROMSTAGE || \
+	(ENV_DECOMPRESSOR || ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || \
 	(ENV_SEPARATE_VERSTAGE && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)))
 
 #if ENV_X86
@@ -299,9 +299,9 @@
 #define ENV_INITIAL_STAGE		ENV_BOOTBLOCK
 #endif
 
-#define ENV_CREATES_CBMEM	ENV_ROMSTAGE
-#define ENV_HAS_CBMEM		(ENV_ROMSTAGE | ENV_POSTCAR | ENV_RAMSTAGE)
-#define ENV_RAMINIT		ENV_ROMSTAGE
+#define ENV_CREATES_CBMEM	(ENV_SEPARATE_ROMSTAGE || (ENV_BOOTBLOCK && !CONFIG(SEPARATE_ROMSTAGE)))
+#define ENV_HAS_CBMEM		(ENV_CREATES_CBMEM || ENV_POSTCAR || ENV_RAMSTAGE)
+#define ENV_RAMINIT		(ENV_SEPARATE_ROMSTAGE || (ENV_BOOTBLOCK && !CONFIG(SEPARATE_ROMSTAGE)))
 
 #if ENV_X86
 #define ENV_HAS_SPINLOCKS		!ENV_ROMSTAGE_OR_BEFORE
@@ -314,7 +314,7 @@
 /* When set <arch/smp/spinlock.h> is included for the spinlock implementation. */
 #define ENV_SUPPORTS_SMP		(CONFIG(SMP) && ENV_HAS_SPINLOCKS)
 
-#if ENV_X86 && CONFIG(COOP_MULTITASKING) && (ENV_RAMSTAGE || ENV_ROMSTAGE)
+#if ENV_X86 && CONFIG(COOP_MULTITASKING) && (ENV_RAMSTAGE || ENV_CREATES_CBMEM)
 /* TODO: Enable in all x86 stages */
 #define ENV_SUPPORTS_COOP         1
 #else
diff --git a/src/include/symbols.h b/src/include/symbols.h
index 5410798..b1e44e11 100644
--- a/src/include/symbols.h
+++ b/src/include/symbols.h
@@ -39,7 +39,7 @@
 DECLARE_REGION(fmap_cache)
 DECLARE_REGION(tpm_log)
 
-#if ENV_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)
+#if ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)
 DECLARE_REGION(bss)
 DECLARE_REGION(asan_shadow)
 #endif
diff --git a/src/include/timestamp.h b/src/include/timestamp.h
index cbe9934..ec2364f 100644
--- a/src/include/timestamp.h
+++ b/src/include/timestamp.h
@@ -11,7 +11,7 @@
  * timestamp_init() needs to be called once in *one* of the ENV_ROMSTAGE_OR_BEFORE
  * stages (bootblock, romstage, verstage, etc). It's up to the chipset/arch
  * to make the call in the earliest stage, otherwise some timestamps will be lost.
- * For x86 ENV_ROMSTAGE call must be made before CAR is torn down.
+ * For x86 ENV_BOOTBLOCK / ENV_SEPARATE_ROMSTAGE call must be made before CAR is torn down.
  */
 void timestamp_init(uint64_t base);
 /*
diff --git a/src/lib/asan.c b/src/lib/asan.c
index 9493b3b..8f92d5a 100644
--- a/src/lib/asan.c
+++ b/src/lib/asan.c
@@ -16,7 +16,7 @@
 
 static inline void *asan_mem_to_shadow(const void *addr)
 {
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	return (void *)((uintptr_t)&_asan_shadow + (((uintptr_t)addr -
 		(uintptr_t)&_car_region_start) >> ASAN_SHADOW_SCALE_SHIFT));
 #elif ENV_RAMSTAGE
@@ -27,7 +27,7 @@
 
 static inline const void *asan_shadow_to_mem(const void *shadow_addr)
 {
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	return (void *)((uintptr_t)&_car_region_start + (((uintptr_t)shadow_addr -
 		(uintptr_t)&_asan_shadow) << ASAN_SHADOW_SCALE_SHIFT));
 #elif ENV_RAMSTAGE
@@ -237,7 +237,7 @@
 						size_t size, bool write,
 						unsigned long ret_ip)
 {
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	if (((uintptr_t)addr < (uintptr_t)&_car_region_start) ||
 		((uintptr_t)addr > (uintptr_t)&_ebss))
 		return;
@@ -269,7 +269,7 @@
 
 uintptr_t __asan_shadow_offset(uintptr_t addr)
 {
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	return (uintptr_t)&_asan_shadow - (((uintptr_t)&_car_region_start) >>
 		ASAN_SHADOW_SCALE_SHIFT);
 #elif ENV_RAMSTAGE
@@ -323,7 +323,7 @@
 
 void asan_init(void)
 {
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	size_t size = (size_t)&_ebss - (size_t)&_car_region_start;
 	asan_unpoison_shadow((void *)&_car_region_start, size);
 #elif ENV_RAMSTAGE
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index e5f232c..92a00e4 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -141,16 +141,21 @@
 		return true;
 	if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
 		return true;
-	/* We assume here romstage and postcar are never compressed. */
-	if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
+
+	/* Payload loader (ramstage) always needs LZMA. */
+	if (ENV_PAYLOAD_LOADER)
+		return true;
+	/* Only other use of LZMA is ramstage compression. */
+	if (!CONFIG(COMPRESS_RAMSTAGE_LZMA))
 		return false;
-	if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
-		return false;
-	if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE_LZMA))
-		return false;
-	if (ENV_SMM)
-		return false;
-	return true;
+	/* If there is a postcar, it loads the ramstage. */
+	if (CONFIG(POSTCAR_STAGE))
+		return ENV_POSTCAR;
+	/* If there is no postcar but a separate romstage, it loads the ramstage. */
+	if (CONFIG(SEPARATE_ROMSTAGE))
+		return ENV_SEPARATE_ROMSTAGE;
+	/* Otherwise, the combined bootblock+romstage loads the ramstage. */
+	return ENV_BOOTBLOCK;
 }
 
 static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
@@ -333,7 +338,7 @@
 		dead_code();
 
 	/* We don't want to cross the vboot boundary */
-	if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
+	if (ENV_SEPARATE_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
 		return;
 
 	DEBUG("%s(name='%s')\n", __func__, name);
diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c
index 9e661c5..b2abbff 100644
--- a/src/lib/prog_loaders.c
+++ b/src/lib/prog_loaders.c
@@ -9,6 +9,7 @@
 #include <program_loading.h>
 #include <reset.h>
 #include <rmodule.h>
+#include <romstage_common.h>
 #include <security/vboot/vboot_common.h>
 #include <stage_cache.h>
 #include <symbols.h>
@@ -16,6 +17,13 @@
 
 void run_romstage(void)
 {
+	if (!CONFIG(SEPARATE_ROMSTAGE)) {
+		/* Call romstage instead of loading it as a cbfs file. */
+		timestamp_add_now(TS_ROMSTAGE_START);
+		romstage_main();
+		dead_code();
+	}
+
 	struct prog romstage =
 		PROG_INIT(PROG_ROMSTAGE, CONFIG_CBFS_PREFIX "/romstage");
 
diff --git a/src/mainboard/emulation/qemu-armv7/romstage.c b/src/mainboard/emulation/qemu-armv7/romstage.c
index 598ddde..05a75bb 100644
--- a/src/mainboard/emulation/qemu-armv7/romstage.c
+++ b/src/mainboard/emulation/qemu-armv7/romstage.c
@@ -5,11 +5,13 @@
 #include <program_loading.h>
 #include <romstage_common.h>
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	console_init();
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/emulation/qemu-riscv/memlayout.ld b/src/mainboard/emulation/qemu-riscv/memlayout.ld
index 96ab74c..4fdeb9dc 100644
--- a/src/mainboard/emulation/qemu-riscv/memlayout.ld
+++ b/src/mainboard/emulation/qemu-riscv/memlayout.ld
@@ -17,7 +17,7 @@
 	SRAM_END(STAGES_START)
 	DRAM_START(STAGES_START)
 
-#if ENV_ROMSTAGE
+#if ENV_SEPARATE_ROMSTAGE
 	ROMSTAGE(STAGES_START, 128K)
 #endif
 #if ENV_RAMSTAGE
diff --git a/src/mainboard/facebook/fbg1701/board_verified_boot.c b/src/mainboard/facebook/fbg1701/board_verified_boot.c
index 4932964..325acd6 100644
--- a/src/mainboard/facebook/fbg1701/board_verified_boot.c
+++ b/src/mainboard/facebook/fbg1701/board_verified_boot.c
@@ -7,8 +7,10 @@
  * items to the TPM
  */
 const verify_item_t bootblock_verify_list[] = {
+#if CONFIG(SEPARATE_ROMSTAGE)
 	{ VERIFY_FILE, ROMSTAGE, { { NULL, CBFS_TYPE_STAGE } },
 		HASH_IDX_ROM_STAGE, MBOOT_PCR_INDEX_0 },
+#endif
 	{ VERIFY_FILE, BOOTBLOCK, { { NULL, CBFS_TYPE_BOOTBLOCK } },
 		HASH_IDX_BOOTBLOCK, MBOOT_PCR_INDEX_0 },
 	{ VERIFY_FILE, FSP, { { NULL, CBFS_TYPE_FSP } }, HASH_IDX_FSP,
diff --git a/src/mainboard/google/butterfly/chromeos.c b/src/mainboard/google/butterfly/chromeos.c
index 4dcf14f..595b2ca 100644
--- a/src/mainboard/google/butterfly/chromeos.c
+++ b/src/mainboard/google/butterfly/chromeos.c
@@ -39,7 +39,7 @@
 	return (ec_mem_read(EC_HW_GPI_STATUS) >> EC_GPI_LID_STAT_BIT) & 1;
 }
 
-/* FIXME: VBOOT reads this in ENV_ROMSTAGE. */
+/* FIXME: VBOOT reads this in ENV_SEPARATE_ROMSTAGE. */
 int get_recovery_mode_switch(void)
 {
 	if (ENV_RAMSTAGE)
diff --git a/src/mainboard/google/daisy/romstage.c b/src/mainboard/google/daisy/romstage.c
index d9ae00c..4fa2b3b 100644
--- a/src/mainboard/google/daisy/romstage.c
+++ b/src/mainboard/google/daisy/romstage.c
@@ -118,6 +118,7 @@
 	return mem;
 }
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_init(timestamp_get());
@@ -126,10 +127,11 @@
 	/*
 	 * From the clocks comment below it looks like serial console won't
 	 * work in the bootblock so keep in the romstage_main flow even with
-	 * !CONFIG  SEPARATE_ROMSTAGE.
+	 * !CONFIG(SEPARATE_ROMSTAGE).
 	 */
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/google/peach_pit/romstage.c b/src/mainboard/google/peach_pit/romstage.c
index 5a4863d..e38d1cb 100644
--- a/src/mainboard/google/peach_pit/romstage.c
+++ b/src/mainboard/google/peach_pit/romstage.c
@@ -202,6 +202,7 @@
 #define simple_spi_test()
 #endif
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_init(timestamp_get());
@@ -210,10 +211,11 @@
 	/*
 	 * From the clocks comment below it looks like serial console won't
 	 * work in the bootblock so keep in the romstage_main flow even with
-	 * !CONFIG SEPARATE_ROMSTAGE.
+	 * !CONFIG(SEPARATE_ROMSTAGE).
 	 */
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/google/veyron/romstage.c b/src/mainboard/google/veyron/romstage.c
index 488f581..7471434 100644
--- a/src/mainboard/google/veyron/romstage.c
+++ b/src/mainboard/google/veyron/romstage.c
@@ -63,6 +63,7 @@
 	rk808_configure_ldo(5, 0); /* VCC33_SD */
 }
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_add_now(TS_ROMSTAGE_START);
@@ -72,6 +73,7 @@
 
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/google/veyron_mickey/romstage.c b/src/mainboard/google/veyron_mickey/romstage.c
index c718ed7..d202a02 100644
--- a/src/mainboard/google/veyron_mickey/romstage.c
+++ b/src/mainboard/google/veyron_mickey/romstage.c
@@ -57,6 +57,7 @@
 	write_l2ctlr(l2ctlr);
 }
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_add_now(TS_ROMSTAGE_START);
@@ -66,6 +67,7 @@
 
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/google/veyron_rialto/romstage.c b/src/mainboard/google/veyron_rialto/romstage.c
index 488f581..7471434 100644
--- a/src/mainboard/google/veyron_rialto/romstage.c
+++ b/src/mainboard/google/veyron_rialto/romstage.c
@@ -63,6 +63,7 @@
 	rk808_configure_ldo(5, 0); /* VCC33_SD */
 }
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	timestamp_add_now(TS_ROMSTAGE_START);
@@ -72,6 +73,7 @@
 
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/mainboard/ti/beaglebone/romstage.c b/src/mainboard/ti/beaglebone/romstage.c
index da6a182..09fd2a3 100644
--- a/src/mainboard/ti/beaglebone/romstage.c
+++ b/src/mainboard/ti/beaglebone/romstage.c
@@ -45,12 +45,14 @@
 	.emif_ddr_phy_ctlr_1 = MT41K256M16HA125E_EMIF_READ_LATENCY,
 };
 
+#if CONFIG(SEPARATE_ROMSTAGE)
 void main(void)
 {
 	console_init();
 	printk(BIOS_INFO, "Hello from romstage.\n");
 	romstage_main();
 }
+#endif
 
 void __noreturn romstage_main(void)
 {
diff --git a/src/security/vboot/Kconfig b/src/security/vboot/Kconfig
index a2af5d2..2d5b20f 100644
--- a/src/security/vboot/Kconfig
+++ b/src/security/vboot/Kconfig
@@ -90,6 +90,7 @@
 config VBOOT_STARTS_IN_BOOTBLOCK
 	bool
 	default n
+	depends on SEPARATE_ROMSTAGE
 	help
 	  Firmware verification happens during the end of or right after the
 	  bootblock. This implies that a static VBOOT2_WORK() buffer must be
diff --git a/src/security/vboot/Makefile.inc b/src/security/vboot/Makefile.inc
index f152444..1b3568a 100644
--- a/src/security/vboot/Makefile.inc
+++ b/src/security/vboot/Makefile.inc
@@ -43,7 +43,9 @@
 endef # vboot-for-stage
 
 $(eval $(call vboot-for-stage,bootblock))
+ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y)
 $(eval $(call vboot-for-stage,romstage))
+endif
 $(eval $(call vboot-for-stage,ramstage))
 $(eval $(call vboot-for-stage,postcar))
 
@@ -157,7 +159,11 @@
 ifeq ($(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK),y)
 postinclude-hooks += $$(eval bootblock-srcs += $$(verstage-srcs))
 else
+ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y)
 postinclude-hooks += $$(eval romstage-srcs += $$(verstage-srcs))
+else
+postinclude-hooks += $$(eval bootblock-srcs += $$(verstage-srcs))
+endif
 endif
 endif # CONFIG_VBOOT_SEPARATE_VERSTAGE
 
diff --git a/src/security/vboot/misc.h b/src/security/vboot/misc.h
index 8310647..a7069f3 100644
--- a/src/security/vboot/misc.h
+++ b/src/security/vboot/misc.h
@@ -48,7 +48,7 @@
 	if (CONFIG(VBOOT_SEPARATE_VERSTAGE))
 		return ENV_SEPARATE_VERSTAGE;
 	else if (CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
-		return ENV_ROMSTAGE;
+		return ENV_RAMINIT;
 	else if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
 		return ENV_BOOTBLOCK;
 	else
diff --git a/src/security/vboot/vboot_common.c b/src/security/vboot/vboot_common.c
index f9080c5..68df140 100644
--- a/src/security/vboot/vboot_common.c
+++ b/src/security/vboot/vboot_common.c
@@ -29,7 +29,7 @@
 
 void vboot_save_data(struct vb2_context *ctx)
 {
-	if (!verification_should_run() && !(ENV_ROMSTAGE && CONFIG(VBOOT_EARLY_EC_SYNC))) {
+	if (!verification_should_run() && !(ENV_RAMINIT && CONFIG(VBOOT_EARLY_EC_SYNC))) {
 		if (ctx->flags
 		    & (VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED
 		       | VB2_CONTEXT_SECDATA_KERNEL_CHANGED))
diff --git a/src/soc/intel/common/block/systemagent/memmap.c b/src/soc/intel/common/block/systemagent/memmap.c
index e82c696..04ab735 100644
--- a/src/soc/intel/common/block/systemagent/memmap.c
+++ b/src/soc/intel/common/block/systemagent/memmap.c
@@ -74,7 +74,7 @@
 	 * Store the top_of_ram (ramtop) into the CMOS if SOC_INTEL_COMMON_BASECODE_RAMTOP
 	 * config is enabled.
 	 */
-	if (ENV_ROMSTAGE && CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
+	if (ENV_CREATES_CBMEM && CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
 		update_ramtop(top_of_ram);
 
 	postcar_frame_add_mtrr(pcf, top_of_ram - 16 * MiB, 16 * MiB, MTRR_TYPE_WRBACK);
diff --git a/src/vendorcode/eltan/security/verified_boot/vboot_check.c b/src/vendorcode/eltan/security/verified_boot/vboot_check.c
index 9d61006..62c60f7 100644
--- a/src/vendorcode/eltan/security/verified_boot/vboot_check.c
+++ b/src/vendorcode/eltan/security/verified_boot/vboot_check.c
@@ -363,7 +363,7 @@
 	if (ENV_BOOTBLOCK)
 		verified_boot_bootblock_check();
 
-	if (ENV_ROMSTAGE) {
+	if (ENV_RAMINIT) {
 		if (!initialized && ((prog->type == PROG_REFCODE) ||
 		    (prog->type == PROG_POSTCAR))) {
 			verified_boot_early_check();
diff --git a/tests/lib/imd_cbmem-test.c b/tests/lib/imd_cbmem-test.c
index e345c84..6c0ba5a 100644
--- a/tests/lib/imd_cbmem-test.c
+++ b/tests/lib/imd_cbmem-test.c
@@ -81,7 +81,7 @@
 {
 	cbmem_top_init_once();
 
-	if (ENV_ROMSTAGE)
+	if (ENV_CREATES_CBMEM)
 		assert_ptr_equal(cbmem_top_chipset(), cbmem_top());
 
 	if (ENV_POSTCAR || ENV_RAMSTAGE)