[coreboot-gerrit] Patch set updated for coreboot: 0641fc0 fmap: new API using region_device

Aaron Durbin (adurbin@chromium.org) gerrit at coreboot.org
Sat May 16 00:38:19 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/9170

-gerrit

commit 0641fc0bb5c2f531b4ce968600f2f435d9206726
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Sat Mar 28 23:56:22 2015 -0500

    fmap: new API using region_device
    
    Instead of being pointer based use the region infrastrucutre.
    Additionally, this removes the need for arch-specific compilation
    paths. The users of the new API can use the region APIs to memory
    map or read the region provided by the new fmap API.
    
    Change-Id: Ie36e9ff9cb554234ec394b921f029eeed6845aee
    Signed-off-by: Aaron Durbin <adurbin at chromium.org>
---
 src/Kconfig                                        |   9 +
 src/arch/arm/armv7/bootblock_simple.c              |   2 -
 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/elog/elog.c                            |  49 +--
 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                                 | 151 +++++----
 src/include/cbfs_core.h                            | 128 --------
 src/include/fmap.h                                 |  35 ++
 src/include/fmap_serialized.h                      |  73 +++++
 src/include/rmodule.h                              |   4 +-
 src/lib/Makefile.inc                               |  19 +-
 src/lib/cbfs.c                                     | 351 ++++++++++++---------
 src/lib/cbfs_boot_props.c                          |  62 ++++
 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/fmap.c                                     | 121 +++++++
 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         |  40 ++-
 src/mainboard/google/falco/romstage.c              |   4 +-
 src/mainboard/google/link/romstage.c               |   4 +-
 src/mainboard/google/panther/lan.c                 |  24 +-
 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           |  27 +-
 src/northbridge/intel/haswell/raminit.c            |   4 +-
 src/northbridge/intel/i82830/vga.c                 |   3 +-
 src/northbridge/intel/sandybridge/mrccache.c       |  28 +-
 src/northbridge/intel/sandybridge/raminit.c        |   3 +-
 src/soc/intel/baytrail/romstage/raminit.c          |   3 +-
 src/soc/intel/broadwell/romstage/raminit.c         |   3 +-
 src/soc/intel/common/mrc_cache.c                   |  27 +-
 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 ----
 src/vendorcode/google/chromeos/Kconfig             |   9 -
 src/vendorcode/google/chromeos/cros_vpd.c          |  30 +-
 src/vendorcode/google/chromeos/fmap.c              | 157 ---------
 src/vendorcode/google/chromeos/fmap.h              |  79 -----
 .../google/chromeos/vboot2/vboot_handoff.c         |   2 +-
 .../google/chromeos/vboot2/vboot_loader.c          |   8 +-
 src/vendorcode/google/chromeos/vboot_common.c      |  13 +-
 77 files changed, 926 insertions(+), 1921 deletions(-)

diff --git a/src/Kconfig b/src/Kconfig
index 480b8e9..846833e 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -244,6 +244,15 @@ config CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM
 	 The relocated ramstage is saved in an area specified by the
 	 by the board and/or chipset.
 
+config FLASHMAP_OFFSET
+	hex "Flash Map Offset"
+	default 0x00670000 if NORTHBRIDGE_INTEL_SANDYBRIDGE
+	default 0x00610000 if NORTHBRIDGE_INTEL_IVYBRIDGE
+	default CBFS_SIZE if !ARCH_X86
+	default 0
+	help
+	  Offset of flash map in firmware image
+
 choice
 	prompt "Bootblock behaviour"
 	default BOOTBLOCK_SIMPLE
diff --git a/src/arch/arm/armv7/bootblock_simple.c b/src/arch/arm/armv7/bootblock_simple.c
index ff15e24..b8b60c8 100644
--- a/src/arch/arm/armv7/bootblock_simple.c
+++ b/src/arch/arm/armv7/bootblock_simple.c
@@ -46,8 +46,6 @@ void main(void)
 	exception_init();
 #endif
 
-	cbfs_set_header_offset(0);
-
 	bootblock_soc_init();
 	bootblock_mainboard_init();
 
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/elog/elog.c b/src/drivers/elog/elog.c
index d7751c2..332975f 100644
--- a/src/drivers/elog/elog.c
+++ b/src/drivers/elog/elog.c
@@ -26,6 +26,7 @@
 #include <pc80/mc146818rtc.h>
 #endif
 #include <bcd.h>
+#include <fmap.h>
 #include <rtc.h>
 #include <smbios.h>
 #include <spi-generic.h>
@@ -35,7 +36,6 @@
 #include <elog.h>
 #include "elog_internal.h"
 
-#include <vendorcode/google/chromeos/fmap.h>
 
 #if !IS_ENABLED(CONFIG_CHROMEOS) && CONFIG_ELOG_FLASH_BASE == 0
 #error "CONFIG_ELOG_FLASH_BASE is invalid"
@@ -86,26 +86,6 @@ static inline u32 get_rom_size(void)
 }
 
 /*
- * Convert a memory mapped flash address into a flash offset
- */
-static inline u32 elog_flash_address_to_offset(u8 *address)
-{
-#if CONFIG_ARCH_X86
-	/* For x86, assume address is memory-mapped near 4GB */
-	u32 rom_size;
-
-	if (!elog_spi)
-		return 0;
-
-	rom_size = get_rom_size();
-
-	return (u32)address - ((u32)~0UL - rom_size + 1);
-#else
-	return (u32)(uintptr_t)address;
-#endif
-}
-
-/*
  * Pointer to an event log header in the event data area
  */
 static inline struct event_header*
