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.
27 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
32 __RCSID("$NetBSD: label.c,v 1.6 2007/12/19 05:48:34 dogcow Exp $");
35 #include <sys/types.h>
49 static off_t block
, size
;
50 static unsigned int entry
;
53 const char labelmsg1
[] = "label -a <-l label | -f file> device ...";
54 const char labelmsg2
[] = "label [-b lba] [-i index] [-s lba]";
55 const char labelmsg3
[] = " [-t uuid] <-l label | -f file> device ...";
63 " %*s %s\n", getprogname(), labelmsg1
,
64 getprogname(), labelmsg2
, (int)strlen(getprogname()), "", labelmsg3
);
79 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
81 warnx("%s: error: no primary GPT header; run create or recover",
86 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
88 warnx("%s: error: no secondary GPT header; run recover",
93 tbl
= map_find(MAP_TYPE_PRI_GPT_TBL
);
94 lbt
= map_find(MAP_TYPE_SEC_GPT_TBL
);
95 if (tbl
== NULL
|| lbt
== NULL
) {
96 warnx("%s: error: run recover -- trust me", device_name
);
100 /* Relabel all matching entries in the map. */
101 for (m
= map_first(); m
!= NULL
; m
= m
->map_next
) {
102 if (m
->map_type
!= MAP_TYPE_GPT_PART
|| m
->map_index
< 1)
104 if (entry
> 0 && entry
!= m
->map_index
)
106 if (block
> 0 && block
!= m
->map_start
)
108 if (size
> 0 && size
!= m
->map_size
)
111 i
= m
->map_index
- 1;
114 ent
= (void*)((char*)tbl
->map_data
+ i
*
115 le32toh(hdr
->hdr_entsz
));
116 le_uuid_dec(&ent
->ent_type
, &uuid
);
117 if (!uuid_is_nil(&type
, NULL
) &&
118 !uuid_equal(&type
, &uuid
, NULL
))
121 /* Label the primary entry. */
122 utf8_to_utf16(name
, ent
->ent_name
, 36);
124 hdr
->hdr_crc_table
= htole32(crc32(tbl
->map_data
,
125 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
126 hdr
->hdr_crc_self
= 0;
127 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
133 ent
= (void*)((char*)lbt
->map_data
+ i
*
134 le32toh(hdr
->hdr_entsz
));
136 /* Label the secundary entry. */
137 utf8_to_utf16(name
, ent
->ent_name
, 36);
139 hdr
->hdr_crc_table
= htole32(crc32(lbt
->map_data
,
140 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
141 hdr
->hdr_crc_self
= 0;
142 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
148 printf("%sp%u labeled\n", device_name
, m
->map_index
);
151 printf("partition %d on %s labeled %s\n", m
->map_index
,
158 name_from_file(const char *fn
)
162 size_t maxlen
= 1024;
165 if (strcmp(fn
, "-") != 0) {
168 err(1, "unable to open file %s", fn
);
171 name
= malloc(maxlen
);
172 len
= fread(name
, 1, maxlen
- 1, f
);
174 err(1, "unable to read label from file %s", fn
);
178 /* Only keep the first line, excluding the newline character. */
179 p
= strchr((const char *)name
, '\n');
185 cmd_label(int argc
, char *argv
[])
190 /* Get the label options */
191 while ((ch
= getopt(argc
, argv
, "ab:f:i:l:s:t:")) != -1) {
201 block
= strtoll(optarg
, &p
, 10);
202 if (*p
!= 0 || block
< 1)
208 name_from_file(optarg
);
213 entry
= strtoul(optarg
, &p
, 10);
214 if (*p
!= 0 || entry
< 1)
220 name
= (uint8_t *)strdup(optarg
);
225 size
= strtoll(optarg
, &p
, 10);
226 if (*p
!= 0 || size
< 1)
230 if (!uuid_is_nil(&type
, NULL
))
232 if (parse_uuid(optarg
, &type
) != 0)
241 (block
> 0 || entry
> 0 || size
> 0 || !uuid_is_nil(&type
, NULL
)))
244 if (name
== NULL
|| argc
== optind
)
247 while (optind
< argc
) {
248 fd
= gpt_open(argv
[optind
++]);
250 warn("unable to open device '%s'", device_name
);