[coreboot-gerrit] Patch set updated for coreboot: de7e317 cbfs: add new API using region_device

Aaron Durbin (adurbin@chromium.org) gerrit at coreboot.org
Tue May 19 22:36:12 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/9134

-gerrit

commit de7e3177dadc8d4a4aa18235e545e25f9b36f7a2
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Fri May 15 23:39:23 2015 -0500

    cbfs: add new API using region_device
    
    A new CBFS API is introduced to allow making CBFS access
    easier for providing multiple CBFS sources. That is achieved
    by decoupling the cbfs source from a CBFS file. A CBFS
    source is described by a descriptor. It contains the necessary
    properties for walking a CBFS to locate a file. The CBFS
    file is then decoupled from the CBFS descriptor in that it's
    no longer needed to access the contents of the file.
    
    All of this is accomplished using the regions infrastructure
    by repsenting CBFS sources and files as region_devices. Because
    region_devices can be chained together forming subregions this
    allows one to decouple a CBFS source from a file. This also allows
    one to provide CBFS files that came from other sources for
    payload and/or stage loading.
    
    Change-Id: I339b84fce95f03d1dbb63a0f54a26be5eb07f7c8
    Signed-off-by: Aaron Durbin <adurbin at chromium.org>
---
 src/arch/riscv/rom_media.c                         |  67 +---
 src/arch/x86/lib/Makefile.inc                      |   3 -
 src/arch/x86/lib/mmap_boot.c                       |  44 +++
 src/arch/x86/lib/rom_media.c                       |  98 ------
 src/cpu/allwinner/a10/bootblock_media.c            |   8 +-
 src/cpu/amd/microcode/microcode.c                  |   4 +-
 src/cpu/intel/microcode/microcode.c                |  18 +-
 src/cpu/ti/am335x/bootblock_media.c                |  71 +----
 src/cpu/ti/am335x/nand.c                           |   8 +-
 src/cpu/via/nano/update_ucode.c                    |   5 +-
 src/device/dram/spd_cache.c                        |   4 +-
 src/device/oprom/realmode/x86.c                    |  14 +-
 src/device/oprom/yabel/vbe.c                       |   7 +-
 src/device/pci_rom.c                               |   8 +-
 src/drivers/intel/fsp1_0/fastboot_cache.c          |   7 +-
 src/drivers/intel/fsp1_1/fastboot_cache.c          |   3 +-
 src/drivers/pc80/mc146818rtc.c                     |   8 +-
 src/drivers/spi/spi_flash.c                        |   7 +-
 src/include/cbfs.h                                 | 153 +++++----
 src/include/cbfs_core.h                            | 128 --------
 src/include/rmodule.h                              |   4 +-
 src/lib/Makefile.inc                               |  14 +-
 src/lib/cbfs.c                                     | 355 ++++++++++++---------
 src/lib/cbfs_boot_props.c                          |  70 ++++
 src/lib/cbfs_core.c                                | 324 -------------------
 src/lib/cbfs_core.h                                |  47 ---
 src/lib/cbfs_spi.c                                 |  72 -----
 src/lib/coreboot_table.c                           |   4 +-
 src/lib/loaders/cbfs_payload_loader.c              |   5 +-
 src/lib/loaders/cbfs_ramstage_loader.c             |   3 +-
 src/lib/loaders/cbfs_romstage_loader.c             |   2 +-
 src/lib/rmodule.c                                  |  48 ++-
 src/mainboard/emulation/qemu-armv7/media.c         |  75 +----
 src/mainboard/google/bolt/romstage.c               |   4 +-
 src/mainboard/google/butterfly/mainboard.c         |   5 +-
 src/mainboard/google/falco/romstage.c              |   4 +-
 src/mainboard/google/link/romstage.c               |   4 +-
 src/mainboard/google/panther/lan.c                 |   3 +-
 src/mainboard/google/peach_pit/romstage.c          |  17 +-
 src/mainboard/google/peppy/romstage.c              |   4 +-
 src/mainboard/google/rambi/romstage.c              |   9 +-
 src/mainboard/google/samus/spd/spd.c               |  13 +-
 src/mainboard/google/slippy/romstage.c             |   4 +-
 src/mainboard/google/urara/boardid.c               |  23 +-
 src/mainboard/samsung/lumpy/romstage.c             |   4 +-
 src/mainboard/siemens/mc_tcu3/modhwinfo.c          |   6 +-
 src/northbridge/amd/agesa/common/common.c          |   3 +-
 src/northbridge/amd/agesa/def_callouts.c           |   4 +-
 src/northbridge/amd/pi/agesawrapper.c              |  11 +-
 src/northbridge/amd/pi/def_callouts.c              |   4 +-
 src/northbridge/intel/haswell/mrccache.c           |   3 +-
 src/northbridge/intel/haswell/raminit.c            |   4 +-
 src/northbridge/intel/i82830/vga.c                 |   3 +-
 src/northbridge/intel/sandybridge/mrccache.c       |   3 +-
 src/northbridge/intel/sandybridge/raminit.c        |   3 +-
 src/soc/intel/baytrail/refcode.c                   |  15 +-
 src/soc/intel/baytrail/romstage/raminit.c          |   3 +-
 src/soc/intel/broadwell/refcode.c                  |  15 +-
 src/soc/intel/broadwell/romstage/raminit.c         |   3 +-
 src/soc/nvidia/tegra124/spi.c                      |  75 -----
 src/soc/nvidia/tegra132/ccplex.c                   |  15 +-
 src/soc/nvidia/tegra132/spi.c                      |  75 -----
 src/soc/qualcomm/ipq806x/blobs_init.c              |  12 +-
 src/soc/samsung/exynos5250/alternate_cbfs.c        |  64 ----
 src/soc/samsung/exynos5420/alternate_cbfs.c        |  64 ----
 .../google/chromeos/vboot2/vboot_loader.c          |  27 +-
 66 files changed, 615 insertions(+), 1579 deletions(-)

diff --git a/src/arch/riscv/rom_media.c b/src/arch/riscv/rom_media.c
index 484f1ee..04969f5 100644
--- a/src/arch/riscv/rom_media.c
+++ b/src/arch/riscv/rom_media.c
@@ -19,76 +19,13 @@
  * MA 02110-1301 USA
  */
 #include <boot_device.h>
-#include <cbfs.h>
-#include <console/console.h>
-#include <string.h>
 
 /* This assumes that the CBFS resides at 0x0, which is true for the default
  * configuration. */
