2 * Copyright (c) 2013,2014 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
51 #define LONGOPT_FORMATS 0x01000001
52 #define LONGOPT_SCHEMES 0x01000002
53 #define LONGOPT_VERSION 0x01000003
54 #define LONGOPT_CAPACITY 0x01000004
56 static struct option longopts[] = {
57 { "formats", no_argument, NULL, LONGOPT_FORMATS },
58 { "schemes", no_argument, NULL, LONGOPT_SCHEMES },
59 { "version", no_argument, NULL, LONGOPT_VERSION },
60 { "capacity", required_argument, NULL, LONGOPT_CAPACITY },
64 static uint64_t min_capacity = 0;
65 static uint64_t max_capacity = 0;
67 struct partlisthead partlist = TAILQ_HEAD_INITIALIZER(partlist);
78 uint32_t active_partition = 0;
81 print_formats(int usage)
83 struct mkimg_format *f;
87 fprintf(stderr, " formats:\n");
89 while ((f = format_iterate(f)) != NULL) {
90 fprintf(stderr, "\t%s\t- %s\n", f->name,
96 while ((f = format_iterate(f)) != NULL) {
97 printf("%s%s", sep, f->name);
105 print_schemes(int usage)
107 struct mkimg_scheme *s;
111 fprintf(stderr, " schemes:\n");
113 while ((s = scheme_iterate(s)) != NULL) {
114 fprintf(stderr, "\t%s\t- %s\n", s->name,
120 while ((s = scheme_iterate(s)) != NULL) {
121 printf("%s%s", sep, s->name);
138 printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
142 usage(const char *why)
145 warnx("error: %s", why);
147 fprintf(stderr, "usage: %s <options>\n", getprogname());
149 fprintf(stderr, " options:\n");
150 fprintf(stderr, "\t--formats\t- list image formats\n");
151 fprintf(stderr, "\t--schemes\t- list partition schemes\n");
152 fprintf(stderr, "\t--version\t- show version information\n");
154 fprintf(stderr, "\t-a <num>\t- mark num'th partion as active\n");
155 fprintf(stderr, "\t-b <file>\t- file containing boot code\n");
156 fprintf(stderr, "\t-c <num>\t- minimum capacity (in bytes) of the disk\n");
157 fprintf(stderr, "\t-C <num>\t- maximum capacity (in bytes) of the disk\n");
158 fprintf(stderr, "\t-f <format>\n");
159 fprintf(stderr, "\t-o <file>\t- file to write image into\n");
160 fprintf(stderr, "\t-p <partition>\n");
161 fprintf(stderr, "\t-s <scheme>\n");
162 fprintf(stderr, "\t-v\t\t- increase verbosity\n");
163 fprintf(stderr, "\t-y\t\t- [developers] enable unit test\n");
164 fprintf(stderr, "\t-H <num>\t- number of heads to simulate\n");
165 fprintf(stderr, "\t-P <num>\t- physical sector size\n");
166 fprintf(stderr, "\t-S <num>\t- logical sector size\n");
167 fprintf(stderr, "\t-T <num>\t- number of tracks to simulate\n");
173 fprintf(stderr, " partition specification:\n");
174 fprintf(stderr, "\t<t>[/<l>]::<size>[:[+]<offset>]\t- "
175 "empty partition of given size and\n\t\t\t\t\t"
176 " optional relative or absolute offset\n");
177 fprintf(stderr, "\t<t>[/<l>]:=<file>\t\t- partition content and size "
178 "are\n\t\t\t\t\t determined by the named file\n");
179 fprintf(stderr, "\t<t>[/<l>]:-<cmd>\t\t- partition content and size "
180 "are taken\n\t\t\t\t\t from the output of the command to run\n");
181 fprintf(stderr, "\t-\t\t\t\t- unused partition entry\n");
182 fprintf(stderr, "\t where:\n");
183 fprintf(stderr, "\t\t<t>\t- scheme neutral partition type\n");
184 fprintf(stderr, "\t\t<l>\t- optional scheme-dependent partition "
191 parse_uint32(uint32_t *valp, uint32_t min, uint32_t max, const char *arg)
195 if (expand_number(arg, &val) == -1)
197 if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
199 *valp = (uint32_t)val;
204 parse_uint64(uint64_t *valp, uint64_t min, uint64_t max, const char *arg)
208 if (expand_number(arg, &val) == -1)
210 if (val < min || val > max)
220 return (((nr & (nr - 1)) == 0) ? 1 : 0);
224 * A partition specification has the following format:
225 * <type> ':' <kind> <contents>
227 * type the partition type alias
228 * kind the interpretation of the contents specification
229 * ':' contents holds the size of an empty partition
230 * '=' contents holds the name of a file to read
231 * '-' contents holds a command to run; the output of
232 * which is the contents of the partition.
233 * contents the specification of a partition's contents
235 * A specification that is a single dash indicates an unused partition
239 parse_part(const char *spec)
246 if (strcmp(spec, "-") == 0) {
251 part = calloc(1, sizeof(struct part));
255 sep = strchr(spec, ':');
260 len = sep - spec + 1;
265 part->alias = malloc(len);
266 if (part->alias == NULL) {
270 strlcpy(part->alias, spec, len);
275 part->kind = PART_KIND_SIZE;
278 part->kind = PART_KIND_FILE;
281 part->kind = PART_KIND_PIPE;
289 part->contents = strdup(spec);
290 if (part->contents == NULL) {
296 sep = strchr(spec, '/');
299 if (strlen(part->alias) == 0 || strlen(sep) == 0) {
303 part->label = strdup(sep);
304 if (part->label == NULL) {
310 part->index = nparts;
311 TAILQ_INSERT_TAIL(&partlist, part, link);
316 if (part->alias != NULL)
322 #if defined(SPARSE_WRITE)
324 sparse_write(int fd, const void *ptr, size_t sz)
333 p = memchr(buf, 0, sz);
335 len = (p != NULL) ? (size_t)(p - buf) : sz;
337 len = (len + secsz - 1) & ~(secsz - 1);
340 wr = write(fd, buf, len);
344 while (len < sz && *p++ == '\0')
350 ofs = lseek(fd, len, SEEK_CUR);
358 p = memchr(buf, 0, sz);
362 #endif /* SPARSE_WRITE */
365 mkimg_chs(lba_t lba, u_int maxcyl, u_int *cylp, u_int *hdp, u_int *secp)
369 *cylp = *hdp = *secp = ~0U;
370 if (nsecs == 1 || nheads == 1)
373 sec = lba % nsecs + 1;
386 capacity_resize(lba_t end)
388 lba_t min_capsz, max_capsz;
390 min_capsz = (min_capacity + secsz - 1) / secsz;
391 max_capsz = (max_capacity + secsz - 1) / secsz;
393 if (max_capsz != 0 && end > max_capsz)
395 if (end >= min_capsz)
398 return (image_set_size(min_capsz));
404 struct part *part, *part2;
405 lba_t start, end, start2, end2;
410 TAILQ_FOREACH(part, &partlist, link) {
412 end = part->block + part->size;
414 part2 = TAILQ_NEXT(part, link);
418 TAILQ_FOREACH_FROM(part2, &partlist, link) {
419 start2 = part2->block;
420 end2 = part2->block + part2->size;
422 if ((start >= start2 && start < end2) ||
423 (end > start2 && end <= end2)) {
424 errx(1, "partition %d overlaps partition %d",
440 lba_t block, blkoffset;
441 off_t bytesize, byteoffset;
446 /* First check partition information */
447 TAILQ_FOREACH(part, &partlist, link) {
448 error = scheme_check_part(part);
450 errc(EX_DATAERR, error, "partition %d", part->index+1);
453 block = scheme_metadata(SCHEME_META_IMG_START, 0);
455 TAILQ_FOREACH(part, &partlist, link) {
456 byteoffset = blkoffset = 0;
459 /* Look for an offset. Set size too if we can. */
460 switch (part->kind) {
462 offset = part->contents;
463 size = strsep(&offset, ":");
464 if (expand_number(size, &bytesize) == -1)
466 if (offset != NULL) {
471 if (expand_number(offset, &byteoffset) == -1)
477 /* Work out exactly where the partition starts. */
478 blkoffset = (byteoffset + secsz - 1) / secsz;
480 part->block = scheme_metadata(SCHEME_META_PART_ABSOLUTE,
483 block = scheme_metadata(SCHEME_META_PART_BEFORE,
489 fprintf(stderr, "partition %d: starting block %llu "
490 "... ", part->index + 1, (long long)part->block);
492 /* Pull in partition contents, set size if we haven't yet. */
493 switch (part->kind) {
495 fd = open(part->contents, O_RDONLY, 0);
497 error = image_copyin(block, fd, &bytesize);
503 fp = popen(part->contents, "r");
506 error = image_copyin(block, fd, &bytesize);
513 errc(EX_IOERR, error, "partition %d", part->index + 1);
514 part->size = (bytesize + secsz - 1) / secsz;
516 bytesize = part->size * secsz;
517 fprintf(stderr, "size %llu bytes (%llu blocks)\n",
518 (long long)bytesize, (long long)part->size);
521 " location %llu bytes (%llu blocks)\n",
522 (long long)byteoffset,
523 (long long)blkoffset);
524 } else if (blkoffset > 0) {
526 " offset %llu bytes (%llu blocks)\n",
527 (long long)byteoffset,
528 (long long)blkoffset);
532 block = scheme_metadata(SCHEME_META_PART_AFTER,
533 part->block + part->size);
539 block = scheme_metadata(SCHEME_META_IMG_END, block);
540 error = image_set_size(block);
542 error = capacity_resize(block);
543 block = image_get_size();
546 error = format_resize(block);
547 block = image_get_size();
550 errc(EX_IOERR, error, "image sizing");
551 ncyls = block / (nsecs * nheads);
552 error = scheme_write(block);
554 errc(EX_IOERR, error, "writing metadata");
558 main(int argc, char *argv[])
564 outfd = 1; /* Write to stdout by default */
565 while ((c = getopt_long(argc, argv, "a:b:c:C:f:o:p:s:vyH:P:S:T:",
566 longopts, NULL)) != -1) {
568 case 'a': /* ACTIVE PARTITION, if supported */
569 error = parse_uint32(&active_partition, 1, 100, optarg);
571 errc(EX_DATAERR, error, "Partition ordinal");
573 case 'b': /* BOOT CODE */
575 usage("multiple bootcode given");
576 bcfd = open(optarg, O_RDONLY, 0);
578 err(EX_UNAVAILABLE, "%s", optarg);
580 case 'c': /* MINIMUM CAPACITY */
581 error = parse_uint64(&min_capacity, 1, INT64_MAX, optarg);
583 errc(EX_DATAERR, error, "minimum capacity in bytes");
585 case 'C': /* MAXIMUM CAPACITY */
586 error = parse_uint64(&max_capacity, 1, INT64_MAX, optarg);
588 errc(EX_DATAERR, error, "maximum capacity in bytes");
590 case 'f': /* OUTPUT FORMAT */
591 if (format_selected() != NULL)
592 usage("multiple formats given");
593 error = format_select(optarg);
595 errc(EX_DATAERR, error, "format");
597 case 'o': /* OUTPUT FILE */
599 usage("multiple output files given");
600 outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
601 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
603 err(EX_CANTCREAT, "%s", optarg);
605 case 'p': /* PARTITION */
606 error = parse_part(optarg);
608 errc(EX_DATAERR, error, "partition");
610 case 's': /* SCHEME */
611 if (scheme_selected() != NULL)
612 usage("multiple schemes given");
613 error = scheme_select(optarg);
615 errc(EX_DATAERR, error, "scheme");
623 case 'H': /* GEOMETRY: HEADS */
624 error = parse_uint32(&nheads, 1, 255, optarg);
626 errc(EX_DATAERR, error, "number of heads");
628 case 'P': /* GEOMETRY: PHYSICAL SECTOR SIZE */
629 error = parse_uint32(&blksz, 512, INT_MAX+1U, optarg);
630 if (error == 0 && !pwr_of_two(blksz))
633 errc(EX_DATAERR, error, "physical sector size");
635 case 'S': /* GEOMETRY: LOGICAL SECTOR SIZE */
636 error = parse_uint32(&secsz, 512, INT_MAX+1U, optarg);
637 if (error == 0 && !pwr_of_two(secsz))
640 errc(EX_DATAERR, error, "logical sector size");
642 case 'T': /* GEOMETRY: TRACK SIZE */
643 error = parse_uint32(&nsecs, 1, 63, optarg);
645 errc(EX_DATAERR, error, "track size");
647 case LONGOPT_FORMATS:
651 case LONGOPT_SCHEMES:
655 case LONGOPT_VERSION:
659 case LONGOPT_CAPACITY:
660 error = parse_uint64(&min_capacity, 1, INT64_MAX, optarg);
662 errc(EX_DATAERR, error, "capacity in bytes");
663 max_capacity = min_capacity;
666 usage("unknown option");
671 usage("trailing arguments");
672 if (scheme_selected() == NULL && nparts > 0)
674 if (nparts == 0 && min_capacity == 0)
675 usage("no partitions");
676 if (max_capacity != 0 && min_capacity > max_capacity)
677 usage("minimum capacity cannot be larger than the maximum one");
681 errx(EX_DATAERR, "the physical block size cannot "
682 "be smaller than the sector size");
686 if (secsz > scheme_max_secsz())
687 errx(EX_DATAERR, "maximum sector size supported is %u; "
688 "size specified is %u", scheme_max_secsz(), secsz);
690 if (nparts > scheme_max_parts())
691 errx(EX_DATAERR, "%d partitions supported; %d given",
692 scheme_max_parts(), nparts);
694 if (format_selected() == NULL)
695 format_select("raw");
698 error = scheme_bootcode(bcfd);
701 errc(EX_DATAERR, error, "boot code");
705 fprintf(stderr, "Logical sector size: %u\n", secsz);
706 fprintf(stderr, "Physical block size: %u\n", blksz);
707 fprintf(stderr, "Sectors per track: %u\n", nsecs);
708 fprintf(stderr, "Number of heads: %u\n", nheads);
710 if (scheme_selected())
711 fprintf(stderr, "Partitioning scheme: %s\n",
712 scheme_selected()->name);
713 fprintf(stderr, "Output file format: %s\n",
714 format_selected()->name);
718 error = image_init();
720 errc(EX_OSERR, error, "cannot initialize");
726 fprintf(stderr, "Number of cylinders: %u\n", ncyls);
729 error = format_write(outfd);
731 errc(EX_IOERR, error, "writing image");