label.c revision 1.21 1 /*-
2 * Copyright (c) 2005 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/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: label.c,v 1.21 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 #include "gpt_uuid.h"
52
53 static int all;
54 static gpt_uuid_t type;
55 static off_t block, size;
56 static unsigned int entry;
57 static uint8_t *name, *xlabel;
58
59 static int cmd_label(gpt_t, int, char *[]);
60
61 static const char *labelhelp[] = {
62 "-a <-l label | -f file>",
63 "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] <-l label | -f file>",
64 };
65
66 struct gpt_cmd c_label = {
67 "label",
68 cmd_label,
69 labelhelp, __arraycount(labelhelp),
70 0,
71 };
72
73 #define usage() gpt_usage(NULL, &c_label)
74
75 static int
76 label(gpt_t gpt)
77 {
78 map_t m;
79 struct gpt_hdr *hdr;
80 struct gpt_ent *ent;
81 unsigned int i;
82
83 if ((hdr = gpt_hdr(gpt)) == NULL)
84 return -1;
85
86 /* Relabel all matching entries in the map. */
87 for (m = map_first(gpt); m != NULL; m = m->map_next) {
88 if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
89 continue;
90 if (entry > 0 && entry != m->map_index)
91 continue;
92 if (block > 0 && block != m->map_start)
93 continue;
94 if (size > 0 && size != m->map_size)
95 continue;
96
97 i = m->map_index - 1;
98
99 ent = gpt_ent_primary(gpt, i);
100 if (xlabel != NULL)
101 if (strcmp((char *)xlabel,
102 (char *)utf16_to_utf8(ent->ent_name)) != 0)
103 continue;
104
105 if (!gpt_uuid_is_nil(type) &&
106 !gpt_uuid_equal(type, ent->ent_type))
107 continue;
108
109 /* Label the primary entry. */
110 utf8_to_utf16(name, ent->ent_name, 36);
111
112 if (gpt_write_primary(gpt) == -1)
113 return -1;
114
115 ent = gpt_ent_backup(gpt, i);
116 /* Label the secondary entry. */
117 utf8_to_utf16(name, ent->ent_name, 36);
118
119 if (gpt_write_backup(gpt) == -1)
120 return -1;
121
122 gpt_msg(gpt, "Partition %d labeled %s", m->map_index, name);
123 }
124 return 0;
125 }
126
127 static void
128 name_from_file(const char *fn)
129 {
130 FILE *f;
131 char *p;
132 size_t maxlen = 1024;
133 size_t len;
134
135 if (strcmp(fn, "-") != 0) {
136 f = fopen(fn, "r");
137 if (f == NULL)
138 err(1, "unable to open file %s", fn);
139 } else
140 f = stdin;
141 name = malloc(maxlen);
142 len = fread(name, 1, maxlen - 1, f);
143 if (ferror(f))
144 err(1, "unable to read label from file %s", fn);
145 if (f != stdin)
146 fclose(f);
147 name[len] = '\0';
148 /* Only keep the first line, excluding the newline character. */
149 p = strchr((const char *)name, '\n');
150 if (p != NULL)
151 *p = '\0';
152 }
153
154 static int
155 cmd_label(gpt_t gpt, int argc, char *argv[])
156 {
157 char *p;
158 int ch;
159 int64_t human_num;
160
161 /* Get the label options */
162 while ((ch = getopt(argc, argv, "ab:f:i:L:l:s:t:")) != -1) {
163 switch(ch) {
164 case 'a':
165 if (all > 0)
166 return usage();
167 all = 1;
168 break;
169 case 'b':
170 if (block > 0)
171 return usage();
172 if (dehumanize_number(optarg, &human_num) < 0)
173 return usage();
174 block = human_num;
175 if (block < 1)
176 return usage();
177 break;
178 case 'f':
179 if (name != NULL)
180 return usage();
181 name_from_file(optarg);
182 break;
183 case 'i':
184 if (entry > 0)
185 return usage();
186 entry = strtoul(optarg, &p, 10);
187 if (*p != 0 || entry < 1)
188 return usage();
189 break;
190 case 'L':
191 if (xlabel != NULL)
192 return usage();
193 xlabel = (uint8_t *)strdup(optarg);
194 break;
195 case 'l':
196 if (name != NULL)
197 return usage();
198 name = (uint8_t *)strdup(optarg);
199 break;
200 case 's':
201 if (size > 0)
202 return usage();
203 size = strtoll(optarg, &p, 10);
204 if (*p != 0 || size < 1)
205 return usage();
206 break;
207 case 't':
208 if (!gpt_uuid_is_nil(type))
209 return usage();
210 if (gpt_uuid_parse(optarg, type) != 0)
211 return usage();
212 break;
213 default:
214 return usage();
215 }
216 }
217
218 if (!all ^
219 (block > 0 || entry > 0 || xlabel != NULL || size > 0 ||
220 !gpt_uuid_is_nil(type)))
221 return usage();
222
223 if (name == NULL || argc != optind)
224 return usage();
225
226 return label(gpt);
227 }
228