Home | History | Annotate | Line # | Download | only in gpt
add.c revision 1.32
      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.32  christos __RCSID("$NetBSD: add.c,v 1.32 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.31  christos #include <sys/param.h>
     41  1.31  christos #include <sys/stat.h>
     42   1.1  christos 
     43   1.1  christos #include <err.h>
     44   1.1  christos #include <stddef.h>
     45   1.1  christos #include <stdio.h>
     46   1.1  christos #include <stdlib.h>
     47   1.1  christos #include <string.h>
     48   1.1  christos #include <unistd.h>
     49   1.1  christos 
     50   1.1  christos #include "map.h"
     51   1.1  christos #include "gpt.h"
     52  1.31  christos #include "gpt_private.h"
     53   1.1  christos 
     54  1.27  christos static gpt_uuid_t type;
     55  1.23   jnemeth static off_t alignment, block, sectors, size;
     56   1.1  christos static unsigned int entry;
     57  1.15   jnemeth static uint8_t *name;
     58  1.32  christos static int cmd_add(gpt_t, int, char *[]);
     59   1.1  christos 
     60  1.32  christos static const char *addhelp[] = {
     61  1.32  christos     "[-a alignment] [-b blocknr] [-i index] [-l label]",
     62  1.32  christos     "[-s size] [-t type]",
     63  1.32  christos };
     64  1.32  christos 
     65  1.32  christos struct gpt_cmd c_add = {
     66  1.32  christos 	"add",
     67  1.32  christos 	cmd_add,
     68  1.32  christos 	addhelp, __arraycount(addhelp),
     69  1.32  christos 	0,
     70  1.32  christos };
     71   1.5       riz 
     72  1.32  christos #define usage() gpt_usage(NULL, &c_add)
     73   1.1  christos 
     74  1.31  christos static int
     75  1.31  christos add(gpt_t gpt)
     76   1.1  christos {
     77  1.31  christos 	map_t map;
     78   1.1  christos 	struct gpt_hdr *hdr;
     79  1.31  christos 	struct gpt_ent *ent, e;
     80   1.1  christos 	unsigned int i;
     81  1.15   jnemeth 	off_t alignsecs;
     82  1.15   jnemeth 
     83  1.31  christos 	if ((hdr = gpt_hdr(gpt)) == NULL)
     84  1.31  christos 		return -1;
     85   1.1  christos 
     86   1.3        he 	ent = NULL;
     87   1.1  christos 
     88   1.1  christos 	if (entry > le32toh(hdr->hdr_entries)) {
     89  1.31  christos 		gpt_warnx(gpt, "index %u out of range (%u max)",
     90   1.1  christos 		    entry, le32toh(hdr->hdr_entries));
     91  1.31  christos 		return -1;
     92   1.1  christos 	}
     93   1.1  christos 
     94   1.1  christos 	if (entry > 0) {
     95   1.1  christos 		i = entry - 1;
     96  1.31  christos 		ent = gpt_ent_primary(gpt, i);
     97  1.27  christos 		if (!gpt_uuid_is_nil(ent->ent_type)) {
     98  1.31  christos 			gpt_warnx(gpt, "Entry at index %u is not free", entry);
     99  1.31  christos 			return -1;
    100   1.1  christos 		}
    101   1.1  christos 	} else {
    102   1.1  christos 		/* Find empty slot in GPT table. */
    103   1.1  christos 		for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    104  1.31  christos 			ent = gpt_ent_primary(gpt, i);
    105  1.27  christos 			if (gpt_uuid_is_nil(ent->ent_type))
    106   1.1  christos 				break;
    107   1.1  christos 		}
    108   1.1  christos 		if (i == le32toh(hdr->hdr_entries)) {
    109  1.31  christos 			gpt_warnx(gpt, "No available table entries");
    110  1.31  christos 			return -1;
    111   1.1  christos 		}
    112   1.1  christos 	}
    113   1.1  christos 
    114  1.15   jnemeth 	if (alignment > 0) {
    115  1.31  christos 		alignsecs = alignment / gpt->secsz;
    116  1.31  christos 		map = map_alloc(gpt, block, sectors, alignsecs);
    117  1.15   jnemeth 		if (map == NULL) {
    118  1.31  christos 			gpt_warnx(gpt, "Not enough space available on "
    119  1.31  christos 			      "device for an aligned partition");
    120  1.31  christos 			return -1;
    121  1.15   jnemeth 		}
    122  1.15   jnemeth 	} else {
    123  1.31  christos 		map = map_alloc(gpt, block, sectors, 0);
    124  1.15   jnemeth 		if (map == NULL) {
    125  1.31  christos 			gpt_warnx(gpt, "Not enough space available on device");
    126  1.31  christos 			return -1;
    127  1.15   jnemeth 		}
    128   1.1  christos 	}
    129   1.1  christos 
    130  1.31  christos 	memset(&e, 0, sizeof(e));
    131  1.31  christos 	gpt_uuid_copy(e.ent_type, type);
    132  1.31  christos 	e.ent_lba_start = htole64(map->map_start);
    133  1.31  christos 	e.ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
    134  1.16   jnemeth 	if (name != NULL)
    135  1.31  christos 		utf8_to_utf16(name, e.ent_name, 36);
    136   1.1  christos 
    137  1.31  christos 	memcpy(ent, &e, sizeof(e));
    138  1.31  christos 	gpt_write_primary(gpt);
    139   1.1  christos 
    140  1.31  christos 	ent = gpt_ent_backup(gpt, i);
    141  1.31  christos 	memcpy(ent, &e, sizeof(e));
    142  1.31  christos 	gpt_write_backup(gpt);
    143   1.1  christos 
    144  1.31  christos 	gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64 "\n", i + 1,
    145  1.30  christos 	    type, map->map_start, map->map_size);
    146  1.31  christos 	return 0;
    147   1.1  christos }
    148   1.1  christos 
    149  1.32  christos static int
    150  1.31  christos cmd_add(gpt_t gpt, int argc, char *argv[])
    151   1.1  christos {
    152   1.1  christos 	char *p;
    153  1.31  christos 	int ch;
    154  1.15   jnemeth 	int64_t human_num;
    155   1.1  christos 
    156  1.15   jnemeth 	while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
    157   1.1  christos 		switch(ch) {
    158  1.15   jnemeth 		case 'a':
    159  1.15   jnemeth 			if (alignment > 0)
    160  1.32  christos 				return usage();
    161  1.15   jnemeth 			if (dehumanize_number(optarg, &human_num) < 0)
    162  1.32  christos 				return usage();
    163  1.15   jnemeth 			alignment = human_num;
    164  1.21   jnemeth 			if (alignment < 1)
    165  1.32  christos 				return usage();
    166  1.15   jnemeth 			break;
    167   1.1  christos 		case 'b':
    168   1.1  christos 			if (block > 0)
    169  1.32  christos 				return usage();
    170  1.19   jnemeth 			if (dehumanize_number(optarg, &human_num) < 0)
    171  1.32  christos 				return usage();
    172  1.19   jnemeth 			block = human_num;
    173  1.21   jnemeth 			if (block < 1)
    174  1.32  christos 				return usage();
    175   1.1  christos 			break;
    176   1.1  christos 		case 'i':
    177   1.1  christos 			if (entry > 0)
    178  1.32  christos 				usage();
    179   1.4       riz 			entry = strtoul(optarg, &p, 10);
    180   1.1  christos 			if (*p != 0 || entry < 1)
    181  1.32  christos 				return usage();
    182   1.1  christos 			break;
    183  1.15   jnemeth 		case 'l':
    184  1.15   jnemeth 			if (name != NULL)
    185  1.32  christos 				return usage();
    186  1.15   jnemeth 			name = (uint8_t *)strdup(optarg);
    187  1.15   jnemeth 			break;
    188   1.1  christos 		case 's':
    189  1.23   jnemeth 			if (sectors > 0 || size > 0)
    190  1.32  christos 				return usage();
    191  1.23   jnemeth 			sectors = strtoll(optarg, &p, 10);
    192  1.23   jnemeth 			if (sectors < 1)
    193  1.32  christos 				return usage();
    194  1.23   jnemeth 			if (*p == '\0')
    195  1.23   jnemeth 				break;
    196  1.23   jnemeth 			if (*p == 's' || *p == 'S') {
    197  1.23   jnemeth 				if (*(p + 1) == '\0')
    198  1.23   jnemeth 					break;
    199  1.23   jnemeth 				else
    200  1.32  christos 					return usage();
    201  1.23   jnemeth 			}
    202  1.23   jnemeth 			if (*p == 'b' || *p == 'B') {
    203  1.23   jnemeth 				if (*(p + 1) == '\0') {
    204  1.23   jnemeth 					size = sectors;
    205  1.23   jnemeth 					sectors = 0;
    206  1.23   jnemeth 					break;
    207  1.23   jnemeth 				} else
    208  1.32  christos 					return usage();
    209  1.23   jnemeth 			}
    210  1.23   jnemeth 			if (dehumanize_number(optarg, &human_num) < 0)
    211  1.32  christos 				return usage();
    212  1.23   jnemeth 			size = human_num;
    213  1.23   jnemeth 			sectors = 0;
    214   1.1  christos 			break;
    215   1.1  christos 		case 't':
    216  1.27  christos 			if (!gpt_uuid_is_nil(type))
    217  1.32  christos 				return usage();
    218  1.27  christos 			if (gpt_uuid_parse(optarg, type) != 0)
    219  1.32  christos 				return usage();
    220   1.1  christos 			break;
    221   1.1  christos 		default:
    222  1.32  christos 			return usage();
    223   1.1  christos 		}
    224   1.1  christos 	}
    225   1.1  christos 
    226   1.1  christos 	if (argc == optind)
    227  1.32  christos 		return usage();
    228   1.1  christos 
    229   1.9  jakllsch 	/* Create NetBSD FFS partitions by default. */
    230  1.27  christos 	if (gpt_uuid_is_nil(type)) {
    231  1.27  christos 		gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
    232   1.1  christos 	}
    233   1.1  christos 
    234  1.31  christos 	if (optind != argc)
    235  1.32  christos 		return usage();
    236   1.1  christos 
    237  1.31  christos 	if ((sectors = gpt_check(gpt, alignment, size)) == -1)
    238  1.31  christos 		return -1;
    239   1.1  christos 
    240  1.31  christos 	return add(gpt);
    241   1.1  christos }
    242