mdt_loader.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Qualcomm Peripheral Image Loader
  3. *
  4. * Copyright (C) 2016 Linaro Ltd
  5. * Copyright (C) 2015 Sony Mobile Communications Inc
  6. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/elf.h>
  19. #include <linux/firmware.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/qcom_scm.h>
  23. #include <linux/sizes.h>
  24. #include <linux/slab.h>
  25. #include <linux/soc/qcom/mdt_loader.h>
  26. static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
  27. {
  28. if (phdr->p_type != PT_LOAD)
  29. return false;
  30. if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
  31. return false;
  32. if (!phdr->p_memsz)
  33. return false;
  34. return true;
  35. }
  36. /**
  37. * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt
  38. * @fw: firmware object for the mdt file
  39. *
  40. * Returns size of the loaded firmware blob, or -EINVAL on failure.
  41. */
  42. ssize_t qcom_mdt_get_size(const struct firmware *fw)
  43. {
  44. const struct elf32_phdr *phdrs;
  45. const struct elf32_phdr *phdr;
  46. const struct elf32_hdr *ehdr;
  47. phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
  48. phys_addr_t max_addr = 0;
  49. int i;
  50. ehdr = (struct elf32_hdr *)fw->data;
  51. phdrs = (struct elf32_phdr *)(ehdr + 1);
  52. for (i = 0; i < ehdr->e_phnum; i++) {
  53. phdr = &phdrs[i];
  54. if (!mdt_phdr_valid(phdr))
  55. continue;
  56. if (phdr->p_paddr < min_addr)
  57. min_addr = phdr->p_paddr;
  58. if (phdr->p_paddr + phdr->p_memsz > max_addr)
  59. max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
  60. }
  61. return min_addr < max_addr ? max_addr - min_addr : -EINVAL;
  62. }
  63. EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
  64. /**
  65. * qcom_mdt_load() - load the firmware which header is loaded as fw
  66. * @dev: device handle to associate resources with
  67. * @fw: firmware object for the mdt file
  68. * @firmware: name of the firmware, for construction of segment file names
  69. * @pas_id: PAS identifier
  70. * @mem_region: allocated memory region to load firmware into
  71. * @mem_phys: physical address of allocated memory region
  72. * @mem_size: size of the allocated memory region
  73. * @reloc_base: adjusted physical address after relocation
  74. *
  75. * Returns 0 on success, negative errno otherwise.
  76. */
  77. int qcom_mdt_load(struct device *dev, const struct firmware *fw,
  78. const char *firmware, int pas_id, void *mem_region,
  79. phys_addr_t mem_phys, size_t mem_size,
  80. phys_addr_t *reloc_base)
  81. {
  82. const struct elf32_phdr *phdrs;
  83. const struct elf32_phdr *phdr;
  84. const struct elf32_hdr *ehdr;
  85. const struct firmware *seg_fw;
  86. phys_addr_t mem_reloc;
  87. phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
  88. phys_addr_t max_addr = 0;
  89. size_t fw_name_len;
  90. ssize_t offset;
  91. char *fw_name;
  92. bool relocate = false;
  93. void *ptr;
  94. int ret;
  95. int i;
  96. if (!fw || !mem_region || !mem_phys || !mem_size)
  97. return -EINVAL;
  98. ehdr = (struct elf32_hdr *)fw->data;
  99. phdrs = (struct elf32_phdr *)(ehdr + 1);
  100. fw_name_len = strlen(firmware);
  101. if (fw_name_len <= 4)
  102. return -EINVAL;
  103. fw_name = kstrdup(firmware, GFP_KERNEL);
  104. if (!fw_name)
  105. return -ENOMEM;
  106. ret = qcom_scm_pas_init_image(pas_id, fw->data, fw->size);
  107. if (ret) {
  108. dev_err(dev, "invalid firmware metadata\n");
  109. goto out;
  110. }
  111. for (i = 0; i < ehdr->e_phnum; i++) {
  112. phdr = &phdrs[i];
  113. if (!mdt_phdr_valid(phdr))
  114. continue;
  115. if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
  116. relocate = true;
  117. if (phdr->p_paddr < min_addr)
  118. min_addr = phdr->p_paddr;
  119. if (phdr->p_paddr + phdr->p_memsz > max_addr)
  120. max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
  121. }
  122. if (relocate) {
  123. ret = qcom_scm_pas_mem_setup(pas_id, mem_phys, max_addr - min_addr);
  124. if (ret) {
  125. dev_err(dev, "unable to setup relocation\n");
  126. goto out;
  127. }
  128. /*
  129. * The image is relocatable, so offset each segment based on
  130. * the lowest segment address.
  131. */
  132. mem_reloc = min_addr;
  133. } else {
  134. /*
  135. * Image is not relocatable, so offset each segment based on
  136. * the allocated physical chunk of memory.
  137. */
  138. mem_reloc = mem_phys;
  139. }
  140. for (i = 0; i < ehdr->e_phnum; i++) {
  141. phdr = &phdrs[i];
  142. if (!mdt_phdr_valid(phdr))
  143. continue;
  144. offset = phdr->p_paddr - mem_reloc;
  145. if (offset < 0 || offset + phdr->p_memsz > mem_size) {
  146. dev_err(dev, "segment outside memory range\n");
  147. ret = -EINVAL;
  148. break;
  149. }
  150. ptr = mem_region + offset;
  151. if (phdr->p_filesz) {
  152. sprintf(fw_name + fw_name_len - 3, "b%02d", i);
  153. ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
  154. ptr, phdr->p_filesz);
  155. if (ret) {
  156. dev_err(dev, "failed to load %s\n", fw_name);
  157. break;
  158. }
  159. release_firmware(seg_fw);
  160. }
  161. if (phdr->p_memsz > phdr->p_filesz)
  162. memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
  163. }
  164. if (reloc_base)
  165. *reloc_base = mem_reloc;
  166. out:
  167. kfree(fw_name);
  168. return ret;
  169. }
  170. EXPORT_SYMBOL_GPL(qcom_mdt_load);
  171. MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
  172. MODULE_LICENSE("GPL v2");