Module Name: src Committed By: christos Date: Thu Dec 3 01:07:28 UTC 2015
Modified Files: src/sbin/gpt: add.c backup.c biosboot.c create.c destroy.c header.c label.c main.c migrate.c recover.c remove.c resize.c resizedisk.c restore.c set.c show.c type.c unset.c Log Message: eliminate static globals so that commands can be re-used. To generate a diff of this commit: cvs rdiff -u -r1.37 -r1.38 src/sbin/gpt/add.c cvs rdiff -u -r1.13 -r1.14 src/sbin/gpt/backup.c cvs rdiff -u -r1.20 -r1.21 src/sbin/gpt/biosboot.c src/sbin/gpt/remove.c cvs rdiff -u -r1.16 -r1.17 src/sbin/gpt/create.c cvs rdiff -u -r1.9 -r1.10 src/sbin/gpt/destroy.c src/sbin/gpt/set.c \ src/sbin/gpt/unset.c cvs rdiff -u -r1.5 -r1.6 src/sbin/gpt/header.c cvs rdiff -u -r1.24 -r1.25 src/sbin/gpt/label.c cvs rdiff -u -r1.4 -r1.5 src/sbin/gpt/main.c cvs rdiff -u -r1.25 -r1.26 src/sbin/gpt/migrate.c cvs rdiff -u -r1.11 -r1.12 src/sbin/gpt/recover.c src/sbin/gpt/resizedisk.c \ src/sbin/gpt/type.c cvs rdiff -u -r1.18 -r1.19 src/sbin/gpt/resize.c cvs rdiff -u -r1.14 -r1.15 src/sbin/gpt/restore.c cvs rdiff -u -r1.27 -r1.28 src/sbin/gpt/show.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sbin/gpt/add.c diff -u src/sbin/gpt/add.c:1.37 src/sbin/gpt/add.c:1.38 --- src/sbin/gpt/add.c:1.37 Wed Dec 2 06:20:34 2015 +++ src/sbin/gpt/add.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: add.c,v 1.37 2015/12/02 11:20:34 jnemeth Exp $"); +__RCSID("$NetBSD: add.c,v 1.38 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -51,15 +51,11 @@ __RCSID("$NetBSD: add.c,v 1.37 2015/12/0 #include "gpt.h" #include "gpt_private.h" -static gpt_uuid_t type; -static off_t alignment, block, sectors, size; -static unsigned int entry; -static uint8_t *name; static int cmd_add(gpt_t, int, char *[]); static const char *addhelp[] = { - "[-a alignment] [-b blocknr] [-i index] [-l label]", - "[-s size] [-t type]", + "[-a alignment] [-b blocknr] [-i index] [-l label]", + "[-s size] [-t type]", }; struct gpt_cmd c_add = { @@ -84,7 +80,8 @@ ent_set(struct gpt_ent *ent, const map_t } static int -add(gpt_t gpt) +add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size, + u_int entry, uint8_t *name, gpt_uuid_t type) { map_t map; struct gpt_hdr *hdr; @@ -157,6 +154,12 @@ static int cmd_add(gpt_t gpt, int argc, char *argv[]) { int ch; + off_t alignment = 0, block = 0, sectors = 0, size = 0; + unsigned int entry = 0; + uint8_t *name = NULL; + gpt_uuid_t type; + + gpt_uuid_copy(type, gpt_uuid_nil); while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) { switch(ch) { @@ -193,5 +196,5 @@ cmd_add(gpt_t gpt, int argc, char *argv[ if ((sectors = gpt_check_ais(gpt, alignment, ~0, size)) == -1) return -1; - return add(gpt); + return add(gpt, alignment, block, sectors, size, entry, name, type); } Index: src/sbin/gpt/backup.c diff -u src/sbin/gpt/backup.c:1.13 src/sbin/gpt/backup.c:1.14 --- src/sbin/gpt/backup.c:1.13 Wed Dec 2 07:36:53 2015 +++ src/sbin/gpt/backup.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: backup.c,v 1.13 2015/12/02 12:36:53 christos Exp $"); +__RCSID("$NetBSD: backup.c,v 1.14 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/bootblock.h> @@ -51,10 +51,8 @@ __RCSID("$NetBSD: backup.c,v 1.13 2015/1 #include "gpt.h" #include "gpt_private.h" -static const char *outfile = "/dev/stdout"; - static const char *backuphelp[] = { - "[-o outfile]", + "[-o outfile]", }; static int cmd_backup(gpt_t, int, char *[]); @@ -241,7 +239,7 @@ store_tbl(gpt_t gpt, const map_t m, prop } static int -backup(gpt_t gpt) +backup(gpt_t gpt, const char *outfile) { map_t m; struct mbr *mbr; @@ -309,12 +307,14 @@ backup(gpt_t gpt) propext = prop_dictionary_externalize(props); PROP_ERR(propext); prop_object_release(props); - if ((fp = fopen(outfile, "w")) == NULL) { + fp = strcmp(outfile, "-") == 0 ? stdout : fopen(outfile, "w"); + if (fp == NULL) { gpt_warn(gpt, "Can't open `%s'", outfile); return -1; } fputs(propext, fp); - fclose(fp); + if (fp != stdin) + fclose(fp); free(propext); return 0; } @@ -323,6 +323,7 @@ static int cmd_backup(gpt_t gpt, int argc, char *argv[]) { int ch; + const char *outfile = "-"; while ((ch = getopt(argc, argv, "o:")) != -1) { switch(ch) { @@ -336,5 +337,5 @@ cmd_backup(gpt_t gpt, int argc, char *ar if (argc != optind) return usage(); - return backup(gpt); + return backup(gpt, outfile); } Index: src/sbin/gpt/biosboot.c diff -u src/sbin/gpt/biosboot.c:1.20 src/sbin/gpt/biosboot.c:1.21 --- src/sbin/gpt/biosboot.c:1.20 Wed Dec 2 07:24:02 2015 +++ src/sbin/gpt/biosboot.c Wed Dec 2 20:07:28 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: biosboot.c,v 1.20 2015/12/02 12:24:02 christos Exp $ */ +/* $NetBSD: biosboot.c,v 1.21 2015/12/03 01:07:28 christos Exp $ */ /* * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: biosboot.c,v 1.20 2015/12/02 12:24:02 christos Exp $"); +__RCSID("$NetBSD: biosboot.c,v 1.21 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/stat.h> @@ -65,20 +65,13 @@ __RCSID("$NetBSD: biosboot.c,v 1.20 2015 #define DEFAULT_BOOTDIR "/usr/mdec" #define DEFAULT_BOOTCODE "gptmbr.bin" -static daddr_t start; -static uint64_t size; - -static char *bootpath; -static unsigned int entry; -static uint8_t *label; - static int cmd_biosboot(gpt_t, int, char *[]); static const char *biosboothelp[] = { - "[-c bootcode] [-i index] [-L label]", + "[-c bootcode] [-i index] [-L label]", #if notyet - "[-a alignment] [-b blocknr] [-i index] [-l label]", - "[-s size] [-t type]", + "[-a alignment] [-b blocknr] [-i index] [-l label]", + "[-s size] [-t type]", #endif }; @@ -92,25 +85,26 @@ struct gpt_cmd c_biosboot = { #define usage() gpt_usage(NULL, &c_biosboot) static struct mbr* -read_boot(gpt_t gpt) +read_boot(gpt_t gpt, const char *bootpath) { - int bfd, ret = 0; + int bfd, ret = -1; struct mbr *buf; struct stat st; + char *bp; buf = NULL; bfd = -1; if (bootpath == NULL) - bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE); + bp = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE); else if (*bootpath == '/') - bootpath = strdup(bootpath); + bp = strdup(bootpath); else { - if (asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0) - bootpath = NULL; + if (asprintf(&bp, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0) + bp = NULL; } - if (bootpath == NULL) { + if (bp == NULL) { gpt_warn(gpt, "Can't allocate memory for bootpath"); goto fail; } @@ -121,31 +115,31 @@ read_boot(gpt_t gpt) } - if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) { - gpt_warn(gpt, "Can't open `%s'", bootpath); + if ((bfd = open(bp, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) { + gpt_warn(gpt, "Can't open `%s'", bp); goto fail; } if (st.st_size != MBR_DSN_OFFSET) { gpt_warnx(gpt, "The bootcode in `%s' does not match the" - " expected size %u", bootpath, MBR_DSN_OFFSET); + " expected size %u", bp, MBR_DSN_OFFSET); goto fail; } if (read(bfd, buf, st.st_size) != st.st_size) { - gpt_warn(gpt, "Error reading from `%s'", bootpath); + gpt_warn(gpt, "Error reading from `%s'", bp); goto fail; } - ret++; - - fail: + ret = 0; +fail: if (bfd != -1) close(bfd); - if (ret == 0) { + if (ret == -1) { free(buf); buf = NULL; } + free(bp); return buf; } @@ -169,7 +163,8 @@ set_bootable(gpt_t gpt, map_t map, map_t } static int -biosboot(gpt_t gpt) +biosboot(gpt_t gpt, daddr_t start, uint64_t size, u_int entry, uint8_t *label, + const char *bootpath) { map_t mbrmap, m; struct mbr *mbr, *bootcode; @@ -194,7 +189,7 @@ biosboot(gpt_t gpt) /* * Update the boot code */ - if ((bootcode = read_boot(gpt)) == NULL) { + if ((bootcode = read_boot(gpt, bootpath)) == NULL) { gpt_warnx(gpt, "Error reading bootcode"); return -1; } @@ -256,9 +251,13 @@ cmd_biosboot(gpt_t gpt, int argc, char * #ifdef DIOCGWEDGEINFO struct dkwedge_info dkw; #endif - char *dev; int ch; gpt_t ngpt = gpt; + daddr_t start = 0; + uint64_t size = 0; + unsigned int entry = 0; + uint8_t *label = NULL; + const char *bootpath = NULL; while ((ch = getopt(argc, argv, "c:i:L:")) != -1) { switch(ch) { @@ -282,25 +281,21 @@ cmd_biosboot(gpt_t gpt, int argc, char * if (argc != optind) return usage(); - start = 0; - size = 0; - #ifdef DIOCGWEDGEINFO if ((gpt->sb.st_mode & S_IFMT) != S_IFREG && ioctl(gpt->fd, DIOCGWEDGEINFO, &dkw) != -1) { if (entry > 0) /* wedges and indexes are mutually exclusive */ return usage(); - dev = dkw.dkw_parent; start = dkw.dkw_offset; size = dkw.dkw_size; - ngpt = gpt_open(dev, gpt->flags, gpt->verbose, + ngpt = gpt_open(dkw.dkw_parent, gpt->flags, gpt->verbose, gpt->mediasz, gpt->secsz); if (ngpt == NULL) return -1; } #endif - biosboot(ngpt); + biosboot(ngpt, start, size, entry, label, bootpath); if (ngpt != gpt) gpt_close(ngpt); Index: src/sbin/gpt/remove.c diff -u src/sbin/gpt/remove.c:1.20 src/sbin/gpt/remove.c:1.21 --- src/sbin/gpt/remove.c:1.20 Tue Dec 1 14:25:24 2015 +++ src/sbin/gpt/remove.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: remove.c,v 1.20 2015/12/01 19:25:24 christos Exp $"); +__RCSID("$NetBSD: remove.c,v 1.21 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,8 +52,8 @@ __RCSID("$NetBSD: remove.c,v 1.20 2015/1 static int cmd_remove(gpt_t, int, char *[]); static const char *removehelp[] = { - "-a", - "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type]", + "-a", + "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type]", }; struct gpt_cmd c_remove = { Index: src/sbin/gpt/create.c diff -u src/sbin/gpt/create.c:1.16 src/sbin/gpt/create.c:1.17 --- src/sbin/gpt/create.c:1.16 Tue Dec 1 14:25:24 2015 +++ src/sbin/gpt/create.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: create.c,v 1.16 2015/12/01 19:25:24 christos Exp $"); +__RCSID("$NetBSD: create.c,v 1.17 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,13 +52,10 @@ __RCSID("$NetBSD: create.c,v 1.16 2015/1 #include "gpt.h" #include "gpt_private.h" -static int force; -static u_int parts; -static int primary_only; static int cmd_create(gpt_t, int, char *[]); static const char *createhelp[] = { - "[-fP] [-p <partitions>]", + "[-fP] [-p partitions]", }; struct gpt_cmd c_create = { @@ -72,7 +69,7 @@ struct gpt_cmd c_create = { static int -create(gpt_t gpt) +create(gpt_t gpt, u_int parts, int force, int primary_only) { off_t last = gpt_last(gpt); map_t map; @@ -128,8 +125,9 @@ static int cmd_create(gpt_t gpt, int argc, char *argv[]) { int ch; - - parts = 128; + int force = 0; + int primary_only = 0; + u_int parts = 128; while ((ch = getopt(argc, argv, "fPp:")) != -1) { switch(ch) { @@ -150,5 +148,5 @@ cmd_create(gpt_t gpt, int argc, char *ar if (argc != optind) return usage(); - return create(gpt); + return create(gpt, parts, force, primary_only); } Index: src/sbin/gpt/destroy.c diff -u src/sbin/gpt/destroy.c:1.9 src/sbin/gpt/destroy.c:1.10 --- src/sbin/gpt/destroy.c:1.9 Tue Dec 1 11:32:19 2015 +++ src/sbin/gpt/destroy.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: destroy.c,v 1.9 2015/12/01 16:32:19 christos Exp $"); +__RCSID("$NetBSD: destroy.c,v 1.10 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -49,12 +49,10 @@ __RCSID("$NetBSD: destroy.c,v 1.9 2015/1 #include "gpt.h" #include "gpt_private.h" -static int recoverable; -static int force; static int cmd_destroy(gpt_t, int, char *[]); static const char *destroyhelp[] = { - "[-rf]", + "[-rf]", }; struct gpt_cmd c_destroy = { @@ -68,7 +66,7 @@ struct gpt_cmd c_destroy = { static int -destroy(gpt_t gpt) +destroy(gpt_t gpt, int force, int recoverable) { map_t pri_hdr, sec_hdr; @@ -108,6 +106,8 @@ static int cmd_destroy(gpt_t gpt, int argc, char *argv[]) { int ch; + int recoverable = 0; + int force = 0; while ((ch = getopt(argc, argv, "fr")) != -1) { switch(ch) { @@ -125,5 +125,5 @@ cmd_destroy(gpt_t gpt, int argc, char *a if (argc != optind) return usage(); - return destroy(gpt); + return destroy(gpt, force, recoverable); } Index: src/sbin/gpt/set.c diff -u src/sbin/gpt/set.c:1.9 src/sbin/gpt/set.c:1.10 --- src/sbin/gpt/set.c:1.9 Tue Dec 1 14:25:24 2015 +++ src/sbin/gpt/set.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: set.c,v 1.9 2015/12/01 19:25:24 christos Exp $"); +__RCSID("$NetBSD: set.c,v 1.10 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,7 +52,7 @@ __RCSID("$NetBSD: set.c,v 1.9 2015/12/01 static int cmd_set(gpt_t, int, char *[]); static const char *sethelp[] = { - "-a attribute -i index", + "-a attribute -i index", }; struct gpt_cmd c_set = { Index: src/sbin/gpt/unset.c diff -u src/sbin/gpt/unset.c:1.9 src/sbin/gpt/unset.c:1.10 --- src/sbin/gpt/unset.c:1.9 Tue Dec 1 14:25:24 2015 +++ src/sbin/gpt/unset.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: unset.c,v 1.9 2015/12/01 19:25:24 christos Exp $"); +__RCSID("$NetBSD: unset.c,v 1.10 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,7 +52,7 @@ __RCSID("$NetBSD: unset.c,v 1.9 2015/12/ static int cmd_unset(gpt_t, int, char *[]); static const char *unsethelp[] = { - "-a attribute -i index", + "-a attribute -i index", }; struct gpt_cmd c_unset = { Index: src/sbin/gpt/header.c diff -u src/sbin/gpt/header.c:1.5 src/sbin/gpt/header.c:1.6 --- src/sbin/gpt/header.c:1.5 Tue Dec 1 11:32:19 2015 +++ src/sbin/gpt/header.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: header.c,v 1.5 2015/12/01 16:32:19 christos Exp $"); +__RCSID("$NetBSD: header.c,v 1.6 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,7 +52,7 @@ __RCSID("$NetBSD: header.c,v 1.5 2015/12 static int cmd_header(gpt_t, int, char *[]); static const char *headerhelp[] = { - "", + "", }; struct gpt_cmd c_header = { Index: src/sbin/gpt/label.c diff -u src/sbin/gpt/label.c:1.24 src/sbin/gpt/label.c:1.25 --- src/sbin/gpt/label.c:1.24 Tue Dec 1 23:07:11 2015 +++ src/sbin/gpt/label.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: label.c,v 1.24 2015/12/02 04:07:11 christos Exp $"); +__RCSID("$NetBSD: label.c,v 1.25 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -53,8 +53,9 @@ __RCSID("$NetBSD: label.c,v 1.24 2015/12 static int cmd_label(gpt_t, int, char *[]); static const char *labelhelp[] = { - "-a <-l label | -f file>", - "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] <-l label | -f file>", + "-a <-l label | -f file>", + "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] " + "<-l label | -f file>", }; struct gpt_cmd c_label = { Index: src/sbin/gpt/main.c diff -u src/sbin/gpt/main.c:1.4 src/sbin/gpt/main.c:1.5 --- src/sbin/gpt/main.c:1.4 Tue Dec 1 11:33:55 2015 +++ src/sbin/gpt/main.c Wed Dec 2 20:07:28 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.4 2015/12/01 16:33:55 christos Exp $ */ +/* $NetBSD: main.c,v 1.5 2015/12/03 01:07:28 christos Exp $ */ /*- * Copyright (c) 2002 Marcel Moolenaar @@ -34,7 +34,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: main.c,v 1.4 2015/12/01 16:33:55 christos Exp $"); +__RCSID("$NetBSD: main.c,v 1.5 2015/12/03 01:07:28 christos Exp $"); #endif #include <stdio.h> @@ -100,15 +100,15 @@ usage(void) { const char *p = getprogname(); const char *f = - "[-nrqv] [-m <mediasize>] [-s <sectorsize>]"; + "[-nrqv] [-m mediasize] [-s sectorsize]"; size_t i; if (strcmp(p, "gpt") == 0) fprintf(stderr, - "Usage: %s %s <command> [<args>] <device>\n", p, f); + "Usage: %s %s command device\n", p, f); else fprintf(stderr, - "Usage: %s %s <device> <command> [<args>]\n", p, f); + "Usage: %s %s device command\n", p, f); fprintf(stderr, "Commands:\n"); for (i = 0; i < __arraycount(cmdsw); i++) gpt_usage("\t", cmdsw[i]); Index: src/sbin/gpt/migrate.c diff -u src/sbin/gpt/migrate.c:1.25 src/sbin/gpt/migrate.c:1.26 --- src/sbin/gpt/migrate.c:1.25 Tue Dec 1 14:25:24 2015 +++ src/sbin/gpt/migrate.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: migrate.c,v 1.25 2015/12/01 19:25:24 christos Exp $"); +__RCSID("$NetBSD: migrate.c,v 1.26 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -76,14 +76,10 @@ __RCSID("$NetBSD: migrate.c,v 1.25 2015/ #define FREEBSD_FS_VINUM 14 #define FREEBSD_FS_ZFS 27 -static int force; -static int slice; -static u_int parts; - static int cmd_migrate(gpt_t, int, char *[]); static const char *migratehelp[] = { - "[-fs] [-p <partitions>]", + "[-fs] [-p partitions]", }; struct gpt_cmd c_migrate = { @@ -260,7 +256,7 @@ migrate_netbsd_disklabel(gpt_t gpt, off_ } static int -migrate(gpt_t gpt) +migrate(gpt_t gpt, u_int parts, int force, int slice) { off_t last = gpt_last(gpt); map_t map; @@ -348,8 +344,9 @@ static int cmd_migrate(gpt_t gpt, int argc, char *argv[]) { int ch; - - parts = 128; + int force = 0; + int slice = 0; + u_int parts = 128; /* Get the migrate options */ while ((ch = getopt(argc, argv, "fp:s")) != -1) { @@ -371,5 +368,5 @@ cmd_migrate(gpt_t gpt, int argc, char *a if (argc != optind) return usage(); - return migrate(gpt); + return migrate(gpt, parts, force, slice); } Index: src/sbin/gpt/recover.c diff -u src/sbin/gpt/recover.c:1.11 src/sbin/gpt/recover.c:1.12 --- src/sbin/gpt/recover.c:1.11 Wed Dec 2 15:42:07 2015 +++ src/sbin/gpt/recover.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: recover.c,v 1.11 2015/12/02 20:42:07 christos Exp $"); +__RCSID("$NetBSD: recover.c,v 1.12 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -49,12 +49,10 @@ __RCSID("$NetBSD: recover.c,v 1.11 2015/ #include "gpt.h" #include "gpt_private.h" -static int recoverable; - static int cmd_recover(gpt_t, int, char *[]); static const char *recoverhelp[] = { - "", + "", }; struct gpt_cmd c_recover = { @@ -158,7 +156,7 @@ recover_gpt_tbl(gpt_t gpt, int type, off } static int -recover(gpt_t gpt) +recover(gpt_t gpt, int recoverable) { uint64_t last; @@ -215,6 +213,7 @@ static int cmd_recover(gpt_t gpt, int argc, char *argv[]) { int ch; + int recoverable = 0; while ((ch = getopt(argc, argv, "r")) != -1) { switch(ch) { @@ -229,5 +228,5 @@ cmd_recover(gpt_t gpt, int argc, char *a if (argc != optind) return usage(); - return recover(gpt); + return recover(gpt, recoverable); } Index: src/sbin/gpt/resizedisk.c diff -u src/sbin/gpt/resizedisk.c:1.11 src/sbin/gpt/resizedisk.c:1.12 --- src/sbin/gpt/resizedisk.c:1.11 Wed Dec 2 15:09:54 2015 +++ src/sbin/gpt/resizedisk.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: resizedisk.c,v 1.11 2015/12/02 20:09:54 christos Exp $"); +__RCSID("$NetBSD: resizedisk.c,v 1.12 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/bootblock.h> @@ -50,12 +50,11 @@ __RCSID("$NetBSD: resizedisk.c,v 1.11 20 #include "gpt.h" #include "gpt_private.h" -static off_t sector, size; static int cmd_resizedisk(gpt_t, int, char *[]); static const char *resizediskhelp[] = { - "[-s size]", + "[-s size]", }; struct gpt_cmd c_resizedisk = { @@ -79,7 +78,7 @@ struct gpt_cmd c_resizedisk = { * - when shrinking, verify that table fits */ static int -resizedisk(gpt_t gpt) +resizedisk(gpt_t gpt, off_t sector, off_t size) { map_t mbrmap; struct gpt_hdr *hdr; @@ -241,6 +240,7 @@ static int cmd_resizedisk(gpt_t gpt, int argc, char *argv[]) { int ch; + off_t sector, size = 0; while ((ch = getopt(argc, argv, "s:")) != -1) { switch(ch) { @@ -259,5 +259,5 @@ cmd_resizedisk(gpt_t gpt, int argc, char if ((sector = gpt_check_ais(gpt, 0, ~0, size)) == -1) return -1; - return resizedisk(gpt); + return resizedisk(gpt, sector, size); } Index: src/sbin/gpt/type.c diff -u src/sbin/gpt/type.c:1.11 src/sbin/gpt/type.c:1.12 --- src/sbin/gpt/type.c:1.11 Tue Dec 1 23:06:10 2015 +++ src/sbin/gpt/type.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: type.c,v 1.11 2015/12/02 04:06:10 christos Exp $"); +__RCSID("$NetBSD: type.c,v 1.12 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,8 +52,8 @@ __RCSID("$NetBSD: type.c,v 1.11 2015/12/ static int cmd_type(gpt_t, int, char *[]); static const char *typehelp[] = { -"-a -T newtype", -"[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype", + "-a -T newtype", + "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype", }; struct gpt_cmd c_type = { @@ -80,6 +80,7 @@ cmd_type(gpt_t gpt, int argc, char *argv struct gpt_find find; memset(&find, 0, sizeof(find)); + gpt_uuid_copy(newtype, gpt_uuid_nil); find.msg = "type changed"; /* Get the type options */ Index: src/sbin/gpt/resize.c diff -u src/sbin/gpt/resize.c:1.18 src/sbin/gpt/resize.c:1.19 --- src/sbin/gpt/resize.c:1.18 Wed Dec 2 15:01:44 2015 +++ src/sbin/gpt/resize.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: resize.c,v 1.18 2015/12/02 20:01:44 christos Exp $"); +__RCSID("$NetBSD: resize.c,v 1.19 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -49,13 +49,10 @@ __RCSID("$NetBSD: resize.c,v 1.18 2015/1 #include "gpt.h" #include "gpt_private.h" -static off_t alignment, sectors, size; -static unsigned int entry; - static int cmd_resize(gpt_t, int, char *[]); static const char *resizehelp[] = { - "-i index [-a alignment] [-s size]", + "-i index [-a alignment] [-s size]", }; struct gpt_cmd c_resize = { @@ -68,7 +65,7 @@ struct gpt_cmd c_resize = { #define usage() gpt_usage(NULL, &c_resize) static int -resize(gpt_t gpt) +resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size) { map_t map; struct gpt_hdr *hdr; @@ -134,6 +131,8 @@ static int cmd_resize(gpt_t gpt, int argc, char *argv[]) { int ch; + off_t alignment = 0, sectors, size = 0; + unsigned int entry = 0; while ((ch = getopt(argc, argv, GPT_AIS)) != -1) { if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1) @@ -146,5 +145,5 @@ cmd_resize(gpt_t gpt, int argc, char *ar if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1) return -1; - return resize(gpt); + return resize(gpt, entry, alignment, sectors, size); } Index: src/sbin/gpt/restore.c diff -u src/sbin/gpt/restore.c:1.14 src/sbin/gpt/restore.c:1.15 --- src/sbin/gpt/restore.c:1.14 Wed Dec 2 17:32:04 2015 +++ src/sbin/gpt/restore.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: restore.c,v 1.14 2015/12/02 22:32:04 christos Exp $"); +__RCSID("$NetBSD: restore.c,v 1.15 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -52,16 +52,12 @@ __RCSID("$NetBSD: restore.c,v 1.14 2015/ #include "gpt.h" #include "gpt_private.h" -static int force; - static int cmd_restore(gpt_t, int, char *[]); static const char *restorehelp[] = { - "[-F] [-i <infile>]", + "[-F] [-i infile]", }; -static const char *infile = "/dev/stdin"; - struct gpt_cmd c_restore = { "restore", cmd_restore, @@ -195,7 +191,7 @@ restore_ent(gpt_t gpt, prop_dictionary_t } static int -restore(gpt_t gpt) +restore(gpt_t gpt, const char *infile, int force) { gpt_uuid_t gpt_guid, uuid; off_t firstdata, last, lastdata, gpe_start, gpe_end; @@ -233,7 +229,8 @@ restore(gpt_t gpt) map->map_type = MAP_TYPE_UNUSED; } - props = prop_dictionary_internalize_from_file(infile); + props = prop_dictionary_internalize_from_file( + strcmp(infile, "-") == 0 ? "/dev/stdin" : infile); if (props == NULL) { gpt_warnx(gpt, "Unable to read/parse backup file"); return -1; @@ -421,6 +418,8 @@ static int cmd_restore(gpt_t gpt, int argc, char *argv[]) { int ch; + int force = 0; + const char *infile = "-"; while ((ch = getopt(argc, argv, "Fi:")) != -1) { switch(ch) { @@ -438,5 +437,5 @@ cmd_restore(gpt_t gpt, int argc, char *a if (argc != optind) return usage(); - return restore(gpt); + return restore(gpt, infile, force); } Index: src/sbin/gpt/show.c diff -u src/sbin/gpt/show.c:1.27 src/sbin/gpt/show.c:1.28 --- src/sbin/gpt/show.c:1.27 Tue Dec 1 23:07:11 2015 +++ src/sbin/gpt/show.c Wed Dec 2 20:07:28 2015 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: show.c,v 1.27 2015/12/02 04:07:11 christos Exp $"); +__RCSID("$NetBSD: show.c,v 1.28 2015/12/03 01:07:28 christos Exp $"); #endif #include <sys/types.h> @@ -49,17 +49,16 @@ __RCSID("$NetBSD: show.c,v 1.27 2015/12/ #include "gpt.h" #include "gpt_private.h" -static int show_label = 0; -static int show_uuid = 0; -static int show_guid = 0; -static unsigned int entry = 0; - static int cmd_show(gpt_t, int, char *[]); static const char *showhelp[] = { - "[-glu] [-i index]", + "[-glu] [-i index]", }; +#define SHOW_UUID 1 +#define SHOW_GUID 2 +#define SHOW_LABEL 4 + struct gpt_cmd c_show = { "show", cmd_show, @@ -70,13 +69,14 @@ struct gpt_cmd c_show = { #define usage() gpt_usage(NULL, &c_show) static int -show(gpt_t gpt) +show(gpt_t gpt, int show) { off_t start; map_t m, p; struct mbr *mbr; struct gpt_ent *ent; unsigned int i; + char buf[128], *b = buf; uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; printf(" %*s", gpt->lbawidth, "start"); @@ -134,23 +134,21 @@ show(gpt_t gpt) case MAP_TYPE_GPT_PART: printf("GPT part "); ent = m->map_data; - if (show_label) { + if (show & SHOW_LABEL) { utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf)); - printf("- \"%s\"", (char *)utfbuf); - } else if (show_guid) { - char buf[128]; - gpt_uuid_snprintf( - buf, sizeof(buf), "%d", ent->ent_guid); - printf("- %s", buf); + b = (char *)buf; + } else if (show & SHOW_GUID) { + gpt_uuid_snprintf( buf, sizeof(buf), "%d", + ent->ent_guid); + } else if (show & SHOW_UUID) { + gpt_uuid_snprintf(buf, sizeof(buf), + "%d", ent->ent_type); } else { - char buf[128]; - if (show_uuid || gpt_uuid_snprintf(buf, - sizeof(buf), "%ls", ent->ent_type) == -1) - gpt_uuid_snprintf(buf, sizeof(buf), - "%d", ent->ent_type); - printf("- %s", buf); + gpt_uuid_snprintf(buf, sizeof(buf), "%ls", + ent->ent_type); } + printf("- %s", b); break; case MAP_TYPE_PMBR: printf("PMBR"); @@ -165,16 +163,31 @@ show(gpt_t gpt) return 0; } +static void +show_num(gpt_t gpt, const char *prompt, uintmax_t num) +{ +#ifdef HN_AUTOSCALE + char human_num[5]; + if (humanize_number(human_num, 5, (int64_t)(num * gpt->secsz), + "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0) + human_num[0] = '\0'; +#endif + printf("%s: %ju", prompt, num); +#ifdef HN_AUTOSCALE + if (human_num[0] != '\0') + printf("(%s)", human_num); +#endif + + printf("\n"); +} + static int -show_one(gpt_t gpt) +show_one(gpt_t gpt, unsigned int entry) { map_t m; struct gpt_ent *ent; char s1[128], s2[128]; uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; -#ifdef HN_AUTOSCALE - char human_num[5]; -#endif for (m = map_first(gpt); m != NULL; m = m->map_next) if (entry == m->map_index) @@ -186,25 +199,8 @@ show_one(gpt_t gpt) ent = m->map_data; printf("Details for index %d:\n", entry); -#ifdef HN_AUTOSCALE - if (humanize_number(human_num, 5, (int64_t)(m->map_start * gpt->secsz), - "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0) - human_num[0] = '\0'; - if (human_num[0] != '\0') - printf("Start: %llu (%s)\n", (long long)m->map_start, - human_num); - else -#endif - printf("Start: %llu\n", (long long)m->map_start); -#ifdef HN_AUTOSCALE - if (humanize_number(human_num, 5, (int64_t)(m->map_size * gpt->secsz), - "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0) - human_num[0] = '\0'; - if (human_num[0] != '\0') - printf("Size: %llu (%s)\n", (long long)m->map_size, human_num); - else -#endif - printf("Size: %llu\n", (long long)m->map_size); + show_num(gpt, "Start", m->map_start); + show_num(gpt, "Size", m->map_size); gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type); gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type); @@ -219,22 +215,22 @@ show_one(gpt_t gpt) printf("Label: %s\n", (char *)utfbuf); printf("Attributes:\n"); - if (ent->ent_attr == 0) + if (ent->ent_attr == 0) { printf(" None\n"); - else { - if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION) - printf(" required for platform to function\n"); - if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL) - printf(" UEFI won't recognize file system\n"); - if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE) - printf(" legacy BIOS boot partition\n"); - if (ent->ent_attr & GPT_ENT_ATTR_BOOTME) - printf(" indicates a bootable partition\n"); - if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) - printf(" attempt to boot this partition only once\n"); - if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED) - printf(" partition that was marked bootonce but failed to boot\n"); + return 0; } + if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION) + printf(" required for platform to function\n"); + if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL) + printf(" UEFI won't recognize file system\n"); + if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE) + printf(" legacy BIOS boot partition\n"); + if (ent->ent_attr & GPT_ENT_ATTR_BOOTME) + printf(" indicates a bootable partition\n"); + if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) + printf(" attempt to boot this partition only once\n"); + if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED) + printf(" partition was marked bootonce but failed to boot\n"); return 0; } @@ -242,21 +238,23 @@ static int cmd_show(gpt_t gpt, int argc, char *argv[]) { int ch; + int xshow = 0; + unsigned int entry = 0; while ((ch = getopt(argc, argv, "gi:lu")) != -1) { switch(ch) { case 'g': - show_guid = 1; + xshow |= SHOW_GUID; break; case 'i': if (gpt_entry_get(&entry) == -1) return usage(); break; case 'l': - show_label = 1; + xshow |= SHOW_LABEL; break; case 'u': - show_uuid = 1; + xshow |= SHOW_UUID; break; default: return usage(); @@ -266,5 +264,5 @@ cmd_show(gpt_t gpt, int argc, char *argv if (argc != optind) return usage(); - return entry > 0 ? show_one(gpt) : show(gpt); + return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow); }