[coreboot-gerrit] Patch set updated for coreboot: 581229b cbfs: simplify program loading

Aaron Durbin (adurbin@chromium.org) gerrit at coreboot.org
Wed May 20 01:02:31 CEST 2015


Aaron Durbin (adurbin at chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10257

-gerrit

commit 581229b2071142e544eba7aa89209056495af05a
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Tue May 19 08:37:23 2015 -0500

    cbfs: simplify program loading
    
    By first removing struct cbfsf and using region_devices
    throughout program loading we can significantly decrease
    the complexity. This patch is mainly for review purposes
    as it can squashed into the new cbfs API patch.
    
    Change-Id: I13fcd0fa548375463eb06cbce0ec85ebd8e41d8f
    Signed-off-by: Aaron Durbin <adurbin at chromium.org>
---
 src/arch/arm/armv7/bootblock_simple.c              |   3 -
 src/device/oprom/realmode/x86.c                    |   7 +-
 src/include/cbfs.h                                 |  44 +---
 src/include/fallback.h                             |   2 +-
 src/include/memlayout.h                            |   2 -
 src/include/program_loading.h                      |  21 +-
 src/include/rmodule.h                              |   4 +-
 src/include/romstage_handoff.h                     |  10 -
 src/include/symbols.h                              |   1 -
 src/lib/Makefile.inc                               |   4 +
 src/lib/cbfs.c                                     |  73 ++----
 src/lib/cbfs_boot_props.c                          |   8 +-
 src/lib/hardwaremain.c                             |   2 -
 src/lib/loaders/Makefile.inc                       |  27 ---
 src/lib/loaders/cbfs_payload_loader.c              |  42 ----
 src/lib/loaders/cbfs_ramstage_loader.c             |  55 -----
 src/lib/loaders/cbfs_romstage_loader.c             |  31 ---
 src/lib/loaders/load_and_run_payload.c             | 118 ----------
 src/lib/loaders/load_and_run_ramstage.c            | 109 ---------
 src/lib/loaders/load_and_run_romstage.c            |  85 -------
 src/lib/prog_loaders.c                             | 247 +++++++++++++++++++++
 src/lib/rmodule.c                                  |  26 +--
 src/lib/selfboot.c                                 |  15 +-
 src/soc/broadcom/cygnus/include/soc/memlayout.ld   |   1 -
 src/soc/intel/baytrail/refcode.c                   |  76 +------
 src/soc/intel/broadwell/refcode.c                  |  78 +------
 src/soc/nvidia/tegra132/ccplex.c                   |   8 +-
 src/soc/qualcomm/ipq806x/include/soc/memlayout.ld  |   3 +-
 .../google/chromeos/vboot2/vboot_loader.c          | 134 +++++------
 29 files changed, 402 insertions(+), 834 deletions(-)

diff --git a/src/arch/arm/armv7/bootblock_simple.c b/src/arch/arm/armv7/bootblock_simple.c
index ff15e24..b010693 100644
--- a/src/arch/arm/armv7/bootblock_simple.c
+++ b/src/arch/arm/armv7/bootblock_simple.c
@@ -22,7 +22,6 @@
 #include <arch/exception.h>
 #include <arch/stages.h>
 #include <bootblock_common.h>
-#include <cbfs.h>
 #include <console/console.h>
 #include <delay.h>
 #include <program_loading.h>
@@ -46,8 +45,6 @@ void main(void)
 	exception_init();
 #endif
 
-	cbfs_set_header_offset(0);
-
 	bootblock_soc_init();
 	bootblock_mainboard_init();
 
diff --git a/src/device/oprom/realmode/x86.c b/src/device/oprom/realmode/x86.c
index 7f8c166..db22ccc 100644
--- a/src/device/oprom/realmode/x86.c
+++ b/src/device/oprom/realmode/x86.c
@@ -378,9 +378,6 @@ static u32 VSA_vrRead(u16 classIndex)
 
 void do_vsmbios(void)
 {
-	struct prog vsa = {
-		.name = "vsa",
-	};
 	printk(BIOS_DEBUG, "Preparing for VSA...\n");
 
 	/* Set up C interrupt handlers */
@@ -392,8 +389,8 @@ void do_vsmbios(void)
 	/* Make sure the code is placed. */
 	setup_realmode_code();
 
-	if (cbfs_boot_load_stage(&vsa) ||
-	    (uintptr_t)prog_entry(&vsa) != VSA2_ENTRY_POINT) {
+	if ((uintptr_t)cbfs_boot_load_stage_by_name("vsa") !=
+	    VSA2_ENTRY_POINT) {
 		printk(BIOS_ERR, "Failed to load VSA.\n");
 		return;
 	}
diff --git a/src/include/cbfs.h b/src/include/cbfs.h
index f914ffb..00c43f2 100644
--- a/src/include/cbfs.h
+++ b/src/include/cbfs.h
@@ -28,13 +28,10 @@
  * CBFS operations consist of the following concepts:
  * - region_device for the boot media
  * - cbfsd which is a descriptor for representing a cbfs instance
- * - cbfsf which is a file handle for manipulating cbfs files
  */
 
 /* Descriptor for cbfs lookup operations. */
 struct cbfsd;
-/* File handle representing a cbfs file. */
-struct cbfsf;
 
 /***********************************************
  * Perform CBFS operations on the boot device. *
@@ -42,40 +39,24 @@ struct cbfsf;
 
 /* Return mapping of option rom found in boot device. NULL on error. */
 void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device);
-/* Load stage into memory filling in prog. Return 0 on success. < 0 on error. */
-int cbfs_boot_load_stage(struct prog *prog);
+/* Load stage by name into memory. Returns entry address on success. NULL on
+ * failure. */
+void *cbfs_boot_load_stage_by_name(const char *name);
 /* Locate file by name and optional type. Return 0 on success. < 0 on error. */
-int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type);
+int cbfs_boot_locate(struct region_device *fh, const char *name,
+			uint32_t *type);
 /* Map file into memory leaking the mapping. Only should be used when
  * leaking mappings are a no-op. Returns NULL on error, else returns
  * the mapping and sets the size of the file. */
 void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size);
 
-/*************************************************
- * Perform CBFS operations on the provided file. *
- *************************************************/
-
-/* Map file into memory. Return NULL on error. */
-void *cbfs_file_mmap(const struct cbfsf *fh, size_t offset, size_t size);
-/* Unmap previously mapped file. Returns 0 on success. < 0 on error. */
-int cbfs_file_munmap(const struct cbfsf *fh, void *mapping);
-/* Read size bytes into b at offset in file. Returns the number of bytes
- * read on success. Returns < 0 on error. */
-ssize_t cbfs_file_readat(const struct cbfsf *fh, void *b, size_t offset,
-				size_t size);
 /* Load stage into memory filling in prog. Return 0 on success. < 0 on error. */
-int cbfs_file_stage_load(const struct cbfsf *fh, struct prog *prog);
-
-/*******************************************************
- * Perform CBFS operations on the provided descriptor. *
- *******************************************************/
+int cbfs_prog_stage_load(struct prog *prog);
 
 /* Locate file by name and optional type. Returns 0 on succcess else < 0 on
  * error.*/
