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