backup.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/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
34 1.1 jnemeth #endif
35 1.1 jnemeth #ifdef __RCSID
36 1.4 christos __RCSID("$NetBSD: backup.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/bootblock.h>
40 1.1 jnemeth #include <sys/types.h>
41 1.1 jnemeth
42 1.1 jnemeth #include <err.h>
43 1.1 jnemeth #include <stddef.h>
44 1.1 jnemeth #include <stdio.h>
45 1.1 jnemeth #include <stdlib.h>
46 1.1 jnemeth #include <string.h>
47 1.1 jnemeth #include <unistd.h>
48 1.1 jnemeth #include <prop/proplib.h>
49 1.1 jnemeth
50 1.1 jnemeth #include "map.h"
51 1.1 jnemeth #include "gpt.h"
52 1.1 jnemeth
53 1.1 jnemeth
54 1.1 jnemeth const char backupmsg[] = "backup device ...";
55 1.1 jnemeth
56 1.1 jnemeth __dead static void
57 1.1 jnemeth usage_backup(void)
58 1.1 jnemeth {
59 1.1 jnemeth
60 1.1 jnemeth fprintf(stderr,
61 1.1 jnemeth "usage: %s %s\n", getprogname(), backupmsg);
62 1.1 jnemeth exit(1);
63 1.1 jnemeth }
64 1.1 jnemeth
65 1.1 jnemeth #define PROP_ERR(x) if (!(x)) { \
66 1.1 jnemeth warn("proplib failure"); \
67 1.1 jnemeth return; \
68 1.1 jnemeth }
69 1.1 jnemeth
70 1.1 jnemeth static void
71 1.1 jnemeth backup(void)
72 1.1 jnemeth {
73 1.1 jnemeth uuid_t u;
74 1.1 jnemeth map_t *m;
75 1.1 jnemeth struct mbr *mbr;
76 1.1 jnemeth struct gpt_ent *ent;
77 1.1 jnemeth struct gpt_hdr *hdr;
78 1.1 jnemeth unsigned int i;
79 1.1 jnemeth prop_dictionary_t props, mbr_dict, gpt_dict, type_dict;
80 1.1 jnemeth prop_array_t mbr_array, gpt_array;
81 1.1 jnemeth prop_data_t propdata;
82 1.1 jnemeth prop_number_t propnum;
83 1.1 jnemeth prop_string_t propstr;
84 1.1 jnemeth char *propext, *s;
85 1.1 jnemeth bool rc;
86 1.1 jnemeth
87 1.1 jnemeth props = prop_dictionary_create();
88 1.1 jnemeth PROP_ERR(props);
89 1.2 jnemeth propnum = prop_number_create_integer(secsz);
90 1.2 jnemeth PROP_ERR(propnum);
91 1.2 jnemeth rc = prop_dictionary_set(props, "sector_size", propnum);
92 1.2 jnemeth PROP_ERR(rc);
93 1.1 jnemeth m = map_first();
94 1.1 jnemeth while (m != NULL) {
95 1.1 jnemeth switch (m->map_type) {
96 1.1 jnemeth case MAP_TYPE_MBR:
97 1.1 jnemeth case MAP_TYPE_PMBR:
98 1.1 jnemeth type_dict = prop_dictionary_create();
99 1.1 jnemeth PROP_ERR(type_dict);
100 1.1 jnemeth mbr = m->map_data;
101 1.1 jnemeth propdata = prop_data_create_data_nocopy(mbr->mbr_code,
102 1.1 jnemeth sizeof(mbr->mbr_code));
103 1.1 jnemeth PROP_ERR(propdata);
104 1.1 jnemeth rc = prop_dictionary_set(type_dict, "code", propdata);
105 1.1 jnemeth PROP_ERR(rc);
106 1.1 jnemeth mbr_array = NULL;
107 1.1 jnemeth for (i = 0; i < 4; i++) {
108 1.1 jnemeth if (mbr->mbr_part[i].part_typ !=
109 1.1 jnemeth MBR_PTYPE_UNUSED) {
110 1.1 jnemeth mbr_dict = prop_dictionary_create();
111 1.1 jnemeth PROP_ERR(mbr_dict);
112 1.1 jnemeth propnum = prop_number_create_integer(i);
113 1.1 jnemeth PROP_ERR(propnum);
114 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
115 1.1 jnemeth "index", propnum);
116 1.1 jnemeth PROP_ERR(rc);
117 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_flag);
118 1.1 jnemeth PROP_ERR(propnum);
119 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
120 1.1 jnemeth "flag", propnum);
121 1.1 jnemeth PROP_ERR(rc);
122 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_shd);
123 1.1 jnemeth PROP_ERR(propnum);
124 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
125 1.1 jnemeth "start_head", propnum);
126 1.1 jnemeth PROP_ERR(rc);
127 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ssect);
128 1.1 jnemeth PROP_ERR(propnum);
129 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
130 1.1 jnemeth "start_sector", propnum);
131 1.1 jnemeth PROP_ERR(rc);
132 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_scyl);
133 1.1 jnemeth PROP_ERR(propnum);
134 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
135 1.1 jnemeth "start_cylinder", propnum);
136 1.1 jnemeth PROP_ERR(rc);
137 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_typ);
138 1.1 jnemeth PROP_ERR(propnum);
139 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
140 1.1 jnemeth "type", propnum);
141 1.1 jnemeth PROP_ERR(rc);
142 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ehd);
143 1.1 jnemeth PROP_ERR(propnum);
144 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
145 1.1 jnemeth "end_head", propnum);
146 1.1 jnemeth PROP_ERR(rc);
147 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_esect);
148 1.1 jnemeth PROP_ERR(propnum);
149 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
150 1.2 jnemeth "end_sector", propnum);
151 1.2 jnemeth PROP_ERR(rc);
152 1.2 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ecyl);
153 1.2 jnemeth PROP_ERR(propnum);
154 1.2 jnemeth rc = prop_dictionary_set(mbr_dict,
155 1.1 jnemeth "end_cylinder", propnum);
156 1.1 jnemeth PROP_ERR(rc);
157 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_lo));
158 1.1 jnemeth PROP_ERR(propnum);
159 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
160 1.1 jnemeth "lba_start_low", propnum);
161 1.1 jnemeth PROP_ERR(rc);
162 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_hi));
163 1.1 jnemeth PROP_ERR(propnum);
164 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
165 1.1 jnemeth "lba_start_high", propnum);
166 1.1 jnemeth PROP_ERR(rc);
167 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_lo));
168 1.1 jnemeth PROP_ERR(propnum);
169 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
170 1.1 jnemeth "lba_size_low", propnum);
171 1.1 jnemeth PROP_ERR(rc);
172 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_hi));
173 1.1 jnemeth PROP_ERR(propnum);
174 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
175 1.1 jnemeth "lba_size_high", propnum);
176 1.1 jnemeth if (mbr_array == NULL) {
177 1.1 jnemeth mbr_array = prop_array_create();
178 1.1 jnemeth PROP_ERR(mbr_array);
179 1.1 jnemeth }
180 1.1 jnemeth rc = prop_array_add(mbr_array,
181 1.1 jnemeth mbr_dict);
182 1.1 jnemeth PROP_ERR(rc);
183 1.1 jnemeth }
184 1.1 jnemeth }
185 1.1 jnemeth if (mbr_array != NULL) {
186 1.1 jnemeth rc = prop_dictionary_set(type_dict,
187 1.1 jnemeth "mbr_array", mbr_array);
188 1.1 jnemeth PROP_ERR(rc);
189 1.1 jnemeth prop_object_release(mbr_array);
190 1.1 jnemeth }
191 1.1 jnemeth rc = prop_dictionary_set(props, "MBR", type_dict);
192 1.1 jnemeth PROP_ERR(rc);
193 1.1 jnemeth prop_object_release(type_dict);
194 1.1 jnemeth break;
195 1.1 jnemeth case MAP_TYPE_PRI_GPT_HDR:
196 1.1 jnemeth type_dict = prop_dictionary_create();
197 1.1 jnemeth PROP_ERR(type_dict);
198 1.1 jnemeth hdr = m->map_data;
199 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le32toh(hdr->hdr_revision));
200 1.1 jnemeth PROP_ERR(propnum);
201 1.1 jnemeth rc = prop_dictionary_set(type_dict, "revision",
202 1.1 jnemeth propnum);
203 1.1 jnemeth PROP_ERR(rc);
204 1.1 jnemeth le_uuid_dec(hdr->hdr_guid, &u);
205 1.1 jnemeth uuid_to_string(&u, &s, NULL);
206 1.1 jnemeth propstr = prop_string_create_cstring(s);
207 1.1 jnemeth free(s);
208 1.1 jnemeth PROP_ERR(propstr);
209 1.1 jnemeth rc = prop_dictionary_set(type_dict, "guid", propstr);
210 1.1 jnemeth PROP_ERR(rc);
211 1.1 jnemeth propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
212 1.1 jnemeth PROP_ERR(propnum);
213 1.1 jnemeth rc = prop_dictionary_set(type_dict, "entries", propnum);
214 1.1 jnemeth PROP_ERR(rc);
215 1.1 jnemeth rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
216 1.1 jnemeth PROP_ERR(rc);
217 1.1 jnemeth prop_object_release(type_dict);
218 1.1 jnemeth break;
219 1.1 jnemeth case MAP_TYPE_PRI_GPT_TBL:
220 1.1 jnemeth type_dict = prop_dictionary_create();
221 1.1 jnemeth PROP_ERR(type_dict);
222 1.1 jnemeth ent = m->map_data;
223 1.3 jnemeth gpt_array = prop_array_create();
224 1.3 jnemeth PROP_ERR(gpt_array);
225 1.1 jnemeth for (i = 1, ent = m->map_data;
226 1.1 jnemeth (char *)ent < (char *)(m->map_data) +
227 1.1 jnemeth m->map_size * secsz; i++, ent++) {
228 1.1 jnemeth gpt_dict = prop_dictionary_create();
229 1.1 jnemeth PROP_ERR(gpt_dict);
230 1.1 jnemeth propnum = prop_number_create_integer(i);
231 1.1 jnemeth PROP_ERR(propnum);
232 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "index",
233 1.1 jnemeth propnum);
234 1.1 jnemeth PROP_ERR(propnum);
235 1.1 jnemeth le_uuid_dec(ent->ent_type, &u);
236 1.1 jnemeth uuid_to_string(&u, &s, NULL);
237 1.1 jnemeth propstr = prop_string_create_cstring(s);
238 1.1 jnemeth free(s);
239 1.1 jnemeth PROP_ERR(propstr);
240 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "type",
241 1.1 jnemeth propstr);
242 1.1 jnemeth le_uuid_dec(ent->ent_guid, &u);
243 1.1 jnemeth uuid_to_string(&u, &s, NULL);
244 1.1 jnemeth propstr = prop_string_create_cstring(s);
245 1.1 jnemeth free(s);
246 1.1 jnemeth PROP_ERR(propstr);
247 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "guid",
248 1.1 jnemeth propstr);
249 1.1 jnemeth PROP_ERR(propstr);
250 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_start));
251 1.1 jnemeth PROP_ERR(propnum);
252 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "start",
253 1.1 jnemeth propnum);
254 1.1 jnemeth PROP_ERR(rc);
255 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_end));
256 1.1 jnemeth PROP_ERR(rc);
257 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "end",
258 1.1 jnemeth propnum);
259 1.1 jnemeth PROP_ERR(rc);
260 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_attr));
261 1.1 jnemeth PROP_ERR(propnum);
262 1.1 jnemeth rc = prop_dictionary_set(gpt_dict,
263 1.1 jnemeth "attributes", propnum);
264 1.1 jnemeth PROP_ERR(rc);
265 1.1 jnemeth s = (char *)utf16_to_utf8(ent->ent_name);
266 1.1 jnemeth if (*s != '\0') {
267 1.1 jnemeth propstr = prop_string_create_cstring(s);
268 1.1 jnemeth PROP_ERR(propstr);
269 1.1 jnemeth rc = prop_dictionary_set(gpt_dict,
270 1.1 jnemeth "name", propstr);
271 1.1 jnemeth PROP_ERR(rc);
272 1.1 jnemeth }
273 1.1 jnemeth rc = prop_array_add(gpt_array, gpt_dict);
274 1.1 jnemeth PROP_ERR(rc);
275 1.1 jnemeth }
276 1.3 jnemeth rc = prop_dictionary_set(type_dict,
277 1.3 jnemeth "gpt_array", gpt_array);
278 1.3 jnemeth PROP_ERR(rc);
279 1.3 jnemeth prop_object_release(gpt_array);
280 1.1 jnemeth rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
281 1.1 jnemeth PROP_ERR(rc);
282 1.1 jnemeth prop_object_release(type_dict);
283 1.1 jnemeth break;
284 1.1 jnemeth }
285 1.1 jnemeth m = m->map_next;
286 1.1 jnemeth }
287 1.1 jnemeth propext = prop_dictionary_externalize(props);
288 1.1 jnemeth PROP_ERR(propext);
289 1.1 jnemeth prop_object_release(props);
290 1.1 jnemeth fputs(propext, stdout);
291 1.3 jnemeth free(propext);
292 1.1 jnemeth }
293 1.1 jnemeth
294 1.1 jnemeth int
295 1.1 jnemeth cmd_backup(int argc, char *argv[])
296 1.1 jnemeth {
297 1.1 jnemeth int fd;
298 1.1 jnemeth
299 1.1 jnemeth if (argc == optind)
300 1.1 jnemeth usage_backup();
301 1.1 jnemeth
302 1.1 jnemeth while (optind < argc) {
303 1.1 jnemeth fd = gpt_open(argv[optind++]);
304 1.1 jnemeth if (fd == -1) {
305 1.1 jnemeth warn("unable to open device '%s'", device_name);
306 1.1 jnemeth continue;
307 1.1 jnemeth }
308 1.1 jnemeth
309 1.1 jnemeth backup();
310 1.1 jnemeth
311 1.1 jnemeth gpt_close(fd);
312 1.1 jnemeth }
313 1.1 jnemeth
314 1.1 jnemeth return (0);
315 1.1 jnemeth }
316