2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2011 Aleksandr Rybalko <ray@dlink.ua>
5 * based on geom_redboot.c
6 * Copyright (c) 2009 Sam Leffler, Errno Consulting
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.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>
47 #include <sys/mutex.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
53 #define MAP_CLASS_NAME "MAP"
54 #define MAP_MAXSLICE 64
55 #define MAP_MAX_MARKER_LEN 64
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;
67 g_map_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
71 struct g_map_softc *sc;
77 if (dwrite > 0 && sc->readonly[pp->index])
80 return (sc->parent_access(pp, dread, dwrite, dexcl));
84 g_map_start(struct bio *bp)
86 struct g_provider *pp;
88 struct g_map_softc *sc;
98 if (bp->bio_cmd == BIO_GETATTR) {
99 if (g_handleattr_int(bp, MAP_CLASS_NAME "::entry",
103 if (g_handleattr_int(bp, MAP_CLASS_NAME "::dsize",
113 g_map_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
114 struct g_consumer *cp __unused, struct g_provider *pp)
116 struct g_map_softc *sc;
117 struct g_slicer *gsp;
121 g_slice_dumpconf(sb, indent, gp, cp, pp);
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]);
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]);
136 find_marker(struct g_consumer *cp, const char *line, off_t *offset)
138 off_t search_start, search_offset, search_step;
141 char *op, key[MAP_MAX_MARKER_LEN], search_key[MAP_MAX_MARKER_LEN];
144 /* Try convert to numeric first */
145 *offset = strtouq(line, &op, 0);
149 bzero(search_key, MAP_MAX_MARKER_LEN);
150 sectorsize = cp->provider->sectorsize;
153 ret = sscanf(line, "search:%li:%li:%63c",
154 &search_start, &search_step, search_key);
156 ret = sscanf(line, "search:%qi:%qi:%63c",
157 &search_start, &search_step, search_key);
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);
167 /* error if search_key is empty */
168 if (strlen(search_key) < 1)
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) {
177 buf = g_read_data(cp, rounddown(search_offset, sectorsize),
178 roundup(strlen(search_key), sectorsize), NULL);
182 * Don't bother doing the rest if buf==NULL; eg derefencing
188 /* Wildcard, replace '.' with byte from data */
189 /* TODO: add support wildcard escape '\.' */
191 strncpy(key, search_key, MAP_MAX_MARKER_LEN);
193 for (c = 0; c < MAP_MAX_MARKER_LEN && key[c]; c++) {
195 key[c] = ((char *)(buf +
196 (search_offset % sectorsize)))[c];
200 /* Assume buf != NULL here */
201 if (memcmp(buf + search_offset % sectorsize,
202 key, strlen(search_key)) == 0) {
204 /* Marker found, so return their offset */
205 *offset = search_offset;
211 /* Marker not found */
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)
219 const char *value, *name;
221 off_t start, end, offset, size, dsize;
224 /* hint.map.0.at="cfid0" - bind to cfid0 media */
225 if (resource_string_value("map", i, "at", &value) != 0)
228 /* Check if this correct provider */
229 if (strcmp(pp->name, value) != 0)
233 * hint.map.0.name="uboot" - name of partition, will be available
234 * as "/dev/map/uboot"
236 if (resource_string_value("map", i, "name", &name) != 0) {
238 printf("MAP: hint.map.%d has no name\n", i);
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
248 if (resource_string_value("map", i, "start", &value) != 0) {
250 printf("MAP: \"%s\" has no start value\n", name);
253 if (find_marker(cp, value, &start) != 0) {
255 printf("MAP: \"%s\" can't parse/use start value\n",
262 if (resource_string_value("map", i, "end", &value) != 0) {
264 printf("MAP: \"%s\" has no end value\n", name);
267 if (find_marker(cp, value, &end) != 0) {
269 printf("MAP: \"%s\" can't parse/use end value\n",
275 /* variable readonly optional, disable write access */
276 if (resource_int_value("map", i, "readonly", &readonly) != 0)
279 /* offset of partition data, from partition begin */
280 if (resource_string_value("map", i, "offset", &value) == 0) {
281 offset = strtouq(value, &op, 0);
284 printf("MAP: \"%s\" can't parse offset\n",
293 /* partition data size */
294 if (resource_string_value("map", i, "dsize", &value) == 0) {
295 dsize = strtouq(value, &op, 0);
298 printf("MAP: \"%s\" can't parse dsize\n",
309 dsize = size - offset;
311 /* end is 0 or size is 0, No MAP - so next */
314 printf("MAP: \"%s\", \"end\" less than "
315 "\"start\"\n", name);
320 if (offset + dsize > size) {
322 printf("MAP: \"%s\", \"dsize\" bigger than "
323 "partition - offset\n", name);
328 ret = g_slice_config(gp, i, G_SLICE_CONFIG_SET, start + offset,
329 dsize, cp->provider->sectorsize, "map/%s", name);
332 printf("MAP: g_slice_config returns %d for \"%s\"\n",
339 printf("MAP: %s: %jxx%jx, data=%jxx%jx "
341 cp->geom->name, (intmax_t)start, (intmax_t)size, (intmax_t)offset,
342 (intmax_t)dsize, name);
345 sc->offset[i] = start;
347 sc->entry[i] = offset;
348 sc->dsize[i] = dsize;
349 sc->readonly[i] = readonly ? 1 : 0;
354 static struct g_geom *
355 g_map_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
357 struct g_map_softc *sc;
358 struct g_consumer *cp;
362 g_trace(G_T_TOPOLOGY, "map_taste(%s,%s)", mp->name, pp->name);
364 if (strcmp(pp->geom->class->name, MAP_CLASS_NAME) == 0)
367 gp = g_slice_new(mp, MAP_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
372 /* interpose our access method */
373 sc->parent_access = gp->access;
374 gp->access = g_map_access;
376 for (i = 0; i < MAP_MAXSLICE; i++)
377 g_map_parse_part(mp, pp, cp, gp, sc, i);
380 g_access(cp, -1, 0, 0);
381 if (LIST_EMPTY(&gp->provider)) {
383 printf("MAP: No valid partition found at %s\n", pp->name);
391 g_map_config(struct gctl_req *req, struct g_class *mp, const char *verb)
396 gp = gctl_get_geom(req, mp, "geom");
399 gctl_error(req, "Unknown verb");
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,
409 DECLARE_GEOM_CLASS(g_map_class, g_map);
410 MODULE_VERSION(geom_map, 0);