Home | History | Annotate | Line # | Download | only in gpt
destroy.c revision 1.9
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  *
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  *
     15  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.5  christos #if HAVE_NBTOOL_CONFIG_H
     28  1.5  christos #include "nbtool_config.h"
     29  1.5  christos #endif
     30  1.5  christos 
     31  1.1  christos #include <sys/cdefs.h>
     32  1.2  christos #ifdef __FBSDID
     33  1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
     34  1.2  christos #endif
     35  1.2  christos #ifdef __RCSID
     36  1.9  christos __RCSID("$NetBSD: destroy.c,v 1.9 2015/12/01 16:32:19 christos Exp $");
     37  1.2  christos #endif
     38  1.1  christos 
     39  1.1  christos #include <sys/types.h>
     40  1.1  christos 
     41  1.1  christos #include <err.h>
     42  1.1  christos #include <stddef.h>
     43  1.1  christos #include <stdio.h>
     44  1.1  christos #include <stdlib.h>
     45  1.1  christos #include <string.h>
     46  1.1  christos #include <unistd.h>
     47  1.1  christos 
     48  1.1  christos #include "map.h"
     49  1.1  christos #include "gpt.h"
     50  1.8  christos #include "gpt_private.h"
     51  1.1  christos 
     52  1.1  christos static int recoverable;
     53  1.7  christos static int force;
     54  1.9  christos static int cmd_destroy(gpt_t, int, char *[]);
     55  1.1  christos 
     56  1.9  christos static const char *destroyhelp[] = {
     57  1.9  christos     "[-rf]",
     58  1.9  christos };
     59  1.9  christos 
     60  1.9  christos struct gpt_cmd c_destroy = {
     61  1.9  christos 	"destroy",
     62  1.9  christos 	cmd_destroy,
     63  1.9  christos 	destroyhelp, __arraycount(destroyhelp),
     64  1.9  christos 	0,
     65  1.9  christos };
     66  1.3       riz 
     67  1.9  christos #define usage() gpt_usage(NULL, &c_destroy)
     68  1.1  christos 
     69  1.1  christos 
     70  1.8  christos static int
     71  1.8  christos destroy(gpt_t gpt)
     72  1.1  christos {
     73  1.8  christos 	map_t pri_hdr, sec_hdr;
     74  1.1  christos 
     75  1.8  christos 	pri_hdr = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
     76  1.8  christos 	sec_hdr = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
     77  1.1  christos 
     78  1.1  christos 	if (pri_hdr == NULL && sec_hdr == NULL) {
     79  1.8  christos 		gpt_warnx(gpt, "Device doesn't contain a GPT");
     80  1.8  christos 		return -1;
     81  1.1  christos 	}
     82  1.1  christos 
     83  1.1  christos 	if (recoverable && sec_hdr == NULL) {
     84  1.8  christos 		gpt_warnx(gpt, "Recoverability not possible");
     85  1.8  christos 		return -1;
     86  1.1  christos 	}
     87  1.1  christos 
     88  1.1  christos 	if (pri_hdr != NULL) {
     89  1.8  christos 		memset(pri_hdr->map_data, 0, gpt->secsz);
     90  1.8  christos 		if (gpt_write(gpt, pri_hdr) == -1) {
     91  1.8  christos 			gpt_warnx(gpt, "Error writing primary header");
     92  1.8  christos 			return -1;
     93  1.8  christos 		}
     94  1.1  christos 	}
     95  1.1  christos 
     96  1.1  christos 	if (!recoverable && sec_hdr != NULL) {
     97  1.8  christos 		memset(sec_hdr->map_data, 0, gpt->secsz);
     98  1.8  christos 		if (gpt_write(gpt, sec_hdr) == -1) {
     99  1.8  christos 			gpt_warnx(gpt, "Error writing backup header");
    100  1.8  christos 			return -1;
    101  1.8  christos 		}
    102  1.1  christos 	}
    103  1.8  christos 
    104  1.8  christos 	return 0;
    105  1.1  christos }
    106  1.1  christos 
    107  1.9  christos static int
    108  1.8  christos cmd_destroy(gpt_t gpt, int argc, char *argv[])
    109  1.1  christos {
    110  1.8  christos 	int ch;
    111  1.1  christos 
    112  1.7  christos 	while ((ch = getopt(argc, argv, "fr")) != -1) {
    113  1.1  christos 		switch(ch) {
    114  1.7  christos 		case 'f':
    115  1.7  christos 			force = 1;
    116  1.7  christos 			break;
    117  1.1  christos 		case 'r':
    118  1.1  christos 			recoverable = 1;
    119  1.1  christos 			break;
    120  1.1  christos 		default:
    121  1.9  christos 			return usage();
    122  1.1  christos 		}
    123  1.1  christos 	}
    124  1.1  christos 
    125  1.8  christos 	if (argc != optind)
    126  1.9  christos 		return usage();
    127  1.1  christos 
    128  1.8  christos 	return destroy(gpt);
    129  1.1  christos }
    130