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