@@ -517,21 +497,22 @@ static void elog_find_flash(void)
 {
 	elog_debug("elog_find_flash()\n");
 
-#if CONFIG_CHROMEOS
-	/* Find the ELOG base and size in FMAP */
-	u8 *flash_base_ptr;
-	int fmap_size = find_fmap_entry("RW_ELOG", (void **)&flash_base_ptr);
-	if (fmap_size < 0) {
-		printk(BIOS_WARNING, "ELOG: Unable to find RW_ELOG in FMAP\n");
-		flash_base = total_size = 0;
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		/* Find the ELOG base and size in FMAP */
+		struct region r;
+
+		if (fmap_locate_area("RW_ELOG", &r) < 0) {
+			printk(BIOS_WARNING,
+				"ELOG: Unable to find RW_ELOG in FMAP\n");
+			flash_base = total_size = 0;
+		} else {
+			flash_base = region_offset(&r);
+			total_size = MIN(region_sz(&r), CONFIG_ELOG_AREA_SIZE);
+		}
 	} else {
-		flash_base = elog_flash_address_to_offset(flash_base_ptr);
-		total_size = MIN(fmap_size, CONFIG_ELOG_AREA_SIZE);
+		flash_base = CONFIG_ELOG_FLASH_BASE;
+		total_size = CONFIG_ELOG_AREA_SIZE;
 	}
-#else
-	flash_base = CONFIG_ELOG_FLASH_BASE;
-	total_size = CONFIG_ELOG_AREA_SIZE;
-#endif
 	log_size = total_size - sizeof(struct elog_header);
 	full_threshold = log_size - ELOG_MIN_AVAILABLE_ENTRIES * MAX_EVENT_SIZE;
 	shrink_size = MIN(total_size * ELOG_SHRINK_PERCENTAGE / 100,
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..15b08f6 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,98 @@
  *
  * 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
+ */
+
+/* Descriptor for cbfs lookup operations. */
+struct cbfsd;
+/* File handle representing a cbfs file. */
+struct cbfsf;
+
+/***********************************************
+ * 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);
+
+/*************************************************
+ * 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. *
+ *******************************************************/
+
+/* 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);
+
+/*****************************************************************
+ * 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;
 };
 
-void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
-			     struct cbfs_media *media,
-			     size_t offset, size_t count);
+struct cbfsf {
+	struct region_device rdev;
+};
 
-void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
-			       const void *address);
+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;
+};
 
-/*
- * 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);
+/* Return < 0 on error otherwise props are filled out accordingly. */
+int cbfs_boot_region_properties(struct cbfs_props *props);
 
-#if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES))
-void cbfs_set_header_offset(size_t offset);
-#else
-static inline void cbfs_set_header_offset(size_t offset) {}
-#endif
 #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/fmap.h b/src/include/fmap.h
new file mode 100644
index 0000000..e575bbf
--- /dev/null
+++ b/src/include/fmap.h
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+#ifndef _FMAP_H_
+#define _FMAP_H_
+
+#include <region.h>
+
+/* Locate the named area in the fmap and fill in a region device representing
+ * that area. The region is a sub-region of the readonly boot media. Return
+ * 0 on success, < 0 on error. */
+int fmap_locate_area_as_rdev(const char *name, struct region_device *area);
+
+/* Locate the named area in the fmap and fill in a region with the
+ * offset and size of that area within the boot media. Return 0 on success,
+ * < 0 on error. */
+int fmap_locate_area(const char *name, struct region *r);
+
+#endif
diff --git a/src/include/fmap_serialized.h b/src/include/fmap_serialized.h
new file mode 100644
index 0000000..3585f0b
--- /dev/null
+++ b/src/include/fmap_serialized.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *    * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *    * 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.
+ *    * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
+ * OWNER 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.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+#ifndef FLASHMAP_SERIALIZED_H__
+#define FLASHMAP_SERIALIZED_H__
+
+#include <stdint.h>
+
+#define FMAP_SIGNATURE		"__FMAP__"
+#define FMAP_VER_MAJOR		1	/* this header's FMAP minor version */
+#define FMAP_VER_MINOR		1	/* this header's FMAP minor version */
+#define FMAP_STRLEN		32	/* maximum length for strings, */
+					/* including null-terminator */
+
+enum fmap_flags {
+	FMAP_AREA_STATIC	= 1 << 0,
+	FMAP_AREA_COMPRESSED	= 1 << 1,
+	FMAP_AREA_RO		= 1 << 2,
+};
+
+/* Mapping of volatile and static regions in firmware binary */
+struct fmap_area {
+	uint32_t offset;                /* offset relative to base */
+	uint32_t size;                  /* size in bytes */
+	uint8_t  name[FMAP_STRLEN];     /* descriptive name */
+	uint16_t flags;                 /* flags for this area */
+}  __attribute__((packed));
+
+struct fmap {
+	uint8_t  signature[8];		/* "__FMAP__" (0x5F5F464D41505F5F) */
+	uint8_t  ver_major;		/* major version */
+	uint8_t  ver_minor;		/* minor version */
+	uint64_t base;			/* address of the firmware binary */
+	uint32_t size;			/* size of firmware binary in bytes */
+	uint8_t  name[FMAP_STRLEN];	/* name of this firmware binary */
+	uint16_t nareas;		/* number of areas described by
+					   fmap_areas[] below */
+	struct fmap_area areas[];
+} __attribute__((packed));
+
+#endif	/* FLASHMAP_SERIALIZED_H__ */
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 11725c4..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
 
@@ -32,12 +33,14 @@ bootblock-y += memcmp.c
 bootblock-y += mem_pool.c
 bootblock-y += region.c
 bootblock-y += boot_device.c
+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
@@ -62,8 +65,10 @@ $(foreach arch,$(ARCH_SUPPORTED),\
 	    $(eval rmodules_$(arch)-y += memcmp.c) \
 	    $(eval rmodules_$(arch)-y += rmodule.ld))
 
+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
@@ -89,6 +94,7 @@ ramstage-y += hardwaremain.c
 ramstage-y += selfboot.c
 ramstage-y += coreboot_table.c
 ramstage-y += bootmem.c
+ramstage-y += fmap.c
 ramstage-y += memchr.c
 ramstage-y += memcmp.c
 ramstage-y += malloc.c
@@ -96,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
@@ -146,9 +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 += cbfs.c cbfs_core.c memcmp.c
+smm-y += fmap.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..b4bcee4 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,258 @@
  *
  * 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>
+
+#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 __attribute__((weak)) boot_device_init(void)
 {
-	return (c <= 9) ? (c + '0') : (c - 10 + 'a');
+	/* Default empty implementation to reduce scattered empty functions. */
 }
 
-static void tohex16(unsigned int val, char* dest)
+int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
 {
-	dest[0] = tohex4(val>>12);
-	dest[1] = tohex4((val>>8) & 0xf);
-	dest[2] = tohex4((val>>4) & 0xf);
-	dest[3] = tohex4(val & 0xf);
-}
+	struct cbfsd cbfs;
+	struct region_device rdev;
+	const struct region_device *boot_dev;
+	struct cbfs_props props;
 
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
-			  uint16_t device, void *dest)
-{
-	char name[17] = "pciXXXX,XXXX.rom";
-	struct cbfs_optionrom *orom;
-	uint8_t *src;
+	boot_device_init();
 
-	tohex16(vendor, name+3);
-	tohex16(device, name+8);
+	if (cbfs_boot_region_properties(&props))
+		return -1;
 
-	orom = (struct cbfs_optionrom *)
-	  cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
+	/* All boot CBFS operations are performed using the RO devie. */
+	boot_dev = boot_device_ro();
 
-	if (orom == NULL)
-		return NULL;
+	if (boot_dev == NULL)
+		return -1;
 
-	/* 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 (rdev_chain(&rdev, boot_dev, props.offset, props.size))
+		return -1;
 
-	/* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
-	src = (uint8_t *)orom; // + sizeof(struct cbfs_optionrom);
+	cbfs.rdev = &rdev;
+	cbfs.align = props.align;
 
-	if (! dest)
-		return src;
+	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_decompress(ntohl(orom->compression),
-			     src,
-			     dest,
-			     ntohl(orom->len)))
+	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);
+}
 
-	LOG("loading stage from %#zx @ 0x%llx (%d bytes), entry @ 0x%llx\n",
-	    offset, stage.load, stage.memlen, stage.entry);
+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;
+		}
 
-	/* Stages rely the below clearing so that the bss is initialized. */
-	memset((void *)(uintptr_t)stage.load, 0, stage.memlen);
+		file.len = ntohl(file.len);
+		file.type = ntohl(file.type);
+		file.offset = ntohl(file.offset);
 
-	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;
+		/* See if names match. */
+		fname = rdev_mmap(rd, offset + fsz, file.offset - fsz);
+
+		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;
 
-	prog_set_area(prog, (void *)(uintptr_t)stage.load, stage.memlen);
-	prog_set_entry(prog, (void *)(uintptr_t)stage.entry, NULL);
+		/* Success. */
+		return 0;
+	}
 
-	return 0;
+	LOG("'%s' not found.\n", name);
+	return -1;
 }
 
