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