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