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