xref: /netbsd-src/sbin/gpt/recover.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
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.15 2015/12/04 16:46:24 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 cmd_recover(gpt_t, int, char *[]);
53 
54 static const char *recoverhelp[] = {
55 	"",
56 };
57 
58 struct gpt_cmd c_recover = {
59 	"recover",
60 	cmd_recover,
61 	recoverhelp, __arraycount(recoverhelp),
62 	0,
63 };
64 
65 #define usage() gpt_usage(NULL, &c_recover)
66 
67 static int
68 recover_gpt_hdr(gpt_t gpt, int type, off_t last)
69 {
70 	const char *name, *origname;
71 	map_t *dgpt, dtbl, sgpt, stbl;
72 	struct gpt_hdr *hdr;
73 
74 	if (gpt_add_hdr(gpt, type, last) == -1)
75 		return -1;
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 	memcpy((*dgpt)->map_data, sgpt->map_data, gpt->secsz);
100 	hdr = (*dgpt)->map_data;
101 	hdr->hdr_lba_self = htole64((uint64_t)(*dgpt)->map_start);
102 	hdr->hdr_lba_alt = htole64((uint64_t)stbl->map_start);
103 	hdr->hdr_lba_table = htole64((uint64_t)dtbl->map_start);
104 	hdr->hdr_crc_self = 0;
105 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
106 	if (gpt_write(gpt, *dgpt) == -1) {
107 		gpt_warnx(gpt, "Writing %s GPT header failed", name);
108 		return -1;
109 	}
110 	gpt_msg(gpt, "Recovered %s GPT header from %s", name, origname);
111 	return 0;
112 }
113 
114 static int
115 recover_gpt_tbl(gpt_t gpt, int type, off_t start)
116 {
117 	const char *name, *origname;
118 	map_t *dtbl, stbl;
119 
120 	switch (type) {
121 	case MAP_TYPE_PRI_GPT_TBL:
122 		dtbl = &gpt->tbl;
123 		stbl = gpt->lbt;
124 		origname = "secondary";
125 		name = "primary";
126 		break;
127 	case MAP_TYPE_SEC_GPT_TBL:
128 		dtbl = &gpt->lbt;
129 		stbl = gpt->tbl;
130 		origname = "primary";
131 		name = "secondary";
132 		break;
133 	default:
134 		gpt_warn(gpt, "Bad table type %d", type);
135 		return -1;
136 	}
137 
138 	*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
139 	if (*dtbl == NULL) {
140 		gpt_warnx(gpt, "Adding %s GPT table failed", name);
141 		return -1;
142 	}
143 	if (gpt_write(gpt, *dtbl) == -1) {
144 		gpt_warnx(gpt, "Writing %s GPT table failed", name);
145 		return -1;
146 	}
147 	gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
148 	return 0;
149 }
150 
151 static int
152 recover(gpt_t gpt, int recoverable)
153 {
154 	uint64_t last;
155 
156 	if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
157 		gpt_warnx(gpt, "Device contains an MBR");
158 		return -1;
159 	}
160 
161 	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
162 	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
163 	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
164 	gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
165 
166 	if (gpt->gpt == NULL && gpt->tpg == NULL) {
167 		gpt_warnx(gpt, "No primary or secondary GPT headers, "
168 		    "can't recover");
169 		return -1;
170 	}
171 	if (gpt->tbl == NULL && gpt->lbt == NULL) {
172 		gpt_warnx(gpt, "No primary or secondary GPT tables, "
173 		    "can't recover");
174 		return -1;
175 	}
176 
177 	last = (uint64_t)(gpt->mediasz / gpt->secsz - 1LL);
178 
179 	if (gpt->gpt != NULL &&
180 	    ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
181 		gpt_warnx(gpt, "Media size has changed, please use "
182 		   "'%s resizedisk'", getprogname());
183 		return -1;
184 	}
185 
186 	if (gpt->tbl != NULL && gpt->lbt == NULL) {
187 		if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
188 		    (off_t)last - gpt->tbl->map_size) == -1)
189 			return -1;
190 	} else if (gpt->tbl == NULL && gpt->lbt != NULL) {
191 		if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
192 			return -1;
193 	}
194 
195 	if (gpt->gpt != NULL && gpt->tpg == NULL) {
196 		if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR,
197 		    (off_t)last) == -1)
198 			return -1;
199 	} else if (gpt->gpt == NULL && gpt->tpg != NULL) {
200 		if (recover_gpt_hdr(gpt, MAP_TYPE_PRI_GPT_HDR, 1LL) == -1)
201 			return -1;
202 	}
203 	return 0;
204 }
205 
206 static int
207 cmd_recover(gpt_t gpt, int argc, char *argv[])
208 {
209 	int ch;
210 	int recoverable = 0;
211 
212 	while ((ch = getopt(argc, argv, "r")) != -1) {
213 		switch(ch) {
214 		case 'r':
215 			recoverable = 1;
216 			break;
217 		default:
218 			return usage();
219 		}
220 	}
221 
222 	if (argc != optind)
223 		return usage();
224 
225 	return recover(gpt, recoverable);
226 }
227