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