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