]>
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 | ||
18 | #include <linux/elf.h> | |
19 | #include <linux/firmware.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/remoteproc.h> | |
e67af182 | 23 | #include <linux/sizes.h> |
051fb70f BA |
24 | #include <linux/slab.h> |
25 | ||
26 | #include "remoteproc_internal.h" | |
27 | #include "qcom_mdt_loader.h" | |
28 | ||
29 | /** | |
30 | * qcom_mdt_find_rsc_table() - provide dummy resource table for remoteproc | |
31 | * @rproc: remoteproc handle | |
32 | * @fw: firmware header | |
33 | * @tablesz: outgoing size of the table | |
34 | * | |
35 | * Returns a dummy table. | |
36 | */ | |
37 | struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc, | |
38 | const struct firmware *fw, | |
39 | int *tablesz) | |
40 | { | |
41 | static struct resource_table table = { .ver = 1, }; | |
42 | ||
43 | *tablesz = sizeof(table); | |
44 | return &table; | |
45 | } | |
46 | EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table); | |
47 | ||
48 | /** | |
49 | * qcom_mdt_parse() - extract useful parameters from the mdt header | |
50 | * @fw: firmware handle | |
51 | * @fw_addr: optional reference for base of the firmware's memory region | |
52 | * @fw_size: optional reference for size of the firmware's memory region | |
53 | * @fw_relocate: optional reference for flagging if the firmware is relocatable | |
54 | * | |
55 | * Returns 0 on success, negative errno otherwise. | |
56 | */ | |
57 | int qcom_mdt_parse(const struct firmware *fw, phys_addr_t *fw_addr, | |
58 | size_t *fw_size, bool *fw_relocate) | |
59 | { | |
60 | const struct elf32_phdr *phdrs; | |
61 | const struct elf32_phdr *phdr; | |
62 | const struct elf32_hdr *ehdr; | |
63 | phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX; | |
64 | phys_addr_t max_addr = 0; | |
65 | bool relocate = false; | |
66 | int i; | |
67 | ||
68 | ehdr = (struct elf32_hdr *)fw->data; | |
69 | phdrs = (struct elf32_phdr *)(ehdr + 1); | |
70 | ||
71 | for (i = 0; i < ehdr->e_phnum; i++) { | |
72 | phdr = &phdrs[i]; | |
73 | ||
74 | if (phdr->p_type != PT_LOAD) | |
75 | continue; | |
76 | ||
77 | if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) | |
78 | continue; | |
79 | ||
80 | if (!phdr->p_memsz) | |
81 | continue; | |
82 | ||
83 | if (phdr->p_flags & QCOM_MDT_RELOCATABLE) | |
84 | relocate = true; | |
85 | ||
86 | if (phdr->p_paddr < min_addr) | |
87 | min_addr = phdr->p_paddr; | |
88 | ||
89 | if (phdr->p_paddr + phdr->p_memsz > max_addr) | |
90 | max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K); | |
91 | } | |
92 | ||
93 | if (fw_addr) | |
94 | *fw_addr = min_addr; | |
95 | if (fw_size) | |
96 | *fw_size = max_addr - min_addr; | |
97 | if (fw_relocate) | |
98 | *fw_relocate = relocate; | |
99 | ||
100 | return 0; | |
101 | } | |
102 | EXPORT_SYMBOL_GPL(qcom_mdt_parse); | |
103 | ||
104 | /** | |
105 | * qcom_mdt_load() - load the firmware which header is defined in fw | |
106 | * @rproc: rproc handle | |
107 | * @fw: frimware object for the header | |
108 | * @firmware: filename of the firmware, for building .bXX names | |
109 | * | |
110 | * Returns 0 on success, negative errno otherwise. | |
111 | */ | |
112 | int qcom_mdt_load(struct rproc *rproc, | |
113 | const struct firmware *fw, | |
114 | const char *firmware) | |
115 | { | |
116 | const struct elf32_phdr *phdrs; | |
117 | const struct elf32_phdr *phdr; | |
118 | const struct elf32_hdr *ehdr; | |
6a255f72 | 119 | const struct firmware *seg_fw; |
051fb70f BA |
120 | size_t fw_name_len; |
121 | char *fw_name; | |
122 | void *ptr; | |
123 | int ret; | |
124 | int i; | |
125 | ||
126 | ehdr = (struct elf32_hdr *)fw->data; | |
127 | phdrs = (struct elf32_phdr *)(ehdr + 1); | |
128 | ||
129 | fw_name_len = strlen(firmware); | |
130 | if (fw_name_len <= 4) | |
131 | return -EINVAL; | |
132 | ||
133 | fw_name = kstrdup(firmware, GFP_KERNEL); | |
134 | if (!fw_name) | |
135 | return -ENOMEM; | |
136 | ||
137 | for (i = 0; i < ehdr->e_phnum; i++) { | |
138 | phdr = &phdrs[i]; | |
139 | ||
140 | if (phdr->p_type != PT_LOAD) | |
141 | continue; | |
142 | ||
143 | if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) | |
144 | continue; | |
145 | ||
146 | if (!phdr->p_memsz) | |
147 | continue; | |
148 | ||
149 | ptr = rproc_da_to_va(rproc, phdr->p_paddr, phdr->p_memsz); | |
150 | if (!ptr) { | |
151 | dev_err(&rproc->dev, "segment outside memory range\n"); | |
152 | ret = -EINVAL; | |
153 | break; | |
154 | } | |
155 | ||
156 | if (phdr->p_filesz) { | |
157 | sprintf(fw_name + fw_name_len - 3, "b%02d", i); | |
6a255f72 | 158 | ret = request_firmware(&seg_fw, fw_name, &rproc->dev); |
051fb70f BA |
159 | if (ret) { |
160 | dev_err(&rproc->dev, "failed to load %s\n", | |
161 | fw_name); | |
162 | break; | |
163 | } | |
164 | ||
6a255f72 | 165 | memcpy(ptr, seg_fw->data, seg_fw->size); |
051fb70f | 166 | |
6a255f72 | 167 | release_firmware(seg_fw); |
051fb70f BA |
168 | } |
169 | ||
170 | if (phdr->p_memsz > phdr->p_filesz) | |
171 | memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz); | |
172 | } | |
173 | ||
174 | kfree(fw_name); | |
175 | ||
176 | return ret; | |
177 | } | |
178 | EXPORT_SYMBOL_GPL(qcom_mdt_load); | |
179 | ||
180 | MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format"); | |
181 | MODULE_LICENSE("GPL v2"); |