xref: /freebsd-12.1/sys/geom/geom_map.c (revision 74d6c131)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2011 Aleksandr Rybalko <[email protected]>
5  *   based on geom_redboot.c
6  * Copyright (c) 2009 Sam Leffler, Errno Consulting
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17  *    redistribution must be conditioned upon including a substantially
18  *    similar Disclaimer requirement for further binary redistribution.
19  *
20  * NO WARRANTY
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGES.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/errno.h>
40 #include <sys/endian.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
45 #include <sys/bio.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sbuf.h>
49 
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
52 
53 #define	MAP_CLASS_NAME	"MAP"
54 #define	MAP_MAXSLICE	64
55 #define	MAP_MAX_MARKER_LEN	64
56 
57 struct g_map_softc {
58 	off_t		 offset[MAP_MAXSLICE];	/* offset in flash */
59 	off_t		 size[MAP_MAXSLICE];	/* image size in bytes */
60 	off_t		 entry[MAP_MAXSLICE];
61 	off_t		 dsize[MAP_MAXSLICE];
62 	uint8_t		 readonly[MAP_MAXSLICE];
63 	g_access_t	*parent_access;
64 };
65 
66 static int
g_map_access(struct g_provider * pp,int dread,int dwrite,int dexcl)67 g_map_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
68 {
69 	struct g_geom *gp;
70 	struct g_slicer *gsp;
71 	struct g_map_softc *sc;
72 
73 	gp = pp->geom;
74 	gsp = gp->softc;
75 	sc = gsp->softc;
76 
77 	if (dwrite > 0 && sc->readonly[pp->index])
78 		return (EPERM);
79 
80 	return (sc->parent_access(pp, dread, dwrite, dexcl));
81 }
82 
83 static int
g_map_start(struct bio * bp)84 g_map_start(struct bio *bp)
85 {
86 	struct g_provider *pp;
87 	struct g_geom *gp;
88 	struct g_map_softc *sc;
89 	struct g_slicer *gsp;
90 	int idx;
91 
92 	pp = bp->bio_to;
93 	idx = pp->index;
94 	gp = pp->geom;
95 	gsp = gp->softc;
96 	sc = gsp->softc;
97 
98 	if (bp->bio_cmd == BIO_GETATTR) {
99 		if (g_handleattr_int(bp, MAP_CLASS_NAME "::entry",
100 		    sc->entry[idx])) {
101 			return (1);
102 		}
103 		if (g_handleattr_int(bp, MAP_CLASS_NAME "::dsize",
104 		    sc->dsize[idx])) {
105 			return (1);
106 		}
107 	}
108 
109 	return (0);
110 }
111 
112 static void
g_map_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp __unused,struct g_provider * pp)113 g_map_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
114     struct g_consumer *cp __unused, struct g_provider *pp)
115 {
116 	struct g_map_softc *sc;
117 	struct g_slicer *gsp;
118 
119 	gsp = gp->softc;
120 	sc = gsp->softc;
121 	g_slice_dumpconf(sb, indent, gp, cp, pp);
122 	if (pp != NULL) {
123 		if (indent == NULL) {
124 			sbuf_printf(sb, " entry %jd", (intmax_t)sc->entry[pp->index]);
125 			sbuf_printf(sb, " dsize %jd", (intmax_t)sc->dsize[pp->index]);
126 		} else {
127 			sbuf_printf(sb, "%s<entry>%jd</entry>\n", indent,
128 			    (intmax_t)sc->entry[pp->index]);
129 			sbuf_printf(sb, "%s<dsize>%jd</dsize>\n", indent,
130 			    (intmax_t)sc->dsize[pp->index]);
131 		}
132 	}
133 }
134 
135 static int
find_marker(struct g_consumer * cp,const char * line,off_t * offset)136 find_marker(struct g_consumer *cp, const char *line, off_t *offset)
137 {
138 	off_t search_start, search_offset, search_step;
139 	size_t sectorsize;
140 	uint8_t *buf;
141 	char *op, key[MAP_MAX_MARKER_LEN], search_key[MAP_MAX_MARKER_LEN];
142 	int ret, c;
143 
144 	/* Try convert to numeric first */
145 	*offset = strtouq(line, &op, 0);
146 	if (*op == '\0')
147 		return (0);
148 
149 	bzero(search_key, MAP_MAX_MARKER_LEN);
150 	sectorsize = cp->provider->sectorsize;
151 
152 #ifdef __LP64__
153 	ret = sscanf(line, "search:%li:%li:%63c",
154 	    &search_start, &search_step, search_key);
155 #else
156 	ret = sscanf(line, "search:%qi:%qi:%63c",
157 	    &search_start, &search_step, search_key);
158 #endif
159 	if (ret < 3)
160 		return (1);
161 
162 	if (bootverbose) {
163 		printf("MAP: search %s for key \"%s\" from 0x%jx, step 0x%jx\n",
164 		    cp->geom->name, search_key, (intmax_t)search_start, (intmax_t)search_step);
165 	}
166 
167 	/* error if search_key is empty */
168 	if (strlen(search_key) < 1)
169 		return (1);
170 
171 	/* sscanf successful, and we start marker search */
172 	for (search_offset = search_start;
173 	     search_offset < cp->provider->mediasize;
174 	     search_offset += search_step) {
175 
176 		g_topology_unlock();
177 		buf = g_read_data(cp, rounddown(search_offset, sectorsize),
178 		    roundup(strlen(search_key), sectorsize), NULL);
179 		g_topology_lock();
180 
181 		/*
182 		 * Don't bother doing the rest if buf==NULL; eg derefencing
183 		 * to assemble 'key'.
184 		 */
185 		if (buf == NULL)
186 			continue;
187 
188 		/* Wildcard, replace '.' with byte from data */
189 		/* TODO: add support wildcard escape '\.' */
190 
191 		strncpy(key, search_key, MAP_MAX_MARKER_LEN);
192 
193 		for (c = 0; c < MAP_MAX_MARKER_LEN && key[c]; c++) {
194 			if (key[c] == '.') {
195 				key[c] = ((char *)(buf +
196 				    (search_offset % sectorsize)))[c];
197 			}
198 		}
199 
200 		/* Assume buf != NULL here */
201 		if (memcmp(buf + search_offset % sectorsize,
202 		    key, strlen(search_key)) == 0) {
203 			g_free(buf);
204 			/* Marker found, so return their offset */
205 			*offset = search_offset;
206 			return (0);
207 		}
208 		g_free(buf);
209 	}
210 
211 	/* Marker not found */
212 	return (1);
213 }
214 
215 static int
g_map_parse_part(struct g_class * mp,struct g_provider * pp,struct g_consumer * cp,struct g_geom * gp,struct g_map_softc * sc,int i)216 g_map_parse_part(struct g_class *mp, struct g_provider *pp,
217     struct g_consumer *cp, struct g_geom *gp, struct g_map_softc *sc, int i)
218 {
219 	const char *value, *name;
220 	char *op;
221 	off_t start, end, offset, size, dsize;
222 	int readonly, ret;
223 
224 	/* hint.map.0.at="cfid0" - bind to cfid0 media */
225 	if (resource_string_value("map", i, "at", &value) != 0)
226 		return (1);
227 
228 	/* Check if this correct provider */
229 	if (strcmp(pp->name, value) != 0)
230 		return (1);
231 
232 	/*
233 	 * hint.map.0.name="uboot" - name of partition, will be available
234 	 * as "/dev/map/uboot"
235 	 */
236 	if (resource_string_value("map", i, "name", &name) != 0) {
237 		if (bootverbose)
238 			printf("MAP: hint.map.%d has no name\n", i);
239 		return (1);
240 	}
241 
242 	/*
243 	 * hint.map.0.start="0x00010000" - partition start at 0x00010000
244 	 * or hint.map.0.start="search:0x00010000:0x200:marker text" -
245 	 * search for text "marker text", begin at 0x10000, step 0x200
246 	 * until we found marker or end of media reached
247 	 */
248 	if (resource_string_value("map", i, "start", &value) != 0) {
249 		if (bootverbose)
250 			printf("MAP: \"%s\" has no start value\n", name);
251 		return (1);
252 	}
253 	if (find_marker(cp, value, &start) != 0) {
254 		if (bootverbose) {
255 			printf("MAP: \"%s\" can't parse/use start value\n",
256 			    name);
257 		}
258 		return (1);
259 	}
260 
261 	/* like "start" */
262 	if (resource_string_value("map", i, "end", &value) != 0) {
263 		if (bootverbose)
264 			printf("MAP: \"%s\" has no end value\n", name);
265 		return (1);
266 	}
267 	if (find_marker(cp, value, &end) != 0) {
268 		if (bootverbose) {
269 			printf("MAP: \"%s\" can't parse/use end value\n",
270 			    name);
271 		}
272 		return (1);
273 	}
274 
275 	/* variable readonly optional, disable write access */
276 	if (resource_int_value("map", i, "readonly", &readonly) != 0)
277 		readonly = 0;
278 
279 	/* offset of partition data, from partition begin */
280 	if (resource_string_value("map", i, "offset", &value) == 0) {
281 		offset = strtouq(value, &op, 0);
282 		if (*op != '\0') {
283 			if (bootverbose) {
284 				printf("MAP: \"%s\" can't parse offset\n",
285 				    name);
286 			}
287 			return (1);
288 		}
289 	} else {
290 		offset = 0;
291 	}
292 
293 	/* partition data size */
294 	if (resource_string_value("map", i, "dsize", &value) == 0) {
295 		dsize = strtouq(value, &op, 0);
296 		if (*op != '\0') {
297 			if (bootverbose) {
298 				printf("MAP: \"%s\" can't parse dsize\n",
299 				    name);
300 			}
301 			return (1);
302 		}
303 	} else {
304 		dsize = 0;
305 	}
306 
307 	size = end - start;
308 	if (dsize == 0)
309 		dsize = size - offset;
310 
311 	/* end is 0 or size is 0, No MAP - so next */
312 	if (end < start) {
313 		if (bootverbose) {
314 			printf("MAP: \"%s\", \"end\" less than "
315 			    "\"start\"\n", name);
316 		}
317 		return (1);
318 	}
319 
320 	if (offset + dsize > size) {
321 		if (bootverbose) {
322 			printf("MAP: \"%s\", \"dsize\" bigger than "
323 			    "partition - offset\n", name);
324 		}
325 		return (1);
326 	}
327 
328 	ret = g_slice_config(gp, i, G_SLICE_CONFIG_SET, start + offset,
329 	    dsize, cp->provider->sectorsize, "map/%s", name);
330 	if (ret != 0) {
331 		if (bootverbose) {
332 			printf("MAP: g_slice_config returns %d for \"%s\"\n",
333 			    ret, name);
334 		}
335 		return (1);
336 	}
337 
338 	if (bootverbose) {
339 		printf("MAP: %s: %jxx%jx, data=%jxx%jx "
340 		    "\"/dev/map/%s\"\n",
341 		    cp->geom->name, (intmax_t)start, (intmax_t)size, (intmax_t)offset,
342 		    (intmax_t)dsize, name);
343 	}
344 
345 	sc->offset[i] = start;
346 	sc->size[i] = size;
347 	sc->entry[i] = offset;
348 	sc->dsize[i] = dsize;
349 	sc->readonly[i] = readonly ? 1 : 0;
350 
351 	return (0);
352 }
353 
354 static struct g_geom *
g_map_taste(struct g_class * mp,struct g_provider * pp,int insist __unused)355 g_map_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
356 {
357 	struct g_map_softc *sc;
358 	struct g_consumer *cp;
359 	struct g_geom *gp;
360 	int i;
361 
362 	g_trace(G_T_TOPOLOGY, "map_taste(%s,%s)", mp->name, pp->name);
363 	g_topology_assert();
364 	if (strcmp(pp->geom->class->name, MAP_CLASS_NAME) == 0)
365 		return (NULL);
366 
367 	gp = g_slice_new(mp, MAP_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
368 	    g_map_start);
369 	if (gp == NULL)
370 		return (NULL);
371 
372 	/* interpose our access method */
373 	sc->parent_access = gp->access;
374 	gp->access = g_map_access;
375 
376 	for (i = 0; i < MAP_MAXSLICE; i++)
377 		g_map_parse_part(mp, pp, cp, gp, sc, i);
378 
379 
380 	g_access(cp, -1, 0, 0);
381 	if (LIST_EMPTY(&gp->provider)) {
382 		if (bootverbose)
383 			printf("MAP: No valid partition found at %s\n", pp->name);
384 		g_slice_spoiled(cp);
385 		return (NULL);
386 	}
387 	return (gp);
388 }
389 
390 static void
g_map_config(struct gctl_req * req,struct g_class * mp,const char * verb)391 g_map_config(struct gctl_req *req, struct g_class *mp, const char *verb)
392 {
393 	struct g_geom *gp;
394 
395 	g_topology_assert();
396 	gp = gctl_get_geom(req, mp, "geom");
397 	if (gp == NULL)
398 		return;
399 	gctl_error(req, "Unknown verb");
400 }
401 
402 static struct g_class g_map_class = {
403 	.name = MAP_CLASS_NAME,
404 	.version = G_VERSION,
405 	.taste = g_map_taste,
406 	.dumpconf = g_map_dumpconf,
407 	.ctlreq = g_map_config,
408 };
409 DECLARE_GEOM_CLASS(g_map_class, g_map);
410 MODULE_VERSION(geom_map, 0);
411