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
7 changes: 7 additions & 0 deletions include/sound/sof.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ struct snd_sof_pdata {
void *hw_pdata;
};

struct snd_sof_ext_man_pdata {
int default_ops_index;
u32 fw_hdr_offset;
};

/*
* Descriptor used for setting up SOF platform data. This is used when
* ACPI/PCI data is missing or mapped differently.
Expand Down Expand Up @@ -115,6 +120,8 @@ struct sof_dev_desc {
const char *default_fw_filename;

const struct snd_sof_dsp_ops *ops;

const struct snd_sof_ext_man_pdata *ext_man_pdata;
};

int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd);
Expand Down
122 changes: 122 additions & 0 deletions include/sound/sof/ext_manifest4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
/*
* This file is provided under a dual BSD/GPLv2 license. When using or
* redistributing this file, you may do so under either license.
*
* Copyright(c) 2021 Intel Corporation. All rights reserved.
*/

/*
* 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.
* Note that not all items in the struct are 32 bits length, and we assume
* the firmware is little-endian.
*/

#ifndef __SOF_FIRMWARE_EXT_MANIFEST4_H__
#define __SOF_FIRMWARE_EXT_MANIFEST4_H__

#include <linux/uuid.h>

/* In ASCII $AE1 */
#define SOF_MAN4_MAGIC_NUMBER 0x31454124

#define SOF_MAN4_MAX_MODULE_NAME_LEN 8
#define SOF_MAN4_MAX_FW_BINARY_NAME 8
#define SOF_MAN4_DEFAULT_HASH_SHA256_LEN 32
#define SOF_MAN4_SEGMENT_NUM 3
#define SOF_MAN4_MODULE_PARAMS_NUM 4
#define SOF_MAN4_FW_HDR_OFFSET 0x2000

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao the FW_HDR_OFFSET is not the same for cavs1.5 abd cavs1.8. Can we please abstract this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao the FW_HDR_OFFSET is not the same for cavs1.5 abd cavs1.8. Can we please abstract this?

Thanks @ranj063. I did some change, could you review it? Thanks.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao thanks but you didnt use the struct defined in the first patch?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao thanks but you didnt use the struct defined in the first patch?

Yes, I did. See snd_sof_fw_ext_man4_parse

static int snd_sof_fw_ext_man4_parse(struct snd_sof_dev *sdev, const struct firmware *fw)
{
	struct snd_sof_pdata *sof_pdata = sdev->pdata;
	const struct sof_dev_desc *desc = sof_pdata->desc;
	const struct snd_sof_ext_man_pdata *ext_man_pdata = desc->ext_man_pdata;
...
	if (ext_man_pdata)
		fw_hdr_offset = ext_man_pdata->fw_hdr_offset;
	else
		fw_hdr_offset = SOF_MAN4_FW_HDR_OFFSET;
...
}


/*********************************************************************
* extended manifest
*-------------------
* css_manifest hdr
Comment thread
ranj063 marked this conversation as resolved.
Resolved
*-------------------
* offset reserved for future
*-------------------
* fw_hdr
*-------------------
* module_entry[0]
*-------------------
* module_entry[1]
*-------------------
* ...
*-------------------
* module_entry[n]
*-------------------
* module_config[0]
*-------------------
* module_config[1]
*-------------------
* ...
*-------------------
* module_config[m]
*-------------------
* FW content
*-------------------
*********************************************************************/

struct sof_ext_manifest4_hdr {
u32 id;
u32 len; /* length of extension manifest */
u16 version_major;
u16 version_minor;
Comment thread
plbossart marked this conversation as resolved.
Resolved
u32 entries;
} __packed;

struct sof_man4_fw_binary_header {
/* This part must be unchanged to be backward compatible with SPT-LP ROM */
uint32_t id;
uint32_t len; /* sizeof(sof_man4_fw_binary_header) in bytes */
uint8_t name[SOF_MAN4_MAX_FW_BINARY_NAME];
uint32_t preload_page_count; /* number of pages of preloaded image */
uint32_t fw_image_flags;
uint32_t feature_mask;
uint16_t major_version;
uint16_t minor_version;
uint16_t hotfix_version;
uint16_t build_version;
Comment thread
plbossart marked this conversation as resolved.
Resolved
uint32_t num_module_entries;

/* This part may change to contain any additional data for BaseFw that is skipped by ROM */
uint32_t hw_buf_base_addr;
uint32_t hw_buf_length;
uint32_t load_offset; /* This value is used by ROM */
} __packed;

struct sof_man4_segment_desc {
uint32_t flags;
uint32_t v_base_addr;
uint32_t file_offset;
} __packed;

