eliminate static globals so that commands can be re-used.

This commit is contained in:
christos 2015-12-03 01:07:28 +00:00
parent 0bc066df68
commit da0a3b4c0b
18 changed files with 178 additions and 187 deletions

View File

@ -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,10 +51,6 @@ __RCSID("$NetBSD: add.c,v 1.37 2015/12/02 11:20:34 jnemeth Exp $");
#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[] = {
@ -84,7 +80,8 @@ ent_set(struct gpt_ent *ent, const map_t map, const gpt_uuid_t xtype,
}
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);
}

View File

@ -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,8 +51,6 @@ __RCSID("$NetBSD: backup.c,v 1.13 2015/12/02 12:36:53 christos Exp $");
#include "gpt.h"
#include "gpt_private.h"
static const char *outfile = "/dev/stdout";
static const char *backuphelp[] = {
"[-o outfile]",
};
@ -241,7 +239,7 @@ store_tbl(gpt_t gpt, const map_t m, prop_dictionary_t *type_dict)
}
static int
backup(gpt_t gpt)
backup(gpt_t gpt, const char *outfile)
{
map_t m;
struct mbr *mbr;
@ -309,11 +307,13 @@ 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);
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 *argv[])
if (argc != optind)
return usage();
return backup(gpt);
return backup(gpt, outfile);
}

View File

@ -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,13 +65,6 @@ __RCSID("$NetBSD: biosboot.c,v 1.20 2015/12/02 12:24:02 christos Exp $");
#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[] = {
@ -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++;
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 tbl, unsigned int i)
}
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 *argv[])
#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 *argv[])
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);

View File

@ -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/12/01 19:25:24 christos Exp $");
#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 *argv[])
if (argc != optind)
return usage();
return create(gpt);
return create(gpt, parts, force, primary_only);
}

View File

@ -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,8 +49,6 @@ __RCSID("$NetBSD: destroy.c,v 1.9 2015/12/01 16:32:19 christos Exp $");
#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[] = {
@ -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 *argv[])
if (argc != optind)
return usage();
return destroy(gpt);
return destroy(gpt, force, recoverable);
}

View File

@ -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>

View File

@ -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>
@ -54,7 +54,8 @@ 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>",
"[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] "
"<-l label | -f file>",
};
struct gpt_cmd c_label = {

View File

@ -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]);

View File

@ -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/12/01 19:25:24 christos Exp $");
#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_t start, struct gpt_ent *ent)
}
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 *argv[])
if (argc != optind)
return usage();
return migrate(gpt);
return migrate(gpt, parts, force, slice);
}

View File

@ -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,8 +49,6 @@ __RCSID("$NetBSD: recover.c,v 1.11 2015/12/02 20:42:07 christos Exp $");
#include "gpt.h"
#include "gpt_private.h"
static int recoverable;
static int cmd_recover(gpt_t, int, char *[]);
static const char *recoverhelp[] = {
@ -158,7 +156,7 @@ recover_gpt_tbl(gpt_t gpt, int type, off_t start)
}
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 *argv[])
if (argc != optind)
return usage();
return recover(gpt);
return recover(gpt, recoverable);
}

View File

@ -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>

View File

@ -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,9 +49,6 @@ __RCSID("$NetBSD: resize.c,v 1.18 2015/12/02 20:01:44 christos Exp $");
#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[] = {
@ -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 *argv[])
if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
return -1;
return resize(gpt);
return resize(gpt, entry, alignment, sectors, size);
}

View File

@ -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,7 +50,6 @@ __RCSID("$NetBSD: resizedisk.c,v 1.11 2015/12/02 20:09:54 christos Exp $");
#include "gpt.h"
#include "gpt_private.h"
static off_t sector, size;
static int cmd_resizedisk(gpt_t, int, char *[]);
@ -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 *argv[])
if ((sector = gpt_check_ais(gpt, 0, ~0, size)) == -1)
return -1;
return resizedisk(gpt);
return resizedisk(gpt, sector, size);
}

View File

@ -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/12/02 22:32:04 christos Exp $");
#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 gpt_dict, void *secbuf, u_int gpt_size,
}
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 *argv[])
if (argc != optind)
return usage();
return restore(gpt);
return restore(gpt, infile, force);
}

View File

@ -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>

View File

@ -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/02 04:07:11 christos Exp $");
#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]",
};
#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);
} else {
char buf[128];
if (show_uuid || gpt_uuid_snprintf(buf,
sizeof(buf), "%ls", ent->ent_type) == -1)
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);
printf("- %s", buf);
} else {
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,9 +215,10 @@ 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 {
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)
@ -233,8 +230,7 @@ show_one(gpt_t gpt)
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");
}
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);
}

View File

@ -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>
@ -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 */

View File

@ -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>