summaryrefslogtreecommitdiff
path: root/payloads/libpayload
diff options
context:
space:
mode:
Diffstat (limited to 'payloads/libpayload')
-rw-r--r--payloads/libpayload/Kconfig6
-rw-r--r--payloads/libpayload/Makefile.inc1
-rw-r--r--payloads/libpayload/include/cbfs.h180
-rw-r--r--payloads/libpayload/include/cbfs_core.h154
-rw-r--r--payloads/libpayload/include/cbfs_glue.h46
-rw-r--r--payloads/libpayload/include/cbfs_legacy.h83
-rw-r--r--payloads/libpayload/libcbfs/Kconfig31
-rw-r--r--payloads/libpayload/libcbfs/Makefile.inc6
-rw-r--r--payloads/libpayload/libcbfs/cbfs.c363
-rw-r--r--payloads/libpayload/libcbfs/cbfs_core.c1
-rw-r--r--payloads/libpayload/libcbfs/cbfs_legacy.c223
-rw-r--r--payloads/libpayload/tests/Makefile.inc1
-rw-r--r--payloads/libpayload/tests/include/mocks/cbfs_util.h114
-rw-r--r--payloads/libpayload/tests/libcbfs/Makefile.inc33
-rw-r--r--payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c642
-rw-r--r--payloads/libpayload/tests/libcbfs/cbfs-verification-test.c247
-rw-r--r--payloads/libpayload/tests/mocks/cbfs_file_mock.c95
-rw-r--r--payloads/libpayload/tests/mocks/die.c16
18 files changed, 1810 insertions, 432 deletions
diff --git a/payloads/libpayload/Kconfig b/payloads/libpayload/Kconfig
index f24778880a..4f8896aa6e 100644
--- a/payloads/libpayload/Kconfig
+++ b/payloads/libpayload/Kconfig
@@ -208,11 +208,7 @@ config PDCURSES
endchoice
-config CBFS
- bool "CBFS support"
- default y
- help
- CBFS is the archive format of coreboot
+source "libcbfs/Kconfig"
config LZMA
bool "LZMA decoder"
diff --git a/payloads/libpayload/Makefile.inc b/payloads/libpayload/Makefile.inc
index 77eab60c6b..ac7bb81715 100644
--- a/payloads/libpayload/Makefile.inc
+++ b/payloads/libpayload/Makefile.inc
@@ -62,6 +62,7 @@ subdirs-$(CONFIG_LP_VBOOT_LIB) += vboot
INCLUDES := -Iinclude -Iinclude/$(ARCHDIR-y) -I$(obj)
INCLUDES += -include include/kconfig.h -include include/compiler.h
INCLUDES += -I$(coreboottop)/src/commonlib/bsd/include
+INCLUDES += -I$(coreboottop)/3rdparty/vboot/firmware/include
CFLAGS += $(INCLUDES) -Os -pipe -nostdinc -ggdb3
CFLAGS += -nostdlib -fno-builtin -ffreestanding -fomit-frame-pointer
diff --git a/payloads/libpayload/include/cbfs.h b/payloads/libpayload/include/cbfs.h
index ab23b02e03..8adadb737e 100644
--- a/payloads/libpayload/include/cbfs.h
+++ b/payloads/libpayload/include/cbfs.h
@@ -1,82 +1,124 @@
-/*
- *
- * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
- * 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.
- * ---------------------------------------------------------------------------
- */
+/* SPDX-License-Identifier: BSD-3-Clause */
#ifndef _CBFS_H_
#define _CBFS_H_
-#include <cbfs_core.h>
+#include <commonlib/bsd/cb_err.h>
+#include <commonlib/bsd/cbfs_mdata.h>
+#include <endian.h>
+#include <stdbool.h>
-/* legacy APIs */
-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,
- 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);
+/**********************************************************************************************
+ * CBFS FILE ACCESS APIs *
+ **********************************************************************************************/
-/* Simple buffer for streaming media. */
-struct cbfs_simple_buffer {
- char *buffer;
- size_t allocated;
- size_t size;
- size_t last_allocate;
-};
+/* For documentation look in src/include/cbfs.h file in the main coreboot source tree. */
-void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
- struct cbfs_media *media,
- size_t offset, size_t count);
+static inline size_t cbfs_load(const char *name, void *buf, size_t size);
+static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size);
-void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
- const void *address);
+static inline void *cbfs_map(const char *name, size_t *size_out);
+static inline void *cbfs_ro_map(const char *name, size_t *size_out);
-// Utility functions
-int run_address(void *f);
+void cbfs_unmap(void *mapping);
-/* Defined in individual arch / board implementation. */
-int init_default_cbfs_media(struct cbfs_media *media);
+static inline size_t cbfs_get_size(const char *name);
+static inline size_t cbfs_ro_get_size(const char *name);
+
+static inline enum cbfs_type cbfs_get_type(const char *name);
+static inline enum cbfs_type cbfs_ro_get_type(const char *name);
+
+static inline bool cbfs_file_exists(const char *name);
+static inline bool cbfs_ro_file_exists(const char *name);
+
+/**********************************************************************************************
+ * INTERNAL HELPERS FOR INLINES, DO NOT USE. *
+ **********************************************************************************************/
+ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mdata);
+
+void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro);
+
+/**********************************************************************************************
+ * INLINE IMPLEMENTATIONS *
+ **********************************************************************************************/
+
+static inline void *cbfs_map(const char *name, size_t *size_out)
+{
+ return _cbfs_load(name, NULL, size_out, false);
+}
+
+static inline void *cbfs_ro_map(const char *name, size_t *size_out)
+{
+ return _cbfs_load(name, NULL, size_out, true);
+}
+
+static inline size_t cbfs_load(const char *name, void *buf, size_t size)
+{
+ if (_cbfs_load(name, buf, &size, false))
+ return size;
+ else
+ return 0;
+}
+
+static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size)
+{
+ if (_cbfs_load(name, buf, &size, true))
+ return size;
+ else
+ return 0;
+}
+
+static inline size_t cbfs_get_size(const char *name)
+{
+ union cbfs_mdata mdata;
+ if (_cbfs_boot_lookup(name, false, &mdata) < 0)
+ return 0;
+ else
+ return be32toh(mdata.h.len);
+}
+
+static inline size_t cbfs_ro_get_size(const char *name)
+{
+ union cbfs_mdata mdata;
+ if (_cbfs_boot_lookup(name, true, &mdata) < 0)
+ return 0;
+ else
+ return be32toh(mdata.h.len);
+}
+
+static inline enum cbfs_type cbfs_get_type(const char *name)
+{
+ union cbfs_mdata mdata;
+ if (_cbfs_boot_lookup(name, false, &mdata) < 0)
+ return CBFS_TYPE_NULL;
+ else
+ return be32toh(mdata.h.type);
+}
+
+static inline enum cbfs_type cbfs_ro_get_type(const char *name)
+{
+ union cbfs_mdata mdata;
+ if (_cbfs_boot_lookup(name, true, &mdata) < 0)
+ return CBFS_TYPE_NULL;
+ else
+ return be32toh(mdata.h.type);
+}
+
+static inline bool cbfs_file_exists(const char *name)
+{
+ union cbfs_mdata mdata;
+ return _cbfs_boot_lookup(name, false, &mdata) >= 0;
+}
+
+static inline bool cbfs_ro_file_exists(const char *name)
+{
+ union cbfs_mdata mdata;
+ return _cbfs_boot_lookup(name, true, &mdata) >= 0;
+}
+
+
+/* Legacy API. Designated for removal in the future. */
+#include <cbfs_legacy.h>
#endif
diff --git a/payloads/libpayload/include/cbfs_core.h b/payloads/libpayload/include/cbfs_core.h
index fc4caa4417..4a638d971a 100644
--- a/payloads/libpayload/include/cbfs_core.h
+++ b/payloads/libpayload/include/cbfs_core.h
@@ -45,139 +45,14 @@
#ifndef _CBFS_CORE_H_
#define _CBFS_CORE_H_
+#include <commonlib/bsd/cbfs_serialized.h>
#include <endian.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
-/** These are standard values for the known compression
- alogrithms that coreboot knows about for stages and
- payloads. Of course, other CBFS users can use whatever
- values they want, as long as they understand them. */
-
-#define CBFS_COMPRESS_NONE 0
-#define CBFS_COMPRESS_LZMA 1
-#define CBFS_COMPRESS_LZ4 2
-
-/** These are standard component types for well known
- components (i.e - those that coreboot needs to consume.
- Users are welcome to use any other value for their
- components */
-
-#define CBFS_TYPE_STAGE 0x10
-#define CBFS_TYPE_SELF 0x20
-#define CBFS_TYPE_FIT 0x21
-#define CBFS_TYPE_OPTIONROM 0x30
-#define CBFS_TYPE_BOOTSPLASH 0x40
-#define CBFS_TYPE_RAW 0x50
-#define CBFS_TYPE_VSA 0x51
-#define CBFS_TYPE_MBI 0x52
-#define CBFS_TYPE_MICROCODE 0x53
-#define CBFS_TYPE_STRUCT 0x70
-#define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
-#define CBFS_COMPONENT_CMOS_LAYOUT 0x01aa
-
-#define CBFS_HEADER_MAGIC 0x4F524243
-#define CBFS_HEADER_VERSION1 0x31313131
-#define CBFS_HEADER_VERSION2 0x31313132
-#define CBFS_HEADER_VERSION CBFS_HEADER_VERSION2
-
#define CBFS_HEADER_INVALID_ADDRESS ((void*)(0xffffffff))
-/* this is the master cbfs header - it must be located somewhere available
- * to bootblock (to load romstage). The last 4 bytes in the image contain its
- * relative offset from the end of the image (as a 32-bit signed integer). */
-
-struct cbfs_header {
- uint32_t magic;
- uint32_t version;
- uint32_t romsize;
- uint32_t bootblocksize;
- uint32_t align; /* fixed to 64 bytes */
- uint32_t offset;
- uint32_t architecture;
- uint32_t pad[1];
-} __packed;
-
-/* this used to be flexible, but wasn't ever set to something different. */
-#define CBFS_ALIGNMENT 64
-
-/* "Unknown" refers to CBFS headers version 1,
- * before the architecture was defined (i.e., x86 only).
- */
-#define CBFS_ARCHITECTURE_UNKNOWN 0xFFFFFFFF
-#define CBFS_ARCHITECTURE_X86 0x00000001
-#define CBFS_ARCHITECTURE_ARM 0x00000010
-#define CBFS_ARCHITECTURE_ARM64 0x00000011
-
-/** This is a component header - every entry in the CBFS
- will have this header.
-
- This is how the component is arranged in the ROM:
-
- -------------- <- 0
- component header
- -------------- <- sizeof(struct component)
- component name
- -------------- <- offset
- data
- ...
- -------------- <- offset + len
-*/
-
-#define CBFS_FILE_MAGIC "LARCHIVE"
-
-struct cbfs_file {
- char magic[8];
- uint32_t len;
- uint32_t type;
- uint32_t attributes_offset;
- uint32_t offset;
- char filename[];
-} __packed;
-
-/* Depending on how the header was initialized, it may be backed with 0x00 or
- * 0xff. Support both. */
-#define CBFS_FILE_ATTR_TAG_UNUSED 0
-#define CBFS_FILE_ATTR_TAG_UNUSED2 0xffffffff
-#define CBFS_FILE_ATTR_TAG_COMPRESSION 0x42435a4c
-#define CBFS_FILE_ATTR_TAG_HASH 0x68736148
-#define CBFS_FILE_ATTR_TAG_IBB 0x32494242 /* Initial BootBlock */
-
-/* The common fields of extended cbfs file attributes.
- Attributes are expected to start with tag/len, then append their
- specific fields. */
-struct cbfs_file_attribute {
- uint32_t tag;
- /* len covers the whole structure, incl. tag and len */
- uint32_t len;
- uint8_t data[0];
-} __packed;
-
-struct cbfs_file_attr_compression {
- uint32_t tag;
- uint32_t len;
- /* whole file compression format. 0 if no compression. */
- uint32_t compression;
- uint32_t decompressed_size;
-} __packed;
-
-struct cbfs_file_attr_hash {
- uint32_t tag;
- uint32_t len;
- uint32_t hash_type;
- /* hash_data is len - sizeof(struct) bytes */
- uint8_t hash_data[];
-} __packed;
-
-/*** Component sub-headers ***/
-
-/* Following are component sub-headers for the "standard"
- component types */
-
-/** This is the sub-header for stage components. Stages are
- loaded by coreboot during the normal boot process */
-
struct cbfs_stage {
uint32_t compression; /** Compression type */
uint64_t entry; /** entry point */
@@ -186,33 +61,6 @@ struct cbfs_stage {
uint32_t memlen; /** total length of object in memory */
} __packed;
-/** this is the sub-header for payload components. Payloads
- are loaded by coreboot at the end of the boot process */
-
-struct cbfs_payload_segment {
- uint32_t type;
- uint32_t compression;
- uint32_t offset;
- uint64_t load_addr;
- uint32_t len;
- uint32_t mem_len;
-} __packed;
-
-struct cbfs_payload {
- struct cbfs_payload_segment segments;
-};
-
-#define PAYLOAD_SEGMENT_CODE 0x45444F43
-#define PAYLOAD_SEGMENT_DATA 0x41544144
-#define PAYLOAD_SEGMENT_BSS 0x20535342
-#define PAYLOAD_SEGMENT_PARAMS 0x41524150
-#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
-
-struct cbfs_optionrom {
- uint32_t compression;
- uint32_t len;
-} __packed;
-
#define CBFS_MEDIA_INVALID_MAP_ADDRESS ((void*)(0xffffffff))
#define CBFS_DEFAULT_MEDIA ((void*)(0x0))
diff --git a/payloads/libpayload/include/cbfs_glue.h b/payloads/libpayload/include/cbfs_glue.h
new file mode 100644
index 0000000000..00d0ea943a
--- /dev/null
+++ b/payloads/libpayload/include/cbfs_glue.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+
+#ifndef _CBFS_CBFS_GLUE_H
+#define _CBFS_CBFS_GLUE_H
+
+#include <libpayload-config.h>
+#include <boot_device.h>
+#include <stdio.h>
+
+#define CBFS_ENABLE_HASHING CONFIG(LP_CBFS_VERIFICATION)
+
+#define ERROR(...) printf("CBFS ERROR: " __VA_ARGS__)
+#define LOG(...) printf("CBFS: " __VA_ARGS__)
+#define DEBUG(...) \
+ do { \
+ if (CONFIG(LP_DEBUG_CBFS)) \
+ printf("CBFS DEBUG: " __VA_ARGS__); \
+ } while (0)
+
+struct cbfs_dev {
+ size_t offset;
+ size_t size;
+};
+
+struct cbfs_boot_device {
+ struct cbfs_dev dev;
+ void *mcache;
+ size_t mcache_size;
+};
+
+typedef const struct cbfs_dev *cbfs_dev_t;
+
+static inline ssize_t cbfs_dev_read(cbfs_dev_t dev, void *buffer, size_t offset, size_t size)
+{
+ if (offset + size < offset || offset + size > dev->size)
+ return CB_ERR_ARG;
+
+ return boot_device_read(buffer, dev->offset + offset, size);
+}
+
+static inline size_t cbfs_dev_size(cbfs_dev_t dev)
+{
+ return dev->size;
+}
+
+#endif /* _CBFS_CBFS_GLUE_H */
diff --git a/payloads/libpayload/include/cbfs_legacy.h b/payloads/libpayload/include/cbfs_legacy.h
new file mode 100644
index 0000000000..c1b896cea4
--- /dev/null
+++ b/payloads/libpayload/include/cbfs_legacy.h
@@ -0,0 +1,83 @@
+/*
+ *
+ * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
+ * 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_LEGACY_H_
+#define _CBFS_LEGACY_H_
+
+#include <cbfs_core.h>
+#include <libpayload.h>
+
+/* legacy APIs */
+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,
+ 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);
+
+/* Simple buffer for streaming media. */
+struct cbfs_simple_buffer {
+ char *buffer;
+ size_t allocated;
+ size_t size;
+ size_t last_allocate;
+};
+
+void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
+ struct cbfs_media *media,
+ size_t offset, size_t count);
+
+void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
+ const void *address);
+
+// Utility functions
+int run_address(void *f);
+
+/* Defined in individual arch / board implementation. */
+int init_default_cbfs_media(struct cbfs_media *media);
+
+#endif
diff --git a/payloads/libpayload/libcbfs/Kconfig b/payloads/libpayload/libcbfs/Kconfig
new file mode 100644
index 0000000000..634d77f3e5
--- /dev/null
+++ b/payloads/libpayload/libcbfs/Kconfig
@@ -0,0 +1,31 @@
+## SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
+
+config CBFS
+ bool "CBFS support"
+ default y
+ help
+ CBFS is the archive format of coreboot
+
+if CBFS
+
+config DEBUG_CBFS
+ bool "Output verbose CBFS debug messages"
+ default n
+ help
+ This option enables additional CBFS related debug messages.
+
+config ENABLE_CBFS_FALLBACK
+ bool "Fallback to RO (COREBOOT) region"
+ default n
+ help
+ When this option is enabled, the CBFS code will look for a file in the
+ RO (COREBOOT) region, if it isn't available in the active RW region.
+ This option makes sense only if CONFIG_VBOOT was enabled in the coreboot.
+
+config CBFS_VERIFICATION
+ bool "Enable CBFS verification"
+ depends on VBOOT
+ help
+ This option enables hash verification of CBFS files in RO (COREBOOT) and RW regions.
+
+endif
diff --git a/payloads/libpayload/libcbfs/Makefile.inc b/payloads/libpayload/libcbfs/Makefile.inc
index 85ef485822..53e6f7fa43 100644
--- a/payloads/libpayload/libcbfs/Makefile.inc
+++ b/payloads/libpayload/libcbfs/Makefile.inc
@@ -28,3 +28,9 @@
libcbfs-$(CONFIG_LP_CBFS) += cbfs.c
libcbfs-$(CONFIG_LP_CBFS) += ram_media.c
+libcbfs-$(CONFIG_LP_CBFS) += cbfs_legacy.c
+
+ifeq ($(CONFIG_LP_CBFS),y)
+libcbfs-srcs += $(coreboottop)/src/commonlib/bsd/cbfs_private.c
+libcbfs-srcs += $(coreboottop)/src/commonlib/bsd/cbfs_mcache.c
+endif
diff --git a/payloads/libpayload/libcbfs/cbfs.c b/payloads/libpayload/libcbfs/cbfs.c
index d24b528ded..530f9e292d 100644
--- a/payloads/libpayload/libcbfs/cbfs.c
+++ b/payloads/libpayload/libcbfs/cbfs.c
@@ -1,243 +1,198 @@
-/*
- *
- * Copyright (C) 2011 secunet Security Networks AG
- * 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.
- */
-#define LIBPAYLOAD
-
-#ifdef LIBPAYLOAD
-# include <libpayload-config.h>
-# if CONFIG(LP_LZMA)
-# include <lzma.h>
-# define CBFS_CORE_WITH_LZMA
-# endif
-# if CONFIG(LP_LZ4)
-# include <lz4.h>
-# define CBFS_CORE_WITH_LZ4
-# endif
-# define CBFS_MINI_BUILD
-#elif defined(__SMM__)
-# define CBFS_MINI_BUILD
-#else
-# define CBFS_CORE_WITH_LZMA
-# define CBFS_CORE_WITH_LZ4
-# include <lib.h>
-#endif
+/* SPDX-License-Identifier: BSD-3-Clause */
+#include <libpayload-config.h>
+#include <arch/virtual.h>
+#include <assert.h>
#include <cbfs.h>
+#include <commonlib/bsd/cbfs_private.h>
+#include <commonlib/bsd/fmap_serialized.h>
+#include <libpayload.h>
+#include <lz4.h>
+#include <lzma.h>
#include <string.h>
+#include <sysinfo.h>
-#ifdef LIBPAYLOAD
-# include <stdio.h>
-# define DEBUG(x...)
-# define LOG(x...)
-# define ERROR(x...) printf(x)
-#else
-# include <console/console.h>
-# define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
-# define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
-# if CONFIG_LP_DEBUG_CBFS
-# define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
-# else
-# define DEBUG(x...)
-# endif
-#endif
-
-#include "cbfs_core.c"
-
-#ifndef __SMM__
-static inline int tohex4(unsigned int c)
-{
- return (c <= 9) ? (c + '0') : (c - 10 + 'a');
-}
-static void tohex16(unsigned int val, char* dest)
+static const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
{
- dest[0] = tohex4(val>>12);
- dest[1] = tohex4((val>>8) & 0xf);
- dest[2] = tohex4((val>>4) & 0xf);
- dest[3] = tohex4(val & 0xf);
-}
+ static struct cbfs_boot_device ro;
+ static struct cbfs_boot_device rw;
+
+ if (!force_ro) {
+ if (!rw.dev.size) {
+ rw.dev.offset = lib_sysinfo.cbfs_offset;
+ rw.dev.size = lib_sysinfo.cbfs_size;
+ rw.mcache = phys_to_virt(lib_sysinfo.cbfs_rw_mcache_offset);
+ rw.mcache_size = lib_sysinfo.cbfs_rw_mcache_size;
+ }
+ return &rw;
+ }
-void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
- uint16_t device)
-{
- char name[17] = "pciXXXX,XXXX.rom";
+ if (ro.dev.size)
+ return &ro;
- tohex16(vendor, name+3);
- tohex16(device, name+8);
+ if (fmap_locate_area("COREBOOT", &ro.dev.offset, &ro.dev.size))
+ return NULL;
+
+ ro.mcache = phys_to_virt(lib_sysinfo.cbfs_ro_mcache_offset);
+ ro.mcache_size = lib_sysinfo.cbfs_ro_mcache_size;
- return cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
+ return &ro;
}
-void * cbfs_load_stage(struct cbfs_media *media, const char *name)
+ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mdata)
{
- struct cbfs_stage *stage = (struct cbfs_stage *)
- cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
- /* this is a mess. There is no ntohll. */
- /* for now, assume compatible byte order until we solve this. */
- uintptr_t entry;
- uint32_t final_size;
-
- if (stage == NULL)
- return (void *) -1;
-
- LOG("loading stage %s @ %p (%d bytes), entry @ 0x%llx\n",
- name,
- (void*)(uintptr_t) stage->load, stage->memlen,
- stage->entry);
-
- final_size = cbfs_decompress(stage->compression,
- ((unsigned char *) stage) +
- sizeof(struct cbfs_stage),
- stage->len,
- (void *) (uintptr_t) stage->load,
- stage->memlen);
- if (!final_size) {
- entry = -1;
- goto out;
+ const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
+ if (!cbd)
+ return CB_ERR;
+
+ size_t data_offset;
+ cb_err_t err = CB_CBFS_CACHE_FULL;
+ if (cbd->mcache_size)
+ err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size, name, mdata,
+ &data_offset);
+
+ if (err == CB_CBFS_CACHE_FULL)
+ err = cbfs_lookup(&cbd->dev, name, mdata, &data_offset, NULL);
+
+ /* Fallback to RO if possible. */
+ if (CONFIG(LP_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) {
+ LOG("Fall back to RO region for '%s'\n", name);
+ return _cbfs_boot_lookup(name, true, mdata);
}
- memset((void *)((uintptr_t)stage->load + final_size), 0,
- stage->memlen - final_size);
-
- DEBUG("stage loaded.\n");
-
- entry = stage->entry;
- // entry = ntohll(stage->entry);
+ if (err) {
+ if (err == CB_CBFS_NOT_FOUND)
+ LOG("'%s' not found.\n", name);
+ else
+ ERROR("Error %d when looking up '%s'\n", err, name);
+ return err;
+ }
-out:
- free(stage);
- return (void *) entry;
+ return cbd->dev.offset + data_offset;
}
-int cbfs_execute_stage(struct cbfs_media *media, const char *name)
+void cbfs_unmap(void *mapping)
{
- struct cbfs_stage *stage = (struct cbfs_stage *)
- cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
+ free(mapping);
+}
- if (stage == NULL)
- return 1;
+static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
+ const union cbfs_mdata *mdata)
+{
+ if (!CONFIG(LP_CBFS_VERIFICATION))
+ return false;
- if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
- LOG("Unable to run %s: Compressed file"
- "Not supported for in-place execution\n", name);
- free(stage);
- return 1;
+ const struct vb2_hash *hash = cbfs_file_hash(mdata);
+ if (!hash) {
+ ERROR("'%s' does not have a file hash!\n", mdata->h.filename);
+ return true;
+ }
+ if (vb2_hash_verify(buffer, size, hash) != VB2_SUCCESS) {
+ ERROR("'%s' file hash mismatch!\n", mdata->h.filename);
+ return true;
}
- LOG("run @ %p\n", (void *) (uintptr_t)ntohll(stage->entry));
- int result = run_address((void *)(uintptr_t)ntohll(stage->entry));
- free(stage);
- return result;
+ return false;
}
-void *cbfs_load_payload(struct cbfs_media *media, const char *name)
+static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buffer,
+ size_t buffer_size, uint32_t compression,
+ const union cbfs_mdata *mdata)
{
- return (struct cbfs_payload *)cbfs_get_file_content(
- media, name, CBFS_TYPE_SELF, NULL);
-}
+ void *load = buffer;
+ size_t out_size = 0;
+
+ DEBUG("Decompressing %zu bytes from '%s' to %p with algo %d\n", in_size,
+ mdata->h.filename, buffer, compression);
+
+ if (compression != CBFS_COMPRESS_NONE) {
+ load = malloc(in_size);
+ if (!load) {
+ ERROR("'%s' buffer allocation failed\n", mdata->h.filename);
+ return 0;
+ }
+ }
-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 (boot_device_read(load, offset, in_size) != in_size) {
+ ERROR("'%s' failed to read contents of file\n", mdata->h.filename);
+ goto out;
+ }
- if (!handle)
- return NULL;
+ if (cbfs_file_hash_mismatch(buffer, in_size, mdata))
+ goto out;
- ret = m->map(m, handle->media_offset,
- handle->content_offset + handle->content_size);
- if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
- free(handle);
- return NULL;
+ switch (compression) {
+ case CBFS_COMPRESS_NONE:
+ out_size = in_size;
+ break;
+ case CBFS_COMPRESS_LZ4:
+ if (!CONFIG(LP_LZ4))
+ goto out;
+ out_size = ulz4fn(load, in_size, buffer, buffer_size);
+ break;
+ case CBFS_COMPRESS_LZMA:
+ if (!CONFIG(LP_LZMA))
+ goto out;
+ out_size = ulzman(load, in_size, buffer, buffer_size);
+ break;
+ default:
+ ERROR("'%s' decompression algo %d not supported\n", mdata->h.filename,
+ compression);
}
-
- free(handle);
- return ret;
+out:
+ if (load != buffer)
+ free(load);
+ return out_size;
}
-void *cbfs_find_file(const char *name, int type) {
- return cbfs_get_file_content(CBFS_DEFAULT_MEDIA, name, type, NULL);
-}
+void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro)
+{
+ ssize_t offset;
+ size_t out_size;
+ union cbfs_mdata mdata;
+ bool malloced = false;
-const struct cbfs_header *get_cbfs_header(void) {
- return cbfs_get_header(CBFS_DEFAULT_MEDIA);
-}
+ DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf,
+ force_ro ? "true" : "false");
-/* Simple buffer */
-
-void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
- struct cbfs_media *media,
- size_t offset, size_t count) {
- void *address = buffer->buffer + buffer->allocated;
- DEBUG("simple_buffer_map(offset=%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) {
- ERROR("simple_buffer: fail to read %zd bytes from 0x%zx\n",
- count, offset);
- return CBFS_MEDIA_INVALID_MAP_ADDRESS;
+ offset = _cbfs_boot_lookup(name, force_ro, &mdata);
+ if (offset < 0)
+ return NULL;
+
+ uint32_t compression = CBFS_COMPRESS_NONE;
+ const struct cbfs_file_attr_compression *cattr =
+ cbfs_find_attr(&mdata, CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
+ if (cattr) {
+ compression = be32toh(cattr->compression);
+ out_size = be32toh(cattr->decompressed_size);
+ } else {
+ out_size = be32toh(mdata.h.len);
}
- buffer->allocated += count;
- buffer->last_allocate = count;
- return address;
-}
-void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
- const void *address) {
- // TODO Add simple buffer management so we can free more than last
- // allocated one.
- DEBUG("simple_buffer_unmap(address=%p): "
- "allocated=%zu, size=%zu, last_allocate=%zu\n",
- address, buffer->allocated, buffer->size,
- buffer->last_allocate);
- if ((buffer->buffer + buffer->allocated - buffer->last_allocate) ==
- address) {
- buffer->allocated -= buffer->last_allocate;
- buffer->last_allocate = 0;
+ if (buf) {
+ if (!size_inout || *size_inout < out_size) {
+ ERROR("'%s' buffer too small", mdata.h.filename);
+ return NULL;
+ }
+ } else {
+ buf = malloc(out_size);
+ if (!buf) {
+ ERROR("'%s' allocation failure", mdata.h.filename);
+ return NULL;
+ }
+ malloced = true;
}
- return NULL;
-}
-/**
- * run_address is passed the address of a function taking no parameters and
- * jumps to it, returning the result.
- * @param f the address to call as a function.
- * @return value returned by the function.
- */
+ if (cbfs_load_and_decompress(offset, be32toh(mdata.h.len), buf, out_size, compression,
+ &mdata)
+ != out_size) {
+ if (malloced)
+ free(buf);
+ return NULL;
+ }
+ if (size_inout)
+ *size_inout = out_size;
-int run_address(void *f)
-{
- int (*v) (void);
- v = f;
- return v();
+ return buf;
}
-
-#endif
diff --git a/payloads/libpayload/libcbfs/cbfs_core.c b/payloads/libpayload/libcbfs/cbfs_core.c
index 82c2846054..bc513682c9 100644
--- a/payloads/libpayload/libcbfs/cbfs_core.c
+++ b/payloads/libpayload/libcbfs/cbfs_core.c
@@ -242,7 +242,6 @@ void *cbfs_get_contents(struct cbfs_handle *handle, size_t *size, size_t limit)
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);
*size = ntohl(comp->decompressed_size);
/* TODO: Implement partial decompression with |limit| */
}
diff --git a/payloads/libpayload/libcbfs/cbfs_legacy.c b/payloads/libpayload/libcbfs/cbfs_legacy.c
new file mode 100644
index 0000000000..8249196d9a
--- /dev/null
+++ b/payloads/libpayload/libcbfs/cbfs_legacy.c
@@ -0,0 +1,223 @@
+/*
+ *
+ * Copyright (C) 2011 secunet Security Networks AG
+ * 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 <libpayload-config.h>
+#if CONFIG(LP_LZMA)
+# include <lzma.h>
+# define CB_CBFS_CORE_WITH_LZMA
+#endif
+#if CONFIG(LP_LZ4)
+# include <lz4.h>
+# define CB_CBFS_CORE_WITH_LZ4
+#endif
+
+#include <cbfs_legacy.h>
+#include <stdio.h>
+
+#define DEBUG(x...)
+#define LOG(x...)
+#define ERROR(x...) printf(x)
+
+#ifndef __SMM__
+
+#include "cbfs_core.c"
+
+static inline int tohex4(unsigned int c)
+{
+ return (c <= 9) ? (c + '0') : (c - 10 + 'a');
+}
+
+static void tohex16(unsigned int val, char *dest)
+{
+ dest[0] = tohex4(val>>12);
+ dest[1] = tohex4((val>>8) & 0xf);
+ dest[2] = tohex4((val>>4) & 0xf);
+ dest[3] = tohex4(val & 0xf);
+}
+
+void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
+ uint16_t device)
+{
+ char name[17] = "pciXXXX,XXXX.rom";
+
+ tohex16(vendor, name+3);
+ tohex16(device, name+8);
+
+ return cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
+}
+
+void *cbfs_load_stage(struct cbfs_media *media, const char *name)
+{
+ struct cbfs_stage *stage = (struct cbfs_stage *)
+ cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
+ /* this is a mess. There is no ntohll. */
+ /* for now, assume compatible byte order until we solve this. */
+ uintptr_t entry;
+ uint32_t final_size;
+
+ if (stage == NULL)
+ return (void *) -1;
+
+ LOG("loading stage %s @ %p (%d bytes), entry @ 0x%llx\n", name,
+ (void *)(uintptr_t)stage->load, stage->memlen, stage->entry);
+
+ final_size = cbfs_decompress(stage->compression,
+ ((unsigned char *) stage) +
+ sizeof(struct cbfs_stage),
+ stage->len,
+ (void *) (uintptr_t) stage->load,
+ stage->memlen);
+ if (!final_size) {
+ entry = -1;
+ goto out;
+ }
+
+ memset((void *)((uintptr_t)stage->load + final_size), 0,
+ stage->memlen - final_size);
+
+ DEBUG("stage loaded.\n");
+
+ entry = stage->entry;
+ // entry = ntohll(stage->entry);
+
+out:
+ free(stage);
+ return (void *) entry;
+}
+
+int cbfs_execute_stage(struct cbfs_media *media, const char *name)
+{
+ struct cbfs_stage *stage = (struct cbfs_stage *)
+ cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
+
+ if (stage == NULL)
+ return 1;
+
+ if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
+ LOG("Unable to run %s: Compressed file"
+ "Not supported for in-place execution\n", name);
+ free(stage);
+ return 1;
+ }
+
+ LOG("run @ %p\n", (void *) (uintptr_t)ntohll(stage->entry));
+ int result = run_address((void *)(uintptr_t)ntohll(stage->entry));
+ free(stage);
+ return result;
+}
+
+void *cbfs_load_payload(struct cbfs_media *media, const char *name)
+{
+ return (struct cbfs_payload *)cbfs_get_file_content(
+ media, name, CBFS_TYPE_SELF, NULL);
+}
+
+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,
+ handle->content_offset + handle->content_size);
+ if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
+ free(handle);
+ return NULL;
+ }
+
+ free(handle);
+ return ret;
+}
+
+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)
+{
+ 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)
+{
+ 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) {
+ ERROR("simple_buffer: fail to read %zd bytes from 0x%zx\n",
+ count, offset);
+ return CBFS_MEDIA_INVALID_MAP_ADDRESS;
+ }
+ buffer->allocated += count;
+ buffer->last_allocate = count;
+ return address;
+}
+
+void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer, const void *address)
+{
+ // TODO Add simple buffer management so we can free more than last
+ // allocated one.
+ DEBUG("simple_buffer_unmap(address=%p): "
+ "allocated=%zu, size=%zu, last_allocate=%zu\n",
+ address, buffer->allocated, buffer->size,
+ buffer->last_allocate);
+ if ((buffer->buffer + buffer->allocated - buffer->last_allocate) ==
+ address) {
+ buffer->allocated -= buffer->last_allocate;
+ buffer->last_allocate = 0;
+ }
+ return NULL;
+}
+
+/**
+ * run_address is passed the address of a function taking no parameters and
+ * jumps to it, returning the result.
+ * @param f the address to call as a function.
+ * @return value returned by the function.
+ */
+
+int run_address(void *f)
+{
+ int (*v)(void);
+ v = f;
+ return v();
+}
+
+#endif /* __SMM__ */
diff --git a/payloads/libpayload/tests/Makefile.inc b/payloads/libpayload/tests/Makefile.inc
index 55c455cf40..e1c2a232d4 100644
--- a/payloads/libpayload/tests/Makefile.inc
+++ b/payloads/libpayload/tests/Makefile.inc
@@ -34,6 +34,7 @@ TEST_CFLAGS := -include include/kconfig.h -include include/compiler.h
TEST_CFLAGS += -Iinclude -Iinclude/mock
TEST_CFLAGS += -I$(coreboottop)/src/commonlib/bsd/include
TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
+TEST_CFLAGS += -I$(coreboottop)/3rdparty/vboot/firmware/include
# Test specific includes
TEST_CFLAGS += -I$(testsrc)/include
diff --git a/payloads/libpayload/tests/include/mocks/cbfs_util.h b/payloads/libpayload/tests/include/mocks/cbfs_util.h
new file mode 100644
index 0000000000..b0a1e79831
--- /dev/null
+++ b/payloads/libpayload/tests/include/mocks/cbfs_util.h
@@ -0,0 +1,114 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef MOCKS_CBFS_UTIL_H
+#define MOCKS_CBFS_UTIL_H
+
+#include <cbfs.h>
+#include <stddef.h>
+#include <tests/test.h>
+
+#define BE32(be32) EMPTY_WRAP(\
+ ((be32) >> 24) & 0xff, ((be32) >> 16) & 0xff, \
+ ((be32) >> 8) & 0xff, ((be32) >> 0) & 0xff)
+
+#define BE64(be64) EMPTY_WRAP( \
+ BE32(((be64) >> 32) & 0xFFFFFFFF), \
+ BE32(((be64) >> 0) & 0xFFFFFFFF))
+
+#define LE32(val32) EMPTY_WRAP(\
+ ((val32) >> 0) & 0xff, ((val32) >> 8) & 0xff, \
+ ((val32) >> 16) & 0xff, ((val32) >> 24) & 0xff)
+
+#define LE64(val64) EMPTY_WRAP( \
+ BE32(((val64) >> 0) & 0xFFFFFFFF), \
+ BE32(((val64) >> 32) & 0xFFFFFFFF))
+
+#define FILENAME_SIZE 16
+
+struct cbfs_test_file {
+ struct cbfs_file header;
+ u8 filename[FILENAME_SIZE];
+ u8 attrs_and_data[200];
+};
+
+#define TEST_MCACHE_SIZE (2 * MiB)
+
+#define HEADER_INITIALIZER(ftype, attr_len, file_len) { \
+ .magic = CBFS_FILE_MAGIC, \
+ .len = htobe32(file_len), \
+ .type = htobe32(ftype), \
+ .attributes_offset = \
+ htobe32(attr_len ? sizeof(struct cbfs_file) + FILENAME_SIZE : 0), \
+ .offset = htobe32(sizeof(struct cbfs_file) + FILENAME_SIZE + attr_len), \
+}
+
+#define HASH_ATTR_SIZE (offsetof(struct cbfs_file_attr_hash, hash.raw) + VB2_SHA256_DIGEST_SIZE)
+
+/* This macro basically does nothing but suppresses linter messages */
+#define EMPTY_WRAP(...) __VA_ARGS__
+
+#define TEST_DATA_1_FILENAME "test/data/1"
+#define TEST_DATA_1_SIZE sizeof((u8[]){TEST_DATA_1})
+#define TEST_DATA_1 EMPTY_WRAP( \
+ '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', \
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', \
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', \
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', \
+ '[', '\\', ']', '^', '_', '`', \
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', \
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
+
+#define TEST_DATA_2_FILENAME "test/data/2"
+#define TEST_DATA_2_SIZE sizeof((u8[]){TEST_DATA_2})
+#define TEST_DATA_2 EMPTY_WRAP( \
+ 0x9d, 0xa9, 0x91, 0xac, 0x5d, 0xb2, 0x70, 0x76, 0x37, 0x94, 0x94, 0xa8, 0x8b, 0x78, \
+ 0xb9, 0xaa, 0x1a, 0x8e, 0x9a, 0x16, 0xbe, 0xdc, 0x29, 0x42, 0x46, 0x58, 0xd4, 0x37, \
+ 0x94, 0xca, 0x05, 0xdb, 0x54, 0xfa, 0xd8, 0x6e, 0x54, 0xd8, 0x30, 0x46, 0x5d, 0x62, \
+ 0xc2, 0xce, 0xd8, 0x74, 0x60, 0xaf, 0x83, 0x8f, 0xfa, 0x97, 0xdd, 0x6e, 0xcb, 0x60, \
+ 0xfa, 0xed, 0x8b, 0x55, 0x9e, 0xc1, 0xc2, 0x18, 0x4f, 0xe2, 0x28, 0x7e, 0xd7, 0x2f, \
+ 0xa2, 0x86, 0xfb, 0x4d, 0x3e, 0x00, 0x5a, 0xf7, 0xc2, 0xad, 0x0e, 0xa7, 0xa2, 0xf7, \
+ 0x38, 0x66, 0xe6, 0x5c, 0x76, 0x98, 0x89, 0x63, 0xeb, 0xc5, 0xf5, 0xb7, 0xa7, 0x58, \
+ 0xe0, 0xf0, 0x2e, 0x2f, 0xb0, 0x95, 0xb7, 0x43, 0x28, 0x19, 0x2d, 0xef, 0x1a, 0xb3, \
+ 0x42, 0x31, 0x55, 0x0f, 0xbc, 0xcd, 0x01, 0xe5, 0x39, 0x18, 0x88, 0x83, 0xb2, 0xc5, \
+ 0x4b, 0x3b, 0x38, 0xe7)
+
+#define TEST_DATA_INT_1_FILENAME "test-int-1"
+#define TEST_DATA_INT_1_SIZE 8
+#define TEST_DATA_INT_1 0xFEDCBA9876543210ULL
+
+#define TEST_DATA_INT_2_FILENAME "test-int-2"
+#define TEST_DATA_INT_2_SIZE 8
+#define TEST_DATA_INT_2 0x10FE32DC54A97698ULL
+
+#define TEST_DATA_INT_3_FILENAME "test-int-3"
+#define TEST_DATA_INT_3_SIZE 8
+#define TEST_DATA_INT_3 0xFA57F003B0036667ULL
+
+#define TEST_SHA256 \
+ EMPTY_WRAP(0xef, 0xc7, 0xb1, 0x0a, 0xbf, 0x54, 0x2f, 0xaa, 0x12, 0xa6, 0xeb, 0xf, \
+ 0xff, 0xf4, 0x19, 0xc1, 0x63, 0xf4, 0x60, 0x50, 0xc5, 0xb0, 0xbe, 0x37, \
+ 0x32, 0x11, 0x19, 0x63, 0x61, 0xe0, 0x53, 0xe0)
+
+#define INVALID_SHA256 \
+ EMPTY_WRAP('T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'n', 'o', 't', ' ', 'a', ' ', 'v', \
+ 'a', 'l', 'i', 'd', ' ', 'S', 'H', 'A', '2', '5', '6', '!', '!', '!', '!', \
+ '!', '!')
+
+extern const u8 test_data_1[TEST_DATA_1_SIZE];
+extern const u8 test_data_2[TEST_DATA_2_SIZE];
+extern const u8 test_data_int_1[TEST_DATA_INT_1_SIZE];
+extern const u8 test_data_int_2[TEST_DATA_INT_2_SIZE];
+extern const u8 test_data_int_3[TEST_DATA_INT_3_SIZE];
+
+extern const u8 good_hash[VB2_SHA256_DIGEST_SIZE];
+extern const u8 bad_hash[VB2_SHA256_DIGEST_SIZE];
+
+extern const struct cbfs_test_file file_no_hash;
+extern const struct cbfs_test_file file_valid_hash;
+extern const struct cbfs_test_file file_broken_hash;
+extern const struct cbfs_test_file test_file_1;
+extern const struct cbfs_test_file test_file_2;
+extern const struct cbfs_test_file test_file_int_1;
+extern const struct cbfs_test_file test_file_int_2;
+extern const struct cbfs_test_file test_file_int_3;
+
+#endif /* MOCKS_CBFS_UTIL_H */
diff --git a/payloads/libpayload/tests/libcbfs/Makefile.inc b/payloads/libpayload/tests/libcbfs/Makefile.inc
new file mode 100644
index 0000000000..ad4efedcba
--- /dev/null
+++ b/payloads/libpayload/tests/libcbfs/Makefile.inc
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+tests-y += cbfs-lookup-no-fallback-test
+tests-y += cbfs-lookup-has-fallback-test
+tests-y += cbfs-verification-no-sha512-test
+tests-y += cbfs-verification-has-sha512-test
+tests-y += cbfs-no-verification-no-sha512-test
+tests-y += cbfs-no-verification-has-sha512-test
+
+
+cbfs-lookup-no-fallback-test-srcs += tests/libcbfs/cbfs-lookup-test.c
+cbfs-lookup-no-fallback-test-srcs += tests/mocks/cbfs_file_mock.c
+cbfs-lookup-no-fallback-test-config += CONFIG_LP_ENABLE_CBFS_FALLBACK=0
+cbfs-lookup-no-fallback-test-config += CONFIG_LP_LZ4=1
+cbfs-lookup-no-fallback-test-config += CONFIG_LP_LZMA=1
+
+$(call copy-test,cbfs-lookup-no-fallback-test,cbfs-lookup-has-fallback-test)
+cbfs-lookup-has-fallback-test-config += CONFIG_LP_ENABLE_CBFS_FALLBACK=1
+
+cbfs-verification-no-sha512-test-srcs += tests/libcbfs/cbfs-verification-test.c
+cbfs-verification-no-sha512-test-srcs += tests/mocks/cbfs_file_mock.c
+cbfs-verification-no-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=1
+cbfs-verification-no-sha512-test-config += VB2_SUPPORT_SHA512=0
+
+$(call copy-test,cbfs-verification-no-sha512-test,cbfs-verification-has-sha512-test)
+cbfs-verification-has-sha512-test-config += VB2_SUPPORT_SHA512=1
+
+$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-no-sha512-test)
+cbfs-verification-has-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=0
+
+$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-has-sha512-test)
+cbfs-verification-has-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=0
+cbfs-verification-has-sha512-test-config += VB2_SUPPORT_SHA512=1
diff --git a/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c
new file mode 100644
index 0000000000..0f7cdc7108
--- /dev/null
+++ b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c
@@ -0,0 +1,642 @@
+/* SPDX-License-Identifier: GPL-2.0.-only */
+
+#include <libpayload-config.h>
+#include <cbfs.h>
+#include <cbfs_glue.h>
+#include <commonlib/bsd/cb_err.h>
+#include <commonlib/bsd/cbfs_mdata.h>
+#include <endian.h>
+#include <mocks/cbfs_util.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysinfo.h>
+#include <tests/test.h>
+
+#include "../libcbfs/cbfs.c"
+
+/* Mocks */
+
+unsigned long virtual_offset = 0;
+struct sysinfo_t lib_sysinfo;
+
+unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst,
+ unsigned long dstn)
+{
+ assert_true(dstn != 0);
+ check_expected(srcn);
+ check_expected(dstn);
+ memcpy(dst, src, dstn);
+ return dstn;
+}
+
+size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
+{
+ assert_non_null(dstn);
+ check_expected(srcn);
+ check_expected(dstn);
+ memcpy(dst, src, dstn);
+ return dstn;
+}
+
+static size_t test_fmap_offset = 0;
+static size_t test_fmap_size = 0;
+static cb_err_t test_fmap_result = CB_SUCCESS;
+
+static void set_fmap_locate_area_results(size_t offset, size_t size, size_t result)
+{
+ test_fmap_offset = offset;
+ test_fmap_size = size;
+ test_fmap_result = result;
+}
+
+cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size)
+{
+ *offset = test_fmap_offset;
+ *size = test_fmap_size;
+ return test_fmap_result;
+}
+
+cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name,
+ union cbfs_mdata *mdata_out, size_t *data_offset_out)
+{
+ assert_non_null(mcache);
+ assert_true(mcache_size > 0 && mcache_size % CBFS_MCACHE_ALIGNMENT == 0);
+ assert_non_null(mdata_out);
+ assert_non_null(data_offset_out);
+
+ check_expected(name);
+
+ cb_err_t ret = mock_type(cb_err_t);
+ if (ret != CB_SUCCESS)
+ return ret;
+
+ memcpy(mdata_out, mock_ptr_type(const union cbfs_mdata *), sizeof(union cbfs_mdata));
+ *data_offset_out = mock_type(size_t);
+ return CB_SUCCESS;
+}
+
+static void expect_cbfs_mcache_lookup(const char *name, cb_err_t err,
+ const union cbfs_mdata *mdata, size_t data_offset_out)
+{
+ expect_string(cbfs_mcache_lookup, name, name);
+ will_return(cbfs_mcache_lookup, err);
+
+ if (err == CB_SUCCESS) {
+ will_return(cbfs_mcache_lookup, mdata);
+ will_return(cbfs_mcache_lookup, data_offset_out);
+ }
+}
+
+cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
+ size_t *data_offset_out, struct vb2_hash *metadata_hash)
+{
+ assert_non_null(dev);
+ check_expected(name);
+
+ cb_err_t ret = mock_type(cb_err_t);
+ if (ret != CB_SUCCESS)
+ return ret;
+
+ memcpy(mdata_out, mock_ptr_type(const union cbfS_mdata *), sizeof(union cbfs_mdata));
+ *data_offset_out = mock_type(size_t);
+ return CB_SUCCESS;
+}
+
+static void expect_cbfs_lookup(const char *name, cb_err_t err, const union cbfs_mdata *mdata,
+ size_t data_offset_out)
+{
+ expect_string(cbfs_lookup, name, name);
+ will_return(cbfs_lookup, err);
+
+ if (err == CB_SUCCESS) {
+ will_return(cbfs_lookup, mdata);
+ will_return(cbfs_lookup, data_offset_out);
+ }
+}
+
+const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check)
+{
+ return mock_ptr_type(void *);
+}
+
+static bool force_single_boot_device_size_failure = false;
+
+ssize_t boot_device_read(void *buf, size_t offset, size_t size)
+{
+ memcpy(buf, (void *)offset, size);
+ if (force_single_boot_device_size_failure) {
+ force_single_boot_device_size_failure = false;
+ return CB_ERR;
+ }
+ return size;
+}
+
+/* Utils */
+
+static size_t get_cbfs_file_size(const void *file_ptr)
+{
+ const struct cbfs_file *f = file_ptr;
+ return be32toh(f->offset) + be32toh(f->len);
+}
+
+static void create_cbfs(const struct cbfs_test_file *files[], const size_t nfiles,
+ uint8_t *buffer, const size_t buffer_size)
+{
+ uint8_t *data_ptr = buffer;
+ size_t file_size = 0;
+ memset(buffer, 0, buffer_size);
+ for (size_t i = 0; i < nfiles; ++i) {
+ if (files[i] == NULL) {
+ file_size = CBFS_ALIGNMENT;
+ assert_true(&data_ptr[file_size] < &buffer[buffer_size]);
+ } else {
+ file_size = get_cbfs_file_size(files[i]);
+ assert_true(&data_ptr[file_size] < &buffer[buffer_size]);
+ memcpy(data_ptr, files[i], file_size);
+ }
+ data_ptr = &data_ptr[file_size];
+ data_ptr = &buffer[ALIGN_UP((uintptr_t)data_ptr - (uintptr_t)buffer,
+ CBFS_ALIGNMENT)];
+ }
+}
+
+static size_t get_created_cbfs_file_start_offset(const struct cbfs_test_file *files[],
+ const size_t nfile)
+{
+ size_t offset_out = 0;
+ size_t offset = 0;
+ for (size_t i = 0; i < nfile; ++i) {
+ offset = files[i] ? get_cbfs_file_size(files[i]) : CBFS_ALIGNMENT;
+ offset_out = ALIGN_UP(offset_out + offset, CBFS_ALIGNMENT);
+ }
+ return offset_out;
+}
+
+/* Setup */
+
+static uint8_t
+ aligned_cbfs_ro_buffer[(sizeof(struct cbfs_test_file) + CBFS_ALIGNMENT * 50)] __aligned(
+ CBFS_ALIGNMENT);
+static const size_t aligned_cbfs_ro_buffer_size = sizeof(aligned_cbfs_ro_buffer);
+static uint8_t
+ aligned_cbfs_rw_buffer[(sizeof(struct cbfs_test_file) + CBFS_ALIGNMENT * 50)] __aligned(
+ CBFS_ALIGNMENT);
+static const size_t aligned_cbfs_rw_buffer_size = sizeof(aligned_cbfs_rw_buffer);
+
+static uint8_t *unaligned_cbfs_ro_buffer = &aligned_cbfs_ro_buffer[5];
+static const size_t unaligned_cbfs_ro_buffer_size = aligned_cbfs_ro_buffer_size - 5;
+static uint8_t *unaligned_cbfs_rw_buffer = &aligned_cbfs_rw_buffer[5];
+static const size_t unaligned_cbfs_rw_buffer_size = aligned_cbfs_rw_buffer_size - 5;
+
+struct cbfs_test_state {
+ uint8_t *cbfs_ro_buf;
+ uint64_t cbfs_ro_size;
+ uint8_t *cbfs_rw_buf;
+ uint64_t cbfs_rw_size;
+
+ size_t mcache_ro_offset;
+ size_t mcache_ro_size;
+ size_t mcache_rw_offset;
+ size_t mcache_rw_size;
+
+ struct cbfs_test_setup {
+ bool unaligned;
+ bool init_ro;
+ bool init_rw;
+ } ex;
+};
+
+
+/* Because of how CMocka works, it should be called in the test function, or in the setup
+ function only if CBFS API capable of initializing RO CBFS boot device is called. */
+static void setup_cbfs_boot_device(struct cbfs_test_state *s)
+{
+ set_fmap_locate_area_results(0, 0, CB_SUCCESS);
+ lib_sysinfo.cbfs_ro_mcache_offset = 0;
+ lib_sysinfo.cbfs_ro_mcache_size = 0;
+ memset((void *)cbfs_get_boot_device(true), 0, sizeof(struct cbfs_boot_device));
+ if (s->ex.init_ro) {
+ set_fmap_locate_area_results((size_t)s->cbfs_ro_buf, s->cbfs_ro_size,
+ CB_SUCCESS);
+ lib_sysinfo.cbfs_ro_mcache_offset = s->mcache_ro_offset;
+ lib_sysinfo.cbfs_ro_mcache_size = s->mcache_ro_size;
+ }
+
+ lib_sysinfo.cbfs_offset = 0;
+ lib_sysinfo.cbfs_size = 0;
+ lib_sysinfo.cbfs_rw_mcache_offset = 0;
+ lib_sysinfo.cbfs_rw_mcache_size = 0;
+ memset((void *)cbfs_get_boot_device(false), 0, sizeof(struct cbfs_boot_device));
+ if (s->ex.init_rw) {
+ lib_sysinfo.cbfs_offset = (uint64_t)s->cbfs_rw_buf;
+ lib_sysinfo.cbfs_size = s->cbfs_rw_size;
+ lib_sysinfo.cbfs_rw_mcache_offset = s->mcache_rw_offset;
+ lib_sysinfo.cbfs_rw_mcache_size = s->mcache_rw_size;
+ }
+}
+
+static int setup_cbfs_test(void **state)
+{
+ struct cbfs_test_state *s = calloc(1, sizeof(*s));
+
+ if (!s)
+ return 1;
+
+ if (*state)
+ memcpy(&s->ex, *state, sizeof(s->ex));
+
+ if (s->ex.init_ro) {
+ if (s->ex.unaligned) {
+ s->cbfs_ro_buf = unaligned_cbfs_ro_buffer;
+ s->cbfs_ro_size = unaligned_cbfs_ro_buffer_size;
+ } else {
+ s->cbfs_ro_buf = aligned_cbfs_ro_buffer;
+ s->cbfs_ro_size = aligned_cbfs_ro_buffer_size;
+ }
+ }
+
+ if (s->ex.init_rw) {
+ if (s->ex.unaligned) {
+ s->cbfs_rw_buf = unaligned_cbfs_rw_buffer;
+ s->cbfs_rw_size = unaligned_cbfs_rw_buffer_size;
+ } else {
+ s->cbfs_rw_buf = aligned_cbfs_rw_buffer;
+ s->cbfs_rw_size = aligned_cbfs_rw_buffer_size;
+ }
+ }
+
+ *state = s;
+
+ return 0;
+}
+
+static int teardown_cbfs_test(void **state)
+{
+ if (*state)
+ free(*state);
+
+ return 0;
+}
+
+/* Tests */
+
+static void test_cbfs_boot_device_init(void **state)
+{
+ const struct cbfs_boot_device *cbd = NULL;
+
+ /* No valid RO, should fail */
+ set_fmap_locate_area_results(0, 0, CB_ERR);
+ lib_sysinfo.cbfs_offset = 0;
+ lib_sysinfo.cbfs_size = 0;
+ lib_sysinfo.cbfs_rw_mcache_size = 0;
+ lib_sysinfo.cbfs_rw_mcache_offset = 0;
+ lib_sysinfo.cbfs_ro_mcache_offset = 0;
+ lib_sysinfo.cbfs_ro_mcache_size = 0;
+ assert_int_equal(NULL, cbfs_get_boot_device(true));
+ assert_null(cbfs_ro_map("file", NULL));
+
+ /* Valid RO */
+ set_fmap_locate_area_results(0x12345678, 0x90ABCDEF, CB_SUCCESS);
+ lib_sysinfo.cbfs_ro_mcache_offset = 0x600D41C3;
+ lib_sysinfo.cbfs_ro_mcache_size = 0xBADBEEFF;
+ cbd = cbfs_get_boot_device(true);
+ assert_non_null(cbd);
+ assert_int_equal(0x12345678, cbd->dev.offset);
+ assert_int_equal(0x90ABCDEF, cbd->dev.size);
+ assert_int_equal(0xBADBEEFF, cbd->mcache_size);
+ assert_int_equal(0x600D41C3, cbd->mcache);
+
+ lib_sysinfo.cbfs_offset = 0xAABBCCDD;
+ lib_sysinfo.cbfs_size = 0x1000;
+ lib_sysinfo.cbfs_rw_mcache_offset = 0x8F8F8F8F;
+ lib_sysinfo.cbfs_rw_mcache_size = 0x500;
+ cbd = cbfs_get_boot_device(false);
+ assert_non_null(cbd);
+ assert_int_equal(0xAABBCCDD, cbd->dev.offset);
+ assert_int_equal(0x1000, cbd->dev.size);
+ assert_int_equal(0x8F8F8F8F, cbd->mcache);
+ assert_int_equal(0x500, cbd->mcache_size);
+}
+
+/* This test checks cbfs_map() basic cases and covers only RW CBFS. */
+void test_cbfs_map(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ void *mapping = NULL;
+ size_t size_out = 0;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_int_1, &test_file_2, NULL, &test_file_int_3,
+ &test_file_int_2, NULL, NULL, &test_file_1,
+ };
+ uint8_t *cbfs_buf = NULL;
+ size_t foffset = 0;
+
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_rw_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 0);
+ expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ mapping = cbfs_map(TEST_DATA_INT_1_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_INT_1_SIZE, size_out);
+ assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 1);
+ expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_2.header.offset));
+ will_return(cbfs_find_attr, &test_file_2.attrs_and_data);
+ expect_value(ulzman, srcn, TEST_DATA_2_SIZE);
+ expect_value(ulzman, dstn, TEST_DATA_2_SIZE);
+ mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_2_SIZE, size_out);
+ assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 3);
+ expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_3.header.offset));
+ will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data);
+ expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE);
+ expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE);
+ mapping = cbfs_map(TEST_DATA_INT_3_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_INT_3_SIZE, size_out);
+ assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 4);
+ expect_cbfs_lookup(TEST_DATA_INT_2_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_2.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ mapping = cbfs_map(TEST_DATA_INT_2_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_INT_2_SIZE, size_out);
+ assert_memory_equal(test_data_int_2, mapping, TEST_DATA_INT_2_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 7);
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_1_SIZE, size_out);
+ assert_memory_equal(test_data_1, mapping, TEST_DATA_1_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0);
+ if (s->ex.init_rw && CONFIG(LP_ENABLE_CBFS_FALLBACK))
+ expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0);
+ mapping = cbfs_map("invalid_file", &size_out);
+ assert_null(mapping);
+}
+
+static void test_cbfs_invalid_compression_algo(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ void *mapping = NULL;
+ size_t size_out = 0;
+ uint8_t *cbfs_buf = NULL;
+ struct cbfs_test_file *f;
+ struct cbfs_file_attr_compression *comp;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_2,
+ };
+
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_rw_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
+
+ f = (struct cbfs_test_file *)cbfs_buf;
+ comp = (struct cbfs_file_attr_compression *)&f->attrs_and_data[0];
+ comp->compression = 0xFFFFFFF0;
+
+ size_out = 0;
+ expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS, (const union cbfs_mdata *)cbfs_buf,
+ be32toh(test_file_1.header.offset));
+ will_return(cbfs_find_attr, comp);
+ mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out);
+ assert_null(mapping);
+}
+
+static void test_cbfs_io_error(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ setup_cbfs_boot_device(s);
+
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_CBFS_IO, 0, 0);
+ assert_null(cbfs_map(TEST_DATA_1_FILENAME, NULL));
+}
+
+static void test_cbfs_successful_fallback_to_ro(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ void *mapping = NULL;
+ size_t size_out = 0;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_1, &test_file_2, &test_file_int_1,
+ &test_file_int_1, &test_file_int_2, &test_file_int_3,
+ };
+ uint8_t *cbfs_buf = NULL;
+ size_t foffset = 0;
+
+ if (!CONFIG(LP_ENABLE_CBFS_FALLBACK)) {
+ print_message("Skipping test, because LP_ENABLE_CBFS_FALLBACK == 0\n");
+ skip();
+ }
+
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_ro_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_ro_buf, s->cbfs_ro_size);
+ if (s->ex.init_rw)
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files) - 2, s->cbfs_rw_buf,
+ s->cbfs_rw_size);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 1);
+ expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_CBFS_NOT_FOUND, 0, 0);
+ expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_2.header.offset));
+ will_return(cbfs_find_attr, &test_file_2.attrs_and_data);
+ expect_value(ulzman, srcn, TEST_DATA_2_SIZE);
+ expect_value(ulzman, dstn, TEST_DATA_2_SIZE);
+ mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_2_SIZE, size_out);
+ assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE);
+ cbfs_unmap(mapping);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 5);
+ expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_CBFS_NOT_FOUND, 0, 0);
+ expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_3.header.offset));
+ will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data);
+ expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE);
+ expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE);
+ mapping = cbfs_map(TEST_DATA_INT_3_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_INT_3_SIZE, size_out);
+ assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE);
+ cbfs_unmap(mapping);
+}
+
+static void test_cbfs_load(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ size_t size_out = 0;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_int_1, &test_file_2, NULL, &test_file_int_3,
+ &test_file_int_2, NULL, NULL, &test_file_1,
+ };
+ uint8_t *cbfs_buf = NULL;
+ uint8_t load_buf[1 * KiB];
+ size_t foffset = 0;
+
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_rw_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
+
+ /* Successful load */
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 0);
+ expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ size_out = cbfs_load(TEST_DATA_INT_1_FILENAME, load_buf, sizeof(load_buf));
+ assert_int_equal(TEST_DATA_INT_1_SIZE, size_out);
+ assert_memory_equal(test_data_int_1, load_buf, TEST_DATA_INT_1_SIZE);
+
+ /* Buffer too small */
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 7);
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ size_out = cbfs_load(TEST_DATA_1_FILENAME, load_buf, TEST_DATA_1_SIZE / 2);
+ assert_int_equal(0, size_out);
+}
+
+static void test_cbfs_map_with_mcache(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ void *mapping = NULL;
+ size_t size_out = 0;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_int_2, &test_file_1, NULL,
+ &test_file_int_3, &test_file_int_1, &test_file_2,
+ };
+ uint8_t *cbfs_buf = NULL;
+ size_t foffset = 0;
+
+ /* Will not be accessed, just needs to be valid. */
+ s->mcache_ro_offset = ALIGN_UP(0x1000, CBFS_MCACHE_ALIGNMENT);
+ s->mcache_ro_size = ALIGN_UP(0x500, CBFS_MCACHE_ALIGNMENT);
+ s->mcache_rw_offset = ALIGN_UP(0x3000, CBFS_MCACHE_ALIGNMENT);
+ s->mcache_rw_size = ALIGN_UP(0x600, CBFS_MCACHE_ALIGNMENT);
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_rw_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 4);
+ expect_cbfs_mcache_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_int_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ mapping = cbfs_map(TEST_DATA_INT_1_FILENAME, &size_out);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_INT_1_SIZE, size_out);
+ assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE);
+ cbfs_unmap(mapping);
+}
+
+static void test_cbfs_boot_device_read_failure(void **state)
+{
+ struct cbfs_test_state *s = *state;
+ void *mapping = NULL;
+ size_t size_out = 0;
+ const struct cbfs_test_file *cbfs_files[] = {
+ &test_file_int_3, &test_file_1, NULL,
+ &test_file_int_3, &test_file_int_1, &test_file_2,
+ };
+ uint8_t *cbfs_buf = NULL;
+ size_t foffset = 0;
+
+ setup_cbfs_boot_device(s);
+ cbfs_buf = s->cbfs_rw_buf;
+ create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
+
+ size_out = 0;
+ foffset = get_created_cbfs_file_start_offset(cbfs_files, 1);
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&cbfs_buf[foffset],
+ foffset + be32toh(test_file_1.header.offset));
+ will_return(cbfs_find_attr, NULL);
+ force_single_boot_device_size_failure = true;
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size_out);
+ assert_null(mapping);
+}
+
+
+#define TEST_CBFS_NAME_ALIGN_RO_RW(fn, test_name, enable_unaligned, enable_init_ro, \
+ enable_init_rw) \
+ ((struct CMUnitTest){ \
+ .name = (test_name), \
+ .test_func = (fn), \
+ .setup_func = setup_cbfs_test, \
+ .teardown_func = teardown_cbfs_test, \
+ .initial_state = \
+ &(struct cbfs_test_setup){ \
+ .unaligned = enable_unaligned, \
+ .init_ro = enable_init_ro, \
+ .init_rw = enable_init_rw, \
+ }, \
+ })
+
+#define TEST_CBFS_LOOKUP(fn) \
+ EMPTY_WRAP(TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW, aligned", false, false, true), \
+ TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW, unaligned", true, false, true))
+
+#define TEST_CBFS_RO_FALLBACK(fn) \
+ EMPTY_WRAP(TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW+RO, aligned", false, true, true), \
+ TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW+RO, unaligned", true, true, true), \
+ TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RO, aligned", false, true, false), \
+ TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RO, unaligned", true, true, false))
+
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_cbfs_boot_device_init),
+ TEST_CBFS_LOOKUP(test_cbfs_map),
+ TEST_CBFS_LOOKUP(test_cbfs_invalid_compression_algo),
+ TEST_CBFS_LOOKUP(test_cbfs_io_error),
+ TEST_CBFS_RO_FALLBACK(test_cbfs_successful_fallback_to_ro),
+ TEST_CBFS_LOOKUP(test_cbfs_load),
+ TEST_CBFS_LOOKUP(test_cbfs_map_with_mcache),
+ TEST_CBFS_LOOKUP(test_cbfs_boot_device_read_failure),
+ };
+
+ return lp_run_group_tests(tests, NULL, NULL);
+}
diff --git a/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c b/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c
new file mode 100644
index 0000000000..2ab3d5302d
--- /dev/null
+++ b/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c
@@ -0,0 +1,247 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <cbfs.h>
+#include <cbfs_glue.h>
+#include <string.h>
+#include <mocks/cbfs_util.h>
+#include <tests/test.h>
+
+#include "../libcbfs/cbfs.c"
+
+/* Mocks */
+
+unsigned long virtual_offset = 0;
+struct sysinfo_t lib_sysinfo;
+
+size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg)
+{
+ if (hash_alg != VB2_HASH_SHA256) {
+ fail_msg("Unsupported hash algorithm: %d\n", hash_alg);
+ return 0;
+ }
+
+ return VB2_SHA256_DIGEST_SIZE;
+}
+
+vb2_error_t vb2_hash_verify(const void *buf, uint32_t size, const struct vb2_hash *hash)
+{
+ check_expected_ptr(buf);
+ check_expected(size);
+
+ assert_int_equal(hash->algo, VB2_HASH_SHA256);
+
+ if (!memcmp(hash->sha256, good_hash, sizeof(good_hash)))
+ return VB2_SUCCESS;
+
+ if (!memcmp(hash->sha256, bad_hash, sizeof(bad_hash)))
+ return VB2_ERROR_SHA_MISMATCH;
+
+ fail_msg("%s called with bad hash", __func__);
+ return VB2_ERROR_SHA_MISMATCH;
+}
+
+unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst,
+ unsigned long dstn)
+{
+ fail_msg("Unexpected call to %s", __func__);
+ return 0;
+}
+
+size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
+{
+ fail_msg("Unexpected call to %s", __func__);
+ return 0;
+}
+
+cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name,
+ union cbfs_mdata *mdata_out, size_t *data_offset_out)
+{
+ return CB_CBFS_CACHE_FULL;
+}
+
+cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
+ size_t *data_offset_out, struct vb2_hash *metadata_hash)
+{
+ assert_non_null(dev);
+ check_expected(name);
+
+ cb_err_t ret = mock_type(cb_err_t);
+ if (ret != CB_SUCCESS)
+ return ret;
+
+ memcpy(mdata_out, mock_ptr_type(const union cbfs_mdata *), sizeof(union cbfs_mdata));
+ *data_offset_out = mock_type(size_t);
+ return CB_SUCCESS;
+}
+
+static void expect_cbfs_lookup(const char *name, cb_err_t err, const union cbfs_mdata *mdata,
+ size_t data_offset_out)
+{
+ expect_string(cbfs_lookup, name, name);
+ will_return(cbfs_lookup, err);
+
+ if (err == CB_SUCCESS) {
+ will_return(cbfs_lookup, mdata);
+ will_return(cbfs_lookup, data_offset_out);
+ }
+}
+
+const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check)
+{
+ return mock_ptr_type(void *);
+}
+
+cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size)
+{
+ *offset = 0;
+ *size = 0;
+ return CB_SUCCESS;
+}
+
+ssize_t boot_device_read(void *buf, size_t offset, size_t size)
+{
+ /* Offset should be based on an address from lib_sysinfo.cbfs_offset */
+ memcpy(buf, (void *)offset, size);
+
+ return size;
+}
+
+const struct vb2_hash *cbfs_file_hash(const union cbfs_mdata *mdata)
+{
+ return mock_ptr_type(const struct vb2_hash *);
+}
+
+/* Utils */
+
+static void clear_cbfs_boot_devices(void)
+{
+ lib_sysinfo.cbfs_ro_mcache_offset = 0;
+ lib_sysinfo.cbfs_ro_mcache_size = 0;
+ lib_sysinfo.cbfs_offset = 0;
+ lib_sysinfo.cbfs_size = 0;
+ lib_sysinfo.cbfs_rw_mcache_offset = 0;
+ lib_sysinfo.cbfs_rw_mcache_size = 0;
+ memset((void *)cbfs_get_boot_device(true), 0, sizeof(struct cbfs_boot_device));
+ memset((void *)cbfs_get_boot_device(false), 0, sizeof(struct cbfs_boot_device));
+}
+
+void set_cbfs(uint64_t offset, size_t size)
+{
+ clear_cbfs_boot_devices();
+ lib_sysinfo.cbfs_offset = offset;
+ lib_sysinfo.cbfs_size = size;
+}
+
+/* Tests */
+
+static int setup_test_cbfs(void **state)
+{
+ clear_cbfs_boot_devices();
+ return 0;
+}
+
+static void test_cbfs_map_no_hash(void **state)
+{
+ void *mapping = NULL;
+ size_t size = 0;
+
+ set_cbfs((uint64_t)&file_no_hash, sizeof(file_no_hash));
+
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&file_no_hash,
+ be32toh(file_no_hash.header.offset));
+ will_return(cbfs_find_attr, NULL);
+
+ if (CONFIG(LP_CBFS_VERIFICATION)) {
+ /* File with no hash. No hash causes hash mismatch by default,
+ so mapping will not be completed successfully. */
+ will_return(cbfs_file_hash, NULL);
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
+ assert_null(mapping);
+ } else {
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_1_SIZE, size);
+ assert_memory_equal(test_data_1, mapping, size);
+ cbfs_unmap(mapping);
+ }
+}
+
+static void test_cbfs_map_valid_hash(void **state)
+{
+ void *mapping = NULL;
+ size_t size = 0;
+ struct vb2_hash hash = {
+ .algo = VB2_HASH_SHA256,
+ };
+ memcpy(&hash.sha256, good_hash, VB2_SHA256_DIGEST_SIZE);
+
+ set_cbfs((uint64_t)&file_valid_hash, sizeof(file_valid_hash));
+
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&file_valid_hash,
+ be32toh(file_valid_hash.header.offset));
+ will_return(cbfs_find_attr, NULL);
+
+
+ if (CONFIG(LP_CBFS_VERIFICATION)) {
+ will_return(cbfs_file_hash, &hash);
+ expect_memory(vb2_hash_verify, buf,
+ &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE], HASH_ATTR_SIZE);
+ expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_1_SIZE, size);
+ assert_memory_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE],
+ size);
+ } else {
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_1_SIZE, size);
+ assert_memory_equal(test_data_1, mapping, size);
+ cbfs_unmap(mapping);
+ }
+}
+
+static void test_cbfs_map_invalid_hash(void **state)
+{
+ void *mapping = NULL;
+ size_t size = 0;
+ struct vb2_hash hash = {
+ .algo = VB2_HASH_SHA256,
+ };
+ memcpy(&hash.sha256, bad_hash, VB2_SHA256_DIGEST_SIZE);
+
+ set_cbfs((uint64_t)&file_broken_hash, sizeof(file_broken_hash));
+
+ expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
+ (const union cbfs_mdata *)&file_broken_hash,
+ be32toh(file_broken_hash.header.offset));
+ will_return(cbfs_find_attr, NULL);
+
+ if (CONFIG(LP_CBFS_VERIFICATION)) {
+ will_return(cbfs_file_hash, &hash);
+ expect_memory(vb2_hash_verify, buf,
+ &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE], HASH_ATTR_SIZE);
+ expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
+ assert_null(mapping);
+ } else {
+ mapping = cbfs_map(TEST_DATA_1_FILENAME, &size);
+ assert_non_null(mapping);
+ assert_int_equal(TEST_DATA_1_SIZE, size);
+ assert_memory_equal(test_data_1, mapping, size);
+ cbfs_unmap(mapping);
+ }
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs),
+ cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs),
+ cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs),
+ };
+
+ return lp_run_group_tests(tests, NULL, NULL);
+}
diff --git a/payloads/libpayload/tests/mocks/cbfs_file_mock.c b/payloads/libpayload/tests/mocks/cbfs_file_mock.c
new file mode 100644
index 0000000000..3ad078c7b0
--- /dev/null
+++ b/payloads/libpayload/tests/mocks/cbfs_file_mock.c
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <mocks/cbfs_util.h>
+
+
+const u8 test_data_1[TEST_DATA_1_SIZE] = { TEST_DATA_1 };
+const u8 test_data_2[TEST_DATA_2_SIZE] = { TEST_DATA_2 };
+const u8 test_data_int_1[TEST_DATA_INT_1_SIZE] = { LE64(TEST_DATA_INT_1) };
+const u8 test_data_int_2[TEST_DATA_INT_2_SIZE] = { LE64(TEST_DATA_INT_2) };
+const u8 test_data_int_3[TEST_DATA_INT_3_SIZE] = { LE64(TEST_DATA_INT_3) };
+
+const u8 good_hash[VB2_SHA256_DIGEST_SIZE] = { TEST_SHA256 };
+const u8 bad_hash[VB2_SHA256_DIGEST_SIZE] = { INVALID_SHA256 };
+
+const struct cbfs_test_file file_no_hash = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_1_SIZE),
+ .filename = TEST_DATA_1_FILENAME,
+ .attrs_and_data = {
+ TEST_DATA_1,
+ },
+};
+
+const struct cbfs_test_file file_valid_hash = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE),
+ .filename = TEST_DATA_1_FILENAME,
+ .attrs_and_data = {
+ BE32(CBFS_FILE_ATTR_TAG_HASH),
+ BE32(HASH_ATTR_SIZE),
+ BE32(VB2_HASH_SHA256),
+ TEST_SHA256,
+ TEST_DATA_1,
+ },
+};
+
+const struct cbfs_test_file file_broken_hash = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE),
+ .filename = TEST_DATA_1_FILENAME,
+ .attrs_and_data = {
+ BE32(CBFS_FILE_ATTR_TAG_HASH),
+ BE32(HASH_ATTR_SIZE),
+ BE32(VB2_HASH_SHA256),
+ INVALID_SHA256,
+ TEST_DATA_1,
+ },
+};
+
+const struct cbfs_test_file test_file_1 = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_1_SIZE),
+ .filename = TEST_DATA_1_FILENAME,
+ .attrs_and_data = {
+ TEST_DATA_1,
+ },
+};
+
+const struct cbfs_test_file test_file_2 = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, sizeof(struct cbfs_file_attr_compression),
+ TEST_DATA_2_SIZE),
+ .filename = TEST_DATA_2_FILENAME,
+ .attrs_and_data = {
+ BE32(CBFS_FILE_ATTR_TAG_COMPRESSION),
+ BE32(sizeof(struct cbfs_file_attr_compression)),
+ BE32(CBFS_COMPRESS_LZMA),
+ BE32(TEST_DATA_2_SIZE),
+ TEST_DATA_2,
+ },
+};
+
+const struct cbfs_test_file test_file_int_1 = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_INT_1_SIZE),
+ .filename = TEST_DATA_INT_1_FILENAME,
+ .attrs_and_data = {
+ LE64(TEST_DATA_INT_1),
+ },
+};
+
+const struct cbfs_test_file test_file_int_2 = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_INT_2_SIZE),
+ .filename = TEST_DATA_INT_2_FILENAME,
+ .attrs_and_data = {
+ LE64(TEST_DATA_INT_2),
+ },
+};
+
+const struct cbfs_test_file test_file_int_3 = {
+ .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, sizeof(struct cbfs_file_attr_compression),
+ TEST_DATA_INT_3_SIZE),
+ .filename = TEST_DATA_INT_3_FILENAME,
+ .attrs_and_data = {
+ BE32(CBFS_FILE_ATTR_TAG_COMPRESSION),
+ BE32(sizeof(struct cbfs_file_attr_compression)),
+ BE32(CBFS_COMPRESS_LZ4),
+ BE32(TEST_DATA_INT_3_SIZE),
+ LE64(TEST_DATA_INT_3),
+ },
+};
diff --git a/payloads/libpayload/tests/mocks/die.c b/payloads/libpayload/tests/mocks/die.c
new file mode 100644
index 0000000000..a67105a12b
--- /dev/null
+++ b/payloads/libpayload/tests/mocks/die.c
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <tests/test.h>
+#include <stdbool.h>
+
+void die_work(const char *file, const char *func, int line, const char *fmt, ...)
+{
+ /* Failing asserts are jumping to the user code (test) if expect_assert_failed() was
+ previously called. Otherwise it jumps to the cmocka code and fails the test. */
+ mock_assert(false, "Mock assetion called", file, line);
+
+ /* Should never be reached */
+ print_error("%s() called...\n", __func__);
+ while (1)
+ ;
+}