2 * Copyright (c) 2010-2011 Aleksandr Rybalko <ray@dlink.ua>
3 * based on geom_redboot.c
4 * Copyright (c) 2009 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/endian.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <geom/geom.h>
49 #include <geom/geom_slice.h>
51 #define MAP_CLASS_NAME "MAP"
52 #define MAP_MAXSLICE 64
53 #define MAP_MAX_MARKER_LEN 64
56 off_t offset[MAP_MAXSLICE]; /* offset in flash */
57 off_t size[MAP_MAXSLICE]; /* image size in bytes */
58 off_t entry[MAP_MAXSLICE];
59 off_t dsize[MAP_MAXSLICE];
60 uint8_t readonly[MAP_MAXSLICE];
61 g_access_t *parent_access;
65 g_map_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
69 struct g_map_softc *sc;
75 if (dwrite > 0 && sc->readonly[pp->index])
78 return (sc->parent_access(pp, dread, dwrite, dexcl));
82 g_map_start(struct bio *bp)
84 struct g_provider *pp;
86 struct g_map_softc *sc;
96 if (bp->bio_cmd == BIO_GETATTR) {
97 if (g_handleattr_int(bp, MAP_CLASS_NAME "::entry",
101 if (g_handleattr_int(bp, MAP_CLASS_NAME "::dsize",
111 g_map_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
112 struct g_consumer *cp __unused, struct g_provider *pp)
114 struct g_map_softc *sc;
115 struct g_slicer *gsp;
119 g_slice_dumpconf(sb, indent, gp, cp, pp);
121 if (indent == NULL) {
122 sbuf_printf(sb, " entry %lld", sc->entry[pp->index]);
123 sbuf_printf(sb, " dsize %lld", sc->dsize[pp->index]);
125 sbuf_printf(sb, "%s<entry>%lld</entry>\n", indent,
126 sc->entry[pp->index]);
127 sbuf_printf(sb, "%s<dsize>%lld</dsize>\n", indent,
128 sc->dsize[pp->index]);
134 find_marker(struct g_consumer *cp, const char *line, off_t *offset)
136 off_t search_start, search_offset, search_step;
139 char *op, key[MAP_MAX_MARKER_LEN], search_key[MAP_MAX_MARKER_LEN];
142 /* Try convert to numeric first */
143 *offset = strtouq(line, &op, 0);
147 bzero(search_key, MAP_MAX_MARKER_LEN);
148 sectorsize = cp->provider->sectorsize;
150 ret = sscanf(line, "search:%qi:%qi:%63c",
151 &search_start, &search_step, search_key);
156 printf("MAP: search key \"%s\" from 0x%llx, step 0x%llx\n",
157 search_key, search_start, search_step);
160 /* error if search_key is empty */
161 if (strlen(search_key) < 1)
164 /* sscanf successful, and we start marker search */
165 for (search_offset = search_start;
166 search_offset < cp->provider->mediasize;
167 search_offset += search_step) {
170 buf = g_read_data(cp, rounddown(search_offset, sectorsize),
171 roundup(strlen(search_key), sectorsize), NULL);
174 /* Wildcard, replace '.' with byte from data */
175 /* TODO: add support wildcard escape '\.' */
177 strncpy(key, search_key, MAP_MAX_MARKER_LEN);
179 for (c = 0; c < MAP_MAX_MARKER_LEN && key[c]; c++) {
181 key[c] = ((char *)(buf +
182 (search_offset % sectorsize)))[c];
186 if (buf != NULL && strncmp(buf + search_offset % sectorsize,
187 key, strlen(search_key)) == 0) {
189 /* Marker found, so return their offset */
190 *offset = search_offset;
196 /* Marker not found */
201 g_map_parse_part(struct g_class *mp, struct g_provider *pp,
202 struct g_consumer *cp, struct g_geom *gp, struct g_map_softc *sc, int i)
204 const char *value, *name;
206 off_t start, end, offset, size, dsize;
209 /* hint.map.0.at="cfid0" - bind to cfid0 media */
210 if (resource_string_value("map", i, "at", &value) != 0)
213 /* Check if this correct provider */
214 if (strcmp(pp->name, value) != 0)
218 * hint.map.0.name="uboot" - name of partition, will be available
219 * as "/dev/map/uboot"
221 if (resource_string_value("map", i, "name", &name) != 0) {
223 printf("MAP: hint.map.%d has no name\n", i);
228 * hint.map.0.start="0x00010000" - partition start at 0x00010000
229 * or hint.map.0.start="search:0x00010000:0x200:marker text" -
230 * search for text "marker text", begin at 0x10000, step 0x200
231 * until we found marker or end of media reached
233 if (resource_string_value("map", i, "start", &value) != 0) {
235 printf("MAP: \"%s\" has no start value\n", name);
238 if (find_marker(cp, value, &start) != 0) {
240 printf("MAP: \"%s\" can't parse/use start value\n",
247 if (resource_string_value("map", i, "end", &value) != 0) {
249 printf("MAP: \"%s\" has no end value\n", name);
252 if (find_marker(cp, value, &end) != 0) {
254 printf("MAP: \"%s\" can't parse/use start value\n",
260 /* variable readonly optional, disable write access */
261 if (resource_int_value("map", i, "readonly", &readonly) != 0)
264 /* offset of partition data, from partition begin */
265 if (resource_string_value("map", i, "offset", &value) == 0) {
266 offset = strtouq(value, &op, 0);
269 printf("MAP: \"%s\" can't parse offset\n",
278 /* partition data size */
279 if (resource_string_value("map", i, "dsize", &value) == 0) {
280 dsize = strtouq(value, &op, 0);
283 printf("MAP: \"%s\" can't parse dsize\n",
294 dsize = size - offset;
296 /* end is 0 or size is 0, No MAP - so next */
299 printf("MAP: \"%s\", \"end\" less than "
300 "\"start\"\n", name);
305 if (offset + dsize > size) {
307 printf("MAP: \"%s\", \"dsize\" bigger than "
308 "partition - offset\n", name);
313 ret = g_slice_config(gp, i, G_SLICE_CONFIG_SET, start + offset,
314 dsize, cp->provider->sectorsize, "map/%s", name);
317 printf("MAP: g_slice_config returns %d for \"%s\"\n",
324 printf("MAP: %llxx%llx, data=%llxx%llx "
326 start, size, offset, dsize, name);
329 sc->offset[i] = start;
331 sc->entry[i] = offset;
332 sc->dsize[i] = dsize;
333 sc->readonly[i] = readonly ? 1 : 0;
338 static struct g_geom *
339 g_map_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
341 struct g_map_softc *sc;
342 struct g_consumer *cp;
346 g_trace(G_T_TOPOLOGY, "map_taste(%s,%s)", mp->name, pp->name);
348 if (strcmp(pp->geom->class->name, MAP_CLASS_NAME) == 0)
351 gp = g_slice_new(mp, MAP_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
356 /* interpose our access method */
357 sc->parent_access = gp->access;
358 gp->access = g_map_access;
360 for (i = 0; i < MAP_MAXSLICE; i++)
361 g_map_parse_part(mp, pp, cp, gp, sc, i);
364 g_access(cp, -1, 0, 0);
365 if (LIST_EMPTY(&gp->provider)) {
367 printf("MAP: No valid partition found at %s\n", pp->name);
375 g_map_config(struct gctl_req *req, struct g_class *mp, const char *verb)
380 gp = gctl_get_geom(req, mp, "geom");
383 gctl_error(req, "Unknown verb");
386 static struct g_class g_map_class = {
387 .name = MAP_CLASS_NAME,
388 .version = G_VERSION,
389 .taste = g_map_taste,
390 .dumpconf = g_map_dumpconf,
391 .ctlreq = g_map_config,
393 DECLARE_GEOM_CLASS(g_map_class, g_map);