[coreboot-gerrit] Patch set updated for coreboot: RFC: Introduce commonlib
Aaron Durbin (adurbin@chromium.org)
gerrit at coreboot.org
Tue Sep 8 22:32:58 CET 2015
Aaron Durbin (adurbin at chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11592
-gerrit
commit 74ae166274a21cffcde57d7d9b40cb05a86eb1a8
Author: Aaron Durbin <adurbin at chromium.org>
Date: Tue Sep 8 13:34:43 2015 -0500
RFC: Introduce commonlib
Instead of reaching into src/include and re-writing code
allow for cleaner code sharing within coreboot and its
utilities. The additional thing needed at this point is
for the utilities to provide a printk() declaration within
a <console/console.h> file. That way code which uses printk()
can than be mapped properly to verbosity of utility parameters.
Change-Id: I9e46a279569733336bc0a018aed96bc924c07cdd
Signed-off-by: Aaron Durbin <adurbin at chromium.org>
---
Makefile.inc | 4 +-
src/commonlib/Makefile.inc | 10 ++
src/commonlib/include/cbmem_id.h | 113 ++++++++++++++++++
src/commonlib/include/console/loglevel.h | 178 ++++++++++++++++++++++++++++
src/commonlib/include/helpers.h | 40 +++++++
src/commonlib/include/mem_pool.h | 73 ++++++++++++
src/commonlib/include/region.h | 157 +++++++++++++++++++++++++
src/commonlib/include/rmodule-defs.h | 63 ++++++++++
src/commonlib/mem_pool.c | 51 ++++++++
src/commonlib/region.c | 196 +++++++++++++++++++++++++++++++
src/include/cbmem_id.h | 113 ------------------
src/include/console/loglevel.h | 178 ----------------------------
src/include/mem_pool.h | 73 ------------
src/include/region.h | 157 -------------------------
src/include/rmodule-defs.h | 63 ----------
src/include/stddef.h | 34 +-----
src/lib/Makefile.inc | 9 --
src/lib/mem_pool.c | 51 --------
src/lib/region.c | 196 -------------------------------
src/vendorcode/amd/pi/Makefile.inc | 1 +
util/cbfstool/Makefile | 1 +
util/cbfstool/Makefile.inc | 1 +
util/cbfstool/rmodule.c | 2 +-
util/cbmem/Makefile | 2 +-
util/cbmem/cbmem.c | 2 +-
25 files changed, 891 insertions(+), 877 deletions(-)
diff --git a/Makefile.inc b/Makefile.inc
index 4add195..250fe91 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -54,7 +54,7 @@ PHONY+= clean-abuild coreboot lint lint-stable build-dirs
#######################################################################
# root source directories of coreboot
-subdirs-y := src/lib src/console src/device src/acpi
+subdirs-y := src/lib src/commonlib/ src/console src/device src/acpi
subdirs-y += src/ec/acpi $(wildcard src/ec/*/*) $(wildcard src/southbridge/*/*)
subdirs-y += $(wildcard src/soc/*/*) $(wildcard src/northbridge/*/*)
subdirs-y += src/superio $(wildcard src/drivers/*) src/cpu src/vendorcode
@@ -267,7 +267,7 @@ ifneq ($(CONFIG_LOCALVERSION),"")
export COREBOOT_EXTRA_VERSION := -$(call strip_quotes,$(CONFIG_LOCALVERSION))
endif
-CPPFLAGS_common := -Isrc -Isrc/include -I$(obj)
+CPPFLAGS_common := -Isrc -Isrc/include -Isrc/commonlib/include -I$(obj)
CPPFLAGS_common += -Isrc/device/oprom/include
CPPFLAGS_common += -include $(src)/include/kconfig.h
diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc
new file mode 100644
index 0000000..70a9b1a
--- /dev/null
+++ b/src/commonlib/Makefile.inc
@@ -0,0 +1,10 @@
+bootblock-y += mem_pool.c
+verstage-y += mem_pool.c
+romstage-y += mem_pool.c
+ramstage-y += mem_pool.c
+
+bootblock-y += region.c
+verstage-y += region.c
+romstage-y += region.c
+ramstage-y += region.c
+smm-y += region.c
diff --git a/src/commonlib/include/cbmem_id.h b/src/commonlib/include/cbmem_id.h
new file mode 100644
index 0000000..6812c41
--- /dev/null
+++ b/src/commonlib/include/cbmem_id.h
@@ -0,0 +1,113 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2009 coresystems GmbH
+ * Copyright (C) 2013 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 _CBMEM_ID_H_
+#define _CBMEM_ID_H_
+
+#define CBMEM_ID_ACPI 0x41435049
+#define CBMEM_ID_ACPI_GNVS 0x474e5653
+#define CBMEM_ID_ACPI_GNVS_PTR 0x474e5650
+#define CBMEM_ID_AGESA_RUNTIME 0x41474553
+#define CBMEM_ID_AMDMCT_MEMINFO 0x494D454E
+#define CBMEM_ID_CAR_GLOBALS 0xcac4e6a3
+#define CBMEM_ID_CBTABLE 0x43425442
+#define CBMEM_ID_CONSOLE 0x434f4e53
+#define CBMEM_ID_COVERAGE 0x47434f56
+#define CBMEM_ID_EHCI_DEBUG 0xe4c1deb9
+#define CBMEM_ID_ELOG 0x454c4f47
+#define CBMEM_ID_FREESPACE 0x46524545
+#define CBMEM_ID_FSP_RESERVED_MEMORY 0x46535052
+#define CBMEM_ID_FSP_RUNTIME 0x52505346
+#define CBMEM_ID_GDT 0x4c474454
+#define CBMEM_ID_HOB_POINTER 0x484f4221
+#define CBMEM_ID_IGD_OPREGION 0x4f444749
+#define CBMEM_ID_IMD_ROOT 0xff4017ff
+#define CBMEM_ID_IMD_SMALL 0x53a11439
+#define CBMEM_ID_MEMINFO 0x494D454D
+#define CBMEM_ID_MPTABLE 0x534d5054
+#define CBMEM_ID_MRCDATA 0x4d524344
+#define CBMEM_ID_MTC 0xcb31d31c
+#define CBMEM_ID_NONE 0x00000000
+#define CBMEM_ID_PIRQ 0x49525154
+#define CBMEM_ID_POWER_STATE 0x50535454
+#define CBMEM_ID_RAM_OOPS 0x05430095
+#define CBMEM_ID_RAMSTAGE 0x9a357a9e
+#define CBMEM_ID_RAMSTAGE_CACHE 0x9a3ca54e
+#define CBMEM_ID_REFCODE 0x04efc0de
+#define CBMEM_ID_REFCODE_CACHE 0x4efc0de5
+#define CBMEM_ID_RESUME 0x5245534d
+#define CBMEM_ID_RESUME_SCRATCH 0x52455343
+#define CBMEM_ID_ROMSTAGE_INFO 0x47545352
+#define CBMEM_ID_ROMSTAGE_RAM_STACK 0x90357ac4
+#define CBMEM_ID_ROOT 0xff4007ff
+#define CBMEM_ID_SMBIOS 0x534d4254
+#define CBMEM_ID_SMM_SAVE_SPACE 0x07e9acee
+#define CBMEM_ID_SPINTABLE 0x59175917
+#define CBMEM_ID_STAGEx_META 0x57a9e000
+#define CBMEM_ID_STAGEx_CACHE 0x57a9e100
+#define CBMEM_ID_TCPA_LOG 0x54435041
+#define CBMEM_ID_TIMESTAMP 0x54494d45
+#define CBMEM_ID_VBOOT_HANDOFF 0x780074f0
+#define CBMEM_ID_VBOOT_WORKBUF 0x78007343
+#define CBMEM_ID_WIFI_CALIBRATION 0x57494649
+
+#define CBMEM_ID_TO_NAME_TABLE \
+ { CBMEM_ID_ACPI, "ACPI " }, \
+ { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " }, \
+ { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " }, \
+ { CBMEM_ID_AGESA_RUNTIME, "AGESA RSVD " }, \
+ { CBMEM_ID_AMDMCT_MEMINFO, "AMDMEM INFO" }, \
+ { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" }, \
+ { CBMEM_ID_CBTABLE, "COREBOOT " }, \
+ { CBMEM_ID_CONSOLE, "CONSOLE " }, \
+ { CBMEM_ID_COVERAGE, "COVERAGE " }, \
+ { CBMEM_ID_EHCI_DEBUG, "USBDEBUG " }, \
+ { CBMEM_ID_ELOG, "ELOG " }, \
+ { CBMEM_ID_FREESPACE, "FREE SPACE " }, \
+ { CBMEM_ID_FSP_RESERVED_MEMORY, "FSP MEMORY " }, \
+ { CBMEM_ID_FSP_RUNTIME, "FSP RUNTIME" }, \
+ { CBMEM_ID_GDT, "GDT " }, \
+ { CBMEM_ID_IMD_ROOT, "IMD ROOT " }, \
+ { CBMEM_ID_IMD_SMALL, "IMD SMALL " }, \
+ { CBMEM_ID_MEMINFO, "MEM INFO " }, \
+ { CBMEM_ID_MPTABLE, "SMP TABLE " }, \
+ { CBMEM_ID_MRCDATA, "MRC DATA " }, \
+ { CBMEM_ID_MTC, "MTC " }, \
+ { CBMEM_ID_PIRQ, "IRQ TABLE " }, \
+ { CBMEM_ID_POWER_STATE, "POWER STATE" }, \
+ { CBMEM_ID_RAM_OOPS, "RAMOOPS " }, \
+ { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " }, \
+ { CBMEM_ID_RAMSTAGE, "RAMSTAGE " }, \
+ { CBMEM_ID_REFCODE_CACHE, "REFCODE $ " }, \
+ { CBMEM_ID_REFCODE, "REFCODE " }, \
+ { CBMEM_ID_RESUME, "ACPI RESUME" }, \
+ { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" }, \
+ { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " }, \
+ { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" }, \
+ { CBMEM_ID_ROOT, "CBMEM ROOT " }, \
+ { CBMEM_ID_SMBIOS, "SMBIOS " }, \
+ { CBMEM_ID_SMM_SAVE_SPACE, "SMM BACKUP " }, \
+ { CBMEM_ID_SPINTABLE, "SPIN TABLE " }, \
+ { CBMEM_ID_TCPA_LOG, "TCPA LOG " }, \
+ { CBMEM_ID_TIMESTAMP, "TIME STAMP " }, \
+ { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " }, \
+ { CBMEM_ID_VBOOT_WORKBUF, "VBOOT WORK " }, \
+ { CBMEM_ID_WIFI_CALIBRATION, "WIFI CLBR " },
+#endif /* _CBMEM_ID_H_ */
diff --git a/src/commonlib/include/console/loglevel.h b/src/commonlib/include/console/loglevel.h
new file mode 100644
index 0000000..e147490
--- /dev/null
+++ b/src/commonlib/include/console/loglevel.h
@@ -0,0 +1,178 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2015 Nicholas Sielicki <sielicki at nicky.io>
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 LOGLEVEL_H
+#define LOGLEVEL_H
+
+/**
+ * @file loglevel.h
+ *
+ * \brief Definitions of the log levels to be used in printk calls.
+ *
+ * Safe for inclusion in assembly.
+ *
+ */
+
+/**
+ * \brief BIOS_EMERG - Emergency / Fatal
+ *
+ * Log level for when the system is entirely unusable. To be used when execution
+ * is halting as a result of the failure. No further instructions should run.
+ *
+ * Example - End of all debug output / death notice.
+ *
+ * @{
+ */
+#define BIOS_EMERG 0
+/** @} */
+
+/**
+ * \brief BIOS_ALERT - Dying / Unrecoverable
+ *
+ * Log level for when the system is certainly in the process of dying.
+ * To be used when execution will eventually halt as a result of the
+ * failure, but the system can still output valuable debugging
+ * information.
+ *
+ * Example - Ram initialization fails, dumping relevant POST codes and
+ * information
+ *
+ * @{
+ */
+#define BIOS_ALERT 1
+/** @} */
+
+/**
+ * \brief BIOS_CRIT - Recovery unlikely
+ *
+ * Log level for when the system has experienced a dire issue in essential
+ * components. To be used when boot will probably be unsuccessful as a
+ * result of the failure, but recovery/retry can be attempted.
+ *
+ * Example - MSR failures, SMM/SMI failures.
+ * or
+ *
+ * @{
+ */
+#define BIOS_CRIT 2
+/** @} */
+
+/**
+ * \brief BIOS_ERR - System in incomplete state.
+ *
+ * Log level for when the system has experienced an issue that may not preclude
+ * a successful boot. To be used when coreboot execution may still succeed,
+ * but the error places some non-essential portion of the machine in a broken
+ * state that will be noticed downstream.
+ *
+ * Example - Payload could still load, but will be missing access to integral
+ * components such as drives.
+ *
+ * @{
+ */
+#define BIOS_ERR 3
+/** @} */
+
+/**
+ * \brief BIOS_WARNING - Bad configuration
+ *
+ * Log level for when the system has noticed an issue that most likely will
+ * not preclude a successful boot. To be used when something is wrong, and
+ * would likely be noticed by an end user.
+ *
+ * Example - Bad ME firmware, bad microcode, mis-clocked CPU
+ *
+ * @{
+ */
+#define BIOS_WARNING 4
+/** @} */
+
+/**
+ * \brief BIOS_NOTICE - Unexpected but relatively insignificant
+ *
+ * Log level for when the system has noticed an issue that is an edge case,
+ * but is handled and is recoverable. To be used when an end-user would likely
+ * not notice.
+ *
+ * Example - Hardware was misconfigured, but is promptly fixed.
+ *
+ * @{
+ */
+#define BIOS_NOTICE 5
+/** @} */
+
+/**
+ * \brief BIOS_INFO - Expected events.
+ *
+ * Log level for when the system has experienced some typical event.
+ * Messages should be superficial in nature.
+ *
+ * Example - Success messages. Status messages.
+ *
+ * @{
+ */
+#define BIOS_INFO 6
+/** @} */
+
+/**
+ * \brief BIOS_DEBUG - Verbose output
+ *
+ * Log level for details of a method. Messages may be dense,
+ * but should not be excessive. Messages should be detailed enough
+ * that this level provides sufficient details to diagnose a problem,
+ * but not necessarily enough to fix it.
+ *
+ * Example - Printing of important variables.
+ *
+ * @{
+ */
+#define BIOS_DEBUG 7
+/** @} */
+
+/**
+ * \brief BIOS_SPEW - Excessively verbose output
+ *
+ * Log level for intricacies of a method. Messages might contain raw
+ * data and will produce large logs. Developers should try to make sure
+ * that this level is not useful to anyone besides developers.
+ *
+ * Example - Data dumps.
+ *
+ * @{
+ */
+#define BIOS_SPEW 8
+/** @} */
+
+/**
+ * \brief BIOS_NEVER - Muted log level.
+ *
+ * Roughly equal to commenting out a printk statement. Because a user
+ * should not set their log level higher than 8, these statements
+ * are never printed.
+ *
+ * Example - A developer might locally define MY_LOGLEVEL to BIOS_SPEW,
+ * and later replace it with BIOS_NEVER as to mute their debug output.
+ *
+ * @{
+ */
+#define BIOS_NEVER 9
+/** @} */
+
+#endif /* LOGLEVEL_H */
diff --git a/src/commonlib/include/helpers.h b/src/commonlib/include/helpers.h
new file mode 100644
index 0000000..d6caca6
--- /dev/null
+++ b/src/commonlib/include/helpers.h
@@ -0,0 +1,40 @@
+
+#ifndef COMMONLIB_HELPERS_H
+#define COMMONLIB_HELPERS_H
+/* This file is for helpers for both coreboot firmware and its utilities. */
+
+
+
+/* Standard units. */
+#define KiB (1<<10)
+#define MiB (1<<20)
+#define GiB (1<<30)
+/* Could we ever run into this one? I hope we get this much memory! */
+#define TiB (1<<40)
+
+#define KHz (1000)
+#define MHz (1000 * KHz)
+#define GHz (1000 * MHz)
+
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+#if !defined(__clang__)
+#define check_member(structure, member, offset) _Static_assert( \
+ offsetof(struct structure, member) == offset, \
+ "`struct " #structure "` offset for `" #member "` is not " #offset )
+#else
+#define check_member(structure, member, offset)
+#endif
+
+/**
+ * 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) );})
+
+#endif /* COMMONLIB_HELPERS_H */
diff --git a/src/commonlib/include/mem_pool.h b/src/commonlib/include/mem_pool.h
new file mode 100644
index 0000000..c57b707
--- /dev/null
+++ b/src/commonlib/include/mem_pool.h
@@ -0,0 +1,73 @@
+/*
+ * 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 _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/src/commonlib/include/region.h b/src/commonlib/include/region.h
new file mode 100644
index 0000000..82db854
--- /dev/null
+++ b/src/commonlib/include/region.h
@@ -0,0 +1,157 @@
+/*
+ * 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 _REGION_H_
+#define _REGION_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);
+
+
+/****************************************
+ * 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);
+};
+
+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_), \
+ }, \
+ }
+
+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 size_t region_device_sz(const struct region_device *rdev)
+{
+ return region_sz(&rdev->region);
+}
+
+static inline size_t region_device_offset(const struct region_device *rdev)
+{
+ return region_offset(&rdev->region);
+}
+
+/* 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));
+}
+
+struct mem_region_device {
+ char *base;
+ struct region_device rdev;
+};
+
+/* Iniitalize at runtime a mem_region_device. This would be used when
+ * the base and size are dynamic or can't be known during linking. */
+void mem_region_device_init(struct mem_region_device *mdev, void *base,
+ size_t size);
+
+extern const struct region_device_ops mem_rdev_ops;
+
+/* Statically initialize mem_region_device. */
+#define MEM_REGION_DEV_INIT(base_, size_) \
+ { \
+ .base = (void *)(base_), \
+ .rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)), \
+ }
+
+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 *);
+
+#endif /* _REGION_H_ */
diff --git a/src/commonlib/include/rmodule-defs.h b/src/commonlib/include/rmodule-defs.h
new file mode 100644
index 0000000..d61837f
--- /dev/null
+++ b/src/commonlib/include/rmodule-defs.h
@@ -0,0 +1,63 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2014 Google Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+#ifndef RMODULE_DEFS_H
+#define RMODULE_DEFS_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define RMODULE_MAGIC 0xf8fe
+#define RMODULE_VERSION_1 1
+
+/* All fields with '_offset' in the name are byte offsets into the flat blob.
+ * The linker and the linker script takes are of assigning the values. */
+struct rmodule_header {
+ uint16_t magic;
+ uint8_t version;
+ uint8_t type;
+ /* The payload represents the program's loadable code and data. */
+ uint32_t payload_begin_offset;
+ uint32_t payload_end_offset;
+ /* Begin and of relocation information about the program module. */
+ uint32_t relocations_begin_offset;
+ uint32_t relocations_end_offset;
+ /* The starting address of the linked program. This address is vital
+ * for determining relocation offsets as the relocation info and other
+ * symbols (bss, entry point) need this value as a basis to calculate
+ * the offsets.
+ */
+ uint32_t module_link_start_address;
+ /* The module_program_size is the size of memory used while running
+ * the program. The program is assumed to consume a contiguous amount
+ * of memory. */
+ uint32_t module_program_size;
+ /* This is program's execution entry point. */
+ uint32_t module_entry_point;
+ /* Optional parameter structure that can be used to pass data into
+ * the module. */
+ uint32_t parameters_begin;
+ uint32_t parameters_end;
+ /* BSS section information so the loader can clear the bss. */
+ uint32_t bss_begin;
+ uint32_t bss_end;
+ /* Add some room for growth. */
+ uint32_t padding[4];
+} __attribute__ ((packed));
+
+#endif /* RMODULE_DEFS_H */
diff --git a/src/commonlib/mem_pool.c b/src/commonlib/mem_pool.c
new file mode 100644
index 0000000..4bd0668
--- /dev/null
+++ b/src/commonlib/mem_pool.c
@@ -0,0 +1,51 @@
+/*
+ * 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 <mem_pool.h>
+#include <stdlib.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/src/commonlib/region.c b/src/commonlib/region.c
new file mode 100644
index 0000000..d5d3762
--- /dev/null
+++ b/src/commonlib/region.c
@@ -0,0 +1,196 @@
+/*
+ * 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 <region.h>
+#include <string.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;
+}
+
+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);
+}
+
+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;
+}
+
+void mem_region_device_init(struct mem_region_device *mdev, void *base,
+ size_t size)
+{
+ memset(mdev, 0, sizeof(*mdev));
+ mdev->base = base;
+ mdev->rdev.ops = &mem_rdev_ops;
+ mdev->rdev.region.size = size;
+}
+
+static void *mdev_mmap(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ 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, void *mapping)
+{
+ 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;
+}
+
+const struct region_device_ops mem_rdev_ops = {
+ .mmap = mdev_mmap,
+ .munmap = mdev_munmap,
+ .readat = mdev_readat,
+};
+
+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;
+}
diff --git a/src/include/cbmem_id.h b/src/include/cbmem_id.h
deleted file mode 100644
index 6812c41..0000000
--- a/src/include/cbmem_id.h
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2009 coresystems GmbH
- * Copyright (C) 2013 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 _CBMEM_ID_H_
-#define _CBMEM_ID_H_
-
-#define CBMEM_ID_ACPI 0x41435049
-#define CBMEM_ID_ACPI_GNVS 0x474e5653
-#define CBMEM_ID_ACPI_GNVS_PTR 0x474e5650
-#define CBMEM_ID_AGESA_RUNTIME 0x41474553
-#define CBMEM_ID_AMDMCT_MEMINFO 0x494D454E
-#define CBMEM_ID_CAR_GLOBALS 0xcac4e6a3
-#define CBMEM_ID_CBTABLE 0x43425442
-#define CBMEM_ID_CONSOLE 0x434f4e53
-#define CBMEM_ID_COVERAGE 0x47434f56
-#define CBMEM_ID_EHCI_DEBUG 0xe4c1deb9
-#define CBMEM_ID_ELOG 0x454c4f47
-#define CBMEM_ID_FREESPACE 0x46524545
-#define CBMEM_ID_FSP_RESERVED_MEMORY 0x46535052
-#define CBMEM_ID_FSP_RUNTIME 0x52505346
-#define CBMEM_ID_GDT 0x4c474454
-#define CBMEM_ID_HOB_POINTER 0x484f4221
-#define CBMEM_ID_IGD_OPREGION 0x4f444749
-#define CBMEM_ID_IMD_ROOT 0xff4017ff
-#define CBMEM_ID_IMD_SMALL 0x53a11439
-#define CBMEM_ID_MEMINFO 0x494D454D
-#define CBMEM_ID_MPTABLE 0x534d5054
-#define CBMEM_ID_MRCDATA 0x4d524344
-#define CBMEM_ID_MTC 0xcb31d31c
-#define CBMEM_ID_NONE 0x00000000
-#define CBMEM_ID_PIRQ 0x49525154
-#define CBMEM_ID_POWER_STATE 0x50535454
-#define CBMEM_ID_RAM_OOPS 0x05430095
-#define CBMEM_ID_RAMSTAGE 0x9a357a9e
-#define CBMEM_ID_RAMSTAGE_CACHE 0x9a3ca54e
-#define CBMEM_ID_REFCODE 0x04efc0de
-#define CBMEM_ID_REFCODE_CACHE 0x4efc0de5
-#define CBMEM_ID_RESUME 0x5245534d
-#define CBMEM_ID_RESUME_SCRATCH 0x52455343
-#define CBMEM_ID_ROMSTAGE_INFO 0x47545352
-#define CBMEM_ID_ROMSTAGE_RAM_STACK 0x90357ac4
-#define CBMEM_ID_ROOT 0xff4007ff
-#define CBMEM_ID_SMBIOS 0x534d4254
-#define CBMEM_ID_SMM_SAVE_SPACE 0x07e9acee
-#define CBMEM_ID_SPINTABLE 0x59175917
-#define CBMEM_ID_STAGEx_META 0x57a9e000
-#define CBMEM_ID_STAGEx_CACHE 0x57a9e100
-#define CBMEM_ID_TCPA_LOG 0x54435041
-#define CBMEM_ID_TIMESTAMP 0x54494d45
-#define CBMEM_ID_VBOOT_HANDOFF 0x780074f0
-#define CBMEM_ID_VBOOT_WORKBUF 0x78007343
-#define CBMEM_ID_WIFI_CALIBRATION 0x57494649
-
-#define CBMEM_ID_TO_NAME_TABLE \
- { CBMEM_ID_ACPI, "ACPI " }, \
- { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " }, \
- { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " }, \
- { CBMEM_ID_AGESA_RUNTIME, "AGESA RSVD " }, \
- { CBMEM_ID_AMDMCT_MEMINFO, "AMDMEM INFO" }, \
- { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" }, \
- { CBMEM_ID_CBTABLE, "COREBOOT " }, \
- { CBMEM_ID_CONSOLE, "CONSOLE " }, \
- { CBMEM_ID_COVERAGE, "COVERAGE " }, \
- { CBMEM_ID_EHCI_DEBUG, "USBDEBUG " }, \
- { CBMEM_ID_ELOG, "ELOG " }, \
- { CBMEM_ID_FREESPACE, "FREE SPACE " }, \
- { CBMEM_ID_FSP_RESERVED_MEMORY, "FSP MEMORY " }, \
- { CBMEM_ID_FSP_RUNTIME, "FSP RUNTIME" }, \
- { CBMEM_ID_GDT, "GDT " }, \
- { CBMEM_ID_IMD_ROOT, "IMD ROOT " }, \
- { CBMEM_ID_IMD_SMALL, "IMD SMALL " }, \
- { CBMEM_ID_MEMINFO, "MEM INFO " }, \
- { CBMEM_ID_MPTABLE, "SMP TABLE " }, \
- { CBMEM_ID_MRCDATA, "MRC DATA " }, \
- { CBMEM_ID_MTC, "MTC " }, \
- { CBMEM_ID_PIRQ, "IRQ TABLE " }, \
- { CBMEM_ID_POWER_STATE, "POWER STATE" }, \
- { CBMEM_ID_RAM_OOPS, "RAMOOPS " }, \
- { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " }, \
- { CBMEM_ID_RAMSTAGE, "RAMSTAGE " }, \
- { CBMEM_ID_REFCODE_CACHE, "REFCODE $ " }, \
- { CBMEM_ID_REFCODE, "REFCODE " }, \
- { CBMEM_ID_RESUME, "ACPI RESUME" }, \
- { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" }, \
- { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " }, \
- { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" }, \
- { CBMEM_ID_ROOT, "CBMEM ROOT " }, \
- { CBMEM_ID_SMBIOS, "SMBIOS " }, \
- { CBMEM_ID_SMM_SAVE_SPACE, "SMM BACKUP " }, \
- { CBMEM_ID_SPINTABLE, "SPIN TABLE " }, \
- { CBMEM_ID_TCPA_LOG, "TCPA LOG " }, \
- { CBMEM_ID_TIMESTAMP, "TIME STAMP " }, \
- { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " }, \
- { CBMEM_ID_VBOOT_WORKBUF, "VBOOT WORK " }, \
- { CBMEM_ID_WIFI_CALIBRATION, "WIFI CLBR " },
-#endif /* _CBMEM_ID_H_ */
diff --git a/src/include/console/loglevel.h b/src/include/console/loglevel.h
deleted file mode 100644
index e147490..0000000
--- a/src/include/console/loglevel.h
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2015 Nicholas Sielicki <sielicki at nicky.io>
- *
- * 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; either version 2 of the License, or
- * (at your option) any later version.
- *
- * 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 LOGLEVEL_H
-#define LOGLEVEL_H
-
-/**
- * @file loglevel.h
- *
- * \brief Definitions of the log levels to be used in printk calls.
- *
- * Safe for inclusion in assembly.
- *
- */
-
-/**
- * \brief BIOS_EMERG - Emergency / Fatal
- *
- * Log level for when the system is entirely unusable. To be used when execution
- * is halting as a result of the failure. No further instructions should run.
- *
- * Example - End of all debug output / death notice.
- *
- * @{
- */
-#define BIOS_EMERG 0
-/** @} */
-
-/**
- * \brief BIOS_ALERT - Dying / Unrecoverable
- *
- * Log level for when the system is certainly in the process of dying.
- * To be used when execution will eventually halt as a result of the
- * failure, but the system can still output valuable debugging
- * information.
- *
- * Example - Ram initialization fails, dumping relevant POST codes and
- * information
- *
- * @{
- */
-#define BIOS_ALERT 1
-/** @} */
-
-/**
- * \brief BIOS_CRIT - Recovery unlikely
- *
- * Log level for when the system has experienced a dire issue in essential
- * components. To be used when boot will probably be unsuccessful as a
- * result of the failure, but recovery/retry can be attempted.
- *
- * Example - MSR failures, SMM/SMI failures.
- * or
- *
- * @{
- */
-#define BIOS_CRIT 2
-/** @} */
-
-/**
- * \brief BIOS_ERR - System in incomplete state.
- *
- * Log level for when the system has experienced an issue that may not preclude
- * a successful boot. To be used when coreboot execution may still succeed,
- * but the error places some non-essential portion of the machine in a broken
- * state that will be noticed downstream.
- *
- * Example - Payload could still load, but will be missing access to integral
- * components such as drives.
- *
- * @{
- */
-#define BIOS_ERR 3
-/** @} */
-
-/**
- * \brief BIOS_WARNING - Bad configuration
- *
- * Log level for when the system has noticed an issue that most likely will
- * not preclude a successful boot. To be used when something is wrong, and
- * would likely be noticed by an end user.
- *
- * Example - Bad ME firmware, bad microcode, mis-clocked CPU
- *
- * @{
- */
-#define BIOS_WARNING 4
-/** @} */
-
-/**
- * \brief BIOS_NOTICE - Unexpected but relatively insignificant
- *
- * Log level for when the system has noticed an issue that is an edge case,
- * but is handled and is recoverable. To be used when an end-user would likely
- * not notice.
- *
- * Example - Hardware was misconfigured, but is promptly fixed.
- *
- * @{
- */
-#define BIOS_NOTICE 5
-/** @} */
-
-/**
- * \brief BIOS_INFO - Expected events.
- *
- * Log level for when the system has experienced some typical event.
- * Messages should be superficial in nature.
- *
- * Example - Success messages. Status messages.
- *
- * @{
- */
-#define BIOS_INFO 6
-/** @} */
-
-/**
- * \brief BIOS_DEBUG - Verbose output
- *
- * Log level for details of a method. Messages may be dense,
- * but should not be excessive. Messages should be detailed enough
- * that this level provides sufficient details to diagnose a problem,
- * but not necessarily enough to fix it.
- *
- * Example - Printing of important variables.
- *
- * @{
- */
-#define BIOS_DEBUG 7
-/** @} */
-
-/**
- * \brief BIOS_SPEW - Excessively verbose output
- *
- * Log level for intricacies of a method. Messages might contain raw
- * data and will produce large logs. Developers should try to make sure
- * that this level is not useful to anyone besides developers.
- *
- * Example - Data dumps.
- *
- * @{
- */
-#define BIOS_SPEW 8
-/** @} */
-
-/**
- * \brief BIOS_NEVER - Muted log level.
- *
- * Roughly equal to commenting out a printk statement. Because a user
- * should not set their log level higher than 8, these statements
- * are never printed.
- *
- * Example - A developer might locally define MY_LOGLEVEL to BIOS_SPEW,
- * and later replace it with BIOS_NEVER as to mute their debug output.
- *
- * @{
- */
-#define BIOS_NEVER 9
-/** @} */
-
-#endif /* LOGLEVEL_H */
diff --git a/src/include/mem_pool.h b/src/include/mem_pool.h
deleted file mode 100644
index c57b707..0000000
--- a/src/include/mem_pool.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-
-#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/src/include/region.h b/src/include/region.h
deleted file mode 100644
index 82db854..0000000
--- a/src/include/region.h
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-
-#ifndef _REGION_H_
-#define _REGION_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);
-
-
-/****************************************
- * 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);
-};
-
-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_), \
- }, \
- }
-
-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 size_t region_device_sz(const struct region_device *rdev)
-{
- return region_sz(&rdev->region);
-}
-
-static inline size_t region_device_offset(const struct region_device *rdev)
-{
- return region_offset(&rdev->region);
-}
-
-/* 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));
-}
-
-struct mem_region_device {
- char *base;
- struct region_device rdev;
-};
-
-/* Iniitalize at runtime a mem_region_device. This would be used when
- * the base and size are dynamic or can't be known during linking. */
-void mem_region_device_init(struct mem_region_device *mdev, void *base,
- size_t size);
-
-extern const struct region_device_ops mem_rdev_ops;
-
-/* Statically initialize mem_region_device. */
-#define MEM_REGION_DEV_INIT(base_, size_) \
- { \
- .base = (void *)(base_), \
- .rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)), \
- }
-
-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 *);
-
-#endif /* _REGION_H_ */
diff --git a/src/include/rmodule-defs.h b/src/include/rmodule-defs.h
deleted file mode 100644
index d61837f..0000000
--- a/src/include/rmodule-defs.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright (C) 2014 Google Inc
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-#ifndef RMODULE_DEFS_H
-#define RMODULE_DEFS_H
-
-#include <stdint.h>
-#include <stddef.h>
-
-#define RMODULE_MAGIC 0xf8fe
-#define RMODULE_VERSION_1 1
-
-/* All fields with '_offset' in the name are byte offsets into the flat blob.
- * The linker and the linker script takes are of assigning the values. */
-struct rmodule_header {
- uint16_t magic;
- uint8_t version;
- uint8_t type;
- /* The payload represents the program's loadable code and data. */
- uint32_t payload_begin_offset;
- uint32_t payload_end_offset;
- /* Begin and of relocation information about the program module. */
- uint32_t relocations_begin_offset;
- uint32_t relocations_end_offset;
- /* The starting address of the linked program. This address is vital
- * for determining relocation offsets as the relocation info and other
- * symbols (bss, entry point) need this value as a basis to calculate
- * the offsets.
- */
- uint32_t module_link_start_address;
- /* The module_program_size is the size of memory used while running
- * the program. The program is assumed to consume a contiguous amount
- * of memory. */
- uint32_t module_program_size;
- /* This is program's execution entry point. */
- uint32_t module_entry_point;
- /* Optional parameter structure that can be used to pass data into
- * the module. */
- uint32_t parameters_begin;
- uint32_t parameters_end;
- /* BSS section information so the loader can clear the bss. */
- uint32_t bss_begin;
- uint32_t bss_end;
- /* Add some room for growth. */
- uint32_t padding[4];
-} __attribute__ ((packed));
-
-#endif /* RMODULE_DEFS_H */
diff --git a/src/include/stddef.h b/src/include/stddef.h
index f87c65f..035a53c 100644
--- a/src/include/stddef.h
+++ b/src/include/stddef.h
@@ -1,6 +1,8 @@
#ifndef STDDEF_H
#define STDDEF_H
+#include <helpers.h>
+
typedef long ptrdiff_t;
#ifndef __SIZE_TYPE__
#define __SIZE_TYPE__ unsigned long
@@ -19,38 +21,6 @@ typedef unsigned int wint_t;
#define NULL ((void *)0)
-/* Standard units. */
-#define KiB (1<<10)
-#define MiB (1<<20)
-#define GiB (1<<30)
-/* Could we ever run into this one? I hope we get this much memory! */
-#define TiB (1<<40)
-
-#define KHz (1000)
-#define MHz (1000 * KHz)
-#define GHz (1000 * MHz)
-
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
-#if !defined(__clang__)
-#define check_member(structure, member, offset) _Static_assert( \
- offsetof(struct structure, member) == offset, \
- "`struct " #structure "` offset for `" #member "` is not " #offset )
-#else
-#define check_member(structure, member, offset)
-#endif
-
-/**
- * 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) );})
-
#ifdef __PRE_RAM__
#define ROMSTAGE_CONST const
#else
diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc
index f4d8c2c..b670782 100644
--- a/src/lib/Makefile.inc
+++ b/src/lib/Makefile.inc
@@ -33,8 +33,6 @@ bootblock-$(CONFIG_CONSOLE_CBMEM) += cbmem_console.c
bootblock-$(CONFIG_I2C_TPM) += delay.c
bootblock-y += memchr.c
bootblock-y += memcmp.c
-bootblock-y += mem_pool.c
-bootblock-y += region.c
bootblock-y += boot_device.c
bootblock-y += fmap.c
@@ -49,7 +47,6 @@ verstage-y += cbfs_boot_props.c
verstage-y += libgcc.c
verstage-y += memcmp.c
verstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
-verstage-y += region.c
verstage-y += boot_device.c
verstage-$(CONFIG_CONSOLE_CBMEM) += cbmem_console.c
verstage-$(CONFIG_COMMON_CBFS_SPI_WRAPPER) += cbfs_spi.c
@@ -62,7 +59,6 @@ endif
verstage-$(CONFIG_GENERIC_UDELAY) += timer.c
verstage-$(CONFIG_GENERIC_GPIO_LIB) += gpio.c
-verstage-y += mem_pool.c
romstage-y += assets.c
romstage-y += prog_loaders.c
@@ -155,15 +151,10 @@ ramstage-$(CONFIG_RELOCATABLE_RAMSTAGE) += cbmem_stage_cache.c
romstage-$(CONFIG_RELOCATABLE_RAMSTAGE) += cbmem_stage_cache.c
endif
-romstage-y += mem_pool.c
-ramstage-y += mem_pool.c
-romstage-y += region.c
-ramstage-y += region.c
romstage-y += boot_device.c
ramstage-y += boot_device.c
-smm-y += region.c
smm-y += boot_device.c
smm-y += fmap.c
smm-y += cbfs.c memcmp.c
diff --git a/src/lib/mem_pool.c b/src/lib/mem_pool.c
deleted file mode 100644
index 4bd0668..0000000
--- a/src/lib/mem_pool.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-
-#include <mem_pool.h>
-#include <stdlib.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/src/lib/region.c b/src/lib/region.c
deleted file mode 100644
index d5d3762..0000000
--- a/src/lib/region.c
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * This file is part of the coreboot project.
- *
- * Copyright 2015 Google Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc.
- */
-
-#include <region.h>
-#include <string.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;
-}
-
-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);
-}
-
-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;
-}
-
-void mem_region_device_init(struct mem_region_device *mdev, void *base,
- size_t size)
-{
- memset(mdev, 0, sizeof(*mdev));
- mdev->base = base;
- mdev->rdev.ops = &mem_rdev_ops;
- mdev->rdev.region.size = size;
-}
-
-static void *mdev_mmap(const struct region_device *rd, size_t offset,
- size_t size)
-{
- 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, void *mapping)
-{
- 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;
-}
-
-const struct region_device_ops mem_rdev_ops = {
- .mmap = mdev_mmap,
- .munmap = mdev_munmap,
- .readat = mdev_readat,
-};
-
-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;
-}
diff --git a/src/vendorcode/amd/pi/Makefile.inc b/src/vendorcode/amd/pi/Makefile.inc
index a3d7fc1..118a2a4 100644
--- a/src/vendorcode/amd/pi/Makefile.inc
+++ b/src/vendorcode/amd/pi/Makefile.inc
@@ -61,6 +61,7 @@ AGESA_INC += -I$(src)/southbridge/amd/pi/hudson
AGESA_INC += -I$(src)/arch/x86/include
AGESA_INC += -I$(src)/include
+AGESA_INC += -I$(src)/commonlib/include
AGESA_CFLAGS += -march=amdfam10 -mno-3dnow -fno-zero-initialized-in-bss -fno-strict-aliasing
CFLAGS_x86_32 += $(AGESA_CFLAGS)
diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile
index 65d5710..db93fe1 100644
--- a/util/cbfstool/Makefile
+++ b/util/cbfstool/Makefile
@@ -9,6 +9,7 @@ CFLAGS += -Wstrict-prototypes -Wwrite-strings
CPPFLAGS += -D_DEFAULT_SOURCE # memccpy() from string.h
CPPFLAGS += -D_POSIX_C_SOURCE=200809L # strdup() from string.h
CPPFLAGS += -Iflashmap
+CPPFLAGS += -I../../src/commonlib/include
LDFLAGS += -g3
CBFSTOOL_BINARY:=$(obj)/cbfstool
diff --git a/util/cbfstool/Makefile.inc b/util/cbfstool/Makefile.inc
index 976f0c2..f14ad11 100644
--- a/util/cbfstool/Makefile.inc
+++ b/util/cbfstool/Makefile.inc
@@ -49,6 +49,7 @@ TOOLCPPFLAGS += -D_XOPEN_SOURCE=700 # strdup() from string.h
TOOLCPPFLAGS += -I$(top)/util/cbfstool/flashmap
TOOLCPPFLAGS += -I$(top)/util/cbfstool
TOOLCPPFLAGS += -I$(objutil)/cbfstool
+TOOLCPPFLAGS += -I$(src)/commonlib/include
TOOLLDFLAGS ?=
ifeq ($(shell uname -s | cut -c-7 2>/dev/null), MINGW32)
diff --git a/util/cbfstool/rmodule.c b/util/cbfstool/rmodule.c
index 1f41d17..5b193d9 100644
--- a/util/cbfstool/rmodule.c
+++ b/util/cbfstool/rmodule.c
@@ -22,7 +22,7 @@
#include "elfparsing.h"
#include "rmodule.h"
-#include "../../src/include/rmodule-defs.h"
+#include <rmodule-defs.h>
struct rmod_context;
diff --git a/util/cbmem/Makefile b/util/cbmem/Makefile
index 1e75345..1d8ad6d 100644
--- a/util/cbmem/Makefile
+++ b/util/cbmem/Makefile
@@ -22,7 +22,7 @@ ROOT = ../../src
CC ?= $(CROSS_COMPILE)gcc
CFLAGS ?= -O2
CFLAGS += -Wall -Werror
-CPPFLAGS += -iquote $(ROOT)/include -iquote $(ROOT)/src/arch/x86
+CPPFLAGS += -iquote $(ROOT)/include -iquote $(ROOT)/src/arch/x86 -I $(ROOT)/commonlib/include
OBJS = $(PROGRAM).o
diff --git a/util/cbmem/cbmem.c b/util/cbmem/cbmem.c
index afb83f5..33d6493 100644
--- a/util/cbmem/cbmem.c
+++ b/util/cbmem/cbmem.c
@@ -51,7 +51,7 @@ typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
-#include "cbmem_id.h"
+#include <cbmem_id.h>
#include "timestamp.h"
#define CBMEM_VERSION "1.1"
More information about the coreboot-gerrit
mailing list