-int cbfs_load_prog_stage(struct cbfs_media *media, struct prog *prog)
+#if !ENV_SMM
+static size_t inflate(void *src, void *dst)
 {
-	struct cbfs_file file;
-	ssize_t offset;
-	struct cbfs_media backing_store;
+#if defined(__BOOTBLOCK__)
+		return 0;
+#endif
+#if (defined(__PRE_RAM__) && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE))
+		return 0;
+#endif
+	return ulzma(src, dst);
+}
 
-	if (init_backing_media(&media, &backing_store))
-		return -1;
+static inline int tohex4(unsigned int c)
+{
+	return (c <= 9) ? (c + '0') : (c - 10 + 'a');
+}
 
-	offset = cbfs_locate_file(media, &file, prog->name);
-	if (offset < 0 || file.type != CBFS_TYPE_STAGE)
-		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);
+}
 
-	if (cbfs_load_prog_stage_by_offset(media, prog, offset) < 0)
-		return -1;
+void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
+{
+	char name[17] = "pciXXXX,XXXX.rom";
 
-	return 0;
+	tohex16(vendor, name+3);
+	tohex16(device, name+8);
+
+	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);
+
+	prog_set_area(pstage, load, stage.memlen);
+	prog_set_entry(pstage, entry, NULL);
+
+	return 0;
+}
+#endif /* !ENV_SMM */
diff --git a/src/lib/cbfs_boot_props.c b/src/lib/cbfs_boot_props.c
new file mode 100644
index 0000000..a35ab38
--- /dev/null
+++ b/src/lib/cbfs_boot_props.c
@@ -0,0 +1,62 @@
+/*
+ * 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;
+
+	bdev = boot_device_ro();
+
+	if (bdev == NULL)
+		return -1;
+
+	if (rdev_readat(bdev, &rel_offset, CONFIG_CBFS_SIZE - sizeof(int32_t),
+			sizeof(int32_t)) != sizeof(int32_t))
+		return -1;
+	if (rdev_readat(bdev, &header, CONFIG_CBFS_SIZE + rel_offset,
+			sizeof(header)) != sizeof(header))
+		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/fmap.c b/src/lib/fmap.c
new file mode 100644
index 0000000..c0cbca1
--- /dev/null
+++ b/src/lib/fmap.c
@@ -0,0 +1,121 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2012-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 <console/console.h>
+#include <fmap.h>
+#include <fmap_serialized.h>
+#include <stddef.h>
+#include <string.h>
+
+/*
+ * See http://code.google.com/p/flashmap/ for more information on FMAP.
+ */
+
+static int find_fmap_directory(struct region_device *fmrd)
+{
+	const struct region_device *boot;
+	struct fmap *fmap;
+	size_t fmap_size;
+	size_t offset = CONFIG_FLASHMAP_OFFSET;
+
+	boot = boot_device_ro();
+
+	if (boot == NULL)
+		return -1;
+
+	fmap_size = sizeof(struct fmap);
+
+	fmap = rdev_mmap(boot, offset, fmap_size);
+
+	if (fmap == NULL)
+		return -1;
+
+	if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) {
+		printk(BIOS_DEBUG, "No FMAP found at %zx offset.\n", offset);
+		rdev_munmap(boot, fmap);
+		return -1;
+	}
+
+	printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %zx.\n",
+	       fmap->name, fmap->ver_major, fmap->ver_minor, offset);
+	printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n",
+	       (long long)fmap->base, fmap->size, fmap->nareas);
+
+	fmap_size += fmap->nareas * sizeof(struct fmap_area);
+
+	rdev_munmap(boot, fmap);
+
+	return rdev_chain(fmrd, boot, offset, fmap_size);
+}
+
+int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
+{
+	const struct region_device *boot;
+	struct region ar;
+
+	boot = boot_device_ro();
+
+	if (fmap_locate_area(name, &ar))
+		return -1;
+
+	return rdev_chain(area, boot, ar.offset, ar.size);
+}
+
+int fmap_locate_area(const char *name, struct region *ar)
+{
+	struct region_device fmrd;
+	size_t offset;
+
+	if (find_fmap_directory(&fmrd))
+		return -1;
+
+	/* Start reading the areas just after fmap header. */
+	offset = sizeof(struct fmap);
+
+	while (1) {
+		struct fmap_area *area;
+
+		area = rdev_mmap(&fmrd, offset, sizeof(*area));
+
+		if (area == NULL)
+			return -1;
+
+		if (strcmp((const char *)area->name, name)) {
+			rdev_munmap(&fmrd, area);
+			offset += sizeof(struct fmap_area);
+			continue;
+		}
+
+		printk(BIOS_DEBUG, "FMAP: area %s found\n", name);
+		printk(BIOS_DEBUG, "FMAP:   offset: %x\n", area->offset);
+		printk(BIOS_DEBUG, "FMAP:   size:   %d bytes\n", area->size);
+
+		ar->offset = area->offset;
+		ar->size = area->size;
+
+		rdev_munmap(&fmrd, area);
+
+		return 0;
+	}
+
+	printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
+
+	return -1;
+}
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 f83b33b..2f3cf1d 100644
--- a/src/mainboard/google/butterfly/mainboard.c
+++ b/src/mainboard/google/butterfly/mainboard.c
@@ -20,11 +20,13 @@
 
 #include <types.h>
 #include <string.h>
