From 6997f77706a8c518a9c9cd84e1291b93f2065cc4 Mon Sep 17 00:00:00 2001 From: Kenneth R Westerback Date: Tue, 26 Jan 2016 02:38:06 +0000 Subject: Simplify logic of read_partition_map(), junking retries with various sizes of logical block. Rely on read_dpme() to do the correct thing. --- sbin/pdisk/partition_map.c | 83 +++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 56 deletions(-) (limited to 'sbin/pdisk') diff --git a/sbin/pdisk/partition_map.c b/sbin/pdisk/partition_map.c index dd75ec753e8..b79e7ce2941 100644 --- a/sbin/pdisk/partition_map.c +++ b/sbin/pdisk/partition_map.c @@ -1,4 +1,4 @@ -/* $OpenBSD: partition_map.c,v 1.64 2016/01/25 23:43:20 krw Exp $ */ +/* $OpenBSD: partition_map.c,v 1.65 2016/01/26 02:38:05 krw Exp $ */ /* * partition_map.c - partition map routines @@ -108,7 +108,7 @@ open_partition_map(int fd, char *name, uint64_t mediasz, uint32_t sectorsz) return NULL; } - if (read_partition_map(map) != -1) + if (read_partition_map(map) == 0) return map; if (!lflag) { @@ -149,69 +149,40 @@ int read_partition_map(struct partition_map_header *map) { struct dpme *dpme; - double d; - int ix, old_logical; + int ix; uint32_t limit; - dpme = malloc(sizeof(struct dpme)); - if (dpme == NULL) { - warn("can't allocate memory for disk buffers"); - return -1; - } - if (read_dpme(map->fd, 1, dpme) == 0) { - warnx("Can't read block 1 from '%s'", map->name); - free(dpme); - return -1; - } else if (dpme->dpme_signature != DPME_SIGNATURE) { - old_logical = map->logical_block; - map->logical_block = 512; - while (map->logical_block <= map->physical_block) { - if (read_dpme(map->fd, 1, dpme) == 0) { - warnx("Can't read block 1 from '%s'", - map->name); - free(dpme); - return -1; - } else if (dpme->dpme_signature == DPME_SIGNATURE) { - d = map->media_size; - map->media_size = (d * old_logical) / - map->logical_block; - break; - } - map->logical_block *= 2; - } - if (map->logical_block > map->physical_block) { - warnx("No valid block 1 on '%s'", map->name); - free(dpme); - return -1; - } - } - limit = dpme->dpme_map_entries; - ix = 1; - while (1) { - if (add_data_to_map(dpme, ix, map) == 0) { - free(dpme); - return -1; - } - if (ix >= limit) { - break; - } else { - ix++; - } - + limit = 1; /* There has to be at least one, which has actual value. */ + for (ix = 1; ix <= limit; ix++) { dpme = malloc(sizeof(struct dpme)); if (dpme == NULL) { - warn("can't allocate memory for disk buffers"); - return -1; + warn("can't allocate memory for partition entry"); + return 1; } if (read_dpme(map->fd, ix, dpme) == 0) { warnx("Can't read block %u from '%s'", ix, map->name); free(dpme); - return -1; - } else if ((dpme->dpme_signature != DPME_SIGNATURE) || - (dpme->dpme_map_entries != limit)) { - warnx("Bad dpme in block %u from '%s'", ix, map->name); + return 1; + } + if (dpme->dpme_signature != DPME_SIGNATURE) { + warnx("Invalid signature on block %d. Expected %x, " + "got %x", ix, DPME_SIGNATURE, + dpme->dpme_signature); + free(dpme); + return 1; + } + if (ix == 1) + limit = dpme->dpme_map_entries; + if (limit != dpme->dpme_map_entries) { + warnx("Invalid entry count on block %d. " + "Expected %d, got %d", ix, limit, + dpme->dpme_map_entries); + free(dpme); + return 1; + } + if (add_data_to_map(dpme, ix, map) == 0) { free(dpme); - return -1; + return 1; } } return 0; -- cgit v1.2.3