-static const struct mem_region_device gboot_dev =
+static const struct mem_region_device boot_dev =
 	MEM_REGION_DEV_INIT(NULL, CONFIG_ROM_SIZE);
 
 const struct region_device *boot_device_ro(void)
 {
-	return &gboot_dev.rdev;
-}
-
-static int rom_media_open(struct cbfs_media *media) {
-	return 0;
-}
-
-static void *rom_media_map(struct cbfs_media *media, size_t offset, size_t count) {
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	printk(BIOS_INFO, "%s: media %p, offset %lx, size %ld.\n", __func__, media, offset, count);
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *rom_media_unmap(struct cbfs_media *media, const void *address) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-static size_t rom_media_read(struct cbfs_media *media, void *dest, size_t offset,
-			   size_t count) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static int rom_media_close(struct cbfs_media *media) {
-	return 0;
-}
-
-static int init_rom_media_cbfs(struct cbfs_media *media) {
-	boot_device_init();
-	media->context = (void *)boot_device_ro();
-	media->open = rom_media_open;
-	media->close = rom_media_close;
-	media->map = rom_media_map;
-	media->unmap = rom_media_unmap;
-	media->read = rom_media_read;
-	return 0;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media) {
-	return init_rom_media_cbfs(media);
+	return &boot_dev.rdev;
 }
diff --git a/src/arch/x86/lib/Makefile.inc b/src/arch/x86/lib/Makefile.inc
index e308be9..0d88a6d 100644
--- a/src/arch/x86/lib/Makefile.inc
+++ b/src/arch/x86/lib/Makefile.inc
@@ -5,7 +5,6 @@ romstage-y += cbfs_and_run.c
 romstage-y += memset.c
 romstage-y += memcpy.c
 romstage-y += memmove.c
-romstage-y += rom_media.c
 romstage-y += mmap_boot.c
 
 endif # CONFIG_ARCH_ROMSTAGE_X86_32
@@ -22,7 +21,6 @@ ramstage-y += memset.c
 ramstage-y += memcpy.c
 ramstage-y += memmove.c
 ramstage-y += ebda.c
-ramstage-y += rom_media.c
 ramstage-y += mmap_boot.c
 ramstage-$(CONFIG_COOP_MULTITASKING) += thread.c
 ramstage-$(CONFIG_COOP_MULTITASKING) += thread_switch.S
@@ -33,7 +31,6 @@ romstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 smm-y += memset.c
 smm-y += memcpy.c
 smm-y += memmove.c
-smm-y += rom_media.c
 smm-y += mmap_boot.c
 
 rmodules_x86_32-y += memset.c
diff --git a/src/arch/x86/lib/mmap_boot.c b/src/arch/x86/lib/mmap_boot.c
index eb7b23e..4dd269b 100644
--- a/src/arch/x86/lib/mmap_boot.c
+++ b/src/arch/x86/lib/mmap_boot.c
@@ -18,6 +18,10 @@
  */
 
 #include <boot_device.h>
+#include <console/console.h>
+#include <cbfs.h>
+#include <endian.h>
+#include <stdlib.h>
 
 /* The ROM is memory mapped just below 4GiB. Form a pointer for the base. */
 #define rom_base ((void *)(uintptr_t)(-(int32_t)CONFIG_ROM_SIZE))
@@ -29,3 +33,43 @@ const struct region_device *boot_device_ro(void)
 {
 	return &boot_dev.rdev;
 }
+
+int cbfs_boot_region_properties(struct cbfs_props *props)
+{
+	struct cbfs_header header;
+	int32_t offset;
+	const struct region_device *bdev;
+
+	bdev = boot_device_ro();
+
+	rdev_readat(bdev, &offset, CONFIG_ROM_SIZE - sizeof(offset),
+			sizeof(offset));
+
+	/* The offset is relative to the end of the media. */
+	offset += CONFIG_ROM_SIZE;
+
+	rdev_readat(bdev, &header , offset, sizeof(header));
+
+	header.magic = ntohl(header.magic);
+	header.romsize = ntohl(header.romsize);
+	header.bootblocksize = ntohl(header.bootblocksize);
+	header.align = ntohl(header.align);
+	header.offset = ntohl(header.offset);
+
+	if (header.magic != CBFS_HEADER_MAGIC)
+		return -1;
+
+	props->align = header.align;
+	props->offset = header.offset;
+	if (CONFIG_ROM_SIZE != header.romsize)
+		props->size = CONFIG_ROM_SIZE;
+	else
+		props->size = header.romsize;
+	props->size -= props->offset;
+	props->size -= header.bootblocksize;
+	props->size = ALIGN_DOWN(props->size, props->align);
+
+	printk(BIOS_DEBUG, "CBFS @ %zx size %zx\n", props->offset, props->size);
+
+	return 0;
+}
diff --git a/src/arch/x86/lib/rom_media.c b/src/arch/x86/lib/rom_media.c
deleted file mode 100644
index c2c4721..0000000
--- a/src/arch/x86/lib/rom_media.c
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
- *
- * 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 <boot_device.h>
-#include <cbfs.h>
-#include <string.h>
-
-#ifdef LIBPAYLOAD
-# define printk(x...)
-# define init_default_cbfs_media libpayload_init_default_cbfs_media
-  extern int libpayload_init_default_cbfs_media(struct cbfs_media *media);
-#else
-# include <console/console.h>
-#endif
-
-// Implementation of memory-mapped ROM media source on X86.
-
-static int x86_rom_open(struct cbfs_media *media) {
-	return 0;
-}
-
-static void *x86_rom_map(struct cbfs_media *media, size_t offset, size_t count) {
-	void *ptr;
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	/* Extremely large offsets are considered relative to end of region. */
-	if ((uint32_t)offset > (uint32_t)0xf0000000)
-		offset += region_device_sz(boot_dev);
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *x86_rom_unmap(struct cbfs_media *media, const void *address) {
-	return NULL;
-}
-
-static size_t x86_rom_read(struct cbfs_media *media, void *dest, size_t offset,
-			   size_t count) {
-	void *ptr;
-
-	ptr = x86_rom_map(media, offset, count);
-
-	if (ptr == (void *)-1)
-		return 0;
-
-	memcpy(dest, ptr, count);
-	x86_rom_unmap(media, ptr);
-	return count;
-}
-
-static int x86_rom_close(struct cbfs_media *media) {
-	return 0;
-}
-
-static int init_x86rom_cbfs_media(struct cbfs_media *media) {
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-
-	if (media->context == NULL)
-		return -1;
-
-	media->open = x86_rom_open;
-	media->close = x86_rom_close;
-	media->map = x86_rom_map;
-	media->unmap = x86_rom_unmap;
-	media->read = x86_rom_read;
-	return 0;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media) {
-	return init_x86rom_cbfs_media(media);
-}
diff --git a/src/cpu/allwinner/a10/bootblock_media.c b/src/cpu/allwinner/a10/bootblock_media.c
index a585bca..17c6039 100644
--- a/src/cpu/allwinner/a10/bootblock_media.c
+++ b/src/cpu/allwinner/a10/bootblock_media.c
@@ -5,16 +5,10 @@
  * Subject to the GNU GPL v2, or (at your option) any later version.
  */
 #include <boot_device.h>
-#include <cbfs.h>
 #include <console/console.h>
 
 const struct region_device *boot_device_ro(void)
 {
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
 	printk(BIOS_ERR, "Oh my! I don't know how to access CBFS yet.");
-	return -1;
+	return NULL;
 }
diff --git a/src/cpu/amd/microcode/microcode.c b/src/cpu/amd/microcode/microcode.c
index 938f5f6..d5bf756 100644
--- a/src/cpu/amd/microcode/microcode.c
+++ b/src/cpu/amd/microcode/microcode.c
@@ -105,8 +105,8 @@ void amd_update_microcode_from_cbfs(u32 equivalent_processor_rev_id)
 		return;
 	}
 
-	ucode = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, MICROCODE_CBFS_FILE,
-				      CBFS_TYPE_MICROCODE, &ucode_len);
+	ucode = cbfs_boot_map_with_leak(MICROCODE_CBFS_FILE,
+					CBFS_TYPE_MICROCODE, &ucode_len);
 	if (!ucode) {
 		UCODE_DEBUG("microcode file not found. Skipping updates.\n");
 		return;
diff --git a/src/cpu/intel/microcode/microcode.c b/src/cpu/intel/microcode/microcode.c
index 947dfd7..c4d2cd0 100644
--- a/src/cpu/intel/microcode/microcode.c
+++ b/src/cpu/intel/microcode/microcode.c
@@ -109,25 +109,29 @@ void intel_microcode_load_unlocked(const void *microcode_patch)
 
 const void *intel_microcode_find(void)
 {
-	struct cbfs_file *microcode_file;
 	const struct microcode *ucode_updates;
-	u32 eax, microcode_len;
+	size_t microcode_len;
+	u32 eax;
 	u32 pf, rev, sig, update_size;
 	unsigned int x86_model, x86_family;
 	msr_t msr;
 
 #ifdef __PRE_RAM__
-	microcode_file = walkcbfs_head((char *) MICROCODE_CBFS_FILE);
-#else
-	microcode_file = cbfs_get_file(CBFS_DEFAULT_MEDIA,
-					  MICROCODE_CBFS_FILE);
-#endif
+	struct cbfs_file *microcode_file;
 
+	microcode_file = walkcbfs_head((char *) MICROCODE_CBFS_FILE);
 	if (!microcode_file)
 		return NULL;
 
 	ucode_updates = CBFS_SUBHEADER(microcode_file);
 	microcode_len = ntohl(microcode_file->len);
+#else
+	ucode_updates = cbfs_boot_map_with_leak(MICROCODE_CBFS_FILE,
+						CBFS_TYPE_MICROCODE,
+						&microcode_len);
+	if (ucode_updates == NULL)
+		return NULL;
+#endif
 
 	/* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
 	msr.lo = 0;
diff --git a/src/cpu/ti/am335x/bootblock_media.c b/src/cpu/ti/am335x/bootblock_media.c
index 702d91a..8eb4c27 100644
--- a/src/cpu/ti/am335x/bootblock_media.c
+++ b/src/cpu/ti/am335x/bootblock_media.c
@@ -18,80 +18,13 @@
  */
 
 #include <boot_device.h>
-#include <cbfs.h>
-#include <console/console.h>
-#include <string.h>
 #include <symbols.h>
 
 /* FIXME: No idea how big the internal SRAM actually is. */
-static const struct mem_region_device gboot_dev =
+static const struct mem_region_device boot_dev =
 	MEM_REGION_DEV_INIT(_dram, CONFIG_ROM_SIZE);
 
 const struct region_device *boot_device_ro(void)
 {
-	return &gboot_dev.rdev;
-}
-
-static int dummy_open(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static int dummy_close(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static void * on_chip_memory_map(struct cbfs_media *media, size_t offset,
-				 size_t count)
-{
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void * dummy_unmap(struct cbfs_media *media, const void *address)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-static size_t on_chip_memory_read(struct cbfs_media *media, void *dest,
-				  size_t offset, size_t count)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-	media->open = dummy_open;
-	media->close = dummy_close;
-	media->map = on_chip_memory_map;
-	media->unmap = dummy_unmap;
-	media->read = on_chip_memory_read;
-
-	return 0;
+	return &boot_dev.rdev;
 }
diff --git a/src/cpu/ti/am335x/nand.c b/src/cpu/ti/am335x/nand.c
index 7e67e97..73dedbb 100644
--- a/src/cpu/ti/am335x/nand.c
+++ b/src/cpu/ti/am335x/nand.c
@@ -18,15 +18,9 @@
  */
 
 #include <boot_device.h>
-#include <cbfs.h>
 
 const struct region_device *boot_device_ro(void)
 {
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
 	/* FIXME: add support for reading coreboot from NAND */
-	return -1;
+	return NULL;
 }
diff --git a/src/cpu/via/nano/update_ucode.c b/src/cpu/via/nano/update_ucode.c
index aa0adeb..a69a291 100644
--- a/src/cpu/via/nano/update_ucode.c
+++ b/src/cpu/via/nano/update_ucode.c
@@ -110,9 +110,8 @@ unsigned int nano_update_ucode(void)
 	u32 *ucode_data;
 	size_t ucode_len;
 
-	ucode_data = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-					   "cpu_microcode_blob.bin",
-					   CBFS_TYPE_MICROCODE, &ucode_len);
+	ucode_data = cbfs_boot_map_with_leak("cpu_microcode_blob.bin",
+					     CBFS_TYPE_MICROCODE, &ucode_len);
 	/* Oops, did you forget to include the microcode ? */
 	if(ucode_data == NULL) {
 		printk(BIOS_ALERT, "WARNING: No microcode file found in CBFS. "
diff --git a/src/device/dram/spd_cache.c b/src/device/dram/spd_cache.c
index 8abb791..fcb1391 100644
--- a/src/device/dram/spd_cache.c
+++ b/src/device/dram/spd_cache.c
@@ -36,8 +36,8 @@ int read_spd_from_cbfs(u8 *buf, int idx)
 	size_t min_len = (idx + 1) * SPD_SIZE;
 
 	printk(BIOS_DEBUG, "read SPD\n");
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		printk(BIOS_EMERG, "file [spd.bin] not found in CBFS");
 	if (spd_file_len < min_len)
diff --git a/src/device/oprom/realmode/x86.c b/src/device/oprom/realmode/x86.c
index 919fd6d..7f8c166 100644
--- a/src/device/oprom/realmode/x86.c
+++ b/src/device/oprom/realmode/x86.c
@@ -273,10 +273,9 @@ void vbe_set_graphics(void)
 	vbe_set_mode(&mode_info);
 #if CONFIG_BOOTSPLASH
 	struct jpeg_decdata *decdata;
-	unsigned char *jpeg = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						    "bootsplash.jpg",
-						    CBFS_TYPE_BOOTSPLASH,
-						    NULL);
+	unsigned char *jpeg = cbfs_boot_map_with_leak("bootsplash.jpg",
+							CBFS_TYPE_BOOTSPLASH,
+							NULL);
 	if (!jpeg) {
 		printk(BIOS_DEBUG, "VBE: No bootsplash found.\n");
 		return;
@@ -379,6 +378,9 @@ 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 */
@@ -390,8 +392,8 @@ void do_vsmbios(void)
 	/* Make sure the code is placed. */
 	setup_realmode_code();
 
-	if ((unsigned int)cbfs_load_stage(CBFS_DEFAULT_MEDIA, "vsa") !=
-	    VSA2_ENTRY_POINT) {
+	if (cbfs_boot_load_stage(&vsa) ||
+	    (uintptr_t)prog_entry(&vsa) != VSA2_ENTRY_POINT) {
 		printk(BIOS_ERR, "Failed to load VSA.\n");
 		return;
 	}
diff --git a/src/device/oprom/yabel/vbe.c b/src/device/oprom/yabel/vbe.c
index ab92c0a..07eb1f3 100644
--- a/src/device/oprom/yabel/vbe.c
+++ b/src/device/oprom/yabel/vbe.c
@@ -724,10 +724,9 @@ void vbe_set_graphics(void)
 	 * cares. */
 	// int imagesize = 1024*768*2;
 
-	unsigned char *jpeg = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						    "bootsplash.jpg",
-						    CBFS_TYPE_BOOTSPLASH,
-						    NULL);
+	unsigned char *jpeg = cbfs_boot_map_with_leak("bootsplash.jpg",
+							CBFS_TYPE_BOOTSPLASH,
+							NULL);
 	if (!jpeg) {
 		DEBUG_PRINTF_VBE("Could not find bootsplash.jpg\n");
 		return;
diff --git a/src/device/pci_rom.c b/src/device/pci_rom.c
index dbc05ac..dd70770 100644
--- a/src/device/pci_rom.c
+++ b/src/device/pci_rom.c
@@ -38,8 +38,7 @@ struct rom_header *pci_rom_probe(struct device *dev)
 	struct pci_data *rom_data;
 
 	/* If it's in FLASH, then don't check device for ROM. */
-	rom_header = cbfs_load_optionrom(CBFS_DEFAULT_MEDIA, dev->vendor,
-					 dev->device, NULL);
+	rom_header = cbfs_boot_map_optionrom(dev->vendor, dev->device);
 
 	u32 vendev = (dev->vendor << 16) | dev->device;
 	u32 mapped_vendev = vendev;
@@ -48,10 +47,9 @@ struct rom_header *pci_rom_probe(struct device *dev)
 
 	if (!rom_header) {
 		if (vendev != mapped_vendev) {
-			rom_header = cbfs_load_optionrom(
-					CBFS_DEFAULT_MEDIA,
+			rom_header = cbfs_boot_map_optionrom(
 					mapped_vendev >> 16,
-					mapped_vendev & 0xffff, NULL);
+					mapped_vendev & 0xffff);
 		}
 	}
 
diff --git a/src/drivers/intel/fsp1_0/fastboot_cache.c b/src/drivers/intel/fsp1_0/fastboot_cache.c
index e8651bb..9f45ed5 100644
--- a/src/drivers/intel/fsp1_0/fastboot_cache.c
+++ b/src/drivers/intel/fsp1_0/fastboot_cache.c
@@ -63,10 +63,9 @@ static int is_mrc_cache(struct mrc_data_container *mrc_cache)
 static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 {
 	size_t region_size;
-	*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						"mrc.cache",
-						CBFS_TYPE_MRC_CACHE,
-						&region_size);
+	*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
+							CBFS_TYPE_MRC_CACHE,
+							&region_size);
 
 	return region_size;
 }
diff --git a/src/drivers/intel/fsp1_1/fastboot_cache.c b/src/drivers/intel/fsp1_1/fastboot_cache.c
index e8651bb..4ad17e8 100644
--- a/src/drivers/intel/fsp1_1/fastboot_cache.c
+++ b/src/drivers/intel/fsp1_1/fastboot_cache.c
@@ -63,8 +63,7 @@ static int is_mrc_cache(struct mrc_data_container *mrc_cache)
 static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 {
 	size_t region_size;
-	*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						"mrc.cache",
+	*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
 						CBFS_TYPE_MRC_CACHE,
 						&region_size);
 
diff --git a/src/drivers/pc80/mc146818rtc.c b/src/drivers/pc80/mc146818rtc.c
index 4f034a2..fe2e1f3 100644
--- a/src/drivers/pc80/mc146818rtc.c
+++ b/src/drivers/pc80/mc146818rtc.c
@@ -212,8 +212,8 @@ enum cb_err get_option(void *dest, const char *name)
 	namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
 
 	/* find the requested entry record */
-	ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
-				   CBFS_COMPONENT_CMOS_LAYOUT, NULL);
+	ct = cbfs_boot_map_with_leak("cmos_layout.bin",
+					CBFS_COMPONENT_CMOS_LAYOUT, NULL);
 	if (!ct) {
 		printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
 						"Options are disabled\n");
@@ -295,8 +295,8 @@ enum cb_err set_option(const char *name, void *value)
 	namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
 
 	/* find the requested entry record */
-	ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
-				   CBFS_COMPONENT_CMOS_LAYOUT, NULL);
+	ct = cbfs_boot_map_with_leak("cmos_layout.bin",
+					CBFS_COMPONENT_CMOS_LAYOUT, NULL);
 	if (!ct) {
 		printk(BIOS_ERR, "cmos_layout.bin could not be found. "
 				 "Options are disabled\n");
diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c
index d2a3c66..b95a0a2 100644
--- a/src/drivers/spi/spi_flash.c
+++ b/src/drivers/spi/spi_flash.c
@@ -7,6 +7,7 @@
  * Licensed under the GPL-2 or later.
  */
 
+#include <boot_device.h>
 #include <cbfs.h>
 #include <cpu/x86/smm.h>
 #include <delay.h>
@@ -407,10 +408,8 @@ void lb_spi_flash(struct lb_header *header)
 	flash->size = sizeof(*flash);
 
 	/* Try to get the flash device if not loaded yet */
-	if (!spi_flash_dev) {
-		struct cbfs_media media;
-		init_default_cbfs_media(&media);
-	}
+	if (!spi_flash_dev)
+		boot_device_init();
 
 	if (spi_flash_dev) {
 		flash->flash_size = spi_flash_dev->size;
diff --git a/src/include/cbfs.h b/src/include/cbfs.h
index 1d7917c..f914ffb 100644
--- a/src/include/cbfs.h
+++ b/src/include/cbfs.h
@@ -1,14 +1,8 @@
 /*
  * This file is part of the coreboot project.
  *
- * Copyright (C) 2008 Jordan Crouse <jordan at cosmicpenguin.net>
- * Copyright (C) 2013-2015 Google, Inc.
+ * Copyright 2015 Google Inc.
  *
- * This file is dual-licensed. You can choose between:
- *   - The GNU GPL, version 2, as published by the Free Software Foundation
- *   - The revised BSD license (without advertising clause)
- *
- * ---------------------------------------------------------------------------
  * 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.
@@ -20,75 +14,100 @@
  *
  * 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
- * ---------------------------------------------------------------------------
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ---------------------------------------------------------------------------
+ * Foundation, Inc.
  */
 
 #ifndef _CBFS_H_
 #define _CBFS_H_
 
-#include <cbfs_core.h>
-
-int init_backing_media(struct cbfs_media **media, struct cbfs_media *backing);
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
-			  uint16_t device, void * dest);
-void *cbfs_load_stage(struct cbfs_media *media, const char *name);
-void *cbfs_load_stage_by_offset(struct cbfs_media *media, ssize_t offset);
-/* Load a stage from a prog structure. Returns < 0 on error. 0 on success. */
-struct prog;
-int cbfs_load_prog_stage(struct cbfs_media *media, struct prog *prog);
-int cbfs_load_prog_stage_by_offset(struct cbfs_media *media,
-					struct prog *prog, ssize_t offset);
-
-/* Simple buffer for streaming media. */
-struct cbfs_simple_buffer {
-	char *buffer;
-	size_t allocated;
-	size_t size;
-	size_t last_allocate;
-};
+#include <cbfs_serialized.h>
+#include <program_loading.h>
+#include <region.h>
+
+/*
+ * 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
+ */
 
-void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
-			     struct cbfs_media *media,
-			     size_t offset, size_t count);
+/* Descriptor for cbfs lookup operations. */
+struct cbfsd;
+/* File handle representing a cbfs file. */
+struct cbfsf;
 
-void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
-			       const void *address);
+/***********************************************
+ * Perform CBFS operations on the boot device. *
+ ***********************************************/
 
+/* 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);
+/* 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);
+/* 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);
 
-/*
- * Defined in individual arch / board implementation.
- *
- * it returns 0 on success and non-zero on error.
- */
-int init_default_cbfs_media(struct cbfs_media *media);
+/*************************************************
+ * 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. *
+ *******************************************************/
 
-#if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES))
+/* 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);
-#else
-static inline void cbfs_set_header_offset(size_t offset) {}
-#endif
+size_t cbfs_get_header_offset(void);
+
+/*****************************************************************
+ * Support structures and functions. Direct field access should  *
+ * only be done by implementers of cbfs regions -- Not the above *
+ * API.                                                          *
+ *****************************************************************/
+
+struct cbfsd {
+	const struct region_device *rdev;
+	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. */
+	size_t align;
+	/* CBFS starts at the following offset within the boot region. */
+	size_t offset;
+	/* CBFS size. */
+	size_t size;
+};
+
+/* Return < 0 on error otherwise props are filled out accordingly. */
+int cbfs_boot_region_properties(struct cbfs_props *props);
+
 #endif
diff --git a/src/include/cbfs_core.h b/src/include/cbfs_core.h
deleted file mode 100644
index c71cc26..0000000
--- a/src/include/cbfs_core.h
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2008 Jordan Crouse <jordan at cosmicpenguin.net>
- * Copyright (C) 2012 Google, Inc.
- * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
- *
- * This file is dual-licensed. You can choose between:
- *   - The GNU GPL, version 2, as published by the Free Software Foundation
- *   - The revised BSD license (without advertising clause)
- *
- * ---------------------------------------------------------------------------
- * 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
- * ---------------------------------------------------------------------------
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ---------------------------------------------------------------------------
- */
-
-#ifndef _CBFS_CORE_H_
-#define _CBFS_CORE_H_
-
-#include <endian.h>
-#include <stddef.h>
-#include <stdint.h>
-#include <cbfs_serialized.h>
-
-#define CBFS_HEADER_INVALID_ADDRESS	((void*)(0xffffffff))
-
-#define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file))
-#define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) )
-
-/*
- * ROMCC does not understand uint64_t, so we hide future definitions as they are
- * unlikely to be ever needed from ROMCC
- */
-#ifndef __ROMCC__
-
-#define CBFS_MEDIA_INVALID_MAP_ADDRESS	((void*)(0xffffffff))
-#define CBFS_DEFAULT_MEDIA		((void*)(0x0))
-
-/* Media for CBFS to load files. */
-struct cbfs_media {
-
-	/* implementation dependent context, to hold resource references */
-	void *context;
-
-	/* opens media and returns 0 on success, -1 on failure */
-	int (*open)(struct cbfs_media *media);
-
-	/* returns number of bytes read from media into dest, starting from
-	 * offset for count of bytes */
-	size_t (*read)(struct cbfs_media *media, void *dest, size_t offset,
-		       size_t count);
-
-	/* returns a pointer to memory with count of bytes from media source
-	 * starting from offset, or CBFS_MEDIA_INVALID_MAP_ADDRESS on failure.
-	 * Note: mapped data can't be free unless unmap is called, even if you
-	 * do close first. */
-	void * (*map)(struct cbfs_media *media, size_t offset, size_t count);
-
-	/* returns NULL and releases the memory by address, which was allocated
-	 * by map */
-	void * (*unmap)(struct cbfs_media *media, const void *address);
-
-	/* closes media and returns 0 on success, -1 on failure. */
-	int (*close)(struct cbfs_media *media);
-};
-
-/*
- * Locate file by name and fill in cbfs_file in host byte order. Returns
- * < 0 on error, else the offset of the file data.
- */
-ssize_t cbfs_locate_file(struct cbfs_media *media, struct cbfs_file *file,
-				const char *name);
-
-/* Read count bytes at offset into dest. Return number of bytes read. */
-size_t cbfs_read(struct cbfs_media *media, void *dest, size_t offset,
-			size_t count);
-
-/* returns pointer to a file entry inside CBFS or NULL */
-struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name);
-
-/* returns pointer to file content inside CBFS after if type is correct */
-void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
-			    int type, size_t *sz);
-
-/* returns decompressed size on success, 0 on failure */
-int cbfs_decompress(int algo, void *src, void *dst, int len);
-
-/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
- *  on failure */
-const struct cbfs_header *cbfs_get_header(struct cbfs_media *media);
-
-#endif /* __ROMCC__ */
-
-#endif
diff --git a/src/include/rmodule.h b/src/include/rmodule.h
index 242ed91..5dc795c 100644
--- a/src/include/rmodule.h
+++ b/src/include/rmodule.h
@@ -54,7 +54,7 @@ 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 cbfs_stage;
+struct cbfsf;
 struct prog;
 
 struct rmod_stage_load {
@@ -63,7 +63,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 cbfs_stage *stage);
+int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh);
 int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl);
 
 struct rmodule {
diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc
index 6d685ff..1160b28 100644
--- a/src/lib/Makefile.inc
+++ b/src/lib/Makefile.inc
@@ -19,7 +19,8 @@
 subdirs-y += loaders
 
 bootblock-y += prog_ops.c
-bootblock-y += cbfs.c cbfs_core.c
+bootblock-y += cbfs.c
+bootblock-y += cbfs_boot_props.c
 bootblock-$(CONFIG_COMMON_CBFS_SPI_WRAPPER) += cbfs_spi.c
 bootblock-$(CONFIG_GENERIC_GPIO_LIB) += gpio.c
 
@@ -37,9 +38,9 @@ bootblock-y += fmap.c
 verstage-y += prog_ops.c
 verstage-y += delay.c
 verstage-y += cbfs.c
-verstage-y += cbfs_core.c
 verstage-y += halt.c
 verstage-y += fmap.c
+verstage-y += cbfs_boot_props.c
 verstage-y += memcmp.c
 verstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 verstage-y += region.c
@@ -66,7 +67,8 @@ $(foreach arch,$(ARCH_SUPPORTED),\
 
 romstage-y += fmap.c
 romstage-$(CONFIG_I2C_TPM) += delay.c
-romstage-y += cbfs.c cbfs_core.c
+romstage-y += cbfs.c
+romstage-y += cbfs_boot_props.c
 romstage-$(CONFIG_COMMON_CBFS_SPI_WRAPPER) += cbfs_spi.c
 romstage-$(CONFIG_COMPRESS_RAMSTAGE) += lzma.c lzmadecode.c
 romstage-$(CONFIG_PRIMITIVE_MEMTEST) += primitive_memtest.c
@@ -100,7 +102,8 @@ smm-$(CONFIG_SMM_TSEG) += malloc.c
 ramstage-y += delay.c
 ramstage-y += fallback_boot.c
 ramstage-y += compute_ip_checksum.c
-ramstage-y += cbfs.c cbfs_core.c
+ramstage-y += cbfs.c
+ramstage-y += cbfs_boot_props.c
 ramstage-$(CONFIG_COMMON_CBFS_SPI_WRAPPER) += cbfs_spi.c
 ramstage-y += lzma.c lzmadecode.c
 ramstage-y += stack.c
@@ -150,10 +153,11 @@ ramstage-y += region.c
 romstage-y += boot_device.c
 ramstage-y += boot_device.c
 
+smm-y += cbfs.c memcmp.c
+smm-y += region.c
 smm-y += region.c
 smm-y += boot_device.c
 smm-y += fmap.c
-smm-y += cbfs.c cbfs_core.c memcmp.c
 smm-$(CONFIG_COMPILER_GCC) += gcc.c
 
 bootblock-y += version.c
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index 8239f07..fb012d1 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -1,8 +1,8 @@
 /*
  * This file is part of the coreboot project.
  *
- * Copyright (C) 2008, Jordan Crouse <jordan at cosmicpenguin.net>
- * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
+ * Copyright (C) 2011 secunet Security Networks AG
+ * 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
@@ -15,195 +15,266 @@
  *
  * 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
+ * Foundation, Inc.
  */
 
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <boot_device.h>
+#include <cbfs.h>
+#include <endian.h>
+#include <lib.h>
+#include <symbols.h>
+
+#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
+#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
+#if IS_ENABLED(CONFIG_DEBUG_CBFS)
+#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
+#else
+#define DEBUG(x...)
+#endif
 
-#include <program_loading.h>
-#include "cbfs_core.h"
-
-#ifndef __SMM__
-static inline int tohex4(unsigned int c)
+void cbfs_set_header_offset(size_t offset)
 {
-	return (c <= 9) ? (c + '0') : (c - 10 + 'a');
+	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)) {
+		_cbfs_header_offset[0] = offset;
+		LOG("header set to: %#zx\n", offset);
+	}
 }
 
