2006-10-16 02:34:15 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2005 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-09-30 00:28:57 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-10-16 02:34:15 +04:00
|
|
|
#include <sys/cdefs.h>
|
2006-10-16 02:36:29 +04:00
|
|
|
#ifdef __FBSDID
|
2006-10-16 02:34:15 +04:00
|
|
|
__FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
|
2006-10-16 02:36:29 +04:00
|
|
|
#endif
|
|
|
|
#ifdef __RCSID
|
2015-11-29 03:14:46 +03:00
|
|
|
__RCSID("$NetBSD: label.c,v 1.19 2015/11/29 00:14:46 christos Exp $");
|
2006-10-16 02:36:29 +04:00
|
|
|
#endif
|
2006-10-16 02:34:15 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "map.h"
|
|
|
|
#include "gpt.h"
|
2014-09-30 21:59:59 +04:00
|
|
|
#include "gpt_uuid.h"
|
2006-10-16 02:34:15 +04:00
|
|
|
|
|
|
|
static int all;
|
2014-09-30 21:59:59 +04:00
|
|
|
static gpt_uuid_t type;
|
2006-10-16 02:34:15 +04:00
|
|
|
static off_t block, size;
|
|
|
|
static unsigned int entry;
|
2014-09-29 09:56:43 +04:00
|
|
|
static uint8_t *name, *xlabel;
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2007-12-19 00:46:45 +03:00
|
|
|
const char labelmsg1[] = "label -a <-l label | -f file> device ...";
|
2014-09-29 09:56:43 +04:00
|
|
|
const char labelmsg2[] = "label [-b blocknr] [-i index] [-L label] "
|
|
|
|
"[-s sectors]";
|
2007-12-19 00:46:45 +03:00
|
|
|
const char labelmsg3[] = " [-t uuid] <-l label | -f file> device ...";
|
|
|
|
|
2011-08-27 21:38:16 +04:00
|
|
|
__dead static void
|
2006-10-16 02:34:15 +04:00
|
|
|
usage_label(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2007-12-19 00:46:45 +03:00
|
|
|
"usage: %s %s\n"
|
|
|
|
" %s %s\n"
|
|
|
|
" %*s %s\n", getprogname(), labelmsg1,
|
2007-12-19 08:48:33 +03:00
|
|
|
getprogname(), labelmsg2, (int)strlen(getprogname()), "", labelmsg3);
|
2006-10-16 02:34:15 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
label(int fd)
|
|
|
|
{
|
|
|
|
map_t *gpt, *tpg;
|
|
|
|
map_t *tbl, *lbt;
|
|
|
|
map_t *m;
|
|
|
|
struct gpt_hdr *hdr;
|
|
|
|
struct gpt_ent *ent;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
|
|
|
|
if (gpt == NULL) {
|
|
|
|
warnx("%s: error: no primary GPT header; run create or recover",
|
|
|
|
device_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
|
|
|
|
if (tpg == NULL) {
|
|
|
|
warnx("%s: error: no secondary GPT header; run recover",
|
|
|
|
device_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
|
|
|
|
lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
|
|
|
|
if (tbl == NULL || lbt == NULL) {
|
|
|
|
warnx("%s: error: run recover -- trust me", device_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Relabel all matching entries in the map. */
|
|
|
|
for (m = map_first(); m != NULL; m = m->map_next) {
|
|
|
|
if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
|
|
|
|
continue;
|
|
|
|
if (entry > 0 && entry != m->map_index)
|
|
|
|
continue;
|
|
|
|
if (block > 0 && block != m->map_start)
|
|
|
|
continue;
|
|
|
|
if (size > 0 && size != m->map_size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
i = m->map_index - 1;
|
|
|
|
|
|
|
|
hdr = gpt->map_data;
|
|
|
|
ent = (void*)((char*)tbl->map_data + i *
|
|
|
|
le32toh(hdr->hdr_entsz));
|
2014-09-29 09:56:43 +04:00
|
|
|
|
|
|
|
if (xlabel != NULL)
|
|
|
|
if (strcmp((char *)xlabel,
|
|
|
|
(char *)utf16_to_utf8(ent->ent_name)) != 0)
|
|
|
|
continue;
|
|
|
|
|
2014-09-30 21:59:59 +04:00
|
|
|
if (!gpt_uuid_is_nil(type) &&
|
|
|
|
!gpt_uuid_equal(type, ent->ent_type))
|
2006-10-16 02:34:15 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Label the primary entry. */
|
|
|
|
utf8_to_utf16(name, ent->ent_name, 36);
|
|
|
|
|
|
|
|
hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
|
|
|
|
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
|
|
|
|
hdr->hdr_crc_self = 0;
|
|
|
|
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
|
|
|
|
|
|
|
|
gpt_write(fd, gpt);
|
|
|
|
gpt_write(fd, tbl);
|
|
|
|
|
|
|
|
hdr = tpg->map_data;
|
|
|
|
ent = (void*)((char*)lbt->map_data + i *
|
|
|
|
le32toh(hdr->hdr_entsz));
|
|
|
|
|
2013-04-13 20:48:03 +04:00
|
|
|
/* Label the secondary entry. */
|
2006-10-16 02:34:15 +04:00
|
|
|
utf8_to_utf16(name, ent->ent_name, 36);
|
|
|
|
|
|
|
|
hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
|
|
|
|
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
|
|
|
|
hdr->hdr_crc_self = 0;
|
|
|
|
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
|
|
|
|
|
|
|
|
gpt_write(fd, lbt);
|
|
|
|
gpt_write(fd, tpg);
|
|
|
|
|
2009-03-13 01:59:03 +03:00
|
|
|
printf("partition %d on %s labeled %s\n", m->map_index,
|
2006-10-16 02:36:29 +04:00
|
|
|
device_name, name);
|
2006-10-16 02:34:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
name_from_file(const char *fn)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char *p;
|
|
|
|
size_t maxlen = 1024;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (strcmp(fn, "-") != 0) {
|
|
|
|
f = fopen(fn, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
err(1, "unable to open file %s", fn);
|
|
|
|
} else
|
|
|
|
f = stdin;
|
|
|
|
name = malloc(maxlen);
|
|
|
|
len = fread(name, 1, maxlen - 1, f);
|
|
|
|
if (ferror(f))
|
|
|
|
err(1, "unable to read label from file %s", fn);
|
|
|
|
if (f != stdin)
|
|
|
|
fclose(f);
|
|
|
|
name[len] = '\0';
|
|
|
|
/* Only keep the first line, excluding the newline character. */
|
2006-10-16 02:43:30 +04:00
|
|
|
p = strchr((const char *)name, '\n');
|
2006-10-16 02:34:15 +04:00
|
|
|
if (p != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_label(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int ch, fd;
|
2013-11-22 08:21:02 +04:00
|
|
|
int64_t human_num;
|
2006-10-16 02:34:15 +04:00
|
|
|
|
|
|
|
/* Get the label options */
|
2014-09-29 09:56:43 +04:00
|
|
|
while ((ch = getopt(argc, argv, "ab:f:i:L:l:s:t:")) != -1) {
|
2006-10-16 02:34:15 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
|
|
|
if (all > 0)
|
|
|
|
usage_label();
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (block > 0)
|
|
|
|
usage_label();
|
2013-11-22 08:21:02 +04:00
|
|
|
if (dehumanize_number(optarg, &human_num) < 0)
|
2006-10-16 02:34:15 +04:00
|
|
|
usage_label();
|
2013-11-22 08:21:02 +04:00
|
|
|
block = human_num;
|
2013-11-28 05:37:14 +04:00
|
|
|
if (block < 1)
|
|
|
|
usage_label();
|
2006-10-16 02:34:15 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (name != NULL)
|
|
|
|
usage_label();
|
|
|
|
name_from_file(optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (entry > 0)
|
|
|
|
usage_label();
|
2007-02-16 20:13:02 +03:00
|
|
|
entry = strtoul(optarg, &p, 10);
|
2006-10-16 02:34:15 +04:00
|
|
|
if (*p != 0 || entry < 1)
|
|
|
|
usage_label();
|
|
|
|
break;
|
2014-09-29 09:56:43 +04:00
|
|
|
case 'L':
|
|
|
|
if (xlabel != NULL)
|
|
|
|
usage_label();
|
|
|
|
xlabel = (uint8_t *)strdup(optarg);
|
|
|
|
break;
|
2006-10-16 02:34:15 +04:00
|
|
|
case 'l':
|
|
|
|
if (name != NULL)
|
|
|
|
usage_label();
|
2006-10-16 02:43:30 +04:00
|
|
|
name = (uint8_t *)strdup(optarg);
|
2006-10-16 02:34:15 +04:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (size > 0)
|
|
|
|
usage_label();
|
|
|
|
size = strtoll(optarg, &p, 10);
|
|
|
|
if (*p != 0 || size < 1)
|
|
|
|
usage_label();
|
|
|
|
break;
|
|
|
|
case 't':
|
2014-09-30 21:59:59 +04:00
|
|
|
if (!gpt_uuid_is_nil(type))
|
2006-10-16 02:34:15 +04:00
|
|
|
usage_label();
|
2014-09-30 21:59:59 +04:00
|
|
|
if (gpt_uuid_parse(optarg, type) != 0)
|
2006-10-16 02:34:15 +04:00
|
|
|
usage_label();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_label();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!all ^
|
2014-09-29 09:56:43 +04:00
|
|
|
(block > 0 || entry > 0 || xlabel != NULL || size > 0 ||
|
2014-09-30 21:59:59 +04:00
|
|
|
!gpt_uuid_is_nil(type)))
|
2006-10-16 02:34:15 +04:00
|
|
|
usage_label();
|
|
|
|
|
|
|
|
if (name == NULL || argc == optind)
|
|
|
|
usage_label();
|
|
|
|
|
|
|
|
while (optind < argc) {
|
2015-11-29 03:14:46 +03:00
|
|
|
fd = gpt_open(argv[optind++], 0);
|
|
|
|
if (fd == -1)
|
2006-10-16 02:34:15 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
label(fd);
|
|
|
|
|
|
|
|
gpt_close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|