2 * Copyright (c) 2002 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
35 #include <sys/types.h>
49 static off_t block
, size
;
50 static unsigned int entry
;
52 const char addmsg
[] = "add [-b lba] [-i index] [-s lba] [-t type] "
61 getprogname(), addmsg
);
75 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
78 warnx("%s: error: no primary GPT header; run create or recover",
83 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
85 warnx("%s: error: no secondary GPT header; run recover",
90 tbl
= map_find(MAP_TYPE_PRI_GPT_TBL
);
91 lbt
= map_find(MAP_TYPE_SEC_GPT_TBL
);
92 if (tbl
== NULL
|| lbt
== NULL
) {
93 warnx("%s: error: run recover -- trust me", device_name
);
98 if (entry
> le32toh(hdr
->hdr_entries
)) {
99 warnx("%s: error: index %u out of range (%u max)", device_name
,
100 entry
, le32toh(hdr
->hdr_entries
));
106 ent
= (void*)((char*)tbl
->map_data
+ i
*
107 le32toh(hdr
->hdr_entsz
));
108 if (!uuid_is_nil((uuid_t
*)&ent
->ent_type
, NULL
)) {
109 warnx("%s: error: entry at index %u is not free",
114 /* Find empty slot in GPT table. */
115 for (i
= 0; i
< le32toh(hdr
->hdr_entries
); i
++) {
116 ent
= (void*)((char*)tbl
->map_data
+ i
*
117 le32toh(hdr
->hdr_entsz
));
118 if (uuid_is_nil((uuid_t
*)&ent
->ent_type
, NULL
))
121 if (i
== le32toh(hdr
->hdr_entries
)) {
122 warnx("%s: error: no available table entries",
128 map
= map_alloc(block
, size
);
130 warnx("%s: error: no space available on device", device_name
);
134 le_uuid_enc((uuid_t
*)&ent
->ent_type
, &type
);
135 ent
->ent_lba_start
= htole64(map
->map_start
);
136 ent
->ent_lba_end
= htole64(map
->map_start
+ map
->map_size
- 1LL);
138 hdr
->hdr_crc_table
= htole32(crc32(tbl
->map_data
,
139 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
140 hdr
->hdr_crc_self
= 0;
141 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
147 ent
= (void*)((char*)lbt
->map_data
+ i
* le32toh(hdr
->hdr_entsz
));
149 le_uuid_enc(&ent
->ent_type
, &type
);
150 ent
->ent_lba_start
= htole64(map
->map_start
);
151 ent
->ent_lba_end
= htole64(map
->map_start
+ map
->map_size
- 1LL);
153 hdr
->hdr_crc_table
= htole32(crc32(lbt
->map_data
,
154 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
155 hdr
->hdr_crc_self
= 0;
156 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
162 printf("%sp%u added\n", device_name
, i
+ 1);
165 printf("Partition added, use:\n");
166 printf("\tdkctl %s addwedge dk<N> %" PRIu64
" %" PRIu64
" <type>\n",
167 device_arg
, map
->map_start
, map
->map_size
);
168 printf("to create a wedge for it\n");
173 cmd_add(int argc
, char *argv
[])
178 /* Get the migrate options */
179 while ((ch
= getopt(argc
, argv
, "b:i:s:t:")) != -1) {
184 block
= strtoll(optarg
, &p
, 10);
185 if (*p
!= 0 || block
< 1)
191 entry
= strtoul(optarg
, &p
, 10);
192 if (*p
!= 0 || entry
< 1)
198 size
= strtoll(optarg
, &p
, 10);
199 if (*p
!= 0 || size
< 1)
203 if (!uuid_is_nil(&type
, NULL
))
205 if (parse_uuid(optarg
, &type
) != 0)
216 /* Create UFS partitions by default. */
217 if (uuid_is_nil(&type
, NULL
)) {
218 uuid_t ufs
= GPT_ENT_TYPE_NETBSD_FFS
;
222 while (optind
< argc
) {
223 fd
= gpt_open(argv
[optind
++]);
225 warn("unable to open device '%s'", device_name
);