-static void tohex16(unsigned int val, char* dest)
+size_t cbfs_get_header_offset(void)
 {
-	dest[0] = tohex4(val>>12);
-	dest[1] = tohex4((val>>8) & 0xf);
-	dest[2] = tohex4((val>>4) & 0xf);
-	dest[3] = tohex4(val & 0xf);
+	if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES))
+		return _cbfs_header_offset[0];
+	return 0;
 }
 
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
-			  uint16_t device, void *dest)
+int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
 {
-	char name[17] = "pciXXXX,XXXX.rom";
-	struct cbfs_optionrom *orom;
-	uint8_t *src;
+	struct cbfsd cbfs;
+	struct region_device rdev;
+	const struct region_device *boot_dev;
+	struct cbfs_props props;
 
-	tohex16(vendor, name+3);
-	tohex16(device, name+8);
+	boot_device_init();
 
-	orom = (struct cbfs_optionrom *)
-	  cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
+	if (cbfs_boot_region_properties(&props))
+		return -1;
 
-	if (orom == NULL)
-		return NULL;
+	/* All boot CBFS operations are performed using the RO devie. */
+	boot_dev = boot_device_ro();
 
-	/* They might have specified a dest address. If so, we can decompress.
-	 * If not, there's not much hope of decompressing or relocating the rom.
-	 * in the common case, the expansion rom is uncompressed, we
-	 * pass 0 in for the dest, and all we have to do is find the rom and
-	 * return a pointer to it.
-	 */
+	if (boot_dev == NULL)
+		return -1;
 
-	/* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
-	src = (uint8_t *)orom; // + sizeof(struct cbfs_optionrom);
+	if (rdev_chain(&rdev, boot_dev, props.offset, props.size))
+		return -1;
 
-	if (! dest)
-		return src;
+	cbfs.rdev = &rdev;
+	cbfs.align = props.align;
 
-	if (!cbfs_decompress(ntohl(orom->compression),
-			     src,
-			     dest,
-			     ntohl(orom->len)))
+	return cbfs_locate(fh, &cbfs, name, type);
+}
+
+void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
+{
+	struct cbfsf fh;
+	size_t fsize;
+
+	if (cbfs_boot_locate(&fh, name, &type))
 		return NULL;
 
-	return dest;
+	fsize = cbfs_file_size(&fh);
+
+	if (size != NULL)
+		*size = fsize;
+
+	return cbfs_file_mmap(&fh, 0, fsize);
 }
 
-int cbfs_load_prog_stage_by_offset(struct cbfs_media *media,
-					struct prog *prog, ssize_t offset)
+void *cbfs_file_mmap(const struct cbfsf *fh, size_t offset, size_t size)
 {
-	struct cbfs_stage stage;
-	struct cbfs_media backing_store;
+	return rdev_mmap(&fh->rdev, offset, size);
+}
 
-	if (init_backing_media(&media, &backing_store))
-		return -1;
+int cbfs_file_munmap(const struct cbfsf *fh, void *mapping)
+{
+	return rdev_munmap(&fh->rdev, mapping);
+}
 
-	if (cbfs_read(media, &stage, offset, sizeof(stage)) != sizeof(stage)) {
-		ERROR("ERROR: failed to read stage header\n");
-		return -1;
-	}
+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)
+{
+	size_t offset;
+	const struct region_device *rd;
+	size_t align;
+
+	offset = 0;
+	rd = cbfs->rdev;
+	align = cbfs->align;
+
+	LOG("Locating '%s'\n", name);
+
+	/* Try to scan the entire cbfs region looking for file name. */
+	while (1) {
+		struct cbfs_file file;
+		const size_t fsz = sizeof(file);
+		char *fname;
+		int name_match;
+		size_t datasz;
+
+		DEBUG("Checking offset %zx\n", offset);
+
+		/* Can't read file. Nothing else to do but bail out. */
+		if (rdev_readat(rd, &file, offset, fsz) != fsz)
+			break;
+
+		if (memcmp(file.magic, CBFS_FILE_MAGIC, sizeof(file.magic))) {
+			offset++;
+			offset = ALIGN_UP(offset, align);
+			continue;
+		}
 
-	LOG("loading stage from %#zx @ 0x%llx (%d bytes), entry @ 0x%llx\n",
-	    offset, stage.load, stage.memlen, stage.entry);
+		file.len = ntohl(file.len);
+		file.type = ntohl(file.type);
+		file.offset = ntohl(file.offset);
 
-	/* Stages rely the below clearing so that the bss is initialized. */
-	memset((void *)(uintptr_t)stage.load, 0, stage.memlen);
+		/* See if names match. */
+		fname = rdev_mmap(rd, offset + fsz, file.offset - fsz);
 
-	if (stage.compression == CBFS_COMPRESS_NONE) {
-		if (cbfs_read(media, (void *)(uintptr_t)stage.load,
-			      offset + sizeof(stage), stage.len) != stage.len) {
-			ERROR("ERROR: Reading stage failed.\n");
-			return -1;
+		if (fname == NULL)
+			break;
+
+		name_match = !strcmp(fname, name);
+		rdev_munmap(rd, fname);
+
+		if (!name_match) {
+			DEBUG(" Unmatched '%s' at %zx\n", fname, offset);
+			offset += file.offset + file.len;
+			offset = ALIGN_UP(offset, align);
+			continue;
 		}
-	} else {
-		void *data = media->map(media, offset + sizeof(stage),
-					stage.len);
-		if (data == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-			ERROR("ERROR: Mapping stage failed.\n");
-			return -1;
+
+		if (type != NULL && *type != file.type) {
+			DEBUG(" Unmatched type %x at %zx\n", file.type, offset);
+			offset += file.offset + file.len;
+			offset = ALIGN_UP(offset, align);
+			continue;
 		}
-		if (!cbfs_decompress(stage.compression, data,
-				    (void *)(uintptr_t)stage.load, stage.len))
-			return -1;
-		media->unmap(media, data);
-	}
 
-	arch_segment_loaded(stage.load, stage.memlen, SEG_FINAL);
-	DEBUG("stage loaded\n");
+		LOG("Found @ offset %zx size %x\n", offset, file.len);
+		/* File and type match. Create a chained region_device to
+		 * represent the cbfs file. */
+		offset += file.offset;
+		datasz = file.len;
+		if (rdev_chain(&fh->rdev, rd, offset, datasz))
+			break;
+
+		/* Success. */
+		return 0;
+	}
 
-	prog_set_area(prog, (void *)(uintptr_t)stage.load, stage.memlen);
-	prog_set_entry(prog, (void *)(uintptr_t)stage.entry, NULL);
+	LOG("'%s' not found.\n", name);
+	return -1;
+}
 
-	return 0;
+static size_t inflate(void *src, void *dst)
+{
+	if (ENV_BOOTBLOCK || ENV_VERSTAGE)
+		return 0;
+	if (ENV_ROMSTAGE && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE))
+		return 0;
+	return ulzma(src, dst);
 }
 
-int cbfs_load_prog_stage(struct cbfs_media *media, struct prog *prog)
+static inline int tohex4(unsigned int c)
 {
-	struct cbfs_file file;
-	ssize_t offset;
-	struct cbfs_media backing_store;
+	return (c <= 9) ? (c + '0') : (c - 10 + 'a');
+}
 
-	if (init_backing_media(&media, &backing_store))
-		return -1;
+static void tohex16(unsigned int val, char* dest)
+{
+	dest[0] = tohex4(val>>12);
+	dest[1] = tohex4((val>>8) & 0xf);
+	dest[2] = tohex4((val>>4) & 0xf);
+	dest[3] = tohex4(val & 0xf);
+}
 
-	offset = cbfs_locate_file(media, &file, prog->name);
-	if (offset < 0 || file.type != CBFS_TYPE_STAGE)
-		return -1;
+void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
+{
+	char name[17] = "pciXXXX,XXXX.rom";
 
-	if (cbfs_load_prog_stage_by_offset(media, prog, offset) < 0)
-		return -1;
+	tohex16(vendor, name+3);
+	tohex16(device, name+8);
 
-	return 0;
+	return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
 }
 
-void *cbfs_load_stage_by_offset(struct cbfs_media *media, ssize_t offset)
+int cbfs_boot_load_stage(struct prog *stage)
 {
-	struct prog prog = {
-		.name = NULL,
-	};
+	struct cbfsf fh;
+	uint32_t type = CBFS_TYPE_STAGE;
 
-	if (cbfs_load_prog_stage_by_offset(media, &prog, offset) < 0)
-		return (void *)-1;
+	if (cbfs_boot_locate(&fh, stage->name, &type))
+		return -1;
 
-	return prog_entry(&prog);
+	return cbfs_file_stage_load(&fh, stage);
 }
 
-void *cbfs_load_stage(struct cbfs_media *media, const char *name)
+int cbfs_file_stage_load(const struct cbfsf *fh, struct prog *pstage)
 {
-	struct prog prog = {
-		.name = name,
-	};
+	struct cbfs_stage stage;
+	uint8_t *load;
+	void *entry;
+	size_t fsize;
+	size_t foffset;
 
-	if (cbfs_load_prog_stage(media, &prog) < 0)
-		return (void *)-1;
+	if (cbfs_file_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
+		return 0;
 
-	return prog_entry(&prog);
-}
+	fsize = cbfs_file_size(fh);
+	fsize -= sizeof(stage);
+	foffset = 0;
+	foffset += sizeof(stage);
 
-/* Simple buffer */
-
-void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
-			     struct cbfs_media *media,
-			     size_t offset, size_t count) {
-	void *address = buffer->buffer + buffer->allocated;
-	DEBUG("simple_buffer_map(offset=%zd, count=%zd): "
-	      "allocated=%zd, size=%zd, last_allocate=%zd\n",
-	    offset, count, buffer->allocated, buffer->size,
-	    buffer->last_allocate);
-	if (buffer->allocated + count > buffer->size) {
-		ERROR("simple_buffer: no room to map %zd bytes from %#zx\n",
-		      count, offset);
-		return CBFS_MEDIA_INVALID_MAP_ADDRESS;
-	}
-	if (media->read(media, address, offset, count) != count) {
-		ERROR("simple_buffer: fail to read %zd bytes from 0x%zx\n",
-		      count, offset);
-		return CBFS_MEDIA_INVALID_MAP_ADDRESS;
-	}
-	buffer->allocated += count;
-	buffer->last_allocate = count;
-	return address;
-}
+	assert(fsize == stage.len);
 
-void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
-			       const void *address) {
-	// TODO Add simple buffer management so we can free more than last
-	// allocated one.
-	DEBUG("simple_buffer_unmap(address=0x%p): "
-	      "allocated=%zd, size=%zd, last_allocate=%zd\n",
-	    address, buffer->allocated, buffer->size,
-	    buffer->last_allocate);
-	if ((buffer->buffer + buffer->allocated - buffer->last_allocate) ==
-	    address) {
-		buffer->allocated -= buffer->last_allocate;
-		buffer->last_allocate = 0;
-	}
-	return NULL;
-}
+	/* Note: cbfs_stage fields are currently in the endianness of the
+	 * running processor. */
+	load = (void *)(uintptr_t)stage.load;
+	entry = (void *)(uintptr_t)stage.entry;
 
