Home | History | Annotate | Line # | Download | only in gpt
remove.c revision 1.2
      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.1  christos #include <sys/cdefs.h>
     28  1.2  christos #ifdef __FBSDID
     29  1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
     30  1.2  christos #endif
     31  1.2  christos #ifdef __RCSID
     32  1.2  christos __RCSID("$NetBSD: remove.c,v 1.2 2006/10/15 22:36:29 christos Exp $");
     33  1.2  christos #endif
     34  1.1  christos 
     35  1.1  christos #include <sys/types.h>
     36  1.1  christos 
     37  1.1  christos #include <err.h>
     38  1.1  christos #include <stddef.h>
     39  1.1  christos #include <stdio.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos #include <unistd.h>
     43  1.1  christos 
     44  1.1  christos #include "map.h"
     45  1.1  christos #include "gpt.h"
     46  1.1  christos 
     47  1.1  christos static int all;
     48  1.1  christos static uuid_t type;
     49  1.1  christos static off_t block, size;
     50  1.1  christos static unsigned int entry;
     51  1.1  christos 
     52  1.1  christos static void
     53  1.1  christos usage_remove(void)
     54  1.1  christos {
     55  1.1  christos 
     56  1.1  christos 	fprintf(stderr,
     57  1.1  christos 	    "usage: %s -a device ...\n"
     58  1.1  christos 	    "       %s [-b lba] [-i index] [-s lba] [-t uuid] device ...\n",
     59  1.1  christos 	    getprogname(), getprogname());
     60  1.1  christos 	exit(1);
     61  1.1  christos }
     62  1.1  christos 
     63  1.1  christos static void
     64  1.1  christos rem(int fd)
     65  1.1  christos {
     66  1.1  christos 	uuid_t uuid;
     67  1.1  christos 	map_t *gpt, *tpg;
     68  1.1  christos 	map_t *tbl, *lbt;
     69  1.1  christos 	map_t *m;
     70  1.1  christos 	struct gpt_hdr *hdr;
     71  1.1  christos 	struct gpt_ent *ent;
     72  1.1  christos 	unsigned int i;
     73  1.1  christos 
     74  1.1  christos 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     75  1.1  christos 	if (gpt == NULL) {
     76  1.1  christos 		warnx("%s: error: no primary GPT header; run create or recover",
     77  1.1  christos 		    device_name);
     78  1.1  christos 		return;
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     82  1.1  christos 	if (tpg == NULL) {
     83  1.1  christos 		warnx("%s: error: no secondary GPT header; run recover",
     84  1.1  christos 		    device_name);
     85  1.1  christos 		return;
     86  1.1  christos 	}
     87  1.1  christos 
     88  1.1  christos 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     89  1.1  christos 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     90  1.1  christos 	if (tbl == NULL || lbt == NULL) {
     91  1.1  christos 		warnx("%s: error: run recover -- trust me", device_name);
     92  1.1  christos 		return;
     93  1.1  christos 	}
     94  1.1  christos 
     95  1.1  christos 	/* Remove all matching entries in the map. */
     96  1.1  christos 	for (m = map_first(); m != NULL; m = m->map_next) {
     97  1.1  christos 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
     98  1.1  christos 			continue;
     99  1.1  christos 		if (entry > 0 && entry != m->map_index)
    100  1.1  christos 			continue;
    101  1.1  christos 		if (block > 0 && block != m->map_start)
    102  1.1  christos 			continue;
    103  1.1  christos 		if (size > 0 && size != m->map_size)
    104  1.1  christos 			continue;
    105  1.1  christos 
    106  1.1  christos 		i = m->map_index - 1;
    107  1.1  christos 
    108  1.1  christos 		hdr = gpt->map_data;
    109  1.1  christos 		ent = (void*)((char*)tbl->map_data + i *
    110  1.1  christos 		    le32toh(hdr->hdr_entsz));
    111  1.1  christos 		le_uuid_dec(&ent->ent_type, &uuid);
    112  1.1  christos 		if (!uuid_is_nil(&type, NULL) &&
    113  1.1  christos 		    !uuid_equal(&type, &uuid, NULL))
    114  1.1  christos 			continue;
    115  1.1  christos 
    116  1.1  christos 		/* Remove the primary entry by clearing the partition type. */
    117  1.2  christos 		uuid_create_nil((uuid_t *)&ent->ent_type, NULL);
    118  1.1  christos 
    119  1.1  christos 		hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    120  1.1  christos 		    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    121  1.1  christos 		hdr->hdr_crc_self = 0;
    122  1.1  christos 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    123  1.1  christos 
    124  1.1  christos 		gpt_write(fd, gpt);
    125  1.1  christos 		gpt_write(fd, tbl);
    126  1.1  christos 
    127  1.1  christos 		hdr = tpg->map_data;
    128  1.1  christos 		ent = (void*)((char*)lbt->map_data + i *
    129  1.1  christos 		    le32toh(hdr->hdr_entsz));
    130  1.1  christos 
    131  1.1  christos 		/* Remove the secundary entry. */
    132  1.2  christos 		uuid_create_nil((uuid_t *)&ent->ent_type, NULL);
    133  1.1  christos 
    134  1.1  christos 		hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    135  1.1  christos 		    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    136  1.1  christos 		hdr->hdr_crc_self = 0;
    137  1.1  christos 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    138  1.1  christos 
    139  1.1  christos 		gpt_write(fd, lbt);
    140  1.1  christos 		gpt_write(fd, tpg);
    141  1.2  christos #ifdef __FreeBSD__
    142  1.1  christos 		printf("%sp%u removed\n", device_name, m->map_index);
    143  1.2  christos #endif
    144  1.2  christos #ifdef __NetBSD__
    145  1.2  christos 		printf("partition %d removed from %s\n", m->map_index,
    146  1.2  christos 		    device_name);
    147  1.2  christos #endif
    148  1.1  christos 	}
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos int
    152  1.1  christos cmd_remove(int argc, char *argv[])
    153  1.1  christos {
    154  1.1  christos 	char *p;
    155  1.1  christos 	int ch, fd;
    156  1.1  christos 
    157  1.1  christos 	/* Get the remove options */
    158  1.1  christos 	while ((ch = getopt(argc, argv, "ab:i:s:t:")) != -1) {
    159  1.1  christos 		switch(ch) {
    160  1.1  christos 		case 'a':
    161  1.1  christos 			if (all > 0)
    162  1.1  christos 				usage_remove();
    163  1.1  christos 			all = 1;
    164  1.1  christos 			break;
    165  1.1  christos 		case 'b':
    166  1.1  christos 			if (block > 0)
    167  1.1  christos 				usage_remove();
    168  1.1  christos 			block = strtoll(optarg, &p, 10);
    169  1.1  christos 			if (*p != 0 || block < 1)
    170  1.1  christos 				usage_remove();
    171  1.1  christos 			break;
    172  1.1  christos 		case 'i':
    173  1.1  christos 			if (entry > 0)
    174  1.1  christos 				usage_remove();
    175  1.1  christos 			entry = strtol(optarg, &p, 10);
    176  1.1  christos 			if (*p != 0 || entry < 1)
    177  1.1  christos 				usage_remove();
    178  1.1  christos 			break;
    179  1.1  christos 		case 's':
    180  1.1  christos 			if (size > 0)
    181  1.1  christos 				usage_remove();
    182  1.1  christos 			size = strtoll(optarg, &p, 10);
    183  1.1  christos 			if (*p != 0 || size < 1)
    184  1.1  christos 				usage_remove();
    185  1.1  christos 			break;
    186  1.1  christos 		case 't':
    187  1.1  christos 			if (!uuid_is_nil(&type, NULL))
    188  1.1  christos 				usage_remove();
    189  1.1  christos 			if (parse_uuid(optarg, &type) != 0)
    190  1.1  christos 				usage_remove();
    191  1.1  christos 			break;
    192  1.1  christos 		default:
    193  1.1  christos 			usage_remove();
    194  1.1  christos 		}
    195  1.1  christos 	}
    196  1.1  christos 
    197  1.1  christos 	if (!all ^
    198  1.1  christos 	    (block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL)))
    199  1.1  christos 		usage_remove();
    200  1.1  christos 
    201  1.1  christos 	if (argc == optind)
    202  1.1  christos 		usage_remove();
    203  1.1  christos 
    204  1.1  christos 	while (optind < argc) {
    205  1.1  christos 		fd = gpt_open(argv[optind++]);
    206  1.1  christos 		if (fd == -1) {
    207  1.1  christos 			warn("unable to open device '%s'", device_name);
    208  1.1  christos 			continue;
    209  1.1  christos 		}
    210  1.1  christos 
    211  1.1  christos 		rem(fd);
    212  1.1  christos 
    213  1.1  christos 		gpt_close(fd);
    214  1.1  christos 	}
    215  1.1  christos 
    216  1.1  christos 	return (0);
    217  1.1  christos }
    218