You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

446 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "elf_loader.h"
  2. /*
  3. * Convert PRU device address (data spaces only) to kernel virtual address
  4. *
  5. * Each PRU has access to all data memories within the PRUSS, accessible at
  6. * different ranges. So, look through both its primary and secondary Data
  7. * RAMs as well as any shared Data RAM to convert a PRU device address to
  8. * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
  9. * RAM1 is primary Data RAM for PRU1.
  10. */
  11. static void *pru_d_da_to_va( struct pru_rproc *pru, uint32_t da, int len){
  12. struct pruss_mem_region dram0, dram1, shrd_ram;
  13. struct pruss *pruss = pru->pruss;
  14. u32 offset;
  15. void *va = NULL;
  16. if( len <= 0 )
  17. return NULL;
  18. dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
  19. dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
  20. /* PRU1 has its local RAM addresses reversed */
  21. if (pru->id == 1)
  22. swap(dram0, dram1);
  23. shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
  24. if (da >= pru->pdram_da && da + len <= pru->pdram_da + dram0.size) {
  25. offset = da - pru->pdram_da;
  26. va = (__force void *)(dram0.va + offset);
  27. } else if (da >= pru->sdram_da &&
  28. da + len <= pru->sdram_da + dram1.size) {
  29. offset = da - pru->sdram_da;
  30. va = (__force void *)(dram1.va + offset);
  31. } else if (da >= pru->shrdram_da &&
  32. da + len <= pru->shrdram_da + shrd_ram.size) {
  33. offset = da - pru->shrdram_da;
  34. va = (__force void *)(shrd_ram.va + offset);
  35. };
  36. return va;
  37. };
  38. /*
  39. * Convert PRU device address (instruction space) to kernel virtual address
  40. *
  41. * A PRU does not have an unified address space. Each PRU has its very own
  42. * private Instruction RAM, and its device address is identical to that of
  43. * its primary Data RAM device address.
  44. */
  45. static void *pru_i_da_to_va( struct pru_rproc *pru, uint32_t da, int len ){
  46. u32 offset;
  47. void *va = NULL;
  48. if (len <= 0)
  49. return NULL;
  50. if (da >= pru->iram_da &&
  51. da + len <= pru->iram_da + pru->mem_regions[PRU_MEM_IRAM].size) {
  52. offset = da - pru->iram_da;
  53. va = (__force void *)(pru->mem_regions[PRU_MEM_IRAM].va +
  54. offset);
  55. };
  56. return va;
  57. };
  58. /* PRU-specific address translator */
  59. static void *pru_da_to_va( struct rproc *rproc, uint64_t da, int len, uint32_t flags ){
  60. struct pru_rproc *pru = rproc->priv;
  61. void *va;
  62. uint32_t exec_flag = 0;
  63. exec_flag = ((flags & RPROC_FLAGS_ELF_SHDR ) ? flags & SHF_EXECINSTR :
  64. ((flags & RPROC_FLAGS_ELF_PHDR ) ? flags & PF_X : 0));
  65. if( exec_flag )
  66. va = pru_i_da_to_va(pru, da, len);
  67. else
  68. va = pru_d_da_to_va(pru, da, len);
  69. return va;
  70. };
  71. static int load_elf_fw( const char *name, const struct firmware *fw ){
  72. FILE *FD = NULL;
  73. FD = fopen( name, "rb" );
  74. if( FD != NULL ){
  75. printf( "Firmware is opened\n" );
  76. // Read file size
  77. fseek( FD, 0, SEEK_END);
  78. fw->size = ftell( FD );
  79. if( fw->size == 0 ){
  80. printf("File read failed.. Aborting\n");
  81. fclose( FD );
  82. return -1;
  83. };
  84. fseek( FD, 0, SEEK_SET );
  85. fw->data = ( uint8_t * ) malloc( fw->size );
  86. if( fw->data != NULL ){
  87. memset( fw->data, 0, fw->size );
  88. if( fw->size !=
  89. fread((unsigned char *) fw->data, 1, fw->size, FD )) {
  90. printf("WARNING: File Size mismatch\n");
  91. }
  92. fclose( FD );
  93. printf( "Firmware is loadded\n" );
  94. printf( "Firmware size is %d bytes\n", fw->size );
  95. return 0;
  96. };
  97. } else {
  98. printf( "Firmware open failure\n" );
  99. };
  100. return -1;
  101. };
  102. /**
  103. * rproc_elf_sanity_check() - Sanity Check ELF firmware image
  104. * @rproc: the remote processor handle
  105. * @fw: the ELF firmware image
  106. *
  107. * Make sure this fw image is sane.
  108. */
  109. static int elf_sanity_check( /*struct rproc *rproc, */ const struct firmware *fw ){
  110. //const char *name = rproc->firmware;
  111. //struct device *dev = &rproc->dev;
  112. struct elf32_hdr *ehdr;
  113. char class;
  114. /*
  115. if( !fw ){
  116. printf( "failed to load %s\n", name );
  117. return -EINVAL;
  118. };
  119. */
  120. if ( fw->size < sizeof(struct elf32_hdr ) ){
  121. printf("Image is too small\n");
  122. return -EINVAL;
  123. };
  124. ehdr = (struct elf32_hdr *) fw->data;
  125. /* We only support ELF32 at this point */
  126. class = ehdr->e_ident[EI_CLASS];
  127. if( class != ELFCLASS32 ){
  128. printf( "Unsupported class: %d\n", class);
  129. return -EINVAL;
  130. };
  131. /* We assume the firmware has the same endianness as the host */
  132. # ifdef __LITTLE_ENDIAN
  133. if( ehdr->e_ident[EI_DATA] != ELFDATA2LSB ){
  134. # else /* BIG ENDIAN */
  135. if( ehdr->e_ident[EI_DATA] != ELFDATA2MSB ){
  136. # endif
  137. printf( "Unsupported firmware endianness\n" );
  138. return -EINVAL;
  139. };
  140. if( fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  141. printf( "Image is too small\n" );
  142. return -EINVAL;
  143. };
  144. if( memcmp(ehdr->e_ident, ELFMAG, SELFMAG)){
  145. printf( "Image is corrupted (bad magic)\n" );
  146. return -EINVAL;
  147. };
  148. if( ehdr->e_phnum == 0 ){
  149. printf( "No loadable segments\n" );
  150. return -EINVAL;
  151. };
  152. if( ehdr->e_phoff > fw->size ){
  153. printf( "Firmware size is too small\n" );
  154. return -EINVAL;
  155. }
  156. return 0;
  157. };
  158. /**
  159. * rproc_elf_get_boot_addr() - Get rproc's boot address.
  160. * @rproc: the remote processor handle
  161. * @fw: the ELF firmware image
  162. *
  163. * This function returns the entry point address of the ELF
  164. * image.
  165. *
  166. * Note that the boot address is not a configurable property of all remote
  167. * processors. Some will always boot at a specific hard-coded address.
  168. */
  169. static u32 elf_get_boot_addr( const struct firmware *fw ){
  170. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  171. return ehdr->e_entry;
  172. };
  173. /**
  174. * rproc_elf_load_segments() - load firmware segments to memory
  175. * @rproc: remote processor which will be booted using these fw segments
  176. * @fw: the ELF firmware image
  177. *
  178. * This function loads the firmware segments to memory, where the remote
  179. * processor expects them.
  180. *
  181. * Some remote processors will expect their code and data to be placed
  182. * in specific device addresses, and can't have them dynamically assigned.
  183. *
  184. * We currently support only those kind of remote processors, and expect
  185. * the program header's paddr member to contain those addresses. We then go
  186. * through the physically contiguous "carveout" memory regions which we
  187. * allocated (and mapped) earlier on behalf of the remote processor,
  188. * and "translate" device address to kernel addresses, so we can copy the
  189. * segments where they are expected.
  190. *
  191. * Currently we only support remote processors that required carveout
  192. * allocations and got them mapped onto their iommus. Some processors
  193. * might be different: they might not have iommus, and would prefer to
  194. * directly allocate memory for every segment/resource. This is not yet
  195. * supported, though.
  196. */
  197. static int elf_load_segments( const struct firmware *fw){
  198. struct elf32_hdr *ehdr;
  199. struct elf32_phdr *phdr;
  200. int i, ret = 0;
  201. const u8 *elf_data = fw->data;
  202. ehdr = (struct elf32_hdr *) elf_data;
  203. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  204. /* go through the available ELF segments */
  205. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  206. u32 da = phdr->p_paddr;
  207. u32 memsz = phdr->p_memsz;
  208. u32 filesz = phdr->p_filesz;
  209. u32 offset = phdr->p_offset;
  210. void *ptr;
  211. if (phdr->p_type != PT_LOAD)
  212. continue;
  213. printf( "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", phdr->p_type, da, memsz, filesz );
  214. if (filesz > memsz) {
  215. printf( "bad phdr filesz 0x%x memsz 0x%x\n", filesz, memsz );
  216. ret = -EINVAL;
  217. break;
  218. };
  219. if( offset + filesz > fw->size ){
  220. printf( "truncated fw: need 0x%x avail 0x%zx\n", offset + filesz, fw->size );
  221. ret = -EINVAL;
  222. break;
  223. };
  224. /* grab the kernel address for this device address */
  225. ptr = pru_da_to_va( rproc, da, memsz,
  226. RPROC_FLAGS_ELF_PHDR | phdr->p_flags ); /*rproc_da_to_va*/
  227. if ( !ptr ) {
  228. printf( "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  229. ret = -EINVAL;
  230. break;
  231. };
  232. /* put the segment where the remote processor expects it */
  233. if (phdr->p_filesz)
  234. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  235. /*
  236. * Zero out remaining memory for this segment.
  237. *
  238. * This isn't strictly required since dma_alloc_coherent already
  239. * did this for us. albeit harmless, we may consider removing
  240. * this.
  241. */
  242. if (memsz > filesz)
  243. memset(ptr + filesz, 0, memsz - filesz);
  244. }
  245. return ret;
  246. }
  247. static struct elf32_shdr *find_table( struct elf32_hdr *ehdr, size_t fw_size ){
  248. struct elf32_shdr *shdr;
  249. int i;
  250. const char *name_table;
  251. struct resource_table *table = NULL;
  252. const u8 *elf_data = (void *)ehdr;
  253. /* look for the resource table and handle it */
  254. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  255. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  256. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  257. u32 size = shdr->sh_size;
  258. u32 offset = shdr->sh_offset;
  259. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  260. continue;
  261. table = (struct resource_table *)(elf_data + offset);
  262. /* make sure we have the entire table */
  263. if (offset + size > fw_size || offset + size < size) {
  264. printf( "resource table truncated\n" );
  265. return NULL;
  266. };
  267. /* make sure table has at least the header */
  268. if (sizeof(struct resource_table) > size) {
  269. printf( "header-less resource table\n" );
  270. return NULL;
  271. };
  272. /* we don't support any version beyond the first */
  273. if (table->ver != 1) {
  274. printf( "unsupported fw ver: %d\n", table->ver );
  275. return NULL;
  276. };
  277. /* make sure reserved bytes are zeroes */
  278. if (table->reserved[0] || table->reserved[1]) {
  279. printf( "non zero reserved bytes\n" );
  280. return NULL;
  281. };
  282. /* make sure the offsets array isn't truncated */
  283. if (table->num * sizeof(table->offset[0]) +
  284. sizeof(struct resource_table) > size) {
  285. printf( "resource table incomplete\n" );
  286. return NULL;
  287. };
  288. return shdr;
  289. };
  290. return NULL;
  291. };
  292. /**
  293. * rproc_elf_find_rsc_table() - find the resource table
  294. * @rproc: the rproc handle
  295. * @fw: the ELF firmware image
  296. * @tablesz: place holder for providing back the table size
  297. *
  298. * This function finds the resource table inside the remote processor's
  299. * firmware. It is used both upon the registration of @rproc (in order
  300. * to look for and register the supported virito devices), and when the
  301. * @rproc is booted.
  302. *
  303. * Returns the pointer to the resource table if it is found, and write its
  304. * size into @tablesz. If a valid table isn't found, NULL is returned
  305. * (and @tablesz isn't set).
  306. */
  307. static struct resource_table *find_rsc_table( const struct firmware *fw, int *tablesz){
  308. struct elf32_hdr *ehdr;
  309. struct elf32_shdr *shdr;
  310. struct resource_table *table = NULL;
  311. const u8 *elf_data = fw->data;
  312. ehdr = (struct elf32_hdr *)elf_data;
  313. shdr = find_table( ehdr, fw->size);
  314. if (!shdr)
  315. return NULL;
  316. table = (struct resource_table *)(elf_data + shdr->sh_offset);
  317. *tablesz = shdr->sh_size;
  318. return table;
  319. };
  320. /**
  321. * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
  322. * @rproc: the rproc handle
  323. * @fw: the ELF firmware image
  324. *
  325. * This function finds the location of the loaded resource table. Don't
  326. * call this function if the table wasn't loaded yet - it's a bug if you do.
  327. *
  328. * Returns the pointer to the resource table if it is found or NULL otherwise.
  329. * If the table wasn't loaded yet the result is unspecified.
  330. */
  331. static struct resource_table *elf_find_loaded_rsc_table( const struct firmware *fw ){
  332. struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
  333. struct elf32_shdr *shdr;
  334. shdr = find_table( ehdr, fw->size );
  335. if (!shdr)
  336. return NULL;
  337. return rproc_da_to_va( rproc, shdr->sh_addr, shdr->sh_size,
  338. RPROC_FLAGS_ELF_SHDR | shdr->sh_flags );
  339. }
  340. /**
  341. * rproc_elf_find_version_section() - find the .version section
  342. * @rproc: the rproc handle
  343. * @fw: the ELF firmware image
  344. * @len: firmware size (in bytes)
  345. * @versz: place holder for providing back the version size
  346. *
  347. * This function finds the .version section inside the remote processor's
  348. * firmware. It is used to provide any version information for the
  349. * firmware.
  350. *
  351. * Returns the pointer to the .version section if it is found, and write its
  352. * size into @versz. If a valid version isn't found, NULL is returned
  353. * (and @versz isn't set).
  354. */
  355. static const char *elf_find_version_section( const struct firmware *fw, int *versz ){
  356. struct elf32_hdr *ehdr;
  357. struct elf32_shdr *shdr;
  358. const char *name_table;
  359. const char *vdata = NULL;
  360. int i;
  361. const u8 *elf_data = fw->data;
  362. ehdr = (struct elf32_hdr *)elf_data;
  363. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  364. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  365. /* look for the version section */
  366. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  367. int size = shdr->sh_size;
  368. int offset = shdr->sh_offset;
  369. if (strcmp(name_table + shdr->sh_name, ".version"))
  370. continue;
  371. vdata = (char *)(elf_data + offset);
  372. /* make sure we have the entire section */
  373. if (offset + size > fw->size) {
  374. printf( "version info truncated\n" );
  375. return NULL;
  376. };
  377. *versz = shdr->sh_size;
  378. break;
  379. };
  380. return vdata;
  381. };
  382. const struct rproc_fw_ops rproc_elf_fw_ops = {
  383. .load = rproc_elf_load_segments,
  384. .find_rsc_table = rproc_elf_find_rsc_table,
  385. .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
  386. .sanity_check = rproc_elf_sanity_check,
  387. .get_boot_addr = rproc_elf_get_boot_addr,
  388. .find_version = rproc_elf_find_version_section
  389. };