backup.c revision 1.3 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.1 jnemeth #include <sys/cdefs.h>
28 1.1 jnemeth #ifdef __FBSDID
29 1.1 jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
30 1.1 jnemeth #endif
31 1.1 jnemeth #ifdef __RCSID
32 1.3 jnemeth __RCSID("$NetBSD: backup.c,v 1.3 2014/09/10 10:49:44 jnemeth Exp $");
33 1.1 jnemeth #endif
34 1.1 jnemeth
35 1.1 jnemeth #include <sys/bootblock.h>
36 1.1 jnemeth #include <sys/types.h>
37 1.1 jnemeth
38 1.1 jnemeth #include <err.h>
39 1.1 jnemeth #include <stddef.h>
40 1.1 jnemeth #include <stdio.h>
41 1.1 jnemeth #include <stdlib.h>
42 1.1 jnemeth #include <string.h>
43 1.1 jnemeth #include <unistd.h>
44 1.1 jnemeth #include <prop/proplib.h>
45 1.1 jnemeth
46 1.1 jnemeth #include "map.h"
47 1.1 jnemeth #include "gpt.h"
48 1.1 jnemeth
49 1.1 jnemeth
50 1.1 jnemeth const char backupmsg[] = "backup device ...";
51 1.1 jnemeth
52 1.1 jnemeth __dead static void
53 1.1 jnemeth usage_backup(void)
54 1.1 jnemeth {
55 1.1 jnemeth
56 1.1 jnemeth fprintf(stderr,
57 1.1 jnemeth "usage: %s %s\n", getprogname(), backupmsg);
58 1.1 jnemeth exit(1);
59 1.1 jnemeth }
60 1.1 jnemeth
61 1.1 jnemeth #define PROP_ERR(x) if (!(x)) { \
62 1.1 jnemeth warn("proplib failure"); \
63 1.1 jnemeth return; \
64 1.1 jnemeth }
65 1.1 jnemeth
66 1.1 jnemeth static void
67 1.1 jnemeth backup(void)
68 1.1 jnemeth {
69 1.1 jnemeth uuid_t u;
70 1.1 jnemeth map_t *m;
71 1.1 jnemeth struct mbr *mbr;
72 1.1 jnemeth struct gpt_ent *ent;
73 1.1 jnemeth struct gpt_hdr *hdr;
74 1.1 jnemeth unsigned int i;
75 1.1 jnemeth prop_dictionary_t props, mbr_dict, gpt_dict, type_dict;
76 1.1 jnemeth prop_array_t mbr_array, gpt_array;
77 1.1 jnemeth prop_data_t propdata;
78 1.1 jnemeth prop_number_t propnum;
79 1.1 jnemeth prop_string_t propstr;
80 1.1 jnemeth char *propext, *s;
81 1.1 jnemeth bool rc;
82 1.1 jnemeth
83 1.1 jnemeth props = prop_dictionary_create();
84 1.1 jnemeth PROP_ERR(props);
85 1.2 jnemeth propnum = prop_number_create_integer(secsz);
86 1.2 jnemeth PROP_ERR(propnum);
87 1.2 jnemeth rc = prop_dictionary_set(props, "sector_size", propnum);
88 1.2 jnemeth PROP_ERR(rc);
89 1.1 jnemeth m = map_first();
90 1.1 jnemeth while (m != NULL) {
91 1.1 jnemeth switch (m->map_type) {
92 1.1 jnemeth case MAP_TYPE_MBR:
93 1.1 jnemeth case MAP_TYPE_PMBR:
94 1.1 jnemeth type_dict = prop_dictionary_create();
95 1.1 jnemeth PROP_ERR(type_dict);
96 1.1 jnemeth mbr = m->map_data;
97 1.1 jnemeth propdata = prop_data_create_data_nocopy(mbr->mbr_code,
98 1.1 jnemeth sizeof(mbr->mbr_code));
99 1.1 jnemeth PROP_ERR(propdata);
100 1.1 jnemeth rc = prop_dictionary_set(type_dict, "code", propdata);
101 1.1 jnemeth PROP_ERR(rc);
102 1.1 jnemeth mbr_array = NULL;
103 1.1 jnemeth for (i = 0; i < 4; i++) {
104 1.1 jnemeth if (mbr->mbr_part[i].part_typ !=
105 1.1 jnemeth MBR_PTYPE_UNUSED) {
106 1.1 jnemeth mbr_dict = prop_dictionary_create();
107 1.1 jnemeth PROP_ERR(mbr_dict);
108 1.1 jnemeth propnum = prop_number_create_integer(i);
109 1.1 jnemeth PROP_ERR(propnum);
110 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
111 1.1 jnemeth "index", propnum);
112 1.1 jnemeth PROP_ERR(rc);
113 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_flag);
114 1.1 jnemeth PROP_ERR(propnum);
115 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
116 1.1 jnemeth "flag", propnum);
117 1.1 jnemeth PROP_ERR(rc);
118 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_shd);
119 1.1 jnemeth PROP_ERR(propnum);
120 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
121 1.1 jnemeth "start_head", propnum);
122 1.1 jnemeth PROP_ERR(rc);
123 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ssect);
124 1.1 jnemeth PROP_ERR(propnum);
125 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
126 1.1 jnemeth "start_sector", propnum);
127 1.1 jnemeth PROP_ERR(rc);
128 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_scyl);
129 1.1 jnemeth PROP_ERR(propnum);
130 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
131 1.1 jnemeth "start_cylinder", propnum);
132 1.1 jnemeth PROP_ERR(rc);
133 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_typ);
134 1.1 jnemeth PROP_ERR(propnum);
135 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
136 1.1 jnemeth "type", propnum);
137 1.1 jnemeth PROP_ERR(rc);
138 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ehd);
139 1.1 jnemeth PROP_ERR(propnum);
140 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
141 1.1 jnemeth "end_head", propnum);
142 1.1 jnemeth PROP_ERR(rc);
143 1.1 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_esect);
144 1.1 jnemeth PROP_ERR(propnum);
145 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
146 1.2 jnemeth "end_sector", propnum);
147 1.2 jnemeth PROP_ERR(rc);
148 1.2 jnemeth propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ecyl);
149 1.2 jnemeth PROP_ERR(propnum);
150 1.2 jnemeth rc = prop_dictionary_set(mbr_dict,
151 1.1 jnemeth "end_cylinder", propnum);
152 1.1 jnemeth PROP_ERR(rc);
153 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_lo));
154 1.1 jnemeth PROP_ERR(propnum);
155 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
156 1.1 jnemeth "lba_start_low", propnum);
157 1.1 jnemeth PROP_ERR(rc);
158 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_hi));
159 1.1 jnemeth PROP_ERR(propnum);
160 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
161 1.1 jnemeth "lba_start_high", propnum);
162 1.1 jnemeth PROP_ERR(rc);
163 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_lo));
164 1.1 jnemeth PROP_ERR(propnum);
165 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
166 1.1 jnemeth "lba_size_low", propnum);
167 1.1 jnemeth PROP_ERR(rc);
168 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_hi));
169 1.1 jnemeth PROP_ERR(propnum);
170 1.1 jnemeth rc = prop_dictionary_set(mbr_dict,
171 1.1 jnemeth "lba_size_high", propnum);
172 1.1 jnemeth if (mbr_array == NULL) {
173 1.1 jnemeth mbr_array = prop_array_create();
174 1.1 jnemeth PROP_ERR(mbr_array);
175 1.1 jnemeth }
176 1.1 jnemeth rc = prop_array_add(mbr_array,
177 1.1 jnemeth mbr_dict);
178 1.1 jnemeth PROP_ERR(rc);
179 1.1 jnemeth }
180 1.1 jnemeth }
181 1.1 jnemeth if (mbr_array != NULL) {
182 1.1 jnemeth rc = prop_dictionary_set(type_dict,
183 1.1 jnemeth "mbr_array", mbr_array);
184 1.1 jnemeth PROP_ERR(rc);
185 1.1 jnemeth prop_object_release(mbr_array);
186 1.1 jnemeth }
187 1.1 jnemeth rc = prop_dictionary_set(props, "MBR", type_dict);
188 1.1 jnemeth PROP_ERR(rc);
189 1.1 jnemeth prop_object_release(type_dict);
190 1.1 jnemeth break;
191 1.1 jnemeth case MAP_TYPE_PRI_GPT_HDR:
192 1.1 jnemeth type_dict = prop_dictionary_create();
193 1.1 jnemeth PROP_ERR(type_dict);
194 1.1 jnemeth hdr = m->map_data;
195 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le32toh(hdr->hdr_revision));
196 1.1 jnemeth PROP_ERR(propnum);
197 1.1 jnemeth rc = prop_dictionary_set(type_dict, "revision",
198 1.1 jnemeth propnum);
199 1.1 jnemeth PROP_ERR(rc);
200 1.1 jnemeth le_uuid_dec(hdr->hdr_guid, &u);
201 1.1 jnemeth uuid_to_string(&u, &s, NULL);
202 1.1 jnemeth propstr = prop_string_create_cstring(s);
203 1.1 jnemeth free(s);
204 1.1 jnemeth PROP_ERR(propstr);
205 1.1 jnemeth rc = prop_dictionary_set(type_dict, "guid", propstr);
206 1.1 jnemeth PROP_ERR(rc);
207 1.1 jnemeth propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
208 1.1 jnemeth PROP_ERR(propnum);
209 1.1 jnemeth rc = prop_dictionary_set(type_dict, "entries", propnum);
210 1.1 jnemeth PROP_ERR(rc);
211 1.1 jnemeth rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
212 1.1 jnemeth PROP_ERR(rc);
213 1.1 jnemeth prop_object_release(type_dict);
214 1.1 jnemeth break;
215 1.1 jnemeth case MAP_TYPE_PRI_GPT_TBL:
216 1.1 jnemeth type_dict = prop_dictionary_create();
217 1.1 jnemeth PROP_ERR(type_dict);
218 1.1 jnemeth ent = m->map_data;
219 1.3 jnemeth gpt_array = prop_array_create();
220 1.3 jnemeth PROP_ERR(gpt_array);
221 1.1 jnemeth for (i = 1, ent = m->map_data;
222 1.1 jnemeth (char *)ent < (char *)(m->map_data) +
223 1.1 jnemeth m->map_size * secsz; i++, ent++) {
224 1.1 jnemeth gpt_dict = prop_dictionary_create();
225 1.1 jnemeth PROP_ERR(gpt_dict);
226 1.1 jnemeth propnum = prop_number_create_integer(i);
227 1.1 jnemeth PROP_ERR(propnum);
228 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "index",
229 1.1 jnemeth propnum);
230 1.1 jnemeth PROP_ERR(propnum);
231 1.1 jnemeth le_uuid_dec(ent->ent_type, &u);
232 1.1 jnemeth uuid_to_string(&u, &s, NULL);
233 1.1 jnemeth propstr = prop_string_create_cstring(s);
234 1.1 jnemeth free(s);
235 1.1 jnemeth PROP_ERR(propstr);
236 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "type",
237 1.1 jnemeth propstr);
238 1.1 jnemeth le_uuid_dec(ent->ent_guid, &u);
239 1.1 jnemeth uuid_to_string(&u, &s, NULL);
240 1.1 jnemeth propstr = prop_string_create_cstring(s);
241 1.1 jnemeth free(s);
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
305 1.1 jnemeth backup();
306 1.1 jnemeth
307 1.1 jnemeth gpt_close(fd);
308 1.1 jnemeth }
309 1.1 jnemeth
310 1.1 jnemeth return (0);
311 1.1 jnemeth }
312