2 * Copyright (c) 2005 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $
27 * $DragonFly: src/sbin/gpt/label.c,v 1.2 2007/06/17 08:34:59 dillon Exp $
30 #include <sys/types.h>
44 static off_t block, size;
45 static unsigned int entry = NOENTRY;
51 const char *common = "<-l label | -f file> device ...";
55 " %s [-b lba] [-i index] [-s lba] [-t uuid] %s\n",
56 getprogname(), common, getprogname(), common);
71 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
73 warnx("%s: error: no primary GPT header; run create or recover",
78 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
80 warnx("%s: error: no secondary GPT header; run recover",
85 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
86 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
87 if (tbl == NULL || lbt == NULL) {
88 warnx("%s: error: run recover -- trust me", device_name);
92 /* Relabel all matching entries in the map. */
93 for (m = map_first(); m != NULL; m = m->map_next) {
94 if (m->map_type != MAP_TYPE_GPT_PART || m->map_index == NOENTRY)
96 if (entry != NOENTRY && entry != m->map_index)
98 if (block > 0 && block != m->map_start)
100 if (size > 0 && size != m->map_size)
106 ent = (void*)((char*)tbl->map_data + i *
107 le32toh(hdr->hdr_entsz));
108 le_uuid_dec(&ent->ent_type, &uuid);
109 if (!uuid_is_nil(&type, NULL) &&
110 !uuid_equal(&type, &uuid, NULL))
113 /* Label the primary entry. */
114 utf8_to_utf16(name, ent->ent_name, 36);
116 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
117 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
118 hdr->hdr_crc_self = 0;
119 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
125 ent = (void*)((char*)lbt->map_data + i *
126 le32toh(hdr->hdr_entsz));
128 /* Label the secundary entry. */
129 utf8_to_utf16(name, ent->ent_name, 36);
131 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
132 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
133 hdr->hdr_crc_self = 0;
134 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
139 printf("%ss%u labeled\n", device_name, m->map_index);
144 name_from_file(const char *fn)
148 size_t maxlen = 1024;
151 if (strcmp(fn, "-") != 0) {
154 err(1, "unable to open file %s", fn);
157 name = malloc(maxlen);
158 len = fread(name, 1, maxlen - 1, f);
160 err(1, "unable to read label from file %s", fn);
164 /* Only keep the first line, excluding the newline character. */
165 p = strchr(name, '\n');
171 cmd_label(int argc, char *argv[])
176 /* Get the label options */
177 while ((ch = getopt(argc, argv, "ab:f:i:l:s:t:")) != -1) {
187 block = strtoll(optarg, &p, 10);
188 if (*p != 0 || block < 1)
194 name_from_file(optarg);
197 if (entry != NOENTRY)
199 entry = strtoul(optarg, &p, 10);
200 if (*p != 0 || entry == NOENTRY)
206 name = strdup(optarg);
211 size = strtoll(optarg, &p, 10);
212 if (*p != 0 || size < 1)
216 if (!uuid_is_nil(&type, NULL))
218 if (parse_uuid(optarg, &type) != 0)
227 (block > 0 || entry != NOENTRY || size > 0 ||
228 !uuid_is_nil(&type, NULL)))
231 if (name == NULL || argc == optind)
234 while (optind < argc) {
235 fd = gpt_open(argv[optind++]);
237 warn("unable to open device '%s'", device_name);