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