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