Home | History | Annotate | Line # | Download | only in gpt
recover.c revision 1.2
      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 #include <sys/cdefs.h>
     28 #ifdef __FBSDID
     29 __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
     30 #endif
     31 #ifdef __RCSID
     32 __RCSID("$NetBSD: recover.c,v 1.2 2006/10/15 22:36:29 christos Exp $");
     33 #endif
     34 
     35 #include <sys/types.h>
     36 
     37 #include <err.h>
     38 #include <stddef.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include "map.h"
     45 #include "gpt.h"
     46 
     47 static int recoverable;
     48 
     49 static void
     50 usage_recover(void)
     51 {
     52 
     53 	fprintf(stderr,
     54 	    "usage: %s device ...\n", getprogname());
     55 	exit(1);
     56 }
     57 
     58 static void
     59 recover(int fd)
     60 {
     61 	off_t last;
     62 	map_t *gpt, *tpg;
     63 	map_t *tbl, *lbt;
     64 	struct gpt_hdr *hdr;
     65 
     66 	if (map_find(MAP_TYPE_MBR) != NULL) {
     67 		warnx("%s: error: device contains a MBR", device_name);
     68 		return;
     69 	}
     70 
     71 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     72 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     73 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     74 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     75 
     76 	if (gpt == NULL && tpg == NULL) {
     77 		warnx("%s: no primary or secondary GPT headers, can't recover",
     78 		    device_name);
     79 		return;
     80 	}
     81 	if (tbl == NULL && lbt == NULL) {
     82 		warnx("%s: no primary or secondary GPT tables, can't recover",
     83 		    device_name);
     84 		return;
     85 	}
     86 
     87 	last = mediasz / secsz - 1LL;
     88 
     89 	if (tbl != NULL && lbt == NULL) {
     90 		lbt = map_add(last - tbl->map_size, tbl->map_size,
     91 		    MAP_TYPE_SEC_GPT_TBL, tbl->map_data);
     92 		if (lbt == NULL) {
     93 			warnx("%s: adding secondary GPT table failed",
     94 			    device_name);
     95 			return;
     96 		}
     97 		gpt_write(fd, lbt);
     98 		warnx("%s: recovered secondary GPT table from primary",
     99 		    device_name);
    100 	} else if (tbl == NULL && lbt != NULL) {
    101 		tbl = map_add(2LL, lbt->map_size, MAP_TYPE_PRI_GPT_TBL,
    102 		    lbt->map_data);
    103 		if (tbl == NULL) {
    104 			warnx("%s: adding primary GPT table failed",
    105 			    device_name);
    106 			return;
    107 		}
    108 		gpt_write(fd, tbl);
    109 		warnx("%s: recovered primary GPT table from secondary",
    110 		    device_name);
    111 	}
    112 
    113 	if (gpt != NULL && tpg == NULL) {
    114 		tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
    115 		    calloc(1, secsz));
    116 		if (tpg == NULL) {
    117 			warnx("%s: adding secondary GPT header failed",
    118 			    device_name);
    119 			return;
    120 		}
    121 		memcpy(tpg->map_data, gpt->map_data, secsz);
    122 		hdr = tpg->map_data;
    123 		hdr->hdr_lba_self = htole64(tpg->map_start);
    124 		hdr->hdr_lba_alt = htole64(gpt->map_start);
    125 		hdr->hdr_lba_table = htole64(lbt->map_start);
    126 		hdr->hdr_crc_self = 0;
    127 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    128 		gpt_write(fd, tpg);
    129 		warnx("%s: recovered secondary GPT header from primary",
    130 		    device_name);
    131 	} else if (gpt == NULL && tpg != NULL) {
    132 		gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
    133 		    calloc(1, secsz));
    134 		if (gpt == NULL) {
    135 			warnx("%s: adding primary GPT header failed",
    136 			    device_name);
    137 			return;
    138 		}
    139 		memcpy(gpt->map_data, tpg->map_data, secsz);
    140 		hdr = gpt->map_data;
    141 		hdr->hdr_lba_self = htole64(gpt->map_start);
    142 		hdr->hdr_lba_alt = htole64(tpg->map_start);
    143 		hdr->hdr_lba_table = htole64(tbl->map_start);
    144 		hdr->hdr_crc_self = 0;
    145 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    146 		gpt_write(fd, gpt);
    147 		warnx("%s: recovered primary GPT header from secondary",
    148 		    device_name);
    149 	}
    150 }
    151 
    152 int
    153 cmd_recover(int argc, char *argv[])
    154 {
    155 	int ch, fd;
    156 
    157 	while ((ch = getopt(argc, argv, "r")) != -1) {
    158 		switch(ch) {
    159 		case 'r':
    160 			recoverable = 1;
    161 			break;
    162 		default:
    163 			usage_recover();
    164 		}
    165 	}
    166 
    167 	if (argc == optind)
    168 		usage_recover();
    169 
    170 	while (optind < argc) {
    171 		fd = gpt_open(argv[optind++]);
    172 		if (fd == -1) {
    173 			warn("unable to open device '%s'", device_name);
    174 			continue;
    175 		}
    176 
    177 		recover(fd);
    178 
    179 		gpt_close(fd);
    180 	}
    181 
    182 	return (0);
    183 }
    184