xref: /OK3568_Linux_fs/kernel/drivers/remoteproc/remoteproc_elf_loader.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Remote Processor Framework Elf loader
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Texas Instruments, Inc.
6*4882a593Smuzhiyun  * Copyright (C) 2011 Google, Inc.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Ohad Ben-Cohen <ohad@wizery.com>
9*4882a593Smuzhiyun  * Brian Swetland <swetland@google.com>
10*4882a593Smuzhiyun  * Mark Grosen <mgrosen@ti.com>
11*4882a593Smuzhiyun  * Fernando Guzman Lugo <fernando.lugo@ti.com>
12*4882a593Smuzhiyun  * Suman Anna <s-anna@ti.com>
13*4882a593Smuzhiyun  * Robert Tivy <rtivy@ti.com>
14*4882a593Smuzhiyun  * Armando Uribe De Leon <x0095078@ti.com>
15*4882a593Smuzhiyun  * Sjur Brændeland <sjur.brandeland@stericsson.com>
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define pr_fmt(fmt)    "%s: " fmt, __func__
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/firmware.h>
22*4882a593Smuzhiyun #include <linux/remoteproc.h>
23*4882a593Smuzhiyun #include <linux/elf.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include "remoteproc_internal.h"
26*4882a593Smuzhiyun #include "remoteproc_elf_helpers.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun  * rproc_elf_sanity_check() - Sanity Check for ELF32/ELF64 firmware image
30*4882a593Smuzhiyun  * @rproc: the remote processor handle
31*4882a593Smuzhiyun  * @fw: the ELF firmware image
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Make sure this fw image is sane (ie a correct ELF32/ELF64 file).
34*4882a593Smuzhiyun  */
rproc_elf_sanity_check(struct rproc * rproc,const struct firmware * fw)35*4882a593Smuzhiyun int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	const char *name = rproc->firmware;
38*4882a593Smuzhiyun 	struct device *dev = &rproc->dev;
39*4882a593Smuzhiyun 	/*
40*4882a593Smuzhiyun 	 * Elf files are beginning with the same structure. Thus, to simplify
41*4882a593Smuzhiyun 	 * header parsing, we can use the elf32_hdr one for both elf64 and
42*4882a593Smuzhiyun 	 * elf32.
43*4882a593Smuzhiyun 	 */
44*4882a593Smuzhiyun 	struct elf32_hdr *ehdr;
45*4882a593Smuzhiyun 	u32 elf_shdr_get_size;
46*4882a593Smuzhiyun 	u64 phoff, shoff;
47*4882a593Smuzhiyun 	char class;
48*4882a593Smuzhiyun 	u16 phnum;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (!fw) {
51*4882a593Smuzhiyun 		dev_err(dev, "failed to load %s\n", name);
52*4882a593Smuzhiyun 		return -EINVAL;
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (fw->size < sizeof(struct elf32_hdr)) {
56*4882a593Smuzhiyun 		dev_err(dev, "Image is too small\n");
57*4882a593Smuzhiyun 		return -EINVAL;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	ehdr = (struct elf32_hdr *)fw->data;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
63*4882a593Smuzhiyun 		dev_err(dev, "Image is corrupted (bad magic)\n");
64*4882a593Smuzhiyun 		return -EINVAL;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	class = ehdr->e_ident[EI_CLASS];
68*4882a593Smuzhiyun 	if (class != ELFCLASS32 && class != ELFCLASS64) {
69*4882a593Smuzhiyun 		dev_err(dev, "Unsupported class: %d\n", class);
70*4882a593Smuzhiyun 		return -EINVAL;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
74*4882a593Smuzhiyun 		dev_err(dev, "elf64 header is too small\n");
75*4882a593Smuzhiyun 		return -EINVAL;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/* We assume the firmware has the same endianness as the host */
79*4882a593Smuzhiyun # ifdef __LITTLE_ENDIAN
80*4882a593Smuzhiyun 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
81*4882a593Smuzhiyun # else /* BIG ENDIAN */
82*4882a593Smuzhiyun 	if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
83*4882a593Smuzhiyun # endif
84*4882a593Smuzhiyun 		dev_err(dev, "Unsupported firmware endianness\n");
85*4882a593Smuzhiyun 		return -EINVAL;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	phoff = elf_hdr_get_e_phoff(class, fw->data);
89*4882a593Smuzhiyun 	shoff = elf_hdr_get_e_shoff(class, fw->data);
90*4882a593Smuzhiyun 	phnum =  elf_hdr_get_e_phnum(class, fw->data);
91*4882a593Smuzhiyun 	elf_shdr_get_size = elf_size_of_shdr(class);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (fw->size < shoff + elf_shdr_get_size) {
94*4882a593Smuzhiyun 		dev_err(dev, "Image is too small\n");
95*4882a593Smuzhiyun 		return -EINVAL;
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (phnum == 0) {
99*4882a593Smuzhiyun 		dev_err(dev, "No loadable segments\n");
100*4882a593Smuzhiyun 		return -EINVAL;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (phoff > fw->size) {
104*4882a593Smuzhiyun 		dev_err(dev, "Firmware size is too small\n");
105*4882a593Smuzhiyun 		return -EINVAL;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	dev_dbg(dev, "Firmware is an elf%d file\n",
109*4882a593Smuzhiyun 		class == ELFCLASS32 ? 32 : 64);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun EXPORT_SYMBOL(rproc_elf_sanity_check);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * rproc_elf_get_boot_addr() - Get rproc's boot address.
117*4882a593Smuzhiyun  * @rproc: the remote processor handle
118*4882a593Smuzhiyun  * @fw: the ELF firmware image
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * This function returns the entry point address of the ELF
121*4882a593Smuzhiyun  * image.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * Note that the boot address is not a configurable property of all remote
124*4882a593Smuzhiyun  * processors. Some will always boot at a specific hard-coded address.
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	return elf_hdr_get_e_entry(fw_elf_get_class(fw), fw->data);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun EXPORT_SYMBOL(rproc_elf_get_boot_addr);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * rproc_elf_load_segments() - load firmware segments to memory
134*4882a593Smuzhiyun  * @rproc: remote processor which will be booted using these fw segments
135*4882a593Smuzhiyun  * @fw: the ELF firmware image
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * This function loads the firmware segments to memory, where the remote
138*4882a593Smuzhiyun  * processor expects them.
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * Some remote processors will expect their code and data to be placed
141*4882a593Smuzhiyun  * in specific device addresses, and can't have them dynamically assigned.
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * We currently support only those kind of remote processors, and expect
144*4882a593Smuzhiyun  * the program header's paddr member to contain those addresses. We then go
145*4882a593Smuzhiyun  * through the physically contiguous "carveout" memory regions which we
146*4882a593Smuzhiyun  * allocated (and mapped) earlier on behalf of the remote processor,
147*4882a593Smuzhiyun  * and "translate" device address to kernel addresses, so we can copy the
148*4882a593Smuzhiyun  * segments where they are expected.
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * Currently we only support remote processors that required carveout
151*4882a593Smuzhiyun  * allocations and got them mapped onto their iommus. Some processors
152*4882a593Smuzhiyun  * might be different: they might not have iommus, and would prefer to
153*4882a593Smuzhiyun  * directly allocate memory for every segment/resource. This is not yet
154*4882a593Smuzhiyun  * supported, though.
155*4882a593Smuzhiyun  */
156*4882a593Smuzhiyun int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct device *dev = &rproc->dev;
159*4882a593Smuzhiyun 	const void *ehdr, *phdr;
160*4882a593Smuzhiyun 	int i, ret = 0;
161*4882a593Smuzhiyun 	u16 phnum;
162*4882a593Smuzhiyun 	const u8 *elf_data = fw->data;
163*4882a593Smuzhiyun 	u8 class = fw_elf_get_class(fw);
164*4882a593Smuzhiyun 	u32 elf_phdr_get_size = elf_size_of_phdr(class);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	ehdr = elf_data;
167*4882a593Smuzhiyun 	phnum = elf_hdr_get_e_phnum(class, ehdr);
168*4882a593Smuzhiyun 	phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/* go through the available ELF segments */
171*4882a593Smuzhiyun 	for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) {
172*4882a593Smuzhiyun 		u64 da = elf_phdr_get_p_paddr(class, phdr);
173*4882a593Smuzhiyun 		u64 memsz = elf_phdr_get_p_memsz(class, phdr);
174*4882a593Smuzhiyun 		u64 filesz = elf_phdr_get_p_filesz(class, phdr);
175*4882a593Smuzhiyun 		u64 offset = elf_phdr_get_p_offset(class, phdr);
176*4882a593Smuzhiyun 		u32 type = elf_phdr_get_p_type(class, phdr);
177*4882a593Smuzhiyun 		bool is_iomem = false;
178*4882a593Smuzhiyun 		void *ptr;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		if (type != PT_LOAD)
181*4882a593Smuzhiyun 			continue;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
184*4882a593Smuzhiyun 			type, da, memsz, filesz);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		if (filesz > memsz) {
187*4882a593Smuzhiyun 			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
188*4882a593Smuzhiyun 				filesz, memsz);
189*4882a593Smuzhiyun 			ret = -EINVAL;
190*4882a593Smuzhiyun 			break;
191*4882a593Smuzhiyun 		}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		if (offset + filesz > fw->size) {
194*4882a593Smuzhiyun 			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
195*4882a593Smuzhiyun 				offset + filesz, fw->size);
196*4882a593Smuzhiyun 			ret = -EINVAL;
197*4882a593Smuzhiyun 			break;
198*4882a593Smuzhiyun 		}
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		if (!rproc_u64_fit_in_size_t(memsz)) {
201*4882a593Smuzhiyun 			dev_err(dev, "size (%llx) does not fit in size_t type\n",
202*4882a593Smuzhiyun 				memsz);
203*4882a593Smuzhiyun 			ret = -EOVERFLOW;
204*4882a593Smuzhiyun 			break;
205*4882a593Smuzhiyun 		}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		/* grab the kernel address for this device address */
208*4882a593Smuzhiyun 		ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
209*4882a593Smuzhiyun 		if (!ptr) {
210*4882a593Smuzhiyun 			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
211*4882a593Smuzhiyun 				memsz);
212*4882a593Smuzhiyun 			ret = -EINVAL;
213*4882a593Smuzhiyun 			break;
214*4882a593Smuzhiyun 		}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 		/* put the segment where the remote processor expects it */
217*4882a593Smuzhiyun 		if (filesz) {
218*4882a593Smuzhiyun 			if (is_iomem)
219*4882a593Smuzhiyun 				memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
220*4882a593Smuzhiyun 			else
221*4882a593Smuzhiyun 				memcpy(ptr, elf_data + offset, filesz);
222*4882a593Smuzhiyun 		}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		/*
225*4882a593Smuzhiyun 		 * Zero out remaining memory for this segment.
226*4882a593Smuzhiyun 		 *
227*4882a593Smuzhiyun 		 * This isn't strictly required since dma_alloc_coherent already
228*4882a593Smuzhiyun 		 * did this for us. albeit harmless, we may consider removing
229*4882a593Smuzhiyun 		 * this.
230*4882a593Smuzhiyun 		 */
231*4882a593Smuzhiyun 		if (memsz > filesz) {
232*4882a593Smuzhiyun 			if (is_iomem)
233*4882a593Smuzhiyun 				memset_io((void __iomem *)(ptr + filesz), 0, memsz - filesz);
234*4882a593Smuzhiyun 			else
235*4882a593Smuzhiyun 				memset(ptr + filesz, 0, memsz - filesz);
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	return ret;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun EXPORT_SYMBOL(rproc_elf_load_segments);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun static const void *
244*4882a593Smuzhiyun find_table(struct device *dev, const struct firmware *fw)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	const void *shdr, *name_table_shdr;
247*4882a593Smuzhiyun 	int i;
248*4882a593Smuzhiyun 	const char *name_table;
249*4882a593Smuzhiyun 	struct resource_table *table = NULL;
250*4882a593Smuzhiyun 	const u8 *elf_data = (void *)fw->data;
251*4882a593Smuzhiyun 	u8 class = fw_elf_get_class(fw);
252*4882a593Smuzhiyun 	size_t fw_size = fw->size;
253*4882a593Smuzhiyun 	const void *ehdr = elf_data;
254*4882a593Smuzhiyun 	u16 shnum = elf_hdr_get_e_shnum(class, ehdr);
255*4882a593Smuzhiyun 	u32 elf_shdr_get_size = elf_size_of_shdr(class);
256*4882a593Smuzhiyun 	u16 shstrndx = elf_hdr_get_e_shstrndx(class, ehdr);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* look for the resource table and handle it */
259*4882a593Smuzhiyun 	/* First, get the section header according to the elf class */
260*4882a593Smuzhiyun 	shdr = elf_data + elf_hdr_get_e_shoff(class, ehdr);
261*4882a593Smuzhiyun 	/* Compute name table section header entry in shdr array */
262*4882a593Smuzhiyun 	name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
263*4882a593Smuzhiyun 	/* Finally, compute the name table section address in elf */
264*4882a593Smuzhiyun 	name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {
267*4882a593Smuzhiyun 		u64 size = elf_shdr_get_sh_size(class, shdr);
268*4882a593Smuzhiyun 		u64 offset = elf_shdr_get_sh_offset(class, shdr);
269*4882a593Smuzhiyun 		u32 name = elf_shdr_get_sh_name(class, shdr);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		if (strcmp(name_table + name, ".resource_table"))
272*4882a593Smuzhiyun 			continue;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		table = (struct resource_table *)(elf_data + offset);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 		/* make sure we have the entire table */
277*4882a593Smuzhiyun 		if (offset + size > fw_size || offset + size < size) {
278*4882a593Smuzhiyun 			dev_err(dev, "resource table truncated\n");
279*4882a593Smuzhiyun 			return NULL;
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 		/* make sure table has at least the header */
283*4882a593Smuzhiyun 		if (sizeof(struct resource_table) > size) {
284*4882a593Smuzhiyun 			dev_err(dev, "header-less resource table\n");
285*4882a593Smuzhiyun 			return NULL;
286*4882a593Smuzhiyun 		}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		/* we don't support any version beyond the first */
289*4882a593Smuzhiyun 		if (table->ver != 1) {
290*4882a593Smuzhiyun 			dev_err(dev, "unsupported fw ver: %d\n", table->ver);
291*4882a593Smuzhiyun 			return NULL;
292*4882a593Smuzhiyun 		}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		/* make sure reserved bytes are zeroes */
295*4882a593Smuzhiyun 		if (table->reserved[0] || table->reserved[1]) {
296*4882a593Smuzhiyun 			dev_err(dev, "non zero reserved bytes\n");
297*4882a593Smuzhiyun 			return NULL;
298*4882a593Smuzhiyun 		}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		/* make sure the offsets array isn't truncated */
301*4882a593Smuzhiyun 		if (struct_size(table, offset, table->num) > size) {
302*4882a593Smuzhiyun 			dev_err(dev, "resource table incomplete\n");
303*4882a593Smuzhiyun 			return NULL;
304*4882a593Smuzhiyun 		}
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		return shdr;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	return NULL;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun /**
313*4882a593Smuzhiyun  * rproc_elf_load_rsc_table() - load the resource table
314*4882a593Smuzhiyun  * @rproc: the rproc handle
315*4882a593Smuzhiyun  * @fw: the ELF firmware image
316*4882a593Smuzhiyun  *
317*4882a593Smuzhiyun  * This function finds the resource table inside the remote processor's
318*4882a593Smuzhiyun  * firmware, load it into the @cached_table and update @table_ptr.
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * Return: 0 on success, negative errno on failure.
321*4882a593Smuzhiyun  */
322*4882a593Smuzhiyun int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	const void *shdr;
325*4882a593Smuzhiyun 	struct device *dev = &rproc->dev;
326*4882a593Smuzhiyun 	struct resource_table *table = NULL;
327*4882a593Smuzhiyun 	const u8 *elf_data = fw->data;
328*4882a593Smuzhiyun 	size_t tablesz;
329*4882a593Smuzhiyun 	u8 class = fw_elf_get_class(fw);
330*4882a593Smuzhiyun 	u64 sh_offset;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	shdr = find_table(dev, fw);
333*4882a593Smuzhiyun 	if (!shdr)
334*4882a593Smuzhiyun 		return -EINVAL;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	sh_offset = elf_shdr_get_sh_offset(class, shdr);
337*4882a593Smuzhiyun 	table = (struct resource_table *)(elf_data + sh_offset);
338*4882a593Smuzhiyun 	tablesz = elf_shdr_get_sh_size(class, shdr);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	/*
341*4882a593Smuzhiyun 	 * Create a copy of the resource table. When a virtio device starts
342*4882a593Smuzhiyun 	 * and calls vring_new_virtqueue() the address of the allocated vring
343*4882a593Smuzhiyun 	 * will be stored in the cached_table. Before the device is started,
344*4882a593Smuzhiyun 	 * cached_table will be copied into device memory.
345*4882a593Smuzhiyun 	 */
346*4882a593Smuzhiyun 	rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
347*4882a593Smuzhiyun 	if (!rproc->cached_table)
348*4882a593Smuzhiyun 		return -ENOMEM;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	rproc->table_ptr = rproc->cached_table;
351*4882a593Smuzhiyun 	rproc->table_sz = tablesz;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	return 0;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun EXPORT_SYMBOL(rproc_elf_load_rsc_table);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun /**
358*4882a593Smuzhiyun  * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
359*4882a593Smuzhiyun  * @rproc: the rproc handle
360*4882a593Smuzhiyun  * @fw: the ELF firmware image
361*4882a593Smuzhiyun  *
362*4882a593Smuzhiyun  * This function finds the location of the loaded resource table. Don't
363*4882a593Smuzhiyun  * call this function if the table wasn't loaded yet - it's a bug if you do.
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  * Returns the pointer to the resource table if it is found or NULL otherwise.
366*4882a593Smuzhiyun  * If the table wasn't loaded yet the result is unspecified.
367*4882a593Smuzhiyun  */
368*4882a593Smuzhiyun struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
369*4882a593Smuzhiyun 						       const struct firmware *fw)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	const void *shdr;
372*4882a593Smuzhiyun 	u64 sh_addr, sh_size;
373*4882a593Smuzhiyun 	u8 class = fw_elf_get_class(fw);
374*4882a593Smuzhiyun 	struct device *dev = &rproc->dev;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	shdr = find_table(&rproc->dev, fw);
377*4882a593Smuzhiyun 	if (!shdr)
378*4882a593Smuzhiyun 		return NULL;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	sh_addr = elf_shdr_get_sh_addr(class, shdr);
381*4882a593Smuzhiyun 	sh_size = elf_shdr_get_sh_size(class, shdr);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (!rproc_u64_fit_in_size_t(sh_size)) {
384*4882a593Smuzhiyun 		dev_err(dev, "size (%llx) does not fit in size_t type\n",
385*4882a593Smuzhiyun 			sh_size);
386*4882a593Smuzhiyun 		return NULL;
387*4882a593Smuzhiyun 	}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	return rproc_da_to_va(rproc, sh_addr, sh_size, NULL);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
392