blob: c464ecebb77e64942a598114162bc753e56cc2e5 [file] [log] [blame]
Fabien Dessenneedbbdad2019-05-31 15:11:33 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07006#include <cpu_func.h>
Fabien Dessenneedbbdad2019-05-31 15:11:33 +02007#include <dm.h>
8#include <elf.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Fabien Dessenneedbbdad2019-05-31 15:11:33 +020010#include <remoteproc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
13#include <linux/compat.h>
Fabien Dessenneedbbdad2019-05-31 15:11:33 +020014
Fabien Dessenneabb8e212019-10-30 14:38:28 +010015/**
16 * struct resource_table - firmware resource table header
17 * @ver: version number
18 * @num: number of resource entries
19 * @reserved: reserved (must be zero)
20 * @offset: array of offsets pointing at the various resource entries
21 *
22 * A resource table is essentially a list of system resources required
23 * by the remote processor. It may also include configuration entries.
24 * If needed, the remote processor firmware should contain this table
25 * as a dedicated ".resource_table" ELF section.
26 *
27 * Some resources entries are mere announcements, where the host is informed
28 * of specific remoteproc configuration. Other entries require the host to
29 * do something (e.g. allocate a system resource). Sometimes a negotiation
30 * is expected, where the firmware requests a resource, and once allocated,
31 * the host should provide back its details (e.g. address of an allocated
32 * memory region).
33 *
34 * The header of the resource table, as expressed by this structure,
35 * contains a version number (should we need to change this format in the
36 * future), the number of available resource entries, and their offsets
37 * in the table.
38 *
39 * Immediately following this header are the resource entries themselves.
40 */
41struct resource_table {
42 u32 ver;
43 u32 num;
44 u32 reserved[2];
45 u32 offset[0];
46} __packed;
47
Fabien Dessenneedbbdad2019-05-31 15:11:33 +020048/* Basic function to verify ELF32 image format */
49int rproc_elf32_sanity_check(ulong addr, ulong size)
50{
51 Elf32_Ehdr *ehdr;
52 char class;
53
54 if (!addr) {
55 pr_debug("Invalid fw address?\n");
56 return -EFAULT;
57 }
58
59 if (size < sizeof(Elf32_Ehdr)) {
60 pr_debug("Image is too small\n");
61 return -ENOSPC;
62 }
63
64 ehdr = (Elf32_Ehdr *)addr;
65 class = ehdr->e_ident[EI_CLASS];
66
67 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
68 pr_debug("Not an executable ELF32 image\n");
69 return -EPROTONOSUPPORT;
70 }
71
72 /* We assume the firmware has the same endianness as the host */
73# ifdef __LITTLE_ENDIAN
74 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
75# else /* BIG ENDIAN */
76 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
77# endif
78 pr_debug("Unsupported firmware endianness\n");
79 return -EILSEQ;
80 }
81
82 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
83 pr_debug("Image is too small\n");
84 return -ENOSPC;
85 }
86
87 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
88 pr_debug("Image is corrupted (bad magic)\n");
89 return -EBADF;
90 }
91
92 if (ehdr->e_phnum == 0) {
93 pr_debug("No loadable segments\n");
94 return -ENOEXEC;
95 }
96
97 if (ehdr->e_phoff > size) {
98 pr_debug("Firmware size is too small\n");
99 return -ENOSPC;
100 }
101
102 return 0;
103}
104
Lokesh Vutladb1f8202019-09-04 16:01:29 +0530105/* Basic function to verify ELF64 image format */
106int rproc_elf64_sanity_check(ulong addr, ulong size)
107{
108 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
109 char class;
110
111 if (!addr) {
112 pr_debug("Invalid fw address?\n");
113 return -EFAULT;
114 }
115
116 if (size < sizeof(Elf64_Ehdr)) {
117 pr_debug("Image is too small\n");
118 return -ENOSPC;
119 }
120
121 class = ehdr->e_ident[EI_CLASS];
122
123 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
124 pr_debug("Not an executable ELF64 image\n");
125 return -EPROTONOSUPPORT;
126 }
127
128 /* We assume the firmware has the same endianness as the host */
129# ifdef __LITTLE_ENDIAN
130 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
131# else /* BIG ENDIAN */
132 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
133# endif
134 pr_debug("Unsupported firmware endianness\n");
135 return -EILSEQ;
136 }
137
138 if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
139 pr_debug("Image is too small\n");
140 return -ENOSPC;
141 }
142
143 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
144 pr_debug("Image is corrupted (bad magic)\n");
145 return -EBADF;
146 }
147
148 if (ehdr->e_phnum == 0) {
149 pr_debug("No loadable segments\n");
150 return -ENOEXEC;
151 }
152
153 if (ehdr->e_phoff > size) {
154 pr_debug("Firmware size is too small\n");
155 return -ENOSPC;
156 }
157
158 return 0;
159}
160
Lokesh Vutla8db0a472019-09-04 16:01:30 +0530161/* Basic function to verify ELF image format */
162int rproc_elf_sanity_check(ulong addr, ulong size)
163{
164 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
165
166 if (!addr) {
167 dev_err(dev, "Invalid firmware address\n");
168 return -EFAULT;
169 }
170
171 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
172 return rproc_elf64_sanity_check(addr, size);
173 else
174 return rproc_elf32_sanity_check(addr, size);
175}
176
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530177int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200178{
179 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
180 Elf32_Phdr *phdr; /* Program header structure pointer */
181 const struct dm_rproc_ops *ops;
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530182 unsigned int i, ret;
183
184 ret = rproc_elf32_sanity_check(addr, size);
185 if (ret) {
186 dev_err(dev, "Invalid ELF32 Image %d\n", ret);
187 return ret;
188 }
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200189
190 ehdr = (Elf32_Ehdr *)addr;
191 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
192
193 ops = rproc_get_ops(dev);
194
195 /* Load each program header */
Fabien Dessenne67e88172019-09-04 09:53:22 +0200196 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200197 void *dst = (void *)(uintptr_t)phdr->p_paddr;
198 void *src = (void *)addr + phdr->p_offset;
199
200 if (phdr->p_type != PT_LOAD)
201 continue;
202
203 if (ops->device_to_virt)
Lokesh Vutlae18166f2019-09-04 16:01:27 +0530204 dst = ops->device_to_virt(dev, (ulong)dst,
205 phdr->p_memsz);
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200206
207 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
208 i, dst, phdr->p_filesz);
209 if (phdr->p_filesz)
210 memcpy(dst, src, phdr->p_filesz);
211 if (phdr->p_filesz != phdr->p_memsz)
212 memset(dst + phdr->p_filesz, 0x00,
213 phdr->p_memsz - phdr->p_filesz);
214 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
215 roundup((unsigned long)dst + phdr->p_filesz,
216 ARCH_DMA_MINALIGN) -
217 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
Fabien Dessenneedbbdad2019-05-31 15:11:33 +0200218 }
219
220 return 0;
221}
Lokesh Vutladb1f8202019-09-04 16:01:29 +0530222
223int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
224{
225 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
226 u64 da, memsz, filesz, offset;
227 Elf64_Ehdr *ehdr;
228 Elf64_Phdr *phdr;
229 int i, ret = 0;
230 void *ptr;
231
232 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
233
234 if (rproc_elf64_sanity_check(addr, size))
235 return -EINVAL;
236
237 ehdr = (Elf64_Ehdr *)addr;
238 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
239
240 /* go through the available ELF segments */
241 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
242 da = phdr->p_paddr;
243 memsz = phdr->p_memsz;
244 filesz = phdr->p_filesz;
245 offset = phdr->p_offset;
246
247 if (phdr->p_type != PT_LOAD)
248 continue;
249
250 dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
251 __func__, phdr->p_type, da, memsz, filesz);
252
253 ptr = (void *)(uintptr_t)da;
254 if (ops->device_to_virt) {
255 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
256 if (!ptr) {
257 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
258 memsz);
259 ret = -EINVAL;
260 break;
261 }
262 }
263
264 if (filesz)
265 memcpy(ptr, (void *)addr + offset, filesz);
266 if (filesz != memsz)
267 memset(ptr + filesz, 0x00, memsz - filesz);
268
269 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
270 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
271 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
272 }
273
274 return ret;
275}
Lokesh Vutla8db0a472019-09-04 16:01:30 +0530276
277int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
278{
279 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
280
281 if (!addr) {
282 dev_err(dev, "Invalid firmware address\n");
283 return -EFAULT;
284 }
285
286 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
287 return rproc_elf64_load_image(dev, addr, size);
288 else
289 return rproc_elf32_load_image(dev, addr, size);
290}
Lokesh Vutla3275e5d2019-09-04 16:01:31 +0530291
292static ulong rproc_elf32_get_boot_addr(ulong addr)
293{
294 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
295
296 return ehdr->e_entry;
297}
298
299static ulong rproc_elf64_get_boot_addr(ulong addr)
300{
301 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
302
303 return ehdr->e_entry;
304}
305
306ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
307{
308 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
309
310 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
311 return rproc_elf64_get_boot_addr(addr);
312 else
313 return rproc_elf32_get_boot_addr(addr);
314}
Fabien Dessenneabb8e212019-10-30 14:38:28 +0100315
316/*
317 * Search for the resource table in an ELF32 image.
318 * Returns the address of the resource table section if found, NULL if there is
319 * no resource table section, or error pointer.
320 */
321static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
322 ulong fw_addr, ulong fw_size)
323{
324 int ret;
325 unsigned int i;
326 const char *name_table;
327 struct resource_table *table;
328 const u8 *elf_data = (void *)fw_addr;
329 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
330 Elf32_Shdr *shdr;
331
332 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
333 if (ret) {
334 pr_debug("Invalid ELF32 Image %d\n", ret);
335 return ERR_PTR(ret);
336 }
337
338 /* look for the resource table and handle it */
339 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
340 name_table = (const char *)(elf_data +
341 shdr[ehdr->e_shstrndx].sh_offset);
342
343 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
344 u32 size = shdr->sh_size;
345 u32 offset = shdr->sh_offset;
346
347 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
348 continue;
349
350 table = (struct resource_table *)(elf_data + offset);
351
352 /* make sure we have the entire table */
353 if (offset + size > fw_size) {
354 pr_debug("resource table truncated\n");
355 return ERR_PTR(-ENOSPC);
356 }
357
358 /* make sure table has at least the header */
359 if (sizeof(*table) > size) {
360 pr_debug("header-less resource table\n");
361 return ERR_PTR(-ENOSPC);
362 }
363
364 /* we don't support any version beyond the first */
365 if (table->ver != 1) {
366 pr_debug("unsupported fw ver: %d\n", table->ver);
367 return ERR_PTR(-EPROTONOSUPPORT);
368 }
369
370 /* make sure reserved bytes are zeroes */
371 if (table->reserved[0] || table->reserved[1]) {
372 pr_debug("non zero reserved bytes\n");
373 return ERR_PTR(-EBADF);
374 }
375
376 /* make sure the offsets array isn't truncated */
377 if (table->num * sizeof(table->offset[0]) +
378 sizeof(*table) > size) {
379 pr_debug("resource table incomplete\n");
380 return ERR_PTR(-ENOSPC);
381 }
382
383 return shdr;
384 }
385
386 return NULL;
387}
388
389/* Load the resource table from an ELF32 image */
390int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
391 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
392{
393 const struct dm_rproc_ops *ops;
394 Elf32_Shdr *shdr;
395 void *src, *dst;
396
397 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
398 if (!shdr)
399 return -ENODATA;
400 if (IS_ERR(shdr))
401 return PTR_ERR(shdr);
402
403 ops = rproc_get_ops(dev);
404 *rsc_addr = (ulong)shdr->sh_addr;
405 *rsc_size = (ulong)shdr->sh_size;
406
407 src = (void *)fw_addr + shdr->sh_offset;
408 if (ops->device_to_virt)
409 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
410 else
411 dst = (void *)rsc_addr;
412
413 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
414 (ulong)dst, *rsc_size);
415
416 memcpy(dst, src, *rsc_size);
417 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
418 roundup((unsigned long)dst + *rsc_size,
419 ARCH_DMA_MINALIGN) -
420 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
421
422 return 0;
423}
424
425/*
426 * Search for the resource table in an ELF64 image.
427 * Returns the address of the resource table section if found, NULL if there is
428 * no resource table section, or error pointer.
429 */
430static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
431 ulong fw_addr, ulong fw_size)
432{
433 int ret;
434 unsigned int i;
435 const char *name_table;
436 struct resource_table *table;
437 const u8 *elf_data = (void *)fw_addr;
438 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
439 Elf64_Shdr *shdr;
440
441 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
442 if (ret) {
443 pr_debug("Invalid ELF64 Image %d\n", ret);
444 return ERR_PTR(ret);
445 }
446
447 /* look for the resource table and handle it */
448 shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
449 name_table = (const char *)(elf_data +
450 shdr[ehdr->e_shstrndx].sh_offset);
451
452 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
453 u64 size = shdr->sh_size;
454 u64 offset = shdr->sh_offset;
455
456 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
457 continue;
458
459 table = (struct resource_table *)(elf_data + offset);
460
461 /* make sure we have the entire table */
462 if (offset + size > fw_size) {
463 pr_debug("resource table truncated\n");
464 return ERR_PTR(-ENOSPC);
465 }
466
467 /* make sure table has at least the header */
468 if (sizeof(*table) > size) {
469 pr_debug("header-less resource table\n");
470 return ERR_PTR(-ENOSPC);
471 }
472
473 /* we don't support any version beyond the first */
474 if (table->ver != 1) {
475 pr_debug("unsupported fw ver: %d\n", table->ver);
476 return ERR_PTR(-EPROTONOSUPPORT);
477 }
478
479 /* make sure reserved bytes are zeroes */
480 if (table->reserved[0] || table->reserved[1]) {
481 pr_debug("non zero reserved bytes\n");
482 return ERR_PTR(-EBADF);
483 }
484
485 /* make sure the offsets array isn't truncated */
486 if (table->num * sizeof(table->offset[0]) +
487 sizeof(*table) > size) {
488 pr_debug("resource table incomplete\n");
489 return ERR_PTR(-ENOSPC);
490 }
491
492 return shdr;
493 }
494
495 return NULL;
496}
497
498/* Load the resource table from an ELF64 image */
499int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
500 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
501{
502 const struct dm_rproc_ops *ops;
503 Elf64_Shdr *shdr;
504 void *src, *dst;
505
506 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
507 if (!shdr)
508 return -ENODATA;
509 if (IS_ERR(shdr))
510 return PTR_ERR(shdr);
511
512 ops = rproc_get_ops(dev);
513 *rsc_addr = (ulong)shdr->sh_addr;
514 *rsc_size = (ulong)shdr->sh_size;
515
516 src = (void *)fw_addr + shdr->sh_offset;
517 if (ops->device_to_virt)
518 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
519 else
520 dst = (void *)rsc_addr;
521
522 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
523 (ulong)dst, *rsc_size);
524
525 memcpy(dst, src, *rsc_size);
526 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
527 roundup((unsigned long)dst + *rsc_size,
528 ARCH_DMA_MINALIGN) -
529 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
530
531 return 0;
532}
533
534/* Load the resource table from an ELF32 or ELF64 image */
535int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
536 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
537
538{
539 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
540
541 if (!fw_addr)
542 return -EFAULT;
543
544 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
545 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
546 rsc_addr, rsc_size);
547 else
548 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
549 rsc_addr, rsc_size);
550}