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