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