qcom_mdt_loader.c 4.4 KB

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