Annotation of src/sbin/gpt/add.c, Revision 1.24
1.1 christos 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: #include <sys/cdefs.h>
1.2 christos 28: #ifdef __FBSDID
29: __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
30: #endif
31: #ifdef __RCSID
1.24 ! jnemeth 32: __RCSID("$NetBSD: add.c,v 1.23 2013/12/08 09:32:51 jnemeth Exp $");
1.2 christos 33: #endif
1.1 christos 34:
35: #include <sys/types.h>
36:
37: #include <err.h>
38: #include <stddef.h>
39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
42: #include <unistd.h>
1.2 christos 43: #include <inttypes.h>
1.1 christos 44:
45: #include "map.h"
46: #include "gpt.h"
47:
48: static uuid_t type;
1.23 jnemeth 49: static off_t alignment, block, sectors, size;
1.1 christos 50: static unsigned int entry;
1.15 jnemeth 51: static uint8_t *name;
1.1 christos 52:
1.18 jnemeth 53: const char addmsg1[] = "add [-a alignment] [-b blocknr] [-i index] [-l label]";
1.23 jnemeth 54: const char addmsg2[] = " [-s size] [-t type] device ...";
1.5 riz 55:
1.11 joerg 56: __dead static void
1.1 christos 57: usage_add(void)
58: {
59:
60: fprintf(stderr,
1.15 jnemeth 61: "usage: %s %s\n"
62: " %*s %s\n", getprogname(), addmsg1,
63: (int)strlen(getprogname()), "", addmsg2);
1.1 christos 64: exit(1);
65: }
66:
67: static void
68: add(int fd)
69: {
1.12 jakllsch 70: uuid_t uuid;
1.1 christos 71: map_t *gpt, *tpg;
72: map_t *tbl, *lbt;
73: map_t *map;
74: struct gpt_hdr *hdr;
75: struct gpt_ent *ent;
76: unsigned int i;
1.15 jnemeth 77: off_t alignsecs;
78:
1.1 christos 79:
80: gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
1.3 he 81: ent = NULL;
1.1 christos 82: if (gpt == NULL) {
83: warnx("%s: error: no primary GPT header; run create or recover",
84: device_name);
85: return;
86: }
87:
88: tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
89: if (tpg == NULL) {
90: warnx("%s: error: no secondary GPT header; run recover",
91: device_name);
92: return;
93: }
94:
95: tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
96: lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
97: if (tbl == NULL || lbt == NULL) {
98: warnx("%s: error: run recover -- trust me", device_name);
99: return;
100: }
101:
102: hdr = gpt->map_data;
103: if (entry > le32toh(hdr->hdr_entries)) {
104: warnx("%s: error: index %u out of range (%u max)", device_name,
105: entry, le32toh(hdr->hdr_entries));
106: return;
107: }
108:
109: if (entry > 0) {
110: i = entry - 1;
111: ent = (void*)((char*)tbl->map_data + i *
112: le32toh(hdr->hdr_entsz));
1.12 jakllsch 113: le_uuid_dec(ent->ent_type, &uuid);
114: if (!uuid_is_nil(&uuid, NULL)) {
1.1 christos 115: warnx("%s: error: entry at index %u is not free",
116: device_name, entry);
117: return;
118: }
119: } else {
120: /* Find empty slot in GPT table. */
121: for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
122: ent = (void*)((char*)tbl->map_data + i *
123: le32toh(hdr->hdr_entsz));
1.12 jakllsch 124: le_uuid_dec(ent->ent_type, &uuid);
125: if (uuid_is_nil(&uuid, NULL))
1.1 christos 126: break;
127: }
128: if (i == le32toh(hdr->hdr_entries)) {
129: warnx("%s: error: no available table entries",
130: device_name);
131: return;
132: }
133: }
134:
1.15 jnemeth 135: if (alignment > 0) {
136: alignsecs = alignment / secsz;
1.23 jnemeth 137: map = map_alloc(block, sectors, alignsecs);
1.15 jnemeth 138: if (map == NULL) {
1.22 jnemeth 139: warnx("%s: error: not enough space available on "
140: "device for an aligned partition", device_name);
141: return;
1.15 jnemeth 142: }
143: } else {
1.23 jnemeth 144: map = map_alloc(block, sectors, 0);
1.15 jnemeth 145: if (map == NULL) {
1.22 jnemeth 146: warnx("%s: error: not enough space available on "
147: "device", device_name);
1.15 jnemeth 148: return;
149: }
1.1 christos 150: }
151:
1.12 jakllsch 152: le_uuid_enc(ent->ent_type, &type);
1.1 christos 153: ent->ent_lba_start = htole64(map->map_start);
154: ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
1.15 jnemeth 155: if (name != NULL)
156: utf8_to_utf16(name, ent->ent_name, 36);
1.1 christos 157:
158: hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
159: le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
160: hdr->hdr_crc_self = 0;
161: hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
162:
163: gpt_write(fd, gpt);
164: gpt_write(fd, tbl);
165:
166: hdr = tpg->map_data;
167: ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
168:
1.12 jakllsch 169: le_uuid_enc(ent->ent_type, &type);
1.1 christos 170: ent->ent_lba_start = htole64(map->map_start);
171: ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
1.16 jnemeth 172: if (name != NULL)
173: utf8_to_utf16(name, ent->ent_name, 36);
1.1 christos 174:
175: hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
176: le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
177: hdr->hdr_crc_self = 0;
178: hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
179:
180: gpt_write(fd, lbt);
181: gpt_write(fd, tpg);
182:
1.24 ! jnemeth 183: printf("Partition %d added, use:\n", i + 1);
1.10 riz 184: printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
185: " <type>\n", device_arg, map->map_start, map->map_size);
1.2 christos 186: printf("to create a wedge for it\n");
1.1 christos 187: }
188:
189: int
190: cmd_add(int argc, char *argv[])
191: {
192: char *p;
193: int ch, fd;
1.15 jnemeth 194: int64_t human_num;
1.1 christos 195:
1.15 jnemeth 196: while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
1.1 christos 197: switch(ch) {
1.15 jnemeth 198: case 'a':
199: if (alignment > 0)
200: usage_add();
201: if (dehumanize_number(optarg, &human_num) < 0)
202: usage_add();
203: alignment = human_num;
1.21 jnemeth 204: if (alignment < 1)
205: usage_add();
1.15 jnemeth 206: break;
1.1 christos 207: case 'b':
208: if (block > 0)
209: usage_add();
1.19 jnemeth 210: if (dehumanize_number(optarg, &human_num) < 0)
1.1 christos 211: usage_add();
1.19 jnemeth 212: block = human_num;
1.21 jnemeth 213: if (block < 1)
214: usage_add();
1.1 christos 215: break;
216: case 'i':
217: if (entry > 0)
218: usage_add();
1.4 riz 219: entry = strtoul(optarg, &p, 10);
1.1 christos 220: if (*p != 0 || entry < 1)
221: usage_add();
222: break;
1.15 jnemeth 223: case 'l':
224: if (name != NULL)
225: usage_add();
226: name = (uint8_t *)strdup(optarg);
227: break;
1.1 christos 228: case 's':
1.23 jnemeth 229: if (sectors > 0 || size > 0)
1.1 christos 230: usage_add();
1.23 jnemeth 231: sectors = strtoll(optarg, &p, 10);
232: if (sectors < 1)
1.1 christos 233: usage_add();
1.23 jnemeth 234: if (*p == '\0')
235: break;
236: if (*p == 's' || *p == 'S') {
237: if (*(p + 1) == '\0')
238: break;
239: else
240: usage_add();
241: }
242: if (*p == 'b' || *p == 'B') {
243: if (*(p + 1) == '\0') {
244: size = sectors;
245: sectors = 0;
246: break;
247: } else
248: usage_add();
249: }
250: if (dehumanize_number(optarg, &human_num) < 0)
251: usage_add();
252: size = human_num;
253: sectors = 0;
1.1 christos 254: break;
255: case 't':
256: if (!uuid_is_nil(&type, NULL))
257: usage_add();
258: if (parse_uuid(optarg, &type) != 0)
259: usage_add();
260: break;
261: default:
262: usage_add();
263: }
264: }
265:
266: if (argc == optind)
267: usage_add();
268:
1.9 jakllsch 269: /* Create NetBSD FFS partitions by default. */
1.1 christos 270: if (uuid_is_nil(&type, NULL)) {
1.13 jakllsch 271: static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
1.9 jakllsch 272: type = nb_ffs;
1.1 christos 273: }
274:
275: while (optind < argc) {
276: fd = gpt_open(argv[optind++]);
277: if (fd == -1) {
278: warn("unable to open device '%s'", device_name);
279: continue;
280: }
281:
1.15 jnemeth 282: if (alignment % secsz != 0) {
1.23 jnemeth 283: warnx("Alignment must be a multiple of sector size;");
284: warnx("the sector size for %s is %d bytes.",
285: device_name, secsz);
286: continue;
287: }
288:
289: if (size % secsz != 0) {
290: warnx("Size in bytes must be a multiple of sector "
291: "size;");
1.15 jnemeth 292: warnx("the sector size for %s is %d bytes.",
293: device_name, secsz);
294: continue;
295: }
1.23 jnemeth 296: if (size > 0)
297: sectors = size / secsz;
1.15 jnemeth 298:
1.1 christos 299: add(fd);
300:
301: gpt_close(fd);
302: }
303:
304: return (0);
305: }
CVSweb <webmaster@jp.NetBSD.org>