Home | History | Annotate | Line # | Download | only in gpt
destroy.c revision 1.8
      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/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: destroy.c,v 1.8 2015/12/01 09:05:33 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 
     41 #include <err.h>
     42 #include <stddef.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "map.h"
     49 #include "gpt.h"
     50 #include "gpt_private.h"
     51 
     52 static int recoverable;
     53 static int force;
     54 
     55 const char destroymsg[] = "destroy [-rf]";
     56 
     57 static int
     58 usage_destroy(void)
     59 {
     60 
     61 	fprintf(stderr,
     62 	    "usage: %s %s\n", getprogname(), destroymsg);
     63 	return -1;
     64 }
     65 
     66 static int
     67 destroy(gpt_t gpt)
     68 {
     69 	map_t pri_hdr, sec_hdr;
     70 
     71 	pri_hdr = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
     72 	sec_hdr = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
     73 
     74 	if (pri_hdr == NULL && sec_hdr == NULL) {
     75 		gpt_warnx(gpt, "Device doesn't contain a GPT");
     76 		return -1;
     77 	}
     78 
     79 	if (recoverable && sec_hdr == NULL) {
     80 		gpt_warnx(gpt, "Recoverability not possible");
     81 		return -1;
     82 	}
     83 
     84 	if (pri_hdr != NULL) {
     85 		memset(pri_hdr->map_data, 0, gpt->secsz);
     86 		if (gpt_write(gpt, pri_hdr) == -1) {
     87 			gpt_warnx(gpt, "Error writing primary header");
     88 			return -1;
     89 		}
     90 	}
     91 
     92 	if (!recoverable && sec_hdr != NULL) {
     93 		memset(sec_hdr->map_data, 0, gpt->secsz);
     94 		if (gpt_write(gpt, sec_hdr) == -1) {
     95 			gpt_warnx(gpt, "Error writing backup header");
     96 			return -1;
     97 		}
     98 	}
     99 
    100 	return 0;
    101 }
    102 
    103 int
    104 cmd_destroy(gpt_t gpt, int argc, char *argv[])
    105 {
    106 	int ch;
    107 
    108 	while ((ch = getopt(argc, argv, "fr")) != -1) {
    109 		switch(ch) {
    110 		case 'f':
    111 			force = 1;
    112 			break;
    113 		case 'r':
    114 			recoverable = 1;
    115 			break;
    116 		default:
    117 			return usage_destroy();
    118 		}
    119 	}
    120 
    121 	if (argc != optind)
    122 		return usage_destroy();
    123 
    124 	return destroy(gpt);
    125 }
    126