+#include <cbfs.h>
 #include <device/device.h>
 #include <device/pci_def.h>
 #include <device/pci_ops.h>
 #include <console/console.h>
 #include <drivers/intel/gma/int15.h>
+#include <fmap.h>
 #include <pc80/mc146818rtc.h>
 #include <arch/acpi.h>
 #include <arch/io.h>
@@ -36,11 +38,6 @@
 #include <smbios.h>
 #include <device/pci.h>
 #include <ec/quanta/ene_kb3940q/ec.h>
-#if CONFIG_CHROMEOS
-#include <vendorcode/google/chromeos/fmap.h>
-#else
-#include <cbfs.h>
-#endif
 
 static unsigned int search(char *p, char *a, unsigned int lengthp,
 			   unsigned int lengtha)
@@ -200,20 +197,29 @@ static void mainboard_init(device_t dev)
 	size_t search_length = -1;
 	u16 io_base = 0;
 	struct device *ethernet_dev = NULL;
-#if CONFIG_CHROMEOS
-	char **vpd_region_ptr = NULL;
-	search_length = find_fmap_entry("RO_VPD", (void **)vpd_region_ptr);
-	search_address = (unsigned long)(*vpd_region_ptr);
-#else
-	void *vpd_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "vpd.bin",
-					       CBFS_TYPE_RAW, &search_length);
-	if (vpd_file) {
-		search_address = (unsigned long)vpd_file;
+	void *vpd_file;
+
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		struct region_device rdev;
+
+		if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) {
+			vpd_file = rdev_mmap_full(&rdev);
+
+			if (vpd_file != NULL) {
+				search_length = region_device_sz(&rdev);
+				search_address = (uintptr_t)vpd_file;
+			}
+		}
 	} else {
-		search_length = -1;
-		search_address = 0;
+		vpd_file = cbfs_boot_map_with_leak("vpd.bin", CBFS_TYPE_RAW,
+							&search_length);
+		if (vpd_file) {
+			search_address = (unsigned long)vpd_file;
+		} else {
+			search_length = -1;
+			search_address = 0;
+		}
 	}
