1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm Peripheral Image Loader
5 * Copyright (C) 2016 Linaro Ltd
6 * Copyright (C) 2015 Sony Mobile Communications Inc
7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
10 #include <linux/device.h>
11 #include <linux/elf.h>
12 #include <linux/firmware.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/qcom_scm.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 #include <linux/soc/qcom/mdt_loader.h>
20 static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
22 if (phdr->p_type != PT_LOAD)
25 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
34 static ssize_t mdt_load_split_segment(void *ptr, const struct elf32_phdr *phdrs,
35 unsigned int segment, const char *fw_name,
38 const struct elf32_phdr *phdr = &phdrs[segment];
39 const struct firmware *seg_fw;
43 if (strlen(fw_name) < 4)
46 seg_name = kstrdup(fw_name, GFP_KERNEL);
50 sprintf(seg_name + strlen(fw_name) - 3, "b%02d", segment);
51 ret = request_firmware_into_buf(&seg_fw, seg_name, dev,
54 dev_err(dev, "error %zd loading %s\n", ret, seg_name);
59 if (seg_fw->size != phdr->p_filesz) {
61 "failed to load segment %d from truncated file %s\n",
66 release_firmware(seg_fw);
73 * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
74 * @fw: firmware object for the mdt file
76 * Returns size of the loaded firmware blob, or -EINVAL on failure.
78 ssize_t qcom_mdt_get_size(const struct firmware *fw)
80 const struct elf32_phdr *phdrs;
81 const struct elf32_phdr *phdr;
82 const struct elf32_hdr *ehdr;
83 phys_addr_t min_addr = PHYS_ADDR_MAX;
84 phys_addr_t max_addr = 0;
87 ehdr = (struct elf32_hdr *)fw->data;
88 phdrs = (struct elf32_phdr *)(ehdr + 1);
90 for (i = 0; i < ehdr->e_phnum; i++) {
93 if (!mdt_phdr_valid(phdr))
96 if (phdr->p_paddr < min_addr)
97 min_addr = phdr->p_paddr;
99 if (phdr->p_paddr + phdr->p_memsz > max_addr)
100 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
103 return min_addr < max_addr ? max_addr - min_addr : -EINVAL;
105 EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
108 * qcom_mdt_read_metadata() - read header and metadata from mdt or mbn
109 * @fw: firmware of mdt header or mbn
110 * @data_len: length of the read metadata blob
111 * @fw_name: name of the firmware, for construction of segment file names
112 * @dev: device handle to associate resources with
114 * The mechanism that performs the authentication of the loading firmware
115 * expects an ELF header directly followed by the segment of hashes, with no
116 * padding inbetween. This function allocates a chunk of memory for this pair
117 * and copy the two pieces into the buffer.
119 * In the case of split firmware the hash is found directly following the ELF
120 * header, rather than at p_offset described by the second program header.
122 * The caller is responsible to free (kfree()) the returned pointer.
124 * Return: pointer to data, or ERR_PTR()
126 void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
127 const char *fw_name, struct device *dev)
129 const struct elf32_phdr *phdrs;
130 const struct elf32_hdr *ehdr;
131 unsigned int hash_segment = 0;
139 ehdr = (struct elf32_hdr *)fw->data;
140 phdrs = (struct elf32_phdr *)(ehdr + 1);
142 if (ehdr->e_phnum < 2)
143 return ERR_PTR(-EINVAL);
145 if (phdrs[0].p_type == PT_LOAD)
146 return ERR_PTR(-EINVAL);
148 for (i = 1; i < ehdr->e_phnum; i++) {
149 if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) {
156 dev_err(dev, "no hash segment found in %s\n", fw_name);
157 return ERR_PTR(-EINVAL);
160 ehdr_size = phdrs[0].p_filesz;
161 hash_size = phdrs[hash_segment].p_filesz;
163 data = kmalloc(ehdr_size + hash_size, GFP_KERNEL);
165 return ERR_PTR(-ENOMEM);
167 /* Copy ELF header */
168 memcpy(data, fw->data, ehdr_size);
170 if (ehdr_size + hash_size == fw->size) {
171 /* Firmware is split and hash is packed following the ELF header */
172 hash_offset = phdrs[0].p_filesz;
173 memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
174 } else if (phdrs[hash_segment].p_offset + hash_size <= fw->size) {
175 /* Hash is in its own segment, but within the loaded file */
176 hash_offset = phdrs[hash_segment].p_offset;
177 memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
179 /* Hash is in its own segment, beyond the loaded file */
180 ret = mdt_load_split_segment(data + ehdr_size, phdrs, hash_segment, fw_name, dev);
187 *data_len = ehdr_size + hash_size;
191 EXPORT_SYMBOL_GPL(qcom_mdt_read_metadata);
194 * qcom_mdt_pas_init() - initialize PAS region for firmware loading
195 * @dev: device handle to associate resources with
196 * @fw: firmware object for the mdt file
197 * @fw_name: name of the firmware, for construction of segment file names
198 * @pas_id: PAS identifier
199 * @mem_phys: physical address of allocated memory region
200 * @ctx: PAS metadata context, to be released by caller
202 * Returns 0 on success, negative errno otherwise.
204 int qcom_mdt_pas_init(struct device *dev, const struct firmware *fw,
205 const char *fw_name, int pas_id, phys_addr_t mem_phys,
206 struct qcom_scm_pas_metadata *ctx)
208 const struct elf32_phdr *phdrs;
209 const struct elf32_phdr *phdr;
210 const struct elf32_hdr *ehdr;
211 phys_addr_t min_addr = PHYS_ADDR_MAX;
212 phys_addr_t max_addr = 0;
218 ehdr = (struct elf32_hdr *)fw->data;
219 phdrs = (struct elf32_phdr *)(ehdr + 1);
221 for (i = 0; i < ehdr->e_phnum; i++) {
224 if (!mdt_phdr_valid(phdr))
227 if (phdr->p_paddr < min_addr)
228 min_addr = phdr->p_paddr;
230 if (phdr->p_paddr + phdr->p_memsz > max_addr)
231 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
234 metadata = qcom_mdt_read_metadata(fw, &metadata_len, fw_name, dev);
235 if (IS_ERR(metadata)) {
236 ret = PTR_ERR(metadata);
237 dev_err(dev, "error %d reading firmware %s metadata\n", ret, fw_name);
241 ret = qcom_scm_pas_init_image(pas_id, metadata, metadata_len, ctx);
244 /* Invalid firmware metadata */
245 dev_err(dev, "error %d initializing firmware %s\n", ret, fw_name);
249 ret = qcom_scm_pas_mem_setup(pas_id, mem_phys, max_addr - min_addr);
251 /* Unable to set up relocation */
252 dev_err(dev, "error %d setting up firmware %s\n", ret, fw_name);
259 EXPORT_SYMBOL_GPL(qcom_mdt_pas_init);
261 static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
262 const char *fw_name, int pas_id, void *mem_region,
263 phys_addr_t mem_phys, size_t mem_size,
264 phys_addr_t *reloc_base, bool pas_init)
266 const struct elf32_phdr *phdrs;
267 const struct elf32_phdr *phdr;
268 const struct elf32_hdr *ehdr;
269 phys_addr_t mem_reloc;
270 phys_addr_t min_addr = PHYS_ADDR_MAX;
272 bool relocate = false;
277 if (!fw || !mem_region || !mem_phys || !mem_size)
280 ehdr = (struct elf32_hdr *)fw->data;
281 phdrs = (struct elf32_phdr *)(ehdr + 1);
283 for (i = 0; i < ehdr->e_phnum; i++) {
286 if (!mdt_phdr_valid(phdr))
289 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
292 if (phdr->p_paddr < min_addr)
293 min_addr = phdr->p_paddr;
298 * The image is relocatable, so offset each segment based on
299 * the lowest segment address.
301 mem_reloc = min_addr;
304 * Image is not relocatable, so offset each segment based on
305 * the allocated physical chunk of memory.
307 mem_reloc = mem_phys;
310 for (i = 0; i < ehdr->e_phnum; i++) {
313 if (!mdt_phdr_valid(phdr))
316 offset = phdr->p_paddr - mem_reloc;
317 if (offset < 0 || offset + phdr->p_memsz > mem_size) {
318 dev_err(dev, "segment outside memory range\n");
323 if (phdr->p_filesz > phdr->p_memsz) {
325 "refusing to load segment %d with p_filesz > p_memsz\n",
331 ptr = mem_region + offset;
333 if (phdr->p_filesz && phdr->p_offset < fw->size &&
334 phdr->p_offset + phdr->p_filesz <= fw->size) {
335 /* Firmware is large enough to be non-split */
336 if (phdr->p_offset + phdr->p_filesz > fw->size) {
337 dev_err(dev, "file %s segment %d would be truncated\n",
343 memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
344 } else if (phdr->p_filesz) {
345 /* Firmware not large enough, load split-out segments */
346 ret = mdt_load_split_segment(ptr, phdrs, i, fw_name, dev);
351 if (phdr->p_memsz > phdr->p_filesz)
352 memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
356 *reloc_base = mem_reloc;
362 * qcom_mdt_load() - load the firmware which header is loaded as fw
363 * @dev: device handle to associate resources with
364 * @fw: firmware object for the mdt file
365 * @firmware: name of the firmware, for construction of segment file names
366 * @pas_id: PAS identifier
367 * @mem_region: allocated memory region to load firmware into
368 * @mem_phys: physical address of allocated memory region
369 * @mem_size: size of the allocated memory region
370 * @reloc_base: adjusted physical address after relocation
372 * Returns 0 on success, negative errno otherwise.
374 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
375 const char *firmware, int pas_id, void *mem_region,
376 phys_addr_t mem_phys, size_t mem_size,
377 phys_addr_t *reloc_base)
381 ret = qcom_mdt_pas_init(dev, fw, firmware, pas_id, mem_phys, NULL);
385 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
386 mem_size, reloc_base, true);
388 EXPORT_SYMBOL_GPL(qcom_mdt_load);
391 * qcom_mdt_load_no_init() - load the firmware which header is loaded as fw
392 * @dev: device handle to associate resources with
393 * @fw: firmware object for the mdt file
394 * @firmware: name of the firmware, for construction of segment file names
395 * @pas_id: PAS identifier
396 * @mem_region: allocated memory region to load firmware into
397 * @mem_phys: physical address of allocated memory region
398 * @mem_size: size of the allocated memory region
399 * @reloc_base: adjusted physical address after relocation
401 * Returns 0 on success, negative errno otherwise.
403 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
404 const char *firmware, int pas_id,
405 void *mem_region, phys_addr_t mem_phys,
406 size_t mem_size, phys_addr_t *reloc_base)
408 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
409 mem_size, reloc_base, false);
411 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
413 MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
414 MODULE_LICENSE("GPL v2");