Home | History | Annotate | Line # | Download | only in gpt
add.c revision 1.38
      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.38 2015/12/03 01:07:28 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/stat.h>
     42 
     43 #include <err.h>
     44 #include <stddef.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 
     50 #include "map.h"
     51 #include "gpt.h"
     52 #include "gpt_private.h"
     53 
     54 static int cmd_add(gpt_t, int, char *[]);
     55 
     56 static const char *addhelp[] = {
     57 	"[-a alignment] [-b blocknr] [-i index] [-l label]",
     58 	"[-s size] [-t type]",
     59 };
     60 
     61 struct gpt_cmd c_add = {
     62 	"add",
     63 	cmd_add,
     64 	addhelp, __arraycount(addhelp),
     65 	0,
     66 };
     67 
     68 #define usage() gpt_usage(NULL, &c_add)
     69 
     70 static void
     71 ent_set(struct gpt_ent *ent, const map_t map, const gpt_uuid_t xtype,
     72     const uint8_t *xname)
     73 {
     74 	gpt_uuid_copy(ent->ent_type, xtype);
     75 	ent->ent_lba_start = htole64(map->map_start);
     76 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
     77 	if (xname == NULL)
     78 		return;
     79 	utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
     80 }
     81 
     82 static int
     83 add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size,
     84     u_int entry, uint8_t *name, gpt_uuid_t type)
     85 {
     86 	map_t map;
     87 	struct gpt_hdr *hdr;
     88 	struct gpt_ent *ent;
     89 	unsigned int i;
     90 	off_t alignsecs;
     91 	char buf[128];
     92 
     93 	if ((hdr = gpt_hdr(gpt)) == NULL)
     94 		return -1;
     95 
     96 	ent = NULL;
     97 
     98 	if (entry > le32toh(hdr->hdr_entries)) {
     99 		gpt_warnx(gpt, "index %u out of range (%u max)",
    100 		    entry, le32toh(hdr->hdr_entries));
    101 		return -1;
    102 	}
    103 
    104 	if (entry > 0) {
    105 		i = entry - 1;
    106 		ent = gpt_ent_primary(gpt, i);
    107 		if (!gpt_uuid_is_nil(ent->ent_type)) {
    108 			gpt_warnx(gpt, "Entry at index %u is not free", entry);
    109 			return -1;
    110 		}
    111 	} else {
    112 		/* Find empty slot in GPT table. */
    113 		for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    114 			ent = gpt_ent_primary(gpt, i);
    115 			if (gpt_uuid_is_nil(ent->ent_type))
    116 				break;
    117 		}
    118 		if (i == le32toh(hdr->hdr_entries)) {
    119 			gpt_warnx(gpt, "No available table entries");
    120 			return -1;
    121 		}
    122 	}
    123 
    124 	if (alignment > 0) {
    125 		alignsecs = alignment / gpt->secsz;
    126 		map = map_alloc(gpt, block, sectors, alignsecs);
    127 		if (map == NULL) {
    128 			gpt_warnx(gpt, "Not enough space available on "
    129 			      "device for an aligned partition");
    130 			return -1;
    131 		}
    132 	} else {
    133 		map = map_alloc(gpt, block, sectors, 0);
    134 		if (map == NULL) {
    135 			gpt_warnx(gpt, "Not enough space available on device");
    136 			return -1;
    137 		}
    138 	}
    139 
    140 	ent_set(ent, map, type, name);
    141 	gpt_write_primary(gpt);
    142 
    143 	ent = gpt_ent_backup(gpt, i);
    144 	ent_set(ent, map, type, name);
    145 	gpt_write_backup(gpt);
    146 
    147 	gpt_uuid_snprintf(buf, sizeof(buf), "%d", type);
    148 	gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64, i + 1,
    149 	    buf, map->map_start, map->map_size);
    150 	return 0;
    151 }
    152 
    153 static int
    154 cmd_add(gpt_t gpt, int argc, char *argv[])
    155 {
    156 	int ch;
    157 	off_t alignment = 0, block = 0, sectors = 0, size = 0;
    158 	unsigned int entry = 0;
    159 	uint8_t *name = NULL;
    160 	gpt_uuid_t type;
    161 
    162 	gpt_uuid_copy(type, gpt_uuid_nil);
    163 
    164 	while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
    165 		switch(ch) {
    166 		case 'b':
    167 			if (gpt_human_get(&block) == -1)
    168 				return usage();
    169 			break;
    170 		case 'l':
    171 			if (gpt_name_get(gpt, &name) == -1)
    172 				return usage();
    173 			break;
    174 		case 't':
    175 			if (gpt_uuid_get(gpt, &type) == -1)
    176 				return usage();
    177 			break;
    178 		default:
    179 			if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
    180 			    == -1)
    181 				return usage();
    182 			break;
    183 		}
    184 	}
    185 
    186 	if (argc != optind)
    187 		return usage();
    188 
    189 	/* Create NetBSD FFS partitions by default. */
    190 	if (gpt_uuid_is_nil(type))
    191 		gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
    192 
    193 	if (optind != argc)
    194 		return usage();
    195 
    196 	if ((sectors = gpt_check_ais(gpt, alignment, ~0, size)) == -1)
    197 		return -1;
    198 
    199 	return add(gpt, alignment, block, sectors, size, entry, name, type);
    200 }
    201