2002-10-27 03:23:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-08-07 06:24:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-27 03:23:05 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "map.h"
|
|
|
|
#include "gpt.h"
|
|
|
|
|
2002-11-30 22:51:46 +00:00
|
|
|
static uuid_t type;
|
|
|
|
static off_t block, size;
|
2004-08-07 06:10:45 +00:00
|
|
|
static unsigned int entry;
|
2002-10-27 03:23:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage_add(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2004-08-07 06:10:45 +00:00
|
|
|
"usage: %s [-b lba] [-i index] [-s lba] [-t uuid] device\n",
|
|
|
|
getprogname());
|
2002-10-27 03:23:05 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add(int fd)
|
|
|
|
{
|
|
|
|
map_t *gpt, *tpg;
|
|
|
|
map_t *tbl, *lbt;
|
|
|
|
map_t *map;
|
|
|
|
struct gpt_hdr *hdr;
|
|
|
|
struct gpt_ent *ent;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
|
|
|
|
if (gpt == NULL) {
|
2003-11-16 06:45:26 +00:00
|
|
|
warnx("%s: error: no primary GPT header; run create or recover",
|
|
|
|
device_name);
|
2002-10-27 03:23:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
|
|
|
|
if (tpg == NULL) {
|
2003-11-16 06:45:26 +00:00
|
|
|
warnx("%s: error: no secondary GPT header; run recover",
|
2002-10-27 03:23:05 +00:00
|
|
|
device_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
|
|
|
|
lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
|
|
|
|
if (tbl == NULL || lbt == NULL) {
|
|
|
|
warnx("%s: error: run recover -- trust me", device_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-07 06:10:45 +00:00
|
|
|
hdr = gpt->map_data;
|
2004-10-25 02:23:39 +00:00
|
|
|
if (entry > le32toh(hdr->hdr_entries)) {
|
2004-08-07 06:10:45 +00:00
|
|
|
warnx("%s: error: index %u out of range (%u max)", device_name,
|
2004-10-25 02:23:39 +00:00
|
|
|
entry, le32toh(hdr->hdr_entries));
|
2002-10-27 03:23:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-07 06:10:45 +00:00
|
|
|
if (entry > 0) {
|
|
|
|
i = entry - 1;
|
2004-10-25 02:23:39 +00:00
|
|
|
ent = (void*)((char*)tbl->map_data + i *
|
|
|
|
le32toh(hdr->hdr_entsz));
|
2004-08-07 06:10:45 +00:00
|
|
|
if (!uuid_is_nil(&ent->ent_type, NULL)) {
|
|
|
|
warnx("%s: error: entry at index %u is not free",
|
|
|
|
device_name, entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Find empty slot in GPT table. */
|
2004-10-25 02:23:39 +00:00
|
|
|
for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
|
2004-08-07 06:10:45 +00:00
|
|
|
ent = (void*)((char*)tbl->map_data + i *
|
2004-10-25 02:23:39 +00:00
|
|
|
le32toh(hdr->hdr_entsz));
|
2004-08-07 06:10:45 +00:00
|
|
|
if (uuid_is_nil(&ent->ent_type, NULL))
|
|
|
|
break;
|
|
|
|
}
|
2004-10-25 02:23:39 +00:00
|
|
|
if (i == le32toh(hdr->hdr_entries)) {
|
2004-08-07 06:10:45 +00:00
|
|
|
warnx("%s: error: no available table entries",
|
|
|
|
device_name);
|
|
|
|
return;
|
|
|
|
}
|
2002-10-27 03:23:05 +00:00
|
|
|
}
|
2004-08-07 06:10:45 +00:00
|
|
|
|
|
|
|
map = map_alloc(block, size);
|
|
|
|
if (map == NULL) {
|
|
|
|
warnx("%s: error: no space available on device", device_name);
|
2002-10-27 03:23:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-25 02:23:39 +00:00
|
|
|
le_uuid_enc(&ent->ent_type, &type);
|
|
|
|
ent->ent_lba_start = htole64(map->map_start);
|
|
|
|
ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
|
2002-10-27 03:23:05 +00:00
|
|
|
|
2004-10-25 02:23:39 +00:00
|
|
|
hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
|
|
|
|
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
|
2002-10-27 03:23:05 +00:00
|
|
|
hdr->hdr_crc_self = 0;
|
2004-10-25 02:23:39 +00:00
|
|
|
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
|
2002-10-27 03:23:05 +00:00
|
|
|
|
|
|
|
gpt_write(fd, gpt);
|
|
|
|
gpt_write(fd, tbl);
|
|
|
|
|
|
|
|
hdr = tpg->map_data;
|
2004-10-25 02:23:39 +00:00
|
|
|
ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
|
2002-10-27 03:23:05 +00:00
|
|
|
|
2004-10-25 02:23:39 +00:00
|
|
|
le_uuid_enc(&ent->ent_type, &type);
|
|
|
|
ent->ent_lba_start = htole64(map->map_start);
|
|
|
|
ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
|
2002-10-27 03:23:05 +00:00
|
|
|
|
2004-10-25 02:23:39 +00:00
|
|
|
hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
|
|
|
|
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
|
2002-10-27 03:23:05 +00:00
|
|
|
hdr->hdr_crc_self = 0;
|
2004-10-25 02:23:39 +00:00
|
|
|
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
|
2002-10-27 03:23:05 +00:00
|
|
|
|
|
|
|
gpt_write(fd, lbt);
|
|
|
|
gpt_write(fd, tpg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_add(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int ch, fd;
|
|
|
|
uint32_t status;
|
|
|
|
|
|
|
|
/* Get the migrate options */
|
2004-08-07 06:10:45 +00:00
|
|
|
while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
|
2002-10-27 03:23:05 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'b':
|
|
|
|
if (block > 0)
|
|
|
|
usage_add();
|
|
|
|
block = strtol(optarg, &p, 10);
|
|
|
|
if (*p != 0 || block < 1)
|
|
|
|
usage_add();
|
|
|
|
break;
|
2004-08-07 06:10:45 +00:00
|
|
|
case 'i':
|
|
|
|
if (entry > 0)
|
|
|
|
usage_add();
|
|
|
|
entry = strtol(optarg, &p, 10);
|
|
|
|
if (*p != 0 || entry < 1)
|
|
|
|
usage_add();
|
|
|
|
break;
|
2002-10-27 03:23:05 +00:00
|
|
|
case 's':
|
|
|
|
if (size > 0)
|
|
|
|
usage_add();
|
|
|
|
size = strtol(optarg, &p, 10);
|
|
|
|
if (*p != 0 || size < 1)
|
|
|
|
usage_add();
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (!uuid_is_nil(&type, NULL))
|
|
|
|
usage_add();
|
|
|
|
uuid_from_string(optarg, &type, &status);
|
|
|
|
if (status != uuid_s_ok) {
|
2002-11-30 22:51:46 +00:00
|
|
|
if (strcmp(optarg, "efi") == 0) {
|
|
|
|
uuid_t efi = GPT_ENT_TYPE_EFI;
|
|
|
|
type = efi;
|
|
|
|
} else if (strcmp(optarg, "swap") == 0) {
|
|
|
|
uuid_t sw = GPT_ENT_TYPE_FREEBSD_SWAP;
|
|
|
|
type = sw;
|
|
|
|
} else if (strcmp(optarg, "ufs") == 0) {
|
|
|
|
uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
|
|
|
|
type = ufs;
|
2004-08-07 06:10:45 +00:00
|
|
|
} else if (strcmp(optarg, "linux") == 0 ||
|
|
|
|
strcmp(optarg, "windows") == 0) {
|
|
|
|
uuid_t m = GPT_ENT_TYPE_MS_BASIC_DATA;
|
|
|
|
type = m;
|
2002-11-30 22:51:46 +00:00
|
|
|
} else
|
|
|
|
usage_add();
|
2002-10-27 03:23:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_add();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == optind)
|
|
|
|
usage_add();
|
|
|
|
|
2004-08-07 06:10:45 +00:00
|
|
|
/* Create UFS partitions by default. */
|
|
|
|
if (uuid_is_nil(&type, NULL)) {
|
|
|
|
uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
|
|
|
|
type = ufs;
|
|
|
|
}
|
|
|
|
|
2002-10-27 03:23:05 +00:00
|
|
|
while (optind < argc) {
|
|
|
|
fd = gpt_open(argv[optind++]);
|
|
|
|
if (fd == -1) {
|
|
|
|
warn("unable to open device '%s'", device_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(fd);
|
|
|
|
|
|
|
|
gpt_close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|