-#endif
+	if (stage.compression == CBFS_COMPRESS_NONE) {
+		if (cbfs_file_readat(fh, load, foffset, fsize) != fsize)
+			return -1;
+	} else if (stage.compression == CBFS_COMPRESS_LZMA) {
+		void *map = cbfs_file_mmap(fh, foffset, fsize);
+
+		if (map == NULL)
+			return -1;
+
+		fsize = inflate(map, load);
+
+		cbfs_file_munmap(fh, map);
+
+		if (!fsize)
+			return -1;
+	} else
+		return -1;
+
+	/* Clear area not covered by file. */
+	memset(&load[fsize], 0, stage.memlen - fsize);
+
+	arch_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
+	prog_set_area(pstage, load, stage.memlen);
+	prog_set_entry(pstage, entry, NULL);
+
+	return 0;
+}
diff --git a/src/lib/cbfs_boot_props.c b/src/lib/cbfs_boot_props.c
new file mode 100644
index 0000000..7e7bb8a
--- /dev/null
+++ b/src/lib/cbfs_boot_props.c
@@ -0,0 +1,70 @@
+/*
+ * 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 <boot_device.h>
+#include <cbfs.h>
+#include <console/console.h>
+#include <endian.h>
+#include <region.h>
+
+/* This function is marked as weak to allow a particular platform to
+ * override the logic. This implementation should work for most devices. */
+int __attribute__((weak)) cbfs_boot_region_properties(struct cbfs_props *props)
+{
+	struct cbfs_header header;
+	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);
+	if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0)
+		return -1;
+
+	offset = base_offset + rel_offset;
+	if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0)
+		return -1;
+
+	header.magic = ntohl(header.magic);
+	header.romsize = ntohl(header.romsize);
+	header.align = ntohl(header.align);
+	header.offset = ntohl(header.offset);
+
+	if (header.magic != CBFS_HEADER_MAGIC)
+		return -1;
+
+	props->align = header.align;
+	props->offset = header.offset;
+	props->size = header.romsize;
+	props->size -= props->offset;
+
+	printk(BIOS_SPEW, "CBFS @ %zx size %zx\n", props->offset, props->size);
+
+	return 0;
+}
diff --git a/src/lib/cbfs_core.c b/src/lib/cbfs_core.c
deleted file mode 100644
index 9f45b3c..0000000
--- a/src/lib/cbfs_core.c
+++ /dev/null
@@ -1,324 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2011 secunet Security Networks AG
- * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/* The CBFS core requires a couple of #defines or functions to adapt it to the
- * target environment:
- *
- * CBFS_CORE_WITH_LZMA (must be #define)
- *      if defined, ulzma() must exist for decompression of data streams
- *
- * ERROR(x...)
- *      print an error message x (in printf format)
- *
- * LOG(x...)
- *      print a message x (in printf format)
- *
- * DEBUG(x...)
- *      print a debug message x (in printf format)
- *
- */
-
-#include <cbfs.h>
-#include <string.h>
-#include <symbols.h>
-
-#if IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)
-void cbfs_set_header_offset(size_t offset)
-{
-	_cbfs_header_offset[0] = offset;
-	LOG("header set to: %#zx\n", offset);
-}
-
-static size_t get_header_offset(void)
-{
-	return _cbfs_header_offset[0];
-}
-#else
-static size_t get_header_offset(void)
-{
-	return 0;
-}
-#endif
-
-#include "cbfs_core.h"
-
-/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
- *  on failure */
-const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
-{
-	size_t offset;
-	const struct cbfs_header *header;
-	struct cbfs_media default_media;
-
-	if (media == CBFS_DEFAULT_MEDIA) {
-		media = &default_media;
-		if (init_default_cbfs_media(media) != 0) {
-			ERROR("Failed to initialize default media.\n");
-			return CBFS_HEADER_INVALID_ADDRESS;
-		}
-	}
-	media->open(media);
-
-	/* TODO: allow negative offsets from the end of the CBFS image at media
-	 * layer (like libpayload) so we can combine these two cases. */
-	if (IS_ENABLED(CONFIG_ARCH_X86)) {
-		offset = *(int32_t *)(uintptr_t)0xfffffffc;
-		header = media->map(media, offset, sizeof(*header));
-	} else {
-
-		offset = get_header_offset();
-
-		if (!offset) {
-			int32_t rel_offset;
-			size_t cbfs_top = CONFIG_CBFS_SIZE;
-			DEBUG("CBFS top at offset: 0x%zx\n", cbfs_top);
-			if (!media->read(media, &rel_offset, cbfs_top -
-					 sizeof(int32_t),
-					 sizeof(int32_t))) {
-				ERROR("Could not read master header offset!\n");
-				media->close(media);
-				return CBFS_HEADER_INVALID_ADDRESS;
-			}
-			offset = cbfs_top + rel_offset;
-		}
-		header = media->map(media, offset, sizeof(*header));
-	}
-	DEBUG("CBFS header offset: 0x%zx/0x%x\n", offset, CONFIG_ROM_SIZE);
-	media->close(media);
-
-	if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-		ERROR("Failed to load CBFS header from 0x%zx\n", offset);
-		return CBFS_HEADER_INVALID_ADDRESS;
-	}
-
-	if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
-		ERROR("Could not find valid CBFS master header at %#zx: "
-		      "magic %#.8x vs %#.8x.\n", offset, CBFS_HEADER_MAGIC,
-		      ntohl(header->magic));
-		if (header->magic == 0xffffffff) {
-			ERROR("Maybe ROM is not mapped properly?\n");
-		}
-		return CBFS_HEADER_INVALID_ADDRESS;
-	}
-	return header;
-}
-
-
-int init_backing_media(struct cbfs_media **media, struct cbfs_media *backing)
-{
-	if (*media == CBFS_DEFAULT_MEDIA) {
-		*media = backing;
-		if (init_default_cbfs_media(*media) != 0) {
-			ERROR("Failed to initialize default media.\n");
-			return -1;
-		}
-	}
-	return 0;
-}
-
-/* public API starts here*/
-ssize_t cbfs_locate_file(struct cbfs_media *media, struct cbfs_file *file,
-				const char *name)
-{
-	const char *file_name;
-	uint32_t offset, align, romsize, name_len;
-	const struct cbfs_header *header;
-	struct cbfs_media default_media;
-
-	if (init_backing_media(&media, &default_media))
-		return -1;
-
-	if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media)))
-		return -1;
-
-	// Logical offset (for source media) of first file.
-	offset = ntohl(header->offset);
-	align = ntohl(header->align);
-	romsize = ntohl(header->romsize);
-
-	// TODO Add a "size" in CBFS header for a platform independent way to
-	// determine the end of CBFS data.
-#if defined(CONFIG_ARCH_X86) && CONFIG_ARCH_X86
-	// resolve actual length of ROM used for CBFS components
-	// the bootblock size was not taken into account
-	romsize -= ntohl(header->bootblocksize);
-
-	// fine tune the length to handle alignment positioning.
-	// using (bootblock size) % align, to derive the
-	// number of bytes the bootblock is off from the alignment size.
-	if ((ntohl(header->bootblocksize) % align))
-		romsize -= (align - (ntohl(header->bootblocksize) % align));
-	else
-		romsize -= 1;
-#endif
-
-	DEBUG("CBFS location: 0x%x~0x%x, align: %d\n", offset, romsize, align);
-	DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
-
-	media->open(media);
-	while (offset < romsize &&
-	       media->read(media, file, offset, sizeof(*file)) == sizeof(*file)) {
-		if (memcmp(CBFS_FILE_MAGIC, file->magic,
-			   sizeof(file->magic)) != 0) {
-			uint32_t new_align = align;
-			if (offset % align)
-				new_align += align - (offset % align);
-			LOG("WARNING: No file header found at 0x%x - "
-			      "try next aligned address: 0x%x.\n", offset,
-			      offset + new_align);
-			offset += new_align;
-			continue;
-		}
-
-		file->len = ntohl(file->len);
-		file->type= ntohl(file->type);
-		file->offset = ntohl(file->offset);
-
-		name_len = file->offset - sizeof(*file);
-		DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,
-		      name_len);
-
-		// load file name (arbitrary length).
-		file_name = (const char *)media->map(
-				media, offset + sizeof(*file), name_len);
-		if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-			ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
-		} else if (strcmp(file_name, name) == 0) {
-			DEBUG("Found file (offset=0x%x, len=%d).\n",
-			    offset + file->offset, file->len);
-			media->unmap(media, file_name);
-			return offset + file->offset;
-		} else {
-			DEBUG(" (unmatched file @0x%x: %s)\n", offset,
-			      file_name);
-			media->unmap(media, file_name);
-		}
-
-		// Move to next file.
-		offset += file->len + file->offset;
-		if (offset % align)
-			offset += align - (offset % align);
-	}
-	media->close(media);
-	LOG("WARNING: '%s' not found.\n", name);
-	return -1;
-}
-
-size_t cbfs_read(struct cbfs_media *media, void *dest, size_t offset,
-			size_t count)
-{
-	struct cbfs_media default_media;
-	size_t nread;
-
-	if (init_backing_media(&media, &default_media))
-		return 0;
-
-	media->open(media);
-	nread = media->read(media, dest, offset, count);
-	media->close(media);
-
-	return nread;
-}
-
-struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
-{
-	struct cbfs_media default_media;
-	struct cbfs_file file, *file_ptr;
-	ssize_t offset;
-
-	if (init_backing_media(&media, &default_media))
-		return NULL;
-
-	offset = cbfs_locate_file(media, &file, name);
-	if (offset < 0)
-		return NULL;
-
-	/* Map both the metadata and the file contents. */
-	media->open(media);
-	offset -= file.offset;
-	file_ptr = media->map(media, offset, file.offset + file.len);
-	media->close(media);
-
-	if (file_ptr == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-		ERROR("ERROR: Mapping %s failed.\n", name);
-		return NULL;
-	}
-
-	return file_ptr;
-}
-
-void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
-			    int type, size_t *sz)
-{
-	struct cbfs_file *file = cbfs_get_file(media, name);
-
-	if (sz)
-		*sz = 0;
-
-	if (file == NULL) {
-		ERROR("Could not find file '%s'.\n", name);
-		return NULL;
-	}
-
-	if (ntohl(file->type) != type) {
-		ERROR("File '%s' is of type %x, but we requested %x.\n", name,
-		      ntohl(file->type), type);
-		return NULL;
-	}
-
-	if (sz)
-		*sz = ntohl(file->len);
-
-	return (void *)CBFS_SUBHEADER(file);
-}
-
-int cbfs_decompress(int algo, void *src, void *dst, int len)
-{
-	switch (algo) {
-		case CBFS_COMPRESS_NONE:
-			/* Reads need to be aligned at 4 bytes to avoid
-			   poor flash performance.  */
-			while (len && ((uintptr_t)src & 3)) {
-				*(u8*)dst++ = *(u8*)src++;
-				len--;
-			}
-			memmove(dst, src, len);
-			return len;
-#ifdef CBFS_CORE_WITH_LZMA
-		case CBFS_COMPRESS_LZMA:
-			return ulzma(src, dst);
-#endif
-		default:
-			ERROR("tried to decompress %d bytes with algorithm #%x,"
-			      "but that algorithm id is unsupported.\n", len,
-			      algo);
-			return 0;
-	}
-}
diff --git a/src/lib/cbfs_core.h b/src/lib/cbfs_core.h
deleted file mode 100644
index f9aeaa8..0000000
--- a/src/lib/cbfs_core.h
+++ /dev/null
@@ -1,47 +0,0 @@
-#ifndef __LIB_CBFS_CORE
-#define __LIB_CBFS_CORE
-
-#include <cbfs.h>
-#include <string.h>
-
-#ifdef LIBPAYLOAD
-# include <libpayload-config.h>
-# ifdef CONFIG_LZMA
-#  include <lzma.h>
-#  define CBFS_CORE_WITH_LZMA
-# endif
-# define CBFS_MINI_BUILD
-#elif defined(__SMM__)
-# define CBFS_MINI_BUILD
-#elif defined(__BOOTBLOCK__)
-  /* No LZMA in boot block. */
-#elif defined(__VERSTAGE__)
-  /* No LZMA in verstage. */
-#elif defined(__PRE_RAM__) && !CONFIG_COMPRESS_RAMSTAGE
-  /* No LZMA in romstage if ramstage is not compressed. */
-#else
-# define CBFS_CORE_WITH_LZMA
-# include <lib.h>
-#endif
-
-#include <cbfs.h>
-#include <string.h>
-#include <cbmem.h>
-
-#ifdef LIBPAYLOAD
-# include <stdio.h>
-# define DEBUG(x...)
-# define LOG(x...) printf(x)
-# define ERROR(x...) printf(x)
-#else
-# include <console/console.h>
-# define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
-# define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
-# if CONFIG_DEBUG_CBFS
-#  define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
-# else
-#  define DEBUG(x...)
-# endif
-#endif
-
-#endif /* __LIB_CBFS_CORE */
diff --git a/src/lib/cbfs_spi.c b/src/lib/cbfs_spi.c
index 6637cdc..b11d92b 100644
--- a/src/lib/cbfs_spi.c
+++ b/src/lib/cbfs_spi.c
@@ -24,8 +24,6 @@
  */
 
 #include <boot_device.h>
