xref: /netbsd-src/sbin/gpt/destroy.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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.10 2015/12/03 01:07:28 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 cmd_destroy(gpt_t, int, char *[]);
53 
54 static const char *destroyhelp[] = {
55 	"[-rf]",
56 };
57 
58 struct gpt_cmd c_destroy = {
59 	"destroy",
60 	cmd_destroy,
61 	destroyhelp, __arraycount(destroyhelp),
62 	0,
63 };
64 
65 #define usage() gpt_usage(NULL, &c_destroy)
66 
67 
68 static int
69 destroy(gpt_t gpt, int force, int recoverable)
70 {
71 	map_t pri_hdr, sec_hdr;
72 
73 	pri_hdr = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
74 	sec_hdr = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
75 
76 	if (pri_hdr == NULL && sec_hdr == NULL) {
77 		gpt_warnx(gpt, "Device doesn't contain a GPT");
78 		return -1;
79 	}
80 
81 	if (recoverable && sec_hdr == NULL) {
82 		gpt_warnx(gpt, "Recoverability not possible");
83 		return -1;
84 	}
85 
86 	if (pri_hdr != NULL) {
87 		memset(pri_hdr->map_data, 0, gpt->secsz);
88 		if (gpt_write(gpt, pri_hdr) == -1) {
89 			gpt_warnx(gpt, "Error writing primary header");
90 			return -1;
91 		}
92 	}
93 
94 	if (!recoverable && sec_hdr != NULL) {
95 		memset(sec_hdr->map_data, 0, gpt->secsz);
96 		if (gpt_write(gpt, sec_hdr) == -1) {
97 			gpt_warnx(gpt, "Error writing backup header");
98 			return -1;
99 		}
100 	}
101 
102 	return 0;
103 }
104 
105 static int
106 cmd_destroy(gpt_t gpt, int argc, char *argv[])
107 {
108 	int ch;
109 	int recoverable = 0;
110 	int force = 0;
111 
112 	while ((ch = getopt(argc, argv, "fr")) != -1) {
113 		switch(ch) {
114 		case 'f':
115 			force = 1;
116 			break;
117 		case 'r':
118 			recoverable = 1;
119 			break;
120 		default:
121 			return usage();
122 		}
123 	}
124 
125 	if (argc != optind)
126 		return usage();
127 
128 	return destroy(gpt, force, recoverable);
129 }
130