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.
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.1 2007/06/16 22:29:27 dillon Exp $
30 #include <sys/types.h>
43 static off_t block
, size
;
44 static unsigned int entry
;
51 "usage: %s [-b lba] [-i index] [-s lba] [-t uuid] device ...\n",
66 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
68 warnx("%s: error: no primary GPT header; run create or recover",
73 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
75 warnx("%s: error: no secondary GPT header; run recover",
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
);
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
));
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",
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
))
111 if (i
== le32toh(hdr
->hdr_entries
)) {
112 warnx("%s: error: no available table entries",
118 map
= map_alloc(block
, size
);
120 warnx("%s: error: no space available on device", device_name
);
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);
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
)));
137 ent
= (void*)((char*)lbt
->map_data
+ i
* le32toh(hdr
->hdr_entsz
));
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);
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
)));
151 printf("%sp%u added\n", device_name
, i
+ 1);
155 cmd_add(int argc
, char *argv
[])
160 /* Get the migrate options */
161 while ((ch
= getopt(argc
, argv
, "b:i:s:t:")) != -1) {
166 block
= strtoll(optarg
, &p
, 10);
167 if (*p
!= 0 || block
< 1)
173 entry
= strtol(optarg
, &p
, 10);
174 if (*p
!= 0 || entry
< 1)
180 size
= strtoll(optarg
, &p
, 10);
181 if (*p
!= 0 || size
< 1)
185 if (!uuid_is_nil(&type
, NULL
))
187 if (parse_uuid(optarg
, &type
) != 0)
198 /* Create UFS partitions by default. */
199 if (uuid_is_nil(&type
, NULL
)) {
200 uuid_t ufs
= GPT_ENT_TYPE_FREEBSD_UFS
;
204 while (optind
< argc
) {
205 fd
= gpt_open(argv
[optind
++]);
207 warn("unable to open device '%s'", device_name
);