-#include <cbfs.h>
-#include <region.h>
 #include <spi_flash.h>
 #include <symbols.h>
 
@@ -69,73 +67,3 @@ const struct region_device *boot_device_ro(void)
 
 	return &mdev.rdev;
 }
-
-static int cbfs_media_open(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static int cbfs_media_close(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static size_t cbfs_media_read(struct cbfs_media *media,
-			      void *dest, size_t offset,
-			      size_t count)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static void *cbfs_media_map(struct cbfs_media *media,
-			    size_t offset, size_t count)
-{
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *cbfs_media_unmap(struct cbfs_media *media,
-			       const void *address)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-
-	if (media->context == NULL)
-		return -1;
-
-	media->open = cbfs_media_open;
-	media->close = cbfs_media_close;
-	media->read = cbfs_media_read;
-	media->map = cbfs_media_map;
-	media->unmap = cbfs_media_unmap;
-
-	return 0;
-}
diff --git a/src/lib/coreboot_table.c b/src/lib/coreboot_table.c
index 4af50ed..1feb68e 100644
--- a/src/lib/coreboot_table.c
+++ b/src/lib/coreboot_table.c
@@ -397,8 +397,8 @@ unsigned long write_coreboot_table(
 
 #if CONFIG_USE_OPTION_TABLE
 	{
-		struct cmos_option_table *option_table = cbfs_get_file_content(
-				CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
+		struct cmos_option_table *option_table =
+			cbfs_boot_map_with_leak("cmos_layout.bin",
 				CBFS_COMPONENT_CMOS_LAYOUT, NULL);
 		if (option_table) {
 			struct lb_record *rec_dest = lb_new_record(head);
diff --git a/src/lib/loaders/cbfs_payload_loader.c b/src/lib/loaders/cbfs_payload_loader.c
index 3928613..fffb558 100644
--- a/src/lib/loaders/cbfs_payload_loader.c
+++ b/src/lib/loaders/cbfs_payload_loader.c
@@ -24,10 +24,9 @@ static int cbfs_locate_payload(struct prog *payload)
 {
 	void *buffer;
 	size_t size;
-	const int type = CBFS_TYPE_PAYLOAD;
+	const uint32_t type = CBFS_TYPE_PAYLOAD;
 
-	buffer = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, payload->name,
-					type, &size);
+	buffer = cbfs_boot_map_with_leak(payload->name, type, &size);
 
 	if (buffer == NULL)
 		return -1;
diff --git a/src/lib/loaders/cbfs_ramstage_loader.c b/src/lib/loaders/cbfs_ramstage_loader.c
index c460653..bbbf695 100644
--- a/src/lib/loaders/cbfs_ramstage_loader.c
+++ b/src/lib/loaders/cbfs_ramstage_loader.c
@@ -44,8 +44,7 @@ static int cbfs_load_ramstage(struct prog *ramstage)
 
 static int cbfs_load_ramstage(struct prog *ramstage)
 {
-	return cbfs_load_prog_stage(CBFS_DEFAULT_MEDIA, ramstage);
-
+	return cbfs_boot_load_stage(ramstage);
 }
 
 #endif /* CONFIG_RELOCATABLE_RAMSTAGE */
diff --git a/src/lib/loaders/cbfs_romstage_loader.c b/src/lib/loaders/cbfs_romstage_loader.c
index e5b4215..7dde2ec 100644
--- a/src/lib/loaders/cbfs_romstage_loader.c
+++ b/src/lib/loaders/cbfs_romstage_loader.c
@@ -22,7 +22,7 @@
 
 static int cbfs_load_romstage(struct prog *romstage)
 {
-	return cbfs_load_prog_stage(CBFS_DEFAULT_MEDIA, romstage);
+	return cbfs_boot_load_stage(romstage);
 }
 
 const struct prog_loader_ops cbfs_romstage_loader = {
diff --git a/src/lib/rmodule.c b/src/lib/rmodule.c
index 799d8f0..d886800 100644
--- a/src/lib/rmodule.c
+++ b/src/lib/rmodule.c
@@ -22,6 +22,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <lib.h>
 #include <console/console.h>
 #include <program_loading.h>
 #include <rmodule.h>
@@ -252,34 +253,57 @@ 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 cbfs_stage *stage)
+int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfsf *fh)
 {
 	struct rmodule rmod_stage;
 	size_t region_size;
 	char *stage_region;
 	int rmodule_offset;
 	int load_offset;
+	struct cbfs_stage stage;
+	void *rmod_loc;
 
-	if (stage == NULL || rsl->prog == NULL || rsl->prog->name == NULL)
+	if (fh == NULL || rsl->prog == NULL || rsl->prog->name == NULL)
+		return -1;
+
+	if (cbfs_file_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
 		return -1;
 
 	rmodule_offset =
 		rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
-		                    stage->memlen, &region_size, &load_offset);
+		                    stage.memlen, &region_size, &load_offset);
 
 	stage_region = cbmem_add(rsl->cbmem_id, region_size);
 
 	if (stage_region == NULL)
 		return -1;
 
+	rmod_loc = &stage_region[rmodule_offset];
+
 	printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
-	       rsl->prog->name, &stage_region[rmodule_offset], stage->memlen);
+	       rsl->prog->name, rmod_loc, stage.memlen);
+
+	if (stage.compression == CBFS_COMPRESS_NONE) {
+		if (cbfs_file_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);
 
-	if (!cbfs_decompress(stage->compression, &stage[1],
-	                    &stage_region[rmodule_offset], stage->len))
+		if (map == NULL)
+			return -1;
+
+		fsize = ulzma(map, rmod_loc);
+
+		cbfs_file_munmap(fh, map);
+
+		if (!fsize)
+			return -1;
+	} else
 		return -1;
 
-	if (rmodule_parse(&stage_region[rmodule_offset], &rmod_stage))
+	if (rmodule_parse(rmod_loc, &rmod_stage))
 		return -1;
 
 	if (rmodule_load(&stage_region[load_offset], &rmod_stage))
@@ -294,13 +318,11 @@ int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfs_stage *stage)
 
 int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl)
 {
-	struct cbfs_stage *stage;
-
-	stage = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-	                              rsl->prog->name, CBFS_TYPE_STAGE, NULL);
+	struct cbfsf fh;
+	uint32_t type = CBFS_TYPE_STAGE;
 
-	if (stage == NULL)
+	if (cbfs_boot_locate(&fh, rsl->prog->name, &type))
 		return -1;
 
-	return rmodule_stage_load(rsl, stage);
+	return rmodule_stage_load(rsl, &fh);
 }
diff --git a/src/mainboard/emulation/qemu-armv7/media.c b/src/mainboard/emulation/qemu-armv7/media.c
index e0f2251..cb0b275 100644
--- a/src/mainboard/emulation/qemu-armv7/media.c
+++ b/src/mainboard/emulation/qemu-armv7/media.c
@@ -13,83 +13,12 @@
  * GNU General Public License for more details.
  */
 #include <boot_device.h>
