Home | History | Annotate | Line # | Download | only in gpt
type.c revision 1.9
      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.9 2015/12/01 16:32:19 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 #include "gpt_private.h"
     51 
     52 static int all;
     53 static gpt_uuid_t type, newtype;
     54 static off_t block, size;
     55 static unsigned int entry;
     56 static uint8_t *label;
     57 
     58 static int cmd_type(gpt_t, int, char *[]);
     59 
     60 static const char *typehelp[] = {
     61 "-a -T newtype",
     62 "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype",
     63 };
     64 
     65 struct gpt_cmd c_type = {
     66 	"type",
     67 	cmd_type,
     68 	typehelp, __arraycount(typehelp),
     69 	0,
     70 };
     71 
     72 #define usage() gpt_usage(NULL, &c_type)
     73 
     74 static int
     75 chtype(gpt_t gpt)
     76 {
     77 	map_t m;
     78 	struct gpt_ent *ent;
     79 	unsigned int i;
     80 
     81 	if (gpt_hdr(gpt) == NULL)
     82 		return -1;
     83 
     84 	/* Change type of all matching entries in the map. */
     85 	for (m = map_first(gpt); m != NULL; m = m->map_next) {
     86 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
     87 			continue;
     88 		if (entry > 0 && entry != m->map_index)
     89 			continue;
     90 		if (block > 0 && block != m->map_start)
     91 			continue;
     92 		if (size > 0 && size != m->map_size)
     93 			continue;
     94 
     95 		i = m->map_index - 1;
     96 
     97 		ent = gpt_ent_primary(gpt, i);
     98 
     99 		if (label != NULL)
    100 			if (strcmp((char *)label,
    101 			    (char *)utf16_to_utf8(ent->ent_name)) != 0)
    102 				continue;
    103 
    104 		if (!gpt_uuid_is_nil(type) &&
    105 		    !gpt_uuid_equal(type, ent->ent_type))
    106 			continue;
    107 
    108 		/* Change the primary entry. */
    109 		gpt_uuid_copy(ent->ent_type, newtype);
    110 
    111 		if (gpt_write_primary(gpt) == -1)
    112 			return -1;
    113 
    114 		ent = gpt_ent_backup(gpt, i);
    115 
    116 		/* Change the secondary entry. */
    117 		gpt_uuid_copy(ent->ent_type, newtype);
    118 
    119 		if (gpt_write_backup(gpt) == -1)
    120 			return -1;
    121 
    122 		gpt_msg(gpt, "Partition %d type changed", m->map_index);
    123 	}
    124 	return 0;
    125 }
    126 
    127 static int
    128 cmd_type(gpt_t gpt, int argc, char *argv[])
    129 {
    130 	char *p;
    131 	int ch;
    132 	int64_t human_num;
    133 
    134 	/* Get the type options */
    135 	while ((ch = getopt(argc, argv, "ab:i:L:s:t:T:")) != -1) {
    136 		switch(ch) {
    137 		case 'a':
    138 			if (all > 0)
    139 				return usage();
    140 			all = 1;
    141 			break;
    142 		case 'b':
    143 			if (block > 0)
    144 				return usage();
    145 			if (dehumanize_number(optarg, &human_num) < 0)
    146 				return usage();
    147 			block = human_num;
    148 			if (block < 1)
    149 				return usage();
    150 			break;
    151 		case 'i':
    152 			if (entry > 0)
    153 				return usage();
    154 			entry = strtoul(optarg, &p, 10);
    155 			if (*p != 0 || entry < 1)
    156 				return usage();
    157 			break;
    158                 case 'L':
    159                         if (label != NULL)
    160                                 return usage();
    161                         label = (uint8_t *)strdup(optarg);
    162                         break;
    163 		case 's':
    164 			if (size > 0)
    165 				return usage();
    166 			size = strtoll(optarg, &p, 10);
    167 			if (*p != 0 || size < 1)
    168 				return usage();
    169 			break;
    170 		case 't':
    171 			if (!gpt_uuid_is_nil(type))
    172 				return usage();
    173 			if (gpt_uuid_parse(optarg, type) != 0)
    174 				return usage();
    175 			break;
    176 		case 'T':
    177 			if (!gpt_uuid_is_nil(newtype))
    178 				return usage();
    179 			if (gpt_uuid_parse(optarg, newtype) != 0)
    180 				return usage();
    181 			break;
    182 		default:
    183 			return usage();
    184 		}
    185 	}
    186 
    187 	if (!all ^
    188 	    (block > 0 || entry > 0 || label != NULL || size > 0 ||
    189 	    !gpt_uuid_is_nil(type)))
    190 		return usage();
    191 	if (gpt_uuid_is_nil(newtype))
    192 		return usage();
    193 
    194 	if (argc != optind)
    195 		return usage();
    196 
    197 	return chtype(gpt);
    198 }
    199