Home | History | Annotate | Line # | Download | only in gpt
type.c revision 1.2
      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 #include <sys/cdefs.h>
     28 #ifdef __FBSDID
     29 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
     30 #endif
     31 #ifdef __RCSID
     32 __RCSID("$NetBSD: type.c,v 1.2 2014/09/29 05:56:43 jnemeth Exp $");
     33 #endif
     34 
     35 #include <sys/types.h>
     36 
     37 #include <err.h>
     38 #include <stddef.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include "map.h"
     45 #include "gpt.h"
     46 
     47 static int all;
     48 static uuid_t type, newtype;
     49 static off_t block, size;
     50 static unsigned int entry;
     51 static uint8_t *label;
     52 
     53 const char typemsg1[] = "type -a -T newtype device ...";
     54 const char typemsg2[] = "type [-b blocknr] [-i index] [-L label] "
     55 	"[-s sectors] [-t type]";
     56 const char typemsg3[] = "     -T newtype device ...";
     57 
     58 __dead static void
     59 usage_type(void)
     60 {
     61 
     62 	fprintf(stderr,
     63             "usage: %s %s\n"
     64             "       %s %s\n"
     65             "       %*s %s\n", getprogname(), typemsg1,
     66             getprogname(), typemsg2, (int)strlen(getprogname()), "", typemsg3);
     67 	exit(1);
     68 }
     69 
     70 static void
     71 chtype(int fd)
     72 {
     73 	uuid_t uuid;
     74 	map_t *gpt, *tpg;
     75 	map_t *tbl, *lbt;
     76 	map_t *m;
     77 	struct gpt_hdr *hdr;
     78 	struct gpt_ent *ent;
     79 	unsigned int i;
     80 
     81 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     82 	if (gpt == NULL) {
     83 		warnx("%s: error: no primary GPT header; run create or recover",
     84 		    device_name);
     85 		return;
     86 	}
     87 
     88 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     89 	if (tpg == NULL) {
     90 		warnx("%s: error: no secondary GPT header; run recover",
     91 		    device_name);
     92 		return;
     93 	}
     94 
     95 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     96 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     97 	if (tbl == NULL || lbt == NULL) {
     98 		warnx("%s: error: run recover -- trust me", device_name);
     99 		return;
    100 	}
    101 
    102 	/* Change type of all matching entries in the map. */
    103 	for (m = map_first(); m != NULL; m = m->map_next) {
    104 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
    105 			continue;
    106 		if (entry > 0 && entry != m->map_index)
    107 			continue;
    108 		if (block > 0 && block != m->map_start)
    109 			continue;
    110 		if (size > 0 && size != m->map_size)
    111 			continue;
    112 
    113 		i = m->map_index - 1;
    114 
    115 		hdr = gpt->map_data;
    116 		ent = (void*)((char*)tbl->map_data + i *
    117 		    le32toh(hdr->hdr_entsz));
    118 
    119 		if (label != NULL)
    120 			if (strcmp((char *)label,
    121 			    (char *)utf16_to_utf8(ent->ent_name)) != 0)
    122 				continue;
    123 
    124 		le_uuid_dec(ent->ent_type, &uuid);
    125 		if (!uuid_is_nil(&type, NULL) &&
    126 		    !uuid_equal(&type, &uuid, NULL))
    127 			continue;
    128 
    129 		/* Change the primary entry. */
    130 		le_uuid_enc(ent->ent_type, &newtype);
    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 		/* Change the secondary entry. */
    145 		le_uuid_enc(ent->ent_type, &newtype);
    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 type changed\n", m->map_index);
    155 	}
    156 }
    157 
    158 int
    159 cmd_type(int argc, char *argv[])
    160 {
    161 	char *p;
    162 	int ch, fd;
    163 	int64_t human_num;
    164 
    165 	/* Get the type options */
    166 	while ((ch = getopt(argc, argv, "ab:i:L:s:t:T:")) != -1) {
    167 		switch(ch) {
    168 		case 'a':
    169 			if (all > 0)
    170 				usage_type();
    171 			all = 1;
    172 			break;
    173 		case 'b':
    174 			if (block > 0)
    175 				usage_type();
    176 			if (dehumanize_number(optarg, &human_num) < 0)
    177 				usage_type();
    178 			block = human_num;
    179 			if (block < 1)
    180 				usage_type();
    181 			break;
    182 		case 'i':
    183 			if (entry > 0)
    184 				usage_type();
    185 			entry = strtoul(optarg, &p, 10);
    186 			if (*p != 0 || entry < 1)
    187 				usage_type();
    188 			break;
    189                 case 'L':
    190                         if (label != NULL)
    191                                 usage_type();
    192                         label = (uint8_t *)strdup(optarg);
    193                         break;
    194 		case 's':
    195 			if (size > 0)
    196 				usage_type();
    197 			size = strtoll(optarg, &p, 10);
    198 			if (*p != 0 || size < 1)
    199 				usage_type();
    200 			break;
    201 		case 't':
    202 			if (!uuid_is_nil(&type, NULL))
    203 				usage_type();
    204 			if (parse_uuid(optarg, &type) != 0)
    205 				usage_type();
    206 			break;
    207 		case 'T':
    208 			if (!uuid_is_nil(&newtype, NULL))
    209 				usage_type();
    210 			if (parse_uuid(optarg, &newtype) != 0)
    211 				usage_type();
    212 			break;
    213 		default:
    214 			usage_type();
    215 		}
    216 	}
    217 
    218 	if (!all ^
    219 	    (block > 0 || entry > 0 || label != NULL || size > 0 ||
    220 	    !uuid_is_nil(&type, NULL)))
    221 		usage_type();
    222 	if (uuid_is_nil(&newtype, NULL))
    223 		usage_type();
    224 
    225 	if (argc == optind)
    226 		usage_type();
    227 
    228 	while (optind < argc) {
    229 		fd = gpt_open(argv[optind++]);
    230 		if (fd == -1) {
    231 			warn("unable to open device '%s'", device_name);
    232 			continue;
    233 		}
    234 
    235 		chtype(fd);
    236 
    237 		gpt_close(fd);
    238 	}
    239 
    240 	return (0);
    241 }
    242