-#endif
 
 	/* Initialize the Embedded Controller */
 	butterfly_ec_init();
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 9d9e9be..6dd5bec 100644
--- a/src/mainboard/google/panther/lan.c
+++ b/src/mainboard/google/panther/lan.c
@@ -24,8 +24,8 @@
 #include <console/console.h>
 #include <device/device.h>
 #include <device/pci.h>
+#include <fmap.h>
 #include <southbridge/intel/bd82x6x/pch.h>
-#include <vendorcode/google/chromeos/fmap.h>
 #include "onboard.h"
 
 static unsigned int search(char *p, u8 *a, unsigned int lengthp,
@@ -117,15 +117,23 @@ static void program_mac_address(u16 io_base)
 	u32 high_dword = 0xD0BA00A0;	/* high dword of mac address */
 	u32 low_dword = 0x0000AD0B;	/* low word of mac address as a dword */
 
-#if CONFIG_CHROMEOS
-	search_length = find_fmap_entry("RO_VPD", &search_address);
-#else
-	search_address = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "vpd.bin",
-					       CBFS_TYPE_RAW, &search_length);
-#endif
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		struct region_device rdev;
+
+		if (fmap_locate_area_as_rdev("RO_VPD", &rdev) == 0) {
+			search_address = rdev_mmap_full(&rdev);
+
+			if (search_address != NULL)
+				search_length = region_device_sz(&rdev);
+		}
+	} else {
+		search_address = cbfs_boot_map_with_leak("vpd.bin",
+							CBFS_TYPE_RAW,
+							&search_length);
+	}
 
 	if (search_length <= 0)
-		printk(BIOS_ERR, "LAN: find_fmap_entry returned -1.\n");
+		printk(BIOS_ERR, "LAN: VPD not found.\n");
 	else
 		get_mac_address(&high_dword, &low_dword, search_address,
 				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 8fbac17..1150be2 100644
--- a/src/northbridge/intel/haswell/mrccache.c
+++ b/src/northbridge/intel/haswell/mrccache.c
@@ -22,6 +22,7 @@
 #include <bootstate.h>
 #include <console/console.h>
 #include <cbfs.h>
+#include <fmap.h>
 #include <ip_checksum.h>
 #include <device/device.h>
 #include <cbmem.h>
@@ -29,9 +30,6 @@
 #include "haswell.h"
 #include <spi-generic.h>
 #include <spi_flash.h>
-#if CONFIG_CHROMEOS
-#include <vendorcode/google/chromeos/fmap.h>
-#endif
 
 /* convert a pointer to flash area into the offset inside the flash */
 static inline u32 to_flash_offset(struct spi_flash *flash, void *p) {
@@ -66,16 +64,21 @@ static int is_mrc_cache(struct mrc_data_container *mrc_cache)
  */
 static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 {
-#if CONFIG_CHROMEOS
-	return find_fmap_entry("RW_MRC_CACHE", (void **)mrc_region_ptr);
-#else
-	size_t region_size;
-	*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						"mrc.cache",
-						CBFS_TYPE_MRC_CACHE,
-						&region_size);
+	size_t region_size = 0;
+
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		struct region_device rdev;
+
+		if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev) == 0) {
+			region_size = region_device_sz(&rdev);
+			*mrc_region_ptr = rdev_mmap_full(&rdev);
+		}
+	} else {
+		*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
+							CBFS_TYPE_MRC_CACHE,
+							&region_size);
+	}
 	return region_size;
