FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_map.c
1 /*-
2 * Copyright (c) 2010-2011 Aleksandr Rybalko <ray@dlink.ua>
3 * based on geom_redboot.c
4 * Copyright (c) 2009 Sam Leffler, Errno Consulting
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 * 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.
17 *
18 * NO WARRANTY
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.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/11.0/sys/geom/geom_map.c 292779 2015-12-27 17:09:23Z allanjude $");
34
35 #include <sys/param.h>
36 #include <sys/bus.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>
43 #include <sys/bio.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sbuf.h>
47
48 #include <geom/geom.h>
49 #include <geom/geom_slice.h>
50
51 #define MAP_CLASS_NAME "MAP"
52 #define MAP_MAXSLICE 64
53 #define MAP_MAX_MARKER_LEN 64
54
55 struct g_map_softc {
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;
62 };
63
64 static int
65 g_map_access(struct g_provider *pp, int dread, int dwrite, int dexcl)
66 {
67 struct g_geom *gp;
68 struct g_slicer *gsp;
69 struct g_map_softc *sc;
70
71 gp = pp->geom;
72 gsp = gp->softc;
73 sc = gsp->softc;
74
75 if (dwrite > 0 && sc->readonly[pp->index])
76 return (EPERM);
77
78 return (sc->parent_access(pp, dread, dwrite, dexcl));
79 }
80
81 static int
82 g_map_start(struct bio *bp)
83 {
84 struct g_provider *pp;
85 struct g_geom *gp;
86 struct g_map_softc *sc;
87 struct g_slicer *gsp;
88 int idx;
89
90 pp = bp->bio_to;
91 idx = pp->index;
92 gp = pp->geom;
93 gsp = gp->softc;
94 sc = gsp->softc;
95
96 if (bp->bio_cmd == BIO_GETATTR) {
97 if (g_handleattr_int(bp, MAP_CLASS_NAME "::entry",
98 sc->entry[idx])) {
99 return (1);
100 }
101 if (g_handleattr_int(bp, MAP_CLASS_NAME "::dsize",
102 sc->dsize[idx])) {
103 return (1);
104 }
105 }
106
107 return (0);
108 }
109
110 static void
111 g_map_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
112 struct g_consumer *cp __unused, struct g_provider *pp)
113 {
114 struct g_map_softc *sc;
115 struct g_slicer *gsp;
116
117 gsp = gp->softc;
118 sc = gsp->softc;
119 g_slice_dumpconf(sb, indent, gp, cp, pp);
120 if (pp != NULL) {
121 if (indent == NULL) {
122 sbuf_printf(sb, " entry %jd", (intmax_t)sc->entry[pp->index]);
123 sbuf_printf(sb, " dsize %jd", (intmax_t)sc->dsize[pp->index]);
124 } else {
125 sbuf_printf(sb, "%s<entry>%jd</entry>\n", indent,
126 (intmax_t)sc->entry[pp->index]);
127 sbuf_printf(sb, "%s<dsize>%jd</dsize>\n", indent,
128 (intmax_t)sc->dsize[pp->index]);
129 }
130 }
131 }
132
133 static int
134 find_marker(struct g_consumer *cp, const char *line, off_t *offset)
135 {
136 off_t search_start, search_offset, search_step;
137 size_t sectorsize;
138 uint8_t *buf;
139 char *op, key[MAP_MAX_MARKER_LEN], search_key[MAP_MAX_MARKER_LEN];
140 int ret, c;
141
142 /* Try convert to numeric first */
143 *offset = strtouq(line, &op, 0);
144 if (*op == '\0')
145 return (0);
146
147 bzero(search_key, MAP_MAX_MARKER_LEN);
148 sectorsize = cp->provider->sectorsize;
149
150 ret = sscanf(line, "search:%qi:%qi:%63c",
151 &search_start, &search_step, search_key);
152 if (ret < 3)
153 return (1);
154
155 if (bootverbose) {
156 printf("MAP: search %s for key \"%s\" from 0x%jx, step 0x%jx\n",
157 cp->geom->name, search_key, (intmax_t)search_start, (intmax_t)search_step);
158 }
159
160 /* error if search_key is empty */
161 if (strlen(search_key) < 1)
162 return (1);
163
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) {
168
169 g_topology_unlock();
170 buf = g_read_data(cp, rounddown(search_offset, sectorsize),
171 roundup(strlen(search_key), sectorsize), NULL);
172 g_topology_lock();
173
174 /*
175 * Don't bother doing the rest if buf==NULL; eg derefencing
176 * to assemble 'key'.
177 */
178 if (buf == NULL)
179 continue;
180
181 /* Wildcard, replace '.' with byte from data */
182 /* TODO: add support wildcard escape '\.' */
183
184 strncpy(key, search_key, MAP_MAX_MARKER_LEN);
185
186 for (c = 0; c < MAP_MAX_MARKER_LEN && key[c]; c++) {
187 if (key[c] == '.') {
188 key[c] = ((char *)(buf +
189 (search_offset % sectorsize)))[c];
190 }
191 }
192
193 /* Assume buf != NULL here */
194 if (memcmp(buf + search_offset % sectorsize,
195 key, strlen(search_key)) == 0) {
196 g_free(buf);
197 /* Marker found, so return their offset */
198 *offset = search_offset;
199 return (0);
200 }
201 g_free(buf);
202 }
203
204 /* Marker not found */
205 return (1);
206 }
207
208 static int
209 g_map_parse_part(struct g_class *mp, struct g_provider *pp,
210 struct g_consumer *cp, struct g_geom *gp, struct g_map_softc *sc, int i)
211 {
212 const char *value, *name;
213 char *op;
214 off_t start, end, offset, size, dsize;
215 int readonly, ret;
216
217 /* hint.map.0.at="cfid0" - bind to cfid0 media */
218 if (resource_string_value("map", i, "at", &value) != 0)
219 return (1);
220
221 /* Check if this correct provider */
222 if (strcmp(pp->name, value) != 0)
223 return (1);
224
225 /*
226 * hint.map.0.name="uboot" - name of partition, will be available
227 * as "/dev/map/uboot"
228 */
229 if (resource_string_value("map", i, "name", &name) != 0) {
230 if (bootverbose)
231 printf("MAP: hint.map.%d has no name\n", i);
232 return (1);
233 }
234
235 /*
236 * hint.map.0.start="0x00010000" - partition start at 0x00010000
237 * or hint.map.0.start="search:0x00010000:0x200:marker text" -
238 * search for text "marker text", begin at 0x10000, step 0x200
239 * until we found marker or end of media reached
240 */
241 if (resource_string_value("map", i, "start", &value) != 0) {
242 if (bootverbose)
243 printf("MAP: \"%s\" has no start value\n", name);
244 return (1);
245 }
246 if (find_marker(cp, value, &start) != 0) {
247 if (bootverbose) {
248 printf("MAP: \"%s\" can't parse/use start value\n",
249 name);
250 }
251 return (1);
252 }
253
254 /* like "start" */
255 if (resource_string_value("map", i, "end", &value) != 0) {
256 if (bootverbose)
257 printf("MAP: \"%s\" has no end value\n", name);
258 return (1);
259 }
260 if (find_marker(cp, value, &end) != 0) {
261 if (bootverbose) {
262 printf("MAP: \"%s\" can't parse/use end value\n",
263 name);
264 }
265 return (1);
266 }
267
268 /* variable readonly optional, disable write access */
269 if (resource_int_value("map", i, "readonly", &readonly) != 0)
270 readonly = 0;
271
272 /* offset of partition data, from partition begin */
273 if (resource_string_value("map", i, "offset", &value) == 0) {
274 offset = strtouq(value, &op, 0);
275 if (*op != '\0') {
276 if (bootverbose) {
277 printf("MAP: \"%s\" can't parse offset\n",
278 name);
279 }
280 return (1);
281 }
282 } else {
283 offset = 0;
284 }
285
286 /* partition data size */
287 if (resource_string_value("map", i, "dsize", &value) == 0) {
288 dsize = strtouq(value, &op, 0);
289 if (*op != '\0') {
290 if (bootverbose) {
291 printf("MAP: \"%s\" can't parse dsize\n",
292 name);
293 }
294 return (1);
295 }
296 } else {
297 dsize = 0;
298 }
299
300 size = end - start;
301 if (dsize == 0)
302 dsize = size - offset;
303
304 /* end is 0 or size is 0, No MAP - so next */
305 if (end < start) {
306 if (bootverbose) {
307 printf("MAP: \"%s\", \"end\" less than "
308 "\"start\"\n", name);
309 }
310 return (1);
311 }
312
313 if (offset + dsize > size) {
314 if (bootverbose) {
315 printf("MAP: \"%s\", \"dsize\" bigger than "
316 "partition - offset\n", name);
317 }
318 return (1);
319 }
320
321 ret = g_slice_config(gp, i, G_SLICE_CONFIG_SET, start + offset,
322 dsize, cp->provider->sectorsize, "map/%s", name);
323 if (ret != 0) {
324 if (bootverbose) {
325 printf("MAP: g_slice_config returns %d for \"%s\"\n",
326 ret, name);
327 }
328 return (1);
329 }
330
331 if (bootverbose) {
332 printf("MAP: %s: %jxx%jx, data=%jxx%jx "
333 "\"/dev/map/%s\"\n",
334 cp->geom->name, (intmax_t)start, (intmax_t)size, (intmax_t)offset,
335 (intmax_t)dsize, name);
336 }
337
338 sc->offset[i] = start;
339 sc->size[i] = size;
340 sc->entry[i] = offset;
341 sc->dsize[i] = dsize;
342 sc->readonly[i] = readonly ? 1 : 0;
343
344 return (0);
345 }
346
347 static struct g_geom *
348 g_map_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
349 {
350 struct g_map_softc *sc;
351 struct g_consumer *cp;
352 struct g_geom *gp;
353 int i;
354
355 g_trace(G_T_TOPOLOGY, "map_taste(%s,%s)", mp->name, pp->name);
356 g_topology_assert();
357 if (strcmp(pp->geom->class->name, MAP_CLASS_NAME) == 0)
358 return (NULL);
359
360 gp = g_slice_new(mp, MAP_MAXSLICE, pp, &cp, &sc, sizeof(*sc),
361 g_map_start);
362 if (gp == NULL)
363 return (NULL);
364
365 /* interpose our access method */
366 sc->parent_access = gp->access;
367 gp->access = g_map_access;
368
369 for (i = 0; i < MAP_MAXSLICE; i++)
370 g_map_parse_part(mp, pp, cp, gp, sc, i);
371
372
373 g_access(cp, -1, 0, 0);
374 if (LIST_EMPTY(&gp->provider)) {
375 if (bootverbose)
376 printf("MAP: No valid partition found at %s\n", pp->name);
377 g_slice_spoiled(cp);
378 return (NULL);
379 }
380 return (gp);
381 }
382
383 static void
384 g_map_config(struct gctl_req *req, struct g_class *mp, const char *verb)
385 {
386 struct g_geom *gp;
387
388 g_topology_assert();
389 gp = gctl_get_geom(req, mp, "geom");
390 if (gp == NULL)
391 return;
392 gctl_error(req, "Unknown verb");
393 }
394
395 static struct g_class g_map_class = {
396 .name = MAP_CLASS_NAME,
397 .version = G_VERSION,
398 .taste = g_map_taste,
399 .dumpconf = g_map_dumpconf,
400 .ctlreq = g_map_config,
401 };
402 DECLARE_GEOM_CLASS(g_map_class, g_map);
Cache object: d0368ccf146f6188f0f021c93893e871
|