[coreboot-gerrit] Patch set updated for coreboot: libpayload: replace cbfs_media api with region_device api
Antonello Dettori (dev@dettori.io)
gerrit at coreboot.org
Fri Aug 26 19:55:22 CEST 2016
Antonello Dettori (dev at dettori.io) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/15774
-gerrit
commit 300d3aeba245a6d573c199ca83aeceb968958be2
Author: Antonello Dettori <dev at dettori.io>
Date: Thu Jul 21 15:30:46 2016 +0200
libpayload: replace cbfs_media api with region_device api
Replace all the functionality provided by the cbfs_image api with
the more recent region_device api used throughout the rest of the
coreboot project.
Change-Id: Ia79f532a9f8701f6c586609d38f6a88bfdf116e9
Signed-off-by: Antonello Dettori <dev at dettori.io>
---
payloads/libpayload/arch/arm/dummy_media.c | 5 +-
payloads/libpayload/arch/arm64/dummy_media.c | 5 +-
payloads/libpayload/arch/mips/dummy_media.c | 5 +-
payloads/libpayload/arch/x86/rom_media.c | 85 +-----
payloads/libpayload/drivers/video/graphics.c | 1 -
payloads/libpayload/include/cbfs.h | 13 +-
payloads/libpayload/include/cbfs_core.h | 40 +--
payloads/libpayload/include/cbfs_ram.h | 57 ----
payloads/libpayload/include/mem_pool.h | 69 +++++
payloads/libpayload/include/region_device.h | 248 +++++++++++++++
payloads/libpayload/include/stddef.h | 2 +
payloads/libpayload/include/stdlib.h | 19 ++
payloads/libpayload/libc/fmap.c | 16 +-
payloads/libpayload/libcbfs/Makefile.inc | 3 +-
payloads/libpayload/libcbfs/cbfs.c | 36 ++-
payloads/libpayload/libcbfs/cbfs_core.c | 117 ++++----
payloads/libpayload/libcbfs/mem_pool.c | 48 +++
payloads/libpayload/libcbfs/ram_media.c | 118 --------
payloads/libpayload/libcbfs/region_device.c | 434 +++++++++++++++++++++++++++
19 files changed, 941 insertions(+), 380 deletions(-)
diff --git a/payloads/libpayload/arch/arm/dummy_media.c b/payloads/libpayload/arch/arm/dummy_media.c
index c67344f..e74d4c7 100644
--- a/payloads/libpayload/arch/arm/dummy_media.c
+++ b/payloads/libpayload/arch/arm/dummy_media.c
@@ -34,10 +34,7 @@
/* The generic cbfs code relies on the libpayload_init_default_cbfs_media
* symbol. Therefore, provide an implementation that just throws an error. */
-int libpayload_init_default_cbfs_media(struct cbfs_media *media);
-
-__attribute__((weak)) int libpayload_init_default_cbfs_media(
- struct cbfs_media *media)
+__attribute__((weak)) int libpayload_init_default_cbfs_media(void)
{
return -1;
}
diff --git a/payloads/libpayload/arch/arm64/dummy_media.c b/payloads/libpayload/arch/arm64/dummy_media.c
index c67344f..e74d4c7 100644
--- a/payloads/libpayload/arch/arm64/dummy_media.c
+++ b/payloads/libpayload/arch/arm64/dummy_media.c
@@ -34,10 +34,7 @@
/* The generic cbfs code relies on the libpayload_init_default_cbfs_media
* symbol. Therefore, provide an implementation that just throws an error. */
-int libpayload_init_default_cbfs_media(struct cbfs_media *media);
-
-__attribute__((weak)) int libpayload_init_default_cbfs_media(
- struct cbfs_media *media)
+__attribute__((weak)) int libpayload_init_default_cbfs_media(void)
{
return -1;
}
diff --git a/payloads/libpayload/arch/mips/dummy_media.c b/payloads/libpayload/arch/mips/dummy_media.c
index 112d7fe..1732b75 100644
--- a/payloads/libpayload/arch/mips/dummy_media.c
+++ b/payloads/libpayload/arch/mips/dummy_media.c
@@ -33,10 +33,7 @@
/* The generic cbfs code relies on the libpayload_init_default_cbfs_media
* symbol. Therefore, provide an implementation that just throws an error. */
-int libpayload_init_default_cbfs_media(struct cbfs_media *media);
-
-__attribute__((weak)) int libpayload_init_default_cbfs_media(
- struct cbfs_media *media)
+__attribute__((weak)) int libpayload_init_default_cbfs_media(void)
{
return -1;
}
diff --git a/payloads/libpayload/arch/x86/rom_media.c b/payloads/libpayload/arch/x86/rom_media.c
index 0c6f521..48c1669 100644
--- a/payloads/libpayload/arch/x86/rom_media.c
+++ b/payloads/libpayload/arch/x86/rom_media.c
@@ -26,86 +26,31 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-#define LIBPAYLOAD
#include <cbfs.h>
#include <string.h>
+#include <sysinfo.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
+static struct mem_region_device boot_dev;
-// 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;
- // Some address (ex, pointer to master header) may be given in memory
- // mapped location. To workaround that, we handle >0xf0000000 as real
- // memory pointer.
-
- if ((uint32_t)offset > (uint32_t)0xf0000000)
- ptr = (void*)offset;
- else
- ptr = (void*)(0 - (uint32_t)media->context + offset);
- return ptr;
+struct region_device *boot_device_ro(void)
+{
+ return &boot_dev.rdev;
}
-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 = x86_rom_map(media, offset, count);
- memcpy(dest, ptr, count);
- x86_rom_unmap(media, ptr);
- return count;
-}
+static int init_x86rom_cbfs_media(void)
+{
+ /* The ROM is memory mapped just below 4GiB.
+ * Form a pointer for the base. */
+ size_t romsize = lib_sysinfo.boot_media_size;
+ void *rombase = (void *)(uintptr_t)(0x100000000ULL - romsize);
-static int x86_rom_close(struct cbfs_media *media) {
- return 0;
-}
+ mem_region_device_ro_init(&boot_dev, rombase, romsize);
-int init_x86rom_cbfs_media(struct cbfs_media *media);
-int init_x86rom_cbfs_media(struct cbfs_media *media) {
- // On X86, we always keep a reference of pointer to CBFS header in
- // 0xfffffffc, and the pointer is still a memory-mapped address.
- // Since the CBFS core always use ROM offset, we need to figure out
- // header->romsize even before media is initialized.
- struct cbfs_header *header = (struct cbfs_header*)
- *(uint32_t*)(0xfffffffc);
- if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
-#if IS_ENABLED(CONFIG_LP_ROM_SIZE)
- printk(BIOS_ERR, "Invalid CBFS master header at %p\n", header);
- media->context = (void*)CONFIG_LP_ROM_SIZE;
-#else
- return -1;
-#endif
- } else {
- uint32_t romsize = ntohl(header->romsize);
- media->context = (void*)romsize;
-#if IS_ENABLED(CONFIG_LP_ROM_SIZE)
- if (CONFIG_LP_ROM_SIZE != romsize)
- printk(BIOS_INFO, "Warning: rom size unmatch (%d/%d)\n",
- CONFIG_LP_ROM_SIZE, romsize);
-#endif
- }
- 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);
+int libpayload_init_default_cbfs_media(void)
+{
+ return init_x86rom_cbfs_media();
}
diff --git a/payloads/libpayload/drivers/video/graphics.c b/payloads/libpayload/drivers/video/graphics.c
index 943f8ed..3d93c88 100644
--- a/payloads/libpayload/drivers/video/graphics.c
+++ b/payloads/libpayload/drivers/video/graphics.c
@@ -52,7 +52,6 @@ static uint8_t *fbaddr;
#define PIVOT_H_MASK (PIVOT_H_LEFT|PIVOT_H_CENTER|PIVOT_H_RIGHT)
#define PIVOT_V_MASK (PIVOT_V_TOP|PIVOT_V_CENTER|PIVOT_V_BOTTOM)
#define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
-#define ABS(x) ((x) < 0 ? -(x) : (x))
static char initialized = 0;
diff --git a/payloads/libpayload/include/cbfs.h b/payloads/libpayload/include/cbfs.h
index 801bdfc..12ea408 100644
--- a/payloads/libpayload/include/cbfs.h
+++ b/payloads/libpayload/include/cbfs.h
@@ -53,11 +53,11 @@ const struct cbfs_header *get_cbfs_header(void);
struct cbfs_file *cbfs_find(const char *name);
void *cbfs_find_file(const char *name, int type);
-int cbfs_execute_stage(struct cbfs_media *media, const char *name);
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
+int cbfs_execute_stage(struct region_device *media, const char *name);
+void *cbfs_load_optionrom(struct region_device *media, uint16_t vendor,
uint16_t device);
-void *cbfs_load_payload(struct cbfs_media *media, const char *name);
-void *cbfs_load_stage(struct cbfs_media *media, const char *name);
+void *cbfs_load_payload(struct region_device *media, const char *name);
+void *cbfs_load_stage(struct region_device *media, const char *name);
/* Simple buffer for streaming media. */
struct cbfs_simple_buffer {
@@ -68,7 +68,7 @@ struct cbfs_simple_buffer {
};
void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
- struct cbfs_media *media,
+ struct region_device *media,
size_t offset, size_t count);
void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
@@ -78,6 +78,7 @@ void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
int run_address(void *f);
/* Defined in individual arch / board implementation. */
-int init_default_cbfs_media(struct cbfs_media *media);
+int libpayload_init_default_cbfs_media(void);
+struct region_device *boot_device_ro(void);
#endif
diff --git a/payloads/libpayload/include/cbfs_core.h b/payloads/libpayload/include/cbfs_core.h
index f94056d..b7234cd 100644
--- a/payloads/libpayload/include/cbfs_core.h
+++ b/payloads/libpayload/include/cbfs_core.h
@@ -50,6 +50,8 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
+#include <region_device.h>
+
/** These are standard values for the known compression
alogrithms that coreboot knows about for stages and
@@ -211,43 +213,14 @@ struct cbfs_optionrom {
uint32_t len;
} __attribute__((packed));
-#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);
-};
-
/*
* Returns pointer to a copy of the file content or NULL on error.
* If the file is compressed, data will be decompressed.
* The caller owns the returned memory.
*/
-void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
+void *cbfs_get_file_content(struct region_device *media, const char *name,
int type, size_t *sz);
/* returns decompressed size on success, 0 on failure */
@@ -255,11 +228,11 @@ 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);
+const struct cbfs_header *cbfs_get_header(struct region_device *media);
/* Persistent handle to a CBFS file that has not yet been fully mapped. */
struct cbfs_handle {
- struct cbfs_media media; /* copy of original media object */
+ struct region_device *media; /* pointer to original media object */
u32 type; /* CBFS file type */
u32 media_offset; /* offset from beginning of media */
u32 attribute_offset; /* relative offset of attributes */
@@ -269,7 +242,8 @@ struct cbfs_handle {
/* Returns handle to CBFS file, or NULL on error. Does not yet map contents.
* Caller is responsible to free() returned handle after use. */
-struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name);
+struct cbfs_handle *cbfs_get_handle(struct region_device *media,
+ const char *name);
/* Given a cbfs_handle and an attribute tag, return a mapping for the first
* instance of the attribute or NULL if none found. */
diff --git a/payloads/libpayload/include/cbfs_ram.h b/payloads/libpayload/include/cbfs_ram.h
deleted file mode 100644
index 72839fe..0000000
--- a/payloads/libpayload/include/cbfs_ram.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * This file is part of the libpayload project.
- *
- * Copyright (C) 2013 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.
- *
- * 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.
- * ---------------------------------------------------------------------------
- * 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_RAM_H_
-#define _CBFS_RAM_H_
-
-#include <stdint.h>
-
-struct cbfs_media;
-
-/* The following functions return 0 for success. None-zero on error. */
-int init_cbfs_ram_media(struct cbfs_media *media, void *start, size_t size);
-int setup_cbfs_from_ram(void *start, uint32_t size);
-int setup_cbfs_from_flash(void);
-
-#endif /* _CBFS_RAM_H_ */
diff --git a/payloads/libpayload/include/mem_pool.h b/payloads/libpayload/include/mem_pool.h
new file mode 100644
index 0000000..c21fa0e
--- /dev/null
+++ b/payloads/libpayload/include/mem_pool.h
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+#ifndef _MEM_POOL_H_
+#define _MEM_POOL_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+/*
+ * The memory pool allows one to allocate memory from a fixed size buffer
+ * that also allows freeing semantics for reuse. However, the current
+ * limitation is that the most recent allocation is the only one that
+ * can be freed. If one tries to free any allocation that isn't the
+ * most recently allocated it will result in a leak within the memory pool.
+ *
+ * The memory returned by allocations are at least 8 byte aligned. Note
+ * that this requires the backing buffer to start on at least an 8 byte
+ * alignment.
+ */
+
+struct mem_pool {
+ uint8_t *buf;
+ size_t size;
+ uint8_t *last_alloc;
+ size_t free_offset;
+};
+
+#define MEM_POOL_INIT(buf_, size_) \
+ { \
+ .buf = (buf_), \
+ .size = (size_), \
+ .last_alloc = NULL, \
+ .free_offset = 0, \
+ }
+
+static inline void mem_pool_reset(struct mem_pool *mp)
+{
+ mp->last_alloc = NULL;
+ mp->free_offset = 0;
+}
+
+/* Initialize a memory pool. */
+static inline void mem_pool_init(struct mem_pool *mp, void *buf, size_t sz)
+{
+ mp->buf = buf;
+ mp->size = sz;
+ mem_pool_reset(mp);
+}
+
+/* Allocate requested size from the memory pool. NULL returned on error. */
+void *mem_pool_alloc(struct mem_pool *mp, size_t sz);
+
+/* Free allocation from memory pool. */
+void mem_pool_free(struct mem_pool *mp, void *alloc);
+
+#endif /* _MEM_POOL_H_ */
diff --git a/payloads/libpayload/include/region_device.h b/payloads/libpayload/include/region_device.h
new file mode 100644
index 0000000..8926ecf
--- /dev/null
+++ b/payloads/libpayload/include/region_device.h
@@ -0,0 +1,248 @@
+/*
+ * 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.
+ */
+
+#ifndef _REGION_H_
+#define _REGION_H_
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <mem_pool.h>
+
+/*
+ * Region support.
+ *
+ * Regions are intended to abstract away the access mechanisms for blocks of
+ * data. This could be SPI, eMMC, or a memory region as the backing store.
+ * They are accessed through a region_device. Subregions can be made by
+ * chaining together multiple region_devices.
+ */
+
+struct region_device;
+
+/*
+ * Returns NULL on error otherwise a buffer is returned with the conents of
+ * the requested data at offset of size.
+ */
+void *rdev_mmap(const struct region_device *rd, size_t offset, size_t size);
+
+/* Unmap a previously mapped area. Returns 0 on success, < 0 on error. */
+int rdev_munmap(const struct region_device *rd, void *mapping);
+
+/*
+ * Returns < 0 on error otherwise returns size of data read at provided
+ * offset filling in the buffer passed.
+ */
+ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset,
+ size_t size);
+
+/*
+ * Returns < 0 on error otherwise returns size of data wrote at provided
+ * offset from the buffer passed.
+ */
+ssize_t rdev_writeat(const struct region_device *rd, void *b, size_t offset,
+ size_t size);
+
+/*
+ * Returns < 0 on error otherwise returns size of data erased.
+ * If eraseat ops is not defined it returns size which indicates
+ * that operation was successful.
+ */
+ssize_t rdev_eraseat(const struct region_device *rd, size_t offset,
+ size_t size);
+
+/****************************************
+ * Implementation of a region device *
+ ****************************************/
+
+/*
+ * Create a child region of the parent provided the sub-region is within
+ * the parent's region. Returns < 0 on error otherwise 0 on success. Note
+ * that the child device only calls through the parent's operations.
+ */
+int rdev_chain(struct region_device *child, const struct region_device *parent,
+ size_t offset, size_t size);
+
+
+/* A region_device operations. */
+struct region_device_ops {
+ void *(*mmap)(const struct region_device *, size_t, size_t);
+ int (*munmap)(const struct region_device *, void *);
+ ssize_t (*readat)(const struct region_device *, void *, size_t, size_t);
+ ssize_t (*writeat)(const struct region_device *, void *, size_t,
+ size_t);
+ ssize_t (*eraseat)(const struct region_device *, size_t, size_t);
+};
+
+struct region {
+ size_t offset;
+ size_t size;
+};
+
+struct region_device {
+ const struct region_device *root;
+ const struct region_device_ops *ops;
+ struct region region;
+};
+
+#define REGION_DEV_INIT(ops_, offset_, size_) \
+ { \
+ .root = NULL, \
+ .ops = (ops_), \
+ .region = { \
+ .offset = (offset_), \
+ .size = (size_), \
+ }, \
+ }
+
+/* Helper to dynamically initialize region device. */
+void region_device_init(struct region_device *rdev,
+ const struct region_device_ops *ops, size_t offset,
+ size_t size);
+
+static inline size_t region_offset(const struct region *r)
+{
+ return r->offset;
+}
+
+static inline size_t region_sz(const struct region *r)
+{
+ return r->size;
+}
+
+static inline const struct region *region_device_region(
+ const struct region_device *rdev)
+{
+ return &rdev->region;
+}
+
+static inline size_t region_device_sz(const struct region_device *rdev)
+{
+ return region_sz(region_device_region(rdev));
+}
+
+static inline size_t region_device_offset(const struct region_device *rdev)
+{
+ return region_offset(region_device_region(rdev));
+}
+
+/* Memory map entire region device. Same semantics as rdev_mmap() above. */
+static inline void *rdev_mmap_full(const struct region_device *rd)
+{
+ return rdev_mmap(rd, 0, region_device_sz(rd));
+}
+
+/*
+ * Compute relative offset of the child (c) w.r.t. the parent (p). Returns < 0
+ * when child is not within the parent's region.
+ */
+ssize_t rdev_relative_offset(const struct region_device *p,
+ const struct region_device *c);
+
+struct mem_region_device {
+ char *base;
+ struct region_device rdev;
+};
+
+/* Inititalize at runtime a mem_region_device. This would be used when
+ * the base and size are dynamic or can't be known during linking.
+ * There are two variants: read-only and read-write. */
+void mem_region_device_ro_init(struct mem_region_device *mdev, void *base,
+ size_t size);
+
+void mem_region_device_rw_init(struct mem_region_device *mdev, void *base,
+ size_t size);
+
+extern const struct region_device_ops mem_rdev_ro_ops;
+
+extern const struct region_device_ops mem_rdev_rw_ops;
+
+/* Statically initialize mem_region_device. */
+#define MEM_REGION_DEV_INIT(base_, size_, ops_) \
+ { \
+ .base = (void *)(base_), \
+ .rdev = REGION_DEV_INIT((ops_), 0, (size_)), \
+ }
+
+#define MEM_REGION_DEV_RO_INIT(base_, size_) \
+ MEM_REGION_DEV_INIT(base_, size_, &mem_rdev_ro_ops) \
+
+#define MEM_REGION_DEV_RW_INIT(base_, size_) \
+ MEM_REGION_DEV_INIT(base_, size_, &mem_rdev_rw_ops) \
+
+struct mmap_helper_region_device {
+ struct mem_pool pool;
+ struct region_device rdev;
+};
+
+#define MMAP_HELPER_REGION_INIT(ops_, offset_, size_) \
+ { \
+ .rdev = REGION_DEV_INIT((ops_), (offset_), (size_)), \
+ }
+
+void mmap_helper_device_init(struct mmap_helper_region_device *mdev,
+ void *cache, size_t cache_size);
+
+void *mmap_helper_rdev_mmap(const struct region_device *, size_t, size_t);
+int mmap_helper_rdev_munmap(const struct region_device *, void *);
+
+/* A translated region device provides the ability to publish a region device
+ * in one address space and use an access mechanism within another address
+ * space. The sub region is the window within the 1st address space and
+ * the request is modified prior to accessing the second address space
+ * provided by access_dev. */
+struct xlate_region_device {
+ const struct region_device *access_dev;
+ struct region sub_region;
+ struct region_device rdev;
+};
+
+extern const struct region_device_ops xlate_rdev_ro_ops;
+
+extern const struct region_device_ops xlate_rdev_rw_ops;
+
+#define XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, sub_size_, \
+ parent_sz_, ops_) \
+ { \
+ .access_dev = access_dev_, \
+ .sub_region = { \
+ .offset = (sub_offset_), \
+ .size = (sub_size_), \
+ }, \
+ .rdev = REGION_DEV_INIT((ops_), 0, (parent_sz_)), \
+ }
+
+#define XLATE_REGION_DEV_RO_INIT(access_dev_, sub_offset_, sub_size_, \
+ parent_sz_) \
+ XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, \
+ sub_size_, parent_sz_, &xlate_rdev_ro_ops), \
+
+#define XLATE_REGION_DEV_RW_INIT(access_dev_, sub_offset_, sub_size_, \
+ parent_sz_) \
+ XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, \
+ sub_size_, parent_sz_, &xlate_rdev_rw_ops), \
+
+/* Helper to dynamically initialize xlate region device. */
+void xlate_region_device_ro_init(struct xlate_region_device *xdev,
+ const struct region_device *access_dev,
+ size_t sub_offset, size_t sub_size,
+ size_t parent_size);
+
+void xlate_region_device_rw_init(struct xlate_region_device *xdev,
+ const struct region_device *access_dev,
+ size_t sub_offset, size_t sub_size,
+ size_t parent_size);
+
+#endif /* _REGION_H_ */
diff --git a/payloads/libpayload/include/stddef.h b/payloads/libpayload/include/stddef.h
index fe455b8..a35bd68 100644
--- a/payloads/libpayload/include/stddef.h
+++ b/payloads/libpayload/include/stddef.h
@@ -20,6 +20,8 @@ typedef __SIZE_TYPE__ ssize_t;
offsetof(struct structure, member) == offset, \
"`struct " #structure "` offset for `" #member "` is not " #offset)
+#define __unused __attribute__((unused))
+
/* Standard units. */
#define KiB (1<<10)
#define MiB (1<<20)
diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h
index 689bf01..cf9156f 100644
--- a/payloads/libpayload/include/stdlib.h
+++ b/payloads/libpayload/include/stdlib.h
@@ -41,6 +41,25 @@
#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1UL))
#define IS_ALIGNED(x,a) (((x) & ((typeof(x))(a)-1UL)) == 0)
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define ABS(a) (((a) < 0) ? (-(a)) : (a))
+#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b))
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
+
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @param ptr: the pointer to the member.
+ * @param type: the type of the container struct this is embedded in.
+ * @param member: the name of the member within the struct.
+ *
+ */
+#define container_of(ptr, type, member) ({ \
+ const __typeof__(((type *)0)->member) * __mptr = (ptr); \
+ (type *)((char *)__mptr - offsetof(type, member)); })
+
/**
* @defgroup malloc Memory allocation functions
* @{
diff --git a/payloads/libpayload/libc/fmap.c b/payloads/libpayload/libc/fmap.c
index af9902e..cf0c57c 100644
--- a/payloads/libpayload/libc/fmap.c
+++ b/payloads/libpayload/libc/fmap.c
@@ -41,15 +41,13 @@ int fmap_region_by_name(const uint32_t fmap_offset, const char * const name,
struct fmap *fmap;
struct fmap fmap_head;
- struct cbfs_media default_media;
- struct cbfs_media *media = &default_media;
+ struct region_device default_media;
+ struct region_device *media = &default_media;
- if (init_default_cbfs_media(media) != 0)
- return -1;
-
- media->open(media);
+ libpayload_init_default_cbfs_media();
+ media = boot_device_ro();
- if (!media->read(media, &fmap_head, fmap_offset, sizeof(fmap_head)))
+ if (rdev_readat(media, &fmap_head, fmap_offset, sizeof(fmap_head)) < 0)
return -1;
if (memcmp(fmap_head.signature, FMAP_SIGNATURE, sizeof(fmap_head.signature))) {
@@ -63,11 +61,9 @@ int fmap_region_by_name(const uint32_t fmap_offset, const char * const name,
if (!fmap)
return -1;
- if (!media->read(media, fmap, fmap_offset, fmap_size))
+ if (rdev_readat(media, fmap, fmap_offset, fmap_size) < 0)
goto err;
- media->close(media);
-
for (i = 0; i < fmap->nareas; i++) {
if (strcmp((const char *)fmap->areas[i].name, name) != 0)
continue;
diff --git a/payloads/libpayload/libcbfs/Makefile.inc b/payloads/libpayload/libcbfs/Makefile.inc
index 133b215..4a3ce1e 100644
--- a/payloads/libpayload/libcbfs/Makefile.inc
+++ b/payloads/libpayload/libcbfs/Makefile.inc
@@ -28,4 +28,5 @@
##
libcbfs-$(CONFIG_LP_CBFS) += cbfs.c
-libcbfs-$(CONFIG_LP_CBFS) += ram_media.c
+libcbfs-$(CONFIG_LP_CBFS) += region_device.c
+libcbfs-$(CONFIG_LP_CBFS) += mem_pool.c
diff --git a/payloads/libpayload/libcbfs/cbfs.c b/payloads/libpayload/libcbfs/cbfs.c
index 3cce799..650d17a 100644
--- a/payloads/libpayload/libcbfs/cbfs.c
+++ b/payloads/libpayload/libcbfs/cbfs.c
@@ -83,7 +83,7 @@ static void tohex16(unsigned int val, char* dest)
dest[3] = tohex4(val & 0xf);
}
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
+void *cbfs_load_optionrom(struct region_device *media, uint16_t vendor,
uint16_t device)
{
char name[17] = "pciXXXX,XXXX.rom";
@@ -94,7 +94,7 @@ void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
return cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
}
-void * cbfs_load_stage(struct cbfs_media *media, const char *name)
+void *cbfs_load_stage(struct region_device *media, const char *name)
{
struct cbfs_stage *stage = (struct cbfs_stage *)
cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
@@ -134,7 +134,8 @@ out:
return (void *) entry;
}
-int cbfs_execute_stage(struct cbfs_media *media, const char *name)
+
+int cbfs_execute_stage(struct region_device *media, const char *name)
{
struct cbfs_stage *stage = (struct cbfs_stage *)
cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
@@ -155,23 +156,23 @@ int cbfs_execute_stage(struct cbfs_media *media, const char *name)
return result;
}
-void *cbfs_load_payload(struct cbfs_media *media, const char *name)
+void *cbfs_load_payload(struct region_device *media, const char *name)
{
return (struct cbfs_payload *)cbfs_get_file_content(
media, name, CBFS_TYPE_PAYLOAD, NULL);
}
-struct cbfs_file *cbfs_find(const char *name) {
+struct cbfs_file *cbfs_find(const char *name)
+{
struct cbfs_handle *handle = cbfs_get_handle(CBFS_DEFAULT_MEDIA, name);
- struct cbfs_media *m = &handle->media;
void *ret;
if (!handle)
return NULL;
- ret = m->map(m, handle->media_offset,
+ ret = rdev_mmap(handle->media, handle->media_offset,
handle->content_offset + handle->content_size);
- if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
+ if (ret == NULL) {
free(handle);
return NULL;
}
@@ -180,30 +181,32 @@ struct cbfs_file *cbfs_find(const char *name) {
return ret;
}
-void *cbfs_find_file(const char *name, int type) {
+void *cbfs_find_file(const char *name, int type)
+{
return cbfs_get_file_content(CBFS_DEFAULT_MEDIA, name, type, NULL);
}
-const struct cbfs_header *get_cbfs_header(void) {
+const struct cbfs_header *get_cbfs_header(void)
+{
return cbfs_get_header(CBFS_DEFAULT_MEDIA);
}
/* Simple buffer */
void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
- struct cbfs_media *media,
- size_t offset, size_t count) {
+ struct region_device *media, size_t offset, size_t count)
+{
void *address = buffer->buffer + buffer->allocated;;
DEBUG("simple_buffer_map(offset=%zu, count=%zu): "
"allocated=%zu, size=%zu, last_allocate=%zu\n",
offset, count, buffer->allocated, buffer->size,
buffer->last_allocate);
if (buffer->allocated + count >= buffer->size)
- return CBFS_MEDIA_INVALID_MAP_ADDRESS;
- if (media->read(media, address, offset, count) != count) {
+ return NULL;
+ if (rdev_readat(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;
+ return NULL;
}
buffer->allocated += count;
buffer->last_allocate = count;
@@ -211,7 +214,8 @@ void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
}
void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
- const void *address) {
+ const void *address)
+{
// TODO Add simple buffer management so we can free more than last
// allocated one.
DEBUG("simple_buffer_unmap(address=0x%p): "
diff --git a/payloads/libpayload/libcbfs/cbfs_core.c b/payloads/libpayload/libcbfs/cbfs_core.c
index 91f2603..b327d7e 100644
--- a/payloads/libpayload/libcbfs/cbfs_core.c
+++ b/payloads/libpayload/libcbfs/cbfs_core.c
@@ -54,32 +54,29 @@
/* 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)
+const struct cbfs_header *cbfs_get_header(struct region_device *media)
{
int32_t rel_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;
- }
+ libpayload_init_default_cbfs_media();
+ media = boot_device_ro();
}
- media->open(media);
- if (!media->read(media, &rel_offset, (size_t)(0 - sizeof(int32_t)),
- sizeof(int32_t))) {
- ERROR("Could not read CBFS master header offset!\n");
- return CBFS_HEADER_INVALID_ADDRESS;
- }
- header = media->map(media, (size_t)rel_offset, sizeof(*header));
+ /* Find the CBFS master header pointer/offset at 0xfffffffc.
+ * There is no return value check since the offset can be negative. */
+ rdev_readat(media, &rel_offset,
+ lib_sysinfo.boot_media_size - sizeof(int32_t),
+ sizeof(int32_t));
+
+ rel_offset += lib_sysinfo.boot_media_size;
+
+ header = rdev_mmap(media, rel_offset, sizeof(*header));
DEBUG("CBFS header at %#zx (-%#zx from end of image).\n",
(size_t)rel_offset, (size_t)-rel_offset);
- media->close(media);
- if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
+ if (header == NULL) {
ERROR("Failed to load CBFS header from %#zx(-%#zx)\n",
(size_t)rel_offset, (size_t)-rel_offset);
return CBFS_HEADER_INVALID_ADDRESS;
@@ -99,7 +96,7 @@ const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
}
static int get_cbfs_range(uint32_t *offset, uint32_t *cbfs_end,
- struct cbfs_media *media)
+ struct region_device *media)
{
const struct cbfs_header *header;
@@ -115,17 +112,17 @@ static int get_cbfs_range(uint32_t *offset, uint32_t *cbfs_end,
header = cbfs_get_header(media);
if (header == CBFS_HEADER_INVALID_ADDRESS)
return -1;
- // Logical offset (for source media) of first file.
+ /* Logical offset (for source media) of first file. */
*offset = ntohl(header->offset);
*cbfs_end = ntohl(header->romsize);
#if IS_ENABLED(CONFIG_LP_ARCH_X86)
- // resolve actual length of ROM used for CBFS components
- // the bootblock size was not taken into account
+ /*resolve actual length of ROM used for CBFS components
+ * the bootblock size was not taken into account. */
*cbfs_end -= 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.
+ /* 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) % CBFS_ALIGNMENT))
*cbfs_end -= (CBFS_ALIGNMENT -
(ntohl(header->bootblocksize) % CBFS_ALIGNMENT));
@@ -135,10 +132,11 @@ static int get_cbfs_range(uint32_t *offset, uint32_t *cbfs_end,
return 0;
}
-/* public API starts here*/
-struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
+/* Public API starts here. */
+struct cbfs_handle *cbfs_get_handle(struct region_device *media,
+ const char *name)
{
- const char *vardata;
+ char *vardata;
uint32_t offset, cbfs_end, vardata_len;
struct cbfs_file file;
struct cbfs_handle *handle = malloc(sizeof(*handle));
@@ -149,33 +147,35 @@ struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
if (get_cbfs_range(&offset, &cbfs_end, media)) {
ERROR("Failed to find cbfs range\n");
free(handle);
+
return NULL;
}
if (media == CBFS_DEFAULT_MEDIA) {
- media = &handle->media;
- if (init_default_cbfs_media(media) != 0) {
- ERROR("Failed to initialize default media.\n");
- free(handle);
- return NULL;
- }
+
+ libpayload_init_default_cbfs_media();
+ media = boot_device_ro();
+ handle->media = media;
} else {
- memcpy(&handle->media, media, sizeof(*media));
+ handle->media = malloc(sizeof(*media));
+ if (!handle->media)
+ return NULL;
+ memcpy(handle->media, media, sizeof(*media));
}
DEBUG("CBFS location: 0x%x~0x%x\n", offset, cbfs_end);
DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
- media->open(media);
while (offset < cbfs_end &&
- media->read(media, &file, offset, sizeof(file)) == sizeof(file)) {
+ rdev_readat(media, &file, offset, sizeof(file)) ==
+ sizeof(file)) {
if (memcmp(CBFS_FILE_MAGIC, file.magic,
sizeof(file.magic)) != 0) {
uint32_t new_align = CBFS_ALIGNMENT;
if (offset % CBFS_ALIGNMENT)
new_align += CBFS_ALIGNMENT -
(offset % CBFS_ALIGNMENT);
- ERROR("ERROR: No file header found at 0x%xx - "
+ ERROR("ERROR: No file header found at 0x%x - "
"try next aligned address: 0x%x.\n", offset,
offset + new_align);
offset += new_align;
@@ -186,17 +186,16 @@ struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
offset, vardata_len);
// load file name (arbitrary length).
- vardata = (const char*)media->map(
- media, offset + sizeof(file), vardata_len);
- if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
+ vardata = rdev_mmap(media, offset + sizeof(file),
+ vardata_len);
+ if (vardata == NULL) {
ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
} else if (strcmp(vardata, name) == 0) {
int file_offset = ntohl(file.offset),
file_len = ntohl(file.len);
DEBUG("Found file (offset=0x%x, len=%d).\n",
offset + file_offset, file_len);
- media->unmap(media, vardata);
- media->close(media);
+ rdev_munmap(media, vardata);
handle->type = ntohl(file.type);
handle->media_offset = offset;
handle->content_offset = file_offset;
@@ -207,7 +206,7 @@ struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
} else {
DEBUG(" (unmatched file @0x%x: %s)\n", offset,
vardata);
- media->unmap(media, vardata);
+ rdev_munmap(media, vardata);
}
// Move to next file.
@@ -215,7 +214,6 @@ struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
if (offset % CBFS_ALIGNMENT)
offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT);
}
- media->close(media);
LOG("WARNING: '%s' not found.\n", name);
free(handle);
return NULL;
@@ -223,7 +221,7 @@ struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name)
void *cbfs_get_contents(struct cbfs_handle *handle, size_t *size, size_t limit)
{
- struct cbfs_media *m = &handle->media;
+ struct region_device *m = handle->media;
size_t on_media_size = handle->content_size;
int algo = CBFS_COMPRESS_NONE;
void *ret = NULL;
@@ -234,6 +232,7 @@ void *cbfs_get_contents(struct cbfs_handle *handle, size_t *size, size_t limit)
struct cbfs_file_attr_compression *comp =
cbfs_get_attr(handle, CBFS_FILE_ATTR_TAG_COMPRESSION);
+
if (comp) {
algo = ntohl(comp->compression);
DEBUG("File '%s' is compressed (alg=%d)\n", name, algo);
@@ -250,22 +249,31 @@ void *cbfs_get_contents(struct cbfs_handle *handle, size_t *size, size_t limit)
}
}
- void *data = m->map(m, handle->media_offset + handle->content_offset,
+ void *data = rdev_mmap(m, handle->media_offset + handle->content_offset,
on_media_size);
- if (data == CBFS_MEDIA_INVALID_MAP_ADDRESS)
+ if (data == NULL)
return NULL;
+ /* Used to avoid running out of heap memory from malloc. */
+ if (algo == CBFS_COMPRESS_NONE)
+ return data;
+
ret = malloc(*size);
+ if (!ret) {
+ ERROR("malloc needed to store the file contents failed.\n");
+ return NULL;
+ }
+
if (ret != NULL && !cbfs_decompress(algo, data, ret, *size)) {
free(ret);
ret = NULL;
}
- m->unmap(m, data);
+ rdev_munmap(m, data);
return ret;
}
-void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
+void *cbfs_get_file_content(struct region_device *media, const char *name,
int type, size_t *sz)
{
void *ret = NULL;
@@ -286,7 +294,7 @@ void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
void *cbfs_get_attr(struct cbfs_handle *handle, uint32_t tag)
{
- struct cbfs_media *m = &handle->media;
+ struct region_device *m = handle->media;
uint32_t offset = handle->media_offset + handle->attribute_offset;
uint32_t end = handle->media_offset + handle->content_offset;
struct cbfs_file_attribute attr;
@@ -297,26 +305,23 @@ void *cbfs_get_attr(struct cbfs_handle *handle, uint32_t tag)
if (handle->attribute_offset <= sizeof(struct cbfs_file))
return NULL;
- m->open(m);
while (offset + sizeof(attr) <= end) {
- if (m->read(m, &attr, offset, sizeof(attr)) != sizeof(attr)) {
+ if (rdev_readat(m, &attr, offset, sizeof(attr)) !=
+ sizeof(attr)) {
ERROR("Failed to read attribute header %#x\n", offset);
- m->close(m);
return NULL;
}
if (ntohl(attr.tag) != tag) {
offset += ntohl(attr.len);
continue;
}
- ret = m->map(m, offset, ntohl(attr.len));
- if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
+ ret = rdev_mmap(m, offset, ntohl(attr.len));
+ if (ret == NULL) {
ERROR("Failed to map attribute at %#x\n", offset);
- m->close(m);
return NULL;
}
return ret;
}
- m->close(m);
return NULL;
}
diff --git a/payloads/libpayload/libcbfs/mem_pool.c b/payloads/libpayload/libcbfs/mem_pool.c
new file mode 100644
index 0000000..c1d3961
--- /dev/null
+++ b/payloads/libpayload/libcbfs/mem_pool.c
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <mem_pool.h>
+
+void *mem_pool_alloc(struct mem_pool *mp, size_t sz)
+{
+ void *p;
+
+ /* Make all allocations be at least 8 byte aligned. */
+ sz = ALIGN_UP(sz, 8);
+
+ /* Determine if any space available. */
+ if ((mp->size - mp->free_offset) < sz)
+ return NULL;
+
+ p = &mp->buf[mp->free_offset];
+
+ mp->free_offset += sz;
+ mp->last_alloc = p;
+
+ return p;
+}
+
+void mem_pool_free(struct mem_pool *mp, void *p)
+{
+ /* Determine if p was the most recent allocation. */
+ if (p == NULL || mp->last_alloc != p)
+ return;
+
+ mp->free_offset = mp->last_alloc - mp->buf;
+ /* No way to track allocation before this one. */
+ mp->last_alloc = NULL;
+}
diff --git a/payloads/libpayload/libcbfs/ram_media.c b/payloads/libpayload/libcbfs/ram_media.c
deleted file mode 100644
index d5469f6..0000000
--- a/payloads/libpayload/libcbfs/ram_media.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * This file is part of the libpayload project.
- *
- * Copyright (C) 2013 Google, Inc.
- *
- * 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.
- */
-#include <cbfs.h>
-#include <cbfs_ram.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-// Implementation of a media source based on given memory buffer.
-struct ram_media {
- char *start;
- size_t size;
-};
-
-static int ram_open(struct cbfs_media *media) {
- return 0;
-}
-
-static void *ram_map(struct cbfs_media *media, size_t offset, size_t count) {
- struct ram_media *m = (struct ram_media*)media->context;
- /* assume addressing from top of image in this case */
- if (offset > 0xf0000000) {
- offset = m->size + offset;
- }
- if (offset + count > m->size) {
- printf("ERROR: ram_map: request out of range (0x%zx+0x%zx)\n",
- offset, count);
- return CBFS_MEDIA_INVALID_MAP_ADDRESS;
- }
- return (void*)(m->start + offset);
-}
-
-static void *ram_unmap(struct cbfs_media *media, const void *address) {
- return NULL;
-}
-
-static size_t ram_read(struct cbfs_media *media, void *dest, size_t offset,
- size_t count) {
- void *ptr = ram_map(media, offset, count);
- if (ptr == CBFS_MEDIA_INVALID_MAP_ADDRESS)
- return 0;
- memcpy(dest, ptr, count);
- ram_unmap(media, ptr);
- return count;
-}
-
-static int ram_close(struct cbfs_media *media) {
- return 0;
-}
-
-int init_cbfs_ram_media(struct cbfs_media *media, void *start, size_t size) {
- // TODO Find a way to release unused media. Maybe adding media->destroy.
- struct ram_media *m = (struct ram_media*)malloc(sizeof(*m));
- m->start = start;
- m->size = size;
- media->context = (void*)m;
- media->open = ram_open;
- media->close = ram_close;
- media->map = ram_map;
- media->unmap = ram_unmap;
- media->read = ram_read;
- return 0;
-}
-
-// Legacy setup_cbfs_from_*.
-static int is_default_cbfs_media_initialized;
-static struct cbfs_media default_cbfs_media;
-
-int setup_cbfs_from_ram(void *start, uint32_t size) {
- int result = init_cbfs_ram_media(&default_cbfs_media, start, size);
- if (result == 0)
- is_default_cbfs_media_initialized = 1;
- return result;
-}
-
-extern int libpayload_init_default_cbfs_media(struct cbfs_media *media);
-int setup_cbfs_from_flash(void) {
- int result = libpayload_init_default_cbfs_media(&default_cbfs_media);
- if (result == 0)
- is_default_cbfs_media_initialized = 1;
- return result;
-}
-
-int init_default_cbfs_media(struct cbfs_media *media) {
- int result = 0;
- if (is_default_cbfs_media_initialized != 1) {
- result = setup_cbfs_from_flash();
- }
- if (result == 0)
- memcpy(media, &default_cbfs_media, sizeof(*media));
- return result;
-}
diff --git a/payloads/libpayload/libcbfs/region_device.c b/payloads/libpayload/libcbfs/region_device.c
new file mode 100644
index 0000000..f87075a
--- /dev/null
+++ b/payloads/libpayload/libcbfs/region_device.c
@@ -0,0 +1,434 @@
+/*
+ * 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.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <region_device.h>
+
+static inline size_t region_end(const struct region *r)
+{
+ return region_sz(r) + region_offset(r);
+}
+
+static int is_subregion(const struct region *p, const struct region *c)
+{
+ if (region_offset(c) < region_offset(p))
+ return 0;
+
+ if (region_sz(c) > region_sz(p))
+ return 0;
+
+ if (region_end(c) > region_end(p))
+ return 0;
+
+ return 1;
+}
+
+static int normalize_and_ok(const struct region *outer, struct region *inner)
+{
+ inner->offset += region_offset(outer);
+ return is_subregion(outer, inner);
+}
+
+static const struct region_device *rdev_root(const struct region_device *rdev)
+{
+ if (rdev->root == NULL)
+ return rdev;
+ return rdev->root;
+}
+
+ssize_t rdev_relative_offset(const struct region_device *p,
+ const struct region_device *c)
+{
+ if (rdev_root(p) != rdev_root(c))
+ return -1;
+
+ if (!is_subregion(&p->region, &c->region))
+ return -1;
+
+ return region_device_offset(c) - region_device_offset(p);
+}
+
+void *rdev_mmap(const struct region_device *rd, size_t offset, size_t size)
+{
+ const struct region_device *rdev;
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ if (!normalize_and_ok(&rd->region, &req))
+ return NULL;
+
+ rdev = rdev_root(rd);
+
+ return rdev->ops->mmap(rdev, req.offset, req.size);
+}
+
+int rdev_munmap(const struct region_device *rd, void *mapping)
+{
+ const struct region_device *rdev;
+
+ rdev = rdev_root(rd);
+
+ return rdev->ops->munmap(rdev, mapping);
+}
+
+ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset,
+ size_t size)
+{
+ const struct region_device *rdev;
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ if (!normalize_and_ok(&rd->region, &req))
+ return -1;
+
+ rdev = rdev_root(rd);
+
+ return rdev->ops->readat(rdev, b, req.offset, req.size);
+}
+
+ssize_t rdev_writeat(const struct region_device *rd, void *b, size_t offset,
+ size_t size)
+{
+ const struct region_device *rdev;
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ if (!normalize_and_ok(&rd->region, &req))
+ return -1;
+
+ rdev = rdev_root(rd);
+
+ if (rdev->ops->writeat == NULL)
+ return -1;
+
+ return rdev->ops->writeat(rdev, b, req.offset, req.size);
+}
+
+ssize_t rdev_eraseat(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ const struct region_device *rdev;
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ if (!normalize_and_ok(&rd->region, &req))
+ return -1;
+
+ rdev = rdev_root(rd);
+
+ /* If the eraseat ptr is NULL we assume that the erase
+ * function was completed successfully. */
+ if (rdev->ops->eraseat == NULL)
+ return size;
+
+ return rdev->ops->eraseat(rdev, req.offset, req.size);
+}
+
+int rdev_chain(struct region_device *child, const struct region_device *parent,
+ size_t offset, size_t size)
+{
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ if (!normalize_and_ok(&parent->region, &req))
+ return -1;
+
+ /* Keep track of root region device. Note the offsets are relative
+ * to the root device. */
+ child->root = rdev_root(parent);
+ child->ops = NULL;
+ child->region.offset = req.offset;
+ child->region.size = req.size;
+
+ return 0;
+}
+
+static void mem_region_device_init(struct mem_region_device *mdev,
+ const struct region_device_ops *ops, void *base, size_t size)
+{
+ memset(mdev, 0, sizeof(*mdev));
+ mdev->base = base;
+ mdev->rdev.ops = ops;
+ mdev->rdev.region.size = size;
+}
+
+void mem_region_device_ro_init(struct mem_region_device *mdev, void *base,
+ size_t size)
+{
+ return mem_region_device_init(mdev, &mem_rdev_ro_ops, base, size);
+}
+
+void mem_region_device_rw_init(struct mem_region_device *mdev, void *base,
+ size_t size)
+{
+ return mem_region_device_init(mdev, &mem_rdev_rw_ops, base, size);
+}
+
+void region_device_init(struct region_device *rdev,
+ const struct region_device_ops *ops, size_t offset,
+ size_t size)
+{
+ memset(rdev, 0, sizeof(*rdev));
+ rdev->root = NULL;
+ rdev->ops = ops;
+ rdev->region.offset = offset;
+ rdev->region.size = size;
+}
+
+static void xlate_region_device_init(struct xlate_region_device *xdev,
+ const struct region_device_ops *ops,
+ const struct region_device *access_dev,
+ size_t sub_offset, size_t sub_size,
+ size_t parent_size)
+{
+ memset(xdev, 0, sizeof(*xdev));
+ xdev->access_dev = access_dev;
+ xdev->sub_region.offset = sub_offset;
+ xdev->sub_region.size = sub_size;
+ region_device_init(&xdev->rdev, ops, 0, parent_size);
+}
+
+void xlate_region_device_ro_init(struct xlate_region_device *xdev,
+ const struct region_device *access_dev,
+ size_t sub_offset, size_t sub_size,
+ size_t parent_size)
+{
+ xlate_region_device_init(xdev, &xlate_rdev_ro_ops, access_dev,
+ sub_offset, sub_size, parent_size);
+}
+
+void xlate_region_device_rw_init(struct xlate_region_device *xdev,
+ const struct region_device *access_dev,
+ size_t sub_offset, size_t sub_size,
+ size_t parent_size)
+{
+ xlate_region_device_init(xdev, &xlate_rdev_rw_ops, access_dev,
+ sub_offset, sub_size, parent_size);
+}
+
+static void *mdev_mmap(const struct region_device *rd, size_t offset,
+ size_t size __unused)
+{
+ const struct mem_region_device *mdev;
+
+ mdev = container_of(rd, __typeof__(*mdev), rdev);
+
+ return &mdev->base[offset];
+}
+
+static int mdev_munmap(const struct region_device *rd __unused,
+ void *mapping __unused)
+{
+ return 0;
+}
+
+static ssize_t mdev_readat(const struct region_device *rd, void *b,
+ size_t offset, size_t size)
+{
+ const struct mem_region_device *mdev;
+
+ mdev = container_of(rd, __typeof__(*mdev), rdev);
+
+ memcpy(b, &mdev->base[offset], size);
+
+ return size;
+}
+
+static ssize_t mdev_writeat(const struct region_device *rd, void *b,
+ size_t offset, size_t size)
+{
+ const struct mem_region_device *mdev;
+
+ mdev = container_of(rd, __typeof__(*mdev), rdev);
+
+ memcpy(&mdev->base[offset], b, size);
+
+ return size;
+}
+
+static ssize_t mdev_eraseat(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ const struct mem_region_device *mdev;
+
+ mdev = container_of(rd, __typeof__(*mdev), rdev);
+
+ memset(&mdev->base[offset], 0, size);
+
+ return size;
+}
+
+const struct region_device_ops mem_rdev_ro_ops = {
+ .mmap = mdev_mmap,
+ .munmap = mdev_munmap,
+ .readat = mdev_readat,
+};
+
+const struct region_device_ops mem_rdev_rw_ops = {
+ .mmap = mdev_mmap,
+ .munmap = mdev_munmap,
+ .readat = mdev_readat,
+ .writeat = mdev_writeat,
+ .eraseat = mdev_eraseat,
+};
+
+void mmap_helper_device_init(struct mmap_helper_region_device *mdev,
+ void *cache, size_t cache_size)
+{
+ mem_pool_init(&mdev->pool, cache, cache_size);
+}
+
+void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ struct mmap_helper_region_device *mdev;
+ void *mapping;
+
+ mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
+
+ mapping = mem_pool_alloc(&mdev->pool, size);
+
+ if (mapping == NULL)
+ return NULL;
+
+ if (rd->ops->readat(rd, mapping, offset, size) != size) {
+ mem_pool_free(&mdev->pool, mapping);
+ return NULL;
+ }
+
+ return mapping;
+}
+
+int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping)
+{
+ struct mmap_helper_region_device *mdev;
+
+ mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
+
+ mem_pool_free(&mdev->pool, mapping);
+
+ return 0;
+}
+
+static void *xlate_mmap(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ const struct xlate_region_device *xldev;
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+
+ xldev = container_of(rd, __typeof__(*xldev), rdev);
+
+ if (!is_subregion(&xldev->sub_region, &req))
+ return NULL;
+
+ offset -= region_offset(&xldev->sub_region);
+
+ return rdev_mmap(xldev->access_dev, offset, size);
+}
+
+static int xlate_munmap(const struct region_device *rd, void *mapping)
+{
+ const struct xlate_region_device *xldev;
+
+ xldev = container_of(rd, __typeof__(*xldev), rdev);
+
+ return rdev_munmap(xldev->access_dev, mapping);
+}
+
+static ssize_t xlate_readat(const struct region_device *rd, void *b,
+ size_t offset, size_t size)
+{
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+ const struct xlate_region_device *xldev;
+
+ xldev = container_of(rd, __typeof__(*xldev), rdev);
+
+ if (!is_subregion(&xldev->sub_region, &req))
+ return -1;
+
+ offset -= region_offset(&xldev->sub_region);
+
+ return rdev_readat(xldev->access_dev, b, offset, size);
+}
+
+static ssize_t xlate_writeat(const struct region_device *rd, void *b,
+ size_t offset, size_t size)
+{
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+ const struct xlate_region_device *xldev;
+
+ xldev = container_of(rd, __typeof__(*xldev), rdev);
+
+ if (!is_subregion(&xldev->sub_region, &req))
+ return -1;
+
+ offset -= region_offset(&xldev->sub_region);
+
+ return rdev_writeat(xldev->access_dev, b, offset, size);
+}
+
+static ssize_t xlate_eraseat(const struct region_device *rd,
+ size_t offset, size_t size)
+{
+ struct region req = {
+ .offset = offset,
+ .size = size,
+ };
+ const struct xlate_region_device *xldev;
+
+ xldev = container_of(rd, __typeof__(*xldev), rdev);
+
+ if (!is_subregion(&xldev->sub_region, &req))
+ return -1;
+
+ offset -= region_offset(&xldev->sub_region);
+
+ return rdev_eraseat(xldev->access_dev, offset, size);
+}
+
+const struct region_device_ops xlate_rdev_ro_ops = {
+ .mmap = xlate_mmap,
+ .munmap = xlate_munmap,
+ .readat = xlate_readat,
+};
+
+const struct region_device_ops xlate_rdev_rw_ops = {
+ .mmap = xlate_mmap,
+ .munmap = xlate_munmap,
+ .readat = xlate_readat,
+ .writeat = xlate_writeat,
+ .eraseat = xlate_eraseat,
+};
More information about the coreboot-gerrit
mailing list