backup.c revision 1.12 1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
30
31 #include <sys/cdefs.h>
32 #ifdef __FBSDID
33 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: backup.c,v 1.12 2015/12/02 04:06:47 christos Exp $");
37 #endif
38
39 #include <sys/bootblock.h>
40 #include <sys/types.h>
41
42 #include <err.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <prop/proplib.h>
49
50 #include "map.h"
51 #include "gpt.h"
52 #include "gpt_private.h"
53
54
55 static const char *backuphelp[] = {
56 "",
57 };
58
59 static int cmd_backup(gpt_t, int, char *[]);
60
61 struct gpt_cmd c_backup = {
62 "backup",
63 cmd_backup,
64 backuphelp, __arraycount(backuphelp),
65 GPT_READONLY,
66 };
67
68 #define usage() gpt_usage(NULL, &c_backup)
69
70 #define PROP_ERR(x) if (!(x)) { \
71 gpt_warnx(gpt, "proplib failure"); \
72 return -1; \
73 }
74
75 #define prop_uint(a) prop_number_create_unsigned_integer(a)
76
77 static int
78 store_mbr(gpt_t gpt, unsigned int i, const struct mbr *mbr,
79 prop_array_t *mbr_array)
80 {
81 prop_dictionary_t mbr_dict;
82 prop_number_t propnum;
83 const struct mbr_part *par = &mbr->mbr_part[i];
84 bool rc;
85
86 if (mbr->mbr_part[i].part_typ == MBR_PTYPE_UNUSED)
87 return 0;
88
89 mbr_dict = prop_dictionary_create();
90 PROP_ERR(mbr_dict);
91 propnum = prop_number_create_integer(i);
92 PROP_ERR(propnum);
93 rc = prop_dictionary_set(mbr_dict, "index", propnum);
94 PROP_ERR(rc);
95 propnum = prop_uint(par->part_flag);
96 PROP_ERR(propnum);
97 rc = prop_dictionary_set(mbr_dict, "flag", propnum);
98 PROP_ERR(rc);
99 propnum = prop_uint(par->part_shd);
100 PROP_ERR(propnum);
101 rc = prop_dictionary_set(mbr_dict, "start_head", propnum);
102 PROP_ERR(rc);
103 propnum = prop_uint(par->part_ssect);
104 PROP_ERR(propnum);
105 rc = prop_dictionary_set(mbr_dict, "start_sector", propnum);
106 PROP_ERR(rc);
107 propnum = prop_uint(par->part_scyl);
108 PROP_ERR(propnum);
109 rc = prop_dictionary_set(mbr_dict, "start_cylinder", propnum);
110 PROP_ERR(rc);
111 propnum = prop_uint(par->part_typ);
112 PROP_ERR(propnum);
113 rc = prop_dictionary_set(mbr_dict, "type", propnum);
114 PROP_ERR(rc);
115 propnum = prop_uint(par->part_ehd);
116 PROP_ERR(propnum);
117 rc = prop_dictionary_set(mbr_dict, "end_head", propnum);
118 PROP_ERR(rc);
119 propnum = prop_uint(par->part_esect);
120 PROP_ERR(propnum);
121 rc = prop_dictionary_set(mbr_dict, "end_sector", propnum);
122 PROP_ERR(rc);
123 propnum = prop_uint(par->part_ecyl);
124 PROP_ERR(propnum);
125 rc = prop_dictionary_set(mbr_dict, "end_cylinder", propnum);
126 PROP_ERR(rc);
127 propnum = prop_uint(le16toh(par->part_start_lo));
128 PROP_ERR(propnum);
129 rc = prop_dictionary_set(mbr_dict, "lba_start_low", propnum);
130 PROP_ERR(rc);
131 propnum = prop_uint(le16toh(par->part_start_hi));
132 PROP_ERR(propnum);
133 rc = prop_dictionary_set(mbr_dict, "lba_start_high", propnum);
134 PROP_ERR(rc);
135 propnum = prop_uint(le16toh(par->part_size_lo));
136 PROP_ERR(propnum);
137 rc = prop_dictionary_set(mbr_dict, "lba_size_low", propnum);
138 PROP_ERR(rc);
139 propnum = prop_uint(le16toh(par->part_size_hi));
140 PROP_ERR(propnum);
141 rc = prop_dictionary_set(mbr_dict, "lba_size_high", propnum);
142 if (*mbr_array == NULL) {
143 *mbr_array = prop_array_create();
144 PROP_ERR(*mbr_array);
145 }
146 rc = prop_array_add(*mbr_array, mbr_dict);
147 PROP_ERR(rc);
148 return 0;
149 }
150
151 static int
152 store_gpt(gpt_t gpt, const struct gpt_hdr *hdr, prop_dictionary_t *type_dict)
153 {
154 prop_number_t propnum;
155 prop_string_t propstr;
156 char buf[128];
157 bool rc;
158
159 *type_dict = prop_dictionary_create();
160 PROP_ERR(type_dict);
161 propnum = prop_uint(le32toh(hdr->hdr_revision));
162 PROP_ERR(propnum);
163 rc = prop_dictionary_set(*type_dict, "revision", propnum);
164 PROP_ERR(rc);
165 gpt_uuid_snprintf(buf, sizeof(buf), "%d", hdr->hdr_guid);
166 propstr = prop_string_create_cstring(buf);
167 PROP_ERR(propstr);
168 rc = prop_dictionary_set(*type_dict, "guid", propstr);
169 PROP_ERR(rc);
170 propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
171 PROP_ERR(propnum);
172 rc = prop_dictionary_set(*type_dict, "entries", propnum);
173 PROP_ERR(rc);
174 return 0;
175 }
176
177 static int
178 store_tbl(gpt_t gpt, const map_t m, prop_dictionary_t *type_dict)
179 {
180 const struct gpt_ent *ent;
181 unsigned int i;
182 prop_dictionary_t gpt_dict;
183 prop_array_t gpt_array;
184 prop_number_t propnum;
185 prop_string_t propstr;
186 char buf[128];
187 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
188 bool rc;
189
190 *type_dict = prop_dictionary_create();
191 PROP_ERR(*type_dict);
192
193 ent = m->map_data;
194 gpt_array = prop_array_create();
195 PROP_ERR(gpt_array);
196 for (i = 1, ent = m->map_data;
197 (const char *)ent < (const char *)(m->map_data) +
198 m->map_size * gpt->secsz; i++, ent++) {
199 gpt_dict = prop_dictionary_create();
200 PROP_ERR(gpt_dict);
201 propnum = prop_number_create_integer(i);
202 PROP_ERR(propnum);
203 rc = prop_dictionary_set(gpt_dict, "index", propnum);
204 PROP_ERR(propnum);
205 gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_type);
206 propstr = prop_string_create_cstring(buf);
207 PROP_ERR(propstr);
208 rc = prop_dictionary_set(gpt_dict, "type", propstr);
209 gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_guid);
210 propstr = prop_string_create_cstring(buf);
211 PROP_ERR(propstr);
212 rc = prop_dictionary_set(gpt_dict, "guid", propstr);
213 PROP_ERR(propstr);
214 propnum = prop_uint(le64toh(ent->ent_lba_start));
215 PROP_ERR(propnum);
216 rc = prop_dictionary_set(gpt_dict, "start", propnum);
217 PROP_ERR(rc);
218 propnum = prop_uint(le64toh(ent->ent_lba_end));
219 PROP_ERR(rc);
220 rc = prop_dictionary_set(gpt_dict, "end", propnum);
221 PROP_ERR(rc);
222 propnum = prop_uint(le64toh(ent->ent_attr));
223 PROP_ERR(propnum);
224 rc = prop_dictionary_set(gpt_dict, "attributes", propnum);
225 PROP_ERR(rc);
226 utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
227 if (utfbuf[0] != '\0') {
228 propstr = prop_string_create_cstring((char *)utfbuf);
229 PROP_ERR(propstr);
230 rc = prop_dictionary_set(gpt_dict, "name", propstr);
231 PROP_ERR(rc);
232 }
233 rc = prop_array_add(gpt_array, gpt_dict);
234 PROP_ERR(rc);
235 }
236 rc = prop_dictionary_set(*type_dict, "gpt_array", gpt_array);
237 PROP_ERR(rc);
238 prop_object_release(gpt_array);
239 return 0;
240 }
241
242 static int
243 backup(gpt_t gpt)
244 {
245 map_t m;
246 struct mbr *mbr;
247 unsigned int i;
248 prop_dictionary_t props, type_dict;
249 prop_array_t mbr_array;
250 prop_data_t propdata;
251 prop_number_t propnum;
252 char *propext;
253 bool rc;
254
255 props = prop_dictionary_create();
256 PROP_ERR(props);
257 propnum = prop_number_create_integer(gpt->secsz);
258 PROP_ERR(propnum);
259 rc = prop_dictionary_set(props, "sector_size", propnum);
260 PROP_ERR(rc);
261 m = map_first(gpt);
262 while (m != NULL) {
263 switch (m->map_type) {
264 case MAP_TYPE_MBR:
265 case MAP_TYPE_PMBR:
266 type_dict = prop_dictionary_create();
267 PROP_ERR(type_dict);
268 mbr = m->map_data;
269 propdata = prop_data_create_data_nocopy(mbr->mbr_code,
270 sizeof(mbr->mbr_code));
271 PROP_ERR(propdata);
272 rc = prop_dictionary_set(type_dict, "code", propdata);
273 PROP_ERR(rc);
274 mbr_array = NULL;
275 for (i = 0; i < 4; i++) {
276 if (store_mbr(gpt, i, mbr, &mbr_array) == -1)
277 return -1;
278 }
279 if (mbr_array != NULL) {
280 rc = prop_dictionary_set(type_dict,
281 "mbr_array", mbr_array);
282 PROP_ERR(rc);
283 prop_object_release(mbr_array);
284 }
285 rc = prop_dictionary_set(props, "MBR", type_dict);
286 PROP_ERR(rc);
287 prop_object_release(type_dict);
288 break;
289 case MAP_TYPE_PRI_GPT_HDR:
290 if (store_gpt(gpt, m->map_data, &type_dict) == -1)
291 return -1;
292
293 rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
294 PROP_ERR(rc);
295 prop_object_release(type_dict);
296 break;
297 case MAP_TYPE_PRI_GPT_TBL:
298 if (store_tbl(gpt, m, &type_dict) == -1)
299 return -1;
300 rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
301 PROP_ERR(rc);
302 prop_object_release(type_dict);
303 break;
304 }
305 m = m->map_next;
306 }
307 propext = prop_dictionary_externalize(props);
308 PROP_ERR(propext);
309 prop_object_release(props);
310 fputs(propext, stdout);
311 free(propext);
312 return 0;
313 }
314
315 static int
316 cmd_backup(gpt_t gpt, int argc, char *argv[])
317 {
318 if (argc != optind)
319 return usage();
320
321 return backup(gpt);
322 }
323