qcomsmempart.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Qualcomm SMEM NAND flash partition parser
  4. *
  5. * Copyright (C) 2020, Linaro Ltd.
  6. */
  7. #include <linux/ctype.h>
  8. #include <linux/module.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/partitions.h>
  11. #include <linux/slab.h>
  12. #include <linux/soc/qcom/smem.h>
  13. #define SMEM_AARM_PARTITION_TABLE 9
  14. #define SMEM_APPS 0
  15. #define SMEM_FLASH_PART_MAGIC1 0x55ee73aa
  16. #define SMEM_FLASH_PART_MAGIC2 0xe35ebddb
  17. #define SMEM_FLASH_PTABLE_V3 3
  18. #define SMEM_FLASH_PTABLE_V4 4
  19. #define SMEM_FLASH_PTABLE_MAX_PARTS_V3 16
  20. #define SMEM_FLASH_PTABLE_MAX_PARTS_V4 48
  21. #define SMEM_FLASH_PTABLE_HDR_LEN (4 * sizeof(u32))
  22. #define SMEM_FLASH_PTABLE_NAME_SIZE 16
  23. /**
  24. * struct smem_flash_pentry - SMEM Flash partition entry
  25. * @name: Name of the partition
  26. * @offset: Offset in blocks
  27. * @length: Length of the partition in blocks
  28. * @attr: Flags for this partition
  29. */
  30. struct smem_flash_pentry {
  31. char name[SMEM_FLASH_PTABLE_NAME_SIZE];
  32. __le32 offset;
  33. __le32 length;
  34. u8 attr;
  35. } __packed __aligned(4);
  36. /**
  37. * struct smem_flash_ptable - SMEM Flash partition table
  38. * @magic1: Partition table Magic 1
  39. * @magic2: Partition table Magic 2
  40. * @version: Partition table version
  41. * @numparts: Number of partitions in this ptable
  42. * @pentry: Flash partition entries belonging to this ptable
  43. */
  44. struct smem_flash_ptable {
  45. __le32 magic1;
  46. __le32 magic2;
  47. __le32 version;
  48. __le32 numparts;
  49. struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
  50. } __packed __aligned(4);
  51. static int parse_qcomsmem_part(struct mtd_info *mtd,
  52. const struct mtd_partition **pparts,
  53. struct mtd_part_parser_data *data)
  54. {
  55. size_t len = SMEM_FLASH_PTABLE_HDR_LEN;
  56. int ret, i, j, tmpparts, numparts = 0;
  57. struct smem_flash_pentry *pentry;
  58. struct smem_flash_ptable *ptable;
  59. struct mtd_partition *parts;
  60. char *name, *c;
  61. if (IS_ENABLED(CONFIG_MTD_SPI_NOR_USE_4K_SECTORS)
  62. && mtd->type == MTD_NORFLASH) {
  63. pr_err("%s: SMEM partition parser is incompatible with 4K sectors\n",
  64. mtd->name);
  65. return -EINVAL;
  66. }
  67. pr_debug("Parsing partition table info from SMEM\n");
  68. ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
  69. if (IS_ERR(ptable)) {
  70. if (PTR_ERR(ptable) != -EPROBE_DEFER)
  71. pr_err("Error reading partition table header\n");
  72. return PTR_ERR(ptable);
  73. }
  74. /* Verify ptable magic */
  75. if (le32_to_cpu(ptable->magic1) != SMEM_FLASH_PART_MAGIC1 ||
  76. le32_to_cpu(ptable->magic2) != SMEM_FLASH_PART_MAGIC2) {
  77. pr_err("Partition table magic verification failed\n");
  78. return -EINVAL;
  79. }
  80. /* Ensure that # of partitions is less than the max we have allocated */
  81. tmpparts = le32_to_cpu(ptable->numparts);
  82. if (tmpparts > SMEM_FLASH_PTABLE_MAX_PARTS_V4) {
  83. pr_err("Partition numbers exceed the max limit\n");
  84. return -EINVAL;
  85. }
  86. /* Find out length of partition data based on table version */
  87. if (le32_to_cpu(ptable->version) <= SMEM_FLASH_PTABLE_V3) {
  88. len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V3 *
  89. sizeof(struct smem_flash_pentry);
  90. } else if (le32_to_cpu(ptable->version) == SMEM_FLASH_PTABLE_V4) {
  91. len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V4 *
  92. sizeof(struct smem_flash_pentry);
  93. } else {
  94. pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
  95. return -EINVAL;
  96. }
  97. /*
  98. * Now that the partition table header has been parsed, verified
  99. * and the length of the partition table calculated, read the
  100. * complete partition table
  101. */
  102. ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
  103. if (IS_ERR(ptable)) {
  104. pr_err("Error reading partition table\n");
  105. return PTR_ERR(ptable);
  106. }
  107. for (i = 0; i < tmpparts; i++) {
  108. pentry = &ptable->pentry[i];
  109. if (pentry->name[0] != '\0')
  110. numparts++;
  111. }
  112. parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
  113. if (!parts)
  114. return -ENOMEM;
  115. for (i = 0, j = 0; i < tmpparts; i++) {
  116. pentry = &ptable->pentry[i];
  117. if (pentry->name[0] == '\0')
  118. continue;
  119. name = kstrdup(pentry->name, GFP_KERNEL);
  120. if (!name) {
  121. ret = -ENOMEM;
  122. goto out_free_parts;
  123. }
  124. /* Convert name to lower case */
  125. for (c = name; *c != '\0'; c++)
  126. *c = tolower(*c);
  127. parts[j].name = name;
  128. parts[j].offset = le32_to_cpu(pentry->offset) * mtd->erasesize;
  129. parts[j].mask_flags = pentry->attr;
  130. parts[j].size = le32_to_cpu(pentry->length) * mtd->erasesize;
  131. pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
  132. i, pentry->name, le32_to_cpu(pentry->offset),
  133. le32_to_cpu(pentry->length), pentry->attr);
  134. j++;
  135. }
  136. pr_debug("SMEM partition table found: ver: %d len: %d\n",
  137. le32_to_cpu(ptable->version), tmpparts);
  138. *pparts = parts;
  139. return numparts;
  140. out_free_parts:
  141. while (--j >= 0)
  142. kfree(parts[j].name);
  143. kfree(parts);
  144. *pparts = NULL;
  145. return ret;
  146. }
  147. static void parse_qcomsmem_cleanup(const struct mtd_partition *pparts,
  148. int nr_parts)
  149. {
  150. int i;
  151. for (i = 0; i < nr_parts; i++)
  152. kfree(pparts[i].name);
  153. kfree(pparts);
  154. }
  155. static const struct of_device_id qcomsmem_of_match_table[] = {
  156. { .compatible = "qcom,smem-part" },
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
  160. static struct mtd_part_parser mtd_parser_qcomsmem = {
  161. .parse_fn = parse_qcomsmem_part,
  162. .cleanup = parse_qcomsmem_cleanup,
  163. .name = "qcomsmem",
  164. .of_match_table = qcomsmem_of_match_table,
  165. };
  166. module_mtd_part_parser(mtd_parser_qcomsmem);
  167. MODULE_LICENSE("GPL v2");
  168. MODULE_AUTHOR("Manivannan Sadhasivam <[email protected]>");
  169. MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");