]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/gpt/add.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / gpt / add.c
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>
28 __FBSDID("$FreeBSD$");
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 add_type;
43 static off_t add_block, add_size;
44 static unsigned int add_entry;
45
46 static void
47 usage_add(void)
48 {
49
50         fprintf(stderr,
51             "usage: %s [-b lba] [-i index] [-s lba] [-t uuid] device ...\n",
52             getprogname());
53         exit(1);
54 }
55
56 map_t *
57 gpt_add_part(int fd, uuid_t type, off_t start, off_t size, unsigned int *entry)
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 (NULL);
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 (NULL);
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 (NULL);
85         }
86
87         hdr = gpt->map_data;
88         if (*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 (NULL);
92         }
93
94         if (*entry > 0) {
95                 i = *entry - 1;
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 (NULL);
102                 }
103         } else {
104                 /* Find empty slot in GPT table. */
105                 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
106                         ent = (void*)((char*)tbl->map_data + i *
107                             le32toh(hdr->hdr_entsz));
108                         if (uuid_is_nil(&ent->ent_type, NULL))
109                                 break;
110                 }
111                 if (i == le32toh(hdr->hdr_entries)) {
112                         warnx("%s: error: no available table entries",
113                             device_name);
114                         return (NULL);
115                 }
116         }
117
118         map = map_alloc(start, size);
119         if (map == NULL) {
120                 warnx("%s: error: no space available on device", device_name);
121                 return (NULL);
122         }
123
124         le_uuid_enc(&ent->ent_type, &type);
125         ent->ent_lba_start = htole64(map->map_start);
126         ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
127
128         hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
129             le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
130         hdr->hdr_crc_self = 0;
131         hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
132
133         gpt_write(fd, gpt);
134         gpt_write(fd, tbl);
135
136         hdr = tpg->map_data;
137         ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
138
139         le_uuid_enc(&ent->ent_type, &type);
140         ent->ent_lba_start = htole64(map->map_start);
141         ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
142
143         hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
144             le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
145         hdr->hdr_crc_self = 0;
146         hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
147
148         gpt_write(fd, lbt);
149         gpt_write(fd, tpg);
150
151         *entry = i + 1;
152
153         return (map);
154 }
155
156 static void
157 add(int fd)
158 {
159
160         if (gpt_add_part(fd, add_type, add_block, add_size, &add_entry) != 0)
161                 return;
162
163         printf("%sp%u added\n", device_name, add_entry);
164 }
165
166 int
167 cmd_add(int argc, char *argv[])
168 {
169         char *p;
170         int ch, fd;
171
172         /* Get the migrate options */
173         while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
174                 switch(ch) {
175                 case 'b':
176                         if (add_block > 0)
177                                 usage_add();
178                         add_block = strtoll(optarg, &p, 10);
179                         if (*p != 0 || add_block < 1)
180                                 usage_add();
181                         break;
182                 case 'i':
183                         if (add_entry > 0)
184                                 usage_add();
185                         add_entry = strtol(optarg, &p, 10);
186                         if (*p != 0 || add_entry < 1)
187                                 usage_add();
188                         break;
189                 case 's':
190                         if (add_size > 0)
191                                 usage_add();
192                         add_size = strtoll(optarg, &p, 10);
193                         if (*p != 0 || add_size < 1)
194                                 usage_add();
195                         break;
196                 case 't':
197                         if (!uuid_is_nil(&add_type, NULL))
198                                 usage_add();
199                         if (parse_uuid(optarg, &add_type) != 0)
200                                 usage_add();
201                         break;
202                 default:
203                         usage_add();
204                 }
205         }
206
207         if (argc == optind)
208                 usage_add();
209
210         /* Create UFS partitions by default. */
211         if (uuid_is_nil(&add_type, NULL)) {
212                 uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
213                 add_type = ufs;
214         }
215
216         while (optind < argc) {
217                 fd = gpt_open(argv[optind++]);
218                 if (fd == -1) {
219                         warn("unable to open device '%s'", device_name);
220                         continue;
221                 }
222
223                 add(fd);
224
225                 gpt_close(fd);
226         }
227
228         return (0);
229 }