xref: /netbsd-src/sbin/gpt/recover.c (revision 464df8b50ee3018025357e001ec760e9140e9323)
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.19 2020/12/13 21:55:25 jnemeth 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 	GPT_SYNC,
63 };
64 
65 #define usage() gpt_usage(NULL, &c_recover)
66 
67 static int
recover_gpt_hdr(gpt_t gpt,int type,off_t last)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 __unused;
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)sgpt->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
recover_gpt_tbl(gpt_t gpt,int type,off_t start)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
recover(gpt_t gpt)152 recover(gpt_t gpt)
153 {
154 	off_t last = gpt_last(gpt);
155 	map_t map;
156 	struct mbr *mbr;
157 
158 	if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
159 		gpt_warnx(gpt, "Device contains an MBR");
160 		return -1;
161 	}
162 
163 	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
164 	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
165 	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
166 	gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
167 
168 	if (gpt->gpt == NULL && gpt->tpg == NULL) {
169 		gpt_warnx(gpt, "No primary or secondary GPT headers, "
170 		    "can't recover");
171 		return -1;
172 	}
173 	if (gpt->tbl == NULL && gpt->lbt == NULL) {
174 		gpt_warnx(gpt, "No primary or secondary GPT tables, "
175 		    "can't recover");
176 		return -1;
177 	}
178 
179 	if (gpt->gpt != NULL &&
180 	    le64toh(((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt) !=
181 	    (uint64_t)last) {
182 		gpt_warnx(gpt, "Media size has changed, please use "
183 		   "'%s resizedisk'", getprogname());
184 		return -1;
185 	}
186 
187 	if (gpt->tbl != NULL && gpt->lbt == NULL) {
188 		if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
189 		    last - gpt->tbl->map_size) == -1)
190 			return -1;
191 	} else if (gpt->tbl == NULL && gpt->lbt != NULL) {
192 		if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
193 			return -1;
194 	}
195 
196 	if (gpt->gpt != NULL && gpt->tpg == NULL) {
197 		if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR, 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 
204 	/*
205 	 * Create PMBR if it doesn't already exist.
206 	 */
207 	if (map_find(gpt, MAP_TYPE_PMBR) == NULL) {
208 		if (map_free(gpt, 0LL, 1LL) == 0) {
209 			gpt_warnx(gpt, "No room for the PMBR");
210 			return -1;
211 		}
212 		mbr = gpt_read(gpt, 0LL, 1);
213 		if (mbr == NULL) {
214 			gpt_warnx(gpt, "Error reading MBR");
215 			return -1;
216 		}
217 		memset(mbr, 0, sizeof(*mbr));
218 		mbr->mbr_sig = htole16(MBR_SIG);
219 		gpt_create_pmbr_part(mbr->mbr_part, last, 0);
220 
221 		map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1);
222 		if (gpt_write(gpt, map) == -1) {
223 			gpt_warn(gpt, "Can't write PMBR");
224 			return -1;
225 		}
226 		gpt_msg(gpt,
227 		    "Recreated PMBR (you may need to rerun 'gpt biosboot'");
228 	}
229 	return 0;
230 }
231 
232 static int
cmd_recover(gpt_t gpt,int argc,char * argv[])233 cmd_recover(gpt_t gpt, int argc, char *argv[])
234 {
235 	int ch;
236 
237 	while ((ch = getopt(argc, argv, "r")) != -1) {
238 		switch(ch) {
239 		default:
240 			return usage();
241 		}
242 	}
243 
244 	if (argc != optind)
245 		return usage();
246 
247 	return recover(gpt);
248 }
249