Home | History | Annotate | Line # | Download | only in gpt
label.c revision 1.21
      1   1.1  christos /*-
      2   1.1  christos  * Copyright (c) 2005 Marcel Moolenaar
      3   1.1  christos  * All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that the following conditions
      7   1.1  christos  * are met:
      8   1.1  christos  *
      9   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  christos  *    documentation and/or other materials provided with the distribution.
     14   1.1  christos  *
     15   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1  christos  */
     26   1.1  christos 
     27  1.16  christos #if HAVE_NBTOOL_CONFIG_H
     28  1.16  christos #include "nbtool_config.h"
     29  1.16  christos #endif
     30  1.16  christos 
     31   1.1  christos #include <sys/cdefs.h>
     32   1.2  christos #ifdef __FBSDID
     33   1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
     34   1.2  christos #endif
     35   1.2  christos #ifdef __RCSID
     36  1.21  christos __RCSID("$NetBSD: label.c,v 1.21 2015/12/01 16:32:19 christos Exp $");
     37   1.2  christos #endif
     38   1.1  christos 
     39   1.1  christos #include <sys/types.h>
     40   1.1  christos 
     41   1.1  christos #include <err.h>
     42   1.1  christos #include <stddef.h>
     43   1.1  christos #include <stdio.h>
     44   1.1  christos #include <stdlib.h>
     45   1.1  christos #include <string.h>
     46   1.1  christos #include <unistd.h>
     47   1.1  christos 
     48   1.1  christos #include "map.h"
     49   1.1  christos #include "gpt.h"
     50  1.20  christos #include "gpt_private.h"
     51  1.18  christos #include "gpt_uuid.h"
     52   1.1  christos 
     53   1.1  christos static int all;
     54  1.18  christos static gpt_uuid_t type;
     55   1.1  christos static off_t block, size;
     56   1.1  christos static unsigned int entry;
     57  1.15   jnemeth static uint8_t *name, *xlabel;
     58   1.1  christos 
     59  1.21  christos static int cmd_label(gpt_t, int, char *[]);
     60   1.5       riz 
     61  1.21  christos static const char *labelhelp[] = {
     62  1.21  christos     "-a <-l label | -f file>",
     63  1.21  christos     "[-b blocknr] [-i index] [-L label] [-s sectors] [-t uuid] <-l label | -f file>",
     64  1.21  christos };
     65  1.21  christos 
     66  1.21  christos struct gpt_cmd c_label = {
     67  1.21  christos 	"label",
     68  1.21  christos 	cmd_label,
     69  1.21  christos 	labelhelp, __arraycount(labelhelp),
     70  1.21  christos 	0,
     71  1.21  christos };
     72  1.21  christos 
     73  1.21  christos #define usage() gpt_usage(NULL, &c_label)
     74   1.1  christos 
     75  1.20  christos static int
     76  1.20  christos label(gpt_t gpt)
     77   1.1  christos {
     78  1.20  christos 	map_t m;
     79   1.1  christos 	struct gpt_hdr *hdr;
     80   1.1  christos 	struct gpt_ent *ent;
     81   1.1  christos 	unsigned int i;
     82   1.1  christos 
     83  1.20  christos 	if ((hdr = gpt_hdr(gpt)) == NULL)
     84  1.20  christos 		return -1;
     85   1.1  christos 
     86   1.1  christos 	/* Relabel all matching entries in the map. */
     87  1.20  christos 	for (m = map_first(gpt); m != NULL; m = m->map_next) {
     88   1.1  christos 		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
     89   1.1  christos 			continue;
     90   1.1  christos 		if (entry > 0 && entry != m->map_index)
     91   1.1  christos 			continue;
     92   1.1  christos 		if (block > 0 && block != m->map_start)
     93   1.1  christos 			continue;
     94   1.1  christos 		if (size > 0 && size != m->map_size)
     95   1.1  christos 			continue;
     96   1.1  christos 
     97   1.1  christos 		i = m->map_index - 1;
     98   1.1  christos 
     99  1.20  christos 		ent = gpt_ent_primary(gpt, i);
    100  1.15   jnemeth 		if (xlabel != NULL)
    101  1.15   jnemeth 			if (strcmp((char *)xlabel,
    102  1.15   jnemeth 			    (char *)utf16_to_utf8(ent->ent_name)) != 0)
    103  1.15   jnemeth 				continue;
    104  1.15   jnemeth 
    105  1.18  christos 		if (!gpt_uuid_is_nil(type) &&
    106  1.18  christos 		    !gpt_uuid_equal(type, ent->ent_type))
    107   1.1  christos 			continue;
    108   1.1  christos 
    109   1.1  christos 		/* Label the primary entry. */
    110   1.1  christos 		utf8_to_utf16(name, ent->ent_name, 36);
    111   1.1  christos 
    112  1.20  christos 		if (gpt_write_primary(gpt) == -1)
    113  1.20  christos 			return -1;
    114   1.1  christos 
    115  1.20  christos 		ent = gpt_ent_backup(gpt, i);
    116   1.9  jakllsch 		/* Label the secondary entry. */
    117   1.1  christos 		utf8_to_utf16(name, ent->ent_name, 36);
    118   1.1  christos 
    119  1.20  christos 		if (gpt_write_backup(gpt) == -1)
    120  1.20  christos 			return -1;
    121   1.1  christos 
    122  1.20  christos 		gpt_msg(gpt, "Partition %d labeled %s", m->map_index, name);
    123   1.1  christos 	}
    124  1.20  christos 	return 0;
    125   1.1  christos }
    126   1.1  christos 
    127   1.1  christos static void
    128   1.1  christos name_from_file(const char *fn)
    129   1.1  christos {
    130   1.1  christos 	FILE *f;
    131   1.1  christos 	char *p;
    132   1.1  christos 	size_t maxlen = 1024;
    133   1.1  christos 	size_t len;
    134   1.1  christos 
    135   1.1  christos 	if (strcmp(fn, "-") != 0) {
    136   1.1  christos 		f = fopen(fn, "r");
    137   1.1  christos 		if (f == NULL)
    138   1.1  christos 			err(1, "unable to open file %s", fn);
    139   1.1  christos 	} else
    140   1.1  christos 		f = stdin;
    141   1.1  christos 	name = malloc(maxlen);
    142   1.1  christos 	len = fread(name, 1, maxlen - 1, f);
    143   1.1  christos 	if (ferror(f))
    144   1.1  christos 		err(1, "unable to read label from file %s", fn);
    145   1.1  christos 	if (f != stdin)
    146   1.1  christos 		fclose(f);
    147   1.1  christos 	name[len] = '\0';
    148   1.1  christos 	/* Only keep the first line, excluding the newline character. */
    149   1.3  christos 	p = strchr((const char *)name, '\n');
    150   1.1  christos 	if (p != NULL)
    151   1.1  christos 		*p = '\0';
    152   1.1  christos }
    153   1.1  christos 
    154  1.21  christos static int
    155  1.20  christos cmd_label(gpt_t gpt, int argc, char *argv[])
    156   1.1  christos {
    157   1.1  christos 	char *p;
    158  1.20  christos 	int ch;
    159  1.12   jnemeth 	int64_t human_num;
    160   1.1  christos 
    161   1.1  christos 	/* Get the label options */
    162  1.15   jnemeth 	while ((ch = getopt(argc, argv, "ab:f:i:L:l:s:t:")) != -1) {
    163   1.1  christos 		switch(ch) {
    164   1.1  christos 		case 'a':
    165   1.1  christos 			if (all > 0)
    166  1.21  christos 				return usage();
    167   1.1  christos 			all = 1;
    168   1.1  christos 			break;
    169   1.1  christos 		case 'b':
    170   1.1  christos 			if (block > 0)
    171  1.21  christos 				return usage();
    172  1.12   jnemeth 			if (dehumanize_number(optarg, &human_num) < 0)
    173  1.21  christos 				return usage();
    174  1.12   jnemeth 			block = human_num;
    175  1.14   jnemeth 			if (block < 1)
    176  1.21  christos 				return usage();
    177   1.1  christos 			break;
    178   1.1  christos 		case 'f':
    179   1.1  christos 			if (name != NULL)
    180  1.21  christos 				return usage();
    181   1.1  christos 			name_from_file(optarg);
    182   1.1  christos 			break;
    183   1.1  christos 		case 'i':
    184   1.1  christos 			if (entry > 0)
    185  1.21  christos 				return usage();
    186   1.4       riz 			entry = strtoul(optarg, &p, 10);
    187   1.1  christos 			if (*p != 0 || entry < 1)
    188  1.21  christos 				return usage();
    189   1.1  christos 			break;
    190  1.15   jnemeth 		case 'L':
    191  1.15   jnemeth 			if (xlabel != NULL)
    192  1.21  christos 				return usage();
    193  1.15   jnemeth 			xlabel = (uint8_t *)strdup(optarg);
    194  1.15   jnemeth 			break;
    195   1.1  christos 		case 'l':
    196   1.1  christos 			if (name != NULL)
    197  1.21  christos 				return usage();
    198   1.3  christos 			name = (uint8_t *)strdup(optarg);
    199   1.1  christos 			break;
    200   1.1  christos 		case 's':
    201   1.1  christos 			if (size > 0)
    202  1.21  christos 				return usage();
    203   1.1  christos 			size = strtoll(optarg, &p, 10);
    204   1.1  christos 			if (*p != 0 || size < 1)
    205  1.21  christos 				return usage();
    206   1.1  christos 			break;
    207   1.1  christos 		case 't':
    208  1.18  christos 			if (!gpt_uuid_is_nil(type))
    209  1.21  christos 				return usage();
    210  1.18  christos 			if (gpt_uuid_parse(optarg, type) != 0)
    211  1.21  christos 				return usage();
    212   1.1  christos 			break;
    213   1.1  christos 		default:
    214  1.21  christos 			return usage();
    215   1.1  christos 		}
    216   1.1  christos 	}
    217   1.1  christos 
    218   1.1  christos 	if (!all ^
    219  1.15   jnemeth 	    (block > 0 || entry > 0 || xlabel != NULL || size > 0 ||
    220  1.18  christos 	    !gpt_uuid_is_nil(type)))
    221  1.21  christos 		return usage();
    222   1.1  christos 
    223  1.20  christos 	if (name == NULL || argc != optind)
    224  1.21  christos 		return usage();
    225   1.1  christos 
    226  1.20  christos 	return label(gpt);
    227   1.1  christos }
    228