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