Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.14-rc3 232 lines 5.7 kB view raw
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 16#include <linux/module.h> 17#include <linux/init.h> 18#include <linux/of.h> 19#include <linux/mtd/mtd.h> 20#include <linux/slab.h> 21#include <linux/mtd/partitions.h> 22 23static bool node_has_compatible(struct device_node *pp) 24{ 25 return of_get_property(pp, "compatible", NULL); 26} 27 28static int parse_ofpart_partitions(struct mtd_info *master, 29 const struct mtd_partition **pparts, 30 struct mtd_part_parser_data *data) 31{ 32 struct mtd_partition *parts; 33 struct device_node *mtd_node; 34 struct device_node *ofpart_node; 35 const char *partname; 36 struct device_node *pp; 37 int nr_parts, i, ret = 0; 38 bool dedicated = true; 39 40 41 /* Pull of_node from the master device node */ 42 mtd_node = mtd_get_of_node(master); 43 if (!mtd_node) 44 return 0; 45 46 ofpart_node = of_get_child_by_name(mtd_node, "partitions"); 47 if (!ofpart_node) { 48 /* 49 * We might get here even when ofpart isn't used at all (e.g., 50 * when using another parser), so don't be louder than 51 * KERN_DEBUG 52 */ 53 pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n", 54 master->name, mtd_node); 55 ofpart_node = mtd_node; 56 dedicated = false; 57 } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) { 58 /* The 'partitions' subnode might be used by another parser */ 59 return 0; 60 } 61 62 /* First count the subnodes */ 63 nr_parts = 0; 64 for_each_child_of_node(ofpart_node, pp) { 65 if (!dedicated && node_has_compatible(pp)) 66 continue; 67 68 nr_parts++; 69 } 70 71 if (nr_parts == 0) 72 return 0; 73 74 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); 75 if (!parts) 76 return -ENOMEM; 77 78 i = 0; 79 for_each_child_of_node(ofpart_node, pp) { 80 const __be32 *reg; 81 int len; 82 int a_cells, s_cells; 83 84 if (!dedicated && node_has_compatible(pp)) 85 continue; 86 87 reg = of_get_property(pp, "reg", &len); 88 if (!reg) { 89 if (dedicated) { 90 pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n", 91 master->name, pp, 92 mtd_node); 93 goto ofpart_fail; 94 } else { 95 nr_parts--; 96 continue; 97 } 98 } 99 100 a_cells = of_n_addr_cells(pp); 101 s_cells = of_n_size_cells(pp); 102 if (len / 4 != a_cells + s_cells) { 103 pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n", 104 master->name, pp, 105 mtd_node); 106 goto ofpart_fail; 107 } 108 109 parts[i].offset = of_read_number(reg, a_cells); 110 parts[i].size = of_read_number(reg + a_cells, s_cells); 111 parts[i].of_node = pp; 112 113 partname = of_get_property(pp, "label", &len); 114 if (!partname) 115 partname = of_get_property(pp, "name", &len); 116 parts[i].name = partname; 117 118 if (of_get_property(pp, "read-only", &len)) 119 parts[i].mask_flags |= MTD_WRITEABLE; 120 121 if (of_get_property(pp, "lock", &len)) 122 parts[i].mask_flags |= MTD_POWERUP_LOCK; 123 124 i++; 125 } 126 127 if (!nr_parts) 128 goto ofpart_none; 129 130 *pparts = parts; 131 return nr_parts; 132 133ofpart_fail: 134 pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n", 135 master->name, pp, mtd_node); 136 ret = -EINVAL; 137ofpart_none: 138 of_node_put(pp); 139 kfree(parts); 140 return ret; 141} 142 143static struct mtd_part_parser ofpart_parser = { 144 .parse_fn = parse_ofpart_partitions, 145 .name = "ofpart", 146}; 147 148static int parse_ofoldpart_partitions(struct mtd_info *master, 149 const struct mtd_partition **pparts, 150 struct mtd_part_parser_data *data) 151{ 152 struct mtd_partition *parts; 153 struct device_node *dp; 154 int i, plen, nr_parts; 155 const struct { 156 __be32 offset, len; 157 } *part; 158 const char *names; 159 160 /* Pull of_node from the master device node */ 161 dp = mtd_get_of_node(master); 162 if (!dp) 163 return 0; 164 165 part = of_get_property(dp, "partitions", &plen); 166 if (!part) 167 return 0; /* No partitions found */ 168 169 pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp); 170 171 nr_parts = plen / sizeof(part[0]); 172 173 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); 174 if (!parts) 175 return -ENOMEM; 176 177 names = of_get_property(dp, "partition-names", &plen); 178 179 for (i = 0; i < nr_parts; i++) { 180 parts[i].offset = be32_to_cpu(part->offset); 181 parts[i].size = be32_to_cpu(part->len) & ~1; 182 /* bit 0 set signifies read only partition */ 183 if (be32_to_cpu(part->len) & 1) 184 parts[i].mask_flags = MTD_WRITEABLE; 185 186 if (names && (plen > 0)) { 187 int len = strlen(names) + 1; 188 189 parts[i].name = names; 190 plen -= len; 191 names += len; 192 } else { 193 parts[i].name = "unnamed"; 194 } 195 196 part++; 197 } 198 199 *pparts = parts; 200 return nr_parts; 201} 202 203static struct mtd_part_parser ofoldpart_parser = { 204 .parse_fn = parse_ofoldpart_partitions, 205 .name = "ofoldpart", 206}; 207 208static int __init ofpart_parser_init(void) 209{ 210 register_mtd_parser(&ofpart_parser); 211 register_mtd_parser(&ofoldpart_parser); 212 return 0; 213} 214 215static void __exit ofpart_parser_exit(void) 216{ 217 deregister_mtd_parser(&ofpart_parser); 218 deregister_mtd_parser(&ofoldpart_parser); 219} 220 221module_init(ofpart_parser_init); 222module_exit(ofpart_parser_exit); 223 224MODULE_LICENSE("GPL"); 225MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree"); 226MODULE_AUTHOR("Vitaly Wool, David Gibson"); 227/* 228 * When MTD core cannot find the requested parser, it tries to load the module 229 * with the same name. Since we provide the ofoldpart parser, we should have 230 * the corresponding alias. 231 */ 232MODULE_ALIAS("ofoldpart");