remoteproc_elf_loader.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. RPROC_FLAGS_ELF_PHDR | phdr->p_flags);
  164. if (!ptr) {
  165. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  166. ret = -EINVAL;
  167. break;
  168. }
  169. /* put the segment where the remote processor expects it */
  170. if (phdr->p_filesz)
  171. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  172. /*
  173. * Zero out remaining memory for this segment.
  174. *
  175. * This isn't strictly required since dma_alloc_coherent already
  176. * did this for us. albeit harmless, we may consider removing
  177. * this.
  178. */
  179. if (memsz > filesz)
  180. memset(ptr + filesz, 0, memsz - filesz);
  181. }
  182. return ret;
  183. }
  184. static struct elf32_shdr *
  185. find_table(struct device *dev, struct elf32_hdr *ehdr, size_t fw_size)
  186. {
  187. struct elf32_shdr *shdr;
  188. int i;
  189. const char *name_table;
  190. struct resource_table *table = NULL;
  191. const u8 *elf_data = (void *)ehdr;
  192. /* look for the resource table and handle it */
  193. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  194. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  195. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  196. u32 size = shdr->sh_size;
  197. u32 offset = shdr->sh_offset;
  198. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  199. continue;
  200. table = (struct resource_table *)(elf_data + offset);
  201. /* make sure we have the entire table */
  202. if (offset + size > fw_size || offset + size < size) {
  203. dev_err(dev, "resource table truncated\n");
  204. return NULL;
  205. }
  206. /* make sure table has at least the header */
  207. if (sizeof(struct resource_table) > size) {
  208. dev_err(dev, "header-less resource table\n");
  209. return NULL;
  210. }
  211. /* we don't support any version beyond the first */
  212. if (table->ver != 1) {
  213. dev_err(dev, "unsupported fw ver: %d\n", table->ver);
  214. return NULL;
  215. }
  216. /* make sure reserved bytes are zeroes */
  217. if (table->reserved[0] || table->reserved[1]) {
  218. dev_err(dev, "non zero reserved bytes\n");
  219. return NULL;
  220. }
  221. /* make sure the offsets array isn't truncated */
  222. if (table->num * sizeof(table->offset[0]) +
  223. sizeof(struct resource_table) > size) {
  224. dev_err(dev, "resource table incomplete\n");
  225. return NULL;
  226. }
  227. return shdr;
  228. }
  229. return NULL;
  230. }
  231. /**
  232. * rproc_elf_find_rsc_table() - find the resource table
  233. * @rproc: the rproc handle
  234. * @fw: the ELF firmware image
  235. * @tablesz: place holder for providing back the table size
  236. *
  237. * This function finds the resource table inside the remote processor's
  238. * firmware. It is used both upon the registration of @rproc (in order
  239. * to look for and register the supported virito devices), and when the
  240. * @rproc is booted.
  241. *
  242. * Returns the pointer to the resource table if it is found, and write its
  243. * size into @tablesz. If a valid table isn't found, NULL is returned
  244. * (and @tablesz isn't set).
  245. */
  246. static struct resource_table *
  247. rproc_elf_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  248. int *tablesz)
  249. {
  250. struct elf32_hdr *ehdr;
  251. struct elf32_shdr *shdr;
  252. struct device *dev = &rproc->dev;
  253. struct resource_table *table = NULL;
  254. const u8 *elf_data = fw->data;
  255. ehdr = (struct elf32_hdr *)elf_data;
  256. shdr = find_table(dev, ehdr, fw->size);
  257. if (!shdr)
  258. return NULL;
  259. table = (struct resource_table *)(elf_data + shdr->sh_offset);
  260. *tablesz = shdr->sh_size;
  261. return table;
  262. }
  263. /**
  264. * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
  265. * @rproc: the rproc handle
  266. * @fw: the ELF firmware image
  267. *
  268. * This function finds the location of the loaded resource table. Don't
  269. * call this function if the table wasn't loaded yet - it's a bug if you do.
  270. *
  271. * Returns the pointer to the resource table if it is found or NULL otherwise.
  272. * If the table wasn't loaded yet the result is unspecified.
  273. */
  274. static struct resource_table *
  275. rproc_elf_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
  276. {
  277. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  278. struct elf32_shdr *shdr;
  279. shdr = find_table(&rproc->dev, ehdr, fw->size);
  280. if (!shdr)
  281. return NULL;
  282. return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size,
  283. RPROC_FLAGS_ELF_SHDR | shdr->sh_flags);
  284. }
  285. /**
  286. * rproc_elf_find_version_section() - find the .version section
  287. * @rproc: the rproc handle
  288. * @fw: the ELF firmware image
  289. * @len: firmware size (in bytes)
  290. * @versz: place holder for providing back the version size
  291. *
  292. * This function finds the .version section inside the remote processor's
  293. * firmware. It is used to provide any version information for the
  294. * firmware.
  295. *
  296. * Returns the pointer to the .version section if it is found, and write its
  297. * size into @versz. If a valid version isn't found, NULL is returned
  298. * (and @versz isn't set).
  299. */
  300. static const char *
  301. rproc_elf_find_version_section(struct rproc *rproc, const struct firmware *fw,
  302. int *versz)
  303. {
  304. struct elf32_hdr *ehdr;
  305. struct elf32_shdr *shdr;
  306. const char *name_table;
  307. struct device *dev = &rproc->dev;
  308. const char *vdata = NULL;
  309. int i;
  310. const u8 *elf_data = fw->data;
  311. ehdr = (struct elf32_hdr *)elf_data;
  312. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  313. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  314. /* look for the version section */
  315. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  316. int size = shdr->sh_size;
  317. int offset = shdr->sh_offset;
  318. if (strcmp(name_table + shdr->sh_name, ".version"))
  319. continue;
  320. vdata = (char *)(elf_data + offset);
  321. /* make sure we have the entire section */
  322. if (offset + size > fw->size) {
  323. dev_err(dev, "version info truncated\n");
  324. return NULL;
  325. }
  326. *versz = shdr->sh_size;
  327. break;
  328. }
  329. return vdata;
  330. }
  331. const struct rproc_fw_ops rproc_elf_fw_ops = {
  332. .load = rproc_elf_load_segments,
  333. .find_rsc_table = rproc_elf_find_rsc_table,
  334. .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
  335. .sanity_check = rproc_elf_sanity_check,
  336. .get_boot_addr = rproc_elf_get_boot_addr,
  337. .find_version = rproc_elf_find_version_section
  338. };