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