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