Merge 4.16
Change-Id: I11db70a8e25a6656c5ec640a703e7b06d5a3672e
This commit is contained in:
@@ -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"
|
||||
@@ -227,6 +223,9 @@ config LZ4
|
||||
help
|
||||
Decoder implementation for the LZ4 compression algorithm.
|
||||
Adds standalone functions (CBFS support coming soon).
|
||||
|
||||
source "vboot/Kconfig"
|
||||
|
||||
endmenu
|
||||
|
||||
menu "Console Options"
|
||||
|
@@ -29,16 +29,23 @@
|
||||
## SUCH DAMAGE.
|
||||
##
|
||||
|
||||
ifneq ($(words $(CURDIR)),1)
|
||||
$(error ERROR: Path to the main directory cannot contain spaces)
|
||||
endif
|
||||
|
||||
ifeq ($(INNER_SCANBUILD),y)
|
||||
CC_real:=$(CC)
|
||||
endif
|
||||
|
||||
export top := $(CURDIR)
|
||||
export coreboottop ?= $(abspath $(top)/../../)
|
||||
export src := src
|
||||
export srck := $(abspath $(top)/../../util/kconfig)
|
||||
export obj ?= build
|
||||
export objutil ?= $(obj)/util
|
||||
export objk := $(objutil)/lp_kconfig
|
||||
export absobj := $(abspath $(obj))
|
||||
VBOOT_SOURCE ?= $(coreboottop)/3rdparty/vboot
|
||||
|
||||
export KCONFIG_AUTOHEADER := $(obj)/config.h
|
||||
export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
|
||||
@@ -289,9 +296,11 @@ includemakefiles= \
|
||||
$(foreach item,$($(special)-y), $(call $(special)-handler,$(dir $(1)),$(item)))) \
|
||||
$(foreach class,$(classes), \
|
||||
$(eval $(class)-srcs+= \
|
||||
$$(subst $(absobj)/,$(obj)/, \
|
||||
$$(subst $(top)/,, \
|
||||
$$(abspath $$(addprefix $(dir $(1)),$$($(class)-y)))))) \
|
||||
$(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
|
||||
$$(abspath $$(subst $(dir $(1))/,/,$$(addprefix $(dir $(1)),$$($(class)-y)))))))) \
|
||||
$(eval subdirs+=$$(subst $(CURDIR)/,,$$(wildcard $$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y))))))
|
||||
|
||||
|
||||
# For each path in $(subdirs) call includemakefiles
|
||||
# Repeat until subdirs is empty
|
||||
@@ -310,8 +319,15 @@ else
|
||||
include $(TOPLEVEL)/tests/Makefile.inc
|
||||
endif
|
||||
|
||||
src-to-obj=$(addsuffix .$(1).o, $(basename $(addprefix $(obj)/, $($(1)-srcs))))
|
||||
$(foreach class,$(classes),$(eval $(class)-objs:=$(call src-to-obj,$(class))))
|
||||
# Converts one or more source file paths to the corresponding build/ paths.
|
||||
# $1 lib name
|
||||
# $2 file path (list)
|
||||
src-to-obj=\
|
||||
$(addsuffix .$(1).o,\
|
||||
$(basename \
|
||||
$(addprefix $(obj)/,\
|
||||
$(subst $(coreboottop)/,coreboot/,$(2)))))
|
||||
$(foreach class,$(classes),$(eval $(class)-objs+=$(call src-to-obj,$(class),$($(class)-srcs))))
|
||||
|
||||
allsrcs:=$(foreach var, $(addsuffix -srcs,$(classes)), $($(var)))
|
||||
allobjs:=$(foreach var, $(addsuffix -objs,$(classes)), $($(var)))
|
||||
@@ -325,7 +341,7 @@ define create_cc_template
|
||||
# $4 additional dependencies
|
||||
ifn$(EMPTY)def $(1)-objs_$(2)_template
|
||||
de$(EMPTY)fine $(1)-objs_$(2)_template
|
||||
$(obj)/$$(1).$(1).o: $$(1).$(2) $(obj)/libpayload-config.h $(4)
|
||||
$$(call src-to-obj,$(1), $$(1).$(2)): $$(1).$(2) $(obj)/libpayload-config.h $(4)
|
||||
@printf " CC $$$$(subst $$$$(obj)/,,$$$$(@))\n"
|
||||
$(CC) $(3) -MMD $$$$(CFLAGS) $(EXTRA_CFLAGS) -c -o $$$$@ $$$$<
|
||||
en$(EMPTY)def
|
||||
@@ -340,7 +356,7 @@ $(foreach class,$(classes), \
|
||||
foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(basename $(file)))))
|
||||
$(eval $(foreach class,$(classes),$(call foreach-src,$(class))))
|
||||
|
||||
DEPENDENCIES = $(allobjs:.o=.d)
|
||||
DEPENDENCIES = $($(filter %.o,%(allobjs)):.o=.d)
|
||||
-include $(DEPENDENCIES)
|
||||
|
||||
printall:
|
||||
|
@@ -46,6 +46,8 @@ classes-$(CONFIG_LP_CBFS) += libcbfs
|
||||
classes-$(CONFIG_LP_LZMA) += liblzma
|
||||
classes-$(CONFIG_LP_LZ4) += liblz4
|
||||
classes-$(CONFIG_LP_REMOTEGDB) += libgdb
|
||||
classes-$(CONFIG_LP_VBOOT_LIB) += vboot_fw
|
||||
classes-$(CONFIG_LP_VBOOT_LIB) += tlcl
|
||||
libraries := $(classes-y)
|
||||
classes-y += head.o
|
||||
|
||||
@@ -55,9 +57,12 @@ subdirs-$(CONFIG_LP_CURSES) += curses
|
||||
subdirs-$(CONFIG_LP_CBFS) += libcbfs
|
||||
subdirs-$(CONFIG_LP_LZMA) += liblzma
|
||||
subdirs-$(CONFIG_LP_LZ4) += liblz4
|
||||
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$(VBOOT_SOURCE)/firmware/include
|
||||
|
||||
CFLAGS += $(INCLUDES) -Os -pipe -nostdinc -ggdb3
|
||||
CFLAGS += -nostdlib -fno-builtin -ffreestanding -fomit-frame-pointer
|
||||
@@ -90,11 +95,11 @@ includes-handler= \
|
||||
|
||||
$(obj)/libpayload.a: $(foreach class,$(libraries),$$($(class)-objs))
|
||||
printf " AR $(subst $(CURDIR)/,,$(@))\n"
|
||||
$(AR) rc $@ $^
|
||||
printf "create $@\n$(foreach objc,$(filter-out %.a,$^),addmod $(objc)\n)$(foreach lib,$(filter %.a,$^),addlib $(lib)\n)save\nend\n" | $(AR) -M
|
||||
|
||||
$(obj)/%.a: $$(%-objs)
|
||||
printf " AR $(subst $(CURDIR)/,,$(@))\n"
|
||||
$(AR) rc $@ $^
|
||||
printf "create $@\n$(foreach objc,$(filter-out %.a,$^),addmod $(objc)\n)$(foreach lib,$(filter %.a,$^),addlib $(lib)\n)save\nend\n" | $(AR) -M
|
||||
|
||||
$(obj)/head.o: $(obj)/arch/$(ARCHDIR-y)/head.head.o.o
|
||||
printf " CP $(subst $(CURDIR)/,,$(@))\n"
|
||||
@@ -115,10 +120,24 @@ install: real-target
|
||||
install -m 755 -d $(DESTDIR)/libpayload/`dirname $$file`; \
|
||||
install -m 644 $$file $(DESTDIR)/libpayload/$$file; \
|
||||
done
|
||||
for file in `find $(coreboottop)/src/commonlib/bsd/include -name *.h -type f`; do \
|
||||
dest_file=$$(realpath --relative-to=$(coreboottop)/src/commonlib/bsd/ $$file); \
|
||||
install -m 755 -d "$(DESTDIR)/libpayload/`dirname $$dest_file`"; \
|
||||
install -m 644 "$$file" "$(DESTDIR)/libpayload/$$dest_file"; \
|
||||
done
|
||||
install -m 644 $(obj)/libpayload-config.h $(DESTDIR)/libpayload/include
|
||||
$(foreach item,$(includes), \
|
||||
install -m 755 -d $(DESTDIR)/libpayload/include/$(call extract_nth,2,$(item)); \
|
||||
install -m 644 $(call extract_nth,1,$(item)) $(DESTDIR)/libpayload/include/$(call extract_nth,2,$(item)); )
|
||||
printf " INSTALL $(DESTDIR)/libpayload/vboot\n"
|
||||
install -m 755 -d $(DESTDIR)/libpayload/vboot
|
||||
for file in `find $(VBOOT_SOURCE)/firmware/include \
|
||||
$(VBOOT_SOURCE)/firmware/2lib/include \
|
||||
-iname '*.h' -type f \
|
||||
| sed 's,$(VBOOT_SOURCE)/firmware/,,'`; do \
|
||||
install -m 755 -d $(DESTDIR)/libpayload/vboot/$$(dirname $$file); \
|
||||
install -m 644 $(VBOOT_SOURCE)/firmware/$$file $(DESTDIR)/libpayload/vboot/$$file ; \
|
||||
done
|
||||
printf " INSTALL $(DESTDIR)/libpayload/bin\n"
|
||||
install -m 755 -d $(DESTDIR)/libpayload/bin
|
||||
install -m 755 bin/lpgcc $(DESTDIR)/libpayload/bin
|
||||
|
@@ -5,3 +5,5 @@ head.o-y += head.c
|
||||
libc-y += virtual.c
|
||||
|
||||
libcbfs-$(CONFIG_LP_CBFS) += mock_media.c
|
||||
|
||||
CFLAGS += -Wno-address-of-packed-member
|
||||
|
@@ -42,6 +42,7 @@ libc-$(CONFIG_LP_GPL) += string.c
|
||||
libgdb-y += gdb.c
|
||||
|
||||
libcbfs-$(CONFIG_LP_CBFS) += rom_media.c
|
||||
libcbfs-$(CONFIG_LP_CBFS) += boot_media.c
|
||||
|
||||
# Multiboot support is configurable
|
||||
libc-$(CONFIG_LP_MULTIBOOT) += multiboot.c
|
||||
|
18
payloads/libpayload/arch/x86/boot_media.c
Normal file
18
payloads/libpayload/arch/x86/boot_media.c
Normal file
@@ -0,0 +1,18 @@
|
||||
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||
|
||||
#include <arch/virtual.h>
|
||||
#include <boot_device.h>
|
||||
#include <commonlib/bsd/cb_err.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sysinfo.h>
|
||||
|
||||
__attribute__((weak)) ssize_t boot_device_read(void *buf, size_t offset, size_t size)
|
||||
{
|
||||
/* Memory-mapping usually only works for the top 16MB. */
|
||||
if (!lib_sysinfo.boot_media_size || lib_sysinfo.boot_media_size - offset > 16 * MiB)
|
||||
return CB_ERR_ARG;
|
||||
const void *const ptr = phys_to_virt(0 - lib_sysinfo.boot_media_size + offset);
|
||||
memcpy(buf, ptr, size);
|
||||
return size;
|
||||
}
|
@@ -47,20 +47,12 @@ static void cb_parse_x86_rom_var_mtrr(void *ptr, struct sysinfo_t *info)
|
||||
info->x86_rom_var_mtrr_index = rom_mtrr->index;
|
||||
}
|
||||
|
||||
static void cb_parse_mrc_cache(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->mrc_cache = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
int cb_parse_arch_specific(struct cb_record *rec, struct sysinfo_t *info)
|
||||
{
|
||||
switch(rec->tag) {
|
||||
case CB_TAG_X86_ROM_MTRR:
|
||||
cb_parse_x86_rom_var_mtrr(rec, info);
|
||||
break;
|
||||
case CB_TAG_MRC_CACHE:
|
||||
cb_parse_mrc_cache(rec, info);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
@@ -63,3 +63,9 @@ if [ -d $BASE/../include ]; then
|
||||
else
|
||||
_INCDIR=$LIBPAYLOAD_PREFIX/include
|
||||
fi
|
||||
|
||||
if [ -d $BASE/../vboot ]; then
|
||||
_VBOOTINCDIR=$BASE/../vboot/include
|
||||
else
|
||||
_VBOOTINCDIR=$LIBPAYLOAD_PREFIX/../vboot/include
|
||||
fi
|
||||
|
@@ -167,6 +167,11 @@ if [ $_LIBDIR = $_OBJ ]; then
|
||||
if [ "$CONFIG_LP_TINYCURSES" = y ]; then
|
||||
_CFLAGS="$_CFLAGS -I$BASE/../curses"
|
||||
fi
|
||||
|
||||
_CFLAGS="$_CFLAGS -I$BASE/../../../src/commonlib/bsd/include"
|
||||
_CFLAGS="$_CFLAGS -I$BASE/../../../3rdparty/vboot/firmware/include"
|
||||
else
|
||||
_CFLAGS="$_CFLAGS -I$_VBOOTINCDIR"
|
||||
fi
|
||||
|
||||
# Check for the -fno-stack-protector silliness
|
||||
@@ -177,7 +182,7 @@ trygccoption -fno-stack-protector
|
||||
_CFLAGS="$_CFLAGS -include $BASE/../include/kconfig.h -include $BASE/../include/compiler.h"
|
||||
_CFLAGS="$_CFLAGS -I`$DEFAULT_CC $_ARCHEXTRA -print-search-dirs | head -n 1 | cut -d' ' -f2`include"
|
||||
|
||||
_LDFLAGS="-L$_LIBDIR $_LDSCRIPT -static"
|
||||
_LDFLAGS="-L$_LIBDIR $_LDSCRIPT -static -Wl,--gc-sections"
|
||||
|
||||
if [ $DOLINK -eq 0 ]; then
|
||||
if [ $DEBUGME -eq 1 ]; then
|
||||
|
@@ -29,6 +29,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __TEST__
|
||||
|
||||
/* CMocka function redefinition */
|
||||
void mock_assert(const int result, const char *const expression, const char *const file,
|
||||
const int line);
|
||||
|
||||
#define MOCK_ASSERT(result, expression) mock_assert((result), (expression), __FILE__, __LINE__)
|
||||
#define assert(statement) MOCK_ASSERT(!!(statement), #statement)
|
||||
|
||||
#else
|
||||
|
||||
// assert's existence depends on NDEBUG state on _last_ inclusion of assert.h,
|
||||
// so don't guard this against double-includes.
|
||||
#ifdef NDEBUG
|
||||
@@ -43,3 +54,5 @@
|
||||
abort(); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TEST__ */
|
||||
|
22
payloads/libpayload/include/boot_device.h
Normal file
22
payloads/libpayload/include/boot_device.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||
|
||||
#ifndef _BOOT_DEVICE_H
|
||||
#define _BOOT_DEVICE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* This is a boot device access function, which is used by libpayload to read data from
|
||||
* the flash memory (or other boot device). It has to be implemented by payloads that want
|
||||
* to use FMAP or libcbfs.
|
||||
*
|
||||
* @param buf The output buffer to which the data should be written to.
|
||||
* @param offset Absolute offset in bytes of the requested boot device memory area. Not aligned.
|
||||
* @param size Size in bytes of the requested boot device memory area. Not aligned.
|
||||
*
|
||||
* @returns Number of bytes returned to the buffer, or negative value on error. Typically should
|
||||
* be equal to the `size`, and not aligned forcefully.
|
||||
*/
|
||||
ssize_t boot_device_read(void *buf, size_t offset, size_t size);
|
||||
|
||||
#endif /* _BOOT_DEVICE_H */
|
@@ -1,82 +1,146 @@
|
||||
/*
|
||||
*
|
||||
* 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);
|
||||
static inline size_t cbfs_unverified_area_load(const char *area, 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);
|
||||
static inline void *cbfs_unverified_area_map(const char *area, 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);
|
||||
|
||||
void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf,
|
||||
size_t *size_inout);
|
||||
|
||||
/**********************************************************************************************
|
||||
* 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 void *cbfs_unverified_area_map(const char *area, const char *name,
|
||||
size_t *size_out)
|
||||
{
|
||||
return _cbfs_unverified_area_load(area, name, NULL, size_out);
|
||||
}
|
||||
|
||||
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_unverified_area_load(const char *area, const char *name, void *buf,
|
||||
size_t size)
|
||||
{
|
||||
if (_cbfs_unverified_area_load(area, name, buf, &size))
|
||||
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
|
||||
|
@@ -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))
|
||||
|
||||
|
46
payloads/libpayload/include/cbfs_glue.h
Normal file
46
payloads/libpayload/include/cbfs_glue.h
Normal file
@@ -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 */
|
82
payloads/libpayload/include/cbfs_legacy.h
Normal file
82
payloads/libpayload/include/cbfs_legacy.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
*
|
||||
* 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>
|
||||
|
||||
/* 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
|
@@ -321,6 +321,16 @@ struct cb_boot_media_params {
|
||||
uint64_t boot_media_size;
|
||||
};
|
||||
|
||||
|
||||
struct cb_cbmem_entry {
|
||||
uint32_t tag;
|
||||
uint32_t size;
|
||||
|
||||
uint64_t address;
|
||||
uint32_t entry_size;
|
||||
uint32_t id;
|
||||
};
|
||||
|
||||
struct cb_tsc_info {
|
||||
uint32_t tag;
|
||||
uint32_t size;
|
||||
@@ -443,6 +453,4 @@ static inline const char *cb_mb_part_string(const struct cb_mainboard *cbm)
|
||||
(void *)(((u8 *) (_rec)) + sizeof(*(_rec)) \
|
||||
+ (sizeof((_rec)->map[0]) * (_idx)))
|
||||
|
||||
/* Helper functions */
|
||||
uintptr_t get_cbmem_addr(const void *cbmem_tab_entry);
|
||||
#endif
|
||||
|
12
payloads/libpayload/include/fmap.h
Normal file
12
payloads/libpayload/include/fmap.h
Normal file
@@ -0,0 +1,12 @@
|
||||
/* SPDX_License-Identifier: BSD-3-Clause */
|
||||
|
||||
#ifndef _FMAP_H
|
||||
#define _FMAP_H
|
||||
|
||||
#include <commonlib/bsd/cb_err.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Looks for area with |name| in FlashMap. Requires lib_sysinfo.fmap_cache. */
|
||||
cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size);
|
||||
|
||||
#endif /* _FMAP_H */
|
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Alternatively, this software may be distributed under the terms of the
|
||||
* GNU General Public License ("GPL") version 2 as published by the Free
|
||||
* Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef FLASHMAP_SERIALIZED_H__
|
||||
#define FLASHMAP_SERIALIZED_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define FMAP_SIGNATURE "__FMAP__"
|
||||
#define FMAP_VER_MAJOR 1 /* this header's FMAP minor version */
|
||||
#define FMAP_VER_MINOR 1 /* this header's FMAP minor version */
|
||||
#define FMAP_STRLEN 32 /* maximum length for strings, */
|
||||
/* including null-terminator */
|
||||
|
||||
enum fmap_flags {
|
||||
FMAP_AREA_STATIC = 1 << 0,
|
||||
FMAP_AREA_COMPRESSED = 1 << 1,
|
||||
FMAP_AREA_RO = 1 << 2,
|
||||
FMAP_AREA_PRESERVE = 1 << 3,
|
||||
};
|
||||
|
||||
/* Mapping of volatile and static regions in firmware binary */
|
||||
struct fmap_area {
|
||||
uint32_t offset; /* offset relative to base */
|
||||
uint32_t size; /* size in bytes */
|
||||
uint8_t name[FMAP_STRLEN]; /* descriptive name */
|
||||
uint16_t flags; /* flags for this area */
|
||||
} __packed;
|
||||
|
||||
struct fmap {
|
||||
uint8_t signature[8]; /* "__FMAP__" (0x5F5F464D41505F5F) */
|
||||
uint8_t ver_major; /* major version */
|
||||
uint8_t ver_minor; /* minor version */
|
||||
uint64_t base; /* address of the firmware binary */
|
||||
uint32_t size; /* size of firmware binary in bytes */
|
||||
uint8_t name[FMAP_STRLEN]; /* name of this firmware binary */
|
||||
uint16_t nareas; /* number of areas described by
|
||||
fmap_areas[] below */
|
||||
struct fmap_area areas[];
|
||||
} __packed;
|
||||
|
||||
#endif /* FLASHMAP_SERIALIZED_H__ */
|
@@ -45,10 +45,11 @@
|
||||
#include <stdbool.h>
|
||||
#include <libpayload-config.h>
|
||||
#include <cbgfx.h>
|
||||
#include <commonlib/bsd/fmap_serialized.h>
|
||||
#include <ctype.h>
|
||||
#include <die.h>
|
||||
#include <endian.h>
|
||||
#include <fmap_serialized.h>
|
||||
#include <fmap.h>
|
||||
#include <ipchksum.h>
|
||||
#include <kconfig.h>
|
||||
#include <stddef.h>
|
||||
@@ -457,6 +458,8 @@ static inline int clz(u32 x)
|
||||
static inline int log2(u32 x) { return (int)sizeof(x) * 8 - clz(x) - 1; }
|
||||
/* Find First Set: __ffs(0xf) == 0, __ffs(0) == -1, __ffs(1 << 31) == 31 */
|
||||
static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
|
||||
/* Find Last Set: __fls(1) == 0, __fls(5) == 2, __fls(1 << 31) == 31 */
|
||||
static inline int __fls(u32 x) { return log2(x); }
|
||||
|
||||
static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
|
||||
static inline int clz64(u64 x)
|
||||
@@ -466,6 +469,7 @@ static inline int clz64(u64 x)
|
||||
|
||||
static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
|
||||
static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
|
||||
static inline int __fls64(u64 x) { return log2_64(x); }
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
|
@@ -26,4 +26,21 @@ void write16(volatile void *addr, uint16_t val);
|
||||
void write32(volatile void *addr, uint32_t val);
|
||||
void write64(volatile void *addr, uint64_t val);
|
||||
|
||||
/* x86 I/O functions */
|
||||
unsigned int inl(int port);
|
||||
unsigned short inw(int port);
|
||||
unsigned char inb(int port);
|
||||
|
||||
void outl(unsigned int val, int port);
|
||||
void outw(unsigned short val, int port);
|
||||
void outb(unsigned char val, int port);
|
||||
|
||||
void outsl(int port, const void *addr, unsigned long count);
|
||||
void outsw(int port, const void *addr, unsigned long count);
|
||||
void outsb(int port, const void *addr, unsigned long count);
|
||||
|
||||
void insl(int port, void *addr, unsigned long count);
|
||||
void insw(int port, void *addr, unsigned long count);
|
||||
void insb(int port, void *addr, unsigned long count);
|
||||
|
||||
#endif /* _ARCH_IO_H */
|
||||
|
@@ -150,6 +150,12 @@ struct sysinfo_t {
|
||||
#endif
|
||||
/* USB Type-C Port Configuration Info */
|
||||
uintptr_t type_c_info;
|
||||
|
||||
/* CBFS RW/RO Metadata Cache */
|
||||
uintptr_t cbfs_ro_mcache_offset;
|
||||
uint32_t cbfs_ro_mcache_size;
|
||||
uintptr_t cbfs_rw_mcache_offset;
|
||||
uint32_t cbfs_rw_mcache_size;
|
||||
};
|
||||
|
||||
extern struct sysinfo_t lib_sysinfo;
|
||||
|
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <libpayload-config.h>
|
||||
#include <libpayload.h>
|
||||
#include <commonlib/bsd/cbmem_id.h>
|
||||
#include <coreboot_tables.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -41,12 +42,6 @@
|
||||
/* === Parsing code === */
|
||||
/* This is the generic parsing code. */
|
||||
|
||||
uintptr_t get_cbmem_addr(const void *const cbmem_tab_entry)
|
||||
{
|
||||
const struct cb_cbmem_tab *const cbmem = cbmem_tab_entry;
|
||||
return cbmem->cbmem_tab;
|
||||
}
|
||||
|
||||
static void cb_parse_memory(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
struct cb_memory *mem = ptr;
|
||||
@@ -83,11 +78,6 @@ static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
|
||||
info->cb_serial = virt_to_phys(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_vboot_workbuf(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->vboot_workbuf = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
struct lb_range *vbnv = (struct lb_range *)ptr;
|
||||
@@ -128,26 +118,6 @@ static void cb_parse_mac_addresses(unsigned char *ptr,
|
||||
info->macs[i] = macs->mac_addrs[i];
|
||||
}
|
||||
|
||||
static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->tstamp_table = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->cbmem_cons = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_acpi_gnvs(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->acpi_gnvs = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_acpi_cnvs(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->acpi_cnvs = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_board_config(unsigned char *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
struct cb_board_config *const config = (struct cb_board_config *)ptr;
|
||||
@@ -188,11 +158,6 @@ static void cb_parse_string(const void *const ptr, uintptr_t *const info)
|
||||
*info = virt_to_phys(str->string);
|
||||
}
|
||||
|
||||
static void cb_parse_wifi_calibration(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->wifi_calibration = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_ramoops(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
struct lb_range *ramoops = (struct lb_range *)ptr;
|
||||
@@ -236,21 +201,6 @@ static void cb_parse_boot_media_params(unsigned char *ptr,
|
||||
info->boot_media_size = bmp->boot_media_size;
|
||||
}
|
||||
|
||||
static void cb_parse_vpd(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->chromeos_vpd = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_fmap_cache(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->fmap_cache = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
static void cb_parse_type_c_info(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
info->type_c_info = get_cbmem_addr(ptr);
|
||||
}
|
||||
|
||||
#if CONFIG(LP_TIMER_RDTSC)
|
||||
static void cb_parse_tsc_info(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
@@ -264,6 +214,57 @@ static void cb_parse_tsc_info(void *ptr, struct sysinfo_t *info)
|
||||
}
|
||||
#endif
|
||||
|
||||
static void cb_parse_cbmem_entry(void *ptr, struct sysinfo_t *info)
|
||||
{
|
||||
const struct cb_cbmem_entry *cbmem_entry = ptr;
|
||||
|
||||
if (cbmem_entry->size != sizeof(*cbmem_entry))
|
||||
return;
|
||||
|
||||
switch (cbmem_entry->id) {
|
||||
case CBMEM_ID_ACPI_CNVS:
|
||||
info->acpi_cnvs = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_ACPI_GNVS:
|
||||
info->acpi_gnvs = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_CBFS_RO_MCACHE:
|
||||
info->cbfs_ro_mcache_offset = cbmem_entry->address;
|
||||
info->cbfs_ro_mcache_size = cbmem_entry->entry_size;
|
||||
break;
|
||||
case CBMEM_ID_CBFS_RW_MCACHE:
|
||||
info->cbfs_rw_mcache_offset = cbmem_entry->address;
|
||||
info->cbfs_rw_mcache_size = cbmem_entry->entry_size;
|
||||
break;
|
||||
case CBMEM_ID_CONSOLE:
|
||||
info->cbmem_cons = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_MRCDATA:
|
||||
info->mrc_cache = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_VBOOT_WORKBUF:
|
||||
info->vboot_workbuf = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_TIMESTAMP:
|
||||
info->tstamp_table = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_VPD:
|
||||
info->chromeos_vpd = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_FMAP:
|
||||
info->fmap_cache = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_WIFI_CALIBRATION:
|
||||
info->wifi_calibration = cbmem_entry->address;
|
||||
break;
|
||||
case CBMEM_ID_TYPE_C_INFO:
|
||||
info->type_c_info = cbmem_entry->address;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
|
||||
{
|
||||
struct cb_header *header;
|
||||
@@ -372,33 +373,15 @@ int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
|
||||
case CB_TAG_VBNV:
|
||||
cb_parse_vbnv(ptr, info);
|
||||
break;
|
||||
case CB_TAG_VBOOT_WORKBUF:
|
||||
cb_parse_vboot_workbuf(ptr, info);
|
||||
break;
|
||||
case CB_TAG_MAC_ADDRS:
|
||||
cb_parse_mac_addresses(ptr, info);
|
||||
break;
|
||||
case CB_TAG_SERIALNO:
|
||||
cb_parse_string(ptr, &info->serialno);
|
||||
break;
|
||||
case CB_TAG_TIMESTAMPS:
|
||||
cb_parse_tstamp(ptr, info);
|
||||
break;
|
||||
case CB_TAG_CBMEM_CONSOLE:
|
||||
cb_parse_cbmem_cons(ptr, info);
|
||||
break;
|
||||
case CB_TAG_ACPI_GNVS:
|
||||
cb_parse_acpi_gnvs(ptr, info);
|
||||
break;
|
||||
case CB_TAG_ACPI_CNVS:
|
||||
cb_parse_acpi_cnvs(ptr, info);
|
||||
break;
|
||||
case CB_TAG_BOARD_CONFIG:
|
||||
cb_parse_board_config(ptr, info);
|
||||
break;
|
||||
case CB_TAG_WIFI_CALIBRATION:
|
||||
cb_parse_wifi_calibration(ptr, info);
|
||||
break;
|
||||
case CB_TAG_RAM_OOPS:
|
||||
cb_parse_ramoops(ptr, info);
|
||||
break;
|
||||
@@ -414,20 +397,14 @@ int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
|
||||
case CB_TAG_BOOT_MEDIA_PARAMS:
|
||||
cb_parse_boot_media_params(ptr, info);
|
||||
break;
|
||||
case CB_TAG_CBMEM_ENTRY:
|
||||
cb_parse_cbmem_entry(ptr, info);
|
||||
break;
|
||||
#if CONFIG(LP_TIMER_RDTSC)
|
||||
case CB_TAG_TSC_INFO:
|
||||
cb_parse_tsc_info(ptr, info);
|
||||
break;
|
||||
#endif
|
||||
case CB_TAG_VPD:
|
||||
cb_parse_vpd(ptr, info);
|
||||
break;
|
||||
case CB_TAG_FMAP:
|
||||
cb_parse_fmap_cache(ptr, info);
|
||||
break;
|
||||
case CB_TAG_TYPE_C_INFO:
|
||||
cb_parse_type_c_info(ptr, info);
|
||||
break;
|
||||
default:
|
||||
cb_parse_arch_specific(rec, info);
|
||||
break;
|
||||
|
@@ -28,10 +28,60 @@
|
||||
|
||||
#include <libpayload-config.h>
|
||||
#include <libpayload.h>
|
||||
#include <commonlib/bsd/fmap_serialized.h>
|
||||
#include <coreboot_tables.h>
|
||||
#include <cbfs.h>
|
||||
#include <fmap_serialized.h>
|
||||
#include <boot_device.h>
|
||||
#include <stdint.h>
|
||||
#include <arch/virtual.h>
|
||||
|
||||
/* Private fmap cache. */
|
||||
static struct fmap *_fmap_cache;
|
||||
|
||||
static cb_err_t fmap_find_area(struct fmap *fmap, const char *name, size_t *offset,
|
||||
size_t *size)
|
||||
{
|
||||
for (size_t i = 0; i < le32toh(fmap->nareas); ++i) {
|
||||
if (strncmp((const char *)fmap->areas[i].name, name, FMAP_STRLEN) != 0)
|
||||
continue;
|
||||
if (offset)
|
||||
*offset = le32toh(fmap->areas[i].offset);
|
||||
if (size)
|
||||
*size = le32toh(fmap->areas[i].size);
|
||||
return CB_SUCCESS;
|
||||
}
|
||||
|
||||
return CB_ERR;
|
||||
}
|
||||
|
||||
static bool fmap_is_signature_valid(struct fmap *fmap)
|
||||
{
|
||||
return memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature)) == 0;
|
||||
}
|
||||
|
||||
static bool fmap_setup_cache(void)
|
||||
{
|
||||
/* Use FMAP cache if available */
|
||||
if (lib_sysinfo.fmap_cache
|
||||
&& fmap_is_signature_valid((struct fmap *)phys_to_virt(lib_sysinfo.fmap_cache))) {
|
||||
_fmap_cache = (struct fmap *)phys_to_virt(lib_sysinfo.fmap_cache);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size)
|
||||
{
|
||||
if (!_fmap_cache && !fmap_setup_cache())
|
||||
return CB_ERR;
|
||||
|
||||
return fmap_find_area(_fmap_cache, name, offset, size);
|
||||
}
|
||||
|
||||
/***********************************************************************************************
|
||||
* LEGACY CODE *
|
||||
**********************************************************************************************/
|
||||
|
||||
int fmap_region_by_name(const uint32_t fmap_offset, const char * const name,
|
||||
uint32_t * const offset, uint32_t * const size)
|
||||
|
31
payloads/libpayload/libcbfs/Kconfig
Normal file
31
payloads/libpayload/libcbfs/Kconfig
Normal file
@@ -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_LIB
|
||||
help
|
||||
This option enables hash verification of CBFS files in RO (COREBOOT) and RW regions.
|
||||
|
||||
endif
|
@@ -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
|
||||
|
@@ -1,243 +1,225 @@
|
||||
/*
|
||||
*
|
||||
* 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 <cbfs_glue.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)
|
||||
static const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
|
||||
{
|
||||
return (c <= 9) ? (c + '0') : (c - 10 + 'a');
|
||||
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;
|
||||
}
|
||||
|
||||
if (ro.dev.size)
|
||||
return &ro;
|
||||
|
||||
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 &ro;
|
||||
}
|
||||
|
||||
static void tohex16(unsigned int val, char* dest)
|
||||
ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mdata)
|
||||
{
|
||||
dest[0] = tohex4(val>>12);
|
||||
dest[1] = tohex4((val>>8) & 0xf);
|
||||
dest[2] = tohex4((val>>4) & 0xf);
|
||||
dest[3] = tohex4(val & 0xf);
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return cbd->dev.offset + data_offset;
|
||||
}
|
||||
|
||||
void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
|
||||
uint16_t device)
|
||||
void cbfs_unmap(void *mapping)
|
||||
{
|
||||
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);
|
||||
free(mapping);
|
||||
}
|
||||
|
||||
void * cbfs_load_stage(struct cbfs_media *media, const char *name)
|
||||
static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
|
||||
const union cbfs_mdata *mdata, bool skip_verification)
|
||||
{
|
||||
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 (!CONFIG(LP_CBFS_VERIFICATION) || skip_verification)
|
||||
return false;
|
||||
|
||||
if (stage == NULL)
|
||||
return (void *) -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("loading stage %s @ %p (%d bytes), entry @ 0x%llx\n",
|
||||
name,
|
||||
(void*)(uintptr_t) stage->load, stage->memlen,
|
||||
stage->entry);
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
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, bool skip_verification)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (boot_device_read(load, offset, in_size) != in_size) {
|
||||
ERROR("'%s' failed to read contents of file\n", mdata->h.filename);
|
||||
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);
|
||||
if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification))
|
||||
goto out;
|
||||
|
||||
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);
|
||||
}
|
||||
out:
|
||||
free(stage);
|
||||
return (void *) entry;
|
||||
if (load != buffer)
|
||||
free(load);
|
||||
return out_size;
|
||||
}
|
||||
|
||||
int cbfs_execute_stage(struct cbfs_media *media, const char *name)
|
||||
static void *do_load(union cbfs_mdata *mdata, ssize_t offset, void *buf, size_t *size_inout,
|
||||
bool skip_verification)
|
||||
{
|
||||
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;
|
||||
bool malloced = false;
|
||||
size_t out_size;
|
||||
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);
|
||||
}
|
||||
|
||||
LOG("run @ %p\n", (void *) (uintptr_t)ntohll(stage->entry));
|
||||
int result = run_address((void *)(uintptr_t)ntohll(stage->entry));
|
||||
free(stage);
|
||||
return result;
|
||||
if (buf) {
|
||||
if (!size_inout || *size_inout < out_size) {
|
||||
ERROR("'%s' buffer too small\n", mdata->h.filename);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
buf = malloc(out_size);
|
||||
if (!buf) {
|
||||
ERROR("'%s' allocation failure\n", mdata->h.filename);
|
||||
return NULL;
|
||||
}
|
||||
malloced = true;
|
||||
}
|
||||
|
||||
if (cbfs_load_and_decompress(offset, be32toh(mdata->h.len), buf, out_size, compression,
|
||||
mdata, skip_verification)
|
||||
!= out_size) {
|
||||
if (malloced)
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
if (size_inout)
|
||||
*size_inout = out_size;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void *cbfs_load_payload(struct cbfs_media *media, const char *name)
|
||||
void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro)
|
||||
{
|
||||
return (struct cbfs_payload *)cbfs_get_file_content(
|
||||
media, name, CBFS_TYPE_SELF, NULL);
|
||||
}
|
||||
ssize_t offset;
|
||||
union cbfs_mdata mdata;
|
||||
|
||||
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;
|
||||
DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf,
|
||||
force_ro ? "true" : "false");
|
||||
|
||||
if (!handle)
|
||||
offset = _cbfs_boot_lookup(name, force_ro, &mdata);
|
||||
if (offset < 0)
|
||||
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 do_load(&mdata, offset, buf, size_inout, false);
|
||||
}
|
||||
|
||||
void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf,
|
||||
size_t *size_inout)
|
||||
{
|
||||
struct cbfs_dev dev;
|
||||
union cbfs_mdata mdata;
|
||||
size_t data_offset;
|
||||
|
||||
DEBUG("%s(area='%s', name='%s', buf=%p)\n", __func__, area, name, buf);
|
||||
|
||||
if (fmap_locate_area(area, &dev.offset, &dev.size) != CB_SUCCESS)
|
||||
return NULL;
|
||||
|
||||
if (cbfs_lookup(&dev, name, &mdata, &data_offset, NULL)) {
|
||||
ERROR("'%s' not found in '%s'\n", name, area);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
free(handle);
|
||||
return ret;
|
||||
return do_load(&mdata, dev.offset + data_offset, buf, size_inout, true);
|
||||
}
|
||||
|
||||
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
|
||||
|
243
payloads/libpayload/libcbfs/cbfs_legacy.c
Normal file
243
payloads/libpayload/libcbfs/cbfs_legacy.c
Normal file
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <cbfs.h>
|
||||
#include <string.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)
|
||||
{
|
||||
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
|
@@ -11,8 +11,6 @@ testobj := $(obj)/tests
|
||||
endif
|
||||
coverage-dir := $(testobj)/coverage_reports
|
||||
|
||||
coreboottop := ../../
|
||||
|
||||
cmockasrc := $(coreboottop)/3rdparty/cmocka
|
||||
cmockaobj := $(objutil)/cmocka
|
||||
CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
|
||||
@@ -34,16 +32,19 @@ TEST_CONFIG_ := CONFIG_LP_
|
||||
# Default includes
|
||||
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$(VBOOT_SOURCE)/firmware/include
|
||||
|
||||
# Test specific includes
|
||||
TEST_CFLAGS += -I$(testsrc)/include -I$(testsrc)/include/mocks
|
||||
TEST_CFLAGS += -I$(testsrc)/include
|
||||
TEST_CFLAGS += -I$(cmockasrc)/include
|
||||
|
||||
# Minimal subset of warnings and errors. Tests can be less strict than actual build.
|
||||
TEST_CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wvla
|
||||
TEST_CFLAGS += -Wwrite-strings -Wno-trigraphs -Wimplicit-fallthrough
|
||||
TEST_CFLAGS += -Wstrict-aliasing -Wshadow -Werror
|
||||
TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
|
||||
|
||||
TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
|
||||
|
||||
@@ -140,10 +141,10 @@ $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
|
||||
objcopy_wrap_flags=''; \
|
||||
for sym in $$($(1)-mocks); do \
|
||||
sym_line="$$$$($(HOSTOBJDUMP) -t $$@.orig \
|
||||
| grep -E \"[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$\")"; \
|
||||
| grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s+$$$$sym$$$$")"; \
|
||||
if [ ! -z "$$$$sym_line" ] ; then \
|
||||
addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
|
||||
section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
|
||||
addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
|
||||
section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
|
||||
objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
|
||||
fi \
|
||||
done ; \
|
||||
|
@@ -6,4 +6,3 @@ speaker-test-srcs += tests/drivers/speaker-test.c
|
||||
speaker-test-mocks += inb
|
||||
speaker-test-mocks += outb
|
||||
speaker-test-mocks += arch_ndelay
|
||||
speaker-test-cflags += -include $(testsrc)/include/mocks/x86_io.h
|
||||
|
@@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
|
||||
#include <libpayload.h>
|
||||
#include <mocks/x86_io.h>
|
||||
|
||||
/* Include source to gain access to private defines */
|
||||
#include "../drivers/speaker.c"
|
||||
|
114
payloads/libpayload/tests/include/mocks/cbfs_util.h
Normal file
114
payloads/libpayload/tests/include/mocks/cbfs_util.h
Normal file
@@ -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 */
|
@@ -1,30 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
|
||||
#ifndef TESTS_MOCKS_X86_IO_H_
|
||||
#define TESTS_MOCKS_X86_IO_H_
|
||||
|
||||
unsigned int inl(int port);
|
||||
|
||||
unsigned short inw(int port);
|
||||
|
||||
unsigned char inb(int port);
|
||||
|
||||
void outl(unsigned int val, int port);
|
||||
|
||||
void outw(unsigned short val, int port);
|
||||
|
||||
void outb(unsigned char val, int port);
|
||||
|
||||
void outsl(int port, const void *addr, unsigned long count);
|
||||
|
||||
void outsw(int port, const void *addr, unsigned long count);
|
||||
|
||||
void outsb(int port, const void *addr, unsigned long count);
|
||||
|
||||
void insl(int port, void *addr, unsigned long count);
|
||||
|
||||
void insw(int port, void *addr, unsigned long count);
|
||||
|
||||
void insb(int port, void *addr, unsigned long count);
|
||||
|
||||
#endif
|
3
payloads/libpayload/tests/libc/Makefile.inc
Normal file
3
payloads/libpayload/tests/libc/Makefile.inc
Normal file
@@ -0,0 +1,3 @@
|
||||
tests-y += fmap_locate_area-test
|
||||
|
||||
fmap_locate_area-test-srcs += tests/libc/fmap_locate_area-test.c
|
112
payloads/libpayload/tests/libc/fmap_locate_area-test.c
Normal file
112
payloads/libpayload/tests/libc/fmap_locate_area-test.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
|
||||
#include "../libc/fmap.c"
|
||||
|
||||
#include <libpayload.h>
|
||||
#include <tests/test.h>
|
||||
|
||||
|
||||
/* Mocks */
|
||||
struct sysinfo_t lib_sysinfo;
|
||||
unsigned long virtual_offset = 0;
|
||||
|
||||
static void reset_fmap_cache(void)
|
||||
{
|
||||
_fmap_cache = NULL;
|
||||
}
|
||||
|
||||
static int setup_fmap_test(void **state)
|
||||
{
|
||||
reset_fmap_cache();
|
||||
lib_sysinfo.fmap_cache = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_fmap_locate_area_no_fmap_available(void **state)
|
||||
{
|
||||
size_t offset = 0;
|
||||
size_t size = 0;
|
||||
|
||||
assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset, &size));
|
||||
}
|
||||
|
||||
static void test_fmap_locate_area_incorrect_signature(void **state)
|
||||
{
|
||||
size_t offset = 0;
|
||||
size_t size = 0;
|
||||
struct fmap mock_fmap = {
|
||||
.signature = "NOT_MAP",
|
||||
};
|
||||
lib_sysinfo.fmap_cache = (uintptr_t)&mock_fmap;
|
||||
|
||||
assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset, &size));
|
||||
}
|
||||
|
||||
static void test_fmap_locate_area_success(void **state)
|
||||
{
|
||||
size_t offset = 0;
|
||||
size_t size = 0;
|
||||
struct fmap mock_fmap = {
|
||||
.signature = FMAP_SIGNATURE,
|
||||
.ver_major = 1,
|
||||
.ver_minor = 1,
|
||||
.base = 0xAABB,
|
||||
.size = 0x10000,
|
||||
.nareas = 3,
|
||||
};
|
||||
struct fmap_area area_1 = {
|
||||
.size = 0x1100,
|
||||
.offset = 0x11,
|
||||
.name = {'F', 'I', 'R', 'S', 'T', '_', 'A', 'R', 'E', 'A', 0},
|
||||
.flags = 0,
|
||||
};
|
||||
struct fmap_area area_2 = {
|
||||
.size = 0x2200,
|
||||
.offset = 0x1111,
|
||||
.name = {'S', 'E', 'C', 'O', 'N', 'D', '_', 'A', 'R', 'E', 'A', 0},
|
||||
.flags = 0,
|
||||
};
|
||||
struct fmap_area area_3 = {
|
||||
.size = 0x100,
|
||||
.offset = 0x3311,
|
||||
.name = {'T', 'H', 'I', 'R', 'D', '_', 'A', 'R', 'E', 'A', 0},
|
||||
.flags = 0,
|
||||
};
|
||||
u8 fmap_buffer[sizeof(struct fmap) + 3 * sizeof(struct fmap_area)];
|
||||
memcpy(fmap_buffer, &mock_fmap, sizeof(mock_fmap));
|
||||
memcpy(&fmap_buffer[sizeof(mock_fmap)], &area_1, sizeof(area_1));
|
||||
memcpy(&fmap_buffer[sizeof(mock_fmap) + sizeof(area_1)], &area_2, sizeof(area_2));
|
||||
memcpy(&fmap_buffer[sizeof(mock_fmap) + sizeof(area_1) + sizeof(area_2)], &area_3,
|
||||
sizeof(area_3));
|
||||
|
||||
/* Cache only */
|
||||
reset_fmap_cache();
|
||||
lib_sysinfo.fmap_cache = (uintptr_t)fmap_buffer;
|
||||
|
||||
assert_int_equal(0, fmap_locate_area("FIRST_AREA", &offset, &size));
|
||||
assert_int_equal(area_1.offset, offset);
|
||||
assert_int_equal(area_1.size, size);
|
||||
|
||||
assert_int_equal(0, fmap_locate_area("THIRD_AREA", &offset, &size));
|
||||
assert_int_equal(area_3.offset, offset);
|
||||
assert_int_equal(area_3.size, size);
|
||||
|
||||
assert_int_equal(0, fmap_locate_area("SECOND_AREA", &offset, &size));
|
||||
assert_int_equal(area_2.offset, offset);
|
||||
assert_int_equal(area_2.size, size);
|
||||
|
||||
reset_fmap_cache();
|
||||
}
|
||||
|
||||
#define FMAP_LOCATE_AREA_TEST(fn) cmocka_unit_test_setup(fn, setup_fmap_test)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_no_fmap_available),
|
||||
FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_incorrect_signature),
|
||||
FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_success),
|
||||
};
|
||||
|
||||
return lp_run_group_tests(tests, NULL, NULL);
|
||||
}
|
33
payloads/libpayload/tests/libcbfs/Makefile.inc
Normal file
33
payloads/libpayload/tests/libcbfs/Makefile.inc
Normal file
@@ -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
|
729
payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c
Normal file
729
payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c
Normal file
@@ -0,0 +1,729 @@
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* This test uses RW CBFS only */
|
||||
static void test_cbfs_unverified_area_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;
|
||||
|
||||
cbfs_buf = s->cbfs_rw_buf;
|
||||
set_fmap_locate_area_results((size_t)cbfs_buf, s->cbfs_rw_size, CB_SUCCESS);
|
||||
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_unverified_area_map("TEST_AREA", 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_unverified_area_map("TEST_AREA", 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_unverified_area_map("TEST_AREA", 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_unverified_area_map("TEST_AREA", 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_unverified_area_map("TEST_AREA", 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);
|
||||
mapping = cbfs_unverified_area_map("TEST_AREA", "invalid_file", &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),
|
||||
TEST_CBFS_LOOKUP(test_cbfs_unverified_area_map),
|
||||
};
|
||||
|
||||
return lp_run_group_tests(tests, NULL, NULL);
|
||||
}
|
247
payloads/libpayload/tests/libcbfs/cbfs-verification-test.c
Normal file
247
payloads/libpayload/tests/libcbfs/cbfs-verification-test.c
Normal file
@@ -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);
|
||||
}
|
95
payloads/libpayload/tests/mocks/cbfs_file_mock.c
Normal file
95
payloads/libpayload/tests/mocks/cbfs_file_mock.c
Normal file
@@ -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),
|
||||
},
|
||||
};
|
16
payloads/libpayload/tests/mocks/die.c
Normal file
16
payloads/libpayload/tests/mocks/die.c
Normal file
@@ -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)
|
||||
;
|
||||
}
|
27
payloads/libpayload/vboot/Kconfig
Normal file
27
payloads/libpayload/vboot/Kconfig
Normal file
@@ -0,0 +1,27 @@
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
config VBOOT_LIB
|
||||
bool "Compile verified boot (vboot) library"
|
||||
default y if CHROMEOS
|
||||
default n
|
||||
help
|
||||
This option enables compiling and building vboot libraries vboot_fw and tlcl.
|
||||
|
||||
if VBOOT_LIB
|
||||
|
||||
config VBOOT_TPM2_MODE
|
||||
bool "TPM2 Mode"
|
||||
default y
|
||||
help
|
||||
This option enables TPM 2.0 support in vboot. Disabling it allows using TPM 1.2.
|
||||
|
||||
config VBOOT_X86_SHA_EXT
|
||||
bool "x86 SHA Extension"
|
||||
default y if CHROMEOS
|
||||
default n
|
||||
depends on ARCH_X86
|
||||
help
|
||||
This option enables SHA256 implementation using x86 SHA processor extension
|
||||
instructions: sha256msg1, sha256msg2, sha256rnds2.
|
||||
|
||||
endif
|
46
payloads/libpayload/vboot/Makefile.inc
Normal file
46
payloads/libpayload/vboot/Makefile.inc
Normal file
@@ -0,0 +1,46 @@
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
VBOOT_BUILD_DIR ?= $(abspath $(obj)/external/vboot)
|
||||
VBOOT_FW_LIB = $(VBOOT_BUILD_DIR)/vboot_fw.a
|
||||
TLCL_LIB = $(VBOOT_BUILD_DIR)/tlcl.a
|
||||
|
||||
vboot_fw-objs += $(VBOOT_FW_LIB)
|
||||
tlcl-objs += $(TLCL_LIB)
|
||||
|
||||
kconfig-to-binary=$(if $(strip $(1)),1,0)
|
||||
vboot-fixup-includes = $(patsubst -I%,-I$(top)/%,\
|
||||
$(patsubst include/%.h,$(top)/include/%.h,\
|
||||
$(filter-out -I$(obj),$(1))))
|
||||
|
||||
ifeq ($(CONFIG_LP_ARCH_MOCK),)
|
||||
VBOOT_CFLAGS += $(call vboot-fixup-includes,$(CFLAGS))
|
||||
VBOOT_CFLAGS += -I$(abspath $(obj))
|
||||
endif
|
||||
|
||||
# Enable vboot debug by default
|
||||
VBOOT_CFLAGS += -DVBOOT_DEBUG
|
||||
|
||||
VBOOT_FIRMWARE_ARCH-$(CONFIG_LP_ARCH_ARM) := arm
|
||||
VBOOT_FIRMWARE_ARCH-$(CONFIG_LP_ARCH_X86) := x86
|
||||
VBOOT_FIRMWARE_ARCH-$(CONFIG_LP_ARCH_ARM64) := arm64
|
||||
|
||||
ifeq ($(CONFIG_LP_ARCH_MOCK)$(VBOOT_FIRMWARE_ARCH-y),)
|
||||
$(error vboot requires architecture to be set in the configuration)
|
||||
endif
|
||||
|
||||
$(VBOOT_FW_LIB): $(obj)/libpayload-config.h
|
||||
@printf " MAKE $(subst $(obj)/,,$(@))\n"
|
||||
+$(Q) FIRMWARE_ARCH="$(VBOOT_FIRMWARE_ARCH-y)" \
|
||||
CC="$(CC)" \
|
||||
CFLAGS="$(VBOOT_CFLAGS)" \
|
||||
$(MAKE) -C "$(VBOOT_SOURCE)" \
|
||||
TPM2_MODE=$(call kconfig-to-binary, $(CONFIG_LP_VBOOT_TPM2_MODE)) \
|
||||
X86_SHA_EXT=$(call kconfig-to-binary, $(CONFIG_LP_VBOOT_X86_SHA_EXT)) \
|
||||
UNROLL_LOOPS=1 \
|
||||
BUILD="$(VBOOT_BUILD_DIR)" \
|
||||
V=$(V) \
|
||||
$(VBOOT_BUILD_DIR)/vboot_fw.a tlcl
|
||||
|
||||
$(TLCL_LIB): $(VBOOT_FW_LIB)
|
||||
|
||||
.PHONY: $(VBOOT_FW_LIB) $(TLCL_LIB)
|
Reference in New Issue
Block a user