svn commit: r263409 - user/marcel/mkimg

Marcel Moolenaar marcel at FreeBSD.org
Thu Mar 20 00:38:20 UTC 2014


Author: marcel
Date: Thu Mar 20 00:38:17 2014
New Revision: 263409
URL: http://svnweb.freebsd.org/changeset/base/263409

Log:
  Use linker sets to provide build-time selection of the schemes
  that need to be supported. Each scheme provides data and callbacks
  for handling the scheme-specifics.
  To this end, put each scheme in its own source file.

Added:
  user/marcel/mkimg/apm.c   (contents, props changed)
  user/marcel/mkimg/bsd.c   (contents, props changed)
  user/marcel/mkimg/ebr.c   (contents, props changed)
  user/marcel/mkimg/gpt.c   (contents, props changed)
  user/marcel/mkimg/mbr.c   (contents, props changed)
  user/marcel/mkimg/pc98.c   (contents, props changed)
  user/marcel/mkimg/vtoc8.c   (contents, props changed)
Modified:
  user/marcel/mkimg/Makefile
  user/marcel/mkimg/mkimg.c
  user/marcel/mkimg/scheme.c
  user/marcel/mkimg/scheme.h

Modified: user/marcel/mkimg/Makefile
==============================================================================
--- user/marcel/mkimg/Makefile	Thu Mar 20 00:32:26 2014	(r263408)
+++ user/marcel/mkimg/Makefile	Thu Mar 20 00:38:17 2014	(r263409)
@@ -4,6 +4,16 @@ PROG=	mkimg
 SRCS=	mkimg.c scheme.c
 MAN=	mkimg.8
 
+# List of schemes to support
+SRCS+=	\
+	apm.c \
+	bsd.c \
+	ebr.c \
+	gpt.c \
+	mbr.c \
+	pc98.c \
+	vtoc8.c
+
 BINDIR?=/usr/sbin
 
 DPADD=	${LIBUTIL}

Added: user/marcel/mkimg/apm.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/apm.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,67 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/apm.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias apm_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+apm_get_leader(u_int parts)
+{
+
+	return (parts + 1);
+}
+
+static off_t
+apm_get_trailer(u_int parts __unused)
+{
+
+	return (0);
+}
+
+static struct mkimg_scheme apm_scheme = {
+	.name = "apm",
+	.description = "Apple Partition Map",
+	.nparts = 4096,
+	.padding = 0,
+	.aliases = apm_aliases,
+	.get_leader = apm_get_leader,
+	.get_trailer = apm_get_trailer
+};
+
+SCHEME_DEFINE(apm_scheme);

Added: user/marcel/mkimg/bsd.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/bsd.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,67 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias bsd_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+bsd_get_leader(u_int parts __unused)
+{
+
+	return (16);
+}
+
+static off_t
+bsd_get_trailer(u_int parts __unused)
+{
+
+	return (0);
+}
+
+static struct mkimg_scheme bsd_scheme = {
+	.name = "bsd",
+	.description = "GUID Partition Table",
+	.nparts = 20,
+	.padding = 0,
+	.aliases = bsd_aliases,
+	.get_leader = bsd_get_leader,
+	.get_trailer = bsd_get_trailer
+};
+
+SCHEME_DEFINE(bsd_scheme);

Added: user/marcel/mkimg/ebr.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/ebr.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,71 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias ebr_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+ebr_get_leader(u_int parts __unused)
+{
+
+	return (1);
+}
+
+static off_t
+ebr_get_trailer(u_int parts __unused)
+{
+
+	/*
+	 * Compensate for having reserved a sector for the EBR after
+	 * the last partition.
+	 */
+	return (-1);
+}
+
+static struct mkimg_scheme ebr_scheme = {
+	.name = "ebr",
+	.description = "Extended Boot Record",
+	.nparts = 4096,
+	.padding = 1,	/* See ebr_get_trailer() above */
+	.aliases = ebr_aliases,
+	.get_leader = ebr_get_leader,
+	.get_trailer = ebr_get_trailer
+};
+
+SCHEME_DEFINE(ebr_scheme);

