remoteproc_elf_helpers.h 3.4 KB

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