Home | History | Annotate | Line # | Download | only in gpt
label.c revision 1.6
      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 #include <sys/cdefs.h>
     28 #ifdef __FBSDID
     29 __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
     30 #endif
     31 #ifdef __RCSID
     32 __RCSID("$NetBSD: label.c,v 1.6 2007/12/19 05:48:34 dogcow Exp $");
     33 #endif
     34 
     35 #include <sys/types.h>
     36 
     37 #include <err.h>
     38 #include <stddef.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include "map.h"
     45 #include "gpt.h"
     46 
     47 static int all;
     48 static uuid_t type;
     49 static off_t block, size;
     50 static unsigned int entry;
     51 static uint8_t *name;
     52 
     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 ...";
     56 
     57 static void
     58 usage_label(void)
     59 {
     60 	fprintf(stderr,
     61 	    "usage: %s %s\n"
     62 	    "       %s %s\n"
     63 	    "       %*s %s\n", getprogname(), labelmsg1,
     64 	    getprogname(), labelmsg2, (int)strlen(getprogname()), "", labelmsg3);
     65 	exit(1);
     66 }
     67 
     68 static void
     69 label(int fd)
     70 {
     71 	uuid_t uuid;
     72 	map_t *gpt, *tpg;
     73 	map_t *tbl, *lbt;
     74 	map_t *m;
     75 	struct gpt_hdr *hdr;
     76 	struct gpt_ent *ent;
     77 	unsigned int i;
     78 
     79 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     80 	if (gpt == NULL) {
     81 		warnx("%s: error: no primary GPT header; run create or recover",
     82 		    device_name);
     83 		return;
     84 	}
     85 
     86 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     87 	if (tpg == NULL) {
     88 		warnx("%s: error: no secondary GPT header; run recover",
     89 		    device_name);
     90 		return;
     91 	}
     92 
     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);
     97 		return;
     98 	}
     99 
    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)
    103 			continue;
    104 		if (entry > 0 && entry != m->map_index)
    105 			continue;
    106 		if (block > 0 && block != m->map_start)
    107 			continue;
    108 		if (size > 0 && size != m->map_size)
    109 			continue;
    110 
    111 		i = m->map_index - 1;
    112 
    113 		hdr = gpt->map_data;
    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))
    119 			continue;
    120 
    121 		/* Label the primary entry. */
    122 		utf8_to_utf16(name, ent->ent_name, 36);
    123 
    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)));
    128 
    129 		gpt_write(fd, gpt);
    130 		gpt_write(fd, tbl);
    131 
    132 		hdr = tpg->map_data;
    133 		ent = (void*)((char*)lbt->map_data + i *
    134 		    le32toh(hdr->hdr_entsz));
    135 
    136 		/* Label the secundary entry. */
    137 		utf8_to_utf16(name, ent->ent_name, 36);
    138 
    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)));
    143 
    144 		gpt_write(fd, lbt);
    145 		gpt_write(fd, tpg);
    146 
    147 #ifdef __FreeBSD__
    148 		printf("%sp%u labeled\n", device_name, m->map_index);
    149 #endif
    150 #ifdef __NetBSD__
    151 		printf("parition %d on %s labeled %s\n", m->map_index,
    152 		    device_name, name);
    153 #endif
    154 	}
    155 }
    156 
    157 static void
    158 name_from_file(const char *fn)
    159 {
    160 	FILE *f;
    161 	char *p;
    162 	size_t maxlen = 1024;
    163 	size_t len;
    164 
    165 	if (strcmp(fn, "-") != 0) {
    166 		f = fopen(fn, "r");
    167 		if (f == NULL)
    168 			err(1, "unable to open file %s", fn);
    169 	} else
    170 		f = stdin;
    171 	name = malloc(maxlen);
    172 	len = fread(name, 1, maxlen - 1, f);
    173 	if (ferror(f))
    174 		err(1, "unable to read label from file %s", fn);
    175 	if (f != stdin)
    176 		fclose(f);
    177 	name[len] = '\0';
    178 	/* Only keep the first line, excluding the newline character. */
    179 	p = strchr((const char *)name, '\n');
    180 	if (p != NULL)
    181 		*p = '\0';
    182 }
    183 
    184 int
    185 cmd_label(int argc, char *argv[])
    186 {
    187 	char *p;
    188 	int ch, fd;
    189 
    190 	/* Get the label options */
    191 	while ((ch = getopt(argc, argv, "ab:f:i:l:s:t:")) != -1) {
    192 		switch(ch) {
    193 		case 'a':
    194 			if (all > 0)
    195 				usage_label();
    196 			all = 1;
    197 			break;
    198 		case 'b':
    199 			if (block > 0)
    200 				usage_label();
    201 			block = strtoll(optarg, &p, 10);
    202 			if (*p != 0 || block < 1)
    203 				usage_label();
    204 			break;
    205 		case 'f':
    206 			if (name != NULL)
    207 				usage_label();
    208 			name_from_file(optarg);
    209 			break;
    210 		case 'i':
    211 			if (entry > 0)
    212 				usage_label();
    213 			entry = strtoul(optarg, &p, 10);
    214 			if (*p != 0 || entry < 1)
    215 				usage_label();
    216 			break;
    217 		case 'l':
    218 			if (name != NULL)
    219 				usage_label();
    220 			name = (uint8_t *)strdup(optarg);
    221 			break;
    222 		case 's':
    223 			if (size > 0)
    224 				usage_label();
    225 			size = strtoll(optarg, &p, 10);
    226 			if (*p != 0 || size < 1)
    227 				usage_label();
    228 			break;
    229 		case 't':
    230 			if (!uuid_is_nil(&type, NULL))
    231 				usage_label();
    232 			if (parse_uuid(optarg, &type) != 0)
    233 				usage_label();
    234 			break;
    235 		default:
    236 			usage_label();
    237 		}
    238 	}
    239 
    240 	if (!all ^
    241 	    (block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL)))
    242 		usage_label();
    243 
    244 	if (name == NULL || argc == optind)
    245 		usage_label();
    246 
    247 	while (optind < argc) {
    248 		fd = gpt_open(argv[optind++]);
    249 		if (fd == -1) {
    250 			warn("unable to open device '%s'", device_name);
    251 			continue;
    252 		}
    253 
    254 		label(fd);
    255 
    256 		gpt_close(fd);
    257 	}
    258 
    259 	return (0);
    260 }
    261