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