-#include <cbfs.h>
-#include <string.h>
-#include <symbols.h>
-#include <console/console.h>
 
 /* Maps directly to qemu memory mapped space of 0x10000 up to rom size. */
-static const struct mem_region_device gboot_dev =
+static const struct mem_region_device boot_dev =
 	MEM_REGION_DEV_INIT((void *)0x10000, CONFIG_ROM_SIZE);
 
 const struct region_device *boot_device_ro(void)
 {
-	return &gboot_dev.rdev;
-}
-
-static int emu_rom_open(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static void *emu_rom_map(struct cbfs_media *media, size_t offset, size_t count)
-{
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *emu_rom_unmap(struct cbfs_media *media, const void *address)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-static size_t emu_rom_read(struct cbfs_media *media, void *dest, size_t offset,
-			   size_t count)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static int emu_rom_close(struct cbfs_media *media)
-{
-	return 0;
-}
-
-static int init_emu_rom_cbfs_media(struct cbfs_media *media)
-{
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-	media->open = emu_rom_open;
-	media->close = emu_rom_close;
-	media->map = emu_rom_map;
-	media->unmap = emu_rom_unmap;
-	media->read = emu_rom_read;
-	return 0;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	return init_emu_rom_cbfs_media(media);
+	return &boot_dev.rdev;
 }
diff --git a/src/mainboard/google/bolt/romstage.c b/src/mainboard/google/bolt/romstage.c
index bead56f..e80aedf 100644
--- a/src/mainboard/google/bolt/romstage.c
+++ b/src/mainboard/google/bolt/romstage.c
@@ -78,8 +78,8 @@ static void copy_spd(struct pei_data *peid)
 	int spd_index = 0; /* No GPIO selection, force index 0 for now */
 
 	printk(BIOS_DEBUG, "SPD index %d\n", spd_index);
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
 
diff --git a/src/mainboard/google/butterfly/mainboard.c b/src/mainboard/google/butterfly/mainboard.c
index 6d2af16..2f3cf1d 100644
--- a/src/mainboard/google/butterfly/mainboard.c
+++ b/src/mainboard/google/butterfly/mainboard.c
@@ -211,9 +211,8 @@ static void mainboard_init(device_t dev)
 			}
 		}
 	} else {
-		vpd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						"vpd.bin", CBFS_TYPE_RAW,
-						&search_length);
+		vpd_file = cbfs_boot_map_with_leak("vpd.bin", CBFS_TYPE_RAW,
+							&search_length);
 		if (vpd_file) {
 			search_address = (unsigned long)vpd_file;
 		} else {
diff --git a/src/mainboard/google/falco/romstage.c b/src/mainboard/google/falco/romstage.c
index 4e79159..d28a914 100644
--- a/src/mainboard/google/falco/romstage.c
+++ b/src/mainboard/google/falco/romstage.c
@@ -79,8 +79,8 @@ static void copy_spd(struct pei_data *peid)
 	size_t spd_file_len;
 
 	printk(BIOS_DEBUG, "SPD index %d\n", spd_index);
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
 
diff --git a/src/mainboard/google/link/romstage.c b/src/mainboard/google/link/romstage.c
index 5844aa6..d3e7c6a 100644
--- a/src/mainboard/google/link/romstage.c
+++ b/src/mainboard/google/link/romstage.c
@@ -94,8 +94,8 @@ static void copy_spd(struct pei_data *peid)
 	int spd_index = get_gpios(gpio_vector);
 
 	printk(BIOS_DEBUG, "spd index %d\n", spd_index);
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
 
diff --git a/src/mainboard/google/panther/lan.c b/src/mainboard/google/panther/lan.c
index 650d018..6dd5bec 100644
--- a/src/mainboard/google/panther/lan.c
+++ b/src/mainboard/google/panther/lan.c
@@ -127,8 +127,7 @@ static void program_mac_address(u16 io_base)
 				search_length = region_device_sz(&rdev);
 		}
 	} else {
-		search_address = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-							"vpd.bin",
+		search_address = cbfs_boot_map_with_leak("vpd.bin",
 							CBFS_TYPE_RAW,
 							&search_length);
 	}
diff --git a/src/mainboard/google/peach_pit/romstage.c b/src/mainboard/google/peach_pit/romstage.c
index 443eafd..b2456fc 100644
--- a/src/mainboard/google/peach_pit/romstage.c
+++ b/src/mainboard/google/peach_pit/romstage.c
@@ -21,12 +21,14 @@
 #include <arch/exception.h>
 #include <arch/stages.h>
 #include <armv7.h>
+#include <boot_device.h>
 #include <cbfs.h>
 #include <cbmem.h>
 #include <console/console.h>
 #include <device/i2c.h>
 #include <drivers/maxim/max77802/max77802.h>
 #include <program_loading.h>
+#include <region.h>
 #include <soc/clk.h>
 #include <soc/cpu.h>
 #include <soc/dmc.h>
