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)
35 * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
36 * @fw: firmware object for the mdt file
38 * Returns size of the loaded firmware blob, or -EINVAL on failure.
40 ssize_t qcom_mdt_get_size(const struct firmware *fw)
42 const struct elf32_phdr *phdrs;
43 const struct elf32_phdr *phdr;
44 const struct elf32_hdr *ehdr;
45 phys_addr_t min_addr = PHYS_ADDR_MAX;
46 phys_addr_t max_addr = 0;
49 ehdr = (struct elf32_hdr *)fw->data;
50 phdrs = (struct elf32_phdr *)(ehdr + 1);
52 for (i = 0; i < ehdr->e_phnum; i++) {
55 if (!mdt_phdr_valid(phdr))
58 if (phdr->p_paddr < min_addr)
59 min_addr = phdr->p_paddr;
61 if (phdr->p_paddr + phdr->p_memsz > max_addr)
62 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
65 return min_addr < max_addr ? max_addr - min_addr : -EINVAL;
67 EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
70 * qcom_mdt_read_metadata() - read header and metadata from mdt or mbn
71 * @fw: firmware of mdt header or mbn
72 * @data_len: length of the read metadata blob
74 * The mechanism that performs the authentication of the loading firmware
75 * expects an ELF header directly followed by the segment of hashes, with no
76 * padding inbetween. This function allocates a chunk of memory for this pair
77 * and copy the two pieces into the buffer.
79 * In the case of split firmware the hash is found directly following the ELF
80 * header, rather than at p_offset described by the second program header.
82 * The caller is responsible to free (kfree()) the returned pointer.
84 * Return: pointer to data, or ERR_PTR()
86 void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len)
88 const struct elf32_phdr *phdrs;
89 const struct elf32_hdr *ehdr;
95 ehdr = (struct elf32_hdr *)fw->data;
96 phdrs = (struct elf32_phdr *)(ehdr + 1);
98 if (ehdr->e_phnum < 2)
99 return ERR_PTR(-EINVAL);
101 if (phdrs[0].p_type == PT_LOAD || phdrs[1].p_type == PT_LOAD)
102 return ERR_PTR(-EINVAL);
104 if ((phdrs[1].p_flags & QCOM_MDT_TYPE_MASK) != QCOM_MDT_TYPE_HASH)
105 return ERR_PTR(-EINVAL);
107 ehdr_size = phdrs[0].p_filesz;
108 hash_size = phdrs[1].p_filesz;
110 data = kmalloc(ehdr_size + hash_size, GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
114 /* Is the header and hash already packed */
115 if (ehdr_size + hash_size == fw->size)
116 hash_offset = phdrs[0].p_filesz;
118 hash_offset = phdrs[1].p_offset;
120 memcpy(data, fw->data, ehdr_size);
121 memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
123 *data_len = ehdr_size + hash_size;
127 EXPORT_SYMBOL_GPL(qcom_mdt_read_metadata);
129 static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
130 const char *firmware, int pas_id, void *mem_region,
131 phys_addr_t mem_phys, size_t mem_size,
132 phys_addr_t *reloc_base, bool pas_init)
134 const struct elf32_phdr *phdrs;
135 const struct elf32_phdr *phdr;
136 const struct elf32_hdr *ehdr;
137 const struct firmware *seg_fw;
138 phys_addr_t mem_reloc;
139 phys_addr_t min_addr = PHYS_ADDR_MAX;
140 phys_addr_t max_addr = 0;
146 bool relocate = false;
151 if (!fw || !mem_region || !mem_phys || !mem_size)
154 ehdr = (struct elf32_hdr *)fw->data;
155 phdrs = (struct elf32_phdr *)(ehdr + 1);
157 fw_name_len = strlen(firmware);
158 if (fw_name_len <= 4)
161 fw_name = kstrdup(firmware, GFP_KERNEL);
166 metadata = qcom_mdt_read_metadata(fw, &metadata_len);
167 if (IS_ERR(metadata)) {
168 ret = PTR_ERR(metadata);
172 ret = qcom_scm_pas_init_image(pas_id, metadata, metadata_len);
176 dev_err(dev, "invalid firmware metadata\n");
181 for (i = 0; i < ehdr->e_phnum; i++) {
184 if (!mdt_phdr_valid(phdr))
187 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
190 if (phdr->p_paddr < min_addr)
191 min_addr = phdr->p_paddr;
193 if (phdr->p_paddr + phdr->p_memsz > max_addr)
194 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
199 ret = qcom_scm_pas_mem_setup(pas_id, mem_phys,
200 max_addr - min_addr);
202 dev_err(dev, "unable to setup relocation\n");
208 * The image is relocatable, so offset each segment based on
209 * the lowest segment address.
211 mem_reloc = min_addr;
214 * Image is not relocatable, so offset each segment based on
215 * the allocated physical chunk of memory.
217 mem_reloc = mem_phys;
220 for (i = 0; i < ehdr->e_phnum; i++) {
223 if (!mdt_phdr_valid(phdr))
226 offset = phdr->p_paddr - mem_reloc;
227 if (offset < 0 || offset + phdr->p_memsz > mem_size) {
228 dev_err(dev, "segment outside memory range\n");
233 if (phdr->p_filesz > phdr->p_memsz) {
235 "refusing to load segment %d with p_filesz > p_memsz\n",
241 ptr = mem_region + offset;
243 if (phdr->p_filesz && phdr->p_offset < fw->size) {
244 /* Firmware is large enough to be non-split */
245 if (phdr->p_offset + phdr->p_filesz > fw->size) {
247 "failed to load segment %d from truncated file %s\n",
253 memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
254 } else if (phdr->p_filesz) {
255 /* Firmware not large enough, load split-out segments */
256 sprintf(fw_name + fw_name_len - 3, "b%02d", i);
257 ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
258 ptr, phdr->p_filesz);
260 dev_err(dev, "failed to load %s\n", fw_name);
264 if (seg_fw->size != phdr->p_filesz) {
266 "failed to load segment %d from truncated file %s\n",
268 release_firmware(seg_fw);
273 release_firmware(seg_fw);
276 if (phdr->p_memsz > phdr->p_filesz)
277 memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
281 *reloc_base = mem_reloc;
290 * qcom_mdt_load() - load the firmware which header is loaded as fw
291 * @dev: device handle to associate resources with
292 * @fw: firmware object for the mdt file
293 * @firmware: name of the firmware, for construction of segment file names
294 * @pas_id: PAS identifier
295 * @mem_region: allocated memory region to load firmware into
296 * @mem_phys: physical address of allocated memory region
297 * @mem_size: size of the allocated memory region
298 * @reloc_base: adjusted physical address after relocation
300 * Returns 0 on success, negative errno otherwise.
302 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
303 const char *firmware, int pas_id, void *mem_region,
304 phys_addr_t mem_phys, size_t mem_size,
305 phys_addr_t *reloc_base)
307 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
308 mem_size, reloc_base, true);
310 EXPORT_SYMBOL_GPL(qcom_mdt_load);
313 * qcom_mdt_load_no_init() - load the firmware which header is loaded as fw
314 * @dev: device handle to associate resources with
315 * @fw: firmware object for the mdt file
316 * @firmware: name of the firmware, for construction of segment file names
317 * @pas_id: PAS identifier
318 * @mem_region: allocated memory region to load firmware into
319 * @mem_phys: physical address of allocated memory region
320 * @mem_size: size of the allocated memory region
321 * @reloc_base: adjusted physical address after relocation
323 * Returns 0 on success, negative errno otherwise.
325 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
326 const char *firmware, int pas_id,
327 void *mem_region, phys_addr_t mem_phys,
328 size_t mem_size, phys_addr_t *reloc_base)
330 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
331 mem_size, reloc_base, false);
333 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
335 MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
336 MODULE_LICENSE("GPL v2");