]>
Commit | Line | Data |
---|---|---|
051fb70f BA |
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 | ||
7f0dd07a | 18 | #include <linux/device.h> |
051fb70f BA |
19 | #include <linux/elf.h> |
20 | #include <linux/firmware.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
7f0dd07a | 23 | #include <linux/qcom_scm.h> |
e67af182 | 24 | #include <linux/sizes.h> |
051fb70f | 25 | #include <linux/slab.h> |
2aad40d9 | 26 | #include <linux/soc/qcom/mdt_loader.h> |
051fb70f | 27 | |
7f0dd07a BA |
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 | ||
051fb70f | 42 | /** |
7f0dd07a BA |
43 | * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt |
44 | * @fw: firmware object for the mdt file | |
051fb70f | 45 | * |
7f0dd07a | 46 | * Returns size of the loaded firmware blob, or -EINVAL on failure. |
051fb70f | 47 | */ |
7f0dd07a | 48 | ssize_t qcom_mdt_get_size(const struct firmware *fw) |
051fb70f BA |
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_t)ULLONG_MAX; | |
54 | phys_addr_t max_addr = 0; | |
051fb70f BA |
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 | ||
7f0dd07a | 63 | if (!mdt_phdr_valid(phdr)) |
051fb70f BA |
64 | continue; |
65 | ||
051fb70f BA |
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 | ||
7f0dd07a | 73 | return min_addr < max_addr ? max_addr - min_addr : -EINVAL; |
051fb70f | 74 | } |
7f0dd07a | 75 | EXPORT_SYMBOL_GPL(qcom_mdt_get_size); |
051fb70f BA |
76 | |
77 | /** | |
7f0dd07a BA |
78 | * qcom_mdt_load() - load the firmware which header is loaded as fw |
79 | * @dev: device handle to associate resources with | |
80 | * @fw: firmware object for the mdt file | |
81 | * @firmware: name of the firmware, for construction of segment file names | |
82 | * @pas_id: PAS identifier | |
83 | * @mem_region: allocated memory region to load firmware into | |
84 | * @mem_phys: physical address of allocated memory region | |
85 | * @mem_size: size of the allocated memory region | |
051fb70f BA |
86 | * |
87 | * Returns 0 on success, negative errno otherwise. | |
88 | */ | |
7f0dd07a BA |
89 | int qcom_mdt_load(struct device *dev, const struct firmware *fw, |
90 | const char *firmware, int pas_id, void *mem_region, | |
91 | phys_addr_t mem_phys, size_t mem_size) | |
051fb70f BA |
92 | { |
93 | const struct elf32_phdr *phdrs; | |
94 | const struct elf32_phdr *phdr; | |
95 | const struct elf32_hdr *ehdr; | |
3e8b571a | 96 | const struct firmware *seg_fw; |
7f0dd07a BA |
97 | phys_addr_t mem_reloc; |
98 | phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX; | |
99 | phys_addr_t max_addr = 0; | |
051fb70f | 100 | size_t fw_name_len; |
01625cc5 | 101 | ssize_t offset; |
051fb70f | 102 | char *fw_name; |
7f0dd07a | 103 | bool relocate = false; |
051fb70f BA |
104 | void *ptr; |
105 | int ret; | |
106 | int i; | |
107 | ||
7f0dd07a BA |
108 | if (!fw || !mem_region || !mem_phys || !mem_size) |
109 | return -EINVAL; | |
110 | ||
051fb70f BA |
111 | ehdr = (struct elf32_hdr *)fw->data; |
112 | phdrs = (struct elf32_phdr *)(ehdr + 1); | |
113 | ||
114 | fw_name_len = strlen(firmware); | |
115 | if (fw_name_len <= 4) | |
116 | return -EINVAL; | |
117 | ||
118 | fw_name = kstrdup(firmware, GFP_KERNEL); | |
119 | if (!fw_name) | |
120 | return -ENOMEM; | |
121 | ||
7f0dd07a BA |
122 | ret = qcom_scm_pas_init_image(pas_id, fw->data, fw->size); |
123 | if (ret) { | |
124 | dev_err(dev, "invalid firmware metadata\n"); | |
125 | goto out; | |
126 | } | |
127 | ||
051fb70f BA |
128 | for (i = 0; i < ehdr->e_phnum; i++) { |
129 | phdr = &phdrs[i]; | |
130 | ||
7f0dd07a | 131 | if (!mdt_phdr_valid(phdr)) |
051fb70f BA |
132 | continue; |
133 | ||
7f0dd07a BA |
134 | if (phdr->p_flags & QCOM_MDT_RELOCATABLE) |
135 | relocate = true; | |
136 | ||
137 | if (phdr->p_paddr < min_addr) | |
138 | min_addr = phdr->p_paddr; | |
139 | ||
140 | if (phdr->p_paddr + phdr->p_memsz > max_addr) | |
141 | max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K); | |
142 | } | |
143 | ||
144 | if (relocate) { | |
145 | ret = qcom_scm_pas_mem_setup(pas_id, mem_phys, max_addr - min_addr); | |
146 | if (ret) { | |
147 | dev_err(dev, "unable to setup relocation\n"); | |
148 | goto out; | |
149 | } | |
150 | ||
151 | /* | |
152 | * The image is relocatable, so offset each segment based on | |
153 | * the lowest segment address. | |
154 | */ | |
155 | mem_reloc = min_addr; | |
156 | } else { | |
157 | /* | |
158 | * Image is not relocatable, so offset each segment based on | |
159 | * the allocated physical chunk of memory. | |
160 | */ | |
161 | mem_reloc = mem_phys; | |
162 | } | |
051fb70f | 163 | |
7f0dd07a BA |
164 | for (i = 0; i < ehdr->e_phnum; i++) { |
165 | phdr = &phdrs[i]; | |
166 | ||
167 | if (!mdt_phdr_valid(phdr)) | |
051fb70f BA |
168 | continue; |
169 | ||
7f0dd07a BA |
170 | offset = phdr->p_paddr - mem_reloc; |
171 | if (offset < 0 || offset + phdr->p_memsz > mem_size) { | |
172 | dev_err(dev, "segment outside memory range\n"); | |
051fb70f BA |
173 | ret = -EINVAL; |
174 | break; | |
175 | } | |
176 | ||
7f0dd07a BA |
177 | ptr = mem_region + offset; |
178 | ||
051fb70f BA |
179 | if (phdr->p_filesz) { |
180 | sprintf(fw_name + fw_name_len - 3, "b%02d", i); | |
7f0dd07a | 181 | ret = request_firmware(&seg_fw, fw_name, dev); |
051fb70f | 182 | if (ret) { |
7f0dd07a | 183 | dev_err(dev, "failed to load %s\n", fw_name); |
051fb70f BA |
184 | break; |
185 | } | |
186 | ||
3e8b571a | 187 | memcpy(ptr, seg_fw->data, seg_fw->size); |
051fb70f | 188 | |
3e8b571a | 189 | release_firmware(seg_fw); |
051fb70f BA |
190 | } |
191 | ||
192 | if (phdr->p_memsz > phdr->p_filesz) | |
193 | memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz); | |
194 | } | |
195 | ||
7f0dd07a | 196 | out: |
051fb70f BA |
197 | kfree(fw_name); |
198 | ||
199 | return ret; | |
200 | } | |
201 | EXPORT_SYMBOL_GPL(qcom_mdt_load); | |
202 | ||
203 | MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format"); | |
204 | MODULE_LICENSE("GPL v2"); |