-int cbfs_locate(struct cbfsf *fh, const struct cbfsd *cbfs, const char *name,
-		uint32_t *type);
-void cbfs_set_header_offset(size_t offset);
-size_t cbfs_get_header_offset(void);
+int cbfs_locate(struct region_device *fh, const struct cbfsd *cbfs,
+		const char *name, uint32_t *type);
 
 /*****************************************************************
  * Support structures and functions. Direct field access should  *
@@ -88,15 +69,6 @@ struct cbfsd {
 	size_t align;
 };
 
-struct cbfsf {
-	struct region_device rdev;
-};
-
-static inline size_t cbfs_file_size(const struct cbfsf *fh)
-{
-	return region_sz(&fh->rdev.region);
-}
-
 /* The cbfs_props struct describes the properties associated with a CBFS. */
 struct cbfs_props {
 	/* Each file is aligned. */
diff --git a/src/include/fallback.h b/src/include/fallback.h
index 7f3c823..740f130 100644
--- a/src/include/fallback.h
+++ b/src/include/fallback.h
@@ -1,7 +1,7 @@
 #ifndef FALLBACK_H
 #define FALLBACK_H
 
-#if !defined(__ASSEMBLER__) && !defined(__PRE_RAM__)
+#if !defined(__ASSEMBLER__)
 
 void boot_successful(void);
 
diff --git a/src/include/memlayout.h b/src/include/memlayout.h
index 263dc5c..a80e03e 100644
--- a/src/include/memlayout.h
+++ b/src/include/memlayout.h
@@ -102,6 +102,4 @@
 		. += sz;
 #endif
 
-#define CBFS_HEADER_OFFSET(addr) REGION(cbfs_header_offset, addr, 4, 4)
-
 #endif /* __MEMLAYOUT_H */
diff --git a/src/include/program_loading.h b/src/include/program_loading.h
index 5fc0fd6..05f5e3e 100644
--- a/src/include/program_loading.h
+++ b/src/include/program_loading.h
@@ -22,6 +22,7 @@
 
 #include <stdint.h>
 #include <stddef.h>
+#include <region.h>
 
 enum {
 	/* Last segment of program. Can be used to take different actions for
@@ -42,6 +43,7 @@ enum prog_type {
 	PROG_VERSTAGE,
 	PROG_ROMSTAGE,
 	PROG_RAMSTAGE,
+	PROG_REFCODE,
 	PROG_PAYLOAD,
 };
 
@@ -49,10 +51,13 @@ enum prog_type {
 struct prog {
 	enum prog_type type;
 	const char *name;
+	/* Source of program content to load. */
+	struct region_device rdev;
 	/* The area can mean different things depending on what type the
-	 * program is. e.g. a payload prog uses this field for the backing
-	 * store of the payload_segments and data. After loading the segments
-	 * area is updated to reflect the bounce buffer used. */
+	 * program is. A stage after being loaded reflects the memory occupied
+	 * by the program, Since payloads are multi-segment one can't express
+	 * the memory layout with one range. Instead this field is updated
+	 * to reflect the bounce buffer used. */
 	struct buffer_area area;
 	/* Entry to program with optional argument. It's up to the architecture
 	 * to decide if argument is passed. */
@@ -92,6 +97,8 @@ static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
 	prog->arg = arg;
 }
 
+/* Locate the identified program to run. Return 0 on success. < 0 on error. */
+int prog_locate(struct prog *prog);
 /* Run the program described by prog. */
 void prog_run(struct prog *prog);
 /* Per architecture implementation running a program. */
@@ -107,10 +114,10 @@ struct prog_loader_ops {
 	/* Determine if the loader is the active one. If so returns 1 else 0
 	 * or < 0 on error. */
 	int (*is_loader_active)(struct prog *prog);
-	/* Returns < 0 on error or 0 on success. This function needs to do
-	 * different things depending on the prog type. See definition
-	 * of struct prog above. */
-	int (*prepare)(struct prog *prog);
+	/* Returns < 0 on error or 0 on success. This function locates
+	 * the rdev representing the file data associated with the passed in
+	 * prog. */
+	int (*locate)(struct prog *prog);
 };
 
 /************************
diff --git a/src/include/rmodule.h b/src/include/rmodule.h
index 5dc795c..7afc3a2 100644
--- a/src/include/rmodule.h
+++ b/src/include/rmodule.h
@@ -54,7 +54,6 @@ int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
 /* Support for loading rmodule stages. This API is only available when
  * using dynamic cbmem because it uses the dynamic cbmem API to obtain
  * the backing store region for the stage. */
-struct cbfsf;
 struct prog;
 
 struct rmod_stage_load {
@@ -63,8 +62,7 @@ struct rmod_stage_load {
 };
 
 /* Both of the following functions return 0 on success, -1 on error. */
-int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh);
-int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl);
+int rmodule_stage_load(struct rmod_stage_load *rsl);
 
 struct rmodule {
 	void *location;
diff --git a/src/include/romstage_handoff.h b/src/include/romstage_handoff.h
index 09934ca..b78bec3 100644
--- a/src/include/romstage_handoff.h
+++ b/src/include/romstage_handoff.h
@@ -38,8 +38,6 @@ struct romstage_handoff {
 	uint8_t reserved[2];
 };
 
-#if defined(__ROMSTAGE__)
-#if CONFIG_EARLY_CBMEM_INIT
 /* The romstage_handoff_find_or_add() function provides the necessary logic
  * for initializing the romstage_handoff structure in cbmem. Different components
  * of the romstage may be responsible for setting up different fields. Therefore
@@ -62,14 +60,6 @@ static inline struct romstage_handoff *romstage_handoff_find_or_add(void)
 
 	return handoff;
 }
-#else /* CONFIG_EARLY_CBMEM_INIT */
-static inline struct romstage_handoff *romstage_handoff_find_or_add(void)
-{
-	return NULL;
-}
-#endif /* CONFIG_EARLY_CBMEM_INIT */
-
-#endif /* defined(__PRE_RAM__) */
 
 #endif /* ROMSTAGE_HANDOFF_H */
 
diff --git a/src/include/symbols.h b/src/include/symbols.h
index 279c5a2..c3ced87 100644
--- a/src/include/symbols.h
+++ b/src/include/symbols.h
@@ -27,7 +27,6 @@ extern u8 _esram[];
 #define _sram_size (_esram - _sram)
 
 extern u8 _dram[];
-extern u32 _cbfs_header_offset[];
 
 extern u8 _preram_cbmem_console[];
 extern u8 _epreram_cbmem_console[];
diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc
index 1160b28..2416736 100644
--- a/src/lib/Makefile.inc
+++ b/src/lib/Makefile.inc
@@ -18,6 +18,7 @@
 #
 subdirs-y += loaders
 
+bootblock-y += prog_loaders.c
 bootblock-y += prog_ops.c
 bootblock-y += cbfs.c
 bootblock-y += cbfs_boot_props.c
@@ -35,6 +36,7 @@ bootblock-y += region.c
 bootblock-y += boot_device.c
 bootblock-y += fmap.c
 
+verstage-y += prog_loaders.c
 verstage-y += prog_ops.c
 verstage-y += delay.c
 verstage-y += cbfs.c
@@ -58,6 +60,7 @@ verstage-$(CONFIG_GENERIC_UDELAY) += timer.c
 verstage-$(CONFIG_GENERIC_GPIO_LIB) += gpio.c
 verstage-y += mem_pool.c
 
+romstage-y += prog_loaders.c
 romstage-y += prog_ops.c
 romstage-y += memchr.c
 romstage-y += memcmp.c
@@ -89,6 +92,7 @@ endif
 
 romstage-$(CONFIG_GENERIC_UDELAY) += timer.c
 
+ramstage-y += prog_loaders.c
 ramstage-y += prog_ops.c
 ramstage-y += hardwaremain.c
 ramstage-y += selfboot.c
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index fb012d1..23dfee7 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -35,22 +35,7 @@
 #define DEBUG(x...)
 #endif
 
-void cbfs_set_header_offset(size_t offset)
-{
-	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)) {
-		_cbfs_header_offset[0] = offset;
-		LOG("header set to: %#zx\n", offset);
-	}
-}
-
-size_t cbfs_get_header_offset(void)
-{
-	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES))
-		return _cbfs_header_offset[0];
-	return 0;
-}
-
-int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
+int cbfs_boot_locate(struct region_device *fh, const char *name, uint32_t *type)
 {
 	struct cbfsd cbfs;
 	struct region_device rdev;
@@ -79,38 +64,22 @@ int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
 
 void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
 {
-	struct cbfsf fh;
+	struct region_device fh;
 	size_t fsize;
 
 	if (cbfs_boot_locate(&fh, name, &type))
 		return NULL;
 
-	fsize = cbfs_file_size(&fh);
+	fsize = region_device_sz(&fh);
 
 	if (size != NULL)
 		*size = fsize;
 
-	return cbfs_file_mmap(&fh, 0, fsize);
-}
-
-void *cbfs_file_mmap(const struct cbfsf *fh, size_t offset, size_t size)
-{
-	return rdev_mmap(&fh->rdev, offset, size);
+	return rdev_mmap(&fh, 0, fsize);
 }
 
-int cbfs_file_munmap(const struct cbfsf *fh, void *mapping)
-{
-	return rdev_munmap(&fh->rdev, mapping);
-}
-
-ssize_t cbfs_file_readat(const struct cbfsf *fh, void *b, size_t offset,
-				size_t size)
-{
-	return rdev_readat(&fh->rdev, b, offset, size);
-}
-
-int cbfs_locate(struct cbfsf *fh, const struct cbfsd *cbfs, const char *name,
-		uint32_t *type)
+int cbfs_locate(struct region_device *fh, const struct cbfsd *cbfs,
+		const char *name, uint32_t *type)
 {
 	size_t offset;
 	const struct region_device *rd;
@@ -174,7 +143,7 @@ int cbfs_locate(struct cbfsf *fh, const struct cbfsd *cbfs, const char *name,
 		 * represent the cbfs file. */
 		offset += file.offset;
 		datasz = file.len;
-		if (rdev_chain(&fh->rdev, rd, offset, datasz))
+		if (rdev_chain(fh, rd, offset, datasz))
 			break;
 
 		/* Success. */
@@ -217,29 +186,35 @@ void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
 	return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
 }
 
-int cbfs_boot_load_stage(struct prog *stage)
+void *cbfs_boot_load_stage_by_name(const char *name)
 {
-	struct cbfsf fh;
+	struct prog stage = {
+		.name = name,
+	};
 	uint32_t type = CBFS_TYPE_STAGE;
 
-	if (cbfs_boot_locate(&fh, stage->name, &type))
-		return -1;
+	if (cbfs_boot_locate(&stage.rdev, name, &type))
+		return NULL;
+
+	if (cbfs_prog_stage_load(&stage))
+		return NULL;
 
-	return cbfs_file_stage_load(&fh, stage);
+	return prog_entry(&stage);
 }
 
-int cbfs_file_stage_load(const struct cbfsf *fh, struct prog *pstage)
+int cbfs_prog_stage_load(struct prog *pstage)
 {
 	struct cbfs_stage stage;
 	uint8_t *load;
 	void *entry;
 	size_t fsize;
 	size_t foffset;
+	const struct region_device *fh = &pstage->rdev;
 
-	if (cbfs_file_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
+	if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
 		return 0;
 
-	fsize = cbfs_file_size(fh);
+	fsize = region_device_sz(fh);
 	fsize -= sizeof(stage);
 	foffset = 0;
 	foffset += sizeof(stage);
@@ -252,17 +227,17 @@ int cbfs_file_stage_load(const struct cbfsf *fh, struct prog *pstage)
 	entry = (void *)(uintptr_t)stage.entry;
 
 	if (stage.compression == CBFS_COMPRESS_NONE) {
-		if (cbfs_file_readat(fh, load, foffset, fsize) != fsize)
+		if (rdev_readat(fh, load, foffset, fsize) != fsize)
 			return -1;
 	} else if (stage.compression == CBFS_COMPRESS_LZMA) {
-		void *map = cbfs_file_mmap(fh, foffset, fsize);
+		void *map = rdev_mmap(fh, foffset, fsize);
 
 		if (map == NULL)
 			return -1;
 
 		fsize = inflate(map, load);
 
-		cbfs_file_munmap(fh, map);
+		rdev_munmap(fh, map);
 
 		if (!fsize)
 			return -1;
diff --git a/src/lib/cbfs_boot_props.c b/src/lib/cbfs_boot_props.c
index 7e7bb8a..21e64d3 100644
--- a/src/lib/cbfs_boot_props.c
+++ b/src/lib/cbfs_boot_props.c
@@ -31,23 +31,19 @@ int __attribute__((weak)) cbfs_boot_region_properties(struct cbfs_props *props)
 	const struct region_device *bdev;
 	int32_t rel_offset;
 	size_t offset;
-	size_t base_offset;
 
 	bdev = boot_device_ro();
 
 	if (bdev == NULL)
 		return -1;
 
-	base_offset = cbfs_get_header_offset();
-	base_offset += CONFIG_CBFS_SIZE;
-
 	/* Find location of header using signed 32-bit offset from
 	 * end of CBFS region. */
-	offset = base_offset - sizeof(int32_t);
+	offset = CONFIG_CBFS_SIZE - sizeof(int32_t);
 	if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0)
 		return -1;
 
-	offset = base_offset + rel_offset;
+	offset = CONFIG_CBFS_SIZE + rel_offset;
 	if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0)
 		return -1;
 
diff --git a/src/lib/hardwaremain.c b/src/lib/hardwaremain.c
index 295edcd..274a9c7 100644
--- a/src/lib/hardwaremain.c
+++ b/src/lib/hardwaremain.c
@@ -228,8 +228,6 @@ static boot_state_t bs_write_tables(void *arg)
 
 static boot_state_t bs_payload_load(void *arg)
 {
-	timestamp_add_now(TS_LOAD_PAYLOAD);
-
 	payload_load();
 
 	return BS_PAYLOAD_BOOT;
diff --git a/src/lib/loaders/Makefile.inc b/src/lib/loaders/Makefile.inc
deleted file mode 100644
index 9dca06b..0000000
--- a/src/lib/loaders/Makefile.inc
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-# This file is part of the coreboot project.
-#
-# Copyright (C) 2014 Google Inc.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; version 2 of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-#
-
-bootblock-y += load_and_run_romstage.c
-bootblock-y += cbfs_romstage_loader.c
-romstage-y += cbfs_ramstage_loader.c
-romstage-y += load_and_run_ramstage.c
-ramstage-y += cbfs_payload_loader.c
-ramstage-y += load_and_run_payload.c
-verstage-y += cbfs_romstage_loader.c
-verstage-y += load_and_run_romstage.c
diff --git a/src/lib/loaders/cbfs_payload_loader.c b/src/lib/loaders/cbfs_payload_loader.c
deleted file mode 100644
index fffb558..0000000
--- a/src/lib/loaders/cbfs_payload_loader.c
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2014 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <cbfs.h>
-#include <program_loading.h>
-
-static int cbfs_locate_payload(struct prog *payload)
-{
-	void *buffer;
-	size_t size;
-	const uint32_t type = CBFS_TYPE_PAYLOAD;
-
-	buffer = cbfs_boot_map_with_leak(payload->name, type, &size);
-
-	if (buffer == NULL)
-		return -1;
-
-	prog_set_area(payload, buffer, size);
-
-	return 0;
-}
-
-const struct prog_loader_ops cbfs_payload_loader = {
-	.name = "CBFS",
-	.prepare = cbfs_locate_payload,
-};
diff --git a/src/lib/loaders/cbfs_ramstage_loader.c b/src/lib/loaders/cbfs_ramstage_loader.c
deleted file mode 100644
index bbbf695..0000000
--- a/src/lib/loaders/cbfs_ramstage_loader.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2008-2009 coresystems GmbH
- * Copyright (C) 2014 Google Inc.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-#include <console/console.h>
-#include <cbfs.h>
-#include <program_loading.h>
-
-#if CONFIG_RELOCATABLE_RAMSTAGE
-#include <rmodule.h>
-#include <cbmem.h>
-
-static int cbfs_load_ramstage(struct prog *ramstage)
-{
-	struct rmod_stage_load rmod_ram = {
-		.cbmem_id = CBMEM_ID_RAMSTAGE,
-		.prog = ramstage,
-	};
-
-	if (rmodule_stage_load_from_cbfs(&rmod_ram)) {
-		printk(BIOS_DEBUG, "Could not load ramstage.\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-#else /* CONFIG_RELOCATABLE_RAMSTAGE */
-
-static int cbfs_load_ramstage(struct prog *ramstage)
-{
-	return cbfs_boot_load_stage(ramstage);
-}
-
-#endif /* CONFIG_RELOCATABLE_RAMSTAGE */
-
-const struct prog_loader_ops cbfs_ramstage_loader = {
-	.name = "CBFS",
-	.prepare = cbfs_load_ramstage,
-};
diff --git a/src/lib/loaders/cbfs_romstage_loader.c b/src/lib/loaders/cbfs_romstage_loader.c
deleted file mode 100644
index 7dde2ec..0000000
--- a/src/lib/loaders/cbfs_romstage_loader.c
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-
-#include <cbfs.h>
-#include <program_loading.h>
-
-static int cbfs_load_romstage(struct prog *romstage)
-{
-	return cbfs_boot_load_stage(romstage);
-}
-
-const struct prog_loader_ops cbfs_romstage_loader = {
-	.name = "CBFS",
-	.prepare = cbfs_load_romstage,
-};
diff --git a/src/lib/loaders/load_and_run_payload.c b/src/lib/loaders/load_and_run_payload.c
deleted file mode 100644
index 4928e0c..0000000
--- a/src/lib/loaders/load_and_run_payload.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2014 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdint.h>
-#include <stdlib.h>
-#include <cbmem.h>
-#include <console/console.h>
-#include <fallback.h>
-#include <lib.h>
-#include <program_loading.h>
-#include <symbols.h>
-#include <timestamp.h>
-
-extern const struct prog_loader_ops vboot_loader;
-extern const struct prog_loader_ops cbfs_payload_loader;
-
-static const struct prog_loader_ops *payload_ops[] = {
-#if CONFIG_VBOOT_VERIFY_FIRMWARE
-	&vboot_loader,
-#endif
-	&cbfs_payload_loader,
-};
-
-static struct prog global_payload = {
-	.name = CONFIG_CBFS_PREFIX "/payload",
-	.type = PROG_PAYLOAD,
-};
-
-void __attribute__((weak)) mirror_payload(struct prog *payload)
-{
-	return;
-}
-
-void payload_load(void)
-{
-	int i;
-	const struct prog_loader_ops *ops;
-	struct prog *payload = &global_payload;
-
-	for (i = 0; i < ARRAY_SIZE(payload_ops); i++) {
-		/* Default loader state is active. */
-		int ret = 1;
-
-		ops = payload_ops[i];
-
-		if (ops->is_loader_active != NULL)
-			ret = ops->is_loader_active(payload);
-
-		if (ret == 0) {
-			printk(BIOS_DEBUG, "%s payload loader inactive.\n",
-				ops->name);
-			continue;
-		} else if (ret < 0) {
-			printk(BIOS_DEBUG, "%s payload loader failure.\n",
-				ops->name);
-			continue;
-		}
-
-		if (ops->prepare(payload) < 0) {
-			printk(BIOS_DEBUG, "%s: could not locate payload.\n",
-				ops->name);
-			continue;
-		}
-		printk(BIOS_DEBUG, "%s: located payload @ %p, %zu bytes.\n",
-			ops->name, prog_start(payload), prog_size(payload));
-		break;
-	}
-
-	if (i == ARRAY_SIZE(payload_ops))
-		goto out;
-
-	mirror_payload(payload);
-
-	/* Pass cbtables to payload if architecture desires it. */
-	prog_set_entry(payload, selfload(payload),
-			cbmem_find(CBMEM_ID_CBTABLE));
-
-out:
-	if (prog_entry(payload) == NULL)
-		die("Payload not loaded.\n");
-}
-
-void payload_run(void)
-{
-	struct prog *payload = &global_payload;
-
-	/* Reset to booting from this image as late as possible */
-	boot_successful();
-
-	printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
-		prog_entry(payload), prog_entry_arg(payload));
-	post_code(POST_ENTER_ELF_BOOT);
-
-	timestamp_add_now(TS_SELFBOOT_JUMP);
-
-	/* Before we go off to run the payload, see if
-	 * we stayed within our bounds.
-	 */
-	checkstack(_estack, 0);
-
-	prog_run(payload);
-}
diff --git a/src/lib/loaders/load_and_run_ramstage.c b/src/lib/loaders/load_and_run_ramstage.c
deleted file mode 100644
index 504dbff..0000000
--- a/src/lib/loaders/load_and_run_ramstage.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2014 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdlib.h>
-#include <console/console.h>
-#include <arch/stages.h>
-#include <cbfs.h>
-#include <program_loading.h>
-#include <romstage_handoff.h>
-#include <stage_cache.h>
-#include <timestamp.h>
-
-extern const struct prog_loader_ops cbfs_ramstage_loader;
-extern const struct prog_loader_ops vboot_loader;
-
-static const struct prog_loader_ops *loaders[] = {
-#if CONFIG_VBOOT_VERIFY_FIRMWARE
-	&vboot_loader,
-#endif
-	&cbfs_ramstage_loader,
-};
-
-void __attribute__((weak)) stage_cache_add(int stage_id, struct prog *stage) {}
-void __attribute__((weak)) stage_cache_load_stage(int stage_id,
-							struct prog *stage) {}
-void __attribute__((weak)) ramstage_cache_invalid(void) {}
-
-static void load_ramstage(const struct prog_loader_ops *ops,
-				struct prog *ramstage)
-{
-	timestamp_add_now(TS_START_COPYRAM);
-
-	if (ops->prepare(ramstage))
-		return;
-
-	stage_cache_add(STAGE_RAMSTAGE, ramstage);
-
-	timestamp_add_now(TS_END_COPYRAM);
-
-	prog_run(ramstage);
-}
-
-static void run_ramstage_from_resume(struct romstage_handoff *handoff,
-					struct prog *ramstage)
-{
-	if (handoff != NULL && handoff->s3_resume) {
-		/* Load the cached ramstage to runtime location. */
-		stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
-
-		if (prog_entry(ramstage) != NULL) {
-			printk(BIOS_DEBUG, "Jumping to image.\n");
-			prog_run(ramstage);
-		}
-		ramstage_cache_invalid();
-	}
-}
-
-void run_ramstage(void)
-{
-	const struct prog_loader_ops *ops;
-	int i;
-	struct prog ramstage = {
-		.name = CONFIG_CBFS_PREFIX "/ramstage",
-		.type = PROG_RAMSTAGE,
-	};
-
-	run_ramstage_from_resume(romstage_handoff_find_or_add(), &ramstage);
-
-	for (i = 0; i < ARRAY_SIZE(loaders); i++) {
-		/* Default loader state is active. */
-		int ret = 1;
-
-		ops = loaders[i];
-
-		if (ops->is_loader_active != NULL)
-			ret = ops->is_loader_active(&ramstage);
-
-		if (ret == 0) {
-			printk(BIOS_DEBUG, "%s ramstage loader inactive.\n",
-				ops->name);
-			continue;
-		} else if (ret < 0) {
-			printk(BIOS_DEBUG, "%s ramstage loader failure.\n",
-				ops->name);
-			continue;
-		}
-
-		printk(BIOS_DEBUG, "%s ramstage loader active.\n", ops->name);
-		load_ramstage(ops, &ramstage);
-	}
-
-	die("Ramstage was not loaded!\n");
-}
diff --git a/src/lib/loaders/load_and_run_romstage.c b/src/lib/loaders/load_and_run_romstage.c
deleted file mode 100644
index b54c691..0000000
--- a/src/lib/loaders/load_and_run_romstage.c
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-
-#include <stdlib.h>
-#include <console/console.h>
-#include <arch/stages.h>
-#include <cbfs.h>
-#include <halt.h>
-#include <program_loading.h>
-#include <rules.h>
-#include <timestamp.h>
-
-extern const struct prog_loader_ops cbfs_romstage_loader;
-extern const struct prog_loader_ops vboot_loader;
-
-static const struct prog_loader_ops *loaders[] = {
-#if CONFIG_VBOOT_VERIFY_FIRMWARE
-	&vboot_loader,
-#endif
-#if !ENV_VERSTAGE || (ENV_VERSTAGE && !CONFIG_RETURN_FROM_VERSTAGE)
-	&cbfs_romstage_loader,
-#endif
-};
-
-void run_romstage(void)
-{
-	int i;
-	struct prog romstage = {
-		.name = CONFIG_CBFS_PREFIX "/romstage",
-		.type = PROG_ROMSTAGE,
-	};
-
-	for (i = 0; i < ARRAY_SIZE(loaders); i++) {
-		/* Default loader state is active. */
-		int ret = 1;
-		const struct prog_loader_ops *ops;
-
-		ops = loaders[i];
-
-		if (ops->is_loader_active != NULL)
-			ret = ops->is_loader_active(&romstage);
-
-		if (ret == 0) {
-			printk(BIOS_DEBUG, "%s romstage loader inactive.\n",
-				ops->name);
-			continue;
-		} else if (ret < 0) {
-			printk(BIOS_DEBUG, "%s romstage loader failure.\n",
-				ops->name);
-			continue;
-		}
-
-		printk(BIOS_DEBUG, "%s romstage loader active.\n", ops->name);
-
-		timestamp_add_now(TS_START_COPYROM);
-
-		if (ops->prepare(&romstage))
-			continue;
-
-		timestamp_add_now(TS_END_COPYROM);
-
-		prog_run(&romstage);
-	}
-
-	if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
-		die("Couldn't load romstage.\n");
-	halt();
-}
diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c
new file mode 100644
index 0000000..881cd99
--- /dev/null
+++ b/src/lib/prog_loaders.c
@@ -0,0 +1,247 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2015 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+
+#include <stdlib.h>
+#include <arch/stages.h>
+#include <boot_device.h>
+#include <cbfs.h>
+#include <cbmem.h>
+#include <console/console.h>
+#include <fallback.h>
+#include <halt.h>
+#include <lib.h>
+#include <program_loading.h>
+#include <romstage_handoff.h>
+#include <rmodule.h>
+#include <rules.h>
+#include <stage_cache.h>
+#include <symbols.h>
+#include <timestamp.h>
+
+#define DEFAULT_CBFS_LOADER_PRESENT \
+	(!ENV_VERSTAGE || (ENV_VERSTAGE && !CONFIG_RETURN_FROM_VERSTAGE))
+
+#if DEFAULT_CBFS_LOADER_PRESENT
+static int cbfs_boot_prog_locate(struct prog *prog)
+{
+	return cbfs_boot_locate(&prog->rdev, prog->name, NULL);
+}
+
+static const struct prog_loader_ops cbfs_default_loader = {
+	.locate = cbfs_boot_prog_locate,
+};
+#endif
+
+extern const struct prog_loader_ops vboot_loader;
+
+static const struct prog_loader_ops *loaders[] = {
+#if CONFIG_VBOOT_VERIFY_FIRMWARE
+	&vboot_loader,
+#endif
+#if DEFAULT_CBFS_LOADER_PRESENT
+	&cbfs_default_loader,
+#endif
+};
+
+int prog_locate(struct prog *prog)
+{
+	int i;
+
+	boot_device_init();
+
+	for (i = 0; i < ARRAY_SIZE(loaders); i++) {
+		/* Default loader state is active. */
+		int ret = 1;
+		const struct prog_loader_ops *ops;
+
+		ops = loaders[i];
+
+		if (ops->is_loader_active != NULL)
+			ret = ops->is_loader_active(prog);
+
+		if (ret == 0) {
+			printk(BIOS_DEBUG, "%s loader inactive.\n",
+				ops->name);
+			continue;
+		} else if (ret < 0) {
+			printk(BIOS_DEBUG, "%s loader failure.\n",
+				ops->name);
+			continue;
+		}
+
+		printk(BIOS_DEBUG, "%s loader active.\n", ops->name);
+
+		if (ops->locate(prog))
+			continue;
+
+		printk(BIOS_DEBUG, "'%s' located at offset: %zx size: %zx\n",
+			prog->name, region_device_offset(&prog->rdev),
+			region_device_sz(&prog->rdev));
+
+		return 0;
+	}
+
+	return -1;
+}
+
+void run_romstage(void)
+{
+	struct prog romstage = {
+		.name = CONFIG_CBFS_PREFIX "/romstage",
+		.type = PROG_ROMSTAGE,
+	};
+
+	/* The only time the default CBFS loader isn't present is during
+	 * VERSTAGE in which it returns back to the calling stage. */
+	if (!DEFAULT_CBFS_LOADER_PRESENT)
+		return;
+
+	if (prog_locate(&romstage))
+		goto fail;
+
+	timestamp_add_now(TS_START_COPYROM);
+
+	if (cbfs_prog_stage_load(&romstage))
+		goto fail;
+
+	timestamp_add_now(TS_END_COPYROM);
+
+	prog_run(&romstage);
+
+fail:
+	if (IS_ENABLED(CONFIG_BOOTBLOCK_CONSOLE))
+		die("Couldn't load romstage.\n");
+	halt();
+}
+
+void __attribute__((weak)) stage_cache_add(int stage_id, struct prog *stage) {}
+void __attribute__((weak)) stage_cache_load_stage(int stage_id,
+							struct prog *stage) {}
+void __attribute__((weak)) ramstage_cache_invalid(void) {}
+
+static void run_ramstage_from_resume(struct romstage_handoff *handoff,
+					struct prog *ramstage)
+{
+	if (handoff != NULL && handoff->s3_resume) {
+		/* Load the cached ramstage to runtime location. */
+		stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
+
+		if (prog_entry(ramstage) != NULL) {
+			printk(BIOS_DEBUG, "Jumping to image.\n");
+			prog_run(ramstage);
+		}
+		ramstage_cache_invalid();
+	}
+}
+
+static int load_relocatable_ramstage(struct prog *ramstage)
+{
+	struct rmod_stage_load rmod_ram = {
+		.cbmem_id = CBMEM_ID_RAMSTAGE,
+		.prog = ramstage,
+	};
+
+	return rmodule_stage_load(&rmod_ram);
+}
+
+void run_ramstage(void)
+{
+	struct prog ramstage = {
+		.name = CONFIG_CBFS_PREFIX "/ramstage",
+		.type = PROG_RAMSTAGE,
+	};
+
+	/* Only x86 systems currently take the same firmware path on resume. */
+	if (IS_ENABLED(CONFIG_ARCH_X86) && IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
+		run_ramstage_from_resume(romstage_handoff_find_or_add(),
+						&ramstage);
+
+	if (prog_locate(&ramstage))
+		goto fail;
+
+	timestamp_add_now(TS_START_COPYRAM);
+
+	if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
+		if (load_relocatable_ramstage(&ramstage))
+			goto fail;
+	} else if (cbfs_prog_stage_load(&ramstage))
+		goto fail;
+
+	stage_cache_add(STAGE_RAMSTAGE, &ramstage);
+
+	timestamp_add_now(TS_END_COPYRAM);
+
+	prog_run(&ramstage);
+
+fail:
+	die("Ramstage was not loaded!\n");
+}
+
+static struct prog global_payload = {
+	.name = CONFIG_CBFS_PREFIX "/payload",
+	.type = PROG_PAYLOAD,
+};
+
+void __attribute__((weak)) mirror_payload(struct prog *payload)
+{
+	return;
+}
+
+void payload_load(void)
+{
+	struct prog *payload = &global_payload;
+
+	timestamp_add_now(TS_LOAD_PAYLOAD);
+
+	if (prog_locate(payload))
+		goto out;
+
+	mirror_payload(payload);
+
+	/* Pass cbtables to payload if architecture desires it. */
+	prog_set_entry(payload, selfload(payload),
+			cbmem_find(CBMEM_ID_CBTABLE));
+
+out:
+	if (prog_entry(payload) == NULL)
+		die("Payload not loaded.\n");
+}
+
+void payload_run(void)
+{
+	struct prog *payload = &global_payload;
+
+	/* Reset to booting from this image as late as possible */
+	boot_successful();
+
+	printk(BIOS_DEBUG, "Jumping to boot code at %p(%p)\n",
+		prog_entry(payload), prog_entry_arg(payload));
+
+	post_code(POST_ENTER_ELF_BOOT);
+
+	timestamp_add_now(TS_SELFBOOT_JUMP);
+
+	/* Before we go off to run the payload, see if
+	 * we stayed within our bounds.
+	 */
+	checkstack(_estack, 0);
+
+	prog_run(payload);
+}
diff --git a/src/lib/rmodule.c b/src/lib/rmodule.c
index d886800..e986590 100644
--- a/src/lib/rmodule.c
+++ b/src/lib/rmodule.c
@@ -253,7 +253,7 @@ int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
 	return region_alignment - sizeof(struct rmodule_header);
 }
 
-int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh)
+int rmodule_stage_load(struct rmod_stage_load *rsl)
 {
 	struct rmodule rmod_stage;
 	size_t region_size;
@@ -262,11 +262,14 @@ int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh)
 	int load_offset;
 	struct cbfs_stage stage;
 	void *rmod_loc;
+	struct region_device *fh;
 
-	if (fh == NULL || rsl->prog == NULL || rsl->prog->name == NULL)
+	if (rsl->prog == NULL || rsl->prog->name == NULL)
 		return -1;
 
-	if (cbfs_file_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
+	fh = &rsl->prog->rdev;
+
+	if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
 		return -1;
 
 	rmodule_offset =
@@ -284,19 +287,19 @@ int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh)
 	       rsl->prog->name, rmod_loc, stage.memlen);
 
 	if (stage.compression == CBFS_COMPRESS_NONE) {
-		if (cbfs_file_readat(fh, rmod_loc, sizeof(stage), stage.len) !=
+		if (rdev_readat(fh, rmod_loc, sizeof(stage), stage.len) !=
 		    stage.len)
 			return -1;
 	} else if (stage.compression == CBFS_COMPRESS_LZMA) {
 		size_t fsize;
-		void *map = cbfs_file_mmap(fh, sizeof(stage), stage.len);
+		void *map = rdev_mmap(fh, sizeof(stage), stage.len);
 
 		if (map == NULL)
 			return -1;
 
 		fsize = ulzma(map, rmod_loc);
 
-		cbfs_file_munmap(fh, map);
+		rdev_munmap(fh, map);
 
 		if (!fsize)
 			return -1;
@@ -315,14 +318,3 @@ int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh)
 
 	return 0;
 }
-
-int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl)
-{
-	struct cbfsf fh;
-	uint32_t type = CBFS_TYPE_STAGE;
-
-	if (cbfs_boot_locate(&fh, rsl->prog->name, &type))
-		return -1;
-
-	return rmodule_stage_load(rsl, &fh);
-}
diff --git a/src/lib/selfboot.c b/src/lib/selfboot.c
index edf36e7..6b4b0af 100644
--- a/src/lib/selfboot.c
+++ b/src/lib/selfboot.c
@@ -211,13 +211,11 @@ static int relocate_segment(unsigned long buffer, struct segment *seg)
 
 static int build_self_segment_list(
 	struct segment *head,
-	struct prog *payload, uintptr_t *entry)
+	struct cbfs_payload *cbfs_payload, uintptr_t *entry)
 {
 	struct segment *new;
 	struct segment *ptr;
 	struct cbfs_payload_segment *segment, *first_segment;
-	struct cbfs_payload *cbfs_payload;
-	cbfs_payload = prog_start(payload);
 	memset(head, 0, sizeof(*head));
 	head->next = head->prev = head;
 	first_segment = segment = &cbfs_payload->segments;
@@ -457,9 +455,15 @@ void *selfload(struct prog *payload)
 {
 	uintptr_t entry = 0;
 	struct segment head;
+	void *data;
+
+	data = rdev_mmap_full(&payload->rdev);
+
+	if (data == NULL)
+		return NULL;
 
 	/* Preprocess the self segments */
-	if (!build_self_segment_list(&head, payload, &entry))
+	if (!build_self_segment_list(&head, data, &entry))
 		goto out;
 
 	/* Load the segments */
@@ -468,8 +472,11 @@ void *selfload(struct prog *payload)
 
 	printk(BIOS_SPEW, "Loaded segments\n");
 
+	rdev_munmap(&payload->rdev, data);
+
 	return (void *)entry;
 
 out:
+	rdev_munmap(&payload->rdev, data);
 	return NULL;
 }
diff --git a/src/soc/broadcom/cygnus/include/soc/memlayout.ld b/src/soc/broadcom/cygnus/include/soc/memlayout.ld
index 6342810..760f192 100644
--- a/src/soc/broadcom/cygnus/include/soc/memlayout.ld
+++ b/src/soc/broadcom/cygnus/include/soc/memlayout.ld
@@ -34,7 +34,6 @@ SECTIONS
 	VBOOT2_WORK(0x02010000, 16K)
 	OVERLAP_VERSTAGE_ROMSTAGE(0x02014000, 120K)
 	PRERAM_CBFS_CACHE(0x02032000, 1K)
-	CBFS_HEADER_OFFSET(0x02032800)
 	STACK(0x02033000, 12K)
 	REGION(reserved_for_secure_service_api, 0x0203F000, 4K, 4)
 	SRAM_END(0x02040000)
diff --git a/src/soc/intel/baytrail/refcode.c b/src/soc/intel/baytrail/refcode.c
index 232c610..618fefd 100644
--- a/src/soc/intel/baytrail/refcode.c
+++ b/src/soc/intel/baytrail/refcode.c
@@ -19,7 +19,6 @@
 
 #include <string.h>
 #include <arch/acpi.h>
-#include <boot_device.h>
 #include <cbmem.h>
 #include <console/console.h>
 #include <console/streams.h>
@@ -27,9 +26,6 @@
 #include <program_loading.h>
 #include <rmodule.h>
 #include <stage_cache.h>
-#if IS_ENABLED(CONFIG_CHROMEOS)
-#include <vendorcode/google/chromeos/vboot_handoff.h>
-#endif
 
 #include <soc/ramstage.h>
 #include <soc/efi_wrapper.h>
@@ -50,66 +46,10 @@ static efi_wrapper_entry_t load_refcode_from_cache(void)
 	return (efi_wrapper_entry_t)prog_entry(&refcode);
 }
 
-static void cache_refcode(const struct rmod_stage_load *rsl)
-{
-	stage_cache_add(STAGE_REFCODE, rsl->prog);
-}
-
-#if IS_ENABLED(CONFIG_CHROMEOS)
-static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
-{
-	struct vboot_handoff *vboot_handoff;
-	const struct firmware_component *fwc;
-	struct cbfsf stage;
-	struct region reg;
-
-	vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
-	fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
-
-	if (vboot_handoff == NULL ||
-	    vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
-	    CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
-	    fwc->size == 0 || fwc->address == 0)
-		return -1;
-
-	reg.offset = fwc->address;
-	reg.size = fwc->size;
-
-	boot_device_init();
-
-	if (boot_device_ro_subregion(&reg, &stage.rdev))
-		return -1;
-
-	printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
-
-	if (rmodule_stage_load(refcode, &stage)) {
-		printk(BIOS_DEBUG, "Error loading reference code.\n");
-		return -1;
-	}
-	return 0;
-}
-#else
-static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
-{
-	return -1;
-}
-#endif
-
-static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
-{
-	printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
-
-	if (rmodule_stage_load_from_cbfs(refcode)) {
-		printk(BIOS_DEBUG, "Error loading reference code.\n");
-		return -1;
-	}
-
-	return 0;
-}
-
 static efi_wrapper_entry_t load_reference_code(void)
 {
 	struct prog prog = {
+		.type = PROG_REFCODE,
 		.name = CONFIG_CBFS_PREFIX "/refcode",
 	};
 	struct rmod_stage_load refcode = {
@@ -121,12 +61,18 @@ static efi_wrapper_entry_t load_reference_code(void)
 		return load_refcode_from_cache();
 	}
 
-	if (load_refcode_from_vboot(&refcode) &&
-		load_refcode_from_cbfs(&refcode))
-			return NULL;
+	if (prog_locate(&prog)) {
+		printk(BIOS_DEBUG, "Couldn't locate reference code.\n");
+		return NULL;
+	}
+
+	if (rmodule_stage_load(&refcode)) {
+		printk(BIOS_DEBUG, "Error loading reference code.\n");
+		return NULL;
+	}
 
 	/* Cache loaded reference code. */
-	cache_refcode(&refcode);
+	stage_cache_add(STAGE_REFCODE, &prog);
 
 	return prog_entry(&prog);
 }
diff --git a/src/soc/intel/broadwell/refcode.c b/src/soc/intel/broadwell/refcode.c
index 0a91514..f39fa92 100644
--- a/src/soc/intel/broadwell/refcode.c
+++ b/src/soc/intel/broadwell/refcode.c
@@ -19,7 +19,6 @@
 
 #include <string.h>
 #include <arch/acpi.h>
-#include <boot_device.h>
 #include <cbmem.h>
 #include <console/console.h>
 #include <console/streams.h>
@@ -28,9 +27,6 @@
 #include <rmodule.h>
 #include <stage_cache.h>
 #include <string.h>
-#if IS_ENABLED(CONFIG_CHROMEOS)
-#include <vendorcode/google/chromeos/vboot_handoff.h>
-#endif
 #include <soc/pei_data.h>
 #include <soc/pei_wrapper.h>
 #include <soc/ramstage.h>
@@ -46,66 +42,10 @@ static pei_wrapper_entry_t load_refcode_from_cache(void)
 	return (pei_wrapper_entry_t)prog_entry(&refcode);
 }
 
