From: krw Date: Fri, 10 Nov 2023 15:41:11 +0000 (+0000) Subject: Enhance 'flag' to accept hex values in addition to the current 0 X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=851351478282e4dd75b05b8351e1fc4a91b967b2;p=openbsd Enhance 'flag' to accept hex values in addition to the current 0 .. INT64_MAX decimal values.. Easier to specify the 64 bits of GPT partition attributes and 0x8000000000000000 (a.k.a. MS_NOAUTOMOUNT, a.k.a. 1 << 63) becomes accessable. Prompted by bug report and testing by Philippe Meunier. Thanks! ok dlg@ --- diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index fa027ab5009..895a1781d71 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.176 2023/04/10 19:44:43 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.177 2023/11/10 15:41:11 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,7 @@ int edit(const int, struct mbr *); int gsetpid(const int); int setpid(const int, struct mbr *); int parsepn(const char *); +int parseflag(const char *, uint64_t *); int ask_num(const char *, int, int, int); int ask_pid(const int); @@ -172,6 +174,38 @@ parsepn(const char *pnstr) return pn; } +int +parseflag(const char *flagstr, uint64_t *flagvalue) +{ + const char *errstr; + char *ep; + uint64_t val; + + if (flagstr[0] == '0' && (flagstr[1] == 'x' || flagstr[1] == 'X')) { + errno = 0; + val = strtoull(flagstr, &ep, 16); + if (errno || ep == flagstr || *ep != '\0' || + (gh.gh_sig != GPTSIGNATURE && val > 0xff)) { + printf("flag value is invalid: %s\n", flagstr); + return -1; + } + goto done; + } + + if (gh.gh_sig == GPTSIGNATURE) + val = strtonum(flagstr, 0, INT64_MAX, &errstr); + else + val = strtonum(flagstr, 0, 0xff, &errstr); + if (errstr) { + printf("flag value is %s: %s\n", errstr, flagstr); + return -1; + } + + done: + *flagvalue = val; + return 0; +} + int edit(const int pn, struct mbr *mbr) { @@ -453,9 +487,8 @@ int Xflag(const char *args, struct mbr *mbr) { char lbuf[LINEBUFSZ]; - const char *errstr; char *part, *flag; - long long val = -1; + uint64_t val; int i, pn; if (strlcpy(lbuf, args, sizeof(lbuf)) >= sizeof(lbuf)) { @@ -471,14 +504,8 @@ Xflag(const char *args, struct mbr *mbr) return CMD_CONT; if (flag != NULL) { - if (gh.gh_sig == GPTSIGNATURE) - val = strtonum(flag, 0, INT64_MAX, &errstr); - else - val = strtonum(flag, 0, 0xff, &errstr); - if (errstr) { - printf("flag value is %s: %s.\n", errstr, flag); + if (parseflag(flag, &val) == -1) return CMD_CONT; - } if (gh.gh_sig == GPTSIGNATURE) gp[pn].gp_attrs = val; else diff --git a/sbin/fdisk/fdisk.8 b/sbin/fdisk/fdisk.8 index 52fe8b47005..6a265a2cb69 100644 --- a/sbin/fdisk/fdisk.8 +++ b/sbin/fdisk/fdisk.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fdisk.8,v 1.118 2023/05/07 13:03:03 krw Exp $ +.\" $OpenBSD: fdisk.8,v 1.119 2023/11/10 15:41:11 krw Exp $ .\" .\" .\" Copyright (c) 1997 Tobias Weingartner @@ -15,7 +15,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: May 7 2023 $ +.Dd $Mdocdate: November 10 2023 $ .Dt FDISK 8 .Os .Sh NAME @@ -320,23 +320,20 @@ The special size value .Sq * causes the partition to be sized to use the remainder of the disk. .It Cm flag Ar # Op Ar value -Sets the partition entry's flag value. +Set the partition's flag (MBR) or attribute (GPT) value. +.Ar value +can be a positive integer or a hex string. +An MBR partition will accept values from 0 to 0xff. +A GPT partition will accept values from 0 to 0xfffffffffffffff. If .Ar value -is not provided the partition entry is flagged as being bootable -and all other partition entries are flagged as not bootable. -Partition entries flagged as bootable are printed with a +is not provided the partition's bootable flag is set +and all other partitions have their bootable flags reset. +MBR partitions with the bootable flag set are printed with a .Sq * prefix. -.Pp -If -.Ar value -is provided the partition entry's flag value is set to -.Ar value -and no other partition entry is modified. -A -.Ar value -of 0 is used to clear the bootable flag. +GPT partitions with the bootable flag set display 'bootable' in +their attributes list. .It Cm update Updates the MBR bootcode. If a bootcode file is present (see