xref: /netbsd-src/sbin/gpt/restore.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: restore.c,v 1.18 2018/07/03 03:41:24 jnemeth Exp $");
37 #endif
38 
39 #include <sys/types.h>
40 #include <sys/bootblock.h>
41 #include <sys/disklabel_gpt.h>
42 
43 #include <err.h>
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <prop/proplib.h>
50 
51 #include "map.h"
52 #include "gpt.h"
53 #include "gpt_private.h"
54 
55 static int cmd_restore(gpt_t, int, char *[]);
56 
57 static const char *restorehelp[] = {
58 	"[-F] [-i infile]",
59 };
60 
61 struct gpt_cmd c_restore = {
62 	"restore",
63 	cmd_restore,
64 	restorehelp, __arraycount(restorehelp),
65 	GPT_SYNC,
66 };
67 
68 #define usage() gpt_usage(NULL, &c_restore)
69 
70 #define PROP_ERR(x)     if (!(x)) {		\
71 	gpt_warnx(gpt, "proplib failure");	\
72 	return -1;				\
73 }
74 
75 #define prop_uint(a) (u_int)prop_number_unsigned_integer_value(a)
76 #define prop_uint16_t(a) (uint16_t)prop_number_unsigned_integer_value(a)
77 #define prop_uint8_t(a) (uint8_t)prop_number_unsigned_integer_value(a)
78 
79 static int
80 restore_mbr(gpt_t gpt, struct mbr *mbr, prop_dictionary_t mbr_dict, off_t last)
81 {
82 	unsigned int i;
83 	prop_number_t propnum;
84 	struct mbr_part *part;
85 
86 	propnum = prop_dictionary_get(mbr_dict, "index");
87 	PROP_ERR(propnum);
88 
89 	i = prop_uint(propnum);
90 	propnum = prop_dictionary_get(mbr_dict, "flag");
91 	PROP_ERR(propnum);
92 	part = &mbr->mbr_part[i];
93 
94 	part->part_flag = prop_uint8_t(propnum);
95 	propnum = prop_dictionary_get(mbr_dict, "start_head");
96 	PROP_ERR(propnum);
97 	part->part_shd = prop_uint8_t(propnum);
98 	propnum = prop_dictionary_get(mbr_dict, "start_sector");
99 	PROP_ERR(propnum);
100 	part->part_ssect = prop_uint8_t(propnum);
101 	propnum = prop_dictionary_get(mbr_dict, "start_cylinder");
102 	PROP_ERR(propnum);
103 	part->part_scyl = prop_uint8_t(propnum);
104 	propnum = prop_dictionary_get(mbr_dict, "type");
105 	PROP_ERR(propnum);
106 	part->part_typ = prop_uint8_t(propnum);
107 	propnum = prop_dictionary_get(mbr_dict, "end_head");
108 	PROP_ERR(propnum);
109 	part->part_ehd = prop_uint8_t(propnum);
110 	propnum = prop_dictionary_get(mbr_dict, "end_sector");
111 	PROP_ERR(propnum);
112 	part->part_esect = prop_uint8_t(propnum);
113 	propnum = prop_dictionary_get(mbr_dict, "end_cylinder");
114 	PROP_ERR(propnum);
115 	part->part_ecyl = prop_uint8_t(propnum);
116 	propnum = prop_dictionary_get(mbr_dict, "lba_start_low");
117 	PROP_ERR(propnum);
118 	part->part_start_lo = htole16(prop_uint16_t(propnum));
119 	propnum = prop_dictionary_get(mbr_dict, "lba_start_high");
120 	PROP_ERR(propnum);
121 	part->part_start_hi = htole16(prop_uint16_t(propnum));
122 	/* adjust PMBR size to size of device */
123 	if (part->part_typ == MBR_PTYPE_PMBR) {
124 		if (last > 0xffffffff) {
125 			mbr->mbr_part[0].part_size_lo = htole16(0xffff);
126 			mbr->mbr_part[0].part_size_hi = htole16(0xffff);
127 		} else {
128 			mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last);
129 			mbr->mbr_part[0].part_size_hi = htole16(
130 			    (uint16_t)(last >> 16));
131 		}
132 	} else {
133 		propnum = prop_dictionary_get(mbr_dict, "lba_size_low");
134 		PROP_ERR(propnum);
135 		part->part_size_lo = htole16(prop_uint16_t(propnum));
136 		propnum = prop_dictionary_get(mbr_dict, "lba_size_high");
137 		PROP_ERR(propnum);
138 		part->part_size_hi = htole16(prop_uint16_t(propnum));
139 	}
140 	return 0;
141 }
142 
143 static int
144 restore_ent(gpt_t gpt, prop_dictionary_t gpt_dict, void *secbuf, u_int gpt_size,
145     u_int entries)
146 {
147 	unsigned int i;
148 	struct gpt_ent ent;
149 	const char *s;
150 	prop_string_t propstr;
151 	prop_number_t propnum;
152 
153 	memset(&ent, 0, sizeof(ent));
154 	propstr = prop_dictionary_get(gpt_dict, "type");
155 	PROP_ERR(propstr);
156 	s = prop_string_cstring_nocopy(propstr);
157 	if (gpt_uuid_parse(s, ent.ent_type) != 0) {
158 		gpt_warnx(gpt, "%s: not able to convert to an UUID", s);
159 		return -1;
160 	}
161 	propstr = prop_dictionary_get(gpt_dict, "guid");
162 	PROP_ERR(propstr);
163 	s = prop_string_cstring_nocopy(propstr);
164 	if (gpt_uuid_parse(s, ent.ent_guid) != 0) {
165 		gpt_warnx(gpt, "%s: not able to convert to an UUID", s);
166 		return -1;
167 	}
168 	propnum = prop_dictionary_get(gpt_dict, "start");
169 	PROP_ERR(propnum);
170 	ent.ent_lba_start = htole64(prop_uint(propnum));
171 	propnum = prop_dictionary_get(gpt_dict, "end");
172 	PROP_ERR(propnum);
173 	ent.ent_lba_end = htole64(prop_uint(propnum));
174 	propnum = prop_dictionary_get(gpt_dict, "attributes");
175 	PROP_ERR(propnum);
176 	ent.ent_attr = htole64(prop_uint(propnum));
177 	propstr = prop_dictionary_get(gpt_dict, "name");
178 	if (propstr != NULL) {
179 		s = prop_string_cstring_nocopy(propstr);
180 		utf8_to_utf16((const uint8_t *)s, ent.ent_name,
181 		    __arraycount(ent.ent_name));
182 	}
183 	propnum = prop_dictionary_get(gpt_dict, "index");
184 	PROP_ERR(propnum);
185 	i = prop_uint(propnum);
186 	if (i > entries) {
187 		gpt_warnx(gpt, "Entity index out of bounds %u > %u\n",
188 		    i, entries);
189 		return -1;
190 	}
191 	memcpy((char *)secbuf + gpt->secsz + ((i - 1) * sizeof(ent)),
192 	    &ent, sizeof(ent));
193 	return 0;
194 }
195 
196 static int
197 restore(gpt_t gpt, const char *infile, int force)
198 {
199 	gpt_uuid_t gpt_guid, uuid;
200 	off_t firstdata, last, lastdata, gpe_start, gpe_end;
201 	map_t map;
202 	struct mbr *mbr;
203 	struct gpt_hdr *hdr;
204 	unsigned int i, gpt_size;
205 	prop_dictionary_t props, gpt_dict, mbr_dict, type_dict;
206 	prop_object_iterator_t propiter;
207 	prop_data_t propdata;
208 	prop_array_t mbr_array, gpt_array;
209 	prop_number_t propnum;
210 	prop_string_t propstr;
211 	unsigned int entries;
212 	const char *s;
213 	void *secbuf = NULL;;
214 	int rv = -1;
215 
216 	last = gpt->mediasz / gpt->secsz - 1LL;
217 
218 	if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) != NULL ||
219 	    map_find(gpt, MAP_TYPE_SEC_GPT_HDR) != NULL) {
220 		if (!force) {
221 			gpt_warnx(gpt, "Device contains a GPT");
222 			return -1;
223 		}
224 	}
225 	map = map_find(gpt, MAP_TYPE_MBR);
226 	if (map != NULL) {
227 		if (!force) {
228 			gpt_warnx(gpt, "Device contains an MBR");
229 			return -1;
230 		}
231 		/* Nuke the MBR in our internal map. */
232 		map->map_type = MAP_TYPE_UNUSED;
233 	}
234 
235 	props = prop_dictionary_internalize_from_file(
236 	    strcmp(infile, "-") == 0 ? "/dev/stdin" : infile);
237 	if (props == NULL) {
238 		gpt_warnx(gpt, "Unable to read/parse backup file");
239 		return -1;
240 	}
241 
242 	propnum = prop_dictionary_get(props, "sector_size");
243 	PROP_ERR(propnum);
244 	if (!prop_number_equals_integer(propnum, gpt->secsz)) {
245 		gpt_warnx(gpt, "Sector size does not match backup");
246 		prop_object_release(props);
247 		return -1;
248 	}
249 
250 	gpt_dict = prop_dictionary_get(props, "GPT_HDR");
251 	PROP_ERR(gpt_dict);
252 
253 	propnum = prop_dictionary_get(gpt_dict, "revision");
254 	PROP_ERR(propnum);
255 	if (!prop_number_equals_unsigned_integer(propnum, 0x10000)) {
256 		gpt_warnx(gpt, "backup is not revision 1.0");
257 		prop_object_release(gpt_dict);
258 		prop_object_release(props);
259 		return -1;
260 	}
261 
262 	propnum = prop_dictionary_get(gpt_dict, "entries");
263 	PROP_ERR(propnum);
264 	entries = prop_uint(propnum);
265 	gpt_size = (u_int)(entries * sizeof(struct gpt_ent) / gpt->secsz);
266 	if (gpt_size * sizeof(struct gpt_ent) % gpt->secsz)
267 		gpt_size++;
268 
269 	propstr = prop_dictionary_get(gpt_dict, "guid");
270 	PROP_ERR(propstr);
271 	s = prop_string_cstring_nocopy(propstr);
272 	if (gpt_uuid_parse(s, gpt_guid) != 0) {
273 		gpt_warnx(gpt, "%s: not able to convert to an UUID", s);
274 		goto out;
275 	}
276 	firstdata = gpt_size + 2;		/* PMBR and GPT header */
277 	lastdata = last - gpt_size - 1;		/* alt. GPT table and header */
278 
279 	type_dict = prop_dictionary_get(props, "GPT_TBL");
280 	PROP_ERR(type_dict);
281 	gpt_array = prop_dictionary_get(type_dict, "gpt_array");
282 	PROP_ERR(gpt_array);
283 	propiter = prop_array_iterator(gpt_array);
284 	PROP_ERR(propiter);
285 	while ((gpt_dict = prop_object_iterator_next(propiter)) != NULL) {
286 		propstr = prop_dictionary_get(gpt_dict, "type");
287 		PROP_ERR(propstr);
288 		s = prop_string_cstring_nocopy(propstr);
289 		if (gpt_uuid_parse(s, uuid) != 0) {
290 			gpt_warnx(gpt, "%s: not able to convert to an UUID", s);
291 			goto out;
292 		}
293 		if (gpt_uuid_is_nil(uuid))
294 			continue;
295 		propnum = prop_dictionary_get(gpt_dict, "start");
296 		PROP_ERR(propnum);
297 		gpe_start = prop_uint(propnum);
298 		propnum = prop_dictionary_get(gpt_dict, "end");
299 		PROP_ERR(propnum);
300 		gpe_end = prop_uint(propnum);
301 		if (gpe_start < firstdata || gpe_end > lastdata) {
302 			gpt_warnx(gpt, "Backup GPT doesn't fit");
303 			goto out;
304 		}
305 	}
306 	prop_object_iterator_release(propiter);
307 
308 	/* GPT TABLE + GPT HEADER */
309 	if ((secbuf = calloc(gpt_size + 1, gpt->secsz)) == NULL) {
310 		gpt_warn(gpt, "not enough memory to create a sector buffer");
311 		goto out;
312 	}
313 
314 	if (lseek(gpt->fd, 0LL, SEEK_SET) == -1) {
315 		gpt_warn(gpt, "Can't seek to beginning");
316 		goto out;
317 	}
318 	for (i = 0; i < firstdata; i++) {
319 		if (write(gpt->fd, secbuf, gpt->secsz) != (ssize_t)gpt->secsz) {
320 			gpt_warn(gpt, "Error writing");
321 			goto out;
322 		}
323 	}
324 	if (lseek(gpt->fd, (lastdata + 1) * gpt->secsz, SEEK_SET) == -1) {
325 		gpt_warn(gpt, "Can't seek to end");
326 		goto out;
327 	}
328 	for (i = (u_int)(lastdata + 1); i <= (u_int)last; i++) {
329 		if (write(gpt->fd, secbuf, gpt->secsz) != (ssize_t)gpt->secsz) {
330 			gpt_warn(gpt, "Error writing");
331 			goto out;
332 		}
333 	}
334 
335 	mbr = secbuf;
336 	type_dict = prop_dictionary_get(props, "MBR");
337 	PROP_ERR(type_dict);
338 	propdata = prop_dictionary_get(type_dict, "code");
339 	PROP_ERR(propdata);
340 	memcpy(mbr->mbr_code, prop_data_data_nocopy(propdata),
341 	    sizeof(mbr->mbr_code));
342 	mbr_array = prop_dictionary_get(type_dict, "mbr_array");
343 	PROP_ERR(mbr_array);
344 	propiter = prop_array_iterator(mbr_array);
345 	PROP_ERR(propiter);
346 	while ((mbr_dict = prop_object_iterator_next(propiter)) != NULL) {
347 		if (restore_mbr(gpt, mbr, mbr_dict, last) == -1)
348 			goto out;
349 	}
350 
351 	prop_object_iterator_release(propiter);
352 	mbr->mbr_sig = htole16(MBR_SIG);
353 	if (lseek(gpt->fd, 0LL, SEEK_SET) == -1 ||
354 	    write(gpt->fd, mbr, gpt->secsz) != (ssize_t)gpt->secsz) {
355 		gpt_warn(gpt, "Unable to seek/write MBR");
356 		return -1;
357 	}
358 
359 	propiter = prop_array_iterator(gpt_array);
360 	PROP_ERR(propiter);
361 
362 	while ((gpt_dict = prop_object_iterator_next(propiter)) != NULL) {
363 		if (restore_ent(gpt, gpt_dict, secbuf, gpt_size, entries) == -1)
364 			goto out;
365 	}
366 	prop_object_iterator_release(propiter);
367 
368 	size_t len = gpt_size * gpt->secsz;
369 	if (lseek(gpt->fd, 2 * gpt->secsz, SEEK_SET) == -1 ||
370 	    write(gpt->fd, (char *)secbuf + gpt->secsz, len) != (ssize_t) len) {
371 		gpt_warn(gpt, "Unable to write primary GPT");
372 		goto out;
373 	}
374 
375 	if (lseek(gpt->fd, (lastdata + 1) * gpt->secsz, SEEK_SET) == -1 ||
376 	    write(gpt->fd, (char *)secbuf + gpt->secsz, len) != (ssize_t) len) {
377 		gpt_warn(gpt, "Unable to write secondary GPT");
378 		goto out;
379 	}
380 
381 	memset(secbuf, 0, gpt->secsz);
382 	hdr = secbuf;
383 	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
384 	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
385 	hdr->hdr_size = htole32(GPT_HDR_SIZE);
386 	hdr->hdr_lba_self = htole64(GPT_HDR_BLKNO);
387 	hdr->hdr_lba_alt = htole64((uint64_t)last);
388 	hdr->hdr_lba_start = htole64((uint64_t)firstdata);
389 	hdr->hdr_lba_end = htole64((uint64_t)lastdata);
390 	gpt_uuid_copy(hdr->hdr_guid, gpt_guid);
391 	hdr->hdr_lba_table = htole64(2);
392 	hdr->hdr_entries = htole32(entries);
393 	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
394 	hdr->hdr_crc_table = htole32(crc32((char *)secbuf + gpt->secsz, len));
395 	hdr->hdr_crc_self = htole32(crc32(hdr, GPT_HDR_SIZE));
396 	if (lseek(gpt->fd, gpt->secsz, SEEK_SET) == -1 ||
397 	    write(gpt->fd, hdr, gpt->secsz) != (ssize_t)gpt->secsz) {
398 		gpt_warn(gpt, "Unable to write primary header");
399 		goto out;
400 	}
401 
402 	hdr->hdr_lba_self = htole64((uint64_t)last);
403 	hdr->hdr_lba_alt = htole64(GPT_HDR_BLKNO);
404 	hdr->hdr_lba_table = htole64((uint64_t)(lastdata + 1));
405 	hdr->hdr_crc_self = 0;
406 	hdr->hdr_crc_self = htole32(crc32(hdr, GPT_HDR_SIZE));
407 	if (lseek(gpt->fd, last * gpt->secsz, SEEK_SET) == -1 ||
408 	    write(gpt->fd, hdr, gpt->secsz) != (ssize_t)gpt->secsz) {
409 		gpt_warn(gpt, "Unable to write secondary header");
410 		goto out;
411 	}
412 	rv = 0;
413 
414 out:
415 	free(secbuf);
416 	prop_object_release(props);
417 	return rv;
418 }
419 
420 static int
421 cmd_restore(gpt_t gpt, int argc, char *argv[])
422 {
423 	int ch;
424 	int force = 0;
425 	const char *infile = "-";
426 
427 	while ((ch = getopt(argc, argv, "Fi:")) != -1) {
428 		switch(ch) {
429 		case 'i':
430 			infile = optarg;
431 			break;
432 		case 'F':
433 			force = 1;
434 			break;
435 		default:
436 			return usage();
437 		}
438 	}
439 
440 	if (argc != optind)
441 		return usage();
442 
443 	return restore(gpt, infile, force);
444 }
445