Home | History | Annotate | Line # | Download | only in gpt
recover.c revision 1.8
      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.8 2015/12/01 09:05:33 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 const char recovermsg[] = "recover";
     55 
     56 static int
     57 usage_recover(void)
     58 {
     59 
     60 	fprintf(stderr,
     61 	    "usage: %s %s\n", getprogname(), recovermsg);
     62 	return -1;
     63 }
     64 
     65 static int
     66 recover(gpt_t gpt)
     67 {
     68 	uint64_t last;
     69 	struct gpt_hdr *hdr;
     70 
     71 	if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
     72 		gpt_warnx(gpt, "Device contains an MBR");
     73 		return -1;
     74 	}
     75 
     76 	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
     77 	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
     78 	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
     79 	gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
     80 
     81 	if (gpt->gpt == NULL && gpt->tpg == NULL) {
     82 		gpt_warnx(gpt, "No primary or secondary GPT headers, "
     83 		    "can't recover");
     84 		return -1;
     85 	}
     86 	if (gpt->tbl == NULL && gpt->lbt == NULL) {
     87 		gpt_warnx(gpt, "No primary or secondary GPT tables, "
     88 		    "can't recover");
     89 		return -1;
     90 	}
     91 
     92 	last = gpt->mediasz / gpt->secsz - 1LL;
     93 
     94 	if (gpt->gpt != NULL &&
     95 	    ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
     96 		gpt_warnx(gpt, "Media size has changed, please use "
     97 		   "'gpt resizedisk'");
     98 		return -1;
     99 	}
    100 
    101 	if (gpt->tbl != NULL && gpt->lbt == NULL) {
    102 		gpt->lbt = map_add(gpt, last - gpt->tbl->map_size,
    103 		    gpt->tbl->map_size, MAP_TYPE_SEC_GPT_TBL,
    104 		    gpt->tbl->map_data);
    105 		if (gpt->lbt == NULL) {
    106 			gpt_warnx(gpt, "Adding secondary GPT table failed");
    107 			return -1;
    108 		}
    109 		if (gpt_write(gpt, gpt->lbt) == -1) {
    110 			gpt_warnx(gpt, "Writing secondary GPT table failed");
    111 			return -1;
    112 		}
    113 		gpt_msg(gpt, "Recovered secondary GPT table from primary");
    114 	} else if (gpt->tbl == NULL && gpt->lbt != NULL) {
    115 		gpt->tbl = map_add(gpt, 2LL, gpt->lbt->map_size,
    116 		    MAP_TYPE_PRI_GPT_TBL, gpt->lbt->map_data);
    117 		if (gpt->tbl == NULL) {
    118 			gpt_warnx(gpt, "Adding primary GPT table failed");
    119 			return -1;
    120 		}
    121 		if (gpt_write(gpt, gpt->tbl) == -1) {
    122 			gpt_warnx(gpt, "Writing primary GPT table failed");
    123 			return -1;
    124 		}
    125 		gpt_msg(gpt, "Recovered primary GPT table from secondary");
    126 	}
    127 
    128 	if (gpt->gpt != NULL && gpt->tpg == NULL) {
    129 		gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
    130 		    calloc(1, gpt->secsz));
    131 		if (gpt->tpg == NULL) {
    132 			gpt_warnx(gpt, "Adding secondary GPT header failed");
    133 			return -1;
    134 		}
    135 		memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
    136 		hdr = gpt->tpg->map_data;
    137 		hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
    138 		hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
    139 		hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
    140 		hdr->hdr_crc_self = 0;
    141 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    142 		if (gpt_write(gpt, gpt->tpg) == -1) {
    143 			gpt_warnx(gpt, "Writing secondary GPT header failed");
    144 			return -1;
    145 		}
    146 		gpt_msg(gpt, "Recovered secondary GPT header from primary");
    147 	} else if (gpt->gpt == NULL && gpt->tpg != NULL) {
    148 		gpt->gpt = map_add(gpt, 1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
    149 		    calloc(1, gpt->secsz));
    150 		if (gpt->gpt == NULL) {
    151 			gpt_warnx(gpt, "Adding primary GPT header failed");
    152 			return -1;
    153 		}
    154 		memcpy(gpt->gpt->map_data, gpt->tpg->map_data, gpt->secsz);
    155 		hdr = gpt->gpt->map_data;
    156 		hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
    157 		hdr->hdr_lba_alt = htole64(gpt->tpg->map_start);
    158 		hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
    159 		hdr->hdr_crc_self = 0;
    160 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    161 		if (gpt_write(gpt, gpt->gpt) == -1) {
    162 			gpt_warnx(gpt, "Writing primary GPT header failed");
    163 			return -1;
    164 		}
    165 		gpt_msg(gpt, "Recovered primary GPT header from secondary");
    166 	}
    167 	return 0;
    168 }
    169 
    170 int
    171 cmd_recover(gpt_t gpt, int argc, char *argv[])
    172 {
    173 	int ch;
    174 
    175 	while ((ch = getopt(argc, argv, "r")) != -1) {
    176 		switch(ch) {
    177 		case 'r':
    178 			recoverable = 1;
    179 			break;
    180 		default:
    181 			return usage_recover();
    182 		}
    183 	}
    184 
    185 	if (argc != optind)
    186 		usage_recover();
    187 
    188 	return recover(gpt);
    189 }
    190