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$");
49 #define LONGOPT_FORMATS 0x01000001
50 #define LONGOPT_SCHEMES 0x01000002
51 #define LONGOPT_VERSION 0x01000003
53 static struct option longopts[] = {
54 { "formats", no_argument, NULL, LONGOPT_FORMATS },
55 { "schemes", no_argument, NULL, LONGOPT_SCHEMES },
56 { "version", no_argument, NULL, LONGOPT_VERSION },
60 static uint64_t capacity;
62 struct partlisthead partlist = TAILQ_HEAD_INITIALIZER(partlist);
73 uint32_t active_partition = 0;
76 print_formats(int usage)
78 struct mkimg_format *f;
82 fprintf(stderr, " formats:\n");
84 while ((f = format_iterate(f)) != NULL) {
85 fprintf(stderr, "\t%s\t- %s\n", f->name,
91 while ((f = format_iterate(f)) != NULL) {
92 printf("%s%s", sep, f->name);
100 print_schemes(int usage)
102 struct mkimg_scheme *s;
106 fprintf(stderr, " schemes:\n");
108 while ((s = scheme_iterate(s)) != NULL) {
109 fprintf(stderr, "\t%s\t- %s\n", s->name,
115 while ((s = scheme_iterate(s)) != NULL) {
116 printf("%s%s", sep, s->name);
133 printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
137 usage(const char *why)
140 warnx("error: %s", why);
142 fprintf(stderr, "usage: %s <options>\n", getprogname());
144 fprintf(stderr, " options:\n");
145 fprintf(stderr, "\t--formats\t- list image formats\n");
146 fprintf(stderr, "\t--schemes\t- list partition schemes\n");
147 fprintf(stderr, "\t--version\t- show version information\n");
149 fprintf(stderr, "\t-a <num>\t- mark num'th partion as active\n");
150 fprintf(stderr, "\t-b <file>\t- file containing boot code\n");
151 fprintf(stderr, "\t-c <num>\t- capacity (in bytes) of the disk\n");
152 fprintf(stderr, "\t-f <format>\n");
153 fprintf(stderr, "\t-o <file>\t- file to write image into\n");
154 fprintf(stderr, "\t-p <partition>\n");
155 fprintf(stderr, "\t-s <scheme>\n");
156 fprintf(stderr, "\t-v\t\t- increase verbosity\n");
157 fprintf(stderr, "\t-y\t\t- [developers] enable unit test\n");
158 fprintf(stderr, "\t-H <num>\t- number of heads to simulate\n");
159 fprintf(stderr, "\t-P <num>\t- physical sector size\n");
160 fprintf(stderr, "\t-S <num>\t- logical sector size\n");
161 fprintf(stderr, "\t-T <num>\t- number of tracks to simulate\n");
167 fprintf(stderr, " partition specification:\n");
168 fprintf(stderr, "\t<t>[/<l>]::<size>\t- empty partition of given "
170 fprintf(stderr, "\t<t>[/<l>]:=<file>\t- partition content and size "
171 "are determined\n\t\t\t\t by the named file\n");
172 fprintf(stderr, "\t<t>[/<l>]:-<cmd>\t- partition content and size "
173 "are taken from\n\t\t\t\t the output of the command to run\n");
174 fprintf(stderr, "\t-\t\t\t- unused partition entry\n");
175 fprintf(stderr, "\t where:\n");
176 fprintf(stderr, "\t\t<t>\t- scheme neutral partition type\n");
177 fprintf(stderr, "\t\t<l>\t- optional scheme-dependent partition "
184 parse_uint32(uint32_t *valp, uint32_t min, uint32_t max, const char *arg)
188 if (expand_number(arg, &val) == -1)
190 if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
192 *valp = (uint32_t)val;
197 parse_uint64(uint64_t *valp, uint64_t min, uint64_t max, const char *arg)
201 if (expand_number(arg, &val) == -1)
203 if (val < min || val > max)
213 return (((nr & (nr - 1)) == 0) ? 1 : 0);
217 * A partition specification has the following format:
218 * <type> ':' <kind> <contents>
220 * type the partition type alias
221 * kind the interpretation of the contents specification
222 * ':' contents holds the size of an empty partition
223 * '=' contents holds the name of a file to read
224 * '-' contents holds a command to run; the output of
225 * which is the contents of the partition.
226 * contents the specification of a partition's contents
228 * A specification that is a single dash indicates an unused partition
232 parse_part(const char *spec)
239 if (strcmp(spec, "-") == 0) {
244 part = calloc(1, sizeof(struct part));
248 sep = strchr(spec, ':');
253 len = sep - spec + 1;
258 part->alias = malloc(len);
259 if (part->alias == NULL) {
263 strlcpy(part->alias, spec, len);
268 part->kind = PART_KIND_SIZE;
271 part->kind = PART_KIND_FILE;
274 part->kind = PART_KIND_PIPE;
282 part->contents = strdup(spec);
283 if (part->contents == NULL) {
289 sep = strchr(spec, '/');
292 if (strlen(part->alias) == 0 || strlen(sep) == 0) {
296 part->label = strdup(sep);
297 if (part->label == NULL) {
303 part->index = nparts;
304 TAILQ_INSERT_TAIL(&partlist, part, link);
309 if (part->alias != NULL)
315 #if defined(SPARSE_WRITE)
317 sparse_write(int fd, const void *ptr, size_t sz)
326 p = memchr(buf, 0, sz);
328 len = (p != NULL) ? (size_t)(p - buf) : sz;
330 len = (len + secsz - 1) & ~(secsz - 1);
333 wr = write(fd, buf, len);
337 while (len < sz && *p++ == '\0')
343 ofs = lseek(fd, len, SEEK_CUR);
351 p = memchr(buf, 0, sz);
355 #endif /* SPARSE_WRITE */
358 mkimg_chs(lba_t lba, u_int maxcyl, u_int *cylp, u_int *hdp, u_int *secp)
362 *cylp = *hdp = *secp = ~0U;
363 if (nsecs == 1 || nheads == 1)
366 sec = lba % nsecs + 1;
379 capacity_resize(lba_t end)
383 capsz = (capacity + secsz - 1) / secsz;
386 return (image_set_size(capsz));
398 /* First check partition information */
399 TAILQ_FOREACH(part, &partlist, link) {
400 error = scheme_check_part(part);
402 errc(EX_DATAERR, error, "partition %d", part->index+1);
405 block = scheme_metadata(SCHEME_META_IMG_START, 0);
406 TAILQ_FOREACH(part, &partlist, link) {
407 block = scheme_metadata(SCHEME_META_PART_BEFORE, block);
409 fprintf(stderr, "partition %d: starting block %llu "
410 "... ", part->index + 1, (long long)block);
412 switch (part->kind) {
414 if (expand_number(part->contents, &bytesize) == -1)
418 fd = open(part->contents, O_RDONLY, 0);
420 error = image_copyin(block, fd, &bytesize);
426 fp = popen(part->contents, "r");
429 error = image_copyin(block, fd, &bytesize);
436 errc(EX_IOERR, error, "partition %d", part->index + 1);
437 part->size = (bytesize + secsz - 1) / secsz;
439 bytesize = part->size * secsz;
440 fprintf(stderr, "size %llu bytes (%llu blocks)\n",
441 (long long)bytesize, (long long)part->size);
443 block = scheme_metadata(SCHEME_META_PART_AFTER,
444 part->block + part->size);
447 block = scheme_metadata(SCHEME_META_IMG_END, block);
448 error = image_set_size(block);
450 error = capacity_resize(block);
451 block = image_get_size();
454 error = format_resize(block);
455 block = image_get_size();
458 errc(EX_IOERR, error, "image sizing");
459 ncyls = block / (nsecs * nheads);
460 error = scheme_write(block);
462 errc(EX_IOERR, error, "writing metadata");
466 main(int argc, char *argv[])
472 outfd = 1; /* Write to stdout by default */
473 while ((c = getopt_long(argc, argv, "a:b:c:f:o:p:s:vyH:P:S:T:",
474 longopts, NULL)) != -1) {
476 case 'a': /* ACTIVE PARTITION, if supported */
477 error = parse_uint32(&active_partition, 1, 100, optarg);
479 errc(EX_DATAERR, error, "Partition ordinal");
481 case 'b': /* BOOT CODE */
483 usage("multiple bootcode given");
484 bcfd = open(optarg, O_RDONLY, 0);
486 err(EX_UNAVAILABLE, "%s", optarg);
488 case 'c': /* CAPACITY */
489 error = parse_uint64(&capacity, 1, INT64_MAX, optarg);
491 errc(EX_DATAERR, error, "capacity in bytes");
493 case 'f': /* OUTPUT FORMAT */
494 if (format_selected() != NULL)
495 usage("multiple formats given");
496 error = format_select(optarg);
498 errc(EX_DATAERR, error, "format");
500 case 'o': /* OUTPUT FILE */
502 usage("multiple output files given");
503 outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
504 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
506 err(EX_CANTCREAT, "%s", optarg);
508 case 'p': /* PARTITION */
509 error = parse_part(optarg);
511 errc(EX_DATAERR, error, "partition");
513 case 's': /* SCHEME */
514 if (scheme_selected() != NULL)
515 usage("multiple schemes given");
516 error = scheme_select(optarg);
518 errc(EX_DATAERR, error, "scheme");
526 case 'H': /* GEOMETRY: HEADS */
527 error = parse_uint32(&nheads, 1, 255, optarg);
529 errc(EX_DATAERR, error, "number of heads");
531 case 'P': /* GEOMETRY: PHYSICAL SECTOR SIZE */
532 error = parse_uint32(&blksz, 512, INT_MAX+1U, optarg);
533 if (error == 0 && !pwr_of_two(blksz))
536 errc(EX_DATAERR, error, "physical sector size");
538 case 'S': /* GEOMETRY: LOGICAL SECTOR SIZE */
539 error = parse_uint32(&secsz, 512, INT_MAX+1U, optarg);
540 if (error == 0 && !pwr_of_two(secsz))
543 errc(EX_DATAERR, error, "logical sector size");
545 case 'T': /* GEOMETRY: TRACK SIZE */
546 error = parse_uint32(&nsecs, 1, 63, optarg);
548 errc(EX_DATAERR, error, "track size");
550 case LONGOPT_FORMATS:
554 case LONGOPT_SCHEMES:
558 case LONGOPT_VERSION:
563 usage("unknown option");
568 usage("trailing arguments");
569 if (scheme_selected() == NULL && nparts > 0)
571 if (nparts == 0 && capacity == 0)
572 usage("no partitions");
576 errx(EX_DATAERR, "the physical block size cannot "
577 "be smaller than the sector size");
581 if (secsz > scheme_max_secsz())
582 errx(EX_DATAERR, "maximum sector size supported is %u; "
583 "size specified is %u", scheme_max_secsz(), secsz);
585 if (nparts > scheme_max_parts())
586 errx(EX_DATAERR, "%d partitions supported; %d given",
587 scheme_max_parts(), nparts);
589 if (format_selected() == NULL)
590 format_select("raw");
593 error = scheme_bootcode(bcfd);
596 errc(EX_DATAERR, error, "boot code");
600 fprintf(stderr, "Logical sector size: %u\n", secsz);
601 fprintf(stderr, "Physical block size: %u\n", blksz);
602 fprintf(stderr, "Sectors per track: %u\n", nsecs);
603 fprintf(stderr, "Number of heads: %u\n", nheads);
605 if (scheme_selected())
606 fprintf(stderr, "Partitioning scheme: %s\n",
607 scheme_selected()->name);
608 fprintf(stderr, "Output file format: %s\n",
609 format_selected()->name);
613 error = image_init();
615 errc(EX_OSERR, error, "cannot initialize");
621 fprintf(stderr, "Number of cylinders: %u\n", ncyls);
624 error = format_write(outfd);
626 errc(EX_IOERR, error, "writing image");