Home | History | Annotate | Line # | Download | only in gpt
add.c revision 1.25
      1 /*-
      2  * Copyright (c) 2002 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/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: add.c,v 1.25 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 #include <inttypes.h>
     48 
     49 #include "map.h"
     50 #include "gpt.h"
     51 
     52 static uuid_t type;
     53 static off_t alignment, block, sectors, size;
     54 static unsigned int entry;
     55 static uint8_t *name;
     56 
     57 const char addmsg1[] = "add [-a alignment] [-b blocknr] [-i index] [-l label]";
     58 const char addmsg2[] = "    [-s size] [-t type] device ...";
     59 
     60 __dead static void
     61 usage_add(void)
     62 {
     63 
     64 	fprintf(stderr,
     65 	    "usage: %s %s\n"
     66 	    "       %*s %s\n", getprogname(), addmsg1,
     67 	    (int)strlen(getprogname()), "", addmsg2);
     68 	exit(1);
     69 }
     70 
     71 static void
     72 add(int fd)
     73 {
     74 	uuid_t uuid;
     75 	map_t *gpt, *tpg;
     76 	map_t *tbl, *lbt;
     77 	map_t *map;
     78 	struct gpt_hdr *hdr;
     79 	struct gpt_ent *ent;
     80 	unsigned int i;
     81 	off_t alignsecs;
     82 
     83 
     84 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     85 	ent = NULL;
     86 	if (gpt == NULL) {
     87 		warnx("%s: error: no primary GPT header; run create or recover",
     88 		    device_name);
     89 		return;
     90 	}
     91 
     92 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     93 	if (tpg == NULL) {
     94 		warnx("%s: error: no secondary GPT header; run recover",
     95 		    device_name);
     96 		return;
     97 	}
     98 
     99 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
    100 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
    101 	if (tbl == NULL || lbt == NULL) {
    102 		warnx("%s: error: run recover -- trust me", device_name);
    103 		return;
    104 	}
    105 
    106 	hdr = gpt->map_data;
    107 	if (entry > le32toh(hdr->hdr_entries)) {
    108 		warnx("%s: error: index %u out of range (%u max)", device_name,
    109 		    entry, le32toh(hdr->hdr_entries));
    110 		return;
    111 	}
    112 
    113 	if (entry > 0) {
    114 		i = entry - 1;
    115 		ent = (void*)((char*)tbl->map_data + i *
    116 		    le32toh(hdr->hdr_entsz));
    117 		le_uuid_dec(ent->ent_type, &uuid);
    118 		if (!uuid_is_nil(&uuid, NULL)) {
    119 			warnx("%s: error: entry at index %u is not free",
    120 			    device_name, entry);
    121 			return;
    122 		}
    123 	} else {
    124 		/* Find empty slot in GPT table. */
    125 		for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    126 			ent = (void*)((char*)tbl->map_data + i *
    127 			    le32toh(hdr->hdr_entsz));
    128 			le_uuid_dec(ent->ent_type, &uuid);
    129 			if (uuid_is_nil(&uuid, NULL))
    130 				break;
    131 		}
    132 		if (i == le32toh(hdr->hdr_entries)) {
    133 			warnx("%s: error: no available table entries",
    134 			    device_name);
    135 			return;
    136 		}
    137 	}
    138 
    139 	if (alignment > 0) {
    140 		alignsecs = alignment / secsz;
    141 		map = map_alloc(block, sectors, alignsecs);
    142 		if (map == NULL) {
    143 			warnx("%s: error: not enough space available on "
    144 			      "device for an aligned partition", device_name);
    145 			return;
    146 		}
    147 	} else {
    148 		map = map_alloc(block, sectors, 0);
    149 		if (map == NULL) {
    150 			warnx("%s: error: not enough space available on "
    151 			      "device", device_name);
    152 			return;
    153 		}
    154 	}
    155 
    156 	le_uuid_enc(ent->ent_type, &type);
    157 	ent->ent_lba_start = htole64(map->map_start);
    158 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
    159 	if (name != NULL)
    160 		utf8_to_utf16(name, ent->ent_name, 36);
    161 
    162 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    163 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    164 	hdr->hdr_crc_self = 0;
    165 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    166 
    167 	gpt_write(fd, gpt);
    168 	gpt_write(fd, tbl);
    169 
    170 	hdr = tpg->map_data;
    171 	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
    172 
    173 	le_uuid_enc(ent->ent_type, &type);
    174 	ent->ent_lba_start = htole64(map->map_start);
    175 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
    176 	if (name != NULL)
    177 		utf8_to_utf16(name, ent->ent_name, 36);
    178 
    179 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    180 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    181 	hdr->hdr_crc_self = 0;
    182 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    183 
    184 	gpt_write(fd, lbt);
    185 	gpt_write(fd, tpg);
    186 
    187 	printf("Partition %d added, use:\n", i + 1);
    188 	printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
    189 	    " <type>\n", device_arg, map->map_start, map->map_size);
    190 	printf("to create a wedge for it\n");
    191 }
    192 
    193 int
    194 cmd_add(int argc, char *argv[])
    195 {
    196 	char *p;
    197 	int ch, fd;
    198 	int64_t human_num;
    199 
    200 	while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
    201 		switch(ch) {
    202 		case 'a':
    203 			if (alignment > 0)
    204 				usage_add();
    205 			if (dehumanize_number(optarg, &human_num) < 0)
    206 				usage_add();
    207 			alignment = human_num;
    208 			if (alignment < 1)
    209 				usage_add();
    210 			break;
    211 		case 'b':
    212 			if (block > 0)
    213 				usage_add();
    214 			if (dehumanize_number(optarg, &human_num) < 0)
    215 				usage_add();
    216 			block = human_num;
    217 			if (block < 1)
    218 				usage_add();
    219 			break;
    220 		case 'i':
    221 			if (entry > 0)
    222 				usage_add();
    223 			entry = strtoul(optarg, &p, 10);
    224 			if (*p != 0 || entry < 1)
    225 				usage_add();
    226 			break;
    227 		case 'l':
    228 			if (name != NULL)
    229 				usage_add();
    230 			name = (uint8_t *)strdup(optarg);
    231 			break;
    232 		case 's':
    233 			if (sectors > 0 || size > 0)
    234 				usage_add();
    235 			sectors = strtoll(optarg, &p, 10);
    236 			if (sectors < 1)
    237 				usage_add();
    238 			if (*p == '\0')
    239 				break;
    240 			if (*p == 's' || *p == 'S') {
    241 				if (*(p + 1) == '\0')
    242 					break;
    243 				else
    244 					usage_add();
    245 			}
    246 			if (*p == 'b' || *p == 'B') {
    247 				if (*(p + 1) == '\0') {
    248 					size = sectors;
    249 					sectors = 0;
    250 					break;
    251 				} else
    252 					usage_add();
    253 			}
    254 			if (dehumanize_number(optarg, &human_num) < 0)
    255 				usage_add();
    256 			size = human_num;
    257 			sectors = 0;
    258 			break;
    259 		case 't':
    260 			if (!uuid_is_nil(&type, NULL))
    261 				usage_add();
    262 			if (parse_uuid(optarg, &type) != 0)
    263 				usage_add();
    264 			break;
    265 		default:
    266 			usage_add();
    267 		}
    268 	}
    269 
    270 	if (argc == optind)
    271 		usage_add();
    272 
    273 	/* Create NetBSD FFS partitions by default. */
    274 	if (uuid_is_nil(&type, NULL)) {
    275 		static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
    276 		type = nb_ffs;
    277 	}
    278 
    279 	while (optind < argc) {
    280 		fd = gpt_open(argv[optind++]);
    281 		if (fd == -1) {
    282 			warn("unable to open device '%s'", device_name);
    283 			continue;
    284 		}
    285 
    286 		if (alignment % secsz != 0) {
    287 			warnx("Alignment must be a multiple of sector size;");
    288 			warnx("the sector size for %s is %d bytes.",
    289 			    device_name, secsz);
    290 			continue;
    291 		}
    292 
    293 		if (size % secsz != 0) {
    294 			warnx("Size in bytes must be a multiple of sector "
    295 			      "size;");
    296 			warnx("the sector size for %s is %d bytes.",
    297 			    device_name, secsz);
    298 			continue;
    299 		}
    300 		if (size > 0)
    301 			sectors = size / secsz;
    302 
    303 		add(fd);
    304 
    305 		gpt_close(fd);
    306 	}
    307 
    308 	return (0);
    309 }
    310