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