xref: /netbsd-src/sbin/gpt/add.c (revision 808dcc6ff47b701a9f47fad30f4fa823494300ca)
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.45 2024/11/02 12:46:49 kre 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 	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,
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 		default:
188 			if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
189 			    == -1)
190 				goto usage;
191 			break;
192 		}
193 	}
194 
195 	if (argc != optind)
196 		return usage();
197 
198 	/* Create NetBSD FFS partitions by default. */
199 	if (gpt_uuid_is_nil(type))
200 		gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
201 
202 	if (optind != argc)
203 		goto cleanup;
204 
205 	if ((sectors = gpt_check_ais(gpt, alignment, ~0U, size)) == -1)
206 		goto cleanup;
207 
208 	return add(gpt, alignment, block, sectors, size, entry, name, type);
209 usage:
210 	return usage();
211 cleanup:
212 	free(name);
213 	return -1;
214 }
215