-static void cache_refcode(const struct rmod_stage_load *rsl)
-{
-	stage_cache_add(STAGE_REFCODE, rsl->prog);
-}
-
-#if IS_ENABLED(CONFIG_CHROMEOS)
-static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
-{
-	struct vboot_handoff *vboot_handoff;
-	const struct firmware_component *fwc;
-	struct cbfsf stage;
-	struct region reg;
-
-	vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
-	fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
-
-	if (vboot_handoff == NULL ||
-	    vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
-	    CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
-	    fwc->size == 0 || fwc->address == 0)
-		return -1;
-
-	reg.offset = fwc->address;
-	reg.size = fwc->size;
-
-	boot_device_init();
-
-	if (boot_device_ro_subregion(&reg, &stage.rdev))
-		return -1;
-
-	printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
-
-	if (rmodule_stage_load(refcode, &stage)) {
-		printk(BIOS_DEBUG, "Error loading reference code.\n");
-		return -1;
-	}
-	return 0;
-}
-#else
-static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
-{
-	return -1;
-}
-#endif
-
-static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
-{
-	printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
-
-	if (rmodule_stage_load_from_cbfs(refcode)) {
-		printk(BIOS_DEBUG, "Error loading reference code.\n");
-		return -1;
-	}
-
-	return 0;
-}
-
-static pei_wrapper_entry_t load_reference_code(void)
+static efi_wrapper_entry_t load_reference_code(void)
 {
 	struct prog prog = {
+		.type = PROG_REFCODE,
 		.name = CONFIG_CBFS_PREFIX "/refcode",
 	};
 	struct rmod_stage_load refcode = {
@@ -117,12 +57,18 @@ static pei_wrapper_entry_t load_reference_code(void)
 		return load_refcode_from_cache();
 	}
 
-	if (load_refcode_from_vboot(&refcode) &&
-		load_refcode_from_cbfs(&refcode))
-			return NULL;
+	if (prog_locate(&prog)) {
+		printk(BIOS_DEBUG, "Couldn't locate reference code.\n");
+		return NULL;
+	}
+
+	if (rmodule_stage_load(&refcode)) {
+		printk(BIOS_DEBUG, "Error loading reference code.\n");
+		return NULL;
+	}
 
 	/* Cache loaded reference code. */
-	cache_refcode(&refcode);
+	stage_cache_add(STAGE_REFCODE, &prog);
 
 	return prog_entry(&prog);
 }