-#endif
 }
 
 /*
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 4a69233..eb6297a 100644
--- a/src/northbridge/intel/sandybridge/mrccache.c
+++ b/src/northbridge/intel/sandybridge/mrccache.c
@@ -22,6 +22,7 @@
 #include <bootstate.h>
 #include <console/console.h>
 #include <cbfs.h>
+#include <fmap.h>
 #include <ip_checksum.h>
 #include <device/device.h>
 #include <cbmem.h>
@@ -29,9 +30,6 @@
 #include "sandybridge.h"
 #include <spi-generic.h>
 #include <spi_flash.h>
-#if CONFIG_CHROMEOS
-#include <vendorcode/google/chromeos/fmap.h>
-#endif
 
 /* convert a pointer to flash area into the offset inside the flash */
 static inline u32 to_flash_offset(struct spi_flash *flash, void *p) {
@@ -66,17 +64,21 @@ static int is_mrc_cache(struct mrc_data_container *mrc_cache)
  */
 static u32 get_mrc_cache_region(struct mrc_data_container **mrc_region_ptr)
 {
-#if CONFIG_CHROMEOS
-	return find_fmap_entry("RW_MRC_CACHE", (void **)mrc_region_ptr);
-#else
-	size_t region_size;
-	*mrc_region_ptr = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
-						"mrc.cache",
-						CBFS_TYPE_MRC_CACHE,
-						&region_size);
-	return region_size;
-#endif
+	size_t region_size = 0;
+
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		struct region_device rdev;
 
+		if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev) == 0) {
+			region_size = region_device_sz(&rdev);
+			*mrc_region_ptr = rdev_mmap_full(&rdev);
+		}
+	} else {
+		*mrc_region_ptr = cbfs_boot_map_with_leak("mrc.cache",
+							CBFS_TYPE_MRC_CACHE,
+							&region_size);
+	}
+	return 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/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/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/intel/common/mrc_cache.c b/src/soc/intel/common/mrc_cache.c
index f854046..8697e9d 100644
--- a/src/soc/intel/common/mrc_cache.c
+++ b/src/soc/intel/common/mrc_cache.c
@@ -20,10 +20,8 @@
 #include <string.h>
 #include <console/console.h>
 #include <cbmem.h>
+#include <fmap.h>
 #include <ip_checksum.h>
-#if CONFIG_CHROMEOS
-#include <vendorcode/google/chromeos/fmap.h>
-#endif
 #include "mrc_cache.h"
 
 #define MRC_DATA_ALIGN           0x1000
@@ -39,16 +37,23 @@ struct mrc_data_region {
 /* common code */
 static int mrc_cache_get_region(struct mrc_data_region *region)
 {
-#if CONFIG_CHROMEOS
-	int ret;
-	ret = find_fmap_entry("RW_MRC_CACHE", &region->base);
-	if (ret >= 0) {
-		region->size = ret;
+	if (IS_ENABLED(CONFIG_CHROMEOS)) {
+		struct region_device rdev;
+
+		if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev))
+			return -1;
+
+		region->size = region_device_sz(&rdev);
+		region->base = rdev_mmap_full(&rdev);
+
+		if (region->base == NULL)
+			return -1;
+
 		return 0;
+	} else {
+		region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
+		region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
 	}
-#endif
-	region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
-	region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
 	return 0;
 }
 
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 a81b1bb..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;
-
-	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/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/Kconfig b/src/vendorcode/google/chromeos/Kconfig
index 90e3505..1763f46 100644
--- a/src/vendorcode/google/chromeos/Kconfig
+++ b/src/vendorcode/google/chromeos/Kconfig
@@ -92,15 +92,6 @@ config CHROMEOS_RAMOOPS_RAM_SIZE
 	default 0x00100000
 	depends on CHROMEOS_RAMOOPS
 
-config FLASHMAP_OFFSET
-	hex "Flash Map Offset"
-	default 0x00670000 if NORTHBRIDGE_INTEL_SANDYBRIDGE
-	default 0x00610000 if NORTHBRIDGE_INTEL_IVYBRIDGE
-	default CBFS_SIZE if !ARCH_X86
-	default 0
-	help
-	  Offset of flash map in firmware image
-
 config EC_SOFTWARE_SYNC
 	bool "Enable EC software sync"
 	default n
diff --git a/src/vendorcode/google/chromeos/cros_vpd.c b/src/vendorcode/google/chromeos/cros_vpd.c
index c0e4830..fed1d82 100644
--- a/src/vendorcode/google/chromeos/cros_vpd.c
+++ b/src/vendorcode/google/chromeos/cros_vpd.c
@@ -6,12 +6,11 @@
 
 #include <console/console.h>
 
-#include <cbfs.h>
+#include <fmap.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "cros_vpd.h"
-#include "fmap.h"
 #include "lib_vpd.h"
 #include "vpd_tables.h"
 
@@ -35,9 +34,7 @@ static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size)
 	MAYBE_STATIC int result = -1;
 	struct google_vpd_info info;
 	int32_t base;
-
-	const struct fmap_area *area;
-	struct cbfs_media media;
+	struct region_device vpd;
 
 	if (cached) {
 		*vpd_address = cached_address;
@@ -46,32 +43,31 @@ static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size)
 	}
 
 	cached = 1;
-	area = find_fmap_area(fmap_find(), "RO_VPD");
-	if (!area) {
+	if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) {
 		printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", __func__);
 		return result;
 	}
-	if (area->size <= GOOGLE_VPD_2_0_OFFSET + sizeof(info)) {
+
+	base = 0;
+	cached_size = region_device_sz(&vpd);
+
+	if ((cached_size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
+	    rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
+			cached_size - GOOGLE_VPD_2_0_OFFSET)) {
 		printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
-		       __func__, area->size);
+		       __func__, cached_size);
 		return result;
 	}
 
-	base = area->offset + GOOGLE_VPD_2_0_OFFSET;
-	cached_size = area->size - GOOGLE_VPD_2_0_OFFSET;
-	init_default_cbfs_media(&media);
-	media.open(&media);
-
 	/* Try if we can find a google_vpd_info, otherwise read whole VPD. */