struct sof_man4_module {
uint32_t id;
uint8_t name[SOF_MAN4_MAX_MODULE_NAME_LEN];
uint8_t uuid[UUID_SIZE];
uint32_t type;
uint8_t hash[SOF_MAN4_DEFAULT_HASH_SHA256_LEN];
uint32_t entry_point;
uint16_t cfg_offset;
uint16_t cfg_count;
uint32_t affinity_mask;
uint16_t instance_max_count;
uint16_t instance_stack_size;
Comment thread
plbossart marked this conversation as resolved.
Resolved
struct sof_man4_segment_desc segments[SOF_MAN4_SEGMENT_NUM];
} __packed;

struct sof_man4_module_config {
uint32_t par[SOF_MAN4_MODULE_PARAMS_NUM]; /* module parameters */
uint32_t is_bytes; /* actual size of instance .bss (bytes) */
uint32_t cps; /* cycles per second */
uint32_t ibs; /* input buffer size (bytes) */
uint32_t obs; /* output buffer size (bytes) */
uint32_t module_flags; /* flags, reserved for future use */
uint32_t cpc; /* max cycles per scheduling period */
uint32_t obls; /* output block size, reserved for future use */
} __packed;

#endif /* __SOF_FIRMWARE_EXT_MANIFEST4_H__ */
109 changes: 107 additions & 2 deletions sound/soc/sof/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
// Generic firmware loader.
//

#include <linux/bitops.h>
#include <linux/firmware.h>
#include <sound/sof.h>
#include <sound/sof/ext_manifest.h>
#include <sound/sof/ext_manifest4.h>
#include "sof-priv.h"
#include "ops.h"

Expand Down Expand Up @@ -272,8 +274,8 @@ static ssize_t snd_sof_ext_man_size(const struct firmware *fw)
}

/* parse extended FW manifest data structures */
static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev,
const struct firmware *fw)
static int snd_sof_fw_ext_man3_parse(struct snd_sof_dev *sdev,
const struct firmware *fw)
{
const struct sof_ext_man_elem_header *elem_hdr;
const struct sof_ext_man_header *head;
Expand Down Expand Up @@ -358,6 +360,109 @@ static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev,
return ext_man_size;
}

static int snd_sof_fw_ext_man4_parse(struct snd_sof_dev *sdev, const struct firmware *fw)
{
struct snd_sof_pdata *sof_pdata = sdev->pdata;
const struct sof_dev_desc *desc = sof_pdata->desc;
const struct snd_sof_ext_man_pdata *ext_man_pdata = desc->ext_man_pdata;
struct sof_ipc4_fw_modules *module_entry;
struct sof_man4_fw_binary_header *fw_header;
struct sof_ext_manifest4_hdr *hdr;
struct sof_man4_module_config *fm_config;
struct sof_man4_module *fm_entry;
ssize_t remaining;
u32 fw_hdr_offset;
int i;

remaining = fw->size;
if (remaining <= sizeof(*hdr)) {
dev_err(sdev->dev, "Invalid fw size\n");
return -EINVAL;
}

hdr = (struct sof_ext_manifest4_hdr *)fw->data;

if (ext_man_pdata)
fw_hdr_offset = ext_man_pdata->fw_hdr_offset;
else
fw_hdr_offset = SOF_MAN4_FW_HDR_OFFSET;

if (remaining <= hdr->len + fw_hdr_offset + sizeof(*fw_header)) {
dev_err(sdev->dev, "Invalid hdr->len %d\n", hdr->len);
return -EINVAL;
}

fw_header = (struct sof_man4_fw_binary_header *)(fw->data + hdr->len +
Comment thread
kv2019i marked this conversation as resolved.
Resolved
fw_hdr_offset);
remaining -= (hdr->len + fw_hdr_offset);

if (remaining <= fw_header->len) {
dev_err(sdev->dev, "Invalid fw_header->len %d\n", fw_header->len);
return -EINVAL;
}

dev_dbg(sdev->dev, " fw %s: header length %x, module num %d", fw_header->name,
fw_header->len, fw_header->num_module_entries);

sdev->fw_modules = devm_kmalloc_array(sdev->dev, fw_header->num_module_entries,
sizeof(*module_entry), GFP_KERNEL);
if (!sdev->fw_modules)
return -ENOMEM;

sdev->fw_module_num = fw_header->num_module_entries;
module_entry = sdev->fw_modules;

fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need fm_entry here yet, so this could be before L415. This is really the first place you need fm_entry. This will not impact the sizeof calculations (L401 et al, this just need to know the type).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao , can you address @kv2019i's comment?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao , can you address @kv2019i's comment?

Yes, I moved fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len); and the remaining calculation right before fm_config = (struct sof_man4_module_config *)(fm_entry + fw_header->num_module_entries);. Or are you suggesting that moving fm_entry only but not the remaining calculation?
Like

        remaining -= fw_header->len;

        if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
                dev_err(sdev->dev, "Invalid num_module_entries %d\n",
                        fw_header->num_module_entries);
                return -EINVAL;
        }
        fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);
        fm_config = (struct sof_man4_module_config *)(fm_entry + fw_header->num_module_entries);