diff --git a/src/soc/nvidia/tegra132/ccplex.c b/src/soc/nvidia/tegra132/ccplex.c
index 7f16d54..c480887 100644
--- a/src/soc/nvidia/tegra132/ccplex.c
+++ b/src/soc/nvidia/tegra132/ccplex.c
@@ -77,7 +77,7 @@ int ccplex_load_mts(void)
 {
 	ssize_t nread;
 	struct stopwatch sw;
-	struct cbfsf fh;
+	struct region_device fh;
 
 	/*
 	 * MTS location is hard coded to this magic address. The hardware will
@@ -93,11 +93,11 @@ int ccplex_load_mts(void)
 	}
 
 	/* Read MTS file into the carveout region. */
-	nread = cbfs_file_readat(&fh, mts, 0, cbfs_file_size(&fh));
+	nread = rdev_readat(&fh, mts, 0, region_device_sz(&fh));
 
-	if (nread != cbfs_file_size(&fh)) {
+	if (nread != region_device_sz(&fh)) {
 		printk(BIOS_DEBUG, "MTS bytes read (%zu) != file length(%u)!\n",
-			nread, cbfs_file_size(&fh));
+			nread, region_device_sz(&fh));
 		return -1;
 	}
 
diff --git a/src/soc/qualcomm/ipq806x/include/soc/memlayout.ld b/src/soc/qualcomm/ipq806x/include/soc/memlayout.ld
index bfb4450..08c156f 100644
--- a/src/soc/qualcomm/ipq806x/include/soc/memlayout.ld
+++ b/src/soc/qualcomm/ipq806x/include/soc/memlayout.ld
@@ -34,9 +34,8 @@ SECTIONS
 	OVERLAP_VERSTAGE_ROMSTAGE(0x2A012000, 64K)
 	VBOOT2_WORK(0x2A022000, 16K)
 	PRERAM_CBMEM_CONSOLE(0x2A026000, 32K)
-	CBFS_HEADER_OFFSET(0x2A02E400)
 
-/*	0x2e404..0x3F000 4 bytes shy of 67KB free */
+/*	0x2e400..0x3F000 67KB free */
 
 /* Keep the below area reserved at all times, it is used by various QCA
    components as shared data
diff --git a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
index 6e100a2..c6a75bd 100644
--- a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
+++ b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
@@ -98,7 +98,8 @@ static int vboot_loader_active(struct prog *prog)
 		printk(BIOS_DEBUG, "VBOOT: Loading verstage.\n");
 
 		/* load verstage from RO */
-		if (cbfs_boot_load_stage(&verstage))
+		if (cbfs_boot_locate(&verstage.rdev, verstage.name, NULL) ||
+		    cbfs_prog_stage_load(&verstage))
 			die("failed to load verstage");
 
 		/* verify and select a slot */
@@ -118,29 +119,32 @@ static int vboot_loader_active(struct prog *prog)
 
 	wd = vboot_get_working_data();
 
-	if (vboot_is_slot_selected(wd)) {
-		if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES) &&
-		    run_verification) {
-			/* RW A or B */
-			struct region_device fw_main;
-
-			if (vb2_get_selected_region(wd, &fw_main))
-				die("failed to reference selected region\n");
-			cbfs_set_header_offset(region_device_offset(&fw_main));
-		}
+	if (vboot_is_slot_selected(wd))
 		return 1;
