mkimg.c revision 271967
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 271967 2014-09-22 15:22:57Z 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 <libutil.h>
39#include <limits.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sysexits.h>
44#include <unistd.h>
45
46#include "image.h"
47#include "format.h"
48#include "mkimg.h"
49#include "scheme.h"
50
51struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
52u_int nparts = 0;
53
54u_int unit_testing;
55u_int verbose;
56
57u_int ncyls = 0;
58u_int nheads = 1;
59u_int nsecs = 1;
60u_int secsz = 512;
61u_int blksz = 0;
62
63static void
64usage(const char *why)
65{
66	struct mkimg_format *f, **f_iter;
67	struct mkimg_scheme *s, **s_iter;
68
69	warnx("error: %s", why);
70	fprintf(stderr, "\nusage: %s <options>\n", getprogname());
71
72	fprintf(stderr, "    options:\n");
73	fprintf(stderr, "\t-b <file>\t-  file containing boot code\n");
74	fprintf(stderr, "\t-f <format>\n");
75	fprintf(stderr, "\t-o <file>\t-  file to write image into\n");
76	fprintf(stderr, "\t-p <partition>\n");
77	fprintf(stderr, "\t-s <scheme>\n");
78	fprintf(stderr, "\t-v\t\t-  increase verbosity\n");
79	fprintf(stderr, "\t-y\t\t-  [developers] enable unit test\n");
80	fprintf(stderr, "\t-H <num>\t-  number of heads to simulate\n");
81	fprintf(stderr, "\t-P <num>\t-  physical sector size\n");
82	fprintf(stderr, "\t-S <num>\t-  logical sector size\n");
83	fprintf(stderr, "\t-T <num>\t-  number of tracks to simulate\n");
84
85	fprintf(stderr, "\n    formats:\n");
86	SET_FOREACH(f_iter, formats) {
87		f = *f_iter;
88		fprintf(stderr, "\t%s\t-  %s\n", f->name, f->description);
89	}
90
91	fprintf(stderr, "\n    schemes:\n");
92	SET_FOREACH(s_iter, schemes) {
93		s = *s_iter;
94		fprintf(stderr, "\t%s\t-  %s\n", s->name, s->description);
95	}
96
97	fprintf(stderr, "\n    partition specification:\n");
98	fprintf(stderr, "\t<t>[/<l>]::<size>\t-  empty partition of given "
99	    "size\n");
100	fprintf(stderr, "\t<t>[/<l>]:=<file>\t-  partition content and size "
101	    "are determined\n\t\t\t\t   by the named file\n");
102	fprintf(stderr, "\t<t>[/<l>]:-<cmd>\t-  partition content and size "
103	    "are taken from\n\t\t\t\t   the output of the command to run\n");
104	fprintf(stderr, "\t-\t\t\t-  unused partition entry\n");
105	fprintf(stderr, "\t    where:\n");
106	fprintf(stderr, "\t\t<t>\t-  scheme neutral partition type\n");
107	fprintf(stderr, "\t\t<l>\t-  optional scheme-dependent partition "
108	    "label\n");
109
110	exit(EX_USAGE);
111}
112
113static int
114parse_number(u_int *valp, u_int min, u_int max, const char *arg)
115{
116	uint64_t val;
117
118	if (expand_number(arg, &val) == -1)
119		return (errno);
120	if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
121		return (EINVAL);
122	*valp = (u_int)val;
123	return (0);
124}
125
126static int
127pwr_of_two(u_int nr)
128{
129
130	return (((nr & (nr - 1)) == 0) ? 1 : 0);
131}
132
133/*
134 * A partition specification has the following format:
135 *	<type> ':' <kind> <contents>
136 * where:
137 *	type	  the partition type alias
138 *	kind	  the interpretation of the contents specification
139 *		  ':'   contents holds the size of an empty partition
140 *		  '='   contents holds the name of a file to read
141 *		  '-'   contents holds a command to run; the output of
142 *			which is the contents of the partition.
143 *	contents  the specification of a partition's contents
144 *
145 * A specification that is a single dash indicates an unused partition
146 * entry.
147 */
148static int
149parse_part(const char *spec)
150{
151	struct part *part;
152	char *sep;
153	size_t len;
154	int error;
155
156	if (strcmp(spec, "-") == 0) {
157		nparts++;
158		return (0);
159	}
160
161	part = calloc(1, sizeof(struct part));
162	if (part == NULL)
163		return (ENOMEM);
164
165	sep = strchr(spec, ':');
166	if (sep == NULL) {
167		error = EINVAL;
168		goto errout;
169	}
170	len = sep - spec + 1;
171	if (len < 2) {
172		error = EINVAL;
173		goto errout;
174	}
175	part->alias = malloc(len);
176	if (part->alias == NULL) {
177		error = ENOMEM;
178		goto errout;
179	}
180	strlcpy(part->alias, spec, len);
181	spec = sep + 1;
182
183	switch (*spec) {
184	case ':':
185		part->kind = PART_KIND_SIZE;
186		break;
187	case '=':
188		part->kind = PART_KIND_FILE;
189		break;
190	case '-':
191		part->kind = PART_KIND_PIPE;
192		break;
193	default:
194		error = EINVAL;
195		goto errout;
196	}
197	spec++;
198
199	part->contents = strdup(spec);
200	if (part->contents == NULL) {
201		error = ENOMEM;
202		goto errout;
203	}
204
205	spec = part->alias;
206	sep = strchr(spec, '/');
207	if (sep != NULL) {
208		*sep++ = '\0';
209		if (strlen(part->alias) == 0 || strlen(sep) == 0) {
210			error = EINVAL;
211			goto errout;
212		}
213		part->label = strdup(sep);
214		if (part->label == NULL) {
215			error = ENOMEM;
216			goto errout;
217		}
218	}
219
220	part->index = nparts;
221	STAILQ_INSERT_TAIL(&partlist, part, link);
222	nparts++;
223	return (0);
224
225 errout:
226	if (part->alias != NULL)
227		free(part->alias);
228	free(part);
229	return (error);
230}
231
232#if defined(SPARSE_WRITE)
233ssize_t
234sparse_write(int fd, const void *ptr, size_t sz)
235{
236	const char *buf, *p;
237	off_t ofs;
238	size_t len;
239	ssize_t wr, wrsz;
240
241	buf = ptr;
242	wrsz = 0;
243	p = memchr(buf, 0, sz);
244	while (sz > 0) {
245		len = (p != NULL) ? (size_t)(p - buf) : sz;
246		if (len > 0) {
247			len = (len + secsz - 1) & ~(secsz - 1);
248			if (len > sz)
249				len = sz;
250			wr = write(fd, buf, len);
251			if (wr < 0)
252				return (-1);
253		} else {
254			while (len < sz && *p++ == '\0')
255				len++;
256			if (len < sz)
257				len &= ~(secsz - 1);
258			if (len == 0)
259				continue;
260			ofs = lseek(fd, len, SEEK_CUR);
261			if (ofs < 0)
262				return (-1);
263			wr = len;
264		}
265		buf += wr;
266		sz -= wr;
267		wrsz += wr;
268		p = memchr(buf, 0, sz);
269	}
270	return (wrsz);
271}
272#endif /* SPARSE_WRITE */
273
274void
275mkimg_uuid(struct uuid *uuid)
276{
277	static uint8_t gen[sizeof(struct uuid)];
278	u_int i;
279
280	if (!unit_testing) {
281		uuidgen(uuid, 1);
282		return;
283	}
284
285	for (i = 0; i < sizeof(gen); i++)
286		gen[i]++;
287	memcpy(uuid, gen, sizeof(uuid_t));
288}
289
290static void
291mkimg(void)
292{
293	FILE *fp;
294	struct part *part;
295	lba_t block;
296	off_t bytesize;
297	int error, fd;
298
299	/* First check partition information */
300	STAILQ_FOREACH(part, &partlist, link) {
301		error = scheme_check_part(part);
302		if (error)
303			errc(EX_DATAERR, error, "partition %d", part->index+1);
304	}
305
306	block = scheme_metadata(SCHEME_META_IMG_START, 0);
307	STAILQ_FOREACH(part, &partlist, link) {
308		block = scheme_metadata(SCHEME_META_PART_BEFORE, block);
309		if (verbose)
310			fprintf(stderr, "partition %d: starting block %llu "
311			    "... ", part->index + 1, (long long)block);
312		part->block = block;
313		switch (part->kind) {
314		case PART_KIND_SIZE:
315			if (expand_number(part->contents, &bytesize) == -1)
316				error = errno;
317			break;
318		case PART_KIND_FILE:
319			fd = open(part->contents, O_RDONLY, 0);
320			if (fd != -1) {
321				error = image_copyin(block, fd, &bytesize);
322				close(fd);
323			} else
324				error = errno;
325			break;
326		case PART_KIND_PIPE:
327			fp = popen(part->contents, "r");
328			if (fp != NULL) {
329				fd = fileno(fp);
330				error = image_copyin(block, fd, &bytesize);
331				pclose(fp);
332			} else
333				error = errno;
334			break;
335		}
336		if (error)
337			errc(EX_IOERR, error, "partition %d", part->index + 1);
338		part->size = (bytesize + secsz - 1) / secsz;
339		if (verbose) {
340			bytesize = part->size * secsz;
341			fprintf(stderr, "size %llu bytes (%llu blocks)\n",
342			     (long long)bytesize, (long long)part->size);
343		}
344		block = scheme_metadata(SCHEME_META_PART_AFTER,
345		    part->block + part->size);
346	}
347
348	block = scheme_metadata(SCHEME_META_IMG_END, block);
349	error = image_set_size(block);
350	if (!error)
351		error = format_resize(block);
352	if (error)
353		errc(EX_IOERR, error, "image sizing");
354	block = image_get_size();
355	ncyls = block / (nsecs * nheads);
356	error = (scheme_write(block));
357	if (error)
358		errc(EX_IOERR, error, "writing metadata");
359}
360
361int
362main(int argc, char *argv[])
363{
364	int bcfd, outfd;
365	int c, error;
366
367	bcfd = -1;
368	outfd = 1;	/* Write to stdout by default */
369	while ((c = getopt(argc, argv, "b:f:o:p:s:vyH:P:S:T:")) != -1) {
370		switch (c) {
371		case 'b':	/* BOOT CODE */
372			if (bcfd != -1)
373				usage("multiple bootcode given");
374			bcfd = open(optarg, O_RDONLY, 0);
375			if (bcfd == -1)
376				err(EX_UNAVAILABLE, "%s", optarg);
377			break;
378		case 'f':	/* OUTPUT FORMAT */
379			if (format_selected() != NULL)
380				usage("multiple formats given");
381			error = format_select(optarg);
382			if (error)
383				errc(EX_DATAERR, error, "format");
384			break;
385		case 'o':	/* OUTPUT FILE */
386			if (outfd != 1)
387				usage("multiple output files given");
388			outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
389			    S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
390			if (outfd == -1)
391				err(EX_CANTCREAT, "%s", optarg);
392			break;
393		case 'p':	/* PARTITION */
394			error = parse_part(optarg);
395			if (error)
396				errc(EX_DATAERR, error, "partition");
397			break;
398		case 's':	/* SCHEME */
399			if (scheme_selected() != NULL)
400				usage("multiple schemes given");
401			error = scheme_select(optarg);
402			if (error)
403				errc(EX_DATAERR, error, "scheme");
404			break;
405		case 'y':
406			unit_testing++;
407			break;
408		case 'v':
409			verbose++;
410			break;
411		case 'H':	/* GEOMETRY: HEADS */
412			error = parse_number(&nheads, 1, 255, optarg);
413			if (error)
414				errc(EX_DATAERR, error, "number of heads");
415			break;
416		case 'P':	/* GEOMETRY: PHYSICAL SECTOR SIZE */
417			error = parse_number(&blksz, 512, INT_MAX+1U, optarg);
418			if (error == 0 && !pwr_of_two(blksz))
419				error = EINVAL;
420			if (error)
421				errc(EX_DATAERR, error, "physical sector size");
422			break;
423		case 'S':	/* GEOMETRY: LOGICAL SECTOR SIZE */
424			error = parse_number(&secsz, 512, INT_MAX+1U, optarg);
425			if (error == 0 && !pwr_of_two(secsz))
426				error = EINVAL;
427			if (error)
428				errc(EX_DATAERR, error, "logical sector size");
429			break;
430		case 'T':	/* GEOMETRY: TRACK SIZE */
431			error = parse_number(&nsecs, 1, 63, optarg);
432			if (error)
433				errc(EX_DATAERR, error, "track size");
434			break;
435		default:
436			usage("unknown option");
437		}
438	}
439
440	if (argc > optind)
441		usage("trailing arguments");
442	if (scheme_selected() == NULL)
443		usage("no scheme");
444	if (nparts == 0)
445		usage("no partitions");
446
447	if (secsz > blksz) {
448		if (blksz != 0)
449			errx(EX_DATAERR, "the physical block size cannot "
450			    "be smaller than the sector size");
451		blksz = secsz;
452	}
453
454	if (secsz > scheme_max_secsz())
455		errx(EX_DATAERR, "maximum sector size supported is %u; "
456		    "size specified is %u", scheme_max_secsz(), secsz);
457
458	if (nparts > scheme_max_parts())
459		errx(EX_DATAERR, "%d partitions supported; %d given",
460		    scheme_max_parts(), nparts);
461
462	if (format_selected() == NULL)
463		format_select("raw");
464
465	if (bcfd != -1) {
466		error = scheme_bootcode(bcfd);
467		close(bcfd);
468		if (error)
469			errc(EX_DATAERR, error, "boot code");
470	}
471
472	if (verbose) {
473		fprintf(stderr, "Logical sector size: %u\n", secsz);
474		fprintf(stderr, "Physical block size: %u\n", blksz);
475		fprintf(stderr, "Sectors per track:   %u\n", nsecs);
476		fprintf(stderr, "Number of heads:     %u\n", nheads);
477		fputc('\n', stderr);
478		fprintf(stderr, "Partitioning scheme: %s\n",
479		    scheme_selected()->name);
480		fprintf(stderr, "Output file format:  %s\n",
481		    format_selected()->name);
482		fputc('\n', stderr);
483	}
484
485	error = image_init();
486	if (error)
487		errc(EX_OSERR, error, "cannot initialize");
488
489	mkimg();
490
491	if (verbose) {
492		fputc('\n', stderr);
493		fprintf(stderr, "Number of cylinders: %u\n", ncyls);
494	}
495
496	error = format_write(outfd);
497	if (error)
498		errc(EX_IOERR, error, "writing image");
499
500	return (0);
501}
502