backup.c revision 1.10 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.10 christos __RCSID("$NetBSD: backup.c,v 1.10 2015/12/01 09:05:33 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.10 christos #include "gpt_private.h"
53 1.1 jnemeth
54 1.1 jnemeth
55 1.10 christos const char backupmsg[] = "backup";
56 1.1 jnemeth
57 1.10 christos static int
58 1.1 jnemeth usage_backup(void)
59 1.1 jnemeth {
60 1.1 jnemeth
61 1.1 jnemeth fprintf(stderr,
62 1.1 jnemeth "usage: %s %s\n", getprogname(), backupmsg);
63 1.10 christos return -1;
64 1.1 jnemeth }
65 1.1 jnemeth
66 1.10 christos #define PROP_ERR(x) if (!(x)) { \
67 1.10 christos gpt_warnx(gpt, "proplib failure"); \
68 1.10 christos return -1; \
69 1.1 jnemeth }
70 1.1 jnemeth
71 1.10 christos static int
72 1.10 christos backup(gpt_t gpt)
73 1.1 jnemeth {
74 1.10 christos map_t m;
75 1.1 jnemeth struct mbr *mbr;
76 1.10 christos struct gpt_hdr *hdr;
77 1.1 jnemeth struct gpt_ent *ent;
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.8 christos char *propext, *s, buf[128];
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.10 christos propnum = prop_number_create_integer(gpt->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.10 christos m = map_first(gpt);
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.8 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d",
205 1.8 christos hdr->hdr_guid);
206 1.8 christos propstr = prop_string_create_cstring(buf);
207 1.1 jnemeth PROP_ERR(propstr);
208 1.1 jnemeth rc = prop_dictionary_set(type_dict, "guid", propstr);
209 1.1 jnemeth PROP_ERR(rc);
210 1.1 jnemeth propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
211 1.1 jnemeth PROP_ERR(propnum);
212 1.1 jnemeth rc = prop_dictionary_set(type_dict, "entries", propnum);
213 1.1 jnemeth PROP_ERR(rc);
214 1.1 jnemeth rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
215 1.1 jnemeth PROP_ERR(rc);
216 1.1 jnemeth prop_object_release(type_dict);
217 1.1 jnemeth break;
218 1.1 jnemeth case MAP_TYPE_PRI_GPT_TBL:
219 1.1 jnemeth type_dict = prop_dictionary_create();
220 1.1 jnemeth PROP_ERR(type_dict);
221 1.1 jnemeth ent = m->map_data;
222 1.3 jnemeth gpt_array = prop_array_create();
223 1.3 jnemeth PROP_ERR(gpt_array);
224 1.1 jnemeth for (i = 1, ent = m->map_data;
225 1.1 jnemeth (char *)ent < (char *)(m->map_data) +
226 1.10 christos m->map_size * gpt->secsz; i++, ent++) {
227 1.1 jnemeth gpt_dict = prop_dictionary_create();
228 1.1 jnemeth PROP_ERR(gpt_dict);
229 1.1 jnemeth propnum = prop_number_create_integer(i);
230 1.1 jnemeth PROP_ERR(propnum);
231 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "index",
232 1.1 jnemeth propnum);
233 1.1 jnemeth PROP_ERR(propnum);
234 1.8 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d",
235 1.8 christos ent->ent_type);
236 1.8 christos propstr = prop_string_create_cstring(buf);
237 1.1 jnemeth PROP_ERR(propstr);
238 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "type",
239 1.1 jnemeth propstr);
240 1.8 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d",
241 1.8 christos ent->ent_guid);
242 1.8 christos propstr = prop_string_create_cstring(buf);
243 1.1 jnemeth PROP_ERR(propstr);
244 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "guid",
245 1.1 jnemeth propstr);
246 1.1 jnemeth PROP_ERR(propstr);
247 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_start));
248 1.1 jnemeth PROP_ERR(propnum);
249 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "start",
250 1.1 jnemeth propnum);
251 1.1 jnemeth PROP_ERR(rc);
252 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_end));
253 1.1 jnemeth PROP_ERR(rc);
254 1.1 jnemeth rc = prop_dictionary_set(gpt_dict, "end",
255 1.1 jnemeth propnum);
256 1.1 jnemeth PROP_ERR(rc);
257 1.1 jnemeth propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_attr));
258 1.1 jnemeth PROP_ERR(propnum);
259 1.1 jnemeth rc = prop_dictionary_set(gpt_dict,
260 1.1 jnemeth "attributes", propnum);
261 1.1 jnemeth PROP_ERR(rc);
262 1.1 jnemeth s = (char *)utf16_to_utf8(ent->ent_name);
263 1.1 jnemeth if (*s != '\0') {
264 1.1 jnemeth propstr = prop_string_create_cstring(s);
265 1.1 jnemeth PROP_ERR(propstr);
266 1.1 jnemeth rc = prop_dictionary_set(gpt_dict,
267 1.1 jnemeth "name", propstr);
268 1.1 jnemeth PROP_ERR(rc);
269 1.1 jnemeth }
270 1.1 jnemeth rc = prop_array_add(gpt_array, gpt_dict);
271 1.1 jnemeth PROP_ERR(rc);
272 1.1 jnemeth }
273 1.3 jnemeth rc = prop_dictionary_set(type_dict,
274 1.3 jnemeth "gpt_array", gpt_array);
275 1.3 jnemeth PROP_ERR(rc);
276 1.3 jnemeth prop_object_release(gpt_array);
277 1.1 jnemeth rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
278 1.1 jnemeth PROP_ERR(rc);
279 1.1 jnemeth prop_object_release(type_dict);
280 1.1 jnemeth break;
281 1.1 jnemeth }
282 1.1 jnemeth m = m->map_next;
283 1.1 jnemeth }
284 1.1 jnemeth propext = prop_dictionary_externalize(props);
285 1.1 jnemeth PROP_ERR(propext);
286 1.1 jnemeth prop_object_release(props);
287 1.1 jnemeth fputs(propext, stdout);
288 1.3 jnemeth free(propext);
289 1.10 christos return 0;
290 1.1 jnemeth }
291 1.1 jnemeth
292 1.1 jnemeth int
293 1.10 christos cmd_backup(gpt_t gpt, int argc, char *argv[])
294 1.1 jnemeth {
295 1.10 christos if (argc != optind)
296 1.1 jnemeth usage_backup();
297 1.1 jnemeth
298 1.10 christos return backup(gpt);
299 1.1 jnemeth }
300