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);
69 static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
70 const char *firmware, int pas_id, void *mem_region,
71 phys_addr_t mem_phys, size_t mem_size,
72 phys_addr_t *reloc_base, bool pas_init)
74 const struct elf32_phdr *phdrs;
75 const struct elf32_phdr *phdr;
76 const struct elf32_hdr *ehdr;
77 const struct firmware *seg_fw;
78 phys_addr_t mem_reloc;
79 phys_addr_t min_addr = PHYS_ADDR_MAX;
80 phys_addr_t max_addr = 0;
84 bool relocate = false;
89 if (!fw || !mem_region || !mem_phys || !mem_size)
92 ehdr = (struct elf32_hdr *)fw->data;
93 phdrs = (struct elf32_phdr *)(ehdr + 1);
95 fw_name_len = strlen(firmware);
99 fw_name = kstrdup(firmware, GFP_KERNEL);
104 ret = qcom_scm_pas_init_image(pas_id, fw->data, fw->size);
106 dev_err(dev, "invalid firmware metadata\n");
111 for (i = 0; i < ehdr->e_phnum; i++) {
114 if (!mdt_phdr_valid(phdr))
117 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
120 if (phdr->p_paddr < min_addr)
121 min_addr = phdr->p_paddr;
123 if (phdr->p_paddr + phdr->p_memsz > max_addr)
124 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
129 ret = qcom_scm_pas_mem_setup(pas_id, mem_phys,
130 max_addr - min_addr);
132 dev_err(dev, "unable to setup relocation\n");
138 * The image is relocatable, so offset each segment based on
139 * the lowest segment address.
141 mem_reloc = min_addr;
144 * Image is not relocatable, so offset each segment based on
145 * the allocated physical chunk of memory.
147 mem_reloc = mem_phys;
150 for (i = 0; i < ehdr->e_phnum; i++) {
153 if (!mdt_phdr_valid(phdr))
156 offset = phdr->p_paddr - mem_reloc;
157 if (offset < 0 || offset + phdr->p_memsz > mem_size) {
158 dev_err(dev, "segment outside memory range\n");
163 ptr = mem_region + offset;
165 if (phdr->p_filesz) {
166 sprintf(fw_name + fw_name_len - 3, "b%02d", i);
167 ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
168 ptr, phdr->p_filesz);
170 dev_err(dev, "failed to load %s\n", fw_name);
174 release_firmware(seg_fw);
177 if (phdr->p_memsz > phdr->p_filesz)
178 memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
182 *reloc_base = mem_reloc;
191 * qcom_mdt_load() - load the firmware which header is loaded as fw
192 * @dev: device handle to associate resources with
193 * @fw: firmware object for the mdt file
194 * @firmware: name of the firmware, for construction of segment file names
195 * @pas_id: PAS identifier
196 * @mem_region: allocated memory region to load firmware into
197 * @mem_phys: physical address of allocated memory region
198 * @mem_size: size of the allocated memory region
199 * @reloc_base: adjusted physical address after relocation
201 * Returns 0 on success, negative errno otherwise.
203 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
204 const char *firmware, int pas_id, void *mem_region,
205 phys_addr_t mem_phys, size_t mem_size,
206 phys_addr_t *reloc_base)
208 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
209 mem_size, reloc_base, true);
211 EXPORT_SYMBOL_GPL(qcom_mdt_load);
214 * qcom_mdt_load_no_init() - load the firmware which header is loaded as fw
215 * @dev: device handle to associate resources with
216 * @fw: firmware object for the mdt file
217 * @firmware: name of the firmware, for construction of segment file names
218 * @pas_id: PAS identifier
219 * @mem_region: allocated memory region to load firmware into
220 * @mem_phys: physical address of allocated memory region
221 * @mem_size: size of the allocated memory region
222 * @reloc_base: adjusted physical address after relocation
224 * Returns 0 on success, negative errno otherwise.
226 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
227 const char *firmware, int pas_id,
228 void *mem_region, phys_addr_t mem_phys,
229 size_t mem_size, phys_addr_t *reloc_base)
231 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
232 mem_size, reloc_base, false);
234 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
236 MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
237 MODULE_LICENSE("GPL v2");