Home | History | Annotate | Line # | Download | only in gpt
recover.c revision 1.11
      1 /*-
      2  * Copyright (c) 2002 Marcel Moolenaar
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #if HAVE_NBTOOL_CONFIG_H
     28 #include "nbtool_config.h"
     29 #endif
     30 
     31 #include <sys/cdefs.h>
     32 #ifdef __FBSDID
     33 __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: recover.c,v 1.11 2015/12/02 20:42:07 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 
     41 #include <err.h>
     42 #include <stddef.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "map.h"
     49 #include "gpt.h"
     50 #include "gpt_private.h"
     51 
     52 static int recoverable;
     53 
     54 static int cmd_recover(gpt_t, int, char *[]);
     55 
     56 static const char *recoverhelp[] = {
     57     "",
     58 };
     59 
     60 struct gpt_cmd c_recover = {
     61 	"recover",
     62 	cmd_recover,
     63 	recoverhelp, __arraycount(recoverhelp),
     64 	0,
     65 };
     66 
     67 #define usage() gpt_usage(NULL, &c_recover)
     68 
     69 static int
     70 recover_gpt_hdr(gpt_t gpt, int type, off_t last)
     71 {
     72 	const char *name, *origname;
     73 	void *p;
     74 	map_t *dgpt, dtbl, sgpt, stbl;
     75 	struct gpt_hdr *hdr;
     76 
     77 	switch (type) {
     78 	case MAP_TYPE_PRI_GPT_HDR:
     79 		dgpt = &gpt->gpt;
     80 		dtbl = gpt->tbl;
     81 		sgpt = gpt->tpg;
     82 		stbl = gpt->lbt;
     83 		origname = "secondary";
     84 		name = "primary";
     85 		break;
     86 	case MAP_TYPE_SEC_GPT_HDR:
     87 		dgpt = &gpt->tpg;
     88 		dtbl = gpt->lbt;
     89 		sgpt = gpt->gpt;
     90 		stbl = gpt->tbl;
     91 		origname = "primary";
     92 		name = "secondary";
     93 		break;
     94 	default:
     95 		gpt_warn(gpt, "Bad table type %d", type);
     96 		return -1;
     97 	}
     98 
     99 	if ((p = calloc(1, gpt->secsz)) == NULL) {
    100 		gpt_warn(gpt, "Cannot allocate %s GPT header", name);
    101 		return -1;
    102 	}
    103 	if ((*dgpt = map_add(gpt, last, 1LL, type, p)) == NULL) {
    104 		gpt_warnx(gpt, "Cannot add %s GPT header", name);
    105 		return -1;
    106 	}
    107 	memcpy((*dgpt)->map_data, sgpt->map_data, gpt->secsz);
    108 	hdr = (*dgpt)->map_data;
    109 	hdr->hdr_lba_self = htole64((*dgpt)->map_start);
    110 	hdr->hdr_lba_alt = htole64(stbl->map_start);
    111 	hdr->hdr_lba_table = htole64(dtbl->map_start);
    112 	hdr->hdr_crc_self = 0;
    113 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    114 	if (gpt_write(gpt, *dgpt) == -1) {
    115 		gpt_warnx(gpt, "Writing %s GPT header failed", name);
    116 		return -1;
    117 	}
    118 	gpt_msg(gpt, "Recovered %s GPT header from %s", name, origname);
    119 	return 0;
    120 }
    121 
    122 static int
    123 recover_gpt_tbl(gpt_t gpt, int type, off_t start)
    124 {
    125 	const char *name, *origname;
    126 	map_t *dtbl, stbl;
    127 
    128 	switch (type) {
    129 	case MAP_TYPE_PRI_GPT_TBL:
    130 		dtbl = &gpt->tbl;
    131 		stbl = gpt->lbt;
    132 		origname = "secondary";
    133 		name = "primary";
    134 		break;
    135 	case MAP_TYPE_SEC_GPT_TBL:
    136 		dtbl = &gpt->lbt;
    137 		stbl = gpt->tbl;
    138 		origname = "primary";
    139 		name = "secondary";
    140 		break;
    141 	default:
    142 		gpt_warn(gpt, "Bad table type %d", type);
    143 		return -1;
    144 	}
    145 
    146 	// XXX: non allocated memory
    147 	*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data);
    148 	if (*dtbl == NULL) {
    149 		gpt_warnx(gpt, "Adding %s GPT table failed", name);
    150 		return -1;
    151 	}
    152 	if (gpt_write(gpt, *dtbl) == -1) {
    153 		gpt_warnx(gpt, "Writing %s GPT table failed", name);
    154 		return -1;
    155 	}
    156 	gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
    157 	return 0;
    158 }
    159 
    160 static int
    161 recover(gpt_t gpt)
    162 {
    163 	uint64_t last;
    164 
    165 	if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
    166 		gpt_warnx(gpt, "Device contains an MBR");
    167 		return -1;
    168 	}
    169 
    170 	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
    171 	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
    172 	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
    173 	gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
    174 
    175 	if (gpt->gpt == NULL && gpt->tpg == NULL) {
    176 		gpt_warnx(gpt, "No primary or secondary GPT headers, "
    177 		    "can't recover");
    178 		return -1;
    179 	}
    180 	if (gpt->tbl == NULL && gpt->lbt == NULL) {
    181 		gpt_warnx(gpt, "No primary or secondary GPT tables, "
    182 		    "can't recover");
    183 		return -1;
    184 	}
    185 
    186 	last = gpt->mediasz / gpt->secsz - 1LL;
    187 
    188 	if (gpt->gpt != NULL &&
    189 	    ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
    190 		gpt_warnx(gpt, "Media size has changed, please use "
    191 		   "'%s resizedisk'", getprogname());
    192 		return -1;
    193 	}
    194 
    195 	if (gpt->tbl != NULL && gpt->lbt == NULL) {
    196 		if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
    197 		    last - gpt->tbl->map_size) == -1)
    198 			return -1;
    199 	} else if (gpt->tbl == NULL && gpt->lbt != NULL) {
    200 		if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
    201 			return -1;
    202 	}
    203 
    204 	if (gpt->gpt != NULL && gpt->tpg == NULL) {
    205 		if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR, last) == -1)
    206 			return -1;
    207 	} else if (gpt->gpt == NULL && gpt->tpg != NULL) {
    208 		if (recover_gpt_hdr(gpt, MAP_TYPE_PRI_GPT_HDR, 1LL) == -1)
    209 			return -1;
    210 	}
    211 	return 0;
    212 }
    213 
    214 static int
    215 cmd_recover(gpt_t gpt, int argc, char *argv[])
    216 {
    217 	int ch;
    218 
    219 	while ((ch = getopt(argc, argv, "r")) != -1) {
    220 		switch(ch) {
    221 		case 'r':
    222 			recoverable = 1;
    223 			break;
    224 		default:
    225 			return usage();
    226 		}
    227 	}
    228 
    229 	if (argc != optind)
    230 		return usage();
    231 
    232 	return recover(gpt);
    233 }
    234