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