mkimg.c revision 272774
1/*- 2 * Copyright (c) 2013,2014 Juniper Networks, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/mkimg.c 272774 2014-10-08 22:06:38Z marcel $"); 29 30#include <sys/linker_set.h> 31#include <sys/queue.h> 32#include <sys/stat.h> 33#include <sys/types.h> 34#include <sys/uuid.h> 35#include <errno.h> 36#include <err.h> 37#include <fcntl.h> 38#include <getopt.h> 39#include <libutil.h> 40#include <limits.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <sysexits.h> 45#include <unistd.h> 46 47#include "image.h" 48#include "format.h" 49#include "mkimg.h" 50#include "scheme.h" 51 52#define LONGOPT_FORMATS 0x01000001 53#define LONGOPT_SCHEMES 0x01000002 54#define LONGOPT_VERSION 0x01000003 55 56static 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 { NULL, 0, NULL, 0 } 61}; 62 63struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist); 64u_int nparts = 0; 65 66u_int unit_testing; 67u_int verbose; 68 69u_int ncyls = 0; 70u_int nheads = 1; 71u_int nsecs = 1; 72u_int secsz = 512; 73u_int blksz = 0; 74 75static void 76print_formats(int usage) 77{ 78 struct mkimg_format *f, **f_iter; 79 const char *sep; 80 81 if (usage) { 82 fprintf(stderr, " formats:\n"); 83 SET_FOREACH(f_iter, formats) { 84 f = *f_iter; 85 fprintf(stderr, "\t%s\t- %s\n", f->name, 86 f->description); 87 } 88 } else { 89 sep = ""; 90 SET_FOREACH(f_iter, formats) { 91 f = *f_iter; 92 printf("%s%s", sep, f->name); 93 sep = " "; 94 } 95 putchar('\n'); 96 } 97} 98 99static void 100print_schemes(int usage) 101{ 102 struct mkimg_scheme *s, **s_iter; 103 const char *sep; 104 105 if (usage) { 106 fprintf(stderr, " schemes:\n"); 107 SET_FOREACH(s_iter, schemes) { 108 s = *s_iter; 109 fprintf(stderr, "\t%s\t- %s\n", s->name, 110 s->description); 111 } 112 } else { 113 sep = ""; 114 SET_FOREACH(s_iter, schemes) { 115 s = *s_iter; 116 printf("%s%s", sep, s->name); 117 sep = " "; 118 } 119 putchar('\n'); 120 } 121} 122 123static void 124print_version(void) 125{ 126 u_int width; 127 128#ifdef __LP64__ 129 width = 64; 130#else 131 width = 32; 132#endif 133 printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width); 134} 135 136static void 137usage(const char *why) 138{ 139 140 warnx("error: %s", why); 141 fputc('\n', stderr); 142 fprintf(stderr, "usage: %s <options>\n", getprogname()); 143 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"); 148 fputc('\n', stderr); 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"); 160 fputc('\n', stderr); 161 print_formats(1); 162 fputc('\n', stderr); 163 print_schemes(1); 164 fputc('\n', stderr); 165 fprintf(stderr, " partition specification:\n"); 166 fprintf(stderr, "\t<t>[/<l>]::<size>\t- empty partition of given " 167 "size\n"); 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 " 176 "label\n"); 177 178 exit(EX_USAGE); 179} 180 181static int 182parse_number(u_int *valp, u_int min, u_int max, const char *arg) 183{ 184 uint64_t val; 185 186 if (expand_number(arg, &val) == -1) 187 return (errno); 188 if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max) 189 return (EINVAL); 190 *valp = (u_int)val; 191 return (0); 192} 193 194static int 195pwr_of_two(u_int nr) 196{ 197 198 return (((nr & (nr - 1)) == 0) ? 1 : 0); 199} 200 201/* 202 * A partition specification has the following format: 203 * <type> ':' <kind> <contents> 204 * where: 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 212 * 213 * A specification that is a single dash indicates an unused partition 214 * entry. 215 */ 216static int 217parse_part(const char *spec) 218{ 219 struct part *part; 220 char *sep; 221 size_t len; 222 int error; 223 224 if (strcmp(spec, "-") == 0) { 225 nparts++; 226 return (0); 227 } 228 229 part = calloc(1, sizeof(struct part)); 230 if (part == NULL) 231 return (ENOMEM); 232 233 sep = strchr(spec, ':'); 234 if (sep == NULL) { 235 error = EINVAL; 236 goto errout; 237 } 238 len = sep - spec + 1; 239 if (len < 2) { 240 error = EINVAL; 241 goto errout; 242 } 243 part->alias = malloc(len); 244 if (part->alias == NULL) { 245 error = ENOMEM; 246 goto errout; 247 } 248 strlcpy(part->alias, spec, len); 249 spec = sep + 1; 250 251 switch (*spec) { 252 case ':': 253 part->kind = PART_KIND_SIZE; 254 break; 255 case '=': 256 part->kind = PART_KIND_FILE; 257 break; 258 case '-': 259 part->kind = PART_KIND_PIPE; 260 break; 261 default: 262 error = EINVAL; 263 goto errout; 264 } 265 spec++; 266 267 part->contents = strdup(spec); 268 if (part->contents == NULL) { 269 error = ENOMEM; 270 goto errout; 271 } 272 273 spec = part->alias; 274 sep = strchr(spec, '/'); 275 if (sep != NULL) { 276 *sep++ = '\0'; 277 if (strlen(part->alias) == 0 || strlen(sep) == 0) { 278 error = EINVAL; 279 goto errout; 280 } 281 part->label = strdup(sep); 282 if (part->label == NULL) { 283 error = ENOMEM; 284 goto errout; 285 } 286 } 287 288 part->index = nparts; 289 STAILQ_INSERT_TAIL(&partlist, part, link); 290 nparts++; 291 return (0); 292 293 errout: 294 if (part->alias != NULL) 295 free(part->alias); 296 free(part); 297 return (error); 298} 299 300#if defined(SPARSE_WRITE) 301ssize_t 302sparse_write(int fd, const void *ptr, size_t sz) 303{ 304 const char *buf, *p; 305 off_t ofs; 306 size_t len; 307 ssize_t wr, wrsz; 308 309 buf = ptr; 310 wrsz = 0; 311 p = memchr(buf, 0, sz); 312 while (sz > 0) { 313 len = (p != NULL) ? (size_t)(p - buf) : sz; 314 if (len > 0) { 315 len = (len + secsz - 1) & ~(secsz - 1); 316 if (len > sz) 317 len = sz; 318 wr = write(fd, buf, len); 319 if (wr < 0) 320 return (-1); 321 } else { 322 while (len < sz && *p++ == '\0') 323 len++; 324 if (len < sz) 325 len &= ~(secsz - 1); 326 if (len == 0) 327 continue; 328 ofs = lseek(fd, len, SEEK_CUR); 329 if (ofs < 0) 330 return (-1); 331 wr = len; 332 } 333 buf += wr; 334 sz -= wr; 335 wrsz += wr; 336 p = memchr(buf, 0, sz); 337 } 338 return (wrsz); 339} 340#endif /* SPARSE_WRITE */ 341 342void 343mkimg_uuid(struct uuid *uuid) 344{ 345 static uint8_t gen[sizeof(struct uuid)]; 346 u_int i; 347 348 if (!unit_testing) { 349 uuidgen(uuid, 1); 350 return; 351 } 352 353 for (i = 0; i < sizeof(gen); i++) 354 gen[i]++; 355 memcpy(uuid, gen, sizeof(uuid_t)); 356} 357 358static void 359mkimg(void) 360{ 361 FILE *fp; 362 struct part *part; 363 lba_t block; 364 off_t bytesize; 365 int error, fd; 366 367 /* First check partition information */ 368 STAILQ_FOREACH(part, &partlist, link) { 369 error = scheme_check_part(part); 370 if (error) 371 errc(EX_DATAERR, error, "partition %d", part->index+1); 372 } 373 374 block = scheme_metadata(SCHEME_META_IMG_START, 0); 375 STAILQ_FOREACH(part, &partlist, link) { 376 block = scheme_metadata(SCHEME_META_PART_BEFORE, block); 377 if (verbose) 378 fprintf(stderr, "partition %d: starting block %llu " 379 "... ", part->index + 1, (long long)block); 380 part->block = block; 381 switch (part->kind) { 382 case PART_KIND_SIZE: 383 if (expand_number(part->contents, &bytesize) == -1) 384 error = errno; 385 break; 386 case PART_KIND_FILE: 387 fd = open(part->contents, O_RDONLY, 0); 388 if (fd != -1) { 389 error = image_copyin(block, fd, &bytesize); 390 close(fd); 391 } else 392 error = errno; 393 break; 394 case PART_KIND_PIPE: 395 fp = popen(part->contents, "r"); 396 if (fp != NULL) { 397 fd = fileno(fp); 398 error = image_copyin(block, fd, &bytesize); 399 pclose(fp); 400 } else 401 error = errno; 402 break; 403 } 404 if (error) 405 errc(EX_IOERR, error, "partition %d", part->index + 1); 406 part->size = (bytesize + secsz - 1) / secsz; 407 if (verbose) { 408 bytesize = part->size * secsz; 409 fprintf(stderr, "size %llu bytes (%llu blocks)\n", 410 (long long)bytesize, (long long)part->size); 411 } 412 block = scheme_metadata(SCHEME_META_PART_AFTER, 413 part->block + part->size); 414 } 415 416 block = scheme_metadata(SCHEME_META_IMG_END, block); 417 error = image_set_size(block); 418 if (!error) 419 error = format_resize(block); 420 if (error) 421 errc(EX_IOERR, error, "image sizing"); 422 block = image_get_size(); 423 ncyls = block / (nsecs * nheads); 424 error = (scheme_write(block)); 425 if (error) 426 errc(EX_IOERR, error, "writing metadata"); 427} 428 429int 430main(int argc, char *argv[]) 431{ 432 int bcfd, outfd; 433 int c, error; 434 435 bcfd = -1; 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) { 439 switch (c) { 440 case 'b': /* BOOT CODE */ 441 if (bcfd != -1) 442 usage("multiple bootcode given"); 443 bcfd = open(optarg, O_RDONLY, 0); 444 if (bcfd == -1) 445 err(EX_UNAVAILABLE, "%s", optarg); 446 break; 447 case 'f': /* OUTPUT FORMAT */ 448 if (format_selected() != NULL) 449 usage("multiple formats given"); 450 error = format_select(optarg); 451 if (error) 452 errc(EX_DATAERR, error, "format"); 453 break; 454 case 'o': /* OUTPUT FILE */ 455 if (outfd != 1) 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); 459 if (outfd == -1) 460 err(EX_CANTCREAT, "%s", optarg); 461 break; 462 case 'p': /* PARTITION */ 463 error = parse_part(optarg); 464 if (error) 465 errc(EX_DATAERR, error, "partition"); 466 break; 467 case 's': /* SCHEME */ 468 if (scheme_selected() != NULL) 469 usage("multiple schemes given"); 470 error = scheme_select(optarg); 471 if (error) 472 errc(EX_DATAERR, error, "scheme"); 473 break; 474 case 'y': 475 unit_testing++; 476 break; 477 case 'v': 478 verbose++; 479 break; 480 case 'H': /* GEOMETRY: HEADS */ 481 error = parse_number(&nheads, 1, 255, optarg); 482 if (error) 483 errc(EX_DATAERR, error, "number of heads"); 484 break; 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)) 488 error = EINVAL; 489 if (error) 490 errc(EX_DATAERR, error, "physical sector size"); 491 break; 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)) 495 error = EINVAL; 496 if (error) 497 errc(EX_DATAERR, error, "logical sector size"); 498 break; 499 case 'T': /* GEOMETRY: TRACK SIZE */ 500 error = parse_number(&nsecs, 1, 63, optarg); 501 if (error) 502 errc(EX_DATAERR, error, "track size"); 503 break; 504 case LONGOPT_FORMATS: 505 print_formats(0); 506 exit(EX_OK); 507 /*NOTREACHED*/ 508 case LONGOPT_SCHEMES: 509 print_schemes(0); 510 exit(EX_OK); 511 /*NOTREACHED*/ 512 case LONGOPT_VERSION: 513 print_version(); 514 exit(EX_OK); 515 /*NOTREACHED*/ 516 default: 517 usage("unknown option"); 518 } 519 } 520 521 if (argc > optind) 522 usage("trailing arguments"); 523 if (scheme_selected() == NULL) 524 usage("no scheme"); 525 if (nparts == 0) 526 usage("no partitions"); 527 528 if (secsz > blksz) { 529 if (blksz != 0) 530 errx(EX_DATAERR, "the physical block size cannot " 531 "be smaller than the sector size"); 532 blksz = secsz; 533 } 534 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); 538 539 if (nparts > scheme_max_parts()) 540 errx(EX_DATAERR, "%d partitions supported; %d given", 541 scheme_max_parts(), nparts); 542 543 if (format_selected() == NULL) 544 format_select("raw"); 545 546 if (bcfd != -1) { 547 error = scheme_bootcode(bcfd); 548 close(bcfd); 549 if (error) 550 errc(EX_DATAERR, error, "boot code"); 551 } 552 553 if (verbose) { 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); 558 fputc('\n', stderr); 559 fprintf(stderr, "Partitioning scheme: %s\n", 560 scheme_selected()->name); 561 fprintf(stderr, "Output file format: %s\n", 562 format_selected()->name); 563 fputc('\n', stderr); 564 } 565 566 error = image_init(); 567 if (error) 568 errc(EX_OSERR, error, "cannot initialize"); 569 570 mkimg(); 571 572 if (verbose) { 573 fputc('\n', stderr); 574 fprintf(stderr, "Number of cylinders: %u\n", ncyls); 575 } 576 577 error = format_write(outfd); 578 if (error) 579 errc(EX_IOERR, error, "writing image"); 580 581 return (0); 582} 583