remoteproc_elf_loader.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Remote Processor Framework Elf loader
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Brian Swetland <swetland@google.com>
  9. * Mark Grosen <mgrosen@ti.com>
  10. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11. * Suman Anna <s-anna@ti.com>
  12. * Robert Tivy <rtivy@ti.com>
  13. * Armando Uribe De Leon <x0095078@ti.com>
  14. * Sjur Brændeland <sjur.brandeland@stericsson.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * version 2 as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #define pr_fmt(fmt) "%s: " fmt, __func__
  26. #include <linux/module.h>
  27. #include <linux/firmware.h>
  28. #include <linux/remoteproc.h>
  29. #include <linux/elf.h>
  30. #include "remoteproc_internal.h"
  31. /**
  32. * rproc_elf_sanity_check() - Sanity Check ELF firmware image
  33. * @rproc: the remote processor handle
  34. * @fw: the ELF firmware image
  35. *
  36. * Make sure this fw image is sane.
  37. */
  38. int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
  39. {
  40. const char *name = rproc->firmware;
  41. struct device *dev = &rproc->dev;
  42. struct elf32_hdr *ehdr;
  43. char class;
  44. if (!fw) {
  45. dev_err(dev, "failed to load %s\n", name);
  46. return -EINVAL;
  47. }
  48. if (fw->size < sizeof(struct elf32_hdr)) {
  49. dev_err(dev, "Image is too small\n");
  50. return -EINVAL;
  51. }
  52. ehdr = (struct elf32_hdr *)fw->data;
  53. /* We only support ELF32 at this point */
  54. class = ehdr->e_ident[EI_CLASS];
  55. if (class != ELFCLASS32) {
  56. dev_err(dev, "Unsupported class: %d\n", class);
  57. return -EINVAL;
  58. }
  59. /* We assume the firmware has the same endianness as the host */
  60. # ifdef __LITTLE_ENDIAN
  61. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  62. # else /* BIG ENDIAN */
  63. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  64. # endif
  65. dev_err(dev, "Unsupported firmware endianness\n");
  66. return -EINVAL;
  67. }
  68. if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  69. dev_err(dev, "Image is too small\n");
  70. return -EINVAL;
  71. }
  72. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  73. dev_err(dev, "Image is corrupted (bad magic)\n");
  74. return -EINVAL;
  75. }
  76. if (ehdr->e_phnum == 0) {
  77. dev_err(dev, "No loadable segments\n");
  78. return -EINVAL;
  79. }
  80. if (ehdr->e_phoff > fw->size) {
  81. dev_err(dev, "Firmware size is too small\n");
  82. return -EINVAL;
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(rproc_elf_sanity_check);
  87. /**
  88. * rproc_elf_get_boot_addr() - Get rproc's boot address.
  89. * @rproc: the remote processor handle
  90. * @fw: the ELF firmware image
  91. *
  92. * This function returns the entry point address of the ELF
  93. * image.
  94. *
  95. * Note that the boot address is not a configurable property of all remote
  96. * processors. Some will always boot at a specific hard-coded address.
  97. */
  98. u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
  99. {
  100. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  101. return ehdr->e_entry;
  102. }
  103. EXPORT_SYMBOL(rproc_elf_get_boot_addr);
  104. /**
  105. * rproc_elf_load_segments() - load firmware segments to memory
  106. * @rproc: remote processor which will be booted using these fw segments
  107. * @fw: the ELF firmware image
  108. *
  109. * This function loads the firmware segments to memory, where the remote
  110. * processor expects them.
  111. *
  112. * Some remote processors will expect their code and data to be placed
  113. * in specific device addresses, and can't have them dynamically assigned.
  114. *
  115. * We currently support only those kind of remote processors, and expect
  116. * the program header's paddr member to contain those addresses. We then go
  117. * through the physically contiguous "carveout" memory regions which we
  118. * allocated (and mapped) earlier on behalf of the remote processor,
  119. * and "translate" device address to kernel addresses, so we can copy the
  120. * segments where they are expected.
  121. *
  122. * Currently we only support remote processors that required carveout
  123. * allocations and got them mapped onto their iommus. Some processors
  124. * might be different: they might not have iommus, and would prefer to
  125. * directly allocate memory for every segment/resource. This is not yet
  126. * supported, though.
  127. */
  128. int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
  129. {
  130. struct device *dev = &rproc->dev;
  131. struct elf32_hdr *ehdr;
  132. struct elf32_phdr *phdr;
  133. int i, ret = 0;
  134. const u8 *elf_data = fw->data;
  135. ehdr = (struct elf32_hdr *)elf_data;
  136. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  137. /* go through the available ELF segments */
  138. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  139. u32 da = phdr->p_paddr;
  140. u32 memsz = phdr->p_memsz;
  141. u32 filesz = phdr->p_filesz;
  142. u32 offset = phdr->p_offset;
  143. void *ptr;
  144. if (phdr->p_type != PT_LOAD)
  145. continue;
  146. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  147. phdr->p_type, da, memsz, filesz);
  148. if (filesz > memsz) {
  149. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  150. filesz, memsz);
  151. ret = -EINVAL;
  152. break;
  153. }
  154. if (offset + filesz > fw->size) {
  155. dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
  156. offset + filesz, fw->size);
  157. ret = -EINVAL;
  158. break;
  159. }
  160. /* grab the kernel address for this device address */
  161. ptr = rproc_da_to_va(rproc, da, memsz);
  162. if (!ptr) {
  163. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  164. ret = -EINVAL;
  165. break;
  166. }
  167. /* put the segment where the remote processor expects it */
  168. if (phdr->p_filesz)
  169. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  170. /*
  171. * Zero out remaining memory for this segment.
  172. *
  173. * This isn't strictly required since dma_alloc_coherent already
  174. * did this for us. albeit harmless, we may consider removing
  175. * this.
  176. */
  177. if (memsz > filesz)
  178. memset(ptr + filesz, 0, memsz - filesz);
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(rproc_elf_load_segments);
  183. static struct elf32_shdr *
  184. find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  185. {
  186. struct elf32_shdr *shdr;
  187. int i;
  188. const char *name_table;
  189. struct resource_table *table = NULL;
  190. const u8 *elf_data = (void *)ehdr;
  191. /* look for the resource table and handle it */
  192. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  193. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  194. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  195. u32 size = shdr->sh_size;
  196. u32 offset = shdr->sh_offset;
  197. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  198. continue;
  199. table = (struct resource_table *)(elf_data + offset);
  200. /* make sure we have the entire table */
  201. if (offset + size > fw_size || offset + size < size) {
  202. dev_err(dev, "resource table truncated\n");
  203. return NULL;
  204. }
  205. /* make sure table has at least the header */
  206. if (sizeof(struct resource_table) > size) {
  207. dev_err(dev, "header-less resource table\n");
  208. return NULL;
  209. }
  210. /* we don't support any version beyond the first */
  211. if (table->ver != 1) {
  212. dev_err(dev, "unsupported fw ver: %d\n", table->ver);
  213. return NULL;
  214. }
  215. /* make sure reserved bytes are zeroes */
  216. if (table->reserved[0] || table->reserved[1]) {
  217. dev_err(dev, "non zero reserved bytes\n");
  218. return NULL;
  219. }
  220. /* make sure the offsets array isn't truncated */
  221. if (table->num * sizeof(table->offset[0]) +
  222. sizeof(struct resource_table) > size) {
  223. dev_err(dev, "resource table incomplete\n");
  224. return NULL;
  225. }
  226. return shdr;
  227. }
  228. return NULL;
  229. }
  230. /**
  231. * rproc_elf_load_rsc_table() - load the resource table
  232. * @rproc: the rproc handle
  233. * @fw: the ELF firmware image
  234. *
  235. * This function finds the resource table inside the remote processor's
  236. * firmware, load it into the @cached_table and update @table_ptr.
  237. *
  238. * Return: 0 on success, negative errno on failure.
  239. */
  240. int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
  241. {
  242. struct elf32_hdr *ehdr;
  243. struct elf32_shdr *shdr;
  244. struct device *dev = &rproc->dev;
  245. struct resource_table *table = NULL;
  246. const u8 *elf_data = fw->data;
  247. size_t tablesz;
  248. ehdr = (struct elf32_hdr *)elf_data;
  249. shdr = find_table(dev, ehdr, fw->size);
  250. if (!shdr)
  251. return -EINVAL;
  252. table = (struct resource_table *)(elf_data + shdr->sh_offset);
  253. tablesz = shdr->sh_size;
  254. /*
  255. * Create a copy of the resource table. When a virtio device starts
  256. * and calls vring_new_virtqueue() the address of the allocated vring
  257. * will be stored in the cached_table. Before the device is started,
  258. * cached_table will be copied into device memory.
  259. */
  260. rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
  261. if (!rproc->cached_table)
  262. return -ENOMEM;
  263. rproc->table_ptr = rproc->cached_table;
  264. rproc->table_sz = tablesz;
  265. return 0;
  266. }
  267. EXPORT_SYMBOL(rproc_elf_load_rsc_table);
  268. /**
  269. * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
  270. * @rproc: the rproc handle
  271. * @fw: the ELF firmware image
  272. *
  273. * This function finds the location of the loaded resource table. Don't
  274. * call this function if the table wasn't loaded yet - it's a bug if you do.
  275. *
  276. * Returns the pointer to the resource table if it is found or NULL otherwise.
  277. * If the table wasn't loaded yet the result is unspecified.
  278. */
  279. struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
  280. const struct firmware *fw)
  281. {
  282. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  283. struct elf32_shdr *shdr;
  284. shdr = find_table(&rproc->dev, ehdr, fw->size);
  285. if (!shdr)
  286. return NULL;
  287. return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
  288. }
  289. EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);