xref: /openbmc/linux/drivers/soc/qcom/mdt_loader.c (revision 93f5715e)
1 /*
2  * Qualcomm Peripheral Image Loader
3  *
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.
7  *
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.
11  *
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.
16  */
17 
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>
27 
28 static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
29 {
30 	if (phdr->p_type != PT_LOAD)
31 		return false;
32 
33 	if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
34 		return false;
35 
36 	if (!phdr->p_memsz)
37 		return false;
38 
39 	return true;
40 }
41 
42 /**
43  * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
44  * @fw:		firmware object for the mdt file
45  *
46  * Returns size of the loaded firmware blob, or -EINVAL on failure.
47  */
48 ssize_t qcom_mdt_get_size(const struct firmware *fw)
49 {
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;
55 	int i;
56 
57 	ehdr = (struct elf32_hdr *)fw->data;
58 	phdrs = (struct elf32_phdr *)(ehdr + 1);
59 
60 	for (i = 0; i < ehdr->e_phnum; i++) {
61 		phdr = &phdrs[i];
62 
63 		if (!mdt_phdr_valid(phdr))
64 			continue;
65 
66 		if (phdr->p_paddr < min_addr)
67 			min_addr = phdr->p_paddr;
68 
69 		if (phdr->p_paddr + phdr->p_memsz > max_addr)
70 			max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
71 	}
72 
73 	return min_addr < max_addr ? max_addr - min_addr : -EINVAL;
74 }
75 EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
76 
77 static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
78 			   const char *firmware, int pas_id, void *mem_region,
79 			   phys_addr_t mem_phys, size_t mem_size,
80 			   phys_addr_t *reloc_base, bool pas_init)
81 {
82 	const struct elf32_phdr *phdrs;
83 	const struct elf32_phdr *phdr;
84 	const struct elf32_hdr *ehdr;
85 	const struct firmware *seg_fw;
86 	phys_addr_t mem_reloc;
87 	phys_addr_t min_addr = PHYS_ADDR_MAX;
88 	phys_addr_t max_addr = 0;
89 	size_t fw_name_len;
90 	ssize_t offset;
91 	char *fw_name;
92 	bool relocate = false;
93 	void *ptr;
94 	int ret;
95 	int i;
96 
97 	if (!fw || !mem_region || !mem_phys || !mem_size)
98 		return -EINVAL;
99 
100 	ehdr = (struct elf32_hdr *)fw->data;
101 	phdrs = (struct elf32_phdr *)(ehdr + 1);
102 
103 	fw_name_len = strlen(firmware);
104 	if (fw_name_len <= 4)
105 		return -EINVAL;
106 
107 	fw_name = kstrdup(firmware, GFP_KERNEL);
108 	if (!fw_name)
109 		return -ENOMEM;
110 
111 	if (pas_init) {
112 		ret = qcom_scm_pas_init_image(pas_id, fw->data, fw->size);
113 		if (ret) {
114 			dev_err(dev, "invalid firmware metadata\n");
115 			goto out;
116 		}
117 	}
118 
119 	for (i = 0; i < ehdr->e_phnum; i++) {
120 		phdr = &phdrs[i];
121 
122 		if (!mdt_phdr_valid(phdr))
123 			continue;
124 
125 		if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
126 			relocate = true;
127 
128 		if (phdr->p_paddr < min_addr)
129 			min_addr = phdr->p_paddr;
130 
131 		if (phdr->p_paddr + phdr->p_memsz > max_addr)
132 			max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
133 	}
134 
135 	if (relocate) {
136 		if (pas_init) {
137 			ret = qcom_scm_pas_mem_setup(pas_id, mem_phys,
138 						     max_addr - min_addr);
139 			if (ret) {
140 				dev_err(dev, "unable to setup relocation\n");
141 				goto out;
142 			}
143 		}
144 
145 		/*
146 		 * The image is relocatable, so offset each segment based on
147 		 * the lowest segment address.
148 		 */
149 		mem_reloc = min_addr;
150 	} else {
151 		/*
152 		 * Image is not relocatable, so offset each segment based on
153 		 * the allocated physical chunk of memory.
154 		 */
155 		mem_reloc = mem_phys;
156 	}
157 
158 	for (i = 0; i < ehdr->e_phnum; i++) {
159 		phdr = &phdrs[i];
160 
161 		if (!mdt_phdr_valid(phdr))
162 			continue;
163 
164 		offset = phdr->p_paddr - mem_reloc;
165 		if (offset < 0 || offset + phdr->p_memsz > mem_size) {
166 			dev_err(dev, "segment outside memory range\n");
167 			ret = -EINVAL;
168 			break;
169 		}
170 
171 		ptr = mem_region + offset;
172 
173 		if (phdr->p_filesz) {
174 			sprintf(fw_name + fw_name_len - 3, "b%02d", i);
175 			ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
176 							ptr, phdr->p_filesz);
177 			if (ret) {
178 				dev_err(dev, "failed to load %s\n", fw_name);
179 				break;
180 			}
181 
182 			release_firmware(seg_fw);
183 		}
184 
185 		if (phdr->p_memsz > phdr->p_filesz)
186 			memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
187 	}
188 
189 	if (reloc_base)
190 		*reloc_base = mem_reloc;
191 
192 out:
193 	kfree(fw_name);
194 
195 	return ret;
196 }
197 
198 /**
199  * qcom_mdt_load() - load the firmware which header is loaded as fw
200  * @dev:	device handle to associate resources with
201  * @fw:		firmware object for the mdt file
202  * @firmware:	name of the firmware, for construction of segment file names
203  * @pas_id:	PAS identifier
204  * @mem_region:	allocated memory region to load firmware into
205  * @mem_phys:	physical address of allocated memory region
206  * @mem_size:	size of the allocated memory region
207  * @reloc_base:	adjusted physical address after relocation
208  *
209  * Returns 0 on success, negative errno otherwise.
210  */
211 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
212 		  const char *firmware, int pas_id, void *mem_region,
213 		  phys_addr_t mem_phys, size_t mem_size,
214 		  phys_addr_t *reloc_base)
215 {
216 	return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
217 			       mem_size, reloc_base, true);
218 }
219 EXPORT_SYMBOL_GPL(qcom_mdt_load);
220 
221 /**
222  * qcom_mdt_load_no_init() - load the firmware which header is loaded as fw
223  * @dev:	device handle to associate resources with
224  * @fw:		firmware object for the mdt file
225  * @firmware:	name of the firmware, for construction of segment file names
226  * @pas_id:	PAS identifier
227  * @mem_region:	allocated memory region to load firmware into
228  * @mem_phys:	physical address of allocated memory region
229  * @mem_size:	size of the allocated memory region
230  * @reloc_base:	adjusted physical address after relocation
231  *
232  * Returns 0 on success, negative errno otherwise.
233  */
234 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
235 			  const char *firmware, int pas_id,
236 			  void *mem_region, phys_addr_t mem_phys,
237 			  size_t mem_size, phys_addr_t *reloc_base)
238 {
239 	return __qcom_mdt_load(dev, fw, firmware, pas_id, mem_region, mem_phys,
240 			       mem_size, reloc_base, false);
241 }
242 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
243 
244 MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
245 MODULE_LICENSE("GPL v2");
246