Repository navigation
ASoC: SOF: add ext manifest 4 firmware support #3176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I did. See snd_sof_fw_ext_man4_parse |
||
|
|
||
| /********************************************************************* | ||
| * extended manifest | ||
| *------------------- | ||
| * css_manifest hdr | ||
|
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; | ||
|
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; | ||
|
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; | ||
|
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__ */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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 + | ||
|
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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I moved The reason I kept the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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); | ||
|
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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a module doesnt have a config, its
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bardliao can cfg_count be > 1?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, and the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But here is inside a for loop. So I don't think I can move
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ranj063. I did some change, could you review it? Thanks.