@@ -176,29 +178,28 @@ static unsigned long primitive_mem_test(void)
 /* here is a simple SPI debug test, known to fid trouble */
 static void simple_spi_test(void)
 {
-	struct cbfs_media default_media, *media;
+	const struct region_device *boot_dev;
 	int i, amt = 4 * MiB, errors = 0;
 	//u32 *data = (void *)0x40000000;
 	u32 data[1024];
 	u32 in;
 
+	boot_device_init();
+	boot_dev = boot_device_ro();
 	amt = sizeof(data);
-	media = &default_media;
-	if (init_default_cbfs_media(media) != 0) {
+	if (boot_dev == NULL) {
 		printk(BIOS_SPEW, "Failed to initialize default media.\n");
 		return;
 	}
 
-
-	media->open(media);
-	if (media->read(media, data, (size_t) 0, amt) < amt){
+	if (rdev_readat(boot_dev, data, 0, amt) < amt) {
 		printk(BIOS_SPEW, "simple_spi_test fails\n");
 		return;
 	}
 
 
 	for(i = 0; i < amt; i += 4){
-		if (media->read(media, &in, (size_t) i, 4) < 1){
+		if (rdev_readat(boot_dev, &in, i, 4) < 4) {
 			printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
 			return;
 		}
@@ -207,7 +208,7 @@ static void simple_spi_test(void)
 			printk(BIOS_SPEW, "BAD at %d(%p):\nRAM %08lx\nSPI %08lx\n",
 			       i, &data[i/4], (unsigned long)data[i/4], (unsigned long)in);
 			/* reread it to see which is wrong. */
-			if (media->read(media, &in, (size_t) i, 4) < 1){
+			if (rdev_readat(boot_dev, &in, i, 4) < 4) {
 				printk(BIOS_SPEW, "simple_spi_test fails at %d\n", i);
 				return;
 			}
diff --git a/src/mainboard/google/peppy/romstage.c b/src/mainboard/google/peppy/romstage.c
index 27962e1..6f00216 100644
--- a/src/mainboard/google/peppy/romstage.c
+++ b/src/mainboard/google/peppy/romstage.c
@@ -82,8 +82,8 @@ static void copy_spd(struct pei_data *peid)
 	size_t spd_file_len;
 
 	printk(BIOS_DEBUG, "SPD index %d\n", spd_index);
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
 
diff --git a/src/mainboard/google/rambi/romstage.c b/src/mainboard/google/rambi/romstage.c
index 0f431d9..b25e1dc 100644
--- a/src/mainboard/google/rambi/romstage.c
+++ b/src/mainboard/google/rambi/romstage.c
@@ -71,9 +71,10 @@ static void *get_spd_pointer(char *spd_file_content, int total_spds, int *dual)
 
 void mainboard_romstage_entry(struct romstage_params *rp)
 {
-	struct cbfs_file *spd_file;
 	void *spd_content;
 	int dual_channel = 0;
+	void *spd_file;
+	size_t spd_fsize;
 
 	struct mrc_params mp = {
 		.mainboard = {
@@ -83,12 +84,12 @@ void mainboard_romstage_entry(struct romstage_params *rp)
 		},
 	};
 
-	spd_file = cbfs_get_file(CBFS_DEFAULT_MEDIA, "spd.bin");
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_fsize);
 	if (!spd_file)
 		die("SPD data not found.");
 
-	spd_content = get_spd_pointer(CBFS_SUBHEADER(spd_file),
-	                              ntohl(spd_file->len) / SPD_SIZE,
+	spd_content = get_spd_pointer(spd_file, spd_fsize / SPD_SIZE,
 	                              &dual_channel);
 	mp.mainboard.dram_data[0] = spd_content;
 	if (dual_channel)
diff --git a/src/mainboard/google/samus/spd/spd.c b/src/mainboard/google/samus/spd/spd.c
index 1308ab2..a2f2973 100644
--- a/src/mainboard/google/samus/spd/spd.c
+++ b/src/mainboard/google/samus/spd/spd.c
@@ -90,8 +90,8 @@ void mainboard_fill_spd_data(struct pei_data *pei_data)
 	};
 	int spd_gpio[4];
 	int spd_index;
-	int spd_file_len;
-	struct cbfs_file *spd_file;
+	size_t spd_file_len;
+	char *spd_file;
 
 	spd_gpio[0] = get_gpio(spd_bits[0]);
 	spd_gpio[1] = get_gpio(spd_bits[1]);
@@ -106,10 +106,9 @@ void mainboard_fill_spd_data(struct pei_data *pei_data)
 	       spd_bits[3], spd_gpio[3], spd_bits[2], spd_gpio[2],
 	       spd_bits[1], spd_gpio[1], spd_bits[0], spd_gpio[0]);
 
-	spd_file = cbfs_get_file(CBFS_DEFAULT_MEDIA, "spd.bin");
+	spd_file = cbfs_boot_map_with_leak("spd.bin", 0xab, &spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
-	spd_file_len = ntohl(spd_file->len);
 
 	if (spd_file_len < ((spd_index + 1) * SPD_LEN)) {
 		printk(BIOS_ERR, "SPD index override to 0 - old hardware?\n");
@@ -121,10 +120,8 @@ void mainboard_fill_spd_data(struct pei_data *pei_data)
 
 	/* Assume same memory in both channels */
 	spd_index *= SPD_LEN;
-	memcpy(pei_data->spd_data[0][0],
-	       ((char*)CBFS_SUBHEADER(spd_file)) + spd_index, SPD_LEN);
-	memcpy(pei_data->spd_data[1][0],
-	       ((char*)CBFS_SUBHEADER(spd_file)) + spd_index, SPD_LEN);
+	memcpy(pei_data->spd_data[0][0], spd_file + spd_index, SPD_LEN);
+	memcpy(pei_data->spd_data[1][0], spd_file + spd_index, SPD_LEN);
 
 	/* Make sure a valid SPD was found */
 	if (pei_data->spd_data[0][0][0] == 0)
diff --git a/src/mainboard/google/slippy/romstage.c b/src/mainboard/google/slippy/romstage.c
index c242ecd..c89687f 100644
--- a/src/mainboard/google/slippy/romstage.c
+++ b/src/mainboard/google/slippy/romstage.c
@@ -80,8 +80,8 @@ static void copy_spd(struct pei_data *peid)
 	size_t spd_file_len;
 
 	printk(BIOS_DEBUG, "SPD index %d\n", spd_index);
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_file)
 		die("SPD data not found.");
 
diff --git a/src/mainboard/google/urara/boardid.c b/src/mainboard/google/urara/boardid.c
index a95bc14..0b46408 100644
--- a/src/mainboard/google/urara/boardid.c
+++ b/src/mainboard/google/urara/boardid.c
@@ -23,7 +23,7 @@
 #include <string.h>
 
 #include <boardid.h>
-#include <cbfs_core.h>
+#include <cbfs.h>
 #include <console/console.h>
 
 #include "mainboard/google/urara/urara_boardid.h"
@@ -48,28 +48,17 @@ static int cached_board_id = -1;
 
 static uint8_t retrieve_board_id(void)
 {
-	struct cbfs_file *board_id_file;
 	const char *board_id_file_name = CBFS_BOARD_ID_FILE_NAME;
 	char *file_contents;
 	int i;
-	unsigned length;
+	size_t length;
 
-	board_id_file = cbfs_get_file(CBFS_DEFAULT_MEDIA, board_id_file_name);
-	if (!board_id_file) {
-		printk(BIOS_WARNING,
-		       "board_id: failed to locate file '%s'\n",
-		       board_id_file_name);
-		return 0;
-	}
-
-	length = be32_to_cpu(board_id_file->len);
-
-	file_contents = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-					      board_id_file_name,
-					      CBFS_TYPE_RAW, NULL);
+	file_contents = cbfs_boot_map_with_leak(board_id_file_name,
+						CBFS_TYPE_RAW, &length);
 
 	if (!file_contents) {
-		printk(BIOS_WARNING, "board_id: failed to read file '%s'\n",
+		printk(BIOS_WARNING,
+		       "board_id: failed to locate file '%s'\n",
 		       board_id_file_name);
 		return 0;
 	}
diff --git a/src/mainboard/samsung/lumpy/romstage.c b/src/mainboard/samsung/lumpy/romstage.c
index 29a238a..169a21e 100644
--- a/src/mainboard/samsung/lumpy/romstage.c
+++ b/src/mainboard/samsung/lumpy/romstage.c
@@ -236,8 +236,8 @@ void main(unsigned long bist)
 		break;
 	}
 
-	spd_data = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin",
-					 CBFS_TYPE_SPD, &spd_file_len);
+	spd_data = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD,
+						&spd_file_len);
 	if (!spd_data)
 		die("SPD data not found.");
 	if (spd_file_len < (spd_index + 1) * 256)
diff --git a/src/mainboard/siemens/mc_tcu3/modhwinfo.c b/src/mainboard/siemens/mc_tcu3/modhwinfo.c
index 95bb76c..b077868 100644
--- a/src/mainboard/siemens/mc_tcu3/modhwinfo.c
+++ b/src/mainboard/siemens/mc_tcu3/modhwinfo.c
@@ -31,8 +31,7 @@ u8* get_first_linked_block(char *filename, u8 **file_offset)
 {
 	u8* block_ptr = NULL;
 
-	block_ptr = (cbfs_get_file_content(CBFS_DEFAULT_MEDIA, filename,
-			0x50, NULL));
+	block_ptr = cbfs_boot_map_with_leak(filename, 0x50, NULL);
 	if (!block_ptr)
 		return NULL;
 	if (!strncmp((char*)block_ptr, "H1W2M3I4", LEN_MAGIC_NUM)) {
@@ -57,8 +56,7 @@ struct hwinfo* get_hwinfo(char *filename)
 {
 	struct hwinfo* main_hwinfo;
 
-	main_hwinfo = (struct hwinfo*)(cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-							filename, 0x50, NULL));
+	main_hwinfo = cbfs_boot_map_with_leak(filename, 0x50, NULL);
 	if ((main_hwinfo) &&
 		(!strncmp(main_hwinfo->magicNumber, "H1W2M3I4", LEN_MAGIC_NUM)) &&
 		(main_hwinfo->length == LEN_MAIN_HWINFO))
diff --git a/src/northbridge/amd/agesa/common/common.c b/src/northbridge/amd/agesa/common/common.c
index 29b4c2e..e4f8e9d 100644
--- a/src/northbridge/amd/agesa/common/common.c
+++ b/src/northbridge/amd/agesa/common/common.c
@@ -38,7 +38,8 @@ AGESA_STATUS common_ReadCbfsSpd (UINT32 Func, UINT32 Data, VOID *ConfigPtr)
 
 	char *spd_file;
 
-	spd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "spd.bin", CBFS_TYPE_SPD_BIN, &spd_file_length);
+	spd_file = cbfs_boot_map_with_leak("spd.bin", CBFS_TYPE_SPD_BIN,
+						&spd_file_length);
 	if (!spd_file)
 		die("file [spd.bin] not found in CBFS");
 
diff --git a/src/northbridge/amd/agesa/def_callouts.c b/src/northbridge/amd/agesa/def_callouts.c
index 26b1c0d..3f83e9e 100644
--- a/src/northbridge/amd/agesa/def_callouts.c
+++ b/src/northbridge/amd/agesa/def_callouts.c
@@ -117,8 +117,8 @@ AGESA_STATUS agesa_RunFuncOnAp (UINT32 Func, UINT32 Data, VOID *ConfigPtr)
 AGESA_STATUS agesa_GfxGetVbiosImage(UINT32 Func, UINT32 FchData, VOID *ConfigPrt)
 {
 	GFX_VBIOS_IMAGE_INFO  *pVbiosImageInfo = (GFX_VBIOS_IMAGE_INFO *)ConfigPrt;
-	pVbiosImageInfo->ImagePtr = cbfs_get_file_content(
-			CBFS_DEFAULT_MEDIA, "pci"CONFIG_VGA_BIOS_ID".rom",
+	pVbiosImageInfo->ImagePtr = cbfs_boot_map_with_leak(
+			"pci"CONFIG_VGA_BIOS_ID".rom",
 			CBFS_TYPE_OPTIONROM, NULL);
 	/* printk(BIOS_DEBUG, "IMGptr=%x\n", pVbiosImageInfo->ImagePtr); */
 	return pVbiosImageInfo->ImagePtr == NULL ? AGESA_WARNING : AGESA_SUCCESS;
diff --git a/src/northbridge/amd/pi/agesawrapper.c b/src/northbridge/amd/pi/agesawrapper.c
index 8bcd8ac..fbf7a84 100644
--- a/src/northbridge/amd/pi/agesawrapper.c
+++ b/src/northbridge/amd/pi/agesawrapper.c
@@ -593,20 +593,13 @@ AGESA_STATUS agesawrapper_amdreadeventlog (UINT8 HeapStatus)
 
 const void *agesawrapper_locate_module (const CHAR8 name[8])
 {
-	struct cbfs_media media;
-	struct cbfs_file* file;
 	const void* agesa;
 	const AMD_IMAGE_HEADER* image;
 	const AMD_MODULE_HEADER* module;
 	size_t file_size;
 
-	if (init_default_cbfs_media(&media))
-		return NULL;
-	file = cbfs_get_file(&media, (const char*)CONFIG_CBFS_AGESA_NAME);
-	if (!file)
-		return NULL;
-	agesa = cbfs_get_file_content(&media, (const char*)CONFIG_CBFS_AGESA_NAME,
-		ntohl(file->type), &file_size);
+	agesa = cbfs_boot_map_with_leak((const char *)CONFIG_CBFS_AGESA_NAME,
+					CBFS_TYPE_RAW, &file_size);
 	if (!agesa)
 		return NULL;
 	image =  LibAmdLocateImage(agesa, agesa + file_size - 1, 4096, name);
diff --git a/src/northbridge/amd/pi/def_callouts.c b/src/northbridge/amd/pi/def_callouts.c
index 2caf183..56a24ac 100644
--- a/src/northbridge/amd/pi/def_callouts.c
+++ b/src/northbridge/amd/pi/def_callouts.c
@@ -107,8 +107,8 @@ AGESA_STATUS agesa_RunFuncOnAp (UINT32 Func, UINT32 Data, VOID *ConfigPtr)
 AGESA_STATUS agesa_GfxGetVbiosImage(UINT32 Func, UINT32 FchData, VOID *ConfigPrt)
 {
 	GFX_VBIOS_IMAGE_INFO  *pVbiosImageInfo = (GFX_VBIOS_IMAGE_INFO *)ConfigPrt;
-	pVbiosImageInfo->ImagePtr = cbfs_get_file_content(
-			CBFS_DEFAULT_MEDIA, "pci"CONFIG_VGA_BIOS_ID".rom",
+	pVbiosImageInfo->ImagePtr = cbfs_boot_map_with_leak(
+			"pci"CONFIG_VGA_BIOS_ID".rom",
 			CBFS_TYPE_OPTIONROM, NULL);
 	printk(BIOS_DEBUG, "agesa_GfxGetVbiosImage: IMGptr=%p\n", pVbiosImageInfo->ImagePtr);
 	return (pVbiosImageInfo->ImagePtr ? AGESA_SUCCESS : AGESA_WARNING);
diff --git a/src/northbridge/intel/haswell/mrccache.c b/src/northbridge/intel/haswell/mrccache.c
index e4acda8..1150be2 100644
--- a/src/northbridge/intel/haswell/mrccache.c
+++ b/src/northbridge/intel/haswell/mrccache.c
@@ -74,8 +74,7 @@ static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 			*mrc_region_ptr = rdev_mmap_full(&rdev);
 		}
 	} else {
-		*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-							"mrc.cache",
+		*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
 							CBFS_TYPE_MRC_CACHE,
 							&region_size);
 	}
diff --git a/src/northbridge/intel/haswell/raminit.c b/src/northbridge/intel/haswell/raminit.c
index 6bbb215..4740f73 100644
--- a/src/northbridge/intel/haswell/raminit.c
+++ b/src/northbridge/intel/haswell/raminit.c
@@ -162,8 +162,8 @@ void sdram_initialize(struct pei_data *pei_data)
 	pei_data->tx_byte = do_putchar;
 
 	/* Locate and call UEFI System Agent binary. */
-	entry = (unsigned long)cbfs_get_file_content(
-		CBFS_DEFAULT_MEDIA, "mrc.bin", CBFS_TYPE_MRC, NULL);
+	entry = (unsigned long)cbfs_boot_map_with_leak("mrc.bin",
+							CBFS_TYPE_MRC, NULL);
 	if (entry) {
 		int rv;
 		asm volatile (
diff --git a/src/northbridge/intel/i82830/vga.c b/src/northbridge/intel/i82830/vga.c
index f846e37..cd07dcb 100644
--- a/src/northbridge/intel/i82830/vga.c
+++ b/src/northbridge/intel/i82830/vga.c
@@ -31,8 +31,7 @@ static void vga_init(device_t dev)
 {
 	printk(BIOS_INFO, "Starting Graphics Initialization\n");
 	size_t mbi_len;
-	void *mbi = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "mbi.bin",
-					  CBFS_TYPE_MBI, &mbi_len);
+	void *mbi = cbfs_boot_map_with_leak("mbi.bin", CBFS_TYPE_MBI, &mbi_len);
 
 	if (mbi && mbi_len) {
 		/* The GDT or coreboot table is going to live here. But
diff --git a/src/northbridge/intel/sandybridge/mrccache.c b/src/northbridge/intel/sandybridge/mrccache.c
index 1453390..eb6297a 100644
--- a/src/northbridge/intel/sandybridge/mrccache.c
+++ b/src/northbridge/intel/sandybridge/mrccache.c
@@ -74,8 +74,7 @@ static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 			*mrc_region_ptr = rdev_mmap_full(&rdev);
 		}
 	} else {
-		*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-							"mrc.cache",
+		*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
 							CBFS_TYPE_MRC_CACHE,
 							&region_size);
 	}
diff --git a/src/northbridge/intel/sandybridge/raminit.c b/src/northbridge/intel/sandybridge/raminit.c
index d8c7eef..fde0cfa 100644
--- a/src/northbridge/intel/sandybridge/raminit.c
+++ b/src/northbridge/intel/sandybridge/raminit.c
@@ -246,8 +246,7 @@ void sdram_initialize(struct pei_data *pei_data)
 	pei_data->tx_byte = do_putchar;
 
 	/* Locate and call UEFI System Agent binary. */
-	entry = cbfs_get_file_content(
-		CBFS_DEFAULT_MEDIA, "mrc.bin", CBFS_TYPE_MRC, NULL);
+	entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL);
 	if (entry) {
 		int rv;
 		rv = entry (pei_data);
diff --git a/src/soc/intel/baytrail/refcode.c b/src/soc/intel/baytrail/refcode.c
index 29a6461..232c610 100644
--- a/src/soc/intel/baytrail/refcode.c
+++ b/src/soc/intel/baytrail/refcode.c
@@ -19,6 +19,7 @@
 
 #include <string.h>
 #include <arch/acpi.h>
+#include <boot_device.h>
 #include <cbmem.h>
 #include <console/console.h>
 #include <console/streams.h>
@@ -59,7 +60,8 @@ static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
 {
 	struct vboot_handoff *vboot_handoff;
 	const struct firmware_component *fwc;
-	struct cbfs_stage *stage;
+	struct cbfsf stage;
+	struct region reg;
 
 	vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
 	fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
@@ -70,10 +72,17 @@ static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
 	    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");
-	stage = (void *)(uintptr_t)fwc->address;
 
-	if (rmodule_stage_load(refcode, stage)) {
+	if (rmodule_stage_load(refcode, &stage)) {
 		printk(BIOS_DEBUG, "Error loading reference code.\n");
 		return -1;
 	}
diff --git a/src/soc/intel/baytrail/romstage/raminit.c b/src/soc/intel/baytrail/romstage/raminit.c
index 61c122c..8611d70 100644
--- a/src/soc/intel/baytrail/romstage/raminit.c
+++ b/src/soc/intel/baytrail/romstage/raminit.c
@@ -148,8 +148,7 @@ void raminit(struct mrc_params *mp, int prev_sleep_state)
 	}
 
 	/* Determine if mrc.bin is in the cbfs. */
-	if (cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "mrc.bin", CBFS_TYPE_MRC,
-			NULL) == NULL) {
+	if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
 		printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
 		return;
 	}
diff --git a/src/soc/intel/broadwell/refcode.c b/src/soc/intel/broadwell/refcode.c
index 0eb2ef3..0a91514 100644
--- a/src/soc/intel/broadwell/refcode.c
+++ b/src/soc/intel/broadwell/refcode.c
@@ -19,6 +19,7 @@
 
 #include <string.h>
 #include <arch/acpi.h>
+#include <boot_device.h>
 #include <cbmem.h>
 #include <console/console.h>
 #include <console/streams.h>
@@ -55,7 +56,8 @@ static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
 {
 	struct vboot_handoff *vboot_handoff;
 	const struct firmware_component *fwc;
-	struct cbfs_stage *stage;
+	struct cbfsf stage;
+	struct region reg;
 
 	vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
 	fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
@@ -66,10 +68,17 @@ static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
 	    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");
-	stage = (void *)(uintptr_t)fwc->address;
 
-	if (rmodule_stage_load(refcode, stage)) {
+	if (rmodule_stage_load(refcode, &stage)) {
 		printk(BIOS_DEBUG, "Error loading reference code.\n");
 		return -1;
 	}
diff --git a/src/soc/intel/broadwell/romstage/raminit.c b/src/soc/intel/broadwell/romstage/raminit.c
index 52ea491..7b46a90 100644
--- a/src/soc/intel/broadwell/romstage/raminit.c
+++ b/src/soc/intel/broadwell/romstage/raminit.c
@@ -86,8 +86,7 @@ void raminit(struct pei_data *pei_data)
 	}
 
 	/* Determine if mrc.bin is in the cbfs. */
-	entry = (pei_wrapper_entry_t)cbfs_get_file_content(
-		CBFS_DEFAULT_MEDIA, "mrc.bin", CBFS_TYPE_MRC, NULL);
+	entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL);
 	if (entry == NULL) {
 		printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
 		return;
diff --git a/src/soc/nvidia/tegra124/spi.c b/src/soc/nvidia/tegra124/spi.c
index 6fbb3c4..ca98eca 100644
--- a/src/soc/nvidia/tegra124/spi.c
+++ b/src/soc/nvidia/tegra124/spi.c
@@ -802,18 +802,6 @@ int spi_xfer(struct spi_slave *slave, const void *dout,
 	return ret;
 }
 
-static int tegra_spi_cbfs_open(struct cbfs_media *media)
-{
-	DEBUG_SPI("tegra_spi_cbfs_open\n");
-	return 0;
-}
-
-static int tegra_spi_cbfs_close(struct cbfs_media *media)
-{
-	DEBUG_SPI("tegra_spi_cbfs_close\n");
-	return 0;
-}
-
 #define JEDEC_READ			0x03
 #define JEDEC_READ_OUTSIZE		0x04
 #define JEDEC_FAST_READ_DUAL		0x3b
@@ -877,69 +865,6 @@ tegra_spi_cbfs_read_exit:
 	return ret;
 }
 
-static size_t tegra_spi_cbfs_read(struct cbfs_media *media, void *dest,
-				size_t offset, size_t count)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	printk(BIOS_ERR, "%s: reading %zx bytes from %zx\n",
-			__func__, count, offset);
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static void *tegra_spi_cbfs_map(struct cbfs_media *media, size_t offset,
-				 size_t count)
-{
-	const struct region_device *boot_dev;
-	void *map;
-
-	DEBUG_SPI("tegra_spi_cbfs_map\n");
-
-	boot_dev = media->context;
-
-	map = rdev_mmap(boot_dev, offset, count);
-
-	if (map == NULL)
-		map = (void *)-1;
-
-	return map;
-}
-
-static void *tegra_spi_cbfs_unmap(struct cbfs_media *media,
-				   const void *address)
-{
-	const struct region_device *boot_dev;
-
-	DEBUG_SPI("tegra_spi_cbfs_unmap\n");
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	DEBUG_SPI("Initializing CBFS media on SPI\n");
-
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-	media->open = tegra_spi_cbfs_open;
-	media->close = tegra_spi_cbfs_close;
-	media->read = tegra_spi_cbfs_read;
-	media->map = tegra_spi_cbfs_map;
-	media->unmap = tegra_spi_cbfs_unmap;
-
-	return 0;
-}
-
 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
 {
 	struct tegra_spi_channel *channel = to_tegra_spi(bus);
diff --git a/src/soc/nvidia/tegra132/ccplex.c b/src/soc/nvidia/tegra132/ccplex.c
index f9356a9..7f16d54 100644
--- a/src/soc/nvidia/tegra132/ccplex.c
+++ b/src/soc/nvidia/tegra132/ccplex.c
@@ -75,10 +75,9 @@ static int ccplex_start(void)
 
 int ccplex_load_mts(void)
 {
-	struct cbfs_file file;
-	ssize_t offset;
-	size_t nread;
+	ssize_t nread;
 	struct stopwatch sw;
+	struct cbfsf fh;
 
 	/*
 	 * MTS location is hard coded to this magic address. The hardware will
@@ -86,21 +85,19 @@ int ccplex_load_mts(void)
 	 * place in the carveout region.
 	 */
 	void * const mts = (void *)(uintptr_t)MTS_LOAD_ADDRESS;
-	struct cbfs_media *media = CBFS_DEFAULT_MEDIA;
 
 	stopwatch_init(&sw);
-	offset = cbfs_locate_file(media, &file, MTS_FILE_NAME);
-	if (offset < 0) {
+	if (cbfs_boot_locate(&fh, MTS_FILE_NAME, NULL)) {
 		printk(BIOS_DEBUG, "MTS file not found: %s\n", MTS_FILE_NAME);
 		return -1;
 	}
 
 	/* Read MTS file into the carveout region. */
-	nread = cbfs_read(media, mts, offset, file.len);
+	nread = cbfs_file_readat(&fh, mts, 0, cbfs_file_size(&fh));
 
-	if (nread != file.len) {
+	if (nread != cbfs_file_size(&fh)) {
 		printk(BIOS_DEBUG, "MTS bytes read (%zu) != file length(%u)!\n",
-			nread, file.len);
+			nread, cbfs_file_size(&fh));
 		return -1;
 	}
 
diff --git a/src/soc/nvidia/tegra132/spi.c b/src/soc/nvidia/tegra132/spi.c
index 746e96e..d481b87 100644
--- a/src/soc/nvidia/tegra132/spi.c
+++ b/src/soc/nvidia/tegra132/spi.c
@@ -817,18 +817,6 @@ int spi_xfer(struct spi_slave *slave, const void *dout,
 	return ret;
 }
 
-static int tegra_spi_cbfs_open(struct cbfs_media *media)
-{
-	DEBUG_SPI("tegra_spi_cbfs_open\n");
-	return 0;
-}
-
-static int tegra_spi_cbfs_close(struct cbfs_media *media)
-{
-	DEBUG_SPI("tegra_spi_cbfs_close\n");
-	return 0;
-}
-
 #define JEDEC_READ			0x03
 #define JEDEC_READ_OUTSIZE		0x04
 #define JEDEC_FAST_READ_DUAL		0x3b
@@ -892,69 +880,6 @@ tegra_spi_cbfs_read_exit:
 	return ret;
 }
 
-static size_t tegra_spi_cbfs_read(struct cbfs_media *media, void *dest,
-				size_t offset, size_t count)
-{
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	DEBUG_SPI("%s: reading %zx bytes from %zx\n", __func__, count, offset);
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static void *tegra_spi_cbfs_map(struct cbfs_media *media, size_t offset,
-				 size_t count)
-{
-	const struct region_device *boot_dev;
-	void *map;
-
-	DEBUG_SPI("tegra_spi_cbfs_map\n");
-
-	boot_dev = media->context;
-
-	map = rdev_mmap(boot_dev, offset, count);
-
-	if (map == NULL)
-		map = (void *)-1;
-
-	return map;
-}
-
-static void *tegra_spi_cbfs_unmap(struct cbfs_media *media,
-				   const void *address)
-{
-	const struct region_device *boot_dev;
-
-	DEBUG_SPI("tegra_spi_cbfs_unmap\n");
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	DEBUG_SPI("Initializing CBFS media on SPI\n");
-
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-	media->open = tegra_spi_cbfs_open;
-	media->close = tegra_spi_cbfs_close;
-	media->read = tegra_spi_cbfs_read;
-	media->map = tegra_spi_cbfs_map;
-	media->unmap = tegra_spi_cbfs_unmap;
-
-	return 0;
-}
-
 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs)
 {
 	struct tegra_spi_channel *channel = to_tegra_spi(bus);
diff --git a/src/soc/qualcomm/ipq806x/blobs_init.c b/src/soc/qualcomm/ipq806x/blobs_init.c
index e3478a9..1465995 100644
--- a/src/soc/qualcomm/ipq806x/blobs_init.c
+++ b/src/soc/qualcomm/ipq806x/blobs_init.c
@@ -32,20 +32,18 @@
 
 static void *load_ipq_blob(const char *file_name)
 {
-	struct cbfs_file *blob_file;
 	struct mbn_header *blob_mbn;
 	void *blob_dest;
+	size_t blob_size;
 
-	blob_file = cbfs_get_file(CBFS_DEFAULT_MEDIA, file_name);
-	if (!blob_file)
+	blob_mbn = cbfs_boot_map_with_leak(file_name, CBFS_TYPE_RAW,
+						&blob_size);
+	if (!blob_mbn)
 		return NULL;
 
-	blob_mbn = (struct mbn_header *)((uintptr_t)blob_file +
-					 ntohl(blob_file->offset));
-
 	/* some sanity checks on the headers */
 	if ((blob_mbn->mbn_version != 3) ||
-	    (blob_mbn->mbn_total_size > ntohl(blob_file->len)))
+	    (blob_mbn->mbn_total_size > blob_size))
 		return NULL;
 
 	blob_dest = (void *) blob_mbn->mbn_destination;
diff --git a/src/soc/samsung/exynos5250/alternate_cbfs.c b/src/soc/samsung/exynos5250/alternate_cbfs.c
index d6aa6e6..165171f 100644
--- a/src/soc/samsung/exynos5250/alternate_cbfs.c
+++ b/src/soc/samsung/exynos5250/alternate_cbfs.c
@@ -112,70 +112,6 @@ static int sdmmc_cbfs_open(void)
 	return 0;
 }
 
-static int exynos_cbfs_open(struct cbfs_media *media) {
-	return 0;
-}
-
-static int exynos_cbfs_close(struct cbfs_media *media) {
-	return 0;
-}
-
-static size_t exynos_cbfs_read(struct cbfs_media *media, void *dest,
-				   size_t offset, size_t count) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static void *exynos_cbfs_map(struct cbfs_media *media, size_t offset,
-				 size_t count) {
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *exynos_cbfs_unmap(struct cbfs_media *media,
-				   const void *address) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-
-	if (media->context == NULL)
-		return -1;
-
-	media->open = exynos_cbfs_open;
-	media->close = exynos_cbfs_close;
-	media->read = exynos_cbfs_read;
-	media->map = exynos_cbfs_map;
-	media->unmap = exynos_cbfs_unmap;
-
-	return 0;
-}
-
 static struct mem_region_device alternate_rdev = MEM_REGION_DEV_INIT(NULL, 0);
 
 const struct region_device *boot_device_ro(void)
diff --git a/src/soc/samsung/exynos5420/alternate_cbfs.c b/src/soc/samsung/exynos5420/alternate_cbfs.c
index 9e1d323..3cea42a 100644
--- a/src/soc/samsung/exynos5420/alternate_cbfs.c
+++ b/src/soc/samsung/exynos5420/alternate_cbfs.c
@@ -119,70 +119,6 @@ static int sdmmc_cbfs_open(void)
 	return 0;
 }
 
-static int exynos_cbfs_open(struct cbfs_media *media) {
-	return 0;
-}
-
-static int exynos_cbfs_close(struct cbfs_media *media) {
-	return 0;
-}
-
-static size_t exynos_cbfs_read(struct cbfs_media *media, void *dest,
-				   size_t offset, size_t count) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	if (rdev_readat(boot_dev, dest, offset, count) < 0)
-		return 0;
-
-	return count;
-}
-
-static void *exynos_cbfs_map(struct cbfs_media *media, size_t offset,
-				 size_t count) {
-	const struct region_device *boot_dev;
-	void *ptr;
-
-	boot_dev = media->context;
-
-	ptr = rdev_mmap(boot_dev, offset, count);
-
-	if (ptr == NULL)
-		return (void *)-1;
-
-	return ptr;
-}
-
-static void *exynos_cbfs_unmap(struct cbfs_media *media,
-				   const void *address) {
-	const struct region_device *boot_dev;
-
-	boot_dev = media->context;
-
-	rdev_munmap(boot_dev, (void *)address);
-
-	return NULL;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media)
-{
-	boot_device_init();
-
-	media->context = (void *)boot_device_ro();
-
-	if (media->context == NULL)
-		return -1;
-
-	media->open = exynos_cbfs_open;
-	media->close = exynos_cbfs_close;
-	media->read = exynos_cbfs_read;
-	media->map = exynos_cbfs_map;
-	media->unmap = exynos_cbfs_unmap;
-
-	return 0;
-}
-
 static struct mem_region_device alternate_rdev = MEM_REGION_DEV_INIT(NULL, 0);
 
 const struct region_device *boot_device_ro(void)
diff --git a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
index 226578b..6e100a2 100644
--- a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
+++ b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
@@ -98,7 +98,7 @@ static int vboot_loader_active(struct prog *prog)
 		printk(BIOS_DEBUG, "VBOOT: Loading verstage.\n");
 
 		/* load verstage from RO */
-		if (cbfs_load_prog_stage(CBFS_DEFAULT_MEDIA, &verstage))
+		if (cbfs_boot_load_stage(&verstage))
 			die("failed to load verstage");
 
 		/* verify and select a slot */
@@ -186,8 +186,7 @@ static int vboot_prepare(struct prog *prog)
 	/* 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_load_prog_stage(CBFS_DEFAULT_MEDIA, prog) != 0)
+		if (!ENV_RAMSTAGE && cbfs_boot_load_stage(prog) != 0)
 			return -1;
 
 		/* Need to load payload. */
@@ -195,8 +194,7 @@ static int vboot_prepare(struct prog *prog)
 			void *payload;
 			size_t size;
 
-			payload = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-							prog->name,
+			payload = cbfs_boot_map_with_leak(prog->name,
 							CBFS_TYPE_PAYLOAD,
 							&size);
 
@@ -228,7 +226,7 @@ static int vboot_prepare(struct prog *prog)
 
 		prog_set_area(prog, payload_ptr, region_device_sz(&payload));
 	} else {
-		struct region_device stage;
+		struct cbfsf stage;
 		int stage_index = 0;
 
 		if (prog->type == PROG_ROMSTAGE)
@@ -238,30 +236,19 @@ static int vboot_prepare(struct prog *prog)
 		else
 			die("Invalid program type for vboot.");
 
-		if (vboot_fw_region(stage_index, &fw_main, &stage))
+		if (vboot_fw_region(stage_index, &fw_main, &stage.rdev))
 			die("Vboot stage load failed.");
 
 		if (ENV_ROMSTAGE && IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
-			void *stage_ptr;
 			struct rmod_stage_load rmod_ram = {
 				.cbmem_id = CBMEM_ID_RAMSTAGE,
 				.prog = prog,
 			};
 
-			stage_ptr = rdev_mmap_full(&stage);
-
-			if (stage_ptr == NULL)
-				die("Vboot couldn't load stage.");
-
-			if (rmodule_stage_load(&rmod_ram, stage_ptr))
+			if (rmodule_stage_load(&rmod_ram, &stage))
 				die("Vboot couldn't load stage");
-		} else {
-			size_t offset = region_device_offset(&stage);
-
-			if (cbfs_load_prog_stage_by_offset(CBFS_DEFAULT_MEDIA,
-								prog, offset))
+		} else if (cbfs_file_stage_load(&stage, prog))
 				die("Vboot couldn't load stage");
-		}
 	}
 
 	return 0;



More information about the coreboot-gerrit mailing list