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