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