cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

remoteproc_elf_helpers.h (3486B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2/*
      3 * Remote processor elf helpers defines
      4 *
      5 * Copyright (C) 2020 Kalray, Inc.
      6 */
      7
      8#ifndef REMOTEPROC_ELF_LOADER_H
      9#define REMOTEPROC_ELF_LOADER_H
     10
     11#include <linux/elf.h>
     12#include <linux/types.h>
     13
     14/**
     15 * fw_elf_get_class - Get elf class
     16 * @fw: the ELF firmware image
     17 *
     18 * Note that we use elf32_hdr to access the class since the start of the
     19 * struct is the same for both elf class
     20 *
     21 * Return: elf class of the firmware
     22 */
     23static inline u8 fw_elf_get_class(const struct firmware *fw)
     24{
     25	struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
     26
     27	return ehdr->e_ident[EI_CLASS];
     28}
     29
     30static inline void elf_hdr_init_ident(struct elf32_hdr *hdr, u8 class)
     31{
     32	memcpy(hdr->e_ident, ELFMAG, SELFMAG);
     33	hdr->e_ident[EI_CLASS] = class;
     34	hdr->e_ident[EI_DATA] = ELFDATA2LSB;
     35	hdr->e_ident[EI_VERSION] = EV_CURRENT;
     36	hdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
     37}
     38
     39/* Generate getter and setter for a specific elf struct/field */
     40#define ELF_GEN_FIELD_GET_SET(__s, __field, __type) \
     41static inline __type elf_##__s##_get_##__field(u8 class, const void *arg) \
     42{ \
     43	if (class == ELFCLASS32) \
     44		return (__type) ((const struct elf32_##__s *) arg)->__field; \
     45	else \
     46		return (__type) ((const struct elf64_##__s *) arg)->__field; \
     47} \
     48static inline void elf_##__s##_set_##__field(u8 class, void *arg, \
     49					     __type value) \
     50{ \
     51	if (class == ELFCLASS32) \
     52		((struct elf32_##__s *) arg)->__field = (__type) value; \
     53	else \
     54		((struct elf64_##__s *) arg)->__field = (__type) value; \
     55}
     56
     57ELF_GEN_FIELD_GET_SET(hdr, e_entry, u64)
     58ELF_GEN_FIELD_GET_SET(hdr, e_phnum, u16)
     59ELF_GEN_FIELD_GET_SET(hdr, e_shnum, u16)
     60ELF_GEN_FIELD_GET_SET(hdr, e_phoff, u64)
     61ELF_GEN_FIELD_GET_SET(hdr, e_shoff, u64)
     62ELF_GEN_FIELD_GET_SET(hdr, e_shstrndx, u16)
     63ELF_GEN_FIELD_GET_SET(hdr, e_machine, u16)
     64ELF_GEN_FIELD_GET_SET(hdr, e_type, u16)
     65ELF_GEN_FIELD_GET_SET(hdr, e_version, u32)
     66ELF_GEN_FIELD_GET_SET(hdr, e_ehsize, u32)
     67ELF_GEN_FIELD_GET_SET(hdr, e_phentsize, u16)
     68ELF_GEN_FIELD_GET_SET(hdr, e_shentsize, u16)
     69
     70ELF_GEN_FIELD_GET_SET(phdr, p_paddr, u64)
     71ELF_GEN_FIELD_GET_SET(phdr, p_vaddr, u64)
     72ELF_GEN_FIELD_GET_SET(phdr, p_filesz, u64)
     73ELF_GEN_FIELD_GET_SET(phdr, p_memsz, u64)
     74ELF_GEN_FIELD_GET_SET(phdr, p_type, u32)
     75ELF_GEN_FIELD_GET_SET(phdr, p_offset, u64)
     76ELF_GEN_FIELD_GET_SET(phdr, p_flags, u32)
     77ELF_GEN_FIELD_GET_SET(phdr, p_align, u64)
     78
     79ELF_GEN_FIELD_GET_SET(shdr, sh_type, u32)
     80ELF_GEN_FIELD_GET_SET(shdr, sh_flags, u32)
     81ELF_GEN_FIELD_GET_SET(shdr, sh_entsize, u16)
     82ELF_GEN_FIELD_GET_SET(shdr, sh_size, u64)
     83ELF_GEN_FIELD_GET_SET(shdr, sh_offset, u64)
     84ELF_GEN_FIELD_GET_SET(shdr, sh_name, u32)
     85ELF_GEN_FIELD_GET_SET(shdr, sh_addr, u64)
     86
     87#define ELF_STRUCT_SIZE(__s) \
     88static inline unsigned long elf_size_of_##__s(u8 class) \
     89{ \
     90	if (class == ELFCLASS32)\
     91		return sizeof(struct elf32_##__s); \
     92	else \
     93		return sizeof(struct elf64_##__s); \
     94}
     95
     96ELF_STRUCT_SIZE(shdr)
     97ELF_STRUCT_SIZE(phdr)
     98ELF_STRUCT_SIZE(hdr)
     99
    100static inline unsigned int elf_strtbl_add(const char *name, void *ehdr, u8 class, size_t *index)
    101{
    102	u16 shstrndx = elf_hdr_get_e_shstrndx(class, ehdr);
    103	void *shdr;
    104	char *strtab;
    105	size_t idx, ret;
    106
    107	shdr = ehdr + elf_size_of_hdr(class) + shstrndx * elf_size_of_shdr(class);
    108	strtab = ehdr + elf_shdr_get_sh_offset(class, shdr);
    109	idx = index ? *index : 0;
    110	if (!strtab || !name)
    111		return 0;
    112
    113	ret = idx;
    114	strcpy((strtab + idx), name);
    115	idx += strlen(name) + 1;
    116	if (index)
    117		*index = idx;
    118
    119	return ret;
    120}
    121
    122#endif /* REMOTEPROC_ELF_LOADER_H */