Skip to content

Commit

Permalink
rimage: import rimage image creation and signing tool
Browse files Browse the repository at this point in the history
rimage is being moved from tools repo to sof repo since there is now a
cyclic dependency between both repos and sof needs rimage for it's
build.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  • Loading branch information
lrgirdwo committed Feb 13, 2018
1 parent bf3c0f2 commit 05ef434
Show file tree
Hide file tree
Showing 26 changed files with 2,909 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ reef-*
boot_ldr-*
boot_module
module
rimage/rimage
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
if BUILD_RIMAGE
SUBDIRS = rimage
else
SUBDIRS = src
endif

ACLOCAL_AMFLAGS = -I m4

Expand Down
12 changes: 9 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ Build Instructions

1) Run "autogen.sh"

2) Run the following configure based on your platform.
2) Build and install the rimage ELF image creator and signing tool

./configure --enable-rimage
make
sudo make install

3) Run the following configure based on your platform.

Baytrail :-

Expand All @@ -20,8 +26,8 @@ headers

./configure --with-arch=host --enable-library=yes --host=x86_64-unknown-linux-gnu --prefix=$pwd/../host-root/

3) make
4) make

4) make bin
5) make bin


42 changes: 34 additions & 8 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AC_DEFINE_UNQUOTED([REEF_MICRO], reef_micro, [Reef micro version])
AC_CANONICAL_HOST

# General compiler flags
CFLAGS="${CFLAGS:+$CFLAGS } -O2 -g -Wall -Werror -Wl,-EL -fno-inline-functions -nostdlib -Wmissing-prototypes"
CFLAGS="${CFLAGS:+$CFLAGS } -O2 -g -Wall -Werror -Wl,-EL -Wmissing-prototypes"

# General assembler flags
ASFLAGS="-DASSEMBLY"
Expand All @@ -37,6 +37,13 @@ if test "$have_library" = "yes"; then
fi
AM_CONDITIONAL(BUILD_LIB, test "$have_library" = "yes")

# check if we are building tools
AC_ARG_ENABLE(rimage, [AS_HELP_STRING([--enable-rimage],[build rimage tool])], have_rimage=$enableval, have_rimage=no)
if test "$have_rimage" = "yes"; then
AC_DEFINE([CONFIG_RIMAGE], [1], [Configure to build rimage])
fi
AM_CONDITIONAL(BUILD_RIMAGE, test "$have_rimage" = "yes")

# Architecture support
AC_ARG_WITH([arch],
AS_HELP_STRING([--with-arch], [Specify DSP architecture]),
Expand All @@ -52,7 +59,7 @@ case "$with_arch" in
AC_SUBST(XTENSA_LDFLAGS)

# extra CFLAGS defined here otherwise configure working gcc tests fails.
CFLAGS="${CFLAGS:+$CFLAGS }-mlongcalls"
CFLAGS="${CFLAGS:+$CFLAGS }-fno-inline-functions -nostdlib -mlongcalls"
LDFLAGS="${LDFLAGS:+$LDFLAGS }-nostdlib"

#ARCH_ASFLAGS=""
Expand All @@ -79,7 +86,9 @@ case "$with_arch" in
AC_SUBST(ARCH)
;;
*)
AC_MSG_ERROR([DSP architecture not specified])
if test "$have_rimage" = "no"; then
AC_MSG_ERROR([DSP architecture not specified])
fi
;;
esac

Expand Down Expand Up @@ -197,11 +206,13 @@ case "$with_platform" in
AC_DEFINE([CONFIG_DMA_GW], [1], [Configure DMA Gateway])
;;
*)
if test "$ARCH" = "host"; then
PLATFORM="host"
AC_SUBST(PLATFORM)
else
AC_MSG_ERROR([Host platform not specified])
if test "$have_rimage" = "no"; then
if test "$ARCH" = "host"; then
PLATFORM="host"
AC_SUBST(PLATFORM)
else
AC_MSG_ERROR([Host platform not specified])
fi
fi
;;
esac
Expand Down Expand Up @@ -315,12 +326,26 @@ LT_INIT
AC_CHECK_TOOL([OBJCOPY], [objcopy], [])
AC_CHECK_TOOL([OBJDUMP], [objdump], [])

