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/linker_set.h>
31 #include <sys/queue.h>
33 #include <sys/types.h>
52 #define LONGOPT_FORMATS 0x01000001
53 #define LONGOPT_SCHEMES 0x01000002
54 #define LONGOPT_VERSION 0x01000003
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 },
63 struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
76 print_formats(int usage)
78 struct mkimg_format *f, **f_iter;
82 fprintf(stderr, " formats:\n");
83 SET_FOREACH(f_iter, formats) {
85 fprintf(stderr, "\t%s\t- %s\n", f->name,
90 SET_FOREACH(f_iter, formats) {
92 printf("%s%s", sep, f->name);
100 print_schemes(int usage)
102 struct mkimg_scheme *s, **s_iter;
106 fprintf(stderr, " schemes:\n");
107 SET_FOREACH(s_iter, schemes) {
109 fprintf(stderr, "\t%s\t- %s\n", s->name,
114 SET_FOREACH(s_iter, schemes) {
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-b <file>\t- file containing boot code\n");
150 fprintf(stderr, "\t-f <format>\n");
151 fprintf(stderr, "\t-o <file>\t- file to write image into\n");
152 fprintf(stderr, "\t-p <partition>\n");
153 fprintf(stderr, "\t-s <scheme>\n");
154 fprintf(stderr, "\t-v\t\t- increase verbosity\n");
155 fprintf(stderr, "\t-y\t\t- [developers] enable unit test\n");
156 fprintf(stderr, "\t-H <num>\t- number of heads to simulate\n");
157 fprintf(stderr, "\t-P <num>\t- physical sector size\n");
158 fprintf(stderr, "\t-S <num>\t- logical sector size\n");
159 fprintf(stderr, "\t-T <num>\t- number of tracks to simulate\n");
165 fprintf(stderr, " partition specification:\n");
166 fprintf(stderr, "\t<t>[/<l>]::<size>\t- empty partition of given "
168 fprintf(stderr, "\t<t>[/<l>]:=<file>\t- partition content and size "
169 "are determined\n\t\t\t\t by the named file\n");
170 fprintf(stderr, "\t<t>[/<l>]:-<cmd>\t- partition content and size "
171 "are taken from\n\t\t\t\t the output of the command to run\n");
172 fprintf(stderr, "\t-\t\t\t- unused partition entry\n");
173 fprintf(stderr, "\t where:\n");
174 fprintf(stderr, "\t\t<t>\t- scheme neutral partition type\n");
175 fprintf(stderr, "\t\t<l>\t- optional scheme-dependent partition "
182 parse_number(u_int *valp, u_int min, u_int max, const char *arg)
186 if (expand_number(arg, &val) == -1)
188 if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
198 return (((nr & (nr - 1)) == 0) ? 1 : 0);
202 * A partition specification has the following format:
203 * <type> ':' <kind> <contents>
205 * type the partition type alias
206 * kind the interpretation of the contents specification
207 * ':' contents holds the size of an empty partition
208 * '=' contents holds the name of a file to read
209 * '-' contents holds a command to run; the output of
210 * which is the contents of the partition.
211 * contents the specification of a partition's contents
213 * A specification that is a single dash indicates an unused partition
217 parse_part(const char *spec)
224 if (strcmp(spec, "-") == 0) {
229 part = calloc(1, sizeof(struct part));
233 sep = strchr(spec, ':');
238 len = sep - spec + 1;
243 part->alias = malloc(len);
244 if (part->alias == NULL) {
248 strlcpy(part->alias, spec, len);
253 part->kind = PART_KIND_SIZE;
256 part->kind = PART_KIND_FILE;
259 part->kind = PART_KIND_PIPE;
267 part->contents = strdup(spec);
268 if (part->contents == NULL) {
274 sep = strchr(spec, '/');
277 if (strlen(part->alias) == 0 || strlen(sep) == 0) {
281 part->label = strdup(sep);
282 if (part->label == NULL) {
288 part->index = nparts;
289 STAILQ_INSERT_TAIL(&partlist, part, link);
294 if (part->alias != NULL)
300 #if defined(SPARSE_WRITE)
302 sparse_write(int fd, const void *ptr, size_t sz)
311 p = memchr(buf, 0, sz);
313 len = (p != NULL) ? (size_t)(p - buf) : sz;
315 len = (len + secsz - 1) & ~(secsz - 1);
318 wr = write(fd, buf, len);
322 while (len < sz && *p++ == '\0')
328 ofs = lseek(fd, len, SEEK_CUR);
336 p = memchr(buf, 0, sz);
340 #endif /* SPARSE_WRITE */
343 mkimg_uuid(struct uuid *uuid)
345 static uint8_t gen[sizeof(struct uuid)];
353 for (i = 0; i < sizeof(gen); i++)
355 memcpy(uuid, gen, sizeof(uuid_t));
367 /* First check partition information */
368 STAILQ_FOREACH(part, &partlist, link) {
369 error = scheme_check_part(part);
371 errc(EX_DATAERR, error, "partition %d", part->index+1);
374 block = scheme_metadata(SCHEME_META_IMG_START, 0);
375 STAILQ_FOREACH(part, &partlist, link) {
376 block = scheme_metadata(SCHEME_META_PART_BEFORE, block);
378 fprintf(stderr, "partition %d: starting block %llu "
379 "... ", part->index + 1, (long long)block);
381 switch (part->kind) {
383 if (expand_number(part->contents, &bytesize) == -1)
387 fd = open(part->contents, O_RDONLY, 0);
389 error = image_copyin(block, fd, &bytesize);
395 fp = popen(part->contents, "r");
398 error = image_copyin(block, fd, &bytesize);
405 errc(EX_IOERR, error, "partition %d", part->index + 1);
406 part->size = (bytesize + secsz - 1) / secsz;
408 bytesize = part->size * secsz;
409 fprintf(stderr, "size %llu bytes (%llu blocks)\n",
410 (long long)bytesize, (long long)part->size);
412 block = scheme_metadata(SCHEME_META_PART_AFTER,
413 part->block + part->size);
416 block = scheme_metadata(SCHEME_META_IMG_END, block);
417 error = image_set_size(block);
419 error = format_resize(block);
421 errc(EX_IOERR, error, "image sizing");
422 block = image_get_size();
423 ncyls = block / (nsecs * nheads);
424 error = (scheme_write(block));
426 errc(EX_IOERR, error, "writing metadata");
430 main(int argc, char *argv[])
436 outfd = 1; /* Write to stdout by default */
437 while ((c = getopt_long(argc, argv, "b:f:o:p:s:vyH:P:S:T:",
438 longopts, NULL)) != -1) {
440 case 'b': /* BOOT CODE */
442 usage("multiple bootcode given");
443 bcfd = open(optarg, O_RDONLY, 0);
445 err(EX_UNAVAILABLE, "%s", optarg);
447 case 'f': /* OUTPUT FORMAT */
448 if (format_selected() != NULL)
449 usage("multiple formats given");
450 error = format_select(optarg);
452 errc(EX_DATAERR, error, "format");
454 case 'o': /* OUTPUT FILE */
456 usage("multiple output files given");
457 outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
458 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
460 err(EX_CANTCREAT, "%s", optarg);
462 case 'p': /* PARTITION */
463 error = parse_part(optarg);
465 errc(EX_DATAERR, error, "partition");
467 case 's': /* SCHEME */
468 if (scheme_selected() != NULL)
469 usage("multiple schemes given");
470 error = scheme_select(optarg);
472 errc(EX_DATAERR, error, "scheme");
480 case 'H': /* GEOMETRY: HEADS */
481 error = parse_number(&nheads, 1, 255, optarg);
483 errc(EX_DATAERR, error, "number of heads");
485 case 'P': /* GEOMETRY: PHYSICAL SECTOR SIZE */
486 error = parse_number(&blksz, 512, INT_MAX+1U, optarg);
487 if (error == 0 && !pwr_of_two(blksz))
490 errc(EX_DATAERR, error, "physical sector size");
492 case 'S': /* GEOMETRY: LOGICAL SECTOR SIZE */
493 error = parse_number(&secsz, 512, INT_MAX+1U, optarg);
494 if (error == 0 && !pwr_of_two(secsz))
497 errc(EX_DATAERR, error, "logical sector size");
499 case 'T': /* GEOMETRY: TRACK SIZE */
500 error = parse_number(&nsecs, 1, 63, optarg);
502 errc(EX_DATAERR, error, "track size");
504 case LONGOPT_FORMATS:
508 case LONGOPT_SCHEMES:
512 case LONGOPT_VERSION:
517 usage("unknown option");
522 usage("trailing arguments");
523 if (scheme_selected() == NULL)
526 usage("no partitions");
530 errx(EX_DATAERR, "the physical block size cannot "
531 "be smaller than the sector size");
535 if (secsz > scheme_max_secsz())
536 errx(EX_DATAERR, "maximum sector size supported is %u; "
537 "size specified is %u", scheme_max_secsz(), secsz);
539 if (nparts > scheme_max_parts())
540 errx(EX_DATAERR, "%d partitions supported; %d given",
541 scheme_max_parts(), nparts);
543 if (format_selected() == NULL)
544 format_select("raw");
547 error = scheme_bootcode(bcfd);
550 errc(EX_DATAERR, error, "boot code");
554 fprintf(stderr, "Logical sector size: %u\n", secsz);
555 fprintf(stderr, "Physical block size: %u\n", blksz);
556 fprintf(stderr, "Sectors per track: %u\n", nsecs);
557 fprintf(stderr, "Number of heads: %u\n", nheads);
559 fprintf(stderr, "Partitioning scheme: %s\n",
560 scheme_selected()->name);
561 fprintf(stderr, "Output file format: %s\n",
562 format_selected()->name);
566 error = image_init();
568 errc(EX_OSERR, error, "cannot initialize");
574 fprintf(stderr, "Number of cylinders: %u\n", ncyls);
577 error = format_write(outfd);
579 errc(EX_IOERR, error, "writing image");