recover.c revision 1.14 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.5 christos #if HAVE_NBTOOL_CONFIG_H
28 1.5 christos #include "nbtool_config.h"
29 1.5 christos #endif
30 1.5 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.2 christos #ifdef __FBSDID
33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
34 1.2 christos #endif
35 1.2 christos #ifdef __RCSID
36 1.14 christos __RCSID("$NetBSD: recover.c,v 1.14 2015/12/03 21:30:54 christos Exp $");
37 1.2 christos #endif
38 1.1 christos
39 1.1 christos #include <sys/types.h>
40 1.1 christos
41 1.1 christos #include <err.h>
42 1.1 christos #include <stddef.h>
43 1.1 christos #include <stdio.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <string.h>
46 1.1 christos #include <unistd.h>
47 1.1 christos
48 1.1 christos #include "map.h"
49 1.1 christos #include "gpt.h"
50 1.8 christos #include "gpt_private.h"
51 1.1 christos
52 1.9 christos static int cmd_recover(gpt_t, int, char *[]);
53 1.3 riz
54 1.9 christos static const char *recoverhelp[] = {
55 1.12 christos "",
56 1.9 christos };
57 1.9 christos
58 1.9 christos struct gpt_cmd c_recover = {
59 1.9 christos "recover",
60 1.9 christos cmd_recover,
61 1.9 christos recoverhelp, __arraycount(recoverhelp),
62 1.9 christos 0,
63 1.9 christos };
64 1.1 christos
65 1.9 christos #define usage() gpt_usage(NULL, &c_recover)
66 1.1 christos
67 1.8 christos static int
68 1.10 christos recover_gpt_hdr(gpt_t gpt, int type, off_t last)
69 1.10 christos {
70 1.10 christos const char *name, *origname;
71 1.10 christos void *p;
72 1.10 christos map_t *dgpt, dtbl, sgpt, stbl;
73 1.10 christos struct gpt_hdr *hdr;
74 1.10 christos
75 1.10 christos switch (type) {
76 1.10 christos case MAP_TYPE_PRI_GPT_HDR:
77 1.10 christos dgpt = &gpt->gpt;
78 1.10 christos dtbl = gpt->tbl;
79 1.10 christos sgpt = gpt->tpg;
80 1.10 christos stbl = gpt->lbt;
81 1.10 christos origname = "secondary";
82 1.10 christos name = "primary";
83 1.10 christos break;
84 1.10 christos case MAP_TYPE_SEC_GPT_HDR:
85 1.10 christos dgpt = &gpt->tpg;
86 1.10 christos dtbl = gpt->lbt;
87 1.10 christos sgpt = gpt->gpt;
88 1.10 christos stbl = gpt->tbl;
89 1.10 christos origname = "primary";
90 1.10 christos name = "secondary";
91 1.10 christos break;
92 1.10 christos default:
93 1.10 christos gpt_warn(gpt, "Bad table type %d", type);
94 1.10 christos return -1;
95 1.10 christos }
96 1.10 christos
97 1.10 christos if ((p = calloc(1, gpt->secsz)) == NULL) {
98 1.10 christos gpt_warn(gpt, "Cannot allocate %s GPT header", name);
99 1.10 christos return -1;
100 1.10 christos }
101 1.14 christos if ((*dgpt = map_add(gpt, last, 1LL, type, p, 1)) == NULL) {
102 1.10 christos gpt_warnx(gpt, "Cannot add %s GPT header", name);
103 1.10 christos return -1;
104 1.10 christos }
105 1.10 christos memcpy((*dgpt)->map_data, sgpt->map_data, gpt->secsz);
106 1.10 christos hdr = (*dgpt)->map_data;
107 1.13 christos hdr->hdr_lba_self = htole64((uint64_t)(*dgpt)->map_start);
108 1.13 christos hdr->hdr_lba_alt = htole64((uint64_t)stbl->map_start);
109 1.13 christos hdr->hdr_lba_table = htole64((uint64_t)dtbl->map_start);
110 1.10 christos hdr->hdr_crc_self = 0;
111 1.10 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
112 1.10 christos if (gpt_write(gpt, *dgpt) == -1) {
113 1.10 christos gpt_warnx(gpt, "Writing %s GPT header failed", name);
114 1.10 christos return -1;
115 1.10 christos }
116 1.10 christos gpt_msg(gpt, "Recovered %s GPT header from %s", name, origname);
117 1.10 christos return 0;
118 1.10 christos }
119 1.10 christos
120 1.10 christos static int
121 1.10 christos recover_gpt_tbl(gpt_t gpt, int type, off_t start)
122 1.10 christos {
123 1.10 christos const char *name, *origname;
124 1.10 christos map_t *dtbl, stbl;
125 1.10 christos
126 1.10 christos switch (type) {
127 1.10 christos case MAP_TYPE_PRI_GPT_TBL:
128 1.10 christos dtbl = &gpt->tbl;
129 1.10 christos stbl = gpt->lbt;
130 1.10 christos origname = "secondary";
131 1.10 christos name = "primary";
132 1.10 christos break;
133 1.10 christos case MAP_TYPE_SEC_GPT_TBL:
134 1.10 christos dtbl = &gpt->lbt;
135 1.10 christos stbl = gpt->tbl;
136 1.10 christos origname = "primary";
137 1.10 christos name = "secondary";
138 1.10 christos break;
139 1.10 christos default:
140 1.10 christos gpt_warn(gpt, "Bad table type %d", type);
141 1.10 christos return -1;
142 1.10 christos }
143 1.10 christos
144 1.14 christos *dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
145 1.10 christos if (*dtbl == NULL) {
146 1.10 christos gpt_warnx(gpt, "Adding %s GPT table failed", name);
147 1.10 christos return -1;
148 1.10 christos }
149 1.10 christos if (gpt_write(gpt, *dtbl) == -1) {
150 1.10 christos gpt_warnx(gpt, "Writing %s GPT table failed", name);
151 1.10 christos return -1;
152 1.10 christos }
153 1.10 christos gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
154 1.10 christos return 0;
155 1.10 christos }
156 1.10 christos
157 1.10 christos static int
158 1.12 christos recover(gpt_t gpt, int recoverable)
159 1.1 christos {
160 1.6 jnemeth uint64_t last;
161 1.1 christos
162 1.8 christos if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
163 1.8 christos gpt_warnx(gpt, "Device contains an MBR");
164 1.8 christos return -1;
165 1.8 christos }
166 1.8 christos
167 1.8 christos gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
168 1.8 christos gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
169 1.8 christos gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
170 1.8 christos gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
171 1.8 christos
172 1.8 christos if (gpt->gpt == NULL && gpt->tpg == NULL) {
173 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT headers, "
174 1.8 christos "can't recover");
175 1.8 christos return -1;
176 1.8 christos }
177 1.8 christos if (gpt->tbl == NULL && gpt->lbt == NULL) {
178 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT tables, "
179 1.8 christos "can't recover");
180 1.8 christos return -1;
181 1.8 christos }
182 1.8 christos
183 1.13 christos last = (uint64_t)(gpt->mediasz / gpt->secsz - 1LL);
184 1.8 christos
185 1.8 christos if (gpt->gpt != NULL &&
186 1.8 christos ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
187 1.8 christos gpt_warnx(gpt, "Media size has changed, please use "
188 1.11 christos "'%s resizedisk'", getprogname());
189 1.8 christos return -1;
190 1.8 christos }
191 1.8 christos
192 1.8 christos if (gpt->tbl != NULL && gpt->lbt == NULL) {
193 1.10 christos if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
194 1.13 christos (off_t)last - gpt->tbl->map_size) == -1)
195 1.8 christos return -1;
196 1.8 christos } else if (gpt->tbl == NULL && gpt->lbt != NULL) {
197 1.10 christos if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
198 1.8 christos return -1;
199 1.8 christos }
200 1.8 christos
201 1.8 christos if (gpt->gpt != NULL && gpt->tpg == NULL) {
202 1.13 christos if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR,
203 1.13 christos (off_t)last) == -1)
204 1.8 christos return -1;
205 1.8 christos } else if (gpt->gpt == NULL && gpt->tpg != NULL) {
206 1.10 christos if (recover_gpt_hdr(gpt, MAP_TYPE_PRI_GPT_HDR, 1LL) == -1)
207 1.8 christos return -1;
208 1.1 christos }
209 1.8 christos return 0;
210 1.1 christos }
211 1.1 christos
212 1.9 christos static int
213 1.8 christos cmd_recover(gpt_t gpt, int argc, char *argv[])
214 1.1 christos {
215 1.8 christos int ch;
216 1.12 christos int recoverable = 0;
217 1.1 christos
218 1.1 christos while ((ch = getopt(argc, argv, "r")) != -1) {
219 1.1 christos switch(ch) {
220 1.1 christos case 'r':
221 1.1 christos recoverable = 1;
222 1.1 christos break;
223 1.1 christos default:
224 1.9 christos return usage();
225 1.1 christos }
226 1.1 christos }
227 1.1 christos
228 1.8 christos if (argc != optind)
229 1.9 christos return usage();
230 1.1 christos
231 1.12 christos return recover(gpt, recoverable);
232 1.1 christos }
233