remoteproc_elf_loader.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. static int
  39. rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
  40. {
  41. const char *name = rproc->firmware;
  42. struct device *dev = &rproc->dev;
  43. struct elf32_hdr *ehdr;
  44. char class;
  45. if (!fw) {
  46. dev_err(dev, "failed to load %s\n", name);
  47. return -EINVAL;
  48. }
  49. if (fw->size < sizeof(struct elf32_hdr)) {
  50. dev_err(dev, "Image is too small\n");
  51. return -EINVAL;
  52. }
  53. ehdr = (struct elf32_hdr *)fw->data;
  54. /* We only support ELF32 at this point */
  55. class = ehdr->e_ident[EI_CLASS];
  56. if (class != ELFCLASS32) {
  57. dev_err(dev, "Unsupported class: %d\n", class);
  58. return -EINVAL;
  59. }
  60. /* We assume the firmware has the same endianness as the host */
  61. # ifdef __LITTLE_ENDIAN
  62. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  63. # else /* BIG ENDIAN */
  64. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  65. # endif
  66. dev_err(dev, "Unsupported firmware endianness\n");
  67. return -EINVAL;
  68. }
  69. if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  70. dev_err(dev, "Image is too small\n");
  71. return -EINVAL;
  72. }
  73. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  74. dev_err(dev, "Image is corrupted (bad magic)\n");
  75. return -EINVAL;
  76. }
  77. if (ehdr->e_phnum == 0) {
  78. dev_err(dev, "No loadable segments\n");
  79. return -EINVAL;
  80. }
  81. if (ehdr->e_phoff > fw->size) {
  82. dev_err(dev, "Firmware size is too small\n");
  83. return -EINVAL;
  84. }
  85. return 0;
  86. }
  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. static
  99. u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
  100. {
  101. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  102. return ehdr->e_entry;
  103. }
  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. static int
  129. rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
  130. {
  131. struct device *dev = &rproc->dev;
  132. struct elf32_hdr *ehdr;
  133. struct elf32_phdr *phdr;
  134. int i, ret = 0;
  135. const u8 *elf_data = fw->data;
  136. ehdr = (struct elf32_hdr *)elf_data;
  137. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  138. /* go through the available ELF segments */
  139. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  140. u32 da = phdr->p_paddr;
  141. u32 memsz = phdr->p_memsz;
  142. u32 filesz = phdr->p_filesz;
  143. u32 offset = phdr->p_offset;
  144. void *ptr;
  145. if (phdr->p_type != PT_LOAD)
  146. continue;
  147. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  148. phdr->p_type, da, memsz, filesz);
  149. if (filesz > memsz) {
  150. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  151. filesz, memsz);
  152. ret = -EINVAL;
  153. break;
  154. }
  155. if (offset + filesz > fw->size) {
  156. dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
  157. offset + filesz, fw->size);
  158. ret = -EINVAL;
  159. break;
  160. }
  161. /* grab the kernel address for this device address */
  162. ptr = rproc_da_to_va(rproc, da, memsz);
  163. if (!ptr) {
  164. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  165. ret = -EINVAL;
  166. break;
  167. }
  168. /* put the segment where the remote processor expects it */
  169. if (phdr->p_filesz)
  170. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  171. /*
  172. * Zero out remaining memory for this segment.
  173. *
  174. * This isn't strictly required since dma_alloc_coherent already
  175. * did this for us. albeit harmless, we may consider removing
  176. * this.
  177. */
  178. if (memsz > filesz)
  179. memset(ptr + filesz, 0, memsz - filesz);
  180. }
  181. return ret;
  182. }
  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_find_rsc_table() - find the resource table
  232. * @rproc: the rproc handle
  233. * @fw: the ELF firmware image
  234. * @tablesz: place holder for providing back the table size
  235. *
  236. * This function finds the resource table inside the remote processor's
  237. * firmware. It is used both upon the registration of @rproc (in order
  238. * to look for and register the supported virito devices), and when the
  239. * @rproc is booted.
  240. *
  241. * Returns the pointer to the resource table if it is found, and write its
  242. * size into @tablesz. If a valid table isn't found, NULL is returned
  243. * (and @tablesz isn't set).
  244. */
  245. static struct resource_table *
  246. rproc_elf_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  247. int *tablesz)
  248. {
  249. struct elf32_hdr *ehdr;
  250. struct elf32_shdr *shdr;
  251. struct device *dev = &rproc->dev;
  252. struct resource_table *table = NULL;
  253. const u8 *elf_data = fw->data;
  254. ehdr = (struct elf32_hdr *)elf_data;
  255. shdr = find_table(dev, ehdr, fw->size);
  256. if (!shdr)
  257. return NULL;
  258. table = (struct resource_table *)(elf_data + shdr->sh_offset);
  259. *tablesz = shdr->sh_size;
  260. return table;
  261. }
  262. /**
  263. * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
  264. * @rproc: the rproc handle
  265. * @fw: the ELF firmware image
  266. *
  267. * This function finds the location of the loaded resource table. Don't
  268. * call this function if the table wasn't loaded yet - it's a bug if you do.
  269. *
  270. * Returns the pointer to the resource table if it is found or NULL otherwise.
  271. * If the table wasn't loaded yet the result is unspecified.
  272. */
  273. static struct resource_table *
  274. rproc_elf_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
  275. {
  276. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  277. struct elf32_shdr *shdr;
  278. shdr = find_table(&rproc->dev, ehdr, fw->size);
  279. if (!shdr)
  280. return NULL;
  281. return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
  282. }
  283. const struct rproc_fw_ops rproc_elf_fw_ops = {
  284. .load = rproc_elf_load_segments,
  285. .find_rsc_table = rproc_elf_find_rsc_table,
  286. .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
  287. .sanity_check = rproc_elf_sanity_check,
  288. .get_boot_addr = rproc_elf_get_boot_addr
  289. };