2 * Qualcomm Peripheral Image Loader
4 * Copyright (C) 2016 Linaro Ltd
5 * Copyright (C) 2015 Sony Mobile Communications Inc
6 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/device.h>
19 #include <linux/elf.h>
20 #include <linux/firmware.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/qcom_scm.h>
24 #include <linux/sizes.h>
25 #include <linux/slab.h>
26 #include <linux/soc/qcom/mdt_loader.h>
28 static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
30 if (phdr->p_type != PT_LOAD)
33 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
43 * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
44 * @fw: firmware object for the mdt file
46 * Returns size of the loaded firmware blob, or -EINVAL on failure.
48 ssize_t qcom_mdt_get_size(const struct firmware *fw)
50 const struct elf32_phdr *phdrs;
51 const struct elf32_phdr *phdr;
52 const struct elf32_hdr *ehdr;
53 phys_addr_t min_addr = PHYS_ADDR_MAX;
54 phys_addr_t max_addr = 0;
57 ehdr = (struct elf32_hdr *)fw->data;
58 phdrs = (struct elf32_phdr *)(ehdr + 1);
60 for (i = 0; i < ehdr->e_phnum; i++) {
63 if (!mdt_phdr_valid(phdr))
66 if (phdr->p_paddr < min_addr)
67 min_addr = phdr->p_paddr;
69 if (phdr->p_paddr + phdr->p_memsz > max_addr)
70 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
73 return min_addr < max_addr ? max_addr - min_addr : -EINVAL;
75 EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
78 * qcom_mdt_read_metadata() - read header and metadata from mdt or mbn
79 * @fw: firmware of mdt header or mbn
80 * @data_len: length of the read metadata blob
82 * The mechanism that performs the authentication of the loading firmware
83 * expects an ELF header directly followed by the segment of hashes, with no
84 * padding inbetween. This function allocates a chunk of memory for this pair
85 * and copy the two pieces into the buffer.
87 * In the case of split firmware the hash is found directly following the ELF
88 * header, rather than at p_offset described by the second program header.
90 * The caller is responsible to free (kfree()) the returned pointer.
92 * Return: pointer to data, or ERR_PTR()
94 void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len)
96 const struct elf32_phdr *phdrs;
97 const struct elf32_hdr *ehdr;
103 ehdr = (struct elf32_hdr *)fw->data;
104 phdrs = (struct elf32_phdr *)(ehdr + 1);
106 if (ehdr->e_phnum < 2)
107 return ERR_PTR(-EINVAL);
109 if (phdrs[0].p_type == PT_LOAD || phdrs[1].p_type == PT_LOAD)
110 return ERR_PTR(-EINVAL);
112 if ((phdrs[1].p_flags & QCOM_MDT_TYPE_MASK) != QCOM_MDT_TYPE_HASH)
113 return ERR_PTR(-EINVAL);
115 ehdr_size = phdrs[0].p_filesz;
116 hash_size = phdrs[1].p_filesz;
118 data = kmalloc(ehdr_size + hash_size, GFP_KERNEL);
120 return ERR_PTR(-ENOMEM);
122 /* Is the header and hash already packed */
123 if (ehdr_size + hash_size == fw->size)
124 hash_offset = phdrs[0].p_filesz;
126 hash_offset = phdrs[1].p_offset;
128 memcpy(data, fw->data, ehdr_size);
129 memcpy(data + ehdr_size, fw->data + hash_offset, hash_size);
131 *data_len = ehdr_size + hash_size;
135 EXPORT_SYMBOL_GPL(qcom_mdt_read_metadata);
137 static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
138 const char *firmware, int pas_id, void *mem_region,
139 phys_addr_t mem_phys, size_t mem_size,
140 phys_addr_t *reloc_base, bool pas_init)
142 const struct elf32_phdr *phdrs;
143 const struct elf32_phdr *phdr;
144 const struct elf32_hdr *ehdr;
145 const struct firmware *seg_fw;
146 phys_addr_t mem_reloc;
147 phys_addr_t min_addr = PHYS_ADDR_MAX;
148 phys_addr_t max_addr = 0;
154 bool relocate = false;
159 if (!fw || !mem_region || !mem_phys || !mem_size)
162 ehdr = (struct elf32_hdr *)fw->data;
163 phdrs = (struct elf32_phdr *)(ehdr + 1);
165 fw_name_len = strlen(firmware);
166 if (fw_name_len <= 4)
169 fw_name = kstrdup(firmware, GFP_KERNEL);
174 metadata = qcom_mdt_read_metadata(fw, &metadata_len);
175 if (IS_ERR(metadata)) {
176 ret = PTR_ERR(metadata);
180 ret = qcom_scm_pas_init_image(pas_id, metadata, metadata_len);
184 dev_err(dev, "invalid firmware metadata\n");
189 for (i = 0; i < ehdr->e_phnum; i++) {
192 if (!mdt_phdr_valid(phdr))
195 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
198 if (phdr->p_paddr < min_addr)
199 min_addr = phdr->p_paddr;
201 if (phdr->p_paddr + phdr->p_memsz > max_addr)
202 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
207 ret = qcom_scm_pas_mem_setup(pas_id, mem_phys,
208 max_addr - min_addr);
210 dev_err(dev, "unable to setup relocation\n");
216 * The image is relocatable, so offset each segment based on
217 * the lowest segment address.
219 mem_reloc = min_addr;
222 * Image is not relocatable, so offset each segment based on
223 * the allocated physical chunk of memory.
225 mem_reloc = mem_phys;
228 for (i = 0; i < ehdr->e_phnum; i++) {
231 if (!mdt_phdr_valid(phdr))
234 offset = phdr->p_paddr - mem_reloc;
235 if (offset < 0 || offset + phdr->p_memsz > mem_size) {
236 dev_err(dev, "segment outside memory range\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 release_firmware(seg_fw);
267 if (phdr->p_memsz > phdr->p_filesz)
268 memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
272 *reloc_base = mem_reloc;
281 * qcom_mdt_load() - load the firmware which header is loaded as fw
282 * @dev: device handle to associate resources with
283 * @fw: firmware object for the mdt file
284 * @firmware: name of the firmware, for construction of segment file names
285 * @pas_id: PAS identifier
286 * @mem_region: allocated memory region to load firmware into
287 * @mem_phys: physical address of allocated memory region
288 * @mem_size: size of the allocated memory region
289 * @reloc_base: adjusted physical address after relocation
291 * Returns 0 on success, negative errno otherwise.
293 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
294 const char *firmware, int pas_id, void *mem_region,
295 phys_addr_t mem_phys, size_t mem_size,
296 phys_addr_t *reloc_base)
298 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
299 mem_size, reloc_base, true);
301 EXPORT_SYMBOL_GPL(qcom_mdt_load);
304 * qcom_mdt_load_no_init() - load the firmware which header is loaded as fw
305 * @dev: device handle to associate resources with
306 * @fw: firmware object for the mdt file
307 * @firmware: name of the firmware, for construction of segment file names
308 * @pas_id: PAS identifier
309 * @mem_region: allocated memory region to load firmware into
310 * @mem_phys: physical address of allocated memory region
311 * @mem_size: size of the allocated memory region
312 * @reloc_base: adjusted physical address after relocation
314 * Returns 0 on success, negative errno otherwise.
316 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
317 const char *firmware, int pas_id,
318 void *mem_region, phys_addr_t mem_phys,
319 size_t mem_size, phys_addr_t *reloc_base)
321 return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
322 mem_size, reloc_base, false);
324 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
326 MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
327 MODULE_LICENSE("GPL v2");