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