The reason I kept the remaining calculation right after pointer assignments was that I think it is more readable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with this. Strictly speaking, you only consume/access the data when the pointer is dereferences (at L415), so in that sense more correctly place to adjust remaining would be L416. But I think this is now close enough and may be easier to read.

remaining -= fw_header->len;
Comment thread
bardliao marked this conversation as resolved.
Resolved

if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
dev_err(sdev->dev, "Invalid num_module_entries %d\n",
fw_header->num_module_entries);
return -EINVAL;
}

fm_config = (struct sof_man4_module_config *)(fm_entry + fw_header->num_module_entries);
Comment thread
ranj063 marked this conversation as resolved.
Resolved
remaining -= (fw_header->num_module_entries * sizeof(*fm_entry));
for (i = 0; i < fw_header->num_module_entries; i++) {
dev_dbg(sdev->dev, "module %s : UUID %pUL, ", fm_entry->name,
fm_entry->uuid);

memcpy(&module_entry->man4_module_entry, fm_entry, sizeof(*fm_entry));

if (fm_entry->cfg_count) {
if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) *
sizeof(*fm_config)) {
dev_err(sdev->dev, "Invalid cfg_offset %d\n", fm_entry->cfg_offset);
return -EINVAL;
}

module_entry->bss_size = fm_config[fm_entry->cfg_offset].is_bytes;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"fm_entry->cfg_offset" needs to be validated before use. We have no guarantees that a valid FW (and header) is supplied.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a quick question @bardliao . if a module doesnt have a config in the manifest, what is its cfg_offset? If it is 0, wouldnt this be incorrect? you'd be copying the bss_size from another module's config possibly?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a quick question @bardliao . if a module doesnt have a config in the manifest, what is its cfg_offset? If it is 0, wouldnt this be incorrect? you'd be copying the bss_size from another module's config possibly?

If a module doesnt have a config, its cfg_count will be 0. So, it will not pass the if (fm_entry->cfg_count) test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao can cfg_count be > 1?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao can cfg_count be > 1?

Yes, and the is_bytes field will be the same for all of the fm_configs belong to the fm_entry.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao You don't really need "fm_config" until this line. So you could move the init of fm_config from L407 to here. For readability, having the space check together with use of the variable, is easier to follow (and verify).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bardliao You don't really need "fm_config" until this line. So you could move the init of fm_config from L407 to here. For readability, having the space check together with use of the variable, is easier to follow (and verify).

But here is inside a for loop. So I don't think I can move remaining -= (fw_header->num_module_entries * sizeof(*fm_entry)); here. And it will be less readable if we put fm_config = (struct sof_man4_module_config *)(fm_entry + fw_header->num_module_entries); and remaining -= (fw_header->num_module_entries * sizeof(*fm_entry)); on two places.
So maybe move those two lines right before the for loop?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct @bardliao , keep fm_config init in the original place.

This segment is difficult to follow as each fm_entry will also access fm_config, so you are consuming data from two places in the binary in this loop. So it's not so clear where to actually modify remaining as you are going back and forth in the blob. And you can do all the checks outside the loop either as you need to parse each fm_entry to see how many fm_config entries is has.

I think the current code is probably the best compromise.

}

module_entry->man4_module_entry.id = i;

module_entry++;
fm_entry++;
}

return hdr->len;
}

static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev,
const struct firmware *fw)
{
const struct sof_ext_manifest4_hdr *hdr = (struct sof_ext_manifest4_hdr *)fw->data;

/*
* The fw size should be at least larger than the size of *hdr, and then
* we can check the hdr->id to see if it is a manifest4 firmware.
*/
if (fw->size > sizeof(*hdr) && hdr->id == SOF_MAN4_MAGIC_NUMBER)
return snd_sof_fw_ext_man4_parse(sdev, fw);

return snd_sof_fw_ext_man3_parse(sdev, fw);
}

/*
* IPC Firmware ready.
*/
Expand Down
11 changes: 11 additions & 0 deletions sound/soc/sof/sof-priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sound/sof/trace.h>
#include <uapi/sound/sof/fw.h>
#include <sound/sof/ext_manifest.h>
#include <sound/sof/ext_manifest4.h>
#include <uapi/sound/sof/abi.h>

/* Flag definitions used in sof_core_debug (sof_debug module parameter) */
Expand Down Expand Up @@ -392,6 +393,12 @@ struct snd_sof_ipc {
struct snd_sof_ipc_msg msg;
};

struct sof_ipc4_fw_modules {
struct sof_man4_module man4_module_entry;
u32 bss_size;
void *private;
};

/*
* SOF Device Level.
*/
Expand Down Expand Up @@ -468,6 +475,10 @@ struct snd_sof_dev {
/* FW configuration */
struct sof_ipc_window *info_window;

/* FW module info */
int fw_module_num;
void *fw_modules;

/* IPC timeouts in ms */
int ipc_timeout;
int boot_timeout;
Expand Down