recover.c revision 1.9 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.9 2015/12/01 16:32:19 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 recoverable;
53
54 static int cmd_recover(gpt_t, int, char *[]);
55
56 static const char *recoverhelp[] = {
57 "",
58 };
59
60 struct gpt_cmd c_recover = {
61 "recover",
62 cmd_recover,
63 recoverhelp, __arraycount(recoverhelp),
64 0,
65 };
66
67 #define usage() gpt_usage(NULL, &c_recover)
68
69 static int
70 recover(gpt_t gpt)
71 {
72 uint64_t last;
73 struct gpt_hdr *hdr;
74
75 if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
76 gpt_warnx(gpt, "Device contains an MBR");
77 return -1;
78 }
79
80 gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
81 gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
82 gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
83 gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
84
85 if (gpt->gpt == NULL && gpt->tpg == NULL) {
86 gpt_warnx(gpt, "No primary or secondary GPT headers, "
87 "can't recover");
88 return -1;
89 }
90 if (gpt->tbl == NULL && gpt->lbt == NULL) {
91 gpt_warnx(gpt, "No primary or secondary GPT tables, "
92 "can't recover");
93 return -1;
94 }
95
96 last = gpt->mediasz / gpt->secsz - 1LL;
97
98 if (gpt->gpt != NULL &&
99 ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
100 gpt_warnx(gpt, "Media size has changed, please use "
101 "'gpt resizedisk'");
102 return -1;
103 }
104
105 if (gpt->tbl != NULL && gpt->lbt == NULL) {
106 gpt->lbt = map_add(gpt, last - gpt->tbl->map_size,
107 gpt->tbl->map_size, MAP_TYPE_SEC_GPT_TBL,
108 gpt->tbl->map_data);
109 if (gpt->lbt == NULL) {
110 gpt_warnx(gpt, "Adding secondary GPT table failed");
111 return -1;
112 }
113 if (gpt_write(gpt, gpt->lbt) == -1) {
114 gpt_warnx(gpt, "Writing secondary GPT table failed");
115 return -1;
116 }
117 gpt_msg(gpt, "Recovered secondary GPT table from primary");
118 } else if (gpt->tbl == NULL && gpt->lbt != NULL) {
119 gpt->tbl = map_add(gpt, 2LL, gpt->lbt->map_size,
120 MAP_TYPE_PRI_GPT_TBL, gpt->lbt->map_data);
121 if (gpt->tbl == NULL) {
122 gpt_warnx(gpt, "Adding primary GPT table failed");
123 return -1;
124 }
125 if (gpt_write(gpt, gpt->tbl) == -1) {
126 gpt_warnx(gpt, "Writing primary GPT table failed");
127 return -1;
128 }
129 gpt_msg(gpt, "Recovered primary GPT table from secondary");
130 }
131
132 if (gpt->gpt != NULL && gpt->tpg == NULL) {
133 gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
134 calloc(1, gpt->secsz));
135 if (gpt->tpg == NULL) {
136 gpt_warnx(gpt, "Adding secondary GPT header failed");
137 return -1;
138 }
139 memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
140 hdr = gpt->tpg->map_data;
141 hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
142 hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
143 hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
144 hdr->hdr_crc_self = 0;
145 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
146 if (gpt_write(gpt, gpt->tpg) == -1) {
147 gpt_warnx(gpt, "Writing secondary GPT header failed");
148 return -1;
149 }
150 gpt_msg(gpt, "Recovered secondary GPT header from primary");
151 } else if (gpt->gpt == NULL && gpt->tpg != NULL) {
152 gpt->gpt = map_add(gpt, 1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
153 calloc(1, gpt->secsz));
154 if (gpt->gpt == NULL) {
155 gpt_warnx(gpt, "Adding primary GPT header failed");
156 return -1;
157 }
158 memcpy(gpt->gpt->map_data, gpt->tpg->map_data, gpt->secsz);
159 hdr = gpt->gpt->map_data;
160 hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
161 hdr->hdr_lba_alt = htole64(gpt->tpg->map_start);
162 hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
163 hdr->hdr_crc_self = 0;
164 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
165 if (gpt_write(gpt, gpt->gpt) == -1) {
166 gpt_warnx(gpt, "Writing primary GPT header failed");
167 return -1;
168 }
169 gpt_msg(gpt, "Recovered primary GPT header from secondary");
170 }
171 return 0;
172 }
173
174 static int
175 cmd_recover(gpt_t gpt, int argc, char *argv[])
176 {
177 int ch;
178
179 while ((ch = getopt(argc, argv, "r")) != -1) {
180 switch(ch) {
181 case 'r':
182 recoverable = 1;
183 break;
184 default:
185 return usage();
186 }
187 }
188
189 if (argc != optind)
190 return usage();
191
192 return recover(gpt);
193 }
194