-	if (media.read(&media, &info, base, sizeof(info)) == sizeof(info) &&
+	if (rdev_readat(&vpd, &info, base, sizeof(info)) == sizeof(info) &&
 	    memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
 	    == 0 && cached_size >= info.size + sizeof(info)) {
 		base += sizeof(info);
 		cached_size = info.size;
 	}
 
-	cached_address = media.map(&media, base, cached_size);
-	media.close(&media);
+	cached_address = rdev_mmap(&vpd, base, cached_size);
 	if (cached_address) {
 		*vpd_address = cached_address;
 		*vpd_size = cached_size;
diff --git a/src/vendorcode/google/chromeos/fmap.c b/src/vendorcode/google/chromeos/fmap.c
deleted file mode 100644
index 21d439e..0000000
--- a/src/vendorcode/google/chromeos/fmap.c
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2012 The ChromiumOS 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 <stdint.h>
-#include <stddef.h>
-#include <string.h>
-#include <console/console.h>
-#include <cbfs.h>
-#include "fmap.h"
-
-static int is_fmap_signature_valid(const struct fmap *fmap)
-{
-	const char reversed_sig[] = FMAP_REVERSED_SIGNATURE;
-	const char *p2 = reversed_sig + sizeof(FMAP_REVERSED_SIGNATURE) - 2;
-	const char *p1 = (char *)fmap;
-
-	while (p2 >= reversed_sig)
-		if (*p1++ != *p2--) {
-			printk(BIOS_ERR, "No FMAP found at %p.\n", fmap);
-			return 1;
-		}
-
-	printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %p.\n",
-	       fmap->name, fmap->ver_major, fmap->ver_minor, fmap);
-	printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n",
-	       (unsigned long long)fmap->base, fmap->size, fmap->nareas);
-
-	return 0;
-}
-
-/* Find FMAP data structure in ROM.
- * See http://code.google.com/p/flashmap/ for more information on FMAP.
- */
-const struct fmap *fmap_find(void)
-{
-	/* FIXME: Get rid of the hard codes. The "easy" way would be to
-	 * do a binary search, but since ROM accesses are slow, we don't
-	 * want to spend a lot of time looking for the FMAP. An elegant
-	 * solution would be to store a pointer to the FMAP in the CBFS
-	 * master header; that would require some more changes to cbfstool
-	 * and possibly cros_bundle_firmware.
-	 */
-	const struct fmap *fmap;
-	struct cbfs_media media;
-	size_t size;
-
-	if (init_default_cbfs_media(&media)) {
-		printk(BIOS_ERR, "failed to init default cbfs media\n");
-		return NULL;
-	}
-
-	media.open(&media);
-	fmap = media.map(&media, CONFIG_FLASHMAP_OFFSET, sizeof(*fmap));
-
-	if (fmap == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-		printk(BIOS_ERR, "failed to map FMAP header\n");
-		media.close(&media);
-		return NULL;
-	}
-
-	if (is_fmap_signature_valid(fmap)) {
-		media.unmap(&media, fmap);
-		media.close(&media);
-		return NULL;
-	}
-
-	size = sizeof(*fmap) + sizeof(struct fmap_area) * fmap->nareas;
-	media.unmap(&media, fmap);
-	fmap = media.map(&media, CONFIG_FLASHMAP_OFFSET, size);
-
-	if (fmap == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
-		printk(BIOS_ERR, "failed to map FMAP (size=%zu)\n", size);
-		media.unmap(&media, fmap);
-		media.close(&media);
-		return NULL;
-	}
-
-	media.close(&media);
-	return fmap;
-}
-
-const struct fmap_area *find_fmap_area(const struct fmap *fmap,
-							const char name[])
-{
-	const struct fmap_area *area = NULL;
-
-	if (fmap) {
-		int i;
-		for (i = 0; i < fmap->nareas; i++) {
-			if (!strcmp((const char *)fmap->areas[i].name, name)) {
-				area = &fmap->areas[i];
-				break;
-			}
-		}
-	}
-
-	if (area) {
-		printk(BIOS_DEBUG, "FMAP: area %s found\n", name);
-		printk(BIOS_DEBUG, "FMAP:   offset: %x\n", area->offset);
-		printk(BIOS_DEBUG, "FMAP:   size:   %d bytes\n", area->size);
-	} else {
-		printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
-	}
-
-	return area;
-}
-
-int find_fmap_entry(const char name[], void **pointer)
-{
-	MAYBE_STATIC const struct fmap *fmap = NULL;
-	const struct fmap_area *area;
-	void *base = NULL;
-
-	if (!fmap)
-		fmap = fmap_find();
-
-	area = find_fmap_area(fmap, name);
-
-	if (!area)
-		return -1;
-
-	/* Right now cros_bundle_firmware does not write a valid
-	 * base address into the FMAP. Hence, if base is 0, assume
-	 * 4GB-8MB as base address.
-	 */
-	if (fmap->base) {
-		base = (void *)(unsigned long)fmap->base;
-		printk(BIOS_DEBUG, "FMAP: %s base at %p\n", name, base);
-	} else {
-#if CONFIG_ARCH_X86
-		base = (void *)(0 - CONFIG_ROM_SIZE);
-		printk(BIOS_WARNING, "FMAP: No valid base address, using"
-				" 0x%p\n", base);
-#endif
-	}
-
-	*pointer = (void*) ((uintptr_t)base + area->offset);
-	printk(BIOS_DEBUG, "FMAP: %s at %p (offset %x)\n",
-	       name, *pointer, area->offset);
-	return area->size;
-}
diff --git a/src/vendorcode/google/chromeos/fmap.h b/src/vendorcode/google/chromeos/fmap.h
deleted file mode 100644
index 05d3fb6..0000000
--- a/src/vendorcode/google/chromeos/fmap.h
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright 2010, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *    * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *    * 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.
- *    * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
- * OWNER 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.
- *
- * Alternatively, this software may be distributed under the terms of the
- * GNU General Public License ("GPL") version 2 as published by the Free
- * Software Foundation.
- */
-
-#ifndef FLASHMAP_LIB_FMAP_H__
-#define FLASHMAP_LIB_FMAP_H__
-
-#include <stdint.h>
-
-#define FMAP_REVERSED_SIGNATURE	"__PAMF__" /* avoid magic number in .rodata */
-#define FMAP_VER_MAJOR		1	/* this header's FMAP minor version */
-#define FMAP_VER_MINOR		1	/* this header's FMAP minor version */
-#define FMAP_STRLEN		32	/* maximum length for strings, */
-					/* including null-terminator */
-
-enum fmap_flags {
-	FMAP_AREA_STATIC	= 1 << 0,
-	FMAP_AREA_COMPRESSED	= 1 << 1,
-	FMAP_AREA_RO		= 1 << 2,
-};
-
-/* Mapping of volatile and static regions in firmware binary */
-struct fmap_area {
-	uint32_t offset;                /* offset relative to base */
-	uint32_t size;                  /* size in bytes */
-	uint8_t  name[FMAP_STRLEN];     /* descriptive name */
-	uint16_t flags;                 /* flags for this area */
-}  __attribute__((packed));
-
-struct fmap {
-	uint8_t  signature[8];		/* "__FMAP__" (0x5F5F464D41505F5F) */
-	uint8_t  ver_major;		/* major version */
-	uint8_t  ver_minor;		/* minor version */
-	uint64_t base;			/* address of the firmware binary */
-	uint32_t size;			/* size of firmware binary in bytes */
-	uint8_t  name[FMAP_STRLEN];	/* name of this firmware binary */
-	uint16_t nareas;		/* number of areas described by
-					   fmap_areas[] below */
-	struct fmap_area areas[];
-} __attribute__((packed));
-
-
-/* coreboot specific function prototypes */
-const struct fmap *fmap_find(void);
-const struct fmap_area *find_fmap_area(const struct fmap *fmap,
-							const char name[]);
-int find_fmap_entry(const char name[], void **pointer);
-#endif	/* FLASHMAP_LIB_FMAP_H__*/
diff --git a/src/vendorcode/google/chromeos/vboot2/vboot_handoff.c b/src/vendorcode/google/chromeos/vboot2/vboot_handoff.c
index 9952c00..a20f85e 100644
--- a/src/vendorcode/google/chromeos/vboot2/vboot_handoff.c
+++ b/src/vendorcode/google/chromeos/vboot2/vboot_handoff.c
@@ -26,13 +26,13 @@
 #include <cbmem.h>
 #include <console/console.h>
 #include <console/vtxprintf.h>
