xref: /netbsd-src/sys/dev/flash/flash_mtdparts.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: flash_mtdparts.c,v 1.1 2017/11/13 17:35:58 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: flash_mtdparts.c,v 1.1 2017/11/13 17:35:58 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/kmem.h>
37 
38 #include <dev/flash/flash.h>
39 
40 extern int flash_print(void *, const char *);
41 
42 static void
43 flash_attach_partition(struct flash_interface *flash_if, device_t parent,
44     struct flash_partition *part)
45 {
46 	struct flash_attach_args faa;
47 
48 	faa.flash_if = flash_if;
49 	faa.partinfo = *part;
50 
51 	config_found_ia(parent, "flashbus", &faa, flash_print);
52 }
53 
54 static flash_size_t
55 flash_parse_size(char *partdef, char **ep)
56 {
57 	flash_size_t size;
58 
59 	/* Parse the size parameter */
60 	size = strtoul(partdef, ep, 10);
61 	if (partdef == *ep)
62 		return 0;
63 
64 	switch (**ep) {
65 	case 'G':
66 	case 'g':
67 		size <<= 10;
68 		/* FALLTHROUGH */
69 	case 'M':
70 	case 'm':
71 		size <<= 10;
72 		/* FALLTHROUGH */
73 	case 'K':
74 	case 'k':
75 		size <<= 10;
76 		(*ep)++;
77 		break;
78 	}
79 
80 	return size;
81 }
82 
83 static int
84 flash_parse_partdef(flash_size_t flash_size, char *partdef, flash_off_t *offset,
85     struct flash_partition *ppart)
86 {
87 	struct flash_partition part;
88 
89 	/* Get the partition size */
90 	if (*partdef == '-') {
91 		/* Use the remaining space */
92 		part.part_size = 0;
93 		partdef++;
94 	} else {
95 		part.part_size = flash_parse_size(partdef, &partdef);
96 		if (part.part_size == 0)
97 			return EINVAL;
98 	}
99 
100 	if (*partdef == '@') {
101 		/* Explicit offset */
102 		partdef++;
103 		part.part_offset = flash_parse_size(partdef, &partdef);
104 	} else {
105 		/* Offset is the end of the previous partition */
106 		part.part_offset = *offset;
107 	}
108 
109 	/* Calculate partition size for "all remaining space" parts */
110 	if (part.part_size == 0)
111 		part.part_size = flash_size - part.part_offset;
112 
113 	if (*partdef == '(') {
114 		/* Partition name */
115 		partdef++;
116 		part.part_name = partdef;
117 		partdef = strchr(partdef, ')');
118 		if (partdef == NULL)
119 			return EINVAL;
120 		*partdef = '\0';
121 		partdef++;
122 	}
123 
124 	part.part_flags = 0;
125 	if (strncmp(partdef, "ro", 2) == 0) {
126 		part.part_flags |= FLASH_PART_READONLY;
127 		partdef += 2;
128 	}
129 
130 	*ppart = part;
131 
132 	*offset = part.part_offset + part.part_size;
133 
134 	return 0;
135 }
136 
137 static void
138 flash_parse_mtddef(struct flash_interface *flash_if, device_t parent,
139     flash_size_t flash_size, char *mtddef)
140 {
141 	struct flash_partition part;
142 	char *partdef = mtddef, *nextdef;
143 	flash_off_t offset = 0;
144 	int error;
145 
146 	while (partdef && offset < flash_size) {
147 		/* Find the end */
148 		nextdef = strchr(partdef, ',');
149 		if (nextdef == NULL)
150 		    nextdef = strchr(partdef, ' ');
151 		if (nextdef)
152 			*nextdef++ = '\0';
153 
154 		error = flash_parse_partdef(flash_size, partdef, &offset,
155 		    &part);
156 		if (error) {
157 			aprint_error_dev(parent, "bad partition def '%s'\n",
158 			    partdef);
159 			return;
160 		}
161 
162 		flash_attach_partition(flash_if, parent, &part);
163 
164 		partdef = nextdef;
165 	}
166 }
167 
168 /*
169  * Attach partitions to a given parent device node that match the supplied
170  * device id. The cmdline follows the following format:
171  *
172  * mtdparts=<mtddef>[;<mtddef]
173  * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
174  * <partdef> := <size>[@offset][<name>][ro]
175  * <mtd-id>  := unique id used in mapping driver/device (number of flash bank)
176  * <size>    := memsize OR "-" to denote all remaining space
177  * <name>    := '(' NAME ')'
178  */
179 void
180 flash_attach_mtdparts(struct flash_interface *flash_if, device_t parent,
181     flash_size_t flash_size, const char *mtd_id, const char *cmdline)
182 {
183 	char *mtddef;
184 	size_t mtddeflen;
185 
186 	/* Find the definition for our mtd id */
187 	const char *s = strstr(cmdline, mtd_id);
188 	if (s == NULL || s[strlen(mtd_id)] != ':')
189 		return;
190 
191 	mtddef = kmem_strdupsize(s + strlen(mtd_id) + 1, &mtddeflen, KM_SLEEP);
192 
193 	flash_parse_mtddef(flash_if, parent, flash_size, mtddef);
194 
195 	kmem_free(mtddef, mtddeflen);
196 }
197