Skip to content

Commit

Permalink
soc: qcom: mdt_loader: Enhance split binary detection
Browse files Browse the repository at this point in the history
When booting with split binaries, it may be that the offset of the first
program header lies inside the mdt's filesize, in this case the loader
would incorrectly assume that the bins were not split. The loading would
then continue on and fail for split bins. This change updates the logic used
by the mdt loader to understand whether the firmware images are split or not
by checking if each programs header's segment lies within the file or not.

Signed-off-by: Gokul Krishna Krishnakumar <quic_gokukris@quicinc.com>
Signed-off-by: Melody Olvera <quic_molvera@quicinc.com>
  • Loading branch information
Gokul Krishna Krishnakumar authored and intel-lab-lkp committed Mar 6, 2023
1 parent ec20364 commit 3964310
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions drivers/soc/qcom/mdt_loader.c
Expand Up @@ -31,6 +31,26 @@ static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
return true;
}

static bool qcom_mdt_bins_are_split(const struct firmware *fw)
{
const struct elf32_phdr *phdrs;
const struct elf32_hdr *ehdr;
uint64_t seg_start, seg_end;
int i;

ehdr = (struct elf32_hdr *)fw->data;
phdrs = (struct elf32_phdr *)(ehdr + 1);

for (i = 0; i < ehdr->e_phnum; i++) {
seg_start = phdrs[i].p_offset;
seg_end = phdrs[i].p_offset + phdrs[i].p_filesz;
if (seg_start > fw->size || seg_end > fw->size)
return true;
}

return false;
}

static ssize_t mdt_load_split_segment(void *ptr, const struct elf32_phdr *phdrs,
unsigned int segment, const char *fw_name,
struct device *dev)
Expand Down Expand Up @@ -167,23 +187,13 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
/* Copy ELF header */
memcpy(data, fw->data, ehdr_size);

if (ehdr_size + hash_size == fw->size) {
/* Firmware is split and hash is packed following the ELF header */
hash_offset = phdrs[0].p_filesz;
memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
} else if (phdrs[hash_segment].p_offset + hash_size <= fw->size) {
/* Hash is in its own segment, but within the loaded file */

if (qcom_mdt_bins_are_split(fw)) {
ret = mdt_load_split_segment(data + ehdr_size, phdrs, hash_segment, fw_name, dev);
} else {
hash_offset = phdrs[hash_segment].p_offset;
memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
} else {
/* Hash is in its own segment, beyond the loaded file */
ret = mdt_load_split_segment(data + ehdr_size, phdrs, hash_segment, fw_name, dev);
if (ret) {
kfree(data);
return ERR_PTR(ret);
}
}

*data_len = ehdr_size + hash_size;

return data;
Expand Down Expand Up @@ -270,13 +280,15 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
phys_addr_t min_addr = PHYS_ADDR_MAX;
ssize_t offset;
bool relocate = false;
bool is_split;
void *ptr;
int ret = 0;
int i;

if (!fw || !mem_region || !mem_phys || !mem_size)
return -EINVAL;

is_split = qcom_mdt_bins_are_split(fw);
ehdr = (struct elf32_hdr *)fw->data;
phdrs = (struct elf32_phdr *)(ehdr + 1);

Expand Down Expand Up @@ -330,22 +342,16 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,

ptr = mem_region + offset;

if (phdr->p_filesz && phdr->p_offset < fw->size &&
phdr->p_offset + phdr->p_filesz <= fw->size) {
/* Firmware is large enough to be non-split */
if (phdr->p_offset + phdr->p_filesz > fw->size) {
dev_err(dev, "file %s segment %d would be truncated\n",
fw_name, i);
ret = -EINVAL;
break;
if (phdr->p_filesz) {
if (!is_split) {
/* Firmware is large enough to be non-split */
memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
} else {
/* Firmware not large enough, load split-out segments */
ret = mdt_load_split_segment(ptr, phdrs, i, fw_name, dev);
if (ret)
break;
}

memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
} else if (phdr->p_filesz) {
/* Firmware not large enough, load split-out segments */
ret = mdt_load_split_segment(ptr, phdrs, i, fw_name, dev);
if (ret)
break;
}

if (phdr->p_memsz > phdr->p_filesz)
Expand Down

0 comments on commit 3964310

Please sign in to comment.