xref: /dragonfly/sbin/gpt/add.c (revision bd2b9b6f0a7b343845c3917e37a2a908ff28bab3)
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  * $FreeBSD: src/sbin/gpt/add.c,v 1.15 2006/10/04 18:20:25 marcel Exp $
27  * $DragonFly: src/sbin/gpt/add.c,v 1.6 2008/07/17 01:15:59 dillon Exp $
28  */
29 
30 #include <sys/types.h>
31 
32 #include <err.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "map.h"
40 #include "gpt.h"
41 
42 static uuid_t type;
43 static off_t block, size;
44 static unsigned int entry = NOENTRY;
45 
46 static void
usage_add(void)47 usage_add(void)
48 {
49 
50           fprintf(stderr,
51               "usage: %s [-b lba] [-i index] [-s count] [-t uuid] device ...\n",
52               getprogname());
53           exit(1);
54 }
55 
56 static void
add(int fd)57 add(int fd)
58 {
59           map_t *gpt, *tpg;
60           map_t *tbl, *lbt;
61           map_t *map;
62           struct gpt_hdr *hdr;
63           struct gpt_ent *ent;
64           unsigned int i;
65 
66           gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
67           if (gpt == NULL) {
68                     warnx("%s: error: no primary GPT header; run create or recover",
69                         device_name);
70                     return;
71           }
72 
73           tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
74           if (tpg == NULL) {
75                     warnx("%s: error: no secondary GPT header; run recover",
76                         device_name);
77                     return;
78           }
79 
80           tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
81           lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
82           if (tbl == NULL || lbt == NULL) {
83                     warnx("%s: error: run recover -- trust me", device_name);
84                     return;
85           }
86 
87           hdr = gpt->map_data;
88           if (entry != NOENTRY && entry > le32toh(hdr->hdr_entries)) {
89                     warnx("%s: error: index %u out of range (%u max)", device_name,
90                         entry, le32toh(hdr->hdr_entries));
91                     return;
92           }
93 
94           if (entry != NOENTRY) {
95                     i = entry;
96                     ent = (void*)((char*)tbl->map_data + i *
97                         le32toh(hdr->hdr_entsz));
98                     if (!uuid_is_nil(&ent->ent_type, NULL)) {
99                               warnx("%s: error: entry at index %u is not free",
100                                   device_name, entry);
101                               return;
102                     }
103           } else {
104                     /* Find empty slot in GPT table. */
105                     ent = NULL;
106                     for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
107                               ent = (void*)((char*)tbl->map_data + i *
108                                   le32toh(hdr->hdr_entsz));
109                               if (uuid_is_nil(&ent->ent_type, NULL))
110                                         break;
111                     }
112                     if (i == le32toh(hdr->hdr_entries)) {
113                               warnx("%s: error: no available table entries",
114                                   device_name);
115                               return;
116                     }
117           }
118 
119           map = map_alloc(block, size);
120           if (map == NULL) {
121                     warnx("%s: error: no space available on device", device_name);
122                     return;
123           }
124 
125           le_uuid_enc(&ent->ent_type, &type);
126           ent->ent_lba_start = htole64(map->map_start);
127           ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
128 
129           hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
130               le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
131           hdr->hdr_crc_self = 0;
132           hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
133 
134           gpt_write(fd, gpt);
135           gpt_write(fd, tbl);
136 
137           hdr = tpg->map_data;
138           ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
139 
140           le_uuid_enc(&ent->ent_type, &type);
141           ent->ent_lba_start = htole64(map->map_start);
142           ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
143 
144           hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
145               le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
146           hdr->hdr_crc_self = 0;
147           hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
148 
149           gpt_write(fd, lbt);
150           gpt_write(fd, tpg);
151 
152           printf("%ss%u added\n", device_name, i);
153 }
154 
155 void
add_defaults(int fd)156 add_defaults(int fd)
157 {
158           entry = 0;
159           size = 524288;
160           if (parse_uuid("EFI System", &type) != 0) {
161                     fprintf(stderr, "Unable to lookup uuid 'EFI System'\n");
162                     exit(1);
163           }
164           add(fd);
165 
166           entry = 1;
167           size = 0;
168           if (parse_uuid("DragonFly Label64", &type) != 0) {
169                     fprintf(stderr, "Unable to lookup uuid 'DragonFly Label64'\n");
170                     exit(1);
171           }
172           add(fd);
173 }
174 
175 int
cmd_add(int argc,char * argv[])176 cmd_add(int argc, char *argv[])
177 {
178           char *p;
179           int ch, fd;
180 
181           /* Get the migrate options */
182           while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
183                     switch(ch) {
184                     case 'b':
185                               if (block > 0)
186                                         usage_add();
187                               block = strtoll(optarg, &p, 10);
188                               if (*p != 0 || block < 1)
189                                         usage_add();
190                               break;
191                     case 'i':
192                               if (entry != NOENTRY)
193                                         usage_add();
194                               entry = strtoul(optarg, &p, 10);
195                               if (*p != 0 || entry == NOENTRY)
196                                         usage_add();
197                               break;
198                     case 's':
199                               if (size > 0)
200                                         usage_add();
201                               size = strtoll(optarg, &p, 10);
202                               if (*p != 0 || size < 1)
203                                         usage_add();
204                               break;
205                     case 't':
206                               if (!uuid_is_nil(&type, NULL))
207                                         usage_add();
208                               if (parse_uuid(optarg, &type) != 0)
209                                         usage_add();
210                               break;
211                     default:
212                               usage_add();
213                     }
214           }
215 
216           if (argc == optind)
217                     usage_add();
218 
219           /* Create DragonFly 64 bit label partitions by default. */
220           if (uuid_is_nil(&type, NULL)) {
221                     uint32_t status;
222 
223                     uuid_name_lookup(&type, "DragonFly Label64", &status);
224                     if (status != uuid_s_ok)
225                               err(1, "unable to find uuid for 'DragonFly Label64'");
226           }
227 
228           while (optind < argc) {
229                     fd = gpt_open(argv[optind++]);
230                     if (fd == -1) {
231                               warn("unable to open device '%s'", device_name);
232                               continue;
233                     }
234 
235                     add(fd);
236 
237                     gpt_close(fd);
238           }
239 
240           return (0);
241 }
242