xref: /src/usr.bin/mkimg/mkimg.c (revision 971696b22f7acc8c45600bb56b972340e9b912e8) !
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013,2014 Juniper Networks, Inc.
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 AUTHOR 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 AUTHOR 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/param.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <getopt.h>
35 #include <libutil.h>
36 #include <limits.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sysexits.h>
43 #include <unistd.h>
44 
45 #include "image.h"
46 #include "format.h"
47 #include "mkimg.h"
48 #include "scheme.h"
49 
50 #define	LONGOPT_FORMATS		0x01000001
51 #define	LONGOPT_SCHEMES		0x01000002
52 #define	LONGOPT_VERSION		0x01000003
53 #define	LONGOPT_CAPACITY	0x01000004
54 
55 static struct option longopts[] = {
56 	{ "formats", no_argument, NULL, LONGOPT_FORMATS },
57 	{ "schemes", no_argument, NULL, LONGOPT_SCHEMES },
58 	{ "version", no_argument, NULL, LONGOPT_VERSION },
59 	{ "capacity", required_argument, NULL, LONGOPT_CAPACITY },
60 	{ NULL, 0, NULL, 0 }
61 };
62 
63 static uint64_t min_capacity = 0;
64 static uint64_t max_capacity = 0;
65 
66 /* Fixed timestamp for reproducible builds. */
67 time_t timestamp = (time_t)-1;
68 
69 struct partlisthead partlist = TAILQ_HEAD_INITIALIZER(partlist);
70 u_int nparts = 0;
71 
72 u_int unit_testing;
73 u_int verbose;
74 
75 u_int ncyls = 0;
76 u_int nheads = 1;
77 u_int nsecs = 1;
78 u_int secsz = 512;
79 u_int blksz = 0;
80 uint32_t active_partition = 0;
81 
82 static void
print_formats(int usage)83 print_formats(int usage)
84 {
85 	struct mkimg_format *f;
86 	const char *sep;
87 
88 	if (usage) {
89 		fprintf(stderr, "    formats:\n");
90 		f = NULL;
91 		while ((f = format_iterate(f)) != NULL) {
92 			fprintf(stderr, "\t%s\t-  %s\n", f->name,
93 			    f->description);
94 		}
95 	} else {
96 		sep = "";
97 		f = NULL;
98 		while ((f = format_iterate(f)) != NULL) {
99 			printf("%s%s", sep, f->name);
100 			sep = " ";
101 		}
102 		putchar('\n');
103 	}
104 }
105 
106 static void
print_schemes(int usage)107 print_schemes(int usage)
108 {
109 	struct mkimg_scheme *s;
110 	const char *sep;
111 
112 	if (usage) {
113 		fprintf(stderr, "    schemes:\n");
114 		s = NULL;
115 		while ((s = scheme_iterate(s)) != NULL) {
116 			fprintf(stderr, "\t%s\t-  %s\n", s->name,
117 			    s->description);
118 		}
119 	} else {
120 		sep = "";
121 		s = NULL;
122 		while ((s = scheme_iterate(s)) != NULL) {
123 			printf("%s%s", sep, s->name);
124 			sep = " ";
125 		}
126 		putchar('\n');
127 	}
128 }
129 
130 static void
print_version(void)131 print_version(void)
132 {
133 	u_int width;
134 
135 #ifdef __LP64__
136 	width = 64;
137 #else
138 	width = 32;
139 #endif
140 	printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
141 }
142 
143 static void
usage(const char * why)144 usage(const char *why)
145 {
146 
147 	if (why != NULL) {
148 		warnx("error: %s", why);
149 		fputc('\n', stderr);
150 	}
151 	fprintf(stderr, "usage: %s <options>\n", getprogname());
152 
153 	fprintf(stderr, "    options:\n");
154 	fprintf(stderr, "\t--formats\t-  list image formats\n");
155 	fprintf(stderr, "\t--schemes\t-  list partition schemes\n");
156 	fprintf(stderr, "\t--version\t-  show version information\n");
157 	fprintf(stderr, "\t--capacity\t-  minimum and maximum capacity (in bytes)\n");
158 	fputc('\n', stderr);
159 	fprintf(stderr, "\t-a <num>\t-  mark num'th partition as active\n");
160 	fprintf(stderr, "\t-b <file>\t-  file containing boot code\n");
161 	fprintf(stderr, "\t-c <num>\t-  minimum capacity (in bytes) of the disk\n");
162 	fprintf(stderr, "\t-C <num>\t-  maximum capacity (in bytes) of the disk\n");
163 	fprintf(stderr, "\t-f <format>\n");
164 	fprintf(stderr, "\t-h\t\t-  show this usage information\n");
165 	fprintf(stderr, "\t-o <file>\t-  file to write image into\n");
166 	fprintf(stderr, "\t-p <partition>\n");
167 	fprintf(stderr, "\t-s <scheme>\n");
168 	fprintf(stderr, "\t-t <num>\t-  set timestamp (seconds since epoch)\n");
169 	fprintf(stderr, "\t-v\t\t-  increase verbosity\n");
170 	fprintf(stderr, "\t-y\t\t-  [developers] enable unit test\n");
171 	fprintf(stderr, "\t-H <num>\t-  number of heads to simulate\n");
172 	fprintf(stderr, "\t-P <num>\t-  physical sector size\n");
173 	fprintf(stderr, "\t-S <num>\t-  logical sector size\n");
174 	fprintf(stderr, "\t-T <num>\t-  number of tracks to simulate\n");
175 	fputc('\n', stderr);
176 	print_formats(1);
177 	fputc('\n', stderr);
178 	print_schemes(1);
179 	fputc('\n', stderr);
180 	fprintf(stderr, "    partition specification:\n");
181 	fprintf(stderr, "\t<type>[/<label>]::<size>[:[+]<offset>]\t-  "
182 	    "empty partition of given size and\n\t\t\t\t\t"
183 	    "   optional relative or absolute offset\n");
184 	fprintf(stderr, "\t<type>[/<label>]:=<file>[:[+]offset]\t-  partition "
185 	    "content and size are\n\t\t\t\t\t"
186 	    "   determined by the named file and\n"
187 	    "\t\t\t\t\t   optional relative or absolute offset\n");
188 	fprintf(stderr, "\t<type>[/<label>]:-<cmd>\t\t-  partition content and size "
189 	    "are taken\n\t\t\t\t\t   from the output of the command to run\n");
190 	fprintf(stderr, "\t-\t\t\t\t-  unused partition entry\n");
191 	fprintf(stderr, "\t    where:\n");
192 	fprintf(stderr, "\t\t<type>\t-  scheme neutral partition type\n");
193 	fprintf(stderr, "\t\t<label>\t-  optional scheme-dependent partition "
194 	    "label\n");
195 
196 	exit(EX_USAGE);
197 }
198 
199 static int
parse_uint32(uint32_t * valp,uint32_t min,uint32_t max,const char * arg)200 parse_uint32(uint32_t *valp, uint32_t min, uint32_t max, const char *arg)
201 {
202 	uint64_t val;
203 
204 	if (expand_number(arg, &val) == -1)
205 		return (errno);
206 	if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
207 		return (EINVAL);
208 	*valp = (uint32_t)val;
209 	return (0);
210 }
211 
212 static int
parse_uint64(uint64_t * valp,uint64_t min,uint64_t max,const char * arg)213 parse_uint64(uint64_t *valp, uint64_t min, uint64_t max, const char *arg)
214 {
215 	uint64_t val;
216 
217 	if (expand_number(arg, &val) == -1)
218 		return (errno);
219 	if (val < min || val > max)
220 		return (EINVAL);
221 	*valp = val;
222 	return (0);
223 }
224 
225 static int
pwr_of_two(u_int nr)226 pwr_of_two(u_int nr)
227 {
228 
229 	return (((nr & (nr - 1)) == 0) ? 1 : 0);
230 }
231 
232 /*
233  * A partition specification has the following format:
234  *	<type> ':' <kind> <contents>
235  * where:
236  *	type	  the partition type alias
237  *	kind	  the interpretation of the contents specification
238  *		  ':'   contents holds the size of an empty partition
239  *		  '='   contents holds the name of a file to read
240  *		  '-'   contents holds a command to run; the output of
241  *			which is the contents of the partition.
242  *	contents  the specification of a partition's contents
243  *
244  * A specification that is a single dash indicates an unused partition
245  * entry.
246  */
247 static int
parse_part(const char * spec)248 parse_part(const char *spec)
249 {
250 	struct part *part;
251 	char *sep;
252 	size_t len;
253 	int error;
254 
255 	if (strcmp(spec, "-") == 0) {
256 		nparts++;
257 		return (0);
258 	}
259 
260 	part = calloc(1, sizeof(struct part));
261 	if (part == NULL)
262 		return (ENOMEM);
263 
264 	sep = strchr(spec, ':');
265 	if (sep == NULL) {
266 		error = EINVAL;
267 		goto errout;
268 	}
269 	len = sep - spec + 1;
270 	if (len < 2) {
271 		error = EINVAL;
272 		goto errout;
273 	}
274 	part->alias = malloc(len);
275 	if (part->alias == NULL) {
276 		error = ENOMEM;
277 		goto errout;
278 	}
279 	strlcpy(part->alias, spec, len);
280 	spec = sep + 1;
281 
282 	switch (*spec) {
283 	case ':':
284 		part->kind = PART_KIND_SIZE;
285 		break;
286 	case '=':
287 		part->kind = PART_KIND_FILE;
288 		break;
289 	case '-':
290 		part->kind = PART_KIND_PIPE;
291 		break;
292 	default:
293 		error = EINVAL;
294 		goto errout;
295 	}
296 	spec++;
297 
298 	part->contents = strdup(spec);
299 	if (part->contents == NULL) {
300 		error = ENOMEM;
301 		goto errout;
302 	}
303 
304 	spec = part->alias;
305 	sep = strchr(spec, '/');
306 	if (sep != NULL) {
307 		*sep++ = '\0';
308 		if (strlen(part->alias) == 0 || strlen(sep) == 0) {
309 			error = EINVAL;
310 			goto errout;
311 		}
312 		part->label = strdup(sep);
313 		if (part->label == NULL) {
314 			error = ENOMEM;
315 			goto errout;
316 		}
317 	}
318 
319 	part->index = nparts;
320 	TAILQ_INSERT_TAIL(&partlist, part, link);
321 	nparts++;
322 	return (0);
323 
324  errout:
325 	if (part->alias != NULL)
326 		free(part->alias);
327 	free(part);
328 	return (error);
329 }
330 
331 #if defined(SPARSE_WRITE)
332 ssize_t
sparse_write(int fd,const void * ptr,size_t sz)333 sparse_write(int fd, const void *ptr, size_t sz)
334 {
335 	const char *buf, *p;
336 	off_t ofs;
337 	size_t len;
338 	ssize_t wr, wrsz;
339 
340 	buf = ptr;
341 	wrsz = 0;
342 	p = memchr(buf, 0, sz);
343 	while (sz > 0) {
344 		len = (p != NULL) ? (size_t)(p - buf) : sz;
345 		if (len > 0) {
346 			len = (len + secsz - 1) & ~(secsz - 1);
347 			if (len > sz)
348 				len = sz;
349 			wr = write(fd, buf, len);
350 			if (wr < 0)
351 				return (-1);
352 		} else {
353 			while (len < sz && *p++ == '\0')
354 				len++;
355 			if (len < sz)
356 				len &= ~(secsz - 1);
357 			if (len == 0)
358 				continue;
359 			ofs = lseek(fd, len, SEEK_CUR);
360 			if (ofs < 0)
361 				return (-1);
362 			wr = len;
363 		}
364 		buf += wr;
365 		sz -= wr;
366 		wrsz += wr;
367 		p = memchr(buf, 0, sz);
368 	}
369 	return (wrsz);
370 }
371 #endif /* SPARSE_WRITE */
372 
373 void
mkimg_chs(lba_t lba,u_int maxcyl,u_int * cylp,u_int * hdp,u_int * secp)374 mkimg_chs(lba_t lba, u_int maxcyl, u_int *cylp, u_int *hdp, u_int *secp)
375 {
376 	u_int hd, sec;
377 
378 	*cylp = *hdp = *secp = ~0U;
379 	if (nsecs == 1 || nheads == 1)
380 		return;
381 
382 	sec = lba % nsecs + 1;
383 	lba /= nsecs;
384 	hd = lba % nheads;
385 	lba /= nheads;
386 	if (lba > maxcyl)
387 		return;
388 
389 	*cylp = lba;
390 	*hdp = hd;
391 	*secp = sec;
392 }
393 
394 static int
capacity_resize(lba_t end)395 capacity_resize(lba_t end)
396 {
397 	lba_t min_capsz, max_capsz;
398 
399 	min_capsz = (min_capacity + secsz - 1) / secsz;
400 	max_capsz = (max_capacity + secsz - 1) / secsz;
401 
402 	if (max_capsz != 0 && end > max_capsz)
403 		return (ENOSPC);
404 	if (end >= min_capsz)
405 		return (0);
406 
407 	return (image_set_size(min_capsz));
408 }
409 
410 static void
mkimg_validate(void)411 mkimg_validate(void)
412 {
413 	struct part *part, *part2;
414 	lba_t start, end, start2, end2;
415 	int i, j;
416 
417 	i = 0;
418 
419 	TAILQ_FOREACH(part, &partlist, link) {
420 		start = part->block;
421 		end = part->block + part->size;
422 		j = i + 1;
423 		part2 = TAILQ_NEXT(part, link);
424 		if (part2 == NULL)
425 			break;
426 
427 		TAILQ_FOREACH_FROM(part2, &partlist, link) {
428 			start2 = part2->block;
429 			end2 = part2->block + part2->size;
430 
431 			if ((start >= start2 && start < end2) ||
432 			    (end > start2 && end <= end2)) {
433 				errx(1, "partition %d overlaps partition %d",
434 				    i, j);
435 			}
436 
437 			j++;
438 		}
439 
440 		i++;
441 	}
442 }
443 
444 static void
mkimg(void)445 mkimg(void)
446 {
447 	FILE *fp;
448 	struct part *part;
449 	lba_t block, blkoffset;
450 	uint64_t bytesize, byteoffset;
451 	char *size, *offset;
452 	bool abs_offset;
453 	int error, fd;
454 
455 	/* First check partition information */
456 	TAILQ_FOREACH(part, &partlist, link) {
457 		error = scheme_check_part(part);
458 		if (error)
459 			errc(EX_DATAERR, error, "partition %d", part->index+1);
460 	}
461 
462 	block = scheme_metadata(SCHEME_META_IMG_START, 0);
463 	abs_offset = false;
464 	TAILQ_FOREACH(part, &partlist, link) {
465 		byteoffset = blkoffset = 0;
466 		abs_offset = false;
467 
468 		/* Look for an offset. Set size too if we can. */
469 		switch (part->kind) {
470 		case PART_KIND_SIZE:
471 		case PART_KIND_FILE:
472 			offset = part->contents;
473 			size = strsep(&offset, ":");
474 			if (part->kind == PART_KIND_SIZE &&
475 			    expand_number(size, &bytesize) == -1)
476 				error = errno;
477 			if (offset != NULL) {
478 				if (*offset != '+')
479 					abs_offset = true;
480 				else
481 					offset++;
482 				if (expand_number(offset, &byteoffset) == -1)
483 					error = errno;
484 			}
485 			break;
486 		}
487 
488 		/* Work out exactly where the partition starts. */
489 		blkoffset = (byteoffset + secsz - 1) / secsz;
490 		if (abs_offset)
491 			block = scheme_metadata(SCHEME_META_PART_ABSOLUTE,
492 			    blkoffset);
493 		else
494 			block = scheme_metadata(SCHEME_META_PART_BEFORE,
495 			    block + blkoffset);
496 		part->block = block;
497 
498 		if (verbose)
499 			fprintf(stderr, "partition %d: starting block %llu "
500 			    "... ", part->index + 1, (long long)part->block);
501 
502 		/* Pull in partition contents, set size if we haven't yet. */
503 		switch (part->kind) {
504 		case PART_KIND_FILE:
505 			fd = open(part->contents, O_RDONLY, 0);
506 			if (fd != -1) {
507 				error = image_copyin(block, fd, &bytesize);
508 				close(fd);
509 			} else
510 				error = errno;
511 			break;
512 		case PART_KIND_PIPE:
513 			fp = popen(part->contents, "r");
514 			if (fp != NULL) {
515 				fd = fileno(fp);
516 				error = image_copyin(block, fd, &bytesize);
517 				pclose(fp);
518 			} else
519 				error = errno;
520 			break;
521 		}
522 		if (error)
523 			errc(EX_IOERR, error, "partition %d", part->index + 1);
524 		part->size = (bytesize + secsz - 1) / secsz;
525 		if (verbose) {
526 			bytesize = part->size * secsz;
527 			fprintf(stderr, "size %llu bytes (%llu blocks)\n",
528 			     (long long)bytesize, (long long)part->size);
529 			if (abs_offset) {
530 				fprintf(stderr,
531 				    "    location %llu bytes (%llu blocks)\n",
532 				    (long long)byteoffset,
533 				    (long long)blkoffset);
534 			} else if (blkoffset > 0) {
535 				fprintf(stderr,
536 				    "    offset %llu bytes (%llu blocks)\n",
537 				    (long long)byteoffset,
538 				    (long long)blkoffset);
539 			}
540 		}
541 		block = scheme_metadata(SCHEME_META_PART_AFTER,
542 		    part->block + part->size);
543 	}
544 
545 	mkimg_validate();
546 
547 	block = scheme_metadata(SCHEME_META_IMG_END, block);
548 	error = image_set_size(block);
549 	if (!error) {
550 		error = capacity_resize(block);
551 		block = image_get_size();
552 	}
553 	if (!error) {
554 		error = format_resize(block);
555 		block = image_get_size();
556 	}
557 	if (error)
558 		errc(EX_IOERR, error, "image sizing");
559 	ncyls = block / (nsecs * nheads);
560 	error = scheme_write(block);
561 	if (error)
562 		errc(EX_IOERR, error, "writing metadata");
563 }
564 
565 int
main(int argc,char * argv[])566 main(int argc, char *argv[])
567 {
568 	const char *format_name;
569 	int bcfd, outfd;
570 	int c, error;
571 
572 	bcfd = -1;
573 	outfd = 1;	/* Write to stdout by default */
574 	while ((c = getopt_long(argc, argv, "a:b:c:C:f:ho:p:s:t:vyH:P:S:T:",
575 	    longopts, NULL)) != -1) {
576 		switch (c) {
577 		case 'a':	/* ACTIVE PARTITION, if supported */
578 			error = parse_uint32(&active_partition, 1, 100, optarg);
579 			if (error)
580 				errc(EX_DATAERR, error, "Partition ordinal");
581 			break;
582 		case 'b':	/* BOOT CODE */
583 			if (bcfd != -1)
584 				usage("multiple bootcode given");
585 			bcfd = open(optarg, O_RDONLY, 0);
586 			if (bcfd == -1)
587 				err(EX_UNAVAILABLE, "%s", optarg);
588 			break;
589 		case 'c':	/* MINIMUM CAPACITY */
590 			error = parse_uint64(&min_capacity, 1, INT64_MAX, optarg);
591 			if (error)
592 				errc(EX_DATAERR, error, "minimum capacity in bytes");
593 			break;
594 		case 'C':	/* MAXIMUM CAPACITY */
595 			error = parse_uint64(&max_capacity, 1, INT64_MAX, optarg);
596 			if (error)
597 				errc(EX_DATAERR, error, "maximum capacity in bytes");
598 			break;
599 		case 'f':	/* OUTPUT FORMAT */
600 			if (format_selected() != NULL)
601 				usage("multiple formats given");
602 			error = format_select(optarg);
603 			if (error)
604 				errc(EX_DATAERR, error, "format");
605 			break;
606 		case 'h':	/* HELP */
607 			usage(NULL);
608 			break;
609 		case 'o':	/* OUTPUT FILE */
610 			if (outfd != 1)
611 				usage("multiple output files given");
612 			outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
613 			    S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
614 			if (outfd == -1)
615 				err(EX_CANTCREAT, "%s", optarg);
616 			break;
617 		case 'p':	/* PARTITION */
618 			error = parse_part(optarg);
619 			if (error)
620 				errc(EX_DATAERR, error, "partition");
621 			break;
622 		case 's':	/* SCHEME */
623 			if (scheme_selected() != NULL)
624 				usage("multiple schemes given");
625 			error = scheme_select(optarg);
626 			if (error)
627 				errc(EX_DATAERR, error, "scheme");
628 			break;
629 		case 't': {
630 			char *ep;
631 			long long val;
632 
633 			errno = 0;
634 			val = strtoll(optarg, &ep, 0);
635 			if (ep == optarg || *ep != '\0')
636 				errno = EINVAL;
637 			if (errno != 0)
638 				errc(EX_DATAERR, errno, "timestamp");
639 			timestamp = (time_t)val;
640 			break;
641 		}
642 		case 'y':
643 			unit_testing++;
644 			break;
645 		case 'v':
646 			verbose++;
647 			break;
648 		case 'H':	/* GEOMETRY: HEADS */
649 			error = parse_uint32(&nheads, 1, 255, optarg);
650 			if (error)
651 				errc(EX_DATAERR, error, "number of heads");
652 			break;
653 		case 'P':	/* GEOMETRY: PHYSICAL SECTOR SIZE */
654 			error = parse_uint32(&blksz, 512, INT_MAX+1U, optarg);
655 			if (error == 0 && !pwr_of_two(blksz))
656 				error = EINVAL;
657 			if (error)
658 				errc(EX_DATAERR, error, "physical sector size");
659 			break;
660 		case 'S':	/* GEOMETRY: LOGICAL SECTOR SIZE */
661 			error = parse_uint32(&secsz, 512, INT_MAX+1U, optarg);
662 			if (error == 0 && !pwr_of_two(secsz))
663 				error = EINVAL;
664 			if (error)
665 				errc(EX_DATAERR, error, "logical sector size");
666 			break;
667 		case 'T':	/* GEOMETRY: TRACK SIZE */
668 			error = parse_uint32(&nsecs, 1, 63, optarg);
669 			if (error)
670 				errc(EX_DATAERR, error, "track size");
671 			break;
672 		case LONGOPT_FORMATS:
673 			print_formats(0);
674 			exit(EX_OK);
675 			/*NOTREACHED*/
676 		case LONGOPT_SCHEMES:
677 			print_schemes(0);
678 			exit(EX_OK);
679 			/*NOTREACHED*/
680 		case LONGOPT_VERSION:
681 			print_version();
682 			exit(EX_OK);
683 			/*NOTREACHED*/
684 		case LONGOPT_CAPACITY:
685 			error = parse_uint64(&min_capacity, 1, INT64_MAX, optarg);
686 			if (error)
687 				errc(EX_DATAERR, error, "capacity in bytes");
688 			max_capacity = min_capacity;
689 			break;
690 		default:
691 			usage("unknown option");
692 		}
693 	}
694 
695 	if (argc > optind)
696 		usage("trailing arguments");
697 	if (scheme_selected() == NULL && nparts > 0)
698 		usage("no scheme");
699 	if (nparts == 0 && min_capacity == 0)
700 		usage("no partitions");
701 	if (max_capacity != 0 && min_capacity > max_capacity)
702 		usage("minimum capacity cannot be larger than the maximum one");
703 
704 	if (secsz > blksz) {
705 		if (blksz != 0)
706 			errx(EX_DATAERR, "the physical block size cannot "
707 			    "be smaller than the sector size");
708 		blksz = secsz;
709 	}
710 
711 	if (secsz > scheme_max_secsz())
712 		errx(EX_DATAERR, "maximum sector size supported is %u; "
713 		    "size specified is %u", scheme_max_secsz(), secsz);
714 
715 	if (nparts > scheme_max_parts())
716 		errx(EX_DATAERR, "%d partitions supported; %d given",
717 		    scheme_max_parts(), nparts);
718 
719 	if (format_selected() == NULL)
720 		format_select("raw");
721 
722 	if (bcfd != -1) {
723 		error = scheme_bootcode(bcfd);
724 		close(bcfd);
725 		if (error)
726 			errc(EX_DATAERR, error, "boot code");
727 	}
728 
729 	format_name = format_selected()->name;
730 	if (verbose) {
731 		fprintf(stderr, "Logical sector size: %u\n", secsz);
732 		fprintf(stderr, "Physical block size: %u\n", blksz);
733 		fprintf(stderr, "Sectors per track:   %u\n", nsecs);
734 		fprintf(stderr, "Number of heads:     %u\n", nheads);
735 		fputc('\n', stderr);
736 		if (scheme_selected())
737 			fprintf(stderr, "Partitioning scheme: %s\n",
738 			    scheme_selected()->name);
739 		fprintf(stderr, "Output file format:  %s\n",
740 		    format_name);
741 		fputc('\n', stderr);
742 	}
743 
744 #if defined(SPARSE_WRITE)
745 	/*
746 	 * sparse_write() fails if output is not seekable so fail early
747 	 * not wasting some load unless output format is raw
748 	 */
749 	if (strcmp("raw", format_name) &&
750 	    lseek(outfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
751 		errx(EX_USAGE, "%s: output must be seekable", format_name);
752 #endif
753 
754 	error = image_init();
755 	if (error)
756 		errc(EX_OSERR, error, "cannot initialize");
757 
758 	mkimg();
759 
760 	if (verbose) {
761 		fputc('\n', stderr);
762 		fprintf(stderr, "Number of cylinders: %u\n", ncyls);
763 	}
764 
765 	error = format_write(outfd);
766 	if (error)
767 		errc(EX_IOERR, error, "writing image");
768 
769 	return (0);
770 }
771