1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (C) 2023, STMicroelectronics 4 */ 5 6 #ifndef ELF_PARSER_H 7 #define ELF_PARSER_H 8 9 #include <elf32.h> 10 #include <stdint.h> 11 #include <tee_api_types.h> 12 13 /** 14 * struct resource_table - firmware resource table header 15 * @ver: version number 16 * @num: number of resource entries 17 * @reserved: reserved (must be zero) 18 * @offset: array of offsets pointing at the various resource entries 19 * 20 * A resource table is essentially a list of system resources required 21 * by the remote processor. It may also include configuration entries. 22 * If needed, the remote processor firmware should contain this table 23 * as a dedicated ".resource_table" ELF section. 24 * 25 * This structure shall be consistent with the Linux kernel structure 26 * definition from include/linux/remoteproc.h. 27 * 28 * The resource_table structure does not need to be packed in OP-TEE. The ELF 29 * parser does not access the system resources structures that follow this 30 * structure in shared memory (no memory alignment constraint on structures). 31 */ 32 struct resource_table { 33 uint32_t ver; 34 uint32_t num; 35 uint32_t reserved[2]; 36 uint32_t offset[]; 37 }; 38 39 struct fw_elf32 { 40 uintptr_t e_entry; 41 uintptr_t e_phoff; 42 uintptr_t e_shoff; 43 uint32_t e_phnum; 44 uint32_t e_shnum; 45 uint32_t e_phentsize; 46 uint32_t e_shentsize; 47 48 Elf32_Phdr *phdr; 49 Elf32_Shdr *shdr; 50 }; 51 52 /* 53 * e32_parse_ehdr() - Check and parse the ELF header 54 * 55 * @fw: Firmware ELF file image 56 * @size: Byte size of firmware ELF file image 57 * Return TEE_SUCCESS or appropriate error. 58 */ 59 TEE_Result e32_parse_ehdr(uint8_t *fw, size_t size); 60 61 /* 62 * e32_parser_load_elf_image() - simple ELF loader 63 * @fw: Firmware ELF file image 64 * @fw_size: Firmware ELF file image byte size 65 * @load_seg: Callback for loading a firmware image segment into device memory 66 * @priv_data: Private data passed to @load_seg callback. 67 * Return TEE_SUCCESS or appropriate error. 68 */ 69 TEE_Result e32_parser_load_elf_image(uint8_t *fw, size_t fw_size, 70 TEE_Result (*load_seg)(uint8_t *src, 71 uint32_t size, 72 uint32_t da, 73 uint32_t mem_size, 74 void *priv), 75 void *priv_data); 76 77 /* 78 * e32_parser_find_rsc_table() - find resource table in an ELF image 79 * @fw: Firmware ELF file image 80 * @fw_size: Firmware ELF file image byte size 81 * @rsc_addr: Output resource table address from the remote proc perspective 82 * @rsc_size: Output resource table size 83 * Return TEE_SUCCESS if found, 84 * TEE_ERROR_NO_DATA if not found, 85 * or appropriate error. 86 */ 87 TEE_Result e32_parser_find_rsc_table(uint8_t *fw, size_t fw_size, 88 Elf32_Addr *rsc_addr, 89 Elf32_Word *rsc_size); 90 91 #endif /*ELF_PARSER_H*/ 92