-	}
 
 	return 0;
 }
 
-static int vboot_fw_region(int fw_index, const struct region_device *fw_main,
-				struct region_device *fw)
+static int vboot_locate_by_components(const struct region_device *fw_main,
+					struct prog *prog)
 {
 	struct vboot_components *fw_info;
 	size_t metadata_sz;
 	size_t offset;
 	size_t size;
+	struct region_device *fw = &prog->rdev;
+	int fw_index = 0;
+
+	if (prog->type == PROG_ROMSTAGE)
+		fw_index = CONFIG_VBOOT_ROMSTAGE_INDEX;
+	else if (prog->type == PROG_RAMSTAGE)
+		fw_index = CONFIG_VBOOT_RAMSTAGE_INDEX;
+	else if (prog->type == PROG_PAYLOAD)
+		fw_index = CONFIG_VBOOT_BOOT_LOADER_INDEX;
+	else if (prog->type == PROG_REFCODE)
+		fw_index = CONFIG_VBOOT_REFCODE_INDEX;
+	else
+		die("Invalid program type for vboot.");
 
 	metadata_sz = sizeof(*fw_info);
 	metadata_sz += MAX_PARSED_FW_COMPONENTS * sizeof(fw_info->entries[0]);
@@ -153,7 +157,7 @@ static int vboot_fw_region(int fw_index, const struct region_device *fw_main,
 	}
 
 	if (fw_index >= fw_info->num_components) {
-		printk(BIOS_INFO, "invalid stage index: %d\n", fw_index);
+		printk(BIOS_INFO, "invalid index: %d\n", fw_index);
 		rdev_munmap(fw_main, fw_info);
 		return -1;
 	}
@@ -163,16 +167,44 @@ static int vboot_fw_region(int fw_index, const struct region_device *fw_main,
 	rdev_munmap(fw_main, fw_info);
 
 	if (rdev_chain(fw, fw_main, offset, size)) {
-		printk(BIOS_INFO, "invalid stage address or size\n");
+		printk(BIOS_INFO, "invalid offset or size\n");
 		return -1;
 	}
 
 	return 0;
 }
 
+static int vboot_locate_by_multi_cbfs(const struct region_device *fw_main,
+					struct prog *prog)
+{
+	struct cbfsd cbfs;
+	struct region_device rdev;
+	struct cbfs_props props;
+
+	if (cbfs_boot_region_properties(&props))
+		return -1;
+
+	if (rdev_chain(&rdev, fw_main, props.offset, props.size))
+		return -1;
+
+	cbfs.rdev = &rdev;
+	cbfs.align = props.align;
+
+	return cbfs_locate(&prog->rdev, &cbfs, prog->name, NULL);
+}
+
+static int vboot_prog_locate(const struct region_device *fw_main,
+				struct prog *prog)
+{
+	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES))
+		return vboot_locate_by_multi_cbfs(fw_main, prog);
+	else
+		return vboot_locate_by_components(fw_main, prog);
+}
+
 /* This function is only called when vboot_loader_active() returns 1. That
  * means we are taking vboot paths. */
