ofpart.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. i++;
  63. }
  64. if (!i) {
  65. of_node_put(pp);
  66. pr_err("No valid partition found on %s\n", node->full_name);
  67. kfree(*pparts);
  68. *pparts = NULL;
  69. return -EINVAL;
  70. }
  71. return nr_parts;
  72. }
  73. static struct mtd_part_parser ofpart_parser = {
  74. .owner = THIS_MODULE,
  75. .parse_fn = parse_ofpart_partitions,
  76. .name = "ofpart",
  77. };
  78. static int parse_ofoldpart_partitions(struct mtd_info *master,
  79. struct mtd_partition **pparts,
  80. struct mtd_part_parser_data *data)
  81. {
  82. struct device_node *dp;
  83. int i, plen, nr_parts;
  84. const struct {
  85. __be32 offset, len;
  86. } *part;
  87. const char *names;
  88. if (!data)
  89. return 0;
  90. dp = data->of_node;
  91. if (!dp)
  92. return 0;
  93. part = of_get_property(dp, "partitions", &plen);
  94. if (!part)
  95. return 0; /* No partitions found */
  96. pr_warning("Device tree uses obsolete partition map binding: %s\n",
  97. dp->full_name);
  98. nr_parts = plen / sizeof(part[0]);
  99. *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  100. if (!*pparts)
  101. return -ENOMEM;
  102. names = of_get_property(dp, "partition-names", &plen);
  103. for (i = 0; i < nr_parts; i++) {
  104. (*pparts)[i].offset = be32_to_cpu(part->offset);
  105. (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  106. /* bit 0 set signifies read only partition */
  107. if (be32_to_cpu(part->len) & 1)
  108. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  109. if (names && (plen > 0)) {
  110. int len = strlen(names) + 1;
  111. (*pparts)[i].name = (char *)names;
  112. plen -= len;
  113. names += len;
  114. } else {
  115. (*pparts)[i].name = "unnamed";
  116. }
  117. part++;
  118. }
  119. return nr_parts;
  120. }
  121. static struct mtd_part_parser ofoldpart_parser = {
  122. .owner = THIS_MODULE,
  123. .parse_fn = parse_ofoldpart_partitions,
  124. .name = "ofoldpart",
  125. };
  126. static int __init ofpart_parser_init(void)
  127. {
  128. int rc;
  129. rc = register_mtd_parser(&ofpart_parser);
  130. if (rc)
  131. goto out;
  132. rc = register_mtd_parser(&ofoldpart_parser);
  133. if (!rc)
  134. return 0;
  135. deregister_mtd_parser(&ofoldpart_parser);
  136. out:
  137. return rc;
  138. }
  139. module_init(ofpart_parser_init);
  140. MODULE_LICENSE("GPL");
  141. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  142. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  143. /*
  144. * When MTD core cannot find the requested parser, it tries to load the module
  145. * with the same name. Since we provide the ofoldpart parser, we should have
  146. * the corresponding alias.
  147. */
  148. MODULE_ALIAS("ofoldpart");