remoteproc_elf_loader.c 11 KB

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