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