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.44 2018/07/03 03:41:23 jnemeth 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
54static int cmd_add(gpt_t, int, char *[]);
55
56static const char *addhelp[] = {
57	"[-a alignment] [-b blocknr] [-i index] [-l label]",
58	"[-s size] [-t type]",
59};
60
61struct 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
70static void
71ent_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
83static int
84add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size,
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 (alignment > 0) {
126		alignsecs = alignment / gpt->secsz;
127		map = map_alloc(gpt, block, sectors, alignsecs);
128		if (map == NULL) {
129			gpt_warnx(gpt, "Not enough space available on "
130			      "device for an aligned partition");
131			return -1;
132		}
133	} else {
134		map = map_alloc(gpt, block, sectors, 0);
135		if (map == NULL) {
136			gpt_warnx(gpt, "Not enough space available on device");
137			return -1;
138		}
139	}
140
141	ent_set(ent, map, type, name);
142	if (gpt_write_primary(gpt) == -1)
143		return -1;
144
145	ent = gpt_ent_backup(gpt, i);
146	ent_set(ent, map, type, name);
147	if (gpt_write_backup(gpt) == -1)
148		return -1;
149
150	gpt_uuid_snprintf(buf, sizeof(buf), "%d", type);
151	gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64, i + 1,
152	    buf, map->map_start, map->map_size);
153	return 0;
154}
155
156static int
157cmd_add(gpt_t gpt, int argc, char *argv[])
158{
159	int ch;
160	off_t alignment = 0, block = 0, sectors = 0, size = 0;
161	unsigned int entry = 0;
162	uint8_t *name = NULL;
163	gpt_uuid_t type;
164
165	gpt_uuid_copy(type, gpt_uuid_nil);
166
167	while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
168		switch(ch) {
169		case 'b':
170			if (gpt_human_get(gpt, &block) == -1)
171				goto usage;
172			break;
173		case 'l':
174			if (gpt_name_get(gpt, &name) == -1)
175				goto usage;
176			break;
177		case 't':
178			if (gpt_uuid_get(gpt, &type) == -1)
179				goto usage;
180			break;
181		default:
182			if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
183			    == -1)
184				goto usage;
185			break;
186		}
187	}
188
189	if (argc != optind)
190		return usage();
191
192	/* Create NetBSD FFS partitions by default. */
193	if (gpt_uuid_is_nil(type))
194		gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
195
196	if (optind != argc)
197		goto cleanup;
198
199	if ((sectors = gpt_check_ais(gpt, alignment, ~0U, size)) == -1)
200		goto cleanup;
201
202	return add(gpt, alignment, block, sectors, size, entry, name, type);
203usage:
204	return usage();
205cleanup:
206	free(name);
207	return -1;
208}
209