scpart.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/mtd/scpart.c: Sercomm Partition Parser
  4. *
  5. * Copyright (C) 2018 NOGUCHI Hiroshi
  6. * Copyright (C) 2022 Mikhail Zhilkin
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/partitions.h>
  12. #include <linux/module.h>
  13. #define MOD_NAME "scpart"
  14. #ifdef pr_fmt
  15. #undef pr_fmt
  16. #endif
  17. #define pr_fmt(fmt) MOD_NAME ": " fmt
  18. #define ID_ALREADY_FOUND 0xffffffffUL
  19. #define MAP_OFFS_IN_BLK 0x800
  20. #define MAP_MIRROR_NUM 2
  21. static const char sc_part_magic[] = {
  22. 'S', 'C', 'F', 'L', 'M', 'A', 'P', 'O', 'K', '\0',
  23. };
  24. #define PART_MAGIC_LEN sizeof(sc_part_magic)
  25. /* assumes that all fields are set by CPU native endian */
  26. struct sc_part_desc {
  27. uint32_t part_id;
  28. uint32_t part_offs;
  29. uint32_t part_bytes;
  30. };
  31. static uint32_t scpart_desc_is_valid(struct sc_part_desc *pdesc)
  32. {
  33. return ((pdesc->part_id != 0xffffffffUL) &&
  34. (pdesc->part_offs != 0xffffffffUL) &&
  35. (pdesc->part_bytes != 0xffffffffUL));
  36. }
  37. static int scpart_scan_partmap(struct mtd_info *master, loff_t partmap_offs,
  38. struct sc_part_desc **ppdesc)
  39. {
  40. int cnt = 0;
  41. int res = 0;
  42. int res2;
  43. uint32_t offs;
  44. size_t retlen;
  45. struct sc_part_desc *pdesc = NULL;
  46. struct sc_part_desc *tmpdesc;
  47. uint8_t *buf;
  48. buf = kzalloc(master->erasesize, GFP_KERNEL);
  49. if (!buf) {
  50. res = -ENOMEM;
  51. goto out;
  52. }
  53. res2 = mtd_read(master, partmap_offs, master->erasesize, &retlen, buf);
  54. if (res2 || retlen != master->erasesize) {
  55. res = -EIO;
  56. goto free;
  57. }
  58. for (offs = MAP_OFFS_IN_BLK;
  59. offs < master->erasesize - sizeof(*tmpdesc);
  60. offs += sizeof(*tmpdesc)) {
  61. tmpdesc = (struct sc_part_desc *)&buf[offs];
  62. if (!scpart_desc_is_valid(tmpdesc))
  63. break;
  64. cnt++;
  65. }
  66. if (cnt > 0) {
  67. int bytes = cnt * sizeof(*pdesc);
  68. pdesc = kcalloc(cnt, sizeof(*pdesc), GFP_KERNEL);
  69. if (!pdesc) {
  70. res = -ENOMEM;
  71. goto free;
  72. }
  73. memcpy(pdesc, &(buf[MAP_OFFS_IN_BLK]), bytes);
  74. *ppdesc = pdesc;
  75. res = cnt;
  76. }
  77. free:
  78. kfree(buf);
  79. out:
  80. return res;
  81. }
  82. static int scpart_find_partmap(struct mtd_info *master,
  83. struct sc_part_desc **ppdesc)
  84. {
  85. int magic_found = 0;
  86. int res = 0;
  87. int res2;
  88. loff_t offs = 0;
  89. size_t retlen;
  90. uint8_t rdbuf[PART_MAGIC_LEN];
  91. while ((magic_found < MAP_MIRROR_NUM) &&
  92. (offs < master->size) &&
  93. !mtd_block_isbad(master, offs)) {
  94. res2 = mtd_read(master, offs, PART_MAGIC_LEN, &retlen, rdbuf);
  95. if (res2 || retlen != PART_MAGIC_LEN) {
  96. res = -EIO;
  97. goto out;
  98. }
  99. if (!memcmp(rdbuf, sc_part_magic, PART_MAGIC_LEN)) {
  100. pr_debug("Signature found at 0x%llx\n", offs);
  101. magic_found++;
  102. res = scpart_scan_partmap(master, offs, ppdesc);
  103. if (res > 0)
  104. goto out;
  105. }
  106. offs += master->erasesize;
  107. }
  108. out:
  109. if (res > 0)
  110. pr_info("Valid 'SC PART MAP' (%d partitions) found at 0x%llx\n", res, offs);
  111. else
  112. pr_info("No valid 'SC PART MAP' was found\n");
  113. return res;
  114. }
  115. static int scpart_parse(struct mtd_info *master,
  116. const struct mtd_partition **pparts,
  117. struct mtd_part_parser_data *data)
  118. {
  119. const char *partname;
  120. int n;
  121. int nr_scparts;
  122. int nr_parts = 0;
  123. int res = 0;
  124. struct sc_part_desc *scpart_map = NULL;
  125. struct mtd_partition *parts = NULL;
  126. struct device_node *mtd_node;
  127. struct device_node *ofpart_node;
  128. struct device_node *pp;
  129. mtd_node = mtd_get_of_node(master);
  130. if (!mtd_node) {
  131. res = -ENOENT;
  132. goto out;
  133. }
  134. ofpart_node = of_get_child_by_name(mtd_node, "partitions");
  135. if (!ofpart_node) {
  136. pr_info("%s: 'partitions' subnode not found on %pOF.\n",
  137. master->name, mtd_node);
  138. res = -ENOENT;
  139. goto out;
  140. }
  141. nr_scparts = scpart_find_partmap(master, &scpart_map);
  142. if (nr_scparts <= 0) {
  143. pr_info("No any partitions was found in 'SC PART MAP'.\n");
  144. res = -ENOENT;
  145. goto free;
  146. }
  147. parts = kcalloc(of_get_child_count(ofpart_node), sizeof(*parts),
  148. GFP_KERNEL);
  149. if (!parts) {
  150. res = -ENOMEM;
  151. goto free;
  152. }
  153. for_each_child_of_node(ofpart_node, pp) {
  154. u32 scpart_id;
  155. if (of_property_read_u32(pp, "sercomm,scpart-id", &scpart_id))
  156. continue;
  157. for (n = 0 ; n < nr_scparts ; n++)
  158. if ((scpart_map[n].part_id != ID_ALREADY_FOUND) &&
  159. (scpart_id == scpart_map[n].part_id))
  160. break;
  161. if (n >= nr_scparts)
  162. /* not match */
  163. continue;
  164. /* add the partition found in OF into MTD partition array */
  165. parts[nr_parts].offset = scpart_map[n].part_offs;
  166. parts[nr_parts].size = scpart_map[n].part_bytes;
  167. parts[nr_parts].of_node = pp;
  168. if (!of_property_read_string(pp, "label", &partname))
  169. parts[nr_parts].name = partname;
  170. if (of_property_read_bool(pp, "read-only"))
  171. parts[nr_parts].mask_flags |= MTD_WRITEABLE;
  172. if (of_property_read_bool(pp, "lock"))
  173. parts[nr_parts].mask_flags |= MTD_POWERUP_LOCK;
  174. /* mark as 'done' */
  175. scpart_map[n].part_id = ID_ALREADY_FOUND;
  176. nr_parts++;
  177. }
  178. if (nr_parts > 0) {
  179. *pparts = parts;
  180. res = nr_parts;
  181. } else
  182. pr_info("No partition in OF matches partition ID with 'SC PART MAP'.\n");
  183. of_node_put(pp);
  184. free:
  185. of_node_put(ofpart_node);
  186. kfree(scpart_map);
  187. if (res <= 0)
  188. kfree(parts);
  189. out:
  190. return res;
  191. }
  192. static const struct of_device_id scpart_parser_of_match_table[] = {
  193. { .compatible = "sercomm,sc-partitions" },
  194. {},
  195. };
  196. MODULE_DEVICE_TABLE(of, scpart_parser_of_match_table);
  197. static struct mtd_part_parser scpart_parser = {
  198. .parse_fn = scpart_parse,
  199. .name = "scpart",
  200. .of_match_table = scpart_parser_of_match_table,
  201. };
  202. module_mtd_part_parser(scpart_parser);
  203. /* mtd parsers will request the module by parser name */
  204. MODULE_ALIAS("scpart");
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("NOGUCHI Hiroshi <drvlabo@gmail.com>");
  207. MODULE_AUTHOR("Mikhail Zhilkin <csharper2005@gmail.com>");
  208. MODULE_DESCRIPTION("Sercomm partition parser");