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