Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rimage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_executable(rimage
hash.c
pkcs1_5.c
manifest.c
ext_manifest.c
elf.c
rimage.c
)
Expand Down
6 changes: 3 additions & 3 deletions rimage/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,11 @@ int elf_validate_modules(struct image *image)
return 0;
}

int elf_find_section(struct image *image, struct module *module,
int elf_find_section(const struct image *image, const struct module *module,
const char *name)
{
Elf32_Ehdr *hdr = &module->hdr;
Elf32_Shdr *section, *s;
const Elf32_Ehdr *hdr = &module->hdr;
const Elf32_Shdr *section, *s;
char *buffer;
size_t count;
int ret, i;
Expand Down
195 changes: 195 additions & 0 deletions rimage/ext_manifest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "kernel/ext_manifest.h"
#include "ext_manifest.h"
#include "ipc/info.h"
#include "rimage.h"

const struct ext_man_header ext_man_template = {
.magic = EXT_MAN_MAGIC_NUMBER,
.header_version = EXT_MAN_VERSION,
.header_size = sizeof(struct ext_man_header),
.full_size = 0, /* runtime variable */
};

static int ext_man_open_file(struct image *image)
{
/* open extended manifest outfile for writing */
sprintf(image->out_ext_man_file, "%s.xman", image->out_file);
unlink(image->out_ext_man_file);

image->out_ext_man_fd = fopen(image->out_ext_man_file, "wb");
if (!image->out_ext_man_fd) {
fprintf(stderr, "error: unable to open %s for writing %d\n",
image->out_ext_man_file, errno);
return errno;
}

return 0;
}

static struct module *ext_man_get_module(struct image *image)
{
struct module *module;
int i;

for (i = image->num_modules > 1 ? 1 : 0; i < image->num_modules; i++) {
module = &image->module[i];

if (elf_find_section(image, module, ".fw_metadata") > 0)
return module;
}

return NULL;
}

static int ext_man_validate(uint32_t section_size, const void *section_data)
{
uint8_t *sbuf = (uint8_t *)section_data;
struct ext_man_elem_header head;
uint32_t offset = 0;

/* copy each head to local struct to omit memory align issues */
while (offset < section_size) {
memcpy(&head, &sbuf[offset], sizeof(head));
fprintf(stdout, "Extended manifest found module, type: 0x%04X size: 0x%04X (%4d) offset: 0x%04X\n",
head.type, head.elem_size, head.elem_size, offset);
if (head.elem_size == 0 || head.elem_size % EXT_MAN_ALIGN) {
fprintf(stderr,
"error: invalid extended manifest element size\n");
return -EINVAL;
}
offset += head.elem_size;
}

/* sum of packets size != section size */
if (offset != section_size) {
fprintf(stderr,
"error: fw_metadata section is inconsistent, section size: 0x%04X != 0x%04X sum of packets size\n",
section_size, offset);
return -EINVAL;
} else {
return 0;
}
}

static int ext_man_build(const struct image *image, const struct module *module,
struct ext_man_header **dst_buff)
{
struct ext_man_header ext_man;
const Elf32_Shdr *section;
uint8_t *buffer = NULL;
int fw_metadata_index;
size_t offset;
size_t read;
int ret = 0;

/* find .fw_metadata section */
fw_metadata_index = elf_find_section(image, module, ".fw_metadata");
if (fw_metadata_index < 0) {
fprintf(stderr, "error: unable to find .fw_metadata section: %d\n",
fw_metadata_index);
ret = fw_metadata_index;
goto out;
}
section = &module->section[fw_metadata_index];

/* fill ext_man, size aligned to 4 to avoid unaligned accesses */
memcpy(&ext_man, &ext_man_template, sizeof(struct ext_man_header));
ext_man.full_size = ext_man.header_size;
ext_man.full_size += section->size;
if (ext_man.full_size % 4) {
fprintf(stderr,
"error: extended manifest size must be aligned to 4\n");
ret = -EINVAL;
goto out;
}

/* alloc buffer for ext_man */
buffer = calloc(1, ext_man.full_size);
if (!buffer) {
ret = -ENOMEM;
goto out;
}

/* fill buffer with ext_man header and section content */
memcpy(buffer, &ext_man, ext_man.header_size);
offset = ext_man.header_size;

fseek(module->fd, section->off, SEEK_SET);
read = fread(buffer + offset, 1, section->size, module->fd);
if (read != section->size) {
fprintf(stderr,
"error: can't read fw_metadata section %d\n",
-errno);
free(buffer);
ret = -errno;
}

*dst_buff = (struct ext_man_header *)buffer;

out:
return ret;
}

int ext_man_write(struct image *image)
{
struct ext_man_header *ext_man = NULL;
struct module *module;
int count;
int ret;

module = ext_man_get_module(image);
if (!module) {
fprintf(stderr, "error: firmware metadata section not found\n");
ret = -EINVAL;
goto out;
}

ret = ext_man_open_file(image);
if (ret)
goto out;

ret = ext_man_build(image, module, &ext_man);
if (ret)
goto out;

/* validate metadata section */
ret = ext_man_validate(ext_man->full_size - ext_man->header_size,
(char *)ext_man + ext_man->header_size);
if (ret) {
ret = -errno;
goto out;
}

/* write extended metadata to file */
count = fwrite(ext_man, 1, ext_man->full_size, image->out_ext_man_fd);

if (count != ext_man->full_size) {
fprintf(stderr,
"error: can't write extended manifest to file %d\n",
-errno);
ret = -errno;
goto out;
}

fprintf(stdout, "Extended manifest saved to file %s size 0x%04X (%d) bytes\n\n",
image->out_ext_man_file, ext_man->full_size,
ext_man->full_size);

out:
if (ext_man)
free(ext_man);
if (image->out_ext_man_fd)
fclose(image->out_ext_man_fd);
return ret;
}
34 changes: 34 additions & 0 deletions rimage/ext_manifest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2020 Intel Corporation. All rights reserved.
*
* Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
*/

/*
* Extended manifest is a place to store metadata about firmware, known during
* compilation time - for example firmware version or used compiler.
* Given information are read on host side before firmware startup.
* This part of output binary is not signed.
*
* To add new content to ext_man, in firmware code define struct which starts
* with ext_man_elem_head followed by usage dependent content and place whole
* struct in "fw_metadata" section. Moreover kernel code should be modified to
* properly read new packet.
*
* Extended manifest designed to be extensible. In header there is a field which
* describe header length, so after appending some data to header then it can be
* easily skipped by device with older version of this header.
* From other side, unknown ext_man elements should be just skipped by host,
* to be backward compatible. Field ext_man_elem_header.elem_size should be
* used in such a situation.
*/

#ifndef __EXT_MAN_H__
#define __EXT_MAN_H__

#include "rimage.h"

int ext_man_write(struct image *image);

#endif /* __EXT_MAN_H__ */
6 changes: 6 additions & 0 deletions rimage/file_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,16 @@ static int simple_write_module(struct image *image, struct module *module)
if (!(module->section[i].flags & valid))
continue;

if (section->vaddr == 0)
continue;

/* dont write bss */
if (section->type == SHT_NOBITS)
continue;

if (section->type == SHT_NOBITS)
continue;

err = write_block(image, module, section);
if (err < 0) {
fprintf(stderr, "error: failed to write section #%d\n",
Expand Down
13 changes: 13 additions & 0 deletions rimage/rimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <errno.h>
#include <string.h>

#include "ext_manifest.h"
#include "rimage.h"
#include "file_format.h"
#include "manifest.h"
Expand Down Expand Up @@ -150,6 +151,18 @@ int main(int argc, char *argv[])
ret = image.adsp->write_firmware_meu(&image);
else
ret = image.adsp->write_firmware(&image);
if (ret) {
fprintf(stderr, "error: unable to write firmware, %d\n",
errno);
goto out;
}

ret = ext_man_write(&image);
if (ret) {
fprintf(stderr, "error: unable to write extended manifest, %d\n",
errno);
goto out;
}

out:
/* close files */
Expand Down
6 changes: 4 additions & 2 deletions rimage/rimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ struct image {
void *rom_image;
FILE *out_rom_fd;
FILE *out_man_fd;
FILE *out_ext_man_fd;
FILE *out_unsigned_fd;
char out_rom_file[256];
char out_man_file[256];
char out_ext_man_file[256];
char out_unsigned_file[256];
};

Expand Down Expand Up @@ -171,8 +173,8 @@ int elf_parse_module(struct image *image, int module_index, const char *name);
void elf_free_module(struct image *image, int module_index);
int elf_is_rom(struct image *image, Elf32_Shdr *section);
int elf_validate_modules(struct image *image);
int elf_find_section(struct image *image, struct module *module,
const char *name);
int elf_find_section(const struct image *image, const struct module *module,
const char *name);
int elf_validate_section(struct image *image, struct module *module,
Elf32_Shdr *section, int index);

Expand Down
18 changes: 17 additions & 1 deletion src/arch/xtensa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,27 @@ else()
add_custom_target(run_meu DEPENDS run_rimage)
endif()

if(CMAKE_HOST_WIN32)
set(GLUE_CMD copy /b sof-${fw_name}.ri.xman + sof-${fw_name}.ri sof-${fw_name}.ri.temp)
else()
set(GLUE_CMD cat sof-${fw_name}.ri.xman sof-${fw_name}.ri > sof-${fw_name}.ri.temp)
endif()

add_custom_target(
ext_man_glue ALL
COMMAND ${GLUE_CMD}
COMMAND ${CMAKE_COMMAND} -E copy sof-${fw_name}.ri.temp sof-${fw_name}.ri
COMMAND ${CMAKE_COMMAND} -E remove sof-${fw_name}.ri.temp
DEPENDS run_meu
VERBATIM
USES_TERMINAL
)

add_custom_target(
bin ALL
COMMAND ${CMAKE_COMMAND} -E copy sof-${fw_name}.ri ${PROJECT_BINARY_DIR}/sof-${fw_name}.ri
COMMAND ${CMAKE_COMMAND} -E copy sof-${fw_name}.ldc ${PROJECT_BINARY_DIR}/sof-${fw_name}.ldc
DEPENDS run_meu bin_extras
DEPENDS ext_man_glue bin_extras
VERBATIM
USES_TERMINAL
)
Expand Down
Loading