#include "elf_loader.h" /* * Convert PRU device address (data spaces only) to kernel virtual address * * Each PRU has access to all data memories within the PRUSS, accessible at * different ranges. So, look through both its primary and secondary Data * RAMs as well as any shared Data RAM to convert a PRU device address to * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data * RAM1 is primary Data RAM for PRU1. * static void *pru_d_da_to_va( struct pru_rproc *pru, uint32_t da, int len){ struct pruss_mem_region dram0, dram1, shrd_ram; struct pruss *pruss = pru->pruss; u32 offset; void *va = NULL; if( len <= 0 ) return NULL; dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0]; dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1]; // PRU1 has its local RAM addresses reversed if (pru->id == 1) swap(dram0, dram1); shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2]; if (da >= pru->pdram_da && da + len <= pru->pdram_da + dram0.size) { offset = da - pru->pdram_da; va = (__force void *)(dram0.va + offset); } else if (da >= pru->sdram_da && da + len <= pru->sdram_da + dram1.size) { offset = da - pru->sdram_da; va = (__force void *)(dram1.va + offset); } else if (da >= pru->shrdram_da && da + len <= pru->shrdram_da + shrd_ram.size) { offset = da - pru->shrdram_da; va = (__force void *)(shrd_ram.va + offset); }; return va; }; /* * Convert PRU device address (instruction space) to kernel virtual address * * A PRU does not have an unified address space. Each PRU has its very own * private Instruction RAM, and its device address is identical to that of * its primary Data RAM device address. * static void *pru_i_da_to_va( struct pru_rproc *pru, uint32_t da, int len ){ u32 offset; void *va = NULL; if (len <= 0) return NULL; if (da >= pru->iram_da && da + len <= pru->iram_da + pru->mem_regions[PRU_MEM_IRAM].size) { offset = da - pru->iram_da; va = (__force void *)(pru->mem_regions[PRU_MEM_IRAM].va + offset); }; return va; }; /* PRU-specific address translator * static void *pru_da_to_va( struct rproc *rproc, uint64_t da, int len, uint32_t flags ){ struct pru_rproc *pru = rproc->priv; void *va; uint32_t exec_flag = 0; exec_flag = ((flags & RPROC_FLAGS_ELF_SHDR ) ? flags & SHF_EXECINSTR : ((flags & RPROC_FLAGS_ELF_PHDR ) ? flags & PF_X : 0)); if( exec_flag ) va = pru_i_da_to_va(pru, da, len); else va = pru_d_da_to_va(pru, da, len); return va; }; */ /* static int load_elf_fw( const char *name, const struct firmware *fw ){ FILE *FD = NULL; FD = fopen( name, "rb" ); if( FD != NULL ){ printf( "Firmware is opened\n" ); // Read file size fseek( FD, 0, SEEK_END); fw->size = ftell( FD ); if( fw->size == 0 ){ printf("File read failed.. Aborting\n"); fclose( FD ); return -1; }; fseek( FD, 0, SEEK_SET ); fw->data = ( uint8_t * ) malloc( fw->size ); if( fw->data != NULL ){ memset( fw->data, 0, fw->size ); if( fw->size != fread((unsigned char *) fw->data, 1, fw->size, FD )) { printf("WARNING: File Size mismatch\n"); } fclose( FD ); printf( "Firmware is loadded\n" ); printf( "Firmware size is %d bytes\n", fw->size ); return 0; }; } else { printf( "Firmware open failure\n" ); }; return -1; }; /** * rproc_elf_sanity_check() - Sanity Check ELF firmware image * @rproc: the remote processor handle * @fw: the ELF firmware image * * Make sure this fw image is sane. * static int elf_sanity_check( struct rproc *rproc, const struct firmware *fw ){ //const char *name = rproc->firmware; //struct device *dev = &rproc->dev; struct elf32_hdr *ehdr; char class; /* if( !fw ){ printf( "failed to load %s\n", name ); return -EINVAL; }; * if ( fw->size < sizeof(struct elf32_hdr ) ){ printf("Image is too small\n"); return -EINVAL; }; ehdr = (struct elf32_hdr *) fw->data; // We only support ELF32 at this point class = ehdr->e_ident[EI_CLASS]; if( class != ELFCLASS32 ){ printf( "Unsupported class: %d\n", class); return -EINVAL; }; //* We assume the firmware has the same endianness as the host # ifdef __LITTLE_ENDIAN if( ehdr->e_ident[EI_DATA] != ELFDATA2LSB ){ # else // BIG ENDIAN if( ehdr->e_ident[EI_DATA] != ELFDATA2MSB ){ # endif printf( "Unsupported firmware endianness\n" ); return -EINVAL; }; if( fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) { printf( "Image is too small\n" ); return -EINVAL; }; if( memcmp(ehdr->e_ident, ELFMAG, SELFMAG)){ printf( "Image is corrupted (bad magic)\n" ); return -EINVAL; }; if( ehdr->e_phnum == 0 ){ printf( "No loadable segments\n" ); return -EINVAL; }; if( ehdr->e_phoff > fw->size ){ printf( "Firmware size is too small\n" ); return -EINVAL; } return 0; }; /** * rproc_elf_get_boot_addr() - Get rproc's boot address. * @rproc: the remote processor handle * @fw: the ELF firmware image * * This function returns the entry point address of the ELF * image. * * Note that the boot address is not a configurable property of all remote * processors. Some will always boot at a specific hard-coded address. * static u32 elf_get_boot_addr( const struct firmware *fw ){ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data; return ehdr->e_entry; }; /** * rproc_elf_load_segments() - load firmware segments to memory * @rproc: remote processor which will be booted using these fw segments * @fw: the ELF firmware image * * This function loads the firmware segments to memory, where the remote * processor expects them. * * Some remote processors will expect their code and data to be placed * in specific device addresses, and can't have them dynamically assigned. * * We currently support only those kind of remote processors, and expect * the program header's paddr member to contain those addresses. We then go * through the physically contiguous "carveout" memory regions which we * allocated (and mapped) earlier on behalf of the remote processor, * and "translate" device address to kernel addresses, so we can copy the * segments where they are expected. * * Currently we only support remote processors that required carveout * allocations and got them mapped onto their iommus. Some processors * might be different: they might not have iommus, and would prefer to * directly allocate memory for every segment/resource. This is not yet * supported, though. * static int elf_load_segments( const struct firmware *fw){ struct elf32_hdr *ehdr; struct elf32_phdr *phdr; int i, ret = 0; const u8 *elf_data = fw->data; ehdr = (struct elf32_hdr *) elf_data; phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); //* go through the available ELF segments for (i = 0; i < ehdr->e_phnum; i++, phdr++) { u32 da = phdr->p_paddr; u32 memsz = phdr->p_memsz; u32 filesz = phdr->p_filesz; u32 offset = phdr->p_offset; void *ptr; if (phdr->p_type != PT_LOAD) continue; printf( "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", phdr->p_type, da, memsz, filesz ); if (filesz > memsz) { printf( "bad phdr filesz 0x%x memsz 0x%x\n", filesz, memsz ); ret = -EINVAL; break; }; if( offset + filesz > fw->size ){ printf( "truncated fw: need 0x%x avail 0x%zx\n", offset + filesz, fw->size ); ret = -EINVAL; break; }; //* grab the kernel address for this device address ptr = pru_da_to_va( rproc, da, memsz, RPROC_FLAGS_ELF_PHDR | phdr->p_flags ); //rproc_da_to_va if ( !ptr ) { printf( "bad phdr da 0x%x mem 0x%x\n", da, memsz); ret = -EINVAL; break; }; //* put the segment where the remote processor expects it if (phdr->p_filesz) memcpy(ptr, elf_data + phdr->p_offset, filesz); /* * Zero out remaining memory for this segment. * * This isn't strictly required since dma_alloc_coherent already * did this for us. albeit harmless, we may consider removing * this. * if (memsz > filesz) memset(ptr + filesz, 0, memsz - filesz); } return ret; } static struct elf32_shdr *find_table( struct elf32_hdr *ehdr, size_t fw_size ){ struct elf32_shdr *shdr; int i; const char *name_table; struct resource_table *table = NULL; const u8 *elf_data = (void *)ehdr; //* look for the resource table and handle it shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset; for (i = 0; i < ehdr->e_shnum; i++, shdr++) { u32 size = shdr->sh_size; u32 offset = shdr->sh_offset; if (strcmp(name_table + shdr->sh_name, ".resource_table")) continue; table = (struct resource_table *)(elf_data + offset); //* make sure we have the entire table if (offset + size > fw_size || offset + size < size) { printf( "resource table truncated\n" ); return NULL; }; //* make sure table has at least the header if (sizeof(struct resource_table) > size) { printf( "header-less resource table\n" ); return NULL; }; //* we don't support any version beyond the first if (table->ver != 1) { printf( "unsupported fw ver: %d\n", table->ver ); return NULL; }; //* make sure reserved bytes are zeroes if (table->reserved[0] || table->reserved[1]) { printf( "non zero reserved bytes\n" ); return NULL; }; //* make sure the offsets array isn't truncated if (table->num * sizeof(table->offset[0]) + sizeof(struct resource_table) > size) { printf( "resource table incomplete\n" ); return NULL; }; return shdr; }; return NULL; }; /** * rproc_elf_find_rsc_table() - find the resource table * @rproc: the rproc handle * @fw: the ELF firmware image * @tablesz: place holder for providing back the table size * * This function finds the resource table inside the remote processor's * firmware. It is used both upon the registration of @rproc (in order * to look for and register the supported virito devices), and when the * @rproc is booted. * * Returns the pointer to the resource table if it is found, and write its * size into @tablesz. If a valid table isn't found, NULL is returned * (and @tablesz isn't set). * static struct resource_table *find_rsc_table( const struct firmware *fw, int *tablesz){ struct elf32_hdr *ehdr; struct elf32_shdr *shdr; struct resource_table *table = NULL; const u8 *elf_data = fw->data; ehdr = (struct elf32_hdr *)elf_data; shdr = find_table( ehdr, fw->size); if (!shdr) return NULL; table = (struct resource_table *)(elf_data + shdr->sh_offset); *tablesz = shdr->sh_size; return table; }; /** * rproc_elf_find_loaded_rsc_table() - find the loaded resource table * @rproc: the rproc handle * @fw: the ELF firmware image * * This function finds the location of the loaded resource table. Don't * call this function if the table wasn't loaded yet - it's a bug if you do. * * Returns the pointer to the resource table if it is found or NULL otherwise. * If the table wasn't loaded yet the result is unspecified. * static struct resource_table *elf_find_loaded_rsc_table( const struct firmware *fw ){ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data; struct elf32_shdr *shdr; shdr = find_table( ehdr, fw->size ); if (!shdr) return NULL; return rproc_da_to_va( rproc, shdr->sh_addr, shdr->sh_size, RPROC_FLAGS_ELF_SHDR | shdr->sh_flags ); } /** * rproc_elf_find_version_section() - find the .version section * @rproc: the rproc handle * @fw: the ELF firmware image * @len: firmware size (in bytes) * @versz: place holder for providing back the version size * * This function finds the .version section inside the remote processor's * firmware. It is used to provide any version information for the * firmware. * * Returns the pointer to the .version section if it is found, and write its * size into @versz. If a valid version isn't found, NULL is returned * (and @versz isn't set). * static const char *elf_find_version_section( const struct firmware *fw, int *versz ){ struct elf32_hdr *ehdr; struct elf32_shdr *shdr; const char *name_table; const char *vdata = NULL; int i; const u8 *elf_data = fw->data; ehdr = (struct elf32_hdr *)elf_data; shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset; //* look for the version section for (i = 0; i < ehdr->e_shnum; i++, shdr++) { int size = shdr->sh_size; int offset = shdr->sh_offset; if (strcmp(name_table + shdr->sh_name, ".version")) continue; vdata = (char *)(elf_data + offset); //* make sure we have the entire section if (offset + size > fw->size) { printf( "version info truncated\n" ); return NULL; }; *versz = shdr->sh_size; break; }; return vdata; }; /* const struct rproc_fw_ops rproc_elf_fw_ops = { .load = rproc_elf_load_segments, .find_rsc_table = rproc_elf_find_rsc_table, .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, .sanity_check = rproc_elf_sanity_check, .get_boot_addr = rproc_elf_get_boot_addr, .find_version = rproc_elf_find_version_section }; */