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.

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