Home | History | Annotate | Line # | Download | only in gpt
      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.46 2025/02/23 20:47:19 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 const struct gpt_cmd c_add = {
     62 	"add",
     63 	cmd_add,
     64 	addhelp, __arraycount(addhelp),
     65 	GPT_SYNC,
     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((uint64_t)map->map_start);
     76 	ent->ent_lba_end = htole64((uint64_t)(map->map_start +
     77 	    map->map_size - 1LL));
     78 	if (xname == NULL)
     79 		return;
     80 	utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
     81 }
     82 
     83 static int
     84 add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size __unused,
     85     u_int entry, uint8_t *name, gpt_uuid_t type)
     86 {
     87 	map_t map;
     88 	struct gpt_hdr *hdr;
     89 	struct gpt_ent *ent;
     90 	unsigned int i;
     91 	off_t alignsecs;
     92 	char buf[128];
     93 
     94 	if ((hdr = gpt_hdr(gpt)) == NULL)
     95 		return -1;
     96 
     97 	ent = NULL;
     98 
     99 	if (entry > le32toh(hdr->hdr_entries)) {
    100 		gpt_warnx(gpt, "index %u out of range (%u max)",
    101 		    entry, le32toh(hdr->hdr_entries));
    102 		return -1;
    103 	}
    104 
    105 	if (entry > 0) {
    106 		i = entry - 1;
    107 		ent = gpt_ent_primary(gpt, i);
    108 		if (!gpt_uuid_is_nil(ent->ent_type)) {
    109 			gpt_warnx(gpt, "Entry at index %u is not free", entry);
    110 			return -1;
    111 		}
    112 	} else {
    113 		/* Find empty slot in GPT table. */
    114 		for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    115 			ent = gpt_ent_primary(gpt, i);
    116 			if (gpt_uuid_is_nil(ent->ent_type))
    117 				break;
    118 		}
    119 		if (i == le32toh(hdr->hdr_entries)) {
    120 			gpt_warnx(gpt, "No available table entries");
    121 			return -1;
    122 		}
    123 	}
    124 
    125 	if (gpt_uuid_is_nil(ent->ent_guid))
    126 		if (gpt_uuid_generate(gpt, ent->ent_guid) == -1) {
    127 			gpt_warnx(gpt, "Unable to make UUID");
    128 			return -1;
    129 		}
    130 
    131 	if (alignment > 0) {
    132 		alignsecs = alignment / gpt->secsz;
    133 		map = map_alloc(gpt, block, sectors, alignsecs);
    134 		if (map == NULL) {
    135 			gpt_warnx(gpt, "Not enough space available on "
    136 			      "device for an aligned partition");
    137 			return -1;
    138 		}
    139 	} else {
    140 		map = map_alloc(gpt, block, sectors, 0);
    141 		if (map == NULL) {
    142 			gpt_warnx(gpt, "Not enough space available on device");
    143 			return -1;
    144 		}
    145 	}
    146 
    147 	ent_set(ent, map, type, name);
    148 	if (gpt_write_primary(gpt) == -1)
    149 		return -1;
    150 
    151 	ent = gpt_ent_backup(gpt, i);
    152 	ent_set(ent, map, type, name);
    153 	if (gpt_write_backup(gpt) == -1)
    154 		return -1;
    155 
    156 	gpt_uuid_snprintf(buf, sizeof(buf), "%d", type);
    157 	gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64, i + 1,
    158 	    buf, map->map_start, map->map_size);
    159 	return 0;
    160 }
    161 
    162 static int
    163 cmd_add(gpt_t gpt, int argc, char *argv[])
    164 {
    165 	int ch;
    166 	off_t alignment = 0, block = 0, sectors = 0, size = 0;
    167 	unsigned int entry = 0;
    168 	uint8_t *name = NULL;
    169 	gpt_uuid_t type;
    170 
    171 	gpt_uuid_copy(type, gpt_uuid_nil);
    172 
    173 	while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
    174 		switch(ch) {
    175 		case 'b':
    176 			if (gpt_human_get(gpt, &block) == -1)
    177 				goto usage;
    178 			break;
    179 		case 'l':
    180 			if (gpt_name_get(gpt, &name) == -1)
    181 				goto usage;
    182 			break;
    183 		case 't':
    184 			if (gpt_uuid_get(gpt, &type) == -1)
    185 				goto usage;
    186 			break;
    187 		case 'a':
    188 		case 's':
    189 		case 'i':
    190 			if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
    191 			    == -1)
    192 				goto usage;
    193 			break;
    194 		default:
    195 			goto usage;
    196 		}
    197 	}
    198 
    199 	if (argc != optind)
    200 		return usage();
    201 
    202 	/* Create NetBSD FFS partitions by default. */
    203 	if (gpt_uuid_is_nil(type))
    204 		gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
    205 
    206 	if (optind != argc)
    207 		goto cleanup;
    208 
    209 	if ((sectors = gpt_check_ais(gpt, alignment, ~0U, size)) == -1)
    210 		goto cleanup;
    211 
    212 	return add(gpt, alignment, block, sectors, size, entry, name, type);
    213 usage:
    214 	return usage();
    215 cleanup:
    216 	free(name);
    217 	return -1;
    218 }
    219