summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2021-07-18 15:28:38 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2021-07-18 15:28:38 +0000
commitbc5fd11d956d64ea44db6f8be2f43820164183ba (patch)
tree1d2ca84ebe3b862d9006076e07b0c249822c5f54
parentea9ac3a8a63c9d680d3eb2fbae55b8944066b478 (diff)
Make GPT_read() obtain and validate the on-disk MBR itself, via
MBR_read(), and report success/failure. Simplifies logic and makes clearer that the protective MBR is a required part of a GPT. With the standardization on 0/-1 return values for helper functions, rename 'valid' to 'error' in GPT_read() to make logic less mind bending. No functional change.
-rw-r--r--sbin/fdisk/fdisk.c9
-rw-r--r--sbin/fdisk/gpt.c35
-rw-r--r--sbin/fdisk/gpt.h4
-rw-r--r--sbin/fdisk/user.c24
4 files changed, 34 insertions, 38 deletions
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c
index 22e2448cf4a..b92c4e12268 100644
--- a/sbin/fdisk/fdisk.c
+++ b/sbin/fdisk/fdisk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fdisk.c,v 1.125 2021/07/18 12:41:00 krw Exp $ */
+/* $OpenBSD: fdisk.c,v 1.126 2021/07/18 15:28:37 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -167,11 +167,6 @@ main(int argc, char *argv[])
if (error)
errx(1, "Can't read MBR!");
- /* Get the GPT if present. Either primary or secondary is ok. */
- efi = MBR_protective_mbr(&mbr);
- if (efi != -1)
- GPT_read(ANYGPT);
-
/* Create initial/default MBR. */
if (mbrfile == NULL) {
memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr));
@@ -195,7 +190,7 @@ main(int argc, char *argv[])
query = NULL;
if (A_flag) {
- if (letoh64(gh.gh_sig) != GPTSIGNATURE)
+ if (GPT_read(ANYGPT))
errx(1, "-A requires a valid GPT");
else {
initial_mbr = mbr; /* Keep current MBR. */
diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c
index 3ed8662cc61..0252b7f83f7 100644
--- a/sbin/fdisk/gpt.c
+++ b/sbin/fdisk/gpt.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gpt.c,v 1.45 2021/07/16 13:29:49 krw Exp $ */
+/* $OpenBSD: gpt.c,v 1.46 2021/07/18 15:28:37 krw Exp $ */
/*
* Copyright (c) 2015 Markus Muller <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
@@ -30,6 +30,7 @@
#include "part.h"
#include "disk.h"
+#include "mbr.h"
#include "misc.h"
#include "gpt.h"
@@ -202,35 +203,45 @@ get_partition_table(void)
return 0;
}
-void
+int
GPT_read(const int which)
{
- int valid;
+ struct mbr mbr;
+ int error;
+
+ error = MBR_read(0, 0, &mbr);
+ if (error == 0)
+ error = MBR_protective_mbr(&mbr);
+ if (error)
+ goto done;
switch (which) {
case PRIMARYGPT:
- valid = get_header(GPTSECTOR);
+ error = get_header(GPTSECTOR);
break;
case SECONDARYGPT:
- valid = get_header(DL_GETDSIZE(&dl) - 1);
+ error = get_header(DL_GETDSIZE(&dl) - 1);
break;
case ANYGPT:
- valid = get_header(GPTSECTOR);
- if (valid != 0 || get_partition_table() != 0)
- valid = get_header(DL_GETDSIZE(&dl) - 1);
+ error = get_header(GPTSECTOR);
+ if (error != 0 || get_partition_table() != 0)
+ error = get_header(DL_GETDSIZE(&dl) - 1);
break;
default:
- return;
+ return -1;
}
- if (valid == 0)
- valid = get_partition_table();
+ if (error == 0)
+ error = get_partition_table();
- if (valid != 0) {
+ done:
+ if (error != 0) {
/* No valid GPT found. Zap any artifacts. */
memset(&gh, 0, sizeof(gh));
memset(&gp, 0, sizeof(gp));
}
+
+ return error;
}
void
diff --git a/sbin/fdisk/gpt.h b/sbin/fdisk/gpt.h
index 0ea4968f42b..ada2b3356f7 100644
--- a/sbin/fdisk/gpt.h
+++ b/sbin/fdisk/gpt.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: gpt.h,v 1.16 2021/07/15 21:58:02 krw Exp $ */
+/* $OpenBSD: gpt.h,v 1.17 2021/07/18 15:28:37 krw Exp $ */
/*
* Copyright (c) 2015 Markus Muller <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
@@ -16,7 +16,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-void GPT_read(const int);
+int GPT_read(const int);
int GPT_get_lba_start(const unsigned int);
int GPT_get_lba_end(const unsigned int);
diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c
index af5ac347df0..cd8b8ea606f 100644
--- a/sbin/fdisk/user.c
+++ b/sbin/fdisk/user.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: user.c,v 1.64 2021/07/18 12:41:00 krw Exp $ */
+/* $OpenBSD: user.c,v 1.65 2021/07/18 15:28:37 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -73,13 +73,8 @@ USER_edit(const uint64_t lba_self, const uint64_t lba_firstembr)
if (error == -1)
goto done;
- if (editlevel == 1) {
- memset(&gh, 0, sizeof(gh));
- memset(&gp, 0, sizeof(gp));
- efi = MBR_protective_mbr(&mbr);
- if (efi != -1)
- GPT_read(ANYGPT);
- }
+ if (editlevel == 1)
+ GPT_read(ANYGPT);
printf("Enter 'help' for information\n");
@@ -140,7 +135,7 @@ USER_print_disk(const int verbosity)
{
struct mbr mbr;
uint64_t lba_self, lba_firstembr;
- int i, efi, error;
+ int i, error;
lba_self = lba_firstembr = 0;
@@ -149,19 +144,14 @@ USER_print_disk(const int verbosity)
if (error == -1)
break;
if (lba_self == 0) {
- efi = MBR_protective_mbr(&mbr);
- if (efi == -1) {
- /* No valid 0xEE partition means no GPT. */
+ if (GPT_read(ANYGPT)) {
if (verbosity == VERBOSE) {
printf("Primary GPT:\nNot Found\n");
printf("\nSecondary GPT:\nNot Found\n");
}
} else if (verbosity == TERSE) {
- /* Should already have read one of Primary/Secondary GPT. */
- if (letoh64(gh.gh_sig) == GPTSIGNATURE) {
- GPT_print("s", verbosity);
- return;
- }
+ GPT_print("s", verbosity);
+ return;
} else {
/*. Read & print both primary and secondary GPT. */
printf("Primary GPT:\n");