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