Home | History | Annotate | Line # | Download | only in gpt
label.c revision 1.16
      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.16 2014/09/29 20:28:57 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 
     51 static int all;
     52 static uuid_t type;
     53 static off_t block, size;
     54 static unsigned int entry;
     55 static uint8_t *name, *xlabel;
     56 
     57 const char labelmsg1[] = "label -a <-l label | -f file> device ...";
     58 const char labelmsg2[] = "label [-b blocknr] [-i index] [-L label] "
     59 	"[-s sectors]";
     60 const char labelmsg3[] = "      [-t uuid] <-l label | -f file> device ...";
     61 
     62 __dead static void
     63 usage_label(void)
     64 {
     65 	fprintf(stderr,
     66 	    "usage: %s %s\n"
     67 	    "       %s %s\n"
     68 	    "       %*s %s\n", getprogname(), labelmsg1,
     69 	    getprogname(), labelmsg2, (int)strlen(getprogname()), "", labelmsg3);
     70 	exit(1);
     71 }
     72 
     73 static void
     74 label(int fd)
     75 {
     76 	uuid_t uuid;
     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 		le_uuid_dec(ent->ent_type, &uuid);
    128 		if (!uuid_is_nil(&type, NULL) &&
    129 		    !uuid_equal(&type, &uuid, NULL))
    130 			continue;
    131 
    132 		/* Label the primary entry. */
    133 		utf8_to_utf16(name, ent->ent_name, 36);
    134 
    135 		hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    136 		    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    137 		hdr->hdr_crc_self = 0;
    138 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    139 
    140 		gpt_write(fd, gpt);
    141 		gpt_write(fd, tbl);
    142 
    143 		hdr = tpg->map_data;
    144 		ent = (void*)((char*)lbt->map_data + i *
    145 		    le32toh(hdr->hdr_entsz));
    146 
    147 		/* Label the secondary entry. */
    148 		utf8_to_utf16(name, ent->ent_name, 36);
    149 
    150 		hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    151 		    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    152 		hdr->hdr_crc_self = 0;
    153 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    154 
    155 		gpt_write(fd, lbt);
    156 		gpt_write(fd, tpg);
    157 
    158 		printf("partition %d on %s labeled %s\n", m->map_index,
    159 		    device_name, name);
    160 	}
    161 }
    162 
    163 static void
    164 name_from_file(const char *fn)
    165 {
    166 	FILE *f;
    167 	char *p;
    168 	size_t maxlen = 1024;
    169 	size_t len;
    170 
    171 	if (strcmp(fn, "-") != 0) {
    172 		f = fopen(fn, "r");
    173 		if (f == NULL)
    174 			err(1, "unable to open file %s", fn);
    175 	} else
    176 		f = stdin;
    177 	name = malloc(maxlen);
    178 	len = fread(name, 1, maxlen - 1, f);
    179 	if (ferror(f))
    180 		err(1, "unable to read label from file %s", fn);
    181 	if (f != stdin)
    182 		fclose(f);
    183 	name[len] = '\0';
    184 	/* Only keep the first line, excluding the newline character. */
    185 	p = strchr((const char *)name, '\n');
    186 	if (p != NULL)
    187 		*p = '\0';
    188 }
    189 
    190 int
    191 cmd_label(int argc, char *argv[])
    192 {
    193 	char *p;
    194 	int ch, fd;
    195 	int64_t human_num;
    196 
    197 	/* Get the label options */
    198 	while ((ch = getopt(argc, argv, "ab:f:i:L:l:s:t:")) != -1) {
    199 		switch(ch) {
    200 		case 'a':
    201 			if (all > 0)
    202 				usage_label();
    203 			all = 1;
    204 			break;
    205 		case 'b':
    206 			if (block > 0)
    207 				usage_label();
    208 			if (dehumanize_number(optarg, &human_num) < 0)
    209 				usage_label();
    210 			block = human_num;
    211 			if (block < 1)
    212 				usage_label();
    213 			break;
    214 		case 'f':
    215 			if (name != NULL)
    216 				usage_label();
    217 			name_from_file(optarg);
    218 			break;
    219 		case 'i':
    220 			if (entry > 0)
    221 				usage_label();
    222 			entry = strtoul(optarg, &p, 10);
    223 			if (*p != 0 || entry < 1)
    224 				usage_label();
    225 			break;
    226 		case 'L':
    227 			if (xlabel != NULL)
    228 				usage_label();
    229 			xlabel = (uint8_t *)strdup(optarg);
    230 			break;
    231 		case 'l':
    232 			if (name != NULL)
    233 				usage_label();
    234 			name = (uint8_t *)strdup(optarg);
    235 			break;
    236 		case 's':
    237 			if (size > 0)
    238 				usage_label();
    239 			size = strtoll(optarg, &p, 10);
    240 			if (*p != 0 || size < 1)
    241 				usage_label();
    242 			break;
    243 		case 't':
    244 			if (!uuid_is_nil(&type, NULL))
    245 				usage_label();
    246 			if (parse_uuid(optarg, &type) != 0)
    247 				usage_label();
    248 			break;
    249 		default:
    250 			usage_label();
    251 		}
    252 	}
    253 
    254 	if (!all ^
    255 	    (block > 0 || entry > 0 || xlabel != NULL || size > 0 ||
    256 	    !uuid_is_nil(&type, NULL)))
    257 		usage_label();
    258 
    259 	if (name == NULL || argc == optind)
    260 		usage_label();
    261 
    262 	while (optind < argc) {
    263 		fd = gpt_open(argv[optind++]);
    264 		if (fd == -1) {
    265 			warn("unable to open device '%s'", device_name);
    266 			continue;
    267 		}
    268 
    269 		label(fd);
    270 
    271 		gpt_close(fd);
    272 	}
    273 
    274 	return (0);
    275 }
    276