Added: user/marcel/mkimg/gpt.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/gpt.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,71 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/gpt.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
+
+static struct mkimg_alias gpt_aliases[] = {
+    {	"efi", ALIAS_PTR(&gpt_uuid_efi) },
+    {	NULL, 0 }
+};
+
+static off_t
+gpt_get_leader(u_int parts)
+{
+
+	return (2 + (parts + 3) / 4);
+}
+
+static off_t
+gpt_get_trailer(u_int parts)
+{
+
+	return (1 + (parts + 3) / 4);
+}
+
+static struct mkimg_scheme gpt_scheme = {
+	.name = "gpt",
+	.description = "GUID Partition Table",
+	.nparts = 4096,
+	.padding = 0,
+	.aliases = gpt_aliases,
+	.get_leader = gpt_get_leader,
+	.get_trailer = gpt_get_trailer
+};
+
+SCHEME_DEFINE(gpt_scheme);

Added: user/marcel/mkimg/mbr.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/mbr.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/diskmbr.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias mbr_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+mbr_get_leader(u_int parts __unused)
+{
+
+	return (1);
+}
+
+static off_t
+mbr_get_trailer(u_int parts __unused)
+{
+
+	return (0);
+}
+
+static struct mkimg_scheme mbr_scheme = {
+	.name = "mbr",
+	.description = "Master Boot Record",
+	.nparts = NDOSPART,
+	.padding = 0,
+	.aliases = mbr_aliases,
+	.get_leader = mbr_get_leader,
+	.get_trailer = mbr_get_trailer
+};
+
+SCHEME_DEFINE(mbr_scheme);

Modified: user/marcel/mkimg/mkimg.c
==============================================================================
--- user/marcel/mkimg/mkimg.c	Thu Mar 20 00:32:26 2014	(r263408)
+++ user/marcel/mkimg/mkimg.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2013 Juniper Networks, Inc.
+ * Copyright (c) 2013,2014 Juniper Networks, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,6 +27,7 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <sys/linker_set.h>
 #include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -66,6 +67,8 @@ cleanup(void)
 static void
 usage(const char *why)
 {
+	struct mkimg_scheme *s, **iter;
+
 	warnx("error: %s", why);
 	fprintf(stderr, "\nusage: %s <options>\n", getprogname());
 
@@ -79,13 +82,10 @@ usage(const char *why)
 	fprintf(stderr, "\t-z\t\t-  write a sparse file\n");
 
 	fprintf(stderr, "    schemes:\n");
-	fprintf(stderr, "\tapm\t-  Apple Partition Map\n");
-	fprintf(stderr, "\tbsd\t-  BSD disk label\n");
-	fprintf(stderr, "\tebr\t-  Extended Boot Record\n");
-	fprintf(stderr, "\tgpt\t-  GUID Partition Table\n");
-	fprintf(stderr, "\tmbr\t-  Master Boot Record\n");
-	fprintf(stderr, "\tpc98\t-  PC-9800 disk partitions\n");
-	fprintf(stderr, "\tvtoc8\t-  SMI VTOC8 disk labels\n");
+	SET_FOREACH(iter, schemes) {
+		s = *iter;
+		fprintf(stderr, "\t%s\t-  %s\n", s->name, s->description);
+	}
 
 	fprintf(stderr, "    partition specification:\n");
 	fprintf(stderr, "\t<type>::<size>\t-  empty partition of given size\n");
@@ -283,7 +283,7 @@ main(int argc, char *argv[])
 				errc(EX_DATAERR, error, "partition");
 			break;
 		case 's':	/* SCHEME */
-			if (scheme_selected() != SCHEME_UNDEF)
+			if (scheme_selected() != NULL)
 				usage("multiple schemes given");
 			error = scheme_select(optarg);
 			if (error)
@@ -299,7 +299,7 @@ main(int argc, char *argv[])
 	}
 	if (argc > optind)
 		usage("trailing arguments");
-	if (scheme_selected() == SCHEME_UNDEF)
+	if (scheme_selected() == NULL)
 		usage("no scheme");
 	if (nparts == 0)
 		usage("no partitions");

Added: user/marcel/mkimg/pc98.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/pc98.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/diskpc98.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias pc98_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+pc98_get_leader(u_int parts __unused)
+{
+
+	return (2);
+}
+
+static off_t
+pc98_get_trailer(u_int parts __unused)
+{
+
+	return (0);
+}
+
+static struct mkimg_scheme pc98_scheme = {
+	.name = "pc98",
+	.description = "PC-9800 disk partitions",
+	.nparts = PC98_NPARTS,
+	.padding = 0,
+	.aliases = pc98_aliases,
+	.get_leader = pc98_get_leader,
+	.get_trailer = pc98_get_trailer
+};
+
+SCHEME_DEFINE(pc98_scheme);

