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