remove.c revision 1.16 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2004 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.14 christos #if HAVE_NBTOOL_CONFIG_H
28 1.14 christos #include "nbtool_config.h"
29 1.14 christos #endif
30 1.14 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.2 christos #ifdef __FBSDID
33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
34 1.2 christos #endif
35 1.2 christos #ifdef __RCSID
36 1.16 christos __RCSID("$NetBSD: remove.c,v 1.16 2014/09/30 17:59:59 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.1 christos
51 1.1 christos static int all;
52 1.16 christos static gpt_uuid_t type;
53 1.1 christos static off_t block, size;
54 1.1 christos static unsigned int entry;
55 1.13 jnemeth static uint8_t *label;
56 1.1 christos
57 1.4 riz const char removemsg1[] = "remove -a device ...";
58 1.13 jnemeth const char removemsg2[] = "remove [-b blocknr] [-i index] [-L label] "
59 1.13 jnemeth "[-s sectors] [-t type] device ...";
60 1.4 riz
61 1.6 joerg __dead static void
62 1.1 christos usage_remove(void)
63 1.1 christos {
64 1.1 christos
65 1.1 christos fprintf(stderr,
66 1.4 riz "usage: %s %s\n"
67 1.4 riz " %s %s\n",
68 1.4 riz getprogname(), removemsg1, getprogname(), removemsg2);
69 1.1 christos exit(1);
70 1.1 christos }
71 1.1 christos
72 1.1 christos static void
73 1.1 christos rem(int fd)
74 1.1 christos {
75 1.1 christos map_t *gpt, *tpg;
76 1.1 christos map_t *tbl, *lbt;
77 1.1 christos map_t *m;
78 1.1 christos struct gpt_hdr *hdr;
79 1.1 christos struct gpt_ent *ent;
80 1.1 christos unsigned int i;
81 1.1 christos
82 1.1 christos gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
83 1.1 christos if (gpt == NULL) {
84 1.1 christos warnx("%s: error: no primary GPT header; run create or recover",
85 1.1 christos device_name);
86 1.1 christos return;
87 1.1 christos }
88 1.1 christos
89 1.1 christos tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
90 1.1 christos if (tpg == NULL) {
91 1.1 christos warnx("%s: error: no secondary GPT header; run recover",
92 1.1 christos device_name);
93 1.1 christos return;
94 1.1 christos }
95 1.1 christos
96 1.1 christos tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
97 1.1 christos lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
98 1.1 christos if (tbl == NULL || lbt == NULL) {
99 1.1 christos warnx("%s: error: run recover -- trust me", device_name);
100 1.1 christos return;
101 1.1 christos }
102 1.1 christos
103 1.1 christos /* Remove all matching entries in the map. */
104 1.1 christos for (m = map_first(); m != NULL; m = m->map_next) {
105 1.1 christos if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
106 1.1 christos continue;
107 1.1 christos if (entry > 0 && entry != m->map_index)
108 1.1 christos continue;
109 1.1 christos if (block > 0 && block != m->map_start)
110 1.1 christos continue;
111 1.1 christos if (size > 0 && size != m->map_size)
112 1.1 christos continue;
113 1.1 christos
114 1.1 christos i = m->map_index - 1;
115 1.1 christos
116 1.1 christos hdr = gpt->map_data;
117 1.1 christos ent = (void*)((char*)tbl->map_data + i *
118 1.1 christos le32toh(hdr->hdr_entsz));
119 1.13 jnemeth
120 1.13 jnemeth if (label != NULL)
121 1.13 jnemeth if (strcmp((char *)label,
122 1.13 jnemeth (char *)utf16_to_utf8(ent->ent_name)) != 0)
123 1.13 jnemeth continue;
124 1.13 jnemeth
125 1.16 christos if (!gpt_uuid_is_nil(type) &&
126 1.16 christos !gpt_uuid_equal(type, ent->ent_type))
127 1.1 christos continue;
128 1.1 christos
129 1.1 christos /* Remove the primary entry by clearing the partition type. */
130 1.16 christos gpt_uuid_copy(ent->ent_type, gpt_uuid_nil);
131 1.1 christos
132 1.1 christos hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
133 1.1 christos le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
134 1.1 christos hdr->hdr_crc_self = 0;
135 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
136 1.1 christos
137 1.1 christos gpt_write(fd, gpt);
138 1.1 christos gpt_write(fd, tbl);
139 1.1 christos
140 1.1 christos hdr = tpg->map_data;
141 1.1 christos ent = (void*)((char*)lbt->map_data + i *
142 1.1 christos le32toh(hdr->hdr_entsz));
143 1.1 christos
144 1.7 jakllsch /* Remove the secondary entry. */
145 1.16 christos gpt_uuid_copy(ent->ent_type, gpt_uuid_nil);
146 1.1 christos
147 1.1 christos hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
148 1.1 christos le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
149 1.1 christos hdr->hdr_crc_self = 0;
150 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
151 1.1 christos
152 1.1 christos gpt_write(fd, lbt);
153 1.1 christos gpt_write(fd, tpg);
154 1.2 christos printf("partition %d removed from %s\n", m->map_index,
155 1.2 christos device_name);
156 1.1 christos }
157 1.1 christos }
158 1.1 christos
159 1.1 christos int
160 1.1 christos cmd_remove(int argc, char *argv[])
161 1.1 christos {
162 1.1 christos char *p;
163 1.1 christos int ch, fd;
164 1.10 jnemeth int64_t human_num;
165 1.1 christos
166 1.1 christos /* Get the remove options */
167 1.13 jnemeth while ((ch = getopt(argc, argv, "ab:i:L:s:t:")) != -1) {
168 1.1 christos switch(ch) {
169 1.1 christos case 'a':
170 1.1 christos if (all > 0)
171 1.1 christos usage_remove();
172 1.1 christos all = 1;
173 1.1 christos break;
174 1.1 christos case 'b':
175 1.1 christos if (block > 0)
176 1.1 christos usage_remove();
177 1.10 jnemeth if (dehumanize_number(optarg, &human_num) < 0)
178 1.1 christos usage_remove();
179 1.10 jnemeth block = human_num;
180 1.12 jnemeth if (block < 1)
181 1.12 jnemeth usage_remove();
182 1.1 christos break;
183 1.1 christos case 'i':
184 1.1 christos if (entry > 0)
185 1.1 christos usage_remove();
186 1.3 riz entry = strtoul(optarg, &p, 10);
187 1.1 christos if (*p != 0 || entry < 1)
188 1.1 christos usage_remove();
189 1.1 christos break;
190 1.13 jnemeth case 'L':
191 1.13 jnemeth if (label != NULL)
192 1.13 jnemeth usage_remove();
193 1.13 jnemeth label = (uint8_t *)strdup(optarg);
194 1.13 jnemeth break;
195 1.1 christos case 's':
196 1.1 christos if (size > 0)
197 1.1 christos usage_remove();
198 1.1 christos size = strtoll(optarg, &p, 10);
199 1.1 christos if (*p != 0 || size < 1)
200 1.1 christos usage_remove();
201 1.1 christos break;
202 1.1 christos case 't':
203 1.16 christos if (!gpt_uuid_is_nil(type))
204 1.1 christos usage_remove();
205 1.16 christos if (gpt_uuid_parse(optarg, type) != 0)
206 1.1 christos usage_remove();
207 1.1 christos break;
208 1.1 christos default:
209 1.1 christos usage_remove();
210 1.1 christos }
211 1.1 christos }
212 1.1 christos
213 1.1 christos if (!all ^
214 1.13 jnemeth (block > 0 || entry > 0 || label != NULL || size > 0 ||
215 1.16 christos !gpt_uuid_is_nil(type)))
216 1.1 christos usage_remove();
217 1.1 christos
218 1.1 christos if (argc == optind)
219 1.1 christos usage_remove();
220 1.1 christos
221 1.1 christos while (optind < argc) {
222 1.1 christos fd = gpt_open(argv[optind++]);
223 1.1 christos if (fd == -1) {
224 1.1 christos warn("unable to open device '%s'", device_name);
225 1.1 christos continue;
226 1.1 christos }
227 1.1 christos
228 1.1 christos rem(fd);
229 1.1 christos
230 1.1 christos gpt_close(fd);
231 1.1 christos }
232 1.1 christos
233 1.1 christos return (0);
234 1.1 christos }
235