2013-08-04 02:37:05 +00:00
|
|
|
/*-
|
2014-03-20 00:38:17 +00:00
|
|
|
* Copyright (c) 2013,2014 Juniper Networks, Inc.
|
2013-08-04 02:37:05 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
#include <sys/linker_set.h>
|
2013-08-04 02:37:05 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-06-28 04:18:55 +00:00
|
|
|
#include <sys/uuid.h>
|
2013-08-04 02:37:05 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2014-09-27 04:53:51 +00:00
|
|
|
#include <getopt.h>
|
2013-08-04 02:37:05 +00:00
|
|
|
#include <libutil.h>
|
2014-03-25 04:45:55 +00:00
|
|
|
#include <limits.h>
|
2013-08-04 02:37:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-05-07 17:33:31 +00:00
|
|
|
#include "image.h"
|
2014-05-14 15:46:07 +00:00
|
|
|
#include "format.h"
|
2014-03-19 21:44:51 +00:00
|
|
|
#include "mkimg.h"
|
2013-08-04 02:37:05 +00:00
|
|
|
#include "scheme.h"
|
|
|
|
|
2014-09-27 04:53:51 +00:00
|
|
|
#define LONGOPT_FORMATS 0x01000001
|
|
|
|
#define LONGOPT_SCHEMES 0x01000002
|
|
|
|
#define LONGOPT_VERSION 0x01000003
|
|
|
|
|
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "formats", no_argument, NULL, LONGOPT_FORMATS },
|
|
|
|
{ "schemes", no_argument, NULL, LONGOPT_SCHEMES },
|
|
|
|
{ "version", no_argument, NULL, LONGOPT_VERSION },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2015-02-22 04:50:47 +00:00
|
|
|
static uint64_t capacity;
|
|
|
|
|
2014-03-19 21:44:51 +00:00
|
|
|
struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
|
|
|
|
u_int nparts = 0;
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-06-28 04:18:55 +00:00
|
|
|
u_int unit_testing;
|
2014-03-27 20:14:40 +00:00
|
|
|
u_int verbose;
|
|
|
|
|
2014-03-25 04:45:55 +00:00
|
|
|
u_int ncyls = 0;
|
|
|
|
u_int nheads = 1;
|
|
|
|
u_int nsecs = 1;
|
2014-03-23 01:10:05 +00:00
|
|
|
u_int secsz = 512;
|
2014-03-27 20:14:40 +00:00
|
|
|
u_int blksz = 0;
|
2014-03-23 01:10:05 +00:00
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
static void
|
2014-09-27 04:53:51 +00:00
|
|
|
print_formats(int usage)
|
2013-08-04 02:37:05 +00:00
|
|
|
{
|
2014-05-08 01:13:18 +00:00
|
|
|
struct mkimg_format *f, **f_iter;
|
2014-09-27 04:53:51 +00:00
|
|
|
const char *sep;
|
|
|
|
|
|
|
|
if (usage) {
|
2014-09-27 17:58:03 +00:00
|
|
|
fprintf(stderr, " formats:\n");
|
2014-09-27 04:53:51 +00:00
|
|
|
SET_FOREACH(f_iter, formats) {
|
|
|
|
f = *f_iter;
|
|
|
|
fprintf(stderr, "\t%s\t- %s\n", f->name,
|
|
|
|
f->description);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sep = "";
|
|
|
|
SET_FOREACH(f_iter, formats) {
|
|
|
|
f = *f_iter;
|
|
|
|
printf("%s%s", sep, f->name);
|
|
|
|
sep = " ";
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_schemes(int usage)
|
|
|
|
{
|
2014-05-08 01:13:18 +00:00
|
|
|
struct mkimg_scheme *s, **s_iter;
|
2014-09-27 04:53:51 +00:00
|
|
|
const char *sep;
|
|
|
|
|
|
|
|
if (usage) {
|
2014-09-27 17:58:03 +00:00
|
|
|
fprintf(stderr, " schemes:\n");
|
2014-09-27 04:53:51 +00:00
|
|
|
SET_FOREACH(s_iter, schemes) {
|
|
|
|
s = *s_iter;
|
|
|
|
fprintf(stderr, "\t%s\t- %s\n", s->name,
|
|
|
|
s->description);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sep = "";
|
|
|
|
SET_FOREACH(s_iter, schemes) {
|
|
|
|
s = *s_iter;
|
|
|
|
printf("%s%s", sep, s->name);
|
|
|
|
sep = " ";
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_version(void)
|
|
|
|
{
|
|
|
|
u_int width;
|
|
|
|
|
|
|
|
#ifdef __LP64__
|
|
|
|
width = 64;
|
|
|
|
#else
|
|
|
|
width = 32;
|
|
|
|
#endif
|
|
|
|
printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(const char *why)
|
|
|
|
{
|
2014-03-20 00:38:17 +00:00
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
warnx("error: %s", why);
|
2014-09-27 17:58:03 +00:00
|
|
|
fputc('\n', stderr);
|
|
|
|
fprintf(stderr, "usage: %s <options>\n", getprogname());
|
2014-03-19 22:27:10 +00:00
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
fprintf(stderr, " options:\n");
|
2014-09-27 17:58:03 +00:00
|
|
|
fprintf(stderr, "\t--formats\t- list image formats\n");
|
|
|
|
fprintf(stderr, "\t--schemes\t- list partition schemes\n");
|
|
|
|
fprintf(stderr, "\t--version\t- show version information\n");
|
|
|
|
fputc('\n', stderr);
|
2014-03-19 22:27:10 +00:00
|
|
|
fprintf(stderr, "\t-b <file>\t- file containing boot code\n");
|
2015-02-22 04:50:47 +00:00
|
|
|
fprintf(stderr, "\t-c <num>\t- capacity (in bytes) of the disk\n");
|
2014-05-08 01:43:33 +00:00
|
|
|
fprintf(stderr, "\t-f <format>\n");
|
2014-03-19 22:27:10 +00:00
|
|
|
fprintf(stderr, "\t-o <file>\t- file to write image into\n");
|
2013-08-04 02:37:05 +00:00
|
|
|
fprintf(stderr, "\t-p <partition>\n");
|
|
|
|
fprintf(stderr, "\t-s <scheme>\n");
|
2014-07-04 18:47:25 +00:00
|
|
|
fprintf(stderr, "\t-v\t\t- increase verbosity\n");
|
|
|
|
fprintf(stderr, "\t-y\t\t- [developers] enable unit test\n");
|
2014-03-25 04:45:55 +00:00
|
|
|
fprintf(stderr, "\t-H <num>\t- number of heads to simulate\n");
|
|
|
|
fprintf(stderr, "\t-P <num>\t- physical sector size\n");
|
|
|
|
fprintf(stderr, "\t-S <num>\t- logical sector size\n");
|
|
|
|
fprintf(stderr, "\t-T <num>\t- number of tracks to simulate\n");
|
2014-09-27 17:58:03 +00:00
|
|
|
fputc('\n', stderr);
|
2014-09-27 04:53:51 +00:00
|
|
|
print_formats(1);
|
2014-09-27 17:58:03 +00:00
|
|
|
fputc('\n', stderr);
|
2014-09-27 04:53:51 +00:00
|
|
|
print_schemes(1);
|
2014-09-27 17:58:03 +00:00
|
|
|
fputc('\n', stderr);
|
|
|
|
fprintf(stderr, " partition specification:\n");
|
2014-03-21 05:37:25 +00:00
|
|
|
fprintf(stderr, "\t<t>[/<l>]::<size>\t- empty partition of given "
|
|
|
|
"size\n");
|
|
|
|
fprintf(stderr, "\t<t>[/<l>]:=<file>\t- partition content and size "
|
|
|
|
"are determined\n\t\t\t\t by the named file\n");
|
2014-03-28 04:42:34 +00:00
|
|
|
fprintf(stderr, "\t<t>[/<l>]:-<cmd>\t- partition content and size "
|
2014-03-21 05:37:25 +00:00
|
|
|
"are taken from\n\t\t\t\t the output of the command to run\n");
|
2014-09-12 20:05:08 +00:00
|
|
|
fprintf(stderr, "\t-\t\t\t- unused partition entry\n");
|
2014-03-19 22:27:10 +00:00
|
|
|
fprintf(stderr, "\t where:\n");
|
2014-03-21 05:37:25 +00:00
|
|
|
fprintf(stderr, "\t\t<t>\t- scheme neutral partition type\n");
|
|
|
|
fprintf(stderr, "\t\t<l>\t- optional scheme-dependent partition "
|
|
|
|
"label\n");
|
2014-03-19 22:27:10 +00:00
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
|
|
|
|
2014-03-25 04:45:55 +00:00
|
|
|
static int
|
2015-02-22 04:50:47 +00:00
|
|
|
parse_uint32(uint32_t *valp, uint32_t min, uint32_t max, const char *arg)
|
2014-03-25 04:45:55 +00:00
|
|
|
{
|
|
|
|
uint64_t val;
|
|
|
|
|
|
|
|
if (expand_number(arg, &val) == -1)
|
|
|
|
return (errno);
|
|
|
|
if (val > UINT_MAX || val < (uint64_t)min || val > (uint64_t)max)
|
|
|
|
return (EINVAL);
|
2015-02-22 04:50:47 +00:00
|
|
|
*valp = (uint32_t)val;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_uint64(uint64_t *valp, uint64_t min, uint64_t max, const char *arg)
|
|
|
|
{
|
|
|
|
uint64_t val;
|
|
|
|
|
|
|
|
if (expand_number(arg, &val) == -1)
|
|
|
|
return (errno);
|
|
|
|
if (val < min || val > max)
|
|
|
|
return (EINVAL);
|
|
|
|
*valp = val;
|
2014-03-25 04:45:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pwr_of_two(u_int nr)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (((nr & (nr - 1)) == 0) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
/*
|
|
|
|
* A partition specification has the following format:
|
|
|
|
* <type> ':' <kind> <contents>
|
|
|
|
* where:
|
|
|
|
* type the partition type alias
|
|
|
|
* kind the interpretation of the contents specification
|
|
|
|
* ':' contents holds the size of an empty partition
|
|
|
|
* '=' contents holds the name of a file to read
|
2014-03-28 04:42:34 +00:00
|
|
|
* '-' contents holds a command to run; the output of
|
2013-08-04 02:37:05 +00:00
|
|
|
* which is the contents of the partition.
|
|
|
|
* contents the specification of a partition's contents
|
2014-09-12 20:05:08 +00:00
|
|
|
*
|
|
|
|
* A specification that is a single dash indicates an unused partition
|
|
|
|
* entry.
|
2013-08-04 02:37:05 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_part(const char *spec)
|
|
|
|
{
|
|
|
|
struct part *part;
|
|
|
|
char *sep;
|
|
|
|
size_t len;
|
|
|
|
int error;
|
|
|
|
|
2014-09-12 20:05:08 +00:00
|
|
|
if (strcmp(spec, "-") == 0) {
|
|
|
|
nparts++;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
part = calloc(1, sizeof(struct part));
|
|
|
|
if (part == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
sep = strchr(spec, ':');
|
|
|
|
if (sep == NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
len = sep - spec + 1;
|
|
|
|
if (len < 2) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
2014-03-20 02:27:25 +00:00
|
|
|
part->alias = malloc(len);
|
|
|
|
if (part->alias == NULL) {
|
2013-08-04 02:37:05 +00:00
|
|
|
error = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
2014-03-20 02:27:25 +00:00
|
|
|
strlcpy(part->alias, spec, len);
|
2013-08-04 02:37:05 +00:00
|
|
|
spec = sep + 1;
|
|
|
|
|
|
|
|
switch (*spec) {
|
|
|
|
case ':':
|
|
|
|
part->kind = PART_KIND_SIZE;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
part->kind = PART_KIND_FILE;
|
|
|
|
break;
|
2014-03-28 04:42:34 +00:00
|
|
|
case '-':
|
2013-08-04 02:37:05 +00:00
|
|
|
part->kind = PART_KIND_PIPE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
spec++;
|
|
|
|
|
|
|
|
part->contents = strdup(spec);
|
|
|
|
if (part->contents == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2014-03-21 05:30:27 +00:00
|
|
|
spec = part->alias;
|
|
|
|
sep = strchr(spec, '/');
|
|
|
|
if (sep != NULL) {
|
|
|
|
*sep++ = '\0';
|
|
|
|
if (strlen(part->alias) == 0 || strlen(sep) == 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
part->label = strdup(sep);
|
|
|
|
if (part->label == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
part->index = nparts;
|
2014-03-19 21:44:51 +00:00
|
|
|
STAILQ_INSERT_TAIL(&partlist, part, link);
|
2013-08-04 02:37:05 +00:00
|
|
|
nparts++;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
errout:
|
2014-03-20 02:27:25 +00:00
|
|
|
if (part->alias != NULL)
|
|
|
|
free(part->alias);
|
2013-08-04 02:37:05 +00:00
|
|
|
free(part);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-03-28 01:55:02 +00:00
|
|
|
#if defined(SPARSE_WRITE)
|
2014-05-07 22:06:54 +00:00
|
|
|
ssize_t
|
2014-05-15 14:37:35 +00:00
|
|
|
sparse_write(int fd, const void *ptr, size_t sz)
|
2014-03-28 01:55:02 +00:00
|
|
|
{
|
2014-05-15 14:37:35 +00:00
|
|
|
const char *buf, *p;
|
2014-03-28 01:55:02 +00:00
|
|
|
off_t ofs;
|
|
|
|
size_t len;
|
|
|
|
ssize_t wr, wrsz;
|
|
|
|
|
2014-05-15 14:37:35 +00:00
|
|
|
buf = ptr;
|
2014-03-28 01:55:02 +00:00
|
|
|
wrsz = 0;
|
|
|
|
p = memchr(buf, 0, sz);
|
|
|
|
while (sz > 0) {
|
2014-03-29 03:55:58 +00:00
|
|
|
len = (p != NULL) ? (size_t)(p - buf) : sz;
|
2014-03-28 01:55:02 +00:00
|
|
|
if (len > 0) {
|
|
|
|
len = (len + secsz - 1) & ~(secsz - 1);
|
|
|
|
if (len > sz)
|
|
|
|
len = sz;
|
|
|
|
wr = write(fd, buf, len);
|
|
|
|
if (wr < 0)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
while (len < sz && *p++ == '\0')
|
|
|
|
len++;
|
|
|
|
if (len < sz)
|
|
|
|
len &= ~(secsz - 1);
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
ofs = lseek(fd, len, SEEK_CUR);
|
|
|
|
if (ofs < 0)
|
|
|
|
return (-1);
|
|
|
|
wr = len;
|
|
|
|
}
|
|
|
|
buf += wr;
|
|
|
|
sz -= wr;
|
|
|
|
wrsz += wr;
|
|
|
|
p = memchr(buf, 0, sz);
|
|
|
|
}
|
|
|
|
return (wrsz);
|
|
|
|
}
|
|
|
|
#endif /* SPARSE_WRITE */
|
|
|
|
|
2014-10-03 20:48:11 +00:00
|
|
|
void
|
|
|
|
mkimg_chs(lba_t lba, u_int maxcyl, u_int *cylp, u_int *hdp, u_int *secp)
|
|
|
|
{
|
|
|
|
u_int hd, sec;
|
|
|
|
|
|
|
|
*cylp = *hdp = *secp = ~0U;
|
|
|
|
if (nsecs == 1 || nheads == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sec = lba % nsecs + 1;
|
|
|
|
lba /= nsecs;
|
|
|
|
hd = lba % nheads;
|
|
|
|
lba /= nheads;
|
|
|
|
if (lba > maxcyl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*cylp = lba;
|
|
|
|
*hdp = hd;
|
|
|
|
*secp = sec;
|
|
|
|
}
|
|
|
|
|
2014-06-28 04:18:55 +00:00
|
|
|
void
|
|
|
|
mkimg_uuid(struct uuid *uuid)
|
|
|
|
{
|
|
|
|
static uint8_t gen[sizeof(struct uuid)];
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (!unit_testing) {
|
|
|
|
uuidgen(uuid, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(gen); i++)
|
|
|
|
gen[i]++;
|
|
|
|
memcpy(uuid, gen, sizeof(uuid_t));
|
|
|
|
}
|
|
|
|
|
2015-02-22 04:50:47 +00:00
|
|
|
static int
|
|
|
|
capacity_resize(lba_t end)
|
|
|
|
{
|
|
|
|
lba_t capsz;
|
|
|
|
|
|
|
|
capsz = (capacity + secsz - 1) / secsz;
|
|
|
|
if (end >= capsz)
|
|
|
|
return (0);
|
|
|
|
return (image_set_size(capsz));
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
static void
|
2014-05-07 22:06:54 +00:00
|
|
|
mkimg(void)
|
2013-08-04 02:37:05 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct part *part;
|
2014-03-23 01:10:05 +00:00
|
|
|
lba_t block;
|
|
|
|
off_t bytesize;
|
2013-08-04 02:37:05 +00:00
|
|
|
int error, fd;
|
|
|
|
|
2014-03-20 20:14:26 +00:00
|
|
|
/* First check partition information */
|
|
|
|
STAILQ_FOREACH(part, &partlist, link) {
|
|
|
|
error = scheme_check_part(part);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "partition %d", part->index+1);
|
|
|
|
}
|
|
|
|
|
2014-03-27 22:45:05 +00:00
|
|
|
block = scheme_metadata(SCHEME_META_IMG_START, 0);
|
2014-03-19 21:44:51 +00:00
|
|
|
STAILQ_FOREACH(part, &partlist, link) {
|
2014-03-27 22:45:05 +00:00
|
|
|
block = scheme_metadata(SCHEME_META_PART_BEFORE, block);
|
|
|
|
if (verbose)
|
2014-03-28 04:14:40 +00:00
|
|
|
fprintf(stderr, "partition %d: starting block %llu "
|
|
|
|
"... ", part->index + 1, (long long)block);
|
2014-03-23 01:10:05 +00:00
|
|
|
part->block = block;
|
2013-08-04 02:37:05 +00:00
|
|
|
switch (part->kind) {
|
|
|
|
case PART_KIND_SIZE:
|
2014-03-23 01:10:05 +00:00
|
|
|
if (expand_number(part->contents, &bytesize) == -1)
|
2013-08-04 02:37:05 +00:00
|
|
|
error = errno;
|
|
|
|
break;
|
|
|
|
case PART_KIND_FILE:
|
|
|
|
fd = open(part->contents, O_RDONLY, 0);
|
|
|
|
if (fd != -1) {
|
2014-05-07 20:50:16 +00:00
|
|
|
error = image_copyin(block, fd, &bytesize);
|
2013-08-04 02:37:05 +00:00
|
|
|
close(fd);
|
|
|
|
} else
|
|
|
|
error = errno;
|
|
|
|
break;
|
|
|
|
case PART_KIND_PIPE:
|
|
|
|
fp = popen(part->contents, "r");
|
|
|
|
if (fp != NULL) {
|
2014-05-07 14:57:56 +00:00
|
|
|
fd = fileno(fp);
|
2014-05-07 20:50:16 +00:00
|
|
|
error = image_copyin(block, fd, &bytesize);
|
2013-08-04 02:37:05 +00:00
|
|
|
pclose(fp);
|
|
|
|
} else
|
|
|
|
error = errno;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-20 02:27:25 +00:00
|
|
|
if (error)
|
2014-03-27 22:45:05 +00:00
|
|
|
errc(EX_IOERR, error, "partition %d", part->index + 1);
|
2014-03-23 01:10:05 +00:00
|
|
|
part->size = (bytesize + secsz - 1) / secsz;
|
2014-03-27 22:45:05 +00:00
|
|
|
if (verbose) {
|
|
|
|
bytesize = part->size * secsz;
|
2014-03-28 04:14:40 +00:00
|
|
|
fprintf(stderr, "size %llu bytes (%llu blocks)\n",
|
2014-03-27 22:45:05 +00:00
|
|
|
(long long)bytesize, (long long)part->size);
|
|
|
|
}
|
|
|
|
block = scheme_metadata(SCHEME_META_PART_AFTER,
|
|
|
|
part->block + part->size);
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:45:05 +00:00
|
|
|
block = scheme_metadata(SCHEME_META_IMG_END, block);
|
2014-05-14 15:46:07 +00:00
|
|
|
error = image_set_size(block);
|
2015-02-22 04:50:47 +00:00
|
|
|
if (!error)
|
|
|
|
error = capacity_resize(block);
|
2014-05-14 15:46:07 +00:00
|
|
|
if (!error)
|
|
|
|
error = format_resize(block);
|
|
|
|
if (error)
|
|
|
|
errc(EX_IOERR, error, "image sizing");
|
|
|
|
block = image_get_size();
|
|
|
|
ncyls = block / (nsecs * nheads);
|
2015-02-22 00:58:10 +00:00
|
|
|
error = scheme_write(block);
|
2014-05-14 15:46:07 +00:00
|
|
|
if (error)
|
|
|
|
errc(EX_IOERR, error, "writing metadata");
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-05-07 22:06:54 +00:00
|
|
|
int bcfd, outfd;
|
2013-08-04 02:37:05 +00:00
|
|
|
int c, error;
|
|
|
|
|
2014-05-07 22:06:54 +00:00
|
|
|
bcfd = -1;
|
|
|
|
outfd = 1; /* Write to stdout by default */
|
2015-02-22 04:50:47 +00:00
|
|
|
while ((c = getopt_long(argc, argv, "b:c:f:o:p:s:vyH:P:S:T:",
|
2014-09-27 04:53:51 +00:00
|
|
|
longopts, NULL)) != -1) {
|
2013-08-04 02:37:05 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'b': /* BOOT CODE */
|
2014-03-21 19:40:05 +00:00
|
|
|
if (bcfd != -1)
|
2013-08-04 02:37:05 +00:00
|
|
|
usage("multiple bootcode given");
|
|
|
|
bcfd = open(optarg, O_RDONLY, 0);
|
|
|
|
if (bcfd == -1)
|
|
|
|
err(EX_UNAVAILABLE, "%s", optarg);
|
|
|
|
break;
|
2015-02-22 04:50:47 +00:00
|
|
|
case 'c': /* CAPACITY */
|
|
|
|
error = parse_uint64(&capacity, 1, OFF_MAX, optarg);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "capacity in bytes");
|
|
|
|
break;
|
2014-05-08 01:43:33 +00:00
|
|
|
case 'f': /* OUTPUT FORMAT */
|
|
|
|
if (format_selected() != NULL)
|
|
|
|
usage("multiple formats given");
|
|
|
|
error = format_select(optarg);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "format");
|
|
|
|
break;
|
2013-08-04 02:37:05 +00:00
|
|
|
case 'o': /* OUTPUT FILE */
|
2014-05-07 22:06:54 +00:00
|
|
|
if (outfd != 1)
|
2013-08-04 02:37:05 +00:00
|
|
|
usage("multiple output files given");
|
|
|
|
outfd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
|
|
|
|
if (outfd == -1)
|
|
|
|
err(EX_CANTCREAT, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 'p': /* PARTITION */
|
|
|
|
error = parse_part(optarg);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "partition");
|
|
|
|
break;
|
|
|
|
case 's': /* SCHEME */
|
2014-03-20 00:38:17 +00:00
|
|
|
if (scheme_selected() != NULL)
|
2013-08-04 02:37:05 +00:00
|
|
|
usage("multiple schemes given");
|
|
|
|
error = scheme_select(optarg);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "scheme");
|
|
|
|
break;
|
2014-06-28 04:18:55 +00:00
|
|
|
case 'y':
|
|
|
|
unit_testing++;
|
|
|
|
break;
|
2014-03-27 20:14:40 +00:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2014-03-25 04:45:55 +00:00
|
|
|
case 'H': /* GEOMETRY: HEADS */
|
2015-02-22 04:50:47 +00:00
|
|
|
error = parse_uint32(&nheads, 1, 255, optarg);
|
2014-03-25 04:45:55 +00:00
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "number of heads");
|
|
|
|
break;
|
|
|
|
case 'P': /* GEOMETRY: PHYSICAL SECTOR SIZE */
|
2015-02-22 04:50:47 +00:00
|
|
|
error = parse_uint32(&blksz, 512, INT_MAX+1U, optarg);
|
2014-03-25 04:45:55 +00:00
|
|
|
if (error == 0 && !pwr_of_two(blksz))
|
|
|
|
error = EINVAL;
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "physical sector size");
|
|
|
|
break;
|
|
|
|
case 'S': /* GEOMETRY: LOGICAL SECTOR SIZE */
|
2015-02-22 04:50:47 +00:00
|
|
|
error = parse_uint32(&secsz, 512, INT_MAX+1U, optarg);
|
2014-03-27 20:14:40 +00:00
|
|
|
if (error == 0 && !pwr_of_two(secsz))
|
2014-03-25 04:45:55 +00:00
|
|
|
error = EINVAL;
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "logical sector size");
|
|
|
|
break;
|
|
|
|
case 'T': /* GEOMETRY: TRACK SIZE */
|
2015-02-22 04:50:47 +00:00
|
|
|
error = parse_uint32(&nsecs, 1, 63, optarg);
|
2014-03-25 04:45:55 +00:00
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "track size");
|
|
|
|
break;
|
2014-09-27 04:53:51 +00:00
|
|
|
case LONGOPT_FORMATS:
|
|
|
|
print_formats(0);
|
|
|
|
exit(EX_OK);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case LONGOPT_SCHEMES:
|
|
|
|
print_schemes(0);
|
|
|
|
exit(EX_OK);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case LONGOPT_VERSION:
|
|
|
|
print_version();
|
|
|
|
exit(EX_OK);
|
|
|
|
/*NOTREACHED*/
|
2013-08-04 02:37:05 +00:00
|
|
|
default:
|
|
|
|
usage("unknown option");
|
|
|
|
}
|
|
|
|
}
|
2014-03-27 20:14:40 +00:00
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
if (argc > optind)
|
|
|
|
usage("trailing arguments");
|
2015-02-22 01:20:49 +00:00
|
|
|
if (scheme_selected() == NULL && nparts > 0)
|
2013-08-04 02:37:05 +00:00
|
|
|
usage("no scheme");
|
2015-02-22 04:50:47 +00:00
|
|
|
if (nparts == 0 && capacity == 0)
|
2013-08-04 02:37:05 +00:00
|
|
|
usage("no partitions");
|
|
|
|
|
2014-03-27 20:14:40 +00:00
|
|
|
if (secsz > blksz) {
|
|
|
|
if (blksz != 0)
|
|
|
|
errx(EX_DATAERR, "the physical block size cannot "
|
|
|
|
"be smaller than the sector size");
|
|
|
|
blksz = secsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secsz > scheme_max_secsz())
|
|
|
|
errx(EX_DATAERR, "maximum sector size supported is %u; "
|
|
|
|
"size specified is %u", scheme_max_secsz(), secsz);
|
|
|
|
|
|
|
|
if (nparts > scheme_max_parts())
|
|
|
|
errx(EX_DATAERR, "%d partitions supported; %d given",
|
|
|
|
scheme_max_parts(), nparts);
|
|
|
|
|
2014-05-08 01:43:33 +00:00
|
|
|
if (format_selected() == NULL)
|
|
|
|
format_select("raw");
|
|
|
|
|
2014-05-07 22:06:54 +00:00
|
|
|
if (bcfd != -1) {
|
|
|
|
error = scheme_bootcode(bcfd);
|
|
|
|
close(bcfd);
|
|
|
|
if (error)
|
|
|
|
errc(EX_DATAERR, error, "boot code");
|
|
|
|
}
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-03-27 20:14:40 +00:00
|
|
|
if (verbose) {
|
2014-03-28 04:14:40 +00:00
|
|
|
fprintf(stderr, "Logical sector size: %u\n", secsz);
|
|
|
|
fprintf(stderr, "Physical block size: %u\n", blksz);
|
|
|
|
fprintf(stderr, "Sectors per track: %u\n", nsecs);
|
|
|
|
fprintf(stderr, "Number of heads: %u\n", nheads);
|
2014-05-08 01:43:33 +00:00
|
|
|
fputc('\n', stderr);
|
2015-02-22 01:20:49 +00:00
|
|
|
if (scheme_selected())
|
|
|
|
fprintf(stderr, "Partitioning scheme: %s\n",
|
|
|
|
scheme_selected()->name);
|
2014-05-08 01:43:33 +00:00
|
|
|
fprintf(stderr, "Output file format: %s\n",
|
|
|
|
format_selected()->name);
|
|
|
|
fputc('\n', stderr);
|
2014-03-27 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 22:06:54 +00:00
|
|
|
error = image_init();
|
|
|
|
if (error)
|
|
|
|
errc(EX_OSERR, error, "cannot initialize");
|
|
|
|
|
|
|
|
mkimg();
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-05-08 01:43:33 +00:00
|
|
|
if (verbose) {
|
|
|
|
fputc('\n', stderr);
|
2014-03-28 04:14:40 +00:00
|
|
|
fprintf(stderr, "Number of cylinders: %u\n", ncyls);
|
2014-05-08 01:43:33 +00:00
|
|
|
}
|
2014-03-27 22:45:05 +00:00
|
|
|
|
2014-05-08 21:12:39 +00:00
|
|
|
error = format_write(outfd);
|
2014-05-07 22:06:54 +00:00
|
|
|
if (error)
|
|
|
|
errc(EX_IOERR, error, "writing image");
|
2013-08-04 02:37:05 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|