Modified: user/marcel/mkimg/scheme.c
==============================================================================
--- user/marcel/mkimg/scheme.c	Thu Mar 20 00:32:26 2014	(r263408)
+++ user/marcel/mkimg/scheme.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2013 Juniper Networks, Inc.
+ * Copyright (c) 2013,2014 Juniper Networks, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,10 +28,8 @@
 __FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
-#include <sys/diskmbr.h>
-#include <sys/diskpc98.h>
+#include <sys/linker_set.h>
 #include <sys/queue.h>
-#include <sys/vtoc.h>
 #include <err.h>
 #include <errno.h>
 #include <stdint.h>
@@ -41,40 +39,25 @@ __FBSDID("$FreeBSD$");
 #include "mkimg.h"
 #include "scheme.h"
 
-static struct scheme {
-	const char *lexeme;
-	u_int token;
-} schemes[] = {
-	{ .lexeme = "apm", .token = SCHEME_APM },
-	{ .lexeme = "bsd", .token = SCHEME_BSD },
-	{ .lexeme = "ebr", .token = SCHEME_EBR },
-	{ .lexeme = "gpt", .token = SCHEME_GPT },
-	{ .lexeme = "mbr", .token = SCHEME_MBR },
-	{ .lexeme = "pc98", .token = SCHEME_PC98 },
-	{ .lexeme = "vtoc8", .token = SCHEME_VTOC8 },
-	{ .lexeme = NULL, .token = SCHEME_UNDEF }
-};
-
-static u_int scheme = SCHEME_UNDEF;
+static struct mkimg_scheme *scheme;
 static u_int secsz = 512;
 
 int
 scheme_select(const char *spec)
 {
-	struct scheme *s;
+	struct mkimg_scheme *s, **iter;
 
-	s = schemes;
-	while (s->lexeme != NULL) {
-		if (strcasecmp(spec, s->lexeme) == 0) {
-			scheme = s->token;
+	SET_FOREACH(iter, schemes) {
+		s = *iter;
+		if (strcasecmp(spec, s->name) == 0) {
+			scheme = s;
 			return (0);
 		}
-		s++;
 	}
 	return (EINVAL);
 }
 
-u_int
+struct mkimg_scheme *
 scheme_selected(void)
 {
 
@@ -82,11 +65,12 @@ scheme_selected(void)
 }
 
 int
-scheme_check_part(struct part *p __unused)
+scheme_check_part(struct part *p)
 {
 
-	warnx("part: index=%u, type=`%s', offset=%ju, size=%ju", p->index,
-	    p->type, (uintmax_t)p->offset, (uintmax_t)p->size);
+	warnx("part(%s): index=%u, type=`%s', offset=%ju, size=%ju",
+	    scheme->name, p->index, p->type, (uintmax_t)p->offset,
+	    (uintmax_t)p->size);
 
 	return (0);
 }
@@ -94,35 +78,8 @@ scheme_check_part(struct part *p __unuse
 u_int
 scheme_max_parts(void)
 {
-	u_int parts;
 
-	switch (scheme) {
-	case SCHEME_APM:
-		parts = 4096;
-		break;
-	case SCHEME_BSD:
-		parts = 20;
-		break;
-	case SCHEME_EBR:
-		parts = 4096;
-		break;
-	case SCHEME_GPT:
-		parts = 4096;
-		break;
-	case SCHEME_MBR:
-		parts = NDOSPART;
-		break;
-	case SCHEME_PC98:
-		parts = PC98_NPARTS;
-		break;
-	case SCHEME_VTOC8:
-		parts = VTOC8_NPARTS;
-		break;
-	default:
-		parts = 0;
-		break;
-	}
-	return (parts);
+	return (scheme->nparts);
 }
 
 off_t
@@ -130,32 +87,7 @@ scheme_first_offset(u_int parts)
 {
 	off_t off;
 
-	switch (scheme) {
-	case SCHEME_APM:
-		off = parts + 1;
-		break;
-	case SCHEME_BSD:
-		off = 16;
-		break;
-	case SCHEME_EBR:
-		off = 1;
-		break;
-	case SCHEME_GPT:
-		off = 2 + (parts + 3) / 4;
-		break;
-	case SCHEME_MBR:
-		off = 1;
-		break;
-	case SCHEME_PC98:
-		off = 2;
-		break;
-	case SCHEME_VTOC8:
-		off = 1;
-		break;
-	default:
-		off = 0;
-		break;
-	}
+	off = scheme->get_leader(parts);
 	off *= secsz;
 	return (off);
 }
@@ -165,26 +97,15 @@ scheme_next_offset(off_t off, uint64_t s
 {
 
 	sz = (sz + secsz - 1) & ~(secsz - 1);
-	if (scheme == SCHEME_EBR)
-		sz += secsz;
+	sz += scheme->padding * secsz;
 	return (off + sz);
 }
 
 void
 scheme_write(int fd, off_t off)
 {
-	off_t lim;
+	off_t trailer;
 
-	switch (scheme) {
-	case SCHEME_GPT:
-		lim = off + secsz * (1 + (nparts + 3) / 4);
-		break;
-	case SCHEME_EBR:
-		off -= secsz;
-		/* FALLTHROUGH */
-	default:
-		lim = off;
-		break;
-	}
-	ftruncate(fd, lim);
+	trailer = scheme->get_trailer(nparts) * secsz;
+	ftruncate(fd, off + trailer);
 }

Modified: user/marcel/mkimg/scheme.h
==============================================================================
--- user/marcel/mkimg/scheme.h	Thu Mar 20 00:32:26 2014	(r263408)
+++ user/marcel/mkimg/scheme.h	Thu Mar 20 00:38:17 2014	(r263409)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2013 Juniper Networks, Inc.
+ * Copyright (c) 2013,2014 Juniper Networks, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -29,17 +29,28 @@
 #ifndef _MKIMG_SCHEME_H_
 #define	_MKIMG_SCHEME_H_
 
-#define	SCHEME_UNDEF	0
-#define	SCHEME_APM	1
-#define	SCHEME_BSD	2
-#define	SCHEME_EBR	3
-#define	SCHEME_GPT	4
-#define	SCHEME_MBR	5
-#define	SCHEME_PC98	6
-#define	SCHEME_VTOC8	7
+struct mkimg_alias {
+	const char *name;
+	uintptr_t tp;
+#define	ALIAS_PTR(p)	(uintptr_t)(p)
+#define	ALIAS_INT(i)	(uintptr_t)(i)
+};
+
+struct mkimg_scheme {
+	const char *name;
+	const char *description;
+	int	nparts;
+	int	padding;
+	struct mkimg_alias *aliases;
+	off_t (*get_leader)(u_int);
+	off_t (*get_trailer)(u_int);
+};
+
+SET_DECLARE(schemes, struct mkimg_scheme);
+#define	SCHEME_DEFINE(nm)	DATA_SET(schemes, nm)
 
 int	scheme_select(const char *);
-u_int	scheme_selected(void);
+struct mkimg_scheme *scheme_selected(void);
 
 int scheme_check_part(struct part *);
 u_int scheme_max_parts(void);

Added: user/marcel/mkimg/vtoc8.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/mkimg/vtoc8.c	Thu Mar 20 00:38:17 2014	(r263409)
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * 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$");
+
+#include <sys/types.h>
+#include <sys/linker_set.h>
+#include <sys/queue.h>
+#include <sys/vtoc.h>
+#include <stdlib.h>
+#include <uuid.h>
+
+#include "mkimg.h"
+#include "scheme.h"
+
+static struct mkimg_alias vtoc8_aliases[] = {
+    {	NULL, 0 }
+};
+
+static off_t
+vtoc8_get_leader(u_int parts __unused)
+{
+
+	return (1);
+}
+
+static off_t
+vtoc8_get_trailer(u_int parts __unused)
+{
+
+	return (0);
+}
+
+static struct mkimg_scheme vtoc8_scheme = {
+	.name = "vtoc8",
+	.description = "SMI VTOC8 disk labels",
+	.nparts = VTOC8_NPARTS,
+	.padding = 0,
+	.aliases = vtoc8_aliases,
+	.get_leader = vtoc8_get_leader,
+	.get_trailer = vtoc8_get_trailer
+};
+
+SCHEME_DEFINE(vtoc8_scheme);


More information about the svn-src-user mailing list