xref: /freebsd/lib/geom/part/geom_part.c (revision 6e5d01124fd4dd57899ddd9260c76dbb43543aa7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007, 2008 Marcel Moolenaar
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 AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/stat.h>
33 #include <sys/vtoc.h>
34 
35 #include <assert.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <libgeom.h>
41 #include <libutil.h>
42 #include <paths.h>
43 #include <signal.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <limits.h>
48 #include <inttypes.h>
49 #include <string.h>
50 #include <strings.h>
51 #include <unistd.h>
52 
53 #include "core/geom.h"
54 #include "misc/subr.h"
55 
56 #ifdef STATIC_GEOM_CLASSES
57 #define	PUBSYM(x)	gpart_##x
58 #else
59 #define	PUBSYM(x)	x
60 #endif
61 
62 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
63 uint32_t PUBSYM(version) = 0;
64 
65 static char sstart[32];
66 static char ssize[32];
67 volatile sig_atomic_t undo_restore;
68 
69 #define	GPART_AUTOFILL	"*"
70 #define	GPART_FLAGS	"C"
71 
72 #define	GPART_PARAM_BOOTCODE	"bootcode"
73 #define	GPART_PARAM_INDEX	"index"
74 #define	GPART_PARAM_PARTCODE	"partcode"
75 #define	GPART_PARAM_SKIP_DSN	"skip_dsn"
76 
77 static struct gclass *find_class(struct gmesh *, const char *);
78 static struct ggeom * find_geom(struct gclass *, const char *);
79 static int geom_is_withered(struct ggeom *);
80 static const char *find_geomcfg(struct ggeom *, const char *);
81 static const char *find_provcfg(struct gprovider *, const char *);
82 static struct gprovider *find_provider(struct ggeom *, off_t);
83 static const char *fmtsize(int64_t);
84 static int gpart_autofill(struct gctl_req *);
85 static int gpart_autofill_resize(struct gctl_req *);
86 static void gpart_bootcode(struct gctl_req *, unsigned int);
87 static void *gpart_bootfile_read(const char *, ssize_t *);
88 static _Noreturn void gpart_issue(struct gctl_req *, unsigned int);
89 static void gpart_show(struct gctl_req *, unsigned int);
90 static void gpart_show_geom(struct ggeom *, const char *, int);
91 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
92 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
93 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
94 static void gpart_print_error(const char *);
95 static void gpart_backup(struct gctl_req *, unsigned int);
96 static void gpart_restore(struct gctl_req *, unsigned int);
97 
98 struct g_command PUBSYM(class_commands)[] = {
99 	{ "add", 0, gpart_issue, {
100 		{ 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
101 		{ 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
102 		{ 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
103 		{ 't', "type", NULL, G_TYPE_STRING },
104 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
105 		{ 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
106 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
107 		G_OPT_SENTINEL },
108 	    "-t type [-a alignment] [-b start] [-s size] [-i index] "
109 		"[-l label] [-f flags] geom"
110 	},
111 	{ "backup", 0, gpart_backup, G_NULL_OPTS,
112 	    "geom"
113 	},
114 	{ "bootcode", 0, gpart_bootcode, {
115 		{ 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
116 		{ 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
117 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
118 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
119 		{ 'N', GPART_PARAM_SKIP_DSN, NULL, G_TYPE_BOOL },
120 		G_OPT_SENTINEL },
121 	    "[-N] [-b bootcode] [-p partcode -i index] [-f flags] geom"
122 	},
123 	{ "commit", 0, gpart_issue, G_NULL_OPTS,
124 	    "geom"
125 	},
126 	{ "create", 0, gpart_issue, {
127 		{ 's', "scheme", NULL, G_TYPE_STRING },
128 		{ 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
129 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
130 		G_OPT_SENTINEL },
131 	    "-s scheme [-n entries] [-f flags] provider"
132 	},
133 	{ "delete", 0, gpart_issue, {
134 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
135 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
136 		G_OPT_SENTINEL },
137 	    "-i index [-f flags] geom"
138 	},
139 	{ "destroy", 0, gpart_issue, {
140 		{ 'F', "force", NULL, G_TYPE_BOOL },
141 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
142 		G_OPT_SENTINEL },
143 	    "[-F] [-f flags] geom"
144 	},
145 	{ "modify", 0, gpart_issue, {
146 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
147 		{ 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
148 		{ 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
149 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
150 		G_OPT_SENTINEL },
151 	    "-i index [-l label] [-t type] [-f flags] geom"
152 	},
153 	{ "set", 0, gpart_issue, {
154 		{ 'a', "attrib", NULL, G_TYPE_STRING },
155 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
156 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
157 		G_OPT_SENTINEL },
158 	    "-a attrib [-i index] [-f flags] geom"
159 	},
160 	{ "show", 0, gpart_show, {
161 		{ 'l', "show_label", NULL, G_TYPE_BOOL },
162 		{ 'r', "show_rawtype", NULL, G_TYPE_BOOL },
163 		{ 'p', "show_providers", NULL, G_TYPE_BOOL },
164 		G_OPT_SENTINEL },
165 	    "[-l | -r] [-p] [geom ...]"
166 	},
167 	{ "undo", 0, gpart_issue, G_NULL_OPTS,
168 	    "geom"
169 	},
170 	{ "unset", 0, gpart_issue, {
171 		{ 'a', "attrib", NULL, G_TYPE_STRING },
172 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
173 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
174 		G_OPT_SENTINEL },
175 	    "-a attrib [-i index] [-f flags] geom"
176 	},
177 	{ "resize", 0, gpart_issue, {
178 		{ 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
179 		{ 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
180 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
181 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
182 		G_OPT_SENTINEL },
183 	    "-i index [-a alignment] [-s size] [-f flags] geom"
184 	},
185 	{ "restore", 0, gpart_restore, {
186 		{ 'F', "force", NULL, G_TYPE_BOOL },
187 		{ 'l', "restore_labels", NULL, G_TYPE_BOOL },
188 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
189 		G_OPT_SENTINEL },
190 	    "[-lF] [-f flags] provider [...]"
191 	},
192 	{ "recover", 0, gpart_issue, {
193 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
194 		G_OPT_SENTINEL },
195 	    "[-f flags] geom"
196 	},
197 	G_CMD_SENTINEL
198 };
199 
200 static struct gclass *
201 find_class(struct gmesh *mesh, const char *name)
202 {
203 	struct gclass *classp;
204 
205 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
206 		if (strcmp(classp->lg_name, name) == 0)
207 			return (classp);
208 	}
209 	return (NULL);
210 }
211 
212 static struct ggeom *
213 find_geom(struct gclass *classp, const char *name)
214 {
215 	struct ggeom *gp, *wgp;
216 
217 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
218 		name += sizeof(_PATH_DEV) - 1;
219 	wgp = NULL;
220 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
221 		if (strcmp(gp->lg_name, name) != 0)
222 			continue;
223 		if (!geom_is_withered(gp))
224 			return (gp);
225 		else
226 			wgp = gp;
227 	}
228 	return (wgp);
229 }
230 
231 static int
232 geom_is_withered(struct ggeom *gp)
233 {
234 	struct gconfig *gc;
235 
236 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
237 		if (!strcmp(gc->lg_name, "wither"))
238 			return (1);
239 	}
240 	return (0);
241 }
242 
243 static const char *
244 find_geomcfg(struct ggeom *gp, const char *cfg)
245 {
246 	struct gconfig *gc;
247 
248 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
249 		if (!strcmp(gc->lg_name, cfg))
250 			return (gc->lg_val);
251 	}
252 	return (NULL);
253 }
254 
255 static const char *
256 find_provcfg(struct gprovider *pp, const char *cfg)
257 {
258 	struct gconfig *gc;
259 
260 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
261 		if (!strcmp(gc->lg_name, cfg))
262 			return (gc->lg_val);
263 	}
264 	return (NULL);
265 }
266 
267 static struct gprovider *
268 find_provider(struct ggeom *gp, off_t minsector)
269 {
270 	struct gprovider *pp, *bestpp;
271 	const char *s;
272 	off_t sector, bestsector;
273 
274 	bestpp = NULL;
275 	bestsector = 0;
276 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
277 		s = find_provcfg(pp, "start");
278 		sector = (off_t)strtoimax(s, NULL, 0);
279 		if (sector < minsector)
280 			continue;
281 		if (bestpp != NULL && sector >= bestsector)
282 			continue;
283 
284 		bestpp = pp;
285 		bestsector = sector;
286 	}
287 	return (bestpp);
288 }
289 
290 static const char *
291 fmtsize(int64_t rawsz)
292 {
293 	static char buf[5];
294 
295 	humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
296 	    HN_B | HN_NOSPACE | HN_DECIMAL);
297 	return (buf);
298 }
299 
300 static const char *
301 fmtattrib(struct gprovider *pp)
302 {
303 	static char buf[128];
304 	struct gconfig *gc;
305 	u_int idx;
306 
307 	buf[0] = '\0';
308 	idx = 0;
309 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
310 		if (strcmp(gc->lg_name, "attrib") != 0)
311 			continue;
312 		idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
313 		    (idx == 0) ? " [" : ",", gc->lg_val);
314 	}
315 	if (idx > 0)
316 		snprintf(buf + idx, sizeof(buf) - idx, "] ");
317 	return (buf);
318 }
319 
320 #define	ALIGNDOWN(d, a)	((d) - (d) % (a))
321 #define	ALIGNUP(d, a)	((d) % (a) ? (d) - (d) % (a) + (a): (d))
322 
323 static int
324 gpart_autofill_resize(struct gctl_req *req)
325 {
326 	struct gmesh mesh;
327 	struct gclass *cp;
328 	struct ggeom *gp;
329 	struct gprovider *pp;
330 	off_t last, size, start, new_size;
331 	off_t lba, new_lba, alignment, offset;
332 	const char *g, *s;
333 	int error, idx, has_alignment;
334 
335 	idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
336 	if (idx < 1)
337 		errx(EXIT_FAILURE, "invalid partition index");
338 
339 	s = gctl_get_ascii(req, "class");
340 	if (s == NULL)
341 		abort();
342 	g = gctl_get_ascii(req, "arg0");
343 	if (g == NULL)
344 		abort();
345 	error = geom_gettree_geom(&mesh, s, g, 1);
346 	if (error)
347 		return (error);
348 	cp = find_class(&mesh, s);
349 	if (cp == NULL)
350 		errx(EXIT_FAILURE, "Class %s not found.", s);
351 	gp = find_geom(cp, g);
352 	if (gp == NULL)
353 		errx(EXIT_FAILURE, "No such geom: %s.", g);
354 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
355 	if (pp == NULL)
356 		errx(EXIT_FAILURE, "Provider for geom %s not found.", g);
357 
358 	s = gctl_get_ascii(req, "alignment");
359 	has_alignment = (*s == '*') ? 0 : 1;
360 	alignment = 1;
361 	if (has_alignment) {
362 		error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
363 		if (error)
364 			errc(EXIT_FAILURE, error, "Invalid alignment param");
365 		if (alignment == 0)
366 			errx(EXIT_FAILURE, "Invalid alignment param");
367 	} else {
368 		lba = pp->lg_stripesize / pp->lg_sectorsize;
369 		if (lba > 0)
370 			alignment = lba;
371 	}
372 	error = gctl_delete_param(req, "alignment");
373 	if (error)
374 		errc(EXIT_FAILURE, error, "internal error");
375 
376 	s = gctl_get_ascii(req, "size");
377 	if (*s == '*')
378 		new_size = 0;
379 	else {
380 		error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
381 		if (error)
382 			errc(EXIT_FAILURE, error, "Invalid size param");
383 		/* no autofill necessary. */
384 		if (has_alignment == 0)
385 			goto done;
386 	}
387 
388 	offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
389 	s = find_geomcfg(gp, "last");
390 	if (s == NULL)
391 		errx(EXIT_FAILURE, "Final block not found for geom %s",
392 		    gp->lg_name);
393 	last = (off_t)strtoimax(s, NULL, 0);
394 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
395 		s = find_provcfg(pp, "index");
396 		if (s == NULL)
397 			continue;
398 		if (atoi(s) == idx)
399 			break;
400 	}
401 	if (pp == NULL)
402 		errx(EXIT_FAILURE, "invalid partition index");
403 
404 	s = find_provcfg(pp, "start");
405 	start = (off_t)strtoimax(s, NULL, 0);
406 	s = find_provcfg(pp, "end");
407 	lba = (off_t)strtoimax(s, NULL, 0);
408 	size = lba - start + 1;
409 
410 	pp = find_provider(gp, lba + 1);
411 	if (new_size > 0 && (new_size <= size || pp == NULL)) {
412 		/* The start offset may be not aligned, so we align the end
413 		 * offset and then calculate the size.
414 		 */
415 		new_size = ALIGNDOWN(start + offset + new_size,
416 		    alignment) - start - offset;
417 		goto done;
418 	}
419 	if (pp == NULL) {
420 		new_size = ALIGNDOWN(last + offset + 1, alignment) -
421 		    start - offset;
422 		if (new_size < size)
423 			return (ENOSPC);
424 	} else {
425 		s = find_provcfg(pp, "start");
426 		new_lba = (off_t)strtoimax(s, NULL, 0);
427 		/*
428 		 * Is there any free space between current and
429 		 * next providers?
430 		 */
431 		new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
432 		if (new_lba > lba)
433 			new_size = new_lba - start;
434 		else {
435 			geom_deletetree(&mesh);
436 			return (ENOSPC);
437 		}
438 	}
439 done:
440 	snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
441 	gctl_change_param(req, "size", -1, ssize);
442 	geom_deletetree(&mesh);
443 	return (0);
444 }
445 
446 static int
447 gpart_autofill(struct gctl_req *req)
448 {
449 	struct gmesh mesh;
450 	struct gclass *cp;
451 	struct ggeom *gp;
452 	struct gprovider *pp;
453 	off_t first, last, a_first;
454 	off_t size, start, a_lba;
455 	off_t lba, len, alignment, offset;
456 	uintmax_t grade;
457 	const char *g, *s;
458 	int error, has_size, has_start, has_alignment;
459 
460 	s = gctl_get_ascii(req, "verb");
461 	if (strcmp(s, "resize") == 0)
462 		return gpart_autofill_resize(req);
463 	if (strcmp(s, "add") != 0)
464 		return (0);
465 
466 	s = gctl_get_ascii(req, "class");
467 	if (s == NULL)
468 		abort();
469 	g = gctl_get_ascii(req, "arg0");
470 	if (g == NULL)
471 		abort();
472 	error = geom_gettree_geom(&mesh, s, g, 1);
473 	if (error)
474 		return (error);
475 	cp = find_class(&mesh, s);
476 	if (cp == NULL)
477 		errx(EXIT_FAILURE, "Class %s not found.", s);
478 	gp = find_geom(cp, g);
479 	if (gp == NULL) {
480 		if (g_device_path(g) == NULL) {
481 			errx(EXIT_FAILURE, "No such geom %s.", g);
482 		} else {
483 			/*
484 			 * We don't free memory allocated by g_device_path() as
485 			 * we are about to exit.
486 			 */
487 			errx(EXIT_FAILURE,
488 			    "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
489 			    g);
490 		}
491 	}
492 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
493 	if (pp == NULL)
494 		errx(EXIT_FAILURE, "Provider for geom %s not found.", g);
495 
496 	s = gctl_get_ascii(req, "alignment");
497 	has_alignment = (*s == '*') ? 0 : 1;
498 	alignment = 1;
499 	if (has_alignment) {
500 		error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
501 		if (error)
502 			errc(EXIT_FAILURE, error, "Invalid alignment param");
503 		if (alignment == 0)
504 			errx(EXIT_FAILURE, "Invalid alignment param");
505 	}
506 	error = gctl_delete_param(req, "alignment");
507 	if (error)
508 		errc(EXIT_FAILURE, error, "internal error");
509 
510 	s = gctl_get_ascii(req, "size");
511 	has_size = (*s == '*') ? 0 : 1;
512 	size = 0;
513 	if (has_size) {
514 		error = g_parse_lba(s, pp->lg_sectorsize, &size);
515 		if (error)
516 			errc(EXIT_FAILURE, error, "Invalid size param");
517 	}
518 
519 	s = gctl_get_ascii(req, "start");
520 	has_start = (*s == '*') ? 0 : 1;
521 	start = 0ULL;
522 	if (has_start) {
523 		error = g_parse_lba(s, pp->lg_sectorsize, &start);
524 		if (error)
525 			errc(EXIT_FAILURE, error, "Invalid start param");
526 	}
527 
528 	/* No autofill necessary. */
529 	if (has_size && has_start && !has_alignment)
530 		goto done;
531 
532 	len = pp->lg_stripesize / pp->lg_sectorsize;
533 	if (len > 0 && !has_alignment)
534 		alignment = len;
535 
536 	/* Adjust parameters to stripeoffset */
537 	offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
538 	start = ALIGNUP(start + offset, alignment);
539 	if (size > alignment)
540 		size = ALIGNDOWN(size, alignment);
541 
542 	s = find_geomcfg(gp, "first");
543 	if (s == NULL)
544 		errx(EXIT_FAILURE, "Starting block not found for geom %s",
545 		    gp->lg_name);
546 	first = (off_t)strtoimax(s, NULL, 0);
547 	s = find_geomcfg(gp, "last");
548 	if (s == NULL)
549 		errx(EXIT_FAILURE, "Final block not found for geom %s",
550 		    gp->lg_name);
551 	last = (off_t)strtoimax(s, NULL, 0);
552 	grade = ~0ULL;
553 	a_first = ALIGNUP(first + offset, alignment);
554 	last = ALIGNDOWN(last + offset + 1, alignment) - 1;
555 	if (a_first < start)
556 		a_first = start;
557 	while ((pp = find_provider(gp, first)) != NULL) {
558 		s = find_provcfg(pp, "start");
559 		lba = (off_t)strtoimax(s, NULL, 0);
560 		a_lba = ALIGNDOWN(lba + offset, alignment);
561 		if (first < a_lba && a_first < a_lba) {
562 			/* Free space [first, lba> */
563 			len = a_lba - a_first;
564 			if (has_size) {
565 				if (len >= size &&
566 				    (uintmax_t)(len - size) < grade) {
567 					start = a_first;
568 					grade = len - size;
569 				}
570 			} else if (has_start) {
571 				if (start >= a_first && start < a_lba) {
572 					size = a_lba - start;
573 					grade = start - a_first;
574 				}
575 			} else {
576 				if (grade == ~0ULL || len > size) {
577 					start = a_first;
578 					size = len;
579 					grade = 0;
580 				}
581 			}
582 		}
583 
584 		s = find_provcfg(pp, "end");
585 		first = (off_t)strtoimax(s, NULL, 0) + 1;
586 		if (first + offset > a_first)
587 			a_first = ALIGNUP(first + offset, alignment);
588 	}
589 	if (a_first <= last) {
590 		/* Free space [first-last] */
591 		len = ALIGNDOWN(last - a_first + 1, alignment);
592 		if (has_size) {
593 			if (len >= size &&
594 			    (uintmax_t)(len - size) < grade) {
595 				start = a_first;
596 				grade = len - size;
597 			}
598 		} else if (has_start) {
599 			if (start >= a_first && start <= last) {
600 				size = ALIGNDOWN(last - start + 1, alignment);
601 				grade = start - a_first;
602 			}
603 		} else {
604 			if (grade == ~0ULL || len > size) {
605 				start = a_first;
606 				size = len;
607 				grade = 0;
608 			}
609 		}
610 	}
611 	if (grade == ~0ULL) {
612 		geom_deletetree(&mesh);
613 		return (ENOSPC);
614 	}
615 	start -= offset;	/* Return back to real offset */
616 done:
617 	snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
618 	gctl_change_param(req, "size", -1, ssize);
619 	snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
620 	gctl_change_param(req, "start", -1, sstart);
621 	geom_deletetree(&mesh);
622 	return (0);
623 }
624 
625 static void
626 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
627 {
628 	struct gprovider *pp;
629 	const char *s, *scheme;
630 	off_t first, last, sector, end;
631 	off_t length, secsz;
632 	int idx, wblocks, wname, wmax;
633 
634 	if (geom_is_withered(gp))
635 		return;
636 	scheme = find_geomcfg(gp, "scheme");
637 	if (scheme == NULL)
638 		errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
639 	s = find_geomcfg(gp, "first");
640 	if (s == NULL)
641 		errx(EXIT_FAILURE, "Starting block not found for geom %s",
642 		    gp->lg_name);
643 	first = (off_t)strtoimax(s, NULL, 0);
644 	s = find_geomcfg(gp, "last");
645 	if (s == NULL)
646 		errx(EXIT_FAILURE, "Final block not found for geom %s",
647 		    gp->lg_name);
648 	last = (off_t)strtoimax(s, NULL, 0);
649 	wblocks = strlen(s);
650 	s = find_geomcfg(gp, "state");
651 	if (s == NULL)
652 		errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
653 	if (s != NULL && *s != 'C')
654 		s = NULL;
655 	wmax = strlen(gp->lg_name);
656 	if (show_providers) {
657 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
658 			wname = strlen(pp->lg_name);
659 			if (wname > wmax)
660 				wmax = wname;
661 		}
662 	}
663 	wname = wmax;
664 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
665 	secsz = pp->lg_sectorsize;
666 	printf("=>%*jd  %*jd  %*s  %s  (%s)%s\n",
667 	    wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
668 	    wname, gp->lg_name,
669 	    scheme, fmtsize(pp->lg_mediasize),
670 	    s ? " [CORRUPT]": "");
671 
672 	while ((pp = find_provider(gp, first)) != NULL) {
673 		s = find_provcfg(pp, "start");
674 		sector = (off_t)strtoimax(s, NULL, 0);
675 
676 		s = find_provcfg(pp, "end");
677 		end = (off_t)strtoimax(s, NULL, 0);
678 		length = end - sector + 1;
679 
680 		s = find_provcfg(pp, "index");
681 		idx = atoi(s);
682 		if (first < sector) {
683 			printf("  %*jd  %*jd  %*s  - free -  (%s)\n",
684 			    wblocks, (intmax_t)first, wblocks,
685 			    (intmax_t)(sector - first), wname, "",
686 			    fmtsize((sector - first) * secsz));
687 		}
688 		if (show_providers) {
689 			printf("  %*jd  %*jd  %*s  %s %s (%s)\n",
690 			    wblocks, (intmax_t)sector, wblocks,
691 			    (intmax_t)length, wname, pp->lg_name,
692 			    find_provcfg(pp, element), fmtattrib(pp),
693 			    fmtsize(pp->lg_mediasize));
694 		} else
695 			printf("  %*jd  %*jd  %*d  %s %s (%s)\n",
696 			    wblocks, (intmax_t)sector, wblocks,
697 			    (intmax_t)length, wname, idx,
698 			    find_provcfg(pp, element), fmtattrib(pp),
699 			    fmtsize(pp->lg_mediasize));
700 		first = end + 1;
701 	}
702 	if (first <= last) {
703 		length = last - first + 1;
704 		printf("  %*jd  %*jd  %*s  - free -  (%s)\n",
705 		    wblocks, (intmax_t)first, wblocks, (intmax_t)length,
706 		    wname, "",
707 		    fmtsize(length * secsz));
708 	}
709 	printf("\n");
710 }
711 
712 static int
713 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
714 {
715 
716 	if (!gctl_get_int(req, "%s", opt))
717 		return (0);
718 
719 	if (elt != NULL)
720 		errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
721 
722 	return (1);
723 }
724 
725 static void
726 gpart_show(struct gctl_req *req, unsigned int fl __unused)
727 {
728 	struct gmesh mesh;
729 	struct gclass *classp;
730 	struct ggeom *gp;
731 	const char *element, *name;
732 	int error, i, nargs, show_providers;
733 
734 	element = NULL;
735 	if (gpart_show_hasopt(req, "show_label", element))
736 		element = "label";
737 	if (gpart_show_hasopt(req, "show_rawtype", element))
738 		element = "rawtype";
739 	if (element == NULL)
740 		element = "type";
741 
742 	name = gctl_get_ascii(req, "class");
743 	if (name == NULL)
744 		abort();
745 	nargs = gctl_get_int(req, "nargs");
746 	if (nargs == 1) {
747 		error = geom_gettree_geom(&mesh, name,
748 		    gctl_get_ascii(req, "arg0"), 1);
749 	} else
750 		error = geom_gettree(&mesh);
751 	if (error != 0)
752 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
753 	classp = find_class(&mesh, name);
754 	if (classp == NULL) {
755 		geom_deletetree(&mesh);
756 		errx(EXIT_FAILURE, "Class %s not found.", name);
757 	}
758 	show_providers = gctl_get_int(req, "show_providers");
759 	if (nargs > 0) {
760 		for (i = 0; i < nargs; i++) {
761 			name = gctl_get_ascii(req, "arg%d", i);
762 			gp = find_geom(classp, name);
763 			if (gp != NULL)
764 				gpart_show_geom(gp, element, show_providers);
765 			else
766 				errx(EXIT_FAILURE, "No such geom: %s.", name);
767 		}
768 	} else {
769 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
770 			gpart_show_geom(gp, element, show_providers);
771 		}
772 	}
773 	geom_deletetree(&mesh);
774 }
775 
776 static void
777 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
778 {
779 	struct gmesh mesh;
780 	struct gclass *classp;
781 	struct gprovider *pp;
782 	struct ggeom *gp;
783 	const char *g, *s, *scheme;
784 	off_t sector, end;
785 	off_t length;
786 	int error, i, windex, wblocks, wtype;
787 
788 	if (gctl_get_int(req, "nargs") != 1)
789 		errx(EXIT_FAILURE, "Invalid number of arguments.");
790 	s = gctl_get_ascii(req, "class");
791 	if (s == NULL)
792 		abort();
793 	g = gctl_get_ascii(req, "arg0");
794 	if (g == NULL)
795 		abort();
796 	error = geom_gettree_geom(&mesh, s, g, 0);
797 	if (error != 0)
798 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
799 	classp = find_class(&mesh, s);
800 	if (classp == NULL) {
801 		geom_deletetree(&mesh);
802 		errx(EXIT_FAILURE, "Class %s not found.", s);
803 	}
804 	gp = find_geom(classp, g);
805 	if (gp == NULL)
806 		errx(EXIT_FAILURE, "No such geom: %s.", g);
807 	scheme = find_geomcfg(gp, "scheme");
808 	if (scheme == NULL)
809 		abort();
810 	s = find_geomcfg(gp, "last");
811 	if (s == NULL)
812 		abort();
813 	wblocks = strlen(s);
814 	wtype = 0;
815 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
816 		s = find_provcfg(pp, "type");
817 		i = strlen(s);
818 		if (i > wtype)
819 			wtype = i;
820 	}
821 	s = find_geomcfg(gp, "entries");
822 	if (s == NULL)
823 		abort();
824 	windex = strlen(s);
825 	printf("%s %s\n", scheme, s);
826 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
827 		s = find_provcfg(pp, "start");
828 		sector = (off_t)strtoimax(s, NULL, 0);
829 
830 		s = find_provcfg(pp, "end");
831 		end = (off_t)strtoimax(s, NULL, 0);
832 		length = end - sector + 1;
833 
834 		s = find_provcfg(pp, "label");
835 		printf("%-*s %*s %*jd %*jd %s %s\n",
836 		    windex, find_provcfg(pp, "index"),
837 		    wtype, find_provcfg(pp, "type"),
838 		    wblocks, (intmax_t)sector,
839 		    wblocks, (intmax_t)length,
840 		    (s != NULL) ? s: "", fmtattrib(pp));
841 	}
842 	geom_deletetree(&mesh);
843 }
844 
845 static int
846 skip_line(const char *p)
847 {
848 
849 	while (*p != '\0') {
850 		if (*p == '#')
851 			return (1);
852 		if (isspace(*p) == 0)
853 			return (0);
854 		p++;
855 	}
856 	return (1);
857 }
858 
859 static void
860 gpart_sighndl(int sig __unused)
861 {
862 	undo_restore = 1;
863 }
864 
865 static void
866 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
867 {
868 	struct gmesh mesh;
869 	struct gclass *classp;
870 	struct gctl_req *r;
871 	struct ggeom *gp;
872 	struct sigaction si_sa;
873 	const char *s, *flags, *errstr, *label;
874 	char **ap, *argv[6], line[BUFSIZ], *pline;
875 	int error, forced, i, l, nargs, created, rl;
876 	intmax_t n;
877 
878 	nargs = gctl_get_int(req, "nargs");
879 	if (nargs < 1)
880 		errx(EXIT_FAILURE, "Invalid number of arguments.");
881 
882 	forced = gctl_get_int(req, "force");
883 	flags = gctl_get_ascii(req, "flags");
884 	rl = gctl_get_int(req, "restore_labels");
885 	s = gctl_get_ascii(req, "class");
886 	if (s == NULL)
887 		abort();
888 	error = geom_gettree(&mesh);
889 	if (error != 0)
890 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
891 	classp = find_class(&mesh, s);
892 	if (classp == NULL) {
893 		geom_deletetree(&mesh);
894 		errx(EXIT_FAILURE, "Class %s not found.", s);
895 	}
896 
897 	sigemptyset(&si_sa.sa_mask);
898 	si_sa.sa_flags = 0;
899 	si_sa.sa_handler = gpart_sighndl;
900 	if (sigaction(SIGINT, &si_sa, 0) == -1)
901 		err(EXIT_FAILURE, "sigaction SIGINT");
902 
903 	if (forced) {
904 		/* destroy existent partition table before restore */
905 		for (i = 0; i < nargs; i++) {
906 			s = gctl_get_ascii(req, "arg%d", i);
907 			gp = find_geom(classp, s);
908 			if (gp != NULL) {
909 				r = gctl_get_handle();
910 				gctl_ro_param(r, "class", -1,
911 				    classp->lg_name);
912 				gctl_ro_param(r, "verb", -1, "destroy");
913 				gctl_ro_param(r, "flags", -1, "restore");
914 				gctl_ro_param(r, "force", sizeof(forced),
915 				    &forced);
916 				gctl_ro_param(r, "arg0", -1, s);
917 				errstr = gctl_issue(r);
918 				if (errstr != NULL && errstr[0] != '\0') {
919 					gpart_print_error(errstr);
920 					gctl_free(r);
921 					goto backout;
922 				}
923 				gctl_free(r);
924 			}
925 		}
926 	}
927 	created = 0;
928 	while (undo_restore == 0 &&
929 	    fgets(line, sizeof(line) - 1, stdin) != NULL) {
930 		/* Format of backup entries:
931 		 * <scheme name> <number of entries>
932 		 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
933 		 */
934 		pline = (char *)line;
935 		pline[strlen(line) - 1] = 0;
936 		if (skip_line(pline))
937 			continue;
938 		for (ap = argv;
939 		    (*ap = strsep(&pline, " \t")) != NULL;)
940 			if (**ap != '\0' && ++ap >= &argv[6])
941 				break;
942 		l = ap - &argv[0];
943 		label = pline = NULL;
944 		if (l == 1 || l == 2) { /* create table */
945 			if (created)
946 				errx(EXIT_FAILURE, "Incorrect backup format.");
947 			if (l == 2)
948 				n = strtoimax(argv[1], NULL, 0);
949 			for (i = 0; i < nargs; i++) {
950 				s = gctl_get_ascii(req, "arg%d", i);
951 				r = gctl_get_handle();
952 				gctl_ro_param(r, "class", -1,
953 				    classp->lg_name);
954 				gctl_ro_param(r, "verb", -1, "create");
955 				gctl_ro_param(r, "scheme", -1, argv[0]);
956 				if (l == 2)
957 					gctl_ro_param(r, "entries",
958 					    sizeof(n), &n);
959 				gctl_ro_param(r, "flags", -1, "restore");
960 				gctl_ro_param(r, "arg0", -1, s);
961 				errstr = gctl_issue(r);
962 				if (errstr != NULL && errstr[0] != '\0') {
963 					gpart_print_error(errstr);
964 					gctl_free(r);
965 					goto backout;
966 				}
967 				gctl_free(r);
968 			}
969 			created = 1;
970 			continue;
971 		} else if (l < 4 || created == 0)
972 			errx(EXIT_FAILURE, "Incorrect backup format.");
973 		else if (l == 5) {
974 			if (strchr(argv[4], '[') == NULL)
975 				label = argv[4];
976 			else
977 				pline = argv[4];
978 		} else if (l == 6) {
979 			label = argv[4];
980 			pline = argv[5];
981 		}
982 		/* Add partitions to each table */
983 		for (i = 0; i < nargs; i++) {
984 			s = gctl_get_ascii(req, "arg%d", i);
985 			r = gctl_get_handle();
986 			n = strtoimax(argv[0], NULL, 0);
987 			gctl_ro_param(r, "class", -1, classp->lg_name);
988 			gctl_ro_param(r, "verb", -1, "add");
989 			gctl_ro_param(r, "flags", -1, "restore");
990 			gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
991 			gctl_ro_param(r, "type", -1, argv[1]);
992 			gctl_ro_param(r, "start", -1, argv[2]);
993 			gctl_ro_param(r, "size", -1, argv[3]);
994 			if (rl != 0 && label != NULL)
995 				gctl_ro_param(r, "label", -1, argv[4]);
996 			gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
997 			gctl_ro_param(r, "arg0", -1, s);
998 			error = gpart_autofill(r);
999 			if (error != 0)
1000 				errc(EXIT_FAILURE, error, "autofill");
1001 			errstr = gctl_issue(r);
1002 			if (errstr != NULL && errstr[0] != '\0') {
1003 				gpart_print_error(errstr);
1004 				gctl_free(r);
1005 				goto backout;
1006 			}
1007 			gctl_free(r);
1008 		}
1009 		if (pline == NULL || *pline != '[')
1010 			continue;
1011 		/* set attributes */
1012 		pline++;
1013 		for (ap = argv;
1014 		    (*ap = strsep(&pline, ",]")) != NULL;)
1015 			if (**ap != '\0' && ++ap >= &argv[6])
1016 				break;
1017 		for (i = 0; i < nargs; i++) {
1018 			l = ap - &argv[0];
1019 			s = gctl_get_ascii(req, "arg%d", i);
1020 			while (l > 0) {
1021 				r = gctl_get_handle();
1022 				gctl_ro_param(r, "class", -1, classp->lg_name);
1023 				gctl_ro_param(r, "verb", -1, "set");
1024 				gctl_ro_param(r, "flags", -1, "restore");
1025 				gctl_ro_param(r, GPART_PARAM_INDEX,
1026 				    sizeof(n), &n);
1027 				gctl_ro_param(r, "attrib", -1, argv[--l]);
1028 				gctl_ro_param(r, "arg0", -1, s);
1029 				errstr = gctl_issue(r);
1030 				if (errstr != NULL && errstr[0] != '\0') {
1031 					gpart_print_error(errstr);
1032 					gctl_free(r);
1033 					goto backout;
1034 				}
1035 				gctl_free(r);
1036 			}
1037 		}
1038 	}
1039 	if (undo_restore)
1040 		goto backout;
1041 	/* commit changes if needed */
1042 	if (strchr(flags, 'C') != NULL) {
1043 		for (i = 0; i < nargs; i++) {
1044 			s = gctl_get_ascii(req, "arg%d", i);
1045 			r = gctl_get_handle();
1046 			gctl_ro_param(r, "class", -1, classp->lg_name);
1047 			gctl_ro_param(r, "verb", -1, "commit");
1048 			gctl_ro_param(r, "arg0", -1, s);
1049 			errstr = gctl_issue(r);
1050 			if (errstr != NULL && errstr[0] != '\0') {
1051 				gpart_print_error(errstr);
1052 				gctl_free(r);
1053 				goto backout;
1054 			}
1055 			gctl_free(r);
1056 		}
1057 	}
1058 	gctl_free(req);
1059 	geom_deletetree(&mesh);
1060 	exit(EXIT_SUCCESS);
1061 
1062 backout:
1063 	for (i = 0; i < nargs; i++) {
1064 		s = gctl_get_ascii(req, "arg%d", i);
1065 		r = gctl_get_handle();
1066 		gctl_ro_param(r, "class", -1, classp->lg_name);
1067 		gctl_ro_param(r, "verb", -1, "undo");
1068 		gctl_ro_param(r, "arg0", -1, s);
1069 		gctl_issue(r);
1070 		gctl_free(r);
1071 	}
1072 	gctl_free(req);
1073 	geom_deletetree(&mesh);
1074 	exit(EXIT_FAILURE);
1075 }
1076 
1077 static void *
1078 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1079 {
1080 	struct stat sb;
1081 	void *code;
1082 	int fd;
1083 
1084 	if (stat(bootfile, &sb) == -1)
1085 		err(EXIT_FAILURE, "%s", bootfile);
1086 	if (!S_ISREG(sb.st_mode))
1087 		errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1088 	if (sb.st_size == 0)
1089 		errx(EXIT_FAILURE, "%s: empty file", bootfile);
1090 	if (*size > 0 && sb.st_size > *size)
1091 		errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1092 		    *size);
1093 
1094 	*size = sb.st_size;
1095 
1096 	fd = open(bootfile, O_RDONLY);
1097 	if (fd == -1)
1098 		err(EXIT_FAILURE, "%s", bootfile);
1099 	code = malloc(*size);
1100 	if (code == NULL)
1101 		err(EXIT_FAILURE, NULL);
1102 	if (read(fd, code, *size) != *size)
1103 		err(EXIT_FAILURE, "%s", bootfile);
1104 	close(fd);
1105 
1106 	return (code);
1107 }
1108 
1109 static void
1110 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1111 {
1112 	char dsf[128];
1113 	struct gprovider *pp;
1114 	const char *s;
1115 	char *buf;
1116 	off_t bsize;
1117 	int fd;
1118 
1119 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1120 		s = find_provcfg(pp, "index");
1121 		if (s == NULL)
1122 			continue;
1123 		if (atoi(s) == idx)
1124 			break;
1125 	}
1126 
1127 	if (pp != NULL) {
1128 		snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1129 		if (pp->lg_mediasize < size)
1130 			errx(EXIT_FAILURE, "%s: not enough space", dsf);
1131 		fd = open(dsf, O_WRONLY);
1132 		if (fd == -1)
1133 			err(EXIT_FAILURE, "%s", dsf);
1134 		/*
1135 		 * When writing to a disk device, the write must be
1136 		 * sector aligned and not write to any partial sectors,
1137 		 * so round up the buffer size to the next sector and zero it.
1138 		 */
1139 		bsize = (size + pp->lg_sectorsize - 1) /
1140 		    pp->lg_sectorsize * pp->lg_sectorsize;
1141 		buf = calloc(1, bsize);
1142 		if (buf == NULL)
1143 			err(EXIT_FAILURE, "%s", dsf);
1144 		bcopy(code, buf, size);
1145 		if (write(fd, buf, bsize) != bsize)
1146 			err(EXIT_FAILURE, "%s", dsf);
1147 		free(buf);
1148 		close(fd);
1149 		printf("partcode written to %s\n", pp->lg_name);
1150 	} else
1151 		errx(EXIT_FAILURE, "invalid partition index");
1152 }
1153 
1154 static void
1155 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1156 {
1157 	char dsf[128];
1158 	struct gprovider *pp;
1159 	const char *s;
1160 	int installed, fd;
1161 
1162 	installed = 0;
1163 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1164 		s = find_provcfg(pp, "index");
1165 		if (s == NULL)
1166 			continue;
1167 		if (idx != 0 && atoi(s) != idx)
1168 			continue;
1169 		snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1170 		if (pp->lg_sectorsize != sizeof(struct vtoc8))
1171 			errx(EXIT_FAILURE, "%s: unexpected sector "
1172 			    "size (%d)\n", dsf, pp->lg_sectorsize);
1173 		if (pp->lg_mediasize < VTOC_BOOTSIZE)
1174 			continue;
1175 		fd = open(dsf, O_WRONLY);
1176 		if (fd == -1)
1177 			err(EXIT_FAILURE, "%s", dsf);
1178 		/*
1179 		 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1180 		 * order to avoid overwriting the label.
1181 		 */
1182 		if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1183 		    sizeof(struct vtoc8))
1184 			err(EXIT_FAILURE, "%s", dsf);
1185 		if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1186 		    VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1187 		    sizeof(struct vtoc8))
1188 			err(EXIT_FAILURE, "%s", dsf);
1189 		installed++;
1190 		close(fd);
1191 		if (idx != 0 && atoi(s) == idx)
1192 			break;
1193 	}
1194 	if (installed == 0)
1195 		errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1196 	else
1197 		printf("partcode written to %s\n",
1198 		    idx != 0 ? pp->lg_name: gp->lg_name);
1199 }
1200 
1201 static void
1202 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1203 {
1204 	struct gmesh mesh;
1205 	struct gclass *classp;
1206 	struct ggeom *gp;
1207 	const char *g, *s;
1208 	void *bootcode, *partcode;
1209 	size_t bootsize, partsize;
1210 	int error, idx, vtoc8;
1211 
1212 	if (gctl_get_int(req, "nargs") != 1)
1213 		errx(EXIT_FAILURE, "Invalid number of arguments.");
1214 
1215 	if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1216 		s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1217 		bootsize = 800 * 1024;		/* Arbitrary limit. */
1218 		bootcode = gpart_bootfile_read(s, &bootsize);
1219 		error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1220 		    bootcode);
1221 		if (error)
1222 			errc(EXIT_FAILURE, error, "internal error");
1223 	} else
1224 		bootcode = NULL;
1225 
1226 	if (!gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1227 		if (bootcode == NULL)
1228 			errx(EXIT_FAILURE, "neither -b nor -p specified");
1229 		if (gctl_has_param(req, GPART_PARAM_INDEX))
1230 			errx(EXIT_FAILURE, "-i is only valid with -p");
1231 		goto nopartcode;
1232 	}
1233 
1234 	s = gctl_get_ascii(req, "class");
1235 	if (s == NULL)
1236 		abort();
1237 	g = gctl_get_ascii(req, "arg0");
1238 	if (g == NULL)
1239 		abort();
1240 	error = geom_gettree_geom(&mesh, s, g, 0);
1241 	if (error != 0)
1242 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1243 	classp = find_class(&mesh, s);
1244 	if (classp == NULL) {
1245 		geom_deletetree(&mesh);
1246 		errx(EXIT_FAILURE, "Class %s not found.", s);
1247 	}
1248 	gp = find_geom(classp, g);
1249 	if (gp == NULL)
1250 		errx(EXIT_FAILURE, "No such geom: %s.", g);
1251 	s = find_geomcfg(gp, "scheme");
1252 	if (s == NULL)
1253 		errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1254 	if (strcmp(s, "VTOC8") == 0)
1255 		vtoc8 = 1;
1256 	else
1257 		vtoc8 = 0;
1258 
1259 	if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1260 		idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1261 		if (idx < 1)
1262 			errx(EXIT_FAILURE, "invalid partition index");
1263 		error = gctl_delete_param(req, GPART_PARAM_INDEX);
1264 		if (error)
1265 			errc(EXIT_FAILURE, error, "internal error");
1266 	} else
1267 		idx = 0;
1268 
1269 	if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1270 		s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1271 		if (vtoc8 != 0)
1272 			partsize = VTOC_BOOTSIZE;
1273 		else
1274 			partsize = 1024 * 1024;		/* Arbitrary limit. */
1275 		partcode = gpart_bootfile_read(s, &partsize);
1276 		error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1277 		if (error)
1278 			errc(EXIT_FAILURE, error, "internal error");
1279 		if (vtoc8 == 0) {
1280 			if (idx == 0)
1281 				errx(EXIT_FAILURE, "missing -i option");
1282 			gpart_write_partcode(gp, idx, partcode, partsize);
1283 		} else {
1284 			if (partsize != VTOC_BOOTSIZE)
1285 				errx(EXIT_FAILURE, "invalid bootcode");
1286 			gpart_write_partcode_vtoc8(gp, idx, partcode);
1287 		}
1288 		free(partcode);
1289 	}
1290 
1291 	geom_deletetree(&mesh);
1292 
1293 nopartcode:
1294 	if (bootcode != NULL)
1295 		gpart_issue(req, fl);
1296 }
1297 
1298 static void
1299 gpart_print_error(const char *errstr)
1300 {
1301 	char *errmsg;
1302 	int error;
1303 
1304 	error = strtol(errstr, &errmsg, 0);
1305 	if (errmsg != errstr) {
1306 		while (errmsg[0] == ' ')
1307 			errmsg++;
1308 		if (errmsg[0] != '\0')
1309 			warnc(error, "%s", errmsg);
1310 		else
1311 			warnc(error, NULL);
1312 	} else
1313 		warnx("%s", errmsg);
1314 }
1315 
1316 static _Noreturn void
1317 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1318 {
1319 	char buf[4096];
1320 	const char *errstr;
1321 	int error, status;
1322 
1323 	if (gctl_get_int(req, "nargs") != 1)
1324 		errx(EXIT_FAILURE, "Invalid number of arguments.");
1325 	(void)gctl_delete_param(req, "nargs");
1326 
1327 	/* autofill parameters (if applicable). */
1328 	error = gpart_autofill(req);
1329 	if (error) {
1330 		warnc(error, "autofill");
1331 		status = EXIT_FAILURE;
1332 		goto done;
1333 	}
1334 
1335 	buf[0] = '\0';
1336 	gctl_add_param(req, "output", sizeof(buf), buf,
1337 	    GCTL_PARAM_WR | GCTL_PARAM_ASCII);
1338 	errstr = gctl_issue(req);
1339 	if (errstr == NULL || errstr[0] == '\0') {
1340 		if (buf[0] != '\0')
1341 			printf("%s", buf);
1342 		status = EXIT_SUCCESS;
1343 		goto done;
1344 	}
1345 
1346 	gpart_print_error(errstr);
1347 	status = EXIT_FAILURE;
1348 
1349  done:
1350 	gctl_free(req);
1351 	exit(status);
1352 }
1353