qcom_mdt_loader.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/elf.h>
  18. #include <linux/firmware.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/remoteproc.h>
  22. #include <linux/sizes.h>
  23. #include <linux/slab.h>
  24. #include "remoteproc_internal.h"
  25. #include "qcom_mdt_loader.h"
  26. /**
  27. * qcom_mdt_find_rsc_table() - provide dummy resource table for remoteproc
  28. * @rproc: remoteproc handle
  29. * @fw: firmware header
  30. * @tablesz: outgoing size of the table
  31. *
  32. * Returns a dummy table.
  33. */
  34. struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
  35. const struct firmware *fw,
  36. int *tablesz)
  37. {
  38. static struct resource_table table = { .ver = 1, };
  39. *tablesz = sizeof(table);
  40. return &table;
  41. }
  42. EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table);
  43. /**
  44. * qcom_mdt_parse() - extract useful parameters from the mdt header
  45. * @fw: firmware handle
  46. * @fw_addr: optional reference for base of the firmware's memory region
  47. * @fw_size: optional reference for size of the firmware's memory region
  48. * @fw_relocate: optional reference for flagging if the firmware is relocatable
  49. *
  50. * Returns 0 on success, negative errno otherwise.
  51. */
  52. int qcom_mdt_parse(const struct firmware *fw, phys_addr_t *fw_addr,
  53. size_t *fw_size, bool *fw_relocate)
  54. {
  55. const struct elf32_phdr *phdrs;
  56. const struct elf32_phdr *phdr;
  57. const struct elf32_hdr *ehdr;
  58. phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
  59. phys_addr_t max_addr = 0;
  60. bool relocate = false;
  61. int i;
  62. ehdr = (struct elf32_hdr *)fw->data;
  63. phdrs = (struct elf32_phdr *)(ehdr + 1);
  64. for (i = 0; i < ehdr->e_phnum; i++) {
  65. phdr = &phdrs[i];
  66. if (phdr->p_type != PT_LOAD)
  67. continue;
  68. if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
  69. continue;
  70. if (!phdr->p_memsz)
  71. continue;
  72. if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
  73. relocate = true;
  74. if (phdr->p_paddr < min_addr)
  75. min_addr = phdr->p_paddr;
  76. if (phdr->p_paddr + phdr->p_memsz > max_addr)
  77. max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
  78. }
  79. if (fw_addr)
  80. *fw_addr = min_addr;
  81. if (fw_size)
  82. *fw_size = max_addr - min_addr;
  83. if (fw_relocate)
  84. *fw_relocate = relocate;
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(qcom_mdt_parse);
  88. /**
  89. * qcom_mdt_load() - load the firmware which header is defined in fw
  90. * @rproc: rproc handle
  91. * @fw: frimware object for the header
  92. * @firmware: filename of the firmware, for building .bXX names
  93. *
  94. * Returns 0 on success, negative errno otherwise.
  95. */
  96. int qcom_mdt_load(struct rproc *rproc,
  97. const struct firmware *fw,
  98. const char *firmware)
  99. {
  100. const struct elf32_phdr *phdrs;
  101. const struct elf32_phdr *phdr;
  102. const struct elf32_hdr *ehdr;
  103. const struct firmware *seg_fw;
  104. size_t fw_name_len;
  105. char *fw_name;
  106. void *ptr;
  107. int ret;
  108. int i;
  109. ehdr = (struct elf32_hdr *)fw->data;
  110. phdrs = (struct elf32_phdr *)(ehdr + 1);
  111. fw_name_len = strlen(firmware);
  112. if (fw_name_len <= 4)
  113. return -EINVAL;
  114. fw_name = kstrdup(firmware, GFP_KERNEL);
  115. if (!fw_name)
  116. return -ENOMEM;
  117. for (i = 0; i < ehdr->e_phnum; i++) {
  118. phdr = &phdrs[i];
  119. if (phdr->p_type != PT_LOAD)
  120. continue;
  121. if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
  122. continue;
  123. if (!phdr->p_memsz)
  124. continue;
  125. ptr = rproc_da_to_va(rproc, phdr->p_paddr, phdr->p_memsz,
  126. RPROC_FLAGS_NONE);
  127. if (!ptr) {
  128. dev_err(&rproc->dev, "segment outside memory range\n");
  129. ret = -EINVAL;
  130. break;
  131. }
  132. if (phdr->p_filesz) {
  133. sprintf(fw_name + fw_name_len - 3, "b%02d", i);
  134. ret = request_firmware(&seg_fw, fw_name, &rproc->dev);
  135. if (ret) {
  136. dev_err(&rproc->dev, "failed to load %s\n",
  137. fw_name);
  138. break;
  139. }
  140. memcpy(ptr, seg_fw->data, seg_fw->size);
  141. release_firmware(seg_fw);
  142. }
  143. if (phdr->p_memsz > phdr->p_filesz)
  144. memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
  145. }
  146. kfree(fw_name);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL_GPL(qcom_mdt_load);
  150. MODULE_DESCRIPTION("Firmware parser for Qualcomm MDT format");
  151. MODULE_LICENSE("GPL v2");