-static int vboot_prepare(struct prog *prog)
+static int vboot_locate(struct prog *prog)
 {
 	struct vb2_working_data *wd;
 	struct region_device fw_main;
@@ -183,79 +215,15 @@ static int vboot_prepare(struct prog *prog)
 	if (verstage_should_load() && !IS_ENABLED(CONFIG_RETURN_FROM_VERSTAGE))
 		return 0;
 
-	/* In the multi cbfs case the cbfs offset pointer has already been
-	 * updated after firmware verification. */
-	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)) {
-		if (!ENV_RAMSTAGE && cbfs_boot_load_stage(prog) != 0)
-			return -1;
-
-		/* Need to load payload. */
-		if (ENV_RAMSTAGE) {
-			void *payload;
-			size_t size;
-
-			payload = cbfs_boot_map_with_leak(prog->name,
-							CBFS_TYPE_PAYLOAD,
-							&size);
-
-			if (payload == NULL)
-				die("Couldn't load payload\n");
-
-			prog_set_area(prog, payload, size);
-		}
-		return 0;
-	}
-
 	wd = vboot_get_working_data();
 	if (vb2_get_selected_region(wd, &fw_main))
 		die("failed to reference selected region\n");
 
-	/* Load payload in ramstage. */
-	if (ENV_RAMSTAGE) {
-		struct region_device payload;
-		void *payload_ptr;
-
-		if (vboot_fw_region(CONFIG_VBOOT_BOOT_LOADER_INDEX,
-						&fw_main, &payload))
-			die("Couldn't load payload.");
-
-		payload_ptr = rdev_mmap_full(&payload);
-
-		if (payload_ptr == NULL)
-			die("Couldn't load payload.");
-
-		prog_set_area(prog, payload_ptr, region_device_sz(&payload));
-	} else {
-		struct cbfsf stage;
-		int stage_index = 0;
-
-		if (prog->type == PROG_ROMSTAGE)
-			stage_index = CONFIG_VBOOT_ROMSTAGE_INDEX;
-		else if (prog->type == PROG_RAMSTAGE)
-			stage_index = CONFIG_VBOOT_RAMSTAGE_INDEX;
-		else
-			die("Invalid program type for vboot.");
-
-		if (vboot_fw_region(stage_index, &fw_main, &stage.rdev))
-			die("Vboot stage load failed.");
-
-		if (ENV_ROMSTAGE && IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
-			struct rmod_stage_load rmod_ram = {
-				.cbmem_id = CBMEM_ID_RAMSTAGE,
-				.prog = prog,
-			};
-
-			if (rmodule_stage_load(&rmod_ram, &stage))
-				die("Vboot couldn't load stage");
-		} else if (cbfs_file_stage_load(&stage, prog))
-				die("Vboot couldn't load stage");
-	}
-
-	return 0;
+	return vboot_prog_locate(&fw_main, prog);
 }
 
 const struct prog_loader_ops vboot_loader = {
 	.name = "VBOOT",
 	.is_loader_active = vboot_loader_active,
-	.prepare = vboot_prepare,
+	.locate = vboot_locate,
 };



More information about the coreboot-gerrit mailing list