ofpart.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Flash partitions described by the OF (or flattened) device tree
  3. *
  4. * Copyright © 2006 MontaVista Software Inc.
  5. * Author: Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * Revised to handle newer style flash binding by:
  8. * Copyright © 2007 David Gibson, IBM Corporation.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/of.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/slab.h>
  20. #include <linux/mtd/partitions.h>
  21. static int parse_ofpart_partitions(struct mtd_info *master,
  22. struct mtd_partition **pparts,
  23. struct mtd_part_parser_data *data)
  24. {
  25. struct device_node *node;
  26. const char *partname;
  27. struct device_node *pp;
  28. int nr_parts, i;
  29. if (!data)
  30. return 0;
  31. node = data->of_node;
  32. if (!node)
  33. return 0;
  34. /* First count the subnodes */
  35. pp = NULL;
  36. nr_parts = 0;
  37. while ((pp = of_get_next_child(node, pp)))
  38. nr_parts++;
  39. if (nr_parts == 0)
  40. return 0;
  41. *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  42. if (!*pparts)
  43. return -ENOMEM;
  44. pp = NULL;
  45. i = 0;
  46. while ((pp = of_get_next_child(node, pp))) {
  47. const __be32 *reg;
  48. int len;
  49. reg = of_get_property(pp, "reg", &len);
  50. if (!reg) {
  51. nr_parts--;
  52. continue;
  53. }
  54. (*pparts)[i].offset = be32_to_cpu(reg[0]);
  55. (*pparts)[i].size = be32_to_cpu(reg[1]);
  56. partname = of_get_property(pp, "label", &len);
  57. if (!partname)
  58. partname = of_get_property(pp, "name", &len);
  59. (*pparts)[i].name = (char *)partname;
  60. if (of_get_property(pp, "read-only", &len))
  61. (*pparts)[i].mask_flags |= MTD_WRITEABLE;
  62. if (of_get_property(pp, "lock", &len))
  63. (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
  64. i++;
  65. }
  66. if (!i) {
  67. of_node_put(pp);
  68. pr_err("No valid partition found on %s\n", node->full_name);
  69. kfree(*pparts);
  70. *pparts = NULL;
  71. return -EINVAL;
  72. }
  73. return nr_parts;
  74. }
  75. static struct mtd_part_parser ofpart_parser = {
  76. .owner = THIS_MODULE,
  77. .parse_fn = parse_ofpart_partitions,
  78. .name = "ofpart",
  79. };
  80. static int parse_ofoldpart_partitions(struct mtd_info *master,
  81. struct mtd_partition **pparts,
  82. struct mtd_part_parser_data *data)
  83. {
  84. struct device_node *dp;
  85. int i, plen, nr_parts;
  86. const struct {
  87. __be32 offset, len;
  88. } *part;
  89. const char *names;
  90. if (!data)
  91. return 0;
  92. dp = data->of_node;
  93. if (!dp)
  94. return 0;
  95. part = of_get_property(dp, "partitions", &plen);
  96. if (!part)
  97. return 0; /* No partitions found */
  98. pr_warning("Device tree uses obsolete partition map binding: %s\n",
  99. dp->full_name);
  100. nr_parts = plen / sizeof(part[0]);
  101. *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  102. if (!*pparts)
  103. return -ENOMEM;
  104. names = of_get_property(dp, "partition-names", &plen);
  105. for (i = 0; i < nr_parts; i++) {
  106. (*pparts)[i].offset = be32_to_cpu(part->offset);
  107. (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  108. /* bit 0 set signifies read only partition */
  109. if (be32_to_cpu(part->len) & 1)
  110. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  111. if (names && (plen > 0)) {
  112. int len = strlen(names) + 1;
  113. (*pparts)[i].name = (char *)names;
  114. plen -= len;
  115. names += len;
  116. } else {
  117. (*pparts)[i].name = "unnamed";
  118. }
  119. part++;
  120. }
  121. return nr_parts;
  122. }
  123. static struct mtd_part_parser ofoldpart_parser = {
  124. .owner = THIS_MODULE,
  125. .parse_fn = parse_ofoldpart_partitions,
  126. .name = "ofoldpart",
  127. };
  128. static int __init ofpart_parser_init(void)
  129. {
  130. int rc;
  131. rc = register_mtd_parser(&ofpart_parser);
  132. if (rc)
  133. goto out;
  134. rc = register_mtd_parser(&ofoldpart_parser);
  135. if (!rc)
  136. return 0;
  137. deregister_mtd_parser(&ofoldpart_parser);
  138. out:
  139. return rc;
  140. }
  141. static void __exit ofpart_parser_exit(void)
  142. {
  143. deregister_mtd_parser(&ofpart_parser);
  144. deregister_mtd_parser(&ofoldpart_parser);
  145. }
  146. module_init(ofpart_parser_init);
  147. module_exit(ofpart_parser_exit);
  148. MODULE_LICENSE("GPL");
  149. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  150. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  151. /*
  152. * When MTD core cannot find the requested parser, it tries to load the module
  153. * with the same name. Since we provide the ofoldpart parser, we should have
  154. * the corresponding alias.
  155. */
  156. MODULE_ALIAS("ofoldpart");