# Check for openssl - used by rimage
AC_CHECK_LIB([crypto], [OPENSSL_config], , [have_openssl="no"])
if test "$have_rimage" = "yes"; then
if test "$have_openssl" = "no"; then
AC_MSG_ERROR([Need OpenSSL libcrypto for rimage code signing])
fi
fi

PEM_KEY_PREFIX="/usr/local/share/rimage"
AC_DEFINE_UNQUOTED([PEM_KEY_PREFIX], ["$PEM_KEY_PREFIX"], ["Path for PEM keys"])
AC_SUBST(PEM_KEY_PREFIX)

AM_EXTRA_RECURSIVE_TARGETS([bin])

AM_EXTRA_RECURSIVE_TARGETS([vminstall])

AC_CONFIG_FILES([
Makefile
rimage/Makefile
rimage/keys/Makefile
src/Makefile
src/tasks/Makefile
src/init/Makefile
Expand Down Expand Up @@ -380,6 +405,7 @@ echo "
Target Architecture: ${ARCH}
Target Platform: ${PLATFORM}
Target Core: ${XTENSA_CORE}
PEM: ${PEM_KEY_PREFIX}

Compiler: ${CC}
CFLAGS: ${CFLAGS}
Expand Down
25 changes: 25 additions & 0 deletions rimage/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SUBDIRS=keys

bin_PROGRAMS = rimage

noinst_HEADERS = \
rimage.h \
css.h \
cse.h \
plat_auth.h \
manifest.h \
file_format.h

rimage_SOURCES = \
file_simple.c \
man_apl.c \
man_cnl.c \
cse.c \
css.c \
plat_auth.c \
hash.c \
pkcs1_5.c \
manifest.c \
elf.c \
rimage.c

45 changes: 45 additions & 0 deletions rimage/cse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2017, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
* Keyon Jie <yang.jie@linux.intel.com>
*/

#include <stdio.h>
#include "rimage.h"
#include "cse.h"
#include "manifest.h"

void ri_cse_create(struct image *image)
{
struct CsePartitionDirHeader *cse_hdr = image->fw_image;
struct sof_man_adsp_meta_file_ext *meta = image->fw_image +
MAN_META_EXT_OFFSET;
struct CsePartitionDirEntry *cse_entry =
image->fw_image + sizeof(*cse_hdr);
uint8_t csum = 0, *val = image->fw_image;
int i, size;

fprintf(stdout, " cse: completing CSE manifest\n");

cse_entry[2].length = meta->comp_desc[0].limit_offset -
MAN_DESC_OFFSET;

/* calculate checksum using BSD algo */
size = sizeof(*cse_hdr) + sizeof(*cse_entry) * MAN_CSE_PARTS;
for (i = 0; i < size; i++) {
if (i == 11)
continue;
csum += val[i];
}
cse_hdr->checksum = 0x100 - csum;
}
42 changes: 42 additions & 0 deletions rimage/cse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2017, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/

#ifndef __CSE_H__
#define __CSE_H__

#include <stdint.h>

struct image;

#define CSE_HEADER_MAKER 0x44504324 /* "DPC$" */

struct CsePartitionDirHeader {
uint32_t header_marker;
uint32_t nb_entries;
uint8_t header_version;
uint8_t entry_version;
uint8_t header_length;
uint8_t checksum;
uint8_t partition_name[4];
} __attribute__((packed));

struct CsePartitionDirEntry {
uint8_t entry_name[12];
uint32_t offset;
uint32_t length;
uint32_t reserved;
} __attribute__((packed));

void ri_cse_create(struct image *image);

#endif
70 changes: 70 additions & 0 deletions rimage/css.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2017, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
* Keyon Jie <yang.jie@linux.intel.com>
*/

#include <stdio.h>
#include <sys/time.h>
#include "rimage.h"
#include "css.h"
#include "manifest.h"

void ri_css_hdr_create(struct image *image)
{
struct css_header *css = image->fw_image + MAN_CSS_HDR_OFFSET;
struct tm *date;
struct timeval tv;
int val;

fprintf(stdout, " cse: completing CSS manifest\n");

/* get local time and date */
gettimeofday(&tv, NULL);
date = localtime(&tv.tv_sec);
date->tm_year += 1900;
fprintf(stdout, " css: set build date to %d:%2.2d:%2.2d\n",
date->tm_year, date->tm_mon, date->tm_mday);

/* year yYyy */
val = date->tm_year / 1000;
css->date |= val << 28;
date->tm_year -= val * 1000;
/* year yyYy */
val = date->tm_year / 100;
css->date |= val << 24;
date->tm_year -= val * 100;
/* year yyyY */
val = date->tm_year / 10;
css->date |= val << 20;
date->tm_year -= val * 10;
/* year Yyyy */
val = date->tm_year;
css->date |= val << 16;

/* month Mm - for some reason month starts at 0 */
val = ++date->tm_mon / 10;
css->date |= val << 12;
date->tm_mon -= (val * 10);
/* month mM */
val = date->tm_mon;
css->date |= val << 8;

/* Day Dd */
val = date->tm_mday / 10;
css->date |= val << 4;
date->tm_mday -= (val * 10);
/* Day dD */
val = date->tm_mday;
css->date |= val << 0;
}
69 changes: 69 additions & 0 deletions rimage/css.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2017, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/

#ifndef __CSS_H__
#define __CSS_H__

#include <stdint.h>

struct image;

#define MAN_CSS_MOD_TYPE 4
#define MAN_CSS_HDR_SIZE 161 /* in words */
#define MAN_CSS_HDR_VERSION 0x10000
#define MAN_CSS_MOD_VENDOR 0x8086
#define MAN_CSS_HDR_ID {'$', 'M', 'N', '2'}

#define MAN_CSS_KEY_SIZE (MAN_RSA_KEY_MODULUS_LEN >> 2)
#define MAN_CSS_MOD_SIZE (MAN_RSA_KEY_MODULUS_LEN >> 2)
#define MAN_CSS_EXP_SIZE (MAN_RSA_KEY_EXPONENT_LEN >> 2)
#define MAN_CSS_MAN_SIZE \
(sizeof(struct fw_image_manifest) >> 2)

/*
* RSA Key and Crypto
*/
#define MAN_RSA_KEY_MODULUS_LEN 256
#define MAN_RSA_KEY_EXPONENT_LEN 4
#define MAN_RSA_SIGNATURE_LEN 256

struct fw_version {
uint16_t major_version;
uint16_t minor_version;
uint16_t hotfix_version;
uint16_t build_version;
} __attribute__((packed));

struct css_header {
uint32_t header_type;
uint32_t header_len;
uint32_t header_version;
uint32_t reserved0; /* must be 0x0 */
uint32_t module_vendor;
uint32_t date;
uint32_t size;
uint8_t header_id[4];
uint32_t padding; /* must be 0x0 */
struct fw_version version;
uint32_t svn;
uint32_t reserved1[18]; /* must be 0x0 */
uint32_t modulus_size;
uint32_t exponent_size;
uint8_t modulus[MAN_RSA_KEY_MODULUS_LEN];
uint8_t exponent[MAN_RSA_KEY_EXPONENT_LEN];
uint8_t signature[MAN_RSA_SIGNATURE_LEN];
} __attribute__((packed));

void ri_css_hdr_create(struct image *image);

#endif
Loading

0 comments on commit 05ef434

Please sign in to comment.