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