Home | History | Annotate | Line # | Download | only in gpt
backup.c revision 1.16
      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.16  christos __RCSID("$NetBSD: backup.c,v 1.16 2015/12/03 21:40:32 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.11  christos static const char *backuphelp[] = {
     55  1.14  christos 	"[-o outfile]",
     56  1.11  christos };
     57  1.11  christos 
     58  1.11  christos static int cmd_backup(gpt_t, int, char *[]);
     59  1.11  christos 
     60  1.11  christos struct gpt_cmd c_backup = {
     61  1.11  christos 	"backup",
     62  1.11  christos 	cmd_backup,
     63  1.11  christos 	backuphelp, __arraycount(backuphelp),
     64  1.11  christos 	GPT_READONLY,
     65  1.11  christos };
     66   1.1   jnemeth 
     67  1.11  christos #define usage() gpt_usage(NULL, &c_backup)
     68   1.1   jnemeth 
     69  1.16  christos #define PROP_ERR(x)	if (!(x)) goto cleanup
     70   1.1   jnemeth 
     71  1.12  christos #define prop_uint(a) prop_number_create_unsigned_integer(a)
     72  1.12  christos 
     73  1.12  christos static int
     74  1.12  christos store_mbr(gpt_t gpt, unsigned int i, const struct mbr *mbr,
     75  1.12  christos     prop_array_t *mbr_array)
     76  1.12  christos {
     77  1.12  christos 	prop_dictionary_t mbr_dict;
     78  1.12  christos 	prop_number_t propnum;
     79  1.12  christos 	const struct mbr_part *par = &mbr->mbr_part[i];
     80  1.12  christos 	bool rc;
     81  1.12  christos 
     82  1.12  christos 	if (mbr->mbr_part[i].part_typ == MBR_PTYPE_UNUSED)
     83  1.12  christos 		return 0;
     84  1.12  christos 
     85  1.12  christos 	mbr_dict = prop_dictionary_create();
     86  1.12  christos 	PROP_ERR(mbr_dict);
     87  1.12  christos 	propnum = prop_number_create_integer(i);
     88  1.12  christos 	PROP_ERR(propnum);
     89  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "index", propnum);
     90  1.12  christos 	PROP_ERR(rc);
     91  1.12  christos 	propnum = prop_uint(par->part_flag);
     92  1.12  christos 	PROP_ERR(propnum);
     93  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "flag", propnum);
     94  1.12  christos 	PROP_ERR(rc);
     95  1.12  christos 	propnum = prop_uint(par->part_shd);
     96  1.12  christos 	PROP_ERR(propnum);
     97  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "start_head", propnum);
     98  1.12  christos 	PROP_ERR(rc);
     99  1.12  christos 	propnum = prop_uint(par->part_ssect);
    100  1.12  christos 	PROP_ERR(propnum);
    101  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "start_sector", propnum);
    102  1.12  christos 	PROP_ERR(rc);
    103  1.12  christos 	propnum = prop_uint(par->part_scyl);
    104  1.12  christos 	PROP_ERR(propnum);
    105  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "start_cylinder", propnum);
    106  1.12  christos 	PROP_ERR(rc);
    107  1.12  christos 	propnum = prop_uint(par->part_typ);
    108  1.12  christos 	PROP_ERR(propnum);
    109  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "type", propnum);
    110  1.12  christos 	PROP_ERR(rc);
    111  1.12  christos 	propnum = prop_uint(par->part_ehd);
    112  1.12  christos 	PROP_ERR(propnum);
    113  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "end_head", propnum);
    114  1.12  christos 	PROP_ERR(rc);
    115  1.12  christos 	propnum = prop_uint(par->part_esect);
    116  1.12  christos 	PROP_ERR(propnum);
    117  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "end_sector", propnum);
    118  1.12  christos 	PROP_ERR(rc);
    119  1.12  christos 	propnum = prop_uint(par->part_ecyl);
    120  1.12  christos 	PROP_ERR(propnum);
    121  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "end_cylinder", propnum);
    122  1.12  christos 	PROP_ERR(rc);
    123  1.12  christos 	propnum = prop_uint(le16toh(par->part_start_lo));
    124  1.12  christos 	PROP_ERR(propnum);
    125  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "lba_start_low", propnum);
    126  1.12  christos 	PROP_ERR(rc);
    127  1.12  christos 	propnum = prop_uint(le16toh(par->part_start_hi));
    128  1.12  christos 	PROP_ERR(propnum);
    129  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "lba_start_high", propnum);
    130  1.12  christos 	PROP_ERR(rc);
    131  1.12  christos 	propnum = prop_uint(le16toh(par->part_size_lo));
    132  1.12  christos 	PROP_ERR(propnum);
    133  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "lba_size_low", propnum);
    134  1.12  christos 	PROP_ERR(rc);
    135  1.12  christos 	propnum = prop_uint(le16toh(par->part_size_hi));
    136  1.12  christos 	PROP_ERR(propnum);
    137  1.12  christos 	rc = prop_dictionary_set(mbr_dict, "lba_size_high", propnum);
    138  1.12  christos 	if (*mbr_array == NULL) {
    139  1.12  christos 		*mbr_array = prop_array_create();
    140  1.12  christos 		PROP_ERR(*mbr_array);
    141  1.12  christos 	}
    142  1.12  christos 	rc = prop_array_add(*mbr_array, mbr_dict);
    143  1.12  christos 	PROP_ERR(rc);
    144  1.12  christos 	return 0;
    145  1.16  christos cleanup:
    146  1.16  christos 	if (mbr_dict)
    147  1.16  christos 		prop_object_release(mbr_dict);
    148  1.16  christos 	gpt_warnx(gpt, "proplib failure");
    149  1.16  christos 	return -1;
    150  1.12  christos }
    151  1.12  christos 
    152  1.12  christos static int
    153  1.12  christos store_gpt(gpt_t gpt, const struct gpt_hdr *hdr, prop_dictionary_t *type_dict)
    154  1.12  christos {
    155  1.12  christos 	prop_number_t propnum;
    156  1.12  christos 	prop_string_t propstr;
    157  1.12  christos 	char buf[128];
    158  1.12  christos 	bool rc;
    159  1.12  christos 
    160  1.12  christos 	*type_dict = prop_dictionary_create();
    161  1.12  christos 	PROP_ERR(type_dict);
    162  1.12  christos 	propnum = prop_uint(le32toh(hdr->hdr_revision));
    163  1.12  christos 	PROP_ERR(propnum);
    164  1.12  christos 	rc = prop_dictionary_set(*type_dict, "revision", propnum);
    165  1.12  christos 	PROP_ERR(rc);
    166  1.12  christos 	gpt_uuid_snprintf(buf, sizeof(buf), "%d", hdr->hdr_guid);
    167  1.12  christos 	propstr = prop_string_create_cstring(buf);
    168  1.12  christos 	PROP_ERR(propstr);
    169  1.12  christos 	rc = prop_dictionary_set(*type_dict, "guid", propstr);
    170  1.12  christos 	PROP_ERR(rc);
    171  1.12  christos 	propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
    172  1.12  christos 	PROP_ERR(propnum);
    173  1.12  christos 	rc = prop_dictionary_set(*type_dict, "entries", propnum);
    174  1.12  christos 	PROP_ERR(rc);
    175  1.12  christos 	return 0;
    176  1.16  christos cleanup:
    177  1.16  christos 	if (*type_dict)
    178  1.16  christos 		prop_object_release(*type_dict);
    179  1.16  christos 	return -1;
    180  1.12  christos }
    181  1.12  christos 
    182  1.12  christos static int
    183  1.12  christos store_tbl(gpt_t gpt, const map_t m, prop_dictionary_t *type_dict)
    184  1.12  christos {
    185  1.12  christos 	const struct gpt_ent *ent;
    186  1.12  christos 	unsigned int i;
    187  1.12  christos 	prop_dictionary_t gpt_dict;
    188  1.12  christos 	prop_array_t gpt_array;
    189  1.12  christos 	prop_number_t propnum;
    190  1.12  christos 	prop_string_t propstr;
    191  1.12  christos 	char buf[128];
    192  1.12  christos 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    193  1.12  christos 	bool rc;
    194  1.12  christos 
    195  1.16  christos 	*type_dict = NULL;
    196  1.16  christos 
    197  1.16  christos 	gpt_array = prop_array_create();
    198  1.16  christos 	PROP_ERR(gpt_array);
    199  1.16  christos 
    200  1.12  christos 	*type_dict = prop_dictionary_create();
    201  1.12  christos 	PROP_ERR(*type_dict);
    202  1.12  christos 
    203  1.12  christos 	ent = m->map_data;
    204  1.12  christos 	for (i = 1, ent = m->map_data;
    205  1.12  christos 	    (const char *)ent < (const char *)(m->map_data) +
    206  1.12  christos 	    m->map_size * gpt->secsz; i++, ent++) {
    207  1.12  christos 		gpt_dict = prop_dictionary_create();
    208  1.12  christos 		PROP_ERR(gpt_dict);
    209  1.12  christos 		propnum = prop_number_create_integer(i);
    210  1.12  christos 		PROP_ERR(propnum);
    211  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "index", propnum);
    212  1.12  christos 		PROP_ERR(propnum);
    213  1.12  christos 		gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_type);
    214  1.12  christos 		propstr = prop_string_create_cstring(buf);
    215  1.12  christos 		PROP_ERR(propstr);
    216  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "type", propstr);
    217  1.12  christos 		gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_guid);
    218  1.12  christos 		propstr = prop_string_create_cstring(buf);
    219  1.12  christos 		PROP_ERR(propstr);
    220  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "guid", propstr);
    221  1.12  christos 		PROP_ERR(propstr);
    222  1.12  christos 		propnum = prop_uint(le64toh(ent->ent_lba_start));
    223  1.12  christos 		PROP_ERR(propnum);
    224  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "start", propnum);
    225  1.12  christos 		PROP_ERR(rc);
    226  1.12  christos 		propnum = prop_uint(le64toh(ent->ent_lba_end));
    227  1.12  christos 		PROP_ERR(rc);
    228  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "end", propnum);
    229  1.12  christos 		PROP_ERR(rc);
    230  1.12  christos 		propnum = prop_uint(le64toh(ent->ent_attr));
    231  1.12  christos 		PROP_ERR(propnum);
    232  1.12  christos 		rc = prop_dictionary_set(gpt_dict, "attributes", propnum);
    233  1.12  christos 		PROP_ERR(rc);
    234  1.12  christos 		utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
    235  1.12  christos 		if (utfbuf[0] != '\0') {
    236  1.12  christos 			propstr = prop_string_create_cstring((char *)utfbuf);
    237  1.12  christos 			PROP_ERR(propstr);
    238  1.12  christos 			rc = prop_dictionary_set(gpt_dict, "name", propstr);
    239  1.12  christos 			PROP_ERR(rc);
    240  1.12  christos 		}
    241  1.12  christos 		rc = prop_array_add(gpt_array, gpt_dict);
    242  1.12  christos 		PROP_ERR(rc);
    243  1.12  christos 	}
    244  1.12  christos 	rc = prop_dictionary_set(*type_dict, "gpt_array", gpt_array);
    245  1.12  christos 	PROP_ERR(rc);
    246  1.12  christos 	prop_object_release(gpt_array);
    247  1.12  christos 	return 0;
    248  1.16  christos cleanup:
    249  1.16  christos 	if (*type_dict)
    250  1.16  christos 		prop_object_release(*type_dict);
    251  1.16  christos 	if (gpt_array)
    252  1.16  christos 		prop_object_release(gpt_array);
    253  1.16  christos 	return -1;
    254  1.12  christos }
    255  1.12  christos 
    256  1.10  christos static int
    257  1.14  christos backup(gpt_t gpt, const char *outfile)
    258   1.1   jnemeth {
    259  1.10  christos 	map_t m;
    260   1.1   jnemeth 	struct mbr *mbr;
    261   1.1   jnemeth 	unsigned int i;
    262  1.12  christos 	prop_dictionary_t props, type_dict;
    263  1.12  christos 	prop_array_t mbr_array;
    264   1.1   jnemeth 	prop_data_t propdata;
    265   1.1   jnemeth 	prop_number_t propnum;
    266  1.12  christos 	char *propext;
    267   1.1   jnemeth 	bool rc;
    268  1.13  christos 	FILE *fp;
    269   1.1   jnemeth 
    270   1.1   jnemeth 	props = prop_dictionary_create();
    271   1.1   jnemeth 	PROP_ERR(props);
    272  1.10  christos 	propnum = prop_number_create_integer(gpt->secsz);
    273   1.2   jnemeth 	PROP_ERR(propnum);
    274   1.2   jnemeth 	rc = prop_dictionary_set(props, "sector_size", propnum);
    275   1.2   jnemeth 	PROP_ERR(rc);
    276  1.10  christos 	m = map_first(gpt);
    277   1.1   jnemeth 	while (m != NULL) {
    278   1.1   jnemeth 		switch (m->map_type) {
    279   1.1   jnemeth 		case MAP_TYPE_MBR:
    280   1.1   jnemeth 		case MAP_TYPE_PMBR:
    281   1.1   jnemeth 			type_dict = prop_dictionary_create();
    282   1.1   jnemeth 			PROP_ERR(type_dict);
    283   1.1   jnemeth 			mbr = m->map_data;
    284   1.1   jnemeth 			propdata = prop_data_create_data_nocopy(mbr->mbr_code,
    285   1.1   jnemeth 			    sizeof(mbr->mbr_code));
    286   1.1   jnemeth 			PROP_ERR(propdata);
    287   1.1   jnemeth 			rc = prop_dictionary_set(type_dict, "code", propdata);
    288   1.1   jnemeth 			PROP_ERR(rc);
    289   1.1   jnemeth 			mbr_array = NULL;
    290   1.1   jnemeth 			for (i = 0; i < 4; i++) {
    291  1.12  christos 				if (store_mbr(gpt, i, mbr, &mbr_array) == -1)
    292  1.16  christos 					goto cleanup;
    293   1.1   jnemeth 			}
    294   1.1   jnemeth 			if (mbr_array != NULL) {
    295   1.1   jnemeth 				rc = prop_dictionary_set(type_dict,
    296   1.1   jnemeth 				    "mbr_array", mbr_array);
    297   1.1   jnemeth 				PROP_ERR(rc);
    298   1.1   jnemeth 				prop_object_release(mbr_array);
    299   1.1   jnemeth 			}
    300   1.1   jnemeth 			rc = prop_dictionary_set(props, "MBR", type_dict);
    301   1.1   jnemeth 			PROP_ERR(rc);
    302   1.1   jnemeth 			prop_object_release(type_dict);
    303   1.1   jnemeth 			break;
    304   1.1   jnemeth 		case MAP_TYPE_PRI_GPT_HDR:
    305  1.12  christos 			if (store_gpt(gpt, m->map_data, &type_dict) == -1)
    306  1.16  christos 				goto cleanup;
    307  1.12  christos 
    308   1.1   jnemeth 			rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
    309   1.1   jnemeth 			PROP_ERR(rc);
    310   1.1   jnemeth 			prop_object_release(type_dict);
    311   1.1   jnemeth 			break;
    312   1.1   jnemeth 		case MAP_TYPE_PRI_GPT_TBL:
    313  1.12  christos 			if (store_tbl(gpt, m, &type_dict) == -1)
    314  1.16  christos 				goto cleanup;
    315   1.1   jnemeth 			rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
    316   1.1   jnemeth 			PROP_ERR(rc);
    317   1.1   jnemeth 			prop_object_release(type_dict);
    318   1.1   jnemeth 			break;
    319   1.1   jnemeth 		}
    320   1.1   jnemeth 		m = m->map_next;
    321   1.1   jnemeth 	}
    322   1.1   jnemeth 	propext = prop_dictionary_externalize(props);
    323   1.1   jnemeth 	PROP_ERR(propext);
    324   1.1   jnemeth 	prop_object_release(props);
    325  1.14  christos 	fp = strcmp(outfile, "-") == 0 ? stdout : fopen(outfile, "w");
    326  1.14  christos 	if (fp == NULL) {
    327  1.13  christos 		gpt_warn(gpt, "Can't open `%s'", outfile);
    328  1.16  christos 		free(propext);
    329  1.16  christos 		goto cleanup;
    330  1.13  christos 	}
    331  1.13  christos 	fputs(propext, fp);
    332  1.15  christos 	if (fp != stdout)
    333  1.14  christos 		fclose(fp);
    334   1.3   jnemeth 	free(propext);
    335  1.10  christos 	return 0;
    336  1.16  christos cleanup:
    337  1.16  christos 	if (props)
    338  1.16  christos 		prop_object_release(props);
    339  1.16  christos 	return -1;
    340   1.1   jnemeth }
    341   1.1   jnemeth 
    342  1.11  christos static int
    343  1.10  christos cmd_backup(gpt_t gpt, int argc, char *argv[])
    344   1.1   jnemeth {
    345  1.13  christos 	int ch;
    346  1.14  christos 	const char *outfile = "-";
    347  1.13  christos 
    348  1.13  christos 	while ((ch = getopt(argc, argv, "o:")) != -1) {
    349  1.13  christos 		switch(ch) {
    350  1.13  christos 		case 'o':
    351  1.13  christos 			outfile = optarg;
    352  1.13  christos 			break;
    353  1.13  christos 		default:
    354  1.13  christos 			return usage();
    355  1.13  christos 		}
    356  1.13  christos 	}
    357  1.10  christos 	if (argc != optind)
    358  1.11  christos 		return usage();
    359   1.1   jnemeth 
    360  1.14  christos 	return backup(gpt, outfile);
    361   1.1   jnemeth }
    362