recover.c revision 1.14 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.14 2015/12/03 21:30:54 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 void *p;
72 map_t *dgpt, dtbl, sgpt, stbl;
73 struct gpt_hdr *hdr;
74
75 switch (type) {
76 case MAP_TYPE_PRI_GPT_HDR:
77 dgpt = &gpt->gpt;
78 dtbl = gpt->tbl;
79 sgpt = gpt->tpg;
80 stbl = gpt->lbt;
81 origname = "secondary";
82 name = "primary";
83 break;
84 case MAP_TYPE_SEC_GPT_HDR:
85 dgpt = &gpt->tpg;
86 dtbl = gpt->lbt;
87 sgpt = gpt->gpt;
88 stbl = gpt->tbl;
89 origname = "primary";
90 name = "secondary";
91 break;
92 default:
93 gpt_warn(gpt, "Bad table type %d", type);
94 return -1;
95 }
96
97 if ((p = calloc(1, gpt->secsz)) == NULL) {
98 gpt_warn(gpt, "Cannot allocate %s GPT header", name);
99 return -1;
100 }
101 if ((*dgpt = map_add(gpt, last, 1LL, type, p, 1)) == NULL) {
102 gpt_warnx(gpt, "Cannot add %s GPT header", name);
103 return -1;
104 }
105 memcpy((*dgpt)->map_data, sgpt->map_data, gpt->secsz);
106 hdr = (*dgpt)->map_data;
107 hdr->hdr_lba_self = htole64((uint64_t)(*dgpt)->map_start);
108 hdr->hdr_lba_alt = htole64((uint64_t)stbl->map_start);
109 hdr->hdr_lba_table = htole64((uint64_t)dtbl->map_start);
110 hdr->hdr_crc_self = 0;
111 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
112 if (gpt_write(gpt, *dgpt) == -1) {
113 gpt_warnx(gpt, "Writing %s GPT header failed", name);
114 return -1;
115 }
116 gpt_msg(gpt, "Recovered %s GPT header from %s", name, origname);
117 return 0;
118 }
119
120 static int
121 recover_gpt_tbl(gpt_t gpt, int type, off_t start)
122 {
123 const char *name, *origname;
124 map_t *dtbl, stbl;
125
126 switch (type) {
127 case MAP_TYPE_PRI_GPT_TBL:
128 dtbl = &gpt->tbl;
129 stbl = gpt->lbt;
130 origname = "secondary";
131 name = "primary";
132 break;
133 case MAP_TYPE_SEC_GPT_TBL:
134 dtbl = &gpt->lbt;
135 stbl = gpt->tbl;
136 origname = "primary";
137 name = "secondary";
138 break;
139 default:
140 gpt_warn(gpt, "Bad table type %d", type);
141 return -1;
142 }
143
144 *dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
145 if (*dtbl == NULL) {
146 gpt_warnx(gpt, "Adding %s GPT table failed", name);
147 return -1;
148 }
149 if (gpt_write(gpt, *dtbl) == -1) {
150 gpt_warnx(gpt, "Writing %s GPT table failed", name);
151 return -1;
152 }
153 gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
154 return 0;
155 }
156
157 static int
158 recover(gpt_t gpt, int recoverable)
159 {
160 uint64_t last;
161
162 if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
163 gpt_warnx(gpt, "Device contains an MBR");
164 return -1;
165 }
166
167 gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
168 gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
169 gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
170 gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
171
172 if (gpt->gpt == NULL && gpt->tpg == NULL) {
173 gpt_warnx(gpt, "No primary or secondary GPT headers, "
174 "can't recover");
175 return -1;
176 }
177 if (gpt->tbl == NULL && gpt->lbt == NULL) {
178 gpt_warnx(gpt, "No primary or secondary GPT tables, "
179 "can't recover");
180 return -1;
181 }
182
183 last = (uint64_t)(gpt->mediasz / gpt->secsz - 1LL);
184
185 if (gpt->gpt != NULL &&
186 ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
187 gpt_warnx(gpt, "Media size has changed, please use "
188 "'%s resizedisk'", getprogname());
189 return -1;
190 }
191
192 if (gpt->tbl != NULL && gpt->lbt == NULL) {
193 if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
194 (off_t)last - gpt->tbl->map_size) == -1)
195 return -1;
196 } else if (gpt->tbl == NULL && gpt->lbt != NULL) {
197 if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
198 return -1;
199 }
200
201 if (gpt->gpt != NULL && gpt->tpg == NULL) {
202 if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR,
203 (off_t)last) == -1)
204 return -1;
205 } else if (gpt->gpt == NULL && gpt->tpg != NULL) {
206 if (recover_gpt_hdr(gpt, MAP_TYPE_PRI_GPT_HDR, 1LL) == -1)
207 return -1;
208 }
209 return 0;
210 }
211
212 static int
213 cmd_recover(gpt_t gpt, int argc, char *argv[])
214 {
215 int ch;
216 int recoverable = 0;
217
218 while ((ch = getopt(argc, argv, "r")) != -1) {
219 switch(ch) {
220 case 'r':
221 recoverable = 1;
222 break;
223 default:
224 return usage();
225 }
226 }
227
228 if (argc != optind)
229 return usage();
230
231 return recover(gpt, recoverable);
232 }
233