soc: qcom: mdt_loader: Handle split bins correctly

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 to fail for split bins.
This change updates the logic used by the mdt loader to understand whether
the firmware images are split or not. It figures this out by checking if
each programs header's segment lies within the file or not.

Change-Id: I18ce4922eadceb96193584d829829878048ea045
Signed-off-by: Siddharth Gupta <[email protected]>
This commit is contained in:
Siddharth Gupta 2020-12-15 17:38:23 -08:00 • committed by Nick Reuter
commit a748d0a179

View file

@ -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;
}
/**
* qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
* @fw: firmware object for the mdt file
@ -128,7 +148,7 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len)
return ERR_PTR(-ENOMEM);
/* Is the header and hash already packed */
if (ehdr_size + hash_size == fw->size)
if (qcom_mdt_bins_are_split(fw))
hash_offset = phdrs[0].p_filesz;
else
hash_offset = phdrs[hash_index].p_offset;
@ -160,6 +180,7 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
void *metadata;
char *fw_name;
bool relocate = false;
bool is_split;
void *ptr;
int ret = 0;
int i;
@ -167,6 +188,7 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
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);
@ -256,37 +278,31 @@ 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) {
/* Firmware is large enough to be non-split */
if (phdr->p_offset + phdr->p_filesz > fw->size) {
dev_err(dev,
"failed to load segment %d from truncated file %s\n",
i, firmware);
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 */
snprintf(fw_name + fw_name_len - 3, 4, "b%02d", i);
ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
ptr, phdr->p_filesz);
if (ret) {
dev_err(dev, "failed to load %s\n", fw_name);
break;
}
memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
} else if (phdr->p_filesz) {
/* Firmware not large enough, load split-out segments */
sprintf(fw_name + fw_name_len - 3, "b%02d", i);
ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
ptr, phdr->p_filesz);
if (ret) {
dev_err(dev, "failed to load %s\n", fw_name);
break;
}
if (seg_fw->size != phdr->p_filesz) {
dev_err(dev,
"failed to load segment %d from truncated file %s\n",
i, fw_name);
release_firmware(seg_fw);
ret = -EINVAL;
break;
}
if (seg_fw->size != phdr->p_filesz) {
dev_err(dev,
"failed to load segment %d from truncated file %s\n",
i, fw_name);
release_firmware(seg_fw);
ret = -EINVAL;
break;
}
release_firmware(seg_fw);
}
if (phdr->p_memsz > phdr->p_filesz)