+#include <fmap.h>
 #include <stdlib.h>
 #include <timestamp.h>
 #define NEED_VB20_INTERNALS  /* TODO: remove me! */
 #include <vb2_api.h>
 #include <vboot_struct.h>
 #include "../chromeos.h"
-#include "../fmap.h"
 #include "../vboot_handoff.h"
 #include "misc.h"
 
diff --git a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
index 2ab3ba6..9ce523c 100644
--- a/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
+++ b/src/vendorcode/google/chromeos/vboot2/vboot_loader.c
@@ -97,7 +97,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 */
@@ -175,8 +175,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. */
@@ -184,8 +183,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);
 
diff --git a/src/vendorcode/google/chromeos/vboot_common.c b/src/vendorcode/google/chromeos/vboot_common.c
index fba4676..c64005d 100644
--- a/src/vendorcode/google/chromeos/vboot_common.c
+++ b/src/vendorcode/google/chromeos/vboot_common.c
@@ -22,27 +22,20 @@
 #include <cbmem.h>
 #include <console/cbmem_console.h>
 #include <console/console.h>
+#include <fmap.h>
 #include <reset.h>
 #include <stddef.h>
 #include <string.h>
 
 #include "chromeos.h"
-#include "fmap.h"
 #include "vboot_common.h"
 #include "vboot_handoff.h"
 
 void vboot_locate_region(const char *name, struct region *region)
 {
-	const struct fmap_area *area;
 
-	region->size = 0;
-
-	area = find_fmap_area(fmap_find(), name);
-
-	if (area != NULL) {
-		region->offset = area->offset;
-		region->size = area->size;
-	}
+	if (fmap_locate_area(name, region))
+		region->size = 0;
 }
 
 void *vboot_get_region(size_t offset, size_t size, void *dest)



More information about the coreboot-gerrit mailing list