rproc-elf-loader.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 <log.h>
  10. #include <remoteproc.h>
  11. #include <asm/cache.h>
  12. #include <dm/device_compat.h>
  13. #include <linux/compat.h>
  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. /* Basic function to verify ELF32 image format */
  47. int rproc_elf32_sanity_check(ulong addr, ulong size)
  48. {
  49. Elf32_Ehdr *ehdr;
  50. char class;
  51. if (!addr) {
  52. pr_debug("Invalid fw address?\n");
  53. return -EFAULT;
  54. }
  55. if (size < sizeof(Elf32_Ehdr)) {
  56. pr_debug("Image is too small\n");
  57. return -ENOSPC;
  58. }
  59. ehdr = (Elf32_Ehdr *)addr;
  60. class = ehdr->e_ident[EI_CLASS];
  61. if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
  62. pr_debug("Not an executable ELF32 image\n");
  63. return -EPROTONOSUPPORT;
  64. }
  65. /* We assume the firmware has the same endianness as the host */
  66. # ifdef __LITTLE_ENDIAN
  67. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  68. # else /* BIG ENDIAN */
  69. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  70. # endif
  71. pr_debug("Unsupported firmware endianness\n");
  72. return -EILSEQ;
  73. }
  74. if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
  75. pr_debug("Image is too small\n");
  76. return -ENOSPC;
  77. }
  78. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  79. pr_debug("Image is corrupted (bad magic)\n");
  80. return -EBADF;
  81. }
  82. if (ehdr->e_phnum == 0) {
  83. pr_debug("No loadable segments\n");
  84. return -ENOEXEC;
  85. }
  86. if (ehdr->e_phoff > size) {
  87. pr_debug("Firmware size is too small\n");
  88. return -ENOSPC;
  89. }
  90. return 0;
  91. }
  92. /* Basic function to verify ELF64 image format */
  93. int rproc_elf64_sanity_check(ulong addr, ulong size)
  94. {
  95. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
  96. char class;
  97. if (!addr) {
  98. pr_debug("Invalid fw address?\n");
  99. return -EFAULT;
  100. }
  101. if (size < sizeof(Elf64_Ehdr)) {
  102. pr_debug("Image is too small\n");
  103. return -ENOSPC;
  104. }
  105. class = ehdr->e_ident[EI_CLASS];
  106. if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
  107. pr_debug("Not an executable ELF64 image\n");
  108. return -EPROTONOSUPPORT;
  109. }
  110. /* We assume the firmware has the same endianness as the host */
  111. # ifdef __LITTLE_ENDIAN
  112. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  113. # else /* BIG ENDIAN */
  114. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  115. # endif
  116. pr_debug("Unsupported firmware endianness\n");
  117. return -EILSEQ;
  118. }
  119. if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
  120. pr_debug("Image is too small\n");
  121. return -ENOSPC;
  122. }
  123. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  124. pr_debug("Image is corrupted (bad magic)\n");
  125. return -EBADF;
  126. }
  127. if (ehdr->e_phnum == 0) {
  128. pr_debug("No loadable segments\n");
  129. return -ENOEXEC;
  130. }
  131. if (ehdr->e_phoff > size) {
  132. pr_debug("Firmware size is too small\n");
  133. return -ENOSPC;
  134. }
  135. return 0;
  136. }
  137. int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
  138. {
  139. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  140. Elf32_Phdr *phdr; /* Program header structure pointer */
  141. const struct dm_rproc_ops *ops;
  142. unsigned int i, ret;
  143. ret = rproc_elf32_sanity_check(addr, size);
  144. if (ret) {
  145. dev_err(dev, "Invalid ELF32 Image %d\n", ret);
  146. return ret;
  147. }
  148. ehdr = (Elf32_Ehdr *)addr;
  149. phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
  150. ops = rproc_get_ops(dev);
  151. /* Load each program header */
  152. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  153. void *dst = (void *)(uintptr_t)phdr->p_paddr;
  154. void *src = (void *)addr + phdr->p_offset;
  155. if (phdr->p_type != PT_LOAD)
  156. continue;
  157. if (ops->device_to_virt)
  158. dst = ops->device_to_virt(dev, (ulong)dst,
  159. phdr->p_memsz);
  160. dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
  161. i, dst, phdr->p_filesz);
  162. if (phdr->p_filesz)
  163. memcpy(dst, src, phdr->p_filesz);
  164. if (phdr->p_filesz != phdr->p_memsz)
  165. memset(dst + phdr->p_filesz, 0x00,
  166. phdr->p_memsz - phdr->p_filesz);
  167. flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
  168. roundup((unsigned long)dst + phdr->p_filesz,
  169. ARCH_DMA_MINALIGN) -
  170. rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
  171. }
  172. return 0;
  173. }
  174. int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
  175. {
  176. const struct dm_rproc_ops *ops = rproc_get_ops(dev);
  177. u64 da, memsz, filesz, offset;
  178. Elf64_Ehdr *ehdr;
  179. Elf64_Phdr *phdr;
  180. int i, ret = 0;
  181. void *ptr;
  182. dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
  183. if (rproc_elf64_sanity_check(addr, size))
  184. return -EINVAL;
  185. ehdr = (Elf64_Ehdr *)addr;
  186. phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
  187. /* go through the available ELF segments */
  188. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  189. da = phdr->p_paddr;
  190. memsz = phdr->p_memsz;
  191. filesz = phdr->p_filesz;
  192. offset = phdr->p_offset;
  193. if (phdr->p_type != PT_LOAD)
  194. continue;
  195. dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
  196. __func__, phdr->p_type, da, memsz, filesz);
  197. ptr = (void *)(uintptr_t)da;
  198. if (ops->device_to_virt) {
  199. ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
  200. if (!ptr) {
  201. dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
  202. memsz);
  203. ret = -EINVAL;
  204. break;
  205. }
  206. }
  207. if (filesz)
  208. memcpy(ptr, (void *)addr + offset, filesz);
  209. if (filesz != memsz)
  210. memset(ptr + filesz, 0x00, memsz - filesz);
  211. flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
  212. roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
  213. rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
  214. }
  215. return ret;
  216. }
  217. int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
  218. {
  219. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
  220. if (!addr) {
  221. dev_err(dev, "Invalid firmware address\n");
  222. return -EFAULT;
  223. }
  224. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  225. return rproc_elf64_load_image(dev, addr, size);
  226. else
  227. return rproc_elf32_load_image(dev, addr, size);
  228. }
  229. static ulong rproc_elf32_get_boot_addr(ulong addr)
  230. {
  231. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
  232. return ehdr->e_entry;
  233. }
  234. static ulong rproc_elf64_get_boot_addr(ulong addr)
  235. {
  236. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
  237. return ehdr->e_entry;
  238. }
  239. ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
  240. {
  241. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
  242. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  243. return rproc_elf64_get_boot_addr(addr);
  244. else
  245. return rproc_elf32_get_boot_addr(addr);
  246. }
  247. /*
  248. * Search for the resource table in an ELF32 image.
  249. * Returns the address of the resource table section if found, NULL if there is
  250. * no resource table section, or error pointer.
  251. */
  252. static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
  253. ulong fw_addr, ulong fw_size)
  254. {
  255. int ret;
  256. unsigned int i;
  257. const char *name_table;
  258. struct resource_table *table;
  259. const u8 *elf_data = (void *)fw_addr;
  260. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
  261. Elf32_Shdr *shdr;
  262. ret = rproc_elf32_sanity_check(fw_addr, fw_size);
  263. if (ret) {
  264. pr_debug("Invalid ELF32 Image %d\n", ret);
  265. return ERR_PTR(ret);
  266. }
  267. /* look for the resource table and handle it */
  268. shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
  269. name_table = (const char *)(elf_data +
  270. shdr[ehdr->e_shstrndx].sh_offset);
  271. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  272. u32 size = shdr->sh_size;
  273. u32 offset = shdr->sh_offset;
  274. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  275. continue;
  276. table = (struct resource_table *)(elf_data + offset);
  277. /* make sure we have the entire table */
  278. if (offset + size > fw_size) {
  279. pr_debug("resource table truncated\n");
  280. return ERR_PTR(-ENOSPC);
  281. }
  282. /* make sure table has at least the header */
  283. if (sizeof(*table) > size) {
  284. pr_debug("header-less resource table\n");
  285. return ERR_PTR(-ENOSPC);
  286. }
  287. /* we don't support any version beyond the first */
  288. if (table->ver != 1) {
  289. pr_debug("unsupported fw ver: %d\n", table->ver);
  290. return ERR_PTR(-EPROTONOSUPPORT);
  291. }
  292. /* make sure reserved bytes are zeroes */
  293. if (table->reserved[0] || table->reserved[1]) {
  294. pr_debug("non zero reserved bytes\n");
  295. return ERR_PTR(-EBADF);
  296. }
  297. /* make sure the offsets array isn't truncated */
  298. if (table->num * sizeof(table->offset[0]) +
  299. sizeof(*table) > size) {
  300. pr_debug("resource table incomplete\n");
  301. return ERR_PTR(-ENOSPC);
  302. }
  303. return shdr;
  304. }
  305. return NULL;
  306. }
  307. /* Load the resource table from an ELF32 image */
  308. int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
  309. ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
  310. {
  311. const struct dm_rproc_ops *ops;
  312. Elf32_Shdr *shdr;
  313. void *src, *dst;
  314. shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
  315. if (!shdr)
  316. return -ENODATA;
  317. if (IS_ERR(shdr))
  318. return PTR_ERR(shdr);
  319. ops = rproc_get_ops(dev);
  320. *rsc_addr = (ulong)shdr->sh_addr;
  321. *rsc_size = (ulong)shdr->sh_size;
  322. src = (void *)fw_addr + shdr->sh_offset;
  323. if (ops->device_to_virt)
  324. dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
  325. else
  326. dst = (void *)rsc_addr;
  327. dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
  328. (ulong)dst, *rsc_size);
  329. memcpy(dst, src, *rsc_size);
  330. flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
  331. roundup((unsigned long)dst + *rsc_size,
  332. ARCH_DMA_MINALIGN) -
  333. rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
  334. return 0;
  335. }
  336. /*
  337. * Search for the resource table in an ELF64 image.
  338. * Returns the address of the resource table section if found, NULL if there is
  339. * no resource table section, or error pointer.
  340. */
  341. static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
  342. ulong fw_addr, ulong fw_size)
  343. {
  344. int ret;
  345. unsigned int i;
  346. const char *name_table;
  347. struct resource_table *table;
  348. const u8 *elf_data = (void *)fw_addr;
  349. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
  350. Elf64_Shdr *shdr;
  351. ret = rproc_elf64_sanity_check(fw_addr, fw_size);
  352. if (ret) {
  353. pr_debug("Invalid ELF64 Image %d\n", ret);
  354. return ERR_PTR(ret);
  355. }
  356. /* look for the resource table and handle it */
  357. shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
  358. name_table = (const char *)(elf_data +
  359. shdr[ehdr->e_shstrndx].sh_offset);
  360. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  361. u64 size = shdr->sh_size;
  362. u64 offset = shdr->sh_offset;
  363. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  364. continue;
  365. table = (struct resource_table *)(elf_data + offset);
  366. /* make sure we have the entire table */
  367. if (offset + size > fw_size) {
  368. pr_debug("resource table truncated\n");
  369. return ERR_PTR(-ENOSPC);
  370. }
  371. /* make sure table has at least the header */
  372. if (sizeof(*table) > size) {
  373. pr_debug("header-less resource table\n");
  374. return ERR_PTR(-ENOSPC);
  375. }
  376. /* we don't support any version beyond the first */
  377. if (table->ver != 1) {
  378. pr_debug("unsupported fw ver: %d\n", table->ver);
  379. return ERR_PTR(-EPROTONOSUPPORT);
  380. }
  381. /* make sure reserved bytes are zeroes */
  382. if (table->reserved[0] || table->reserved[1]) {
  383. pr_debug("non zero reserved bytes\n");
  384. return ERR_PTR(-EBADF);
  385. }
  386. /* make sure the offsets array isn't truncated */
  387. if (table->num * sizeof(table->offset[0]) +
  388. sizeof(*table) > size) {
  389. pr_debug("resource table incomplete\n");
  390. return ERR_PTR(-ENOSPC);
  391. }
  392. return shdr;
  393. }
  394. return NULL;
  395. }
  396. /* Load the resource table from an ELF64 image */
  397. int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
  398. ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
  399. {
  400. const struct dm_rproc_ops *ops;
  401. Elf64_Shdr *shdr;
  402. void *src, *dst;
  403. shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
  404. if (!shdr)
  405. return -ENODATA;
  406. if (IS_ERR(shdr))
  407. return PTR_ERR(shdr);
  408. ops = rproc_get_ops(dev);
  409. *rsc_addr = (ulong)shdr->sh_addr;
  410. *rsc_size = (ulong)shdr->sh_size;
  411. src = (void *)fw_addr + shdr->sh_offset;
  412. if (ops->device_to_virt)
  413. dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
  414. else
  415. dst = (void *)rsc_addr;
  416. dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
  417. (ulong)dst, *rsc_size);
  418. memcpy(dst, src, *rsc_size);
  419. flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
  420. roundup((unsigned long)dst + *rsc_size,
  421. ARCH_DMA_MINALIGN) -
  422. rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
  423. return 0;
  424. }
  425. /* Load the resource table from an ELF32 or ELF64 image */
  426. int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
  427. ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
  428. {
  429. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
  430. if (!fw_addr)
  431. return -EFAULT;
  432. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  433. return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
  434. rsc_addr, rsc_size);
  435. else
  436. return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
  437. rsc_addr, rsc_size);
  438. }