Home | History | Annotate | Line # | Download | only in gpt
recover.c revision 1.1
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  *
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  *
     15  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos #include <sys/cdefs.h>
     28  1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
     29  1.1  christos 
     30  1.1  christos #include <sys/types.h>
     31  1.1  christos 
     32  1.1  christos #include <err.h>
     33  1.1  christos #include <stddef.h>
     34  1.1  christos #include <stdio.h>
     35  1.1  christos #include <stdlib.h>
     36  1.1  christos #include <string.h>
     37  1.1  christos #include <unistd.h>
     38  1.1  christos 
     39  1.1  christos #include "map.h"
     40  1.1  christos #include "gpt.h"
     41  1.1  christos 
     42  1.1  christos static int recoverable;
     43  1.1  christos 
     44  1.1  christos static void
     45  1.1  christos usage_recover(void)
     46  1.1  christos {
     47  1.1  christos 
     48  1.1  christos 	fprintf(stderr,
     49  1.1  christos 	    "usage: %s device ...\n", getprogname());
     50  1.1  christos 	exit(1);
     51  1.1  christos }
     52  1.1  christos 
     53  1.1  christos static void
     54  1.1  christos recover(int fd)
     55  1.1  christos {
     56  1.1  christos 	off_t last;
     57  1.1  christos 	map_t *gpt, *tpg;
     58  1.1  christos 	map_t *tbl, *lbt;
     59  1.1  christos 	struct gpt_hdr *hdr;
     60  1.1  christos 
     61  1.1  christos 	if (map_find(MAP_TYPE_MBR) != NULL) {
     62  1.1  christos 		warnx("%s: error: device contains a MBR", device_name);
     63  1.1  christos 		return;
     64  1.1  christos 	}
     65  1.1  christos 
     66  1.1  christos 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     67  1.1  christos 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     68  1.1  christos 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     69  1.1  christos 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     70  1.1  christos 
     71  1.1  christos 	if (gpt == NULL && tpg == NULL) {
     72  1.1  christos 		warnx("%s: no primary or secondary GPT headers, can't recover",
     73  1.1  christos 		    device_name);
     74  1.1  christos 		return;
     75  1.1  christos 	}
     76  1.1  christos 	if (tbl == NULL && lbt == NULL) {
     77  1.1  christos 		warnx("%s: no primary or secondary GPT tables, can't recover",
     78  1.1  christos 		    device_name);
     79  1.1  christos 		return;
     80  1.1  christos 	}
     81  1.1  christos 
     82  1.1  christos 	last = mediasz / secsz - 1LL;
     83  1.1  christos 
     84  1.1  christos 	if (tbl != NULL && lbt == NULL) {
     85  1.1  christos 		lbt = map_add(last - tbl->map_size, tbl->map_size,
     86  1.1  christos 		    MAP_TYPE_SEC_GPT_TBL, tbl->map_data);
     87  1.1  christos 		if (lbt == NULL) {
     88  1.1  christos 			warnx("%s: adding secondary GPT table failed",
     89  1.1  christos 			    device_name);
     90  1.1  christos 			return;
     91  1.1  christos 		}
     92  1.1  christos 		gpt_write(fd, lbt);
     93  1.1  christos 		warnx("%s: recovered secondary GPT table from primary",
     94  1.1  christos 		    device_name);
     95  1.1  christos 	} else if (tbl == NULL && lbt != NULL) {
     96  1.1  christos 		tbl = map_add(2LL, lbt->map_size, MAP_TYPE_PRI_GPT_TBL,
     97  1.1  christos 		    lbt->map_data);
     98  1.1  christos 		if (tbl == NULL) {
     99  1.1  christos 			warnx("%s: adding primary GPT table failed",
    100  1.1  christos 			    device_name);
    101  1.1  christos 			return;
    102  1.1  christos 		}
    103  1.1  christos 		gpt_write(fd, tbl);
    104  1.1  christos 		warnx("%s: recovered primary GPT table from secondary",
    105  1.1  christos 		    device_name);
    106  1.1  christos 	}
    107  1.1  christos 
    108  1.1  christos 	if (gpt != NULL && tpg == NULL) {
    109  1.1  christos 		tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
    110  1.1  christos 		    calloc(1, secsz));
    111  1.1  christos 		if (tpg == NULL) {
    112  1.1  christos 			warnx("%s: adding secondary GPT header failed",
    113  1.1  christos 			    device_name);
    114  1.1  christos 			return;
    115  1.1  christos 		}
    116  1.1  christos 		memcpy(tpg->map_data, gpt->map_data, secsz);
    117  1.1  christos 		hdr = tpg->map_data;
    118  1.1  christos 		hdr->hdr_lba_self = htole64(tpg->map_start);
    119  1.1  christos 		hdr->hdr_lba_alt = htole64(gpt->map_start);
    120  1.1  christos 		hdr->hdr_lba_table = htole64(lbt->map_start);
    121  1.1  christos 		hdr->hdr_crc_self = 0;
    122  1.1  christos 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    123  1.1  christos 		gpt_write(fd, tpg);
    124  1.1  christos 		warnx("%s: recovered secondary GPT header from primary",
    125  1.1  christos 		    device_name);
    126  1.1  christos 	} else if (gpt == NULL && tpg != NULL) {
    127  1.1  christos 		gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
    128  1.1  christos 		    calloc(1, secsz));
    129  1.1  christos 		if (gpt == NULL) {
    130  1.1  christos 			warnx("%s: adding primary GPT header failed",
    131  1.1  christos 			    device_name);
    132  1.1  christos 			return;
    133  1.1  christos 		}
    134  1.1  christos 		memcpy(gpt->map_data, tpg->map_data, secsz);
    135  1.1  christos 		hdr = gpt->map_data;
    136  1.1  christos 		hdr->hdr_lba_self = htole64(gpt->map_start);
    137  1.1  christos 		hdr->hdr_lba_alt = htole64(tpg->map_start);
    138  1.1  christos 		hdr->hdr_lba_table = htole64(tbl->map_start);
    139  1.1  christos 		hdr->hdr_crc_self = 0;
    140  1.1  christos 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    141  1.1  christos 		gpt_write(fd, gpt);
    142  1.1  christos 		warnx("%s: recovered primary GPT header from secondary",
    143  1.1  christos 		    device_name);
    144  1.1  christos 	}
    145  1.1  christos }
    146  1.1  christos 
    147  1.1  christos int
    148  1.1  christos cmd_recover(int argc, char *argv[])
    149  1.1  christos {
    150  1.1  christos 	int ch, fd;
    151  1.1  christos 
    152  1.1  christos 	while ((ch = getopt(argc, argv, "r")) != -1) {
    153  1.1  christos 		switch(ch) {
    154  1.1  christos 		case 'r':
    155  1.1  christos 			recoverable = 1;
    156  1.1  christos 			break;
    157  1.1  christos 		default:
    158  1.1  christos 			usage_recover();
    159  1.1  christos 		}
    160  1.1  christos 	}
    161  1.1  christos 
    162  1.1  christos 	if (argc == optind)
    163  1.1  christos 		usage_recover();
    164  1.1  christos 
    165  1.1  christos 	while (optind < argc) {
    166  1.1  christos 		fd = gpt_open(argv[optind++]);
    167  1.1  christos 		if (fd == -1) {
    168  1.1  christos 			warn("unable to open device '%s'", device_name);
    169  1.1  christos 			continue;
    170  1.1  christos 		}
    171  1.1  christos 
    172  1.1  christos 		recover(fd);
    173  1.1  christos 
    174  1.1  christos 		gpt_close(fd);
    175  1.1  christos 	}
    176  1.1  christos 
    177  1.1  christos 	return (0);
    178  1.1  christos }
    179