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