label.c revision 1.18 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2005 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.16 christos #if HAVE_NBTOOL_CONFIG_H
28 1.16 christos #include "nbtool_config.h"
29 1.16 christos #endif
30 1.16 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.2 christos #ifdef __FBSDID
33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
34 1.2 christos #endif
35 1.2 christos #ifdef __RCSID
36 1.18 christos __RCSID("$NetBSD: label.c,v 1.18 2014/09/30 17:59:59 christos Exp $");
37 1.2 christos #endif
38 1.1 christos
39 1.1 christos #include <sys/types.h>
40 1.1 christos
41 1.1 christos #include <err.h>
42 1.1 christos #include <stddef.h>
43 1.1 christos #include <stdio.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <string.h>
46 1.1 christos #include <unistd.h>
47 1.1 christos
48 1.1 christos #include "map.h"
49 1.1 christos #include "gpt.h"
50 1.18 christos #include "gpt_uuid.h"
51 1.1 christos
52 1.1 christos static int all;
53 1.18 christos static gpt_uuid_t type;
54 1.1 christos static off_t block, size;
55 1.1 christos static unsigned int entry;
56 1.15 jnemeth static uint8_t *name, *xlabel;
57 1.1 christos
58 1.5 riz const char labelmsg1[] = "label -a <-l label | -f file> device ...";
59 1.15 jnemeth const char labelmsg2[] = "label [-b blocknr] [-i index] [-L label] "
60 1.15 jnemeth "[-s sectors]";
61 1.5 riz const char labelmsg3[] = " [-t uuid] <-l label | -f file> device ...";
62 1.5 riz
63 1.8 joerg __dead static void
64 1.1 christos usage_label(void)
65 1.1 christos {
66 1.1 christos fprintf(stderr,
67 1.5 riz "usage: %s %s\n"
68 1.5 riz " %s %s\n"
69 1.5 riz " %*s %s\n", getprogname(), labelmsg1,
70 1.6 dogcow getprogname(), labelmsg2, (int)strlen(getprogname()), "", labelmsg3);
71 1.1 christos exit(1);
72 1.1 christos }
73 1.1 christos
74 1.1 christos static void
75 1.1 christos label(int fd)
76 1.1 christos {
77 1.1 christos map_t *gpt, *tpg;
78 1.1 christos map_t *tbl, *lbt;
79 1.1 christos map_t *m;
80 1.1 christos struct gpt_hdr *hdr;
81 1.1 christos struct gpt_ent *ent;
82 1.1 christos unsigned int i;
83 1.1 christos
84 1.1 christos gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
85 1.1 christos if (gpt == NULL) {
86 1.1 christos warnx("%s: error: no primary GPT header; run create or recover",
87 1.1 christos device_name);
88 1.1 christos return;
89 1.1 christos }
90 1.1 christos
91 1.1 christos tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
92 1.1 christos if (tpg == NULL) {
93 1.1 christos warnx("%s: error: no secondary GPT header; run recover",
94 1.1 christos device_name);
95 1.1 christos return;
96 1.1 christos }
97 1.1 christos
98 1.1 christos tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
99 1.1 christos lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
100 1.1 christos if (tbl == NULL || lbt == NULL) {
101 1.1 christos warnx("%s: error: run recover -- trust me", device_name);
102 1.1 christos return;
103 1.1 christos }
104 1.1 christos
105 1.1 christos /* Relabel all matching entries in the map. */
106 1.1 christos for (m = map_first(); m != NULL; m = m->map_next) {
107 1.1 christos if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
108 1.1 christos continue;
109 1.1 christos if (entry > 0 && entry != m->map_index)
110 1.1 christos continue;
111 1.1 christos if (block > 0 && block != m->map_start)
112 1.1 christos continue;
113 1.1 christos if (size > 0 && size != m->map_size)
114 1.1 christos continue;
115 1.1 christos
116 1.1 christos i = m->map_index - 1;
117 1.1 christos
118 1.1 christos hdr = gpt->map_data;
119 1.1 christos ent = (void*)((char*)tbl->map_data + i *
120 1.1 christos le32toh(hdr->hdr_entsz));
121 1.15 jnemeth
122 1.15 jnemeth if (xlabel != NULL)
123 1.15 jnemeth if (strcmp((char *)xlabel,
124 1.15 jnemeth (char *)utf16_to_utf8(ent->ent_name)) != 0)
125 1.15 jnemeth continue;
126 1.15 jnemeth
127 1.18 christos if (!gpt_uuid_is_nil(type) &&
128 1.18 christos !gpt_uuid_equal(type, ent->ent_type))
129 1.1 christos continue;
130 1.1 christos
131 1.1 christos /* Label the primary entry. */
132 1.1 christos utf8_to_utf16(name, ent->ent_name, 36);
133 1.1 christos
134 1.1 christos hdr->hdr_crc_table = htole32(crc32(tbl->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, gpt);
140 1.1 christos gpt_write(fd, tbl);
141 1.1 christos
142 1.1 christos hdr = tpg->map_data;
143 1.1 christos ent = (void*)((char*)lbt->map_data + i *
144 1.1 christos le32toh(hdr->hdr_entsz));
145 1.1 christos
146 1.9 jakllsch /* Label the secondary entry. */
147 1.1 christos utf8_to_utf16(name, ent->ent_name, 36);
148 1.1 christos
149 1.1 christos hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
150 1.1 christos le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
151 1.1 christos hdr->hdr_crc_self = 0;
152 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
153 1.1 christos
154 1.1 christos gpt_write(fd, lbt);
155 1.1 christos gpt_write(fd, tpg);
156 1.1 christos
157 1.7 dyoung printf("partition %d on %s labeled %s\n", m->map_index,
158 1.2 christos device_name, name);
159 1.1 christos }
160 1.1 christos }
161 1.1 christos
162 1.1 christos static void
163 1.1 christos name_from_file(const char *fn)
164 1.1 christos {
165 1.1 christos FILE *f;
166 1.1 christos char *p;
167 1.1 christos size_t maxlen = 1024;
168 1.1 christos size_t len;
169 1.1 christos
170 1.1 christos if (strcmp(fn, "-") != 0) {
171 1.1 christos f = fopen(fn, "r");
172 1.1 christos if (f == NULL)
173 1.1 christos err(1, "unable to open file %s", fn);
174 1.1 christos } else
175 1.1 christos f = stdin;
176 1.1 christos name = malloc(maxlen);
177 1.1 christos len = fread(name, 1, maxlen - 1, f);
178 1.1 christos if (ferror(f))
179 1.1 christos err(1, "unable to read label from file %s", fn);
180 1.1 christos if (f != stdin)
181 1.1 christos fclose(f);
182 1.1 christos name[len] = '\0';
183 1.1 christos /* Only keep the first line, excluding the newline character. */
184 1.3 christos p = strchr((const char *)name, '\n');
185 1.1 christos if (p != NULL)
186 1.1 christos *p = '\0';
187 1.1 christos }
188 1.1 christos
189 1.1 christos int
190 1.1 christos cmd_label(int argc, char *argv[])
191 1.1 christos {
192 1.1 christos char *p;
193 1.1 christos int ch, fd;
194 1.12 jnemeth int64_t human_num;
195 1.1 christos
196 1.1 christos /* Get the label options */
197 1.15 jnemeth while ((ch = getopt(argc, argv, "ab:f:i:L:l:s:t:")) != -1) {
198 1.1 christos switch(ch) {
199 1.1 christos case 'a':
200 1.1 christos if (all > 0)
201 1.1 christos usage_label();
202 1.1 christos all = 1;
203 1.1 christos break;
204 1.1 christos case 'b':
205 1.1 christos if (block > 0)
206 1.1 christos usage_label();
207 1.12 jnemeth if (dehumanize_number(optarg, &human_num) < 0)
208 1.1 christos usage_label();
209 1.12 jnemeth block = human_num;
210 1.14 jnemeth if (block < 1)
211 1.14 jnemeth usage_label();
212 1.1 christos break;
213 1.1 christos case 'f':
214 1.1 christos if (name != NULL)
215 1.1 christos usage_label();
216 1.1 christos name_from_file(optarg);
217 1.1 christos break;
218 1.1 christos case 'i':
219 1.1 christos if (entry > 0)
220 1.1 christos usage_label();
221 1.4 riz entry = strtoul(optarg, &p, 10);
222 1.1 christos if (*p != 0 || entry < 1)
223 1.1 christos usage_label();
224 1.1 christos break;
225 1.15 jnemeth case 'L':
226 1.15 jnemeth if (xlabel != NULL)
227 1.15 jnemeth usage_label();
228 1.15 jnemeth xlabel = (uint8_t *)strdup(optarg);
229 1.15 jnemeth break;
230 1.1 christos case 'l':
231 1.1 christos if (name != NULL)
232 1.1 christos usage_label();
233 1.3 christos name = (uint8_t *)strdup(optarg);
234 1.1 christos break;
235 1.1 christos case 's':
236 1.1 christos if (size > 0)
237 1.1 christos usage_label();
238 1.1 christos size = strtoll(optarg, &p, 10);
239 1.1 christos if (*p != 0 || size < 1)
240 1.1 christos usage_label();
241 1.1 christos break;
242 1.1 christos case 't':
243 1.18 christos if (!gpt_uuid_is_nil(type))
244 1.1 christos usage_label();
245 1.18 christos if (gpt_uuid_parse(optarg, type) != 0)
246 1.1 christos usage_label();
247 1.1 christos break;
248 1.1 christos default:
249 1.1 christos usage_label();
250 1.1 christos }
251 1.1 christos }
252 1.1 christos
253 1.1 christos if (!all ^
254 1.15 jnemeth (block > 0 || entry > 0 || xlabel != NULL || size > 0 ||
255 1.18 christos !gpt_uuid_is_nil(type)))
256 1.1 christos usage_label();
257 1.1 christos
258 1.1 christos if (name == NULL || argc == optind)
259 1.1 christos usage_label();
260 1.1 christos
261 1.1 christos while (optind < argc) {
262 1.1 christos fd = gpt_open(argv[optind++]);
263 1.1 christos if (fd == -1) {
264 1.1 christos warn("unable to open device '%s'", device_name);
265 1.1 christos continue;
266 1.1 christos }
267 1.1 christos
268 1.1 christos label(fd);
269 1.1 christos
270 1.1 christos gpt_close(fd);
271 1.1 christos }
272 1.1 christos
273 1.1 christos return (0);
274 1.1 christos }
275