wcd-dsp-utils.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/firmware.h>
  8. #include <linux/elf.h>
  9. #include <linux/slab.h>
  10. #include <linux/list.h>
  11. #include "wcd-dsp-utils.h"
  12. static bool wdsp_is_valid_elf_hdr(const struct elf32_hdr *ehdr,
  13. size_t fw_size)
  14. {
  15. if (fw_size < sizeof(*ehdr)) {
  16. pr_err("%s: Firmware too small\n", __func__);
  17. goto elf_check_fail;
  18. }
  19. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
  20. pr_err("%s: Not an ELF file\n", __func__);
  21. goto elf_check_fail;
  22. }
  23. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  24. pr_err("%s: Not an executable image\n", __func__);
  25. goto elf_check_fail;
  26. }
  27. if (ehdr->e_phnum == 0) {
  28. pr_err("%s: no segments to load\n", __func__);
  29. goto elf_check_fail;
  30. }
  31. if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
  32. sizeof(struct elf32_hdr) > fw_size) {
  33. pr_err("%s: Too small MDT file\n", __func__);
  34. goto elf_check_fail;
  35. }
  36. return true;
  37. elf_check_fail:
  38. return false;
  39. }
  40. static int wdsp_add_segment_to_list(struct device *dev,
  41. const char *img_fname,
  42. const struct elf32_phdr *phdr,
  43. int phdr_idx,
  44. struct list_head *seg_list)
  45. {
  46. struct wdsp_img_segment *seg;
  47. int ret = 0;
  48. /* Do not load segments with zero size */
  49. if (phdr->p_filesz == 0 || phdr->p_memsz == 0)
  50. goto done;
  51. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  52. if (!seg) {
  53. ret = -ENOMEM;
  54. goto done;
  55. }
  56. snprintf(seg->split_fname, sizeof(seg->split_fname),
  57. "%s.b%02d", img_fname, phdr_idx);
  58. ret = request_firmware(&seg->split_fw, seg->split_fname, dev);
  59. if (ret < 0) {
  60. dev_err(dev, "%s: firmware %s not found\n",
  61. __func__, seg->split_fname);
  62. goto bad_seg;
  63. }
  64. if (phdr->p_filesz != seg->split_fw->size) {
  65. dev_err(dev,
  66. "%s: %s size mismatch, phdr_size: 0x%x fw_size: 0x%zx",
  67. __func__, seg->split_fname, phdr->p_filesz,
  68. seg->split_fw->size);
  69. ret = -EINVAL;
  70. goto bad_elf;
  71. }
  72. seg->load_addr = phdr->p_paddr;
  73. seg->size = phdr->p_filesz;
  74. seg->data = (u8 *) seg->split_fw->data;
  75. list_add_tail(&seg->list, seg_list);
  76. done:
  77. return ret;
  78. bad_elf:
  79. release_firmware(seg->split_fw);
  80. bad_seg:
  81. kfree(seg);
  82. return ret;
  83. }
  84. /*
  85. * wdsp_flush_segment_list: Flush the list of segments
  86. * @seg_list: List of segments to be flushed
  87. * This API will traverse through the list of segments provided in
  88. * seg_list, release the firmware for each segment and delete the
  89. * segment from the list.
  90. */
  91. void wdsp_flush_segment_list(struct list_head *seg_list)
  92. {
  93. struct wdsp_img_segment *seg, *next;
  94. list_for_each_entry_safe(seg, next, seg_list, list) {
  95. release_firmware(seg->split_fw);
  96. list_del(&seg->list);
  97. kfree(seg);
  98. }
  99. }
  100. EXPORT_SYMBOL(wdsp_flush_segment_list);
  101. /*
  102. * wdsp_get_segment_list: Get the list of requested segments
  103. * @dev: struct device pointer of caller
  104. * @img_fname: Image name for the mdt and split firmware files
  105. * @segment_type: Requested segment type, should be either
  106. * WDSP_ELF_FLAG_RE or WDSP_ELF_FLAG_WRITE
  107. * @seg_list: An initialized head for list of segmented to be returned
  108. * @entry_point: Pointer to return the entry point of the image
  109. * This API will parse the mdt file for img_fname and create
  110. * an struct wdsp_img_segment for each segment that matches segment_type
  111. * and add this structure to list pointed by seg_list
  112. */
  113. int wdsp_get_segment_list(struct device *dev,
  114. const char *img_fname,
  115. unsigned int segment_type,
  116. struct list_head *seg_list,
  117. u32 *entry_point)
  118. {
  119. const struct firmware *fw;
  120. const struct elf32_hdr *ehdr;
  121. const struct elf32_phdr *phdr;
  122. const u8 *elf_ptr;
  123. char mdt_name[WDSP_IMG_NAME_LEN_MAX];
  124. int ret, phdr_idx;
  125. bool segment_match;
  126. if (!dev) {
  127. ret = -EINVAL;
  128. pr_err("%s: Invalid device handle\n", __func__);
  129. goto done;
  130. }
  131. if (!img_fname || !seg_list || !entry_point) {
  132. ret = -EINVAL;
  133. dev_err(dev, "%s: Invalid input params\n",
  134. __func__);
  135. goto done;
  136. }
  137. if (segment_type != WDSP_ELF_FLAG_RE &&
  138. segment_type != WDSP_ELF_FLAG_WRITE) {
  139. dev_err(dev, "%s: Invalid request for segment_type %d\n",
  140. __func__, segment_type);
  141. ret = -EINVAL;
  142. goto done;
  143. }
  144. snprintf(mdt_name, sizeof(mdt_name), "%s.mdt", img_fname);
  145. ret = request_firmware(&fw, mdt_name, dev);
  146. if (ret < 0) {
  147. dev_err(dev, "%s: firmware %s not found\n",
  148. __func__, mdt_name);
  149. goto done;
  150. }
  151. ehdr = (struct elf32_hdr *) fw->data;
  152. *entry_point = ehdr->e_entry;
  153. if (!wdsp_is_valid_elf_hdr(ehdr, fw->size)) {
  154. dev_err(dev, "%s: fw mdt %s is invalid\n",
  155. __func__, mdt_name);
  156. ret = -EINVAL;
  157. goto bad_elf;
  158. }
  159. elf_ptr = fw->data + sizeof(*ehdr);
  160. for (phdr_idx = 0; phdr_idx < ehdr->e_phnum; phdr_idx++) {
  161. phdr = (struct elf32_phdr *) elf_ptr;
  162. segment_match = false;
  163. switch (segment_type) {
  164. case WDSP_ELF_FLAG_RE:
  165. /*
  166. * Flag can be READ or EXECUTE or both but
  167. * WRITE flag should not be set.
  168. */
  169. if ((phdr->p_flags & segment_type) &&
  170. !(phdr->p_flags & WDSP_ELF_FLAG_WRITE))
  171. segment_match = true;
  172. break;
  173. case WDSP_ELF_FLAG_WRITE:
  174. /*
  175. * If WRITE flag is set, other flags do not
  176. * matter.
  177. */
  178. if (phdr->p_flags & segment_type)
  179. segment_match = true;
  180. break;
  181. }
  182. if (segment_match) {
  183. ret = wdsp_add_segment_to_list(dev, img_fname, phdr,
  184. phdr_idx, seg_list);
  185. if (ret < 0) {
  186. wdsp_flush_segment_list(seg_list);
  187. goto bad_elf;
  188. }
  189. }
  190. elf_ptr = elf_ptr + sizeof(*phdr);
  191. }
  192. bad_elf:
  193. release_firmware(fw);
  194. done:
  195. return ret;
  196. }
  197. EXPORT_SYMBOL(wdsp_get_segment_list);