NetBSD/sbin/gpt/add.c

209 lines
5.2 KiB
C
Raw Normal View History

2006-10-16 02:34:15 +04: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.
*/
2014-09-30 00:28:57 +04:00
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
2006-10-16 02:34:15 +04:00
#include <sys/cdefs.h>
2006-10-16 02:36:29 +04:00
#ifdef __FBSDID
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: add.c,v 1.43 2015/12/29 16:45:04 christos Exp $");
2006-10-16 02:36:29 +04:00
#endif
2006-10-16 02:34:15 +04:00
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
2006-10-16 02:34:15 +04:00
#include <err.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "map.h"
#include "gpt.h"
#include "gpt_private.h"
2006-10-16 02:34:15 +04:00
static int cmd_add(gpt_t, int, char *[]);
2006-10-16 02:34:15 +04:00
static const char *addhelp[] = {
"[-a alignment] [-b blocknr] [-i index] [-l label]",
"[-s size] [-t type]",
};
struct gpt_cmd c_add = {
"add",
cmd_add,
addhelp, __arraycount(addhelp),
0,
};
2006-10-16 02:34:15 +04:00
#define usage() gpt_usage(NULL, &c_add)
2006-10-16 02:34:15 +04:00
static void
ent_set(struct gpt_ent *ent, const map_t map, const gpt_uuid_t xtype,
const uint8_t *xname)
{
gpt_uuid_copy(ent->ent_type, xtype);
2015-12-03 05:02:43 +03:00
ent->ent_lba_start = htole64((uint64_t)map->map_start);
ent->ent_lba_end = htole64((uint64_t)(map->map_start +
map->map_size - 1LL));
if (xname == NULL)
return;
utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
}
static int
add(gpt_t gpt, off_t alignment, off_t block, off_t sectors, off_t size,
u_int entry, uint8_t *name, gpt_uuid_t type)
2006-10-16 02:34:15 +04:00
{
map_t map;
2006-10-16 02:34:15 +04:00
struct gpt_hdr *hdr;
struct gpt_ent *ent;
2006-10-16 02:34:15 +04:00
unsigned int i;
off_t alignsecs;
2015-12-01 22:25:24 +03:00
char buf[128];
if ((hdr = gpt_hdr(gpt)) == NULL)
return -1;
2006-10-16 02:34:15 +04:00
ent = NULL;
2006-10-16 02:34:15 +04:00
if (entry > le32toh(hdr->hdr_entries)) {
gpt_warnx(gpt, "index %u out of range (%u max)",
2006-10-16 02:34:15 +04:00
entry, le32toh(hdr->hdr_entries));
return -1;
2006-10-16 02:34:15 +04:00
}
if (entry > 0) {
i = entry - 1;
ent = gpt_ent_primary(gpt, i);
if (!gpt_uuid_is_nil(ent->ent_type)) {
gpt_warnx(gpt, "Entry at index %u is not free", entry);
return -1;
2006-10-16 02:34:15 +04:00
}
} else {
/* Find empty slot in GPT table. */
for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
ent = gpt_ent_primary(gpt, i);
if (gpt_uuid_is_nil(ent->ent_type))
2006-10-16 02:34:15 +04:00
break;
}
if (i == le32toh(hdr->hdr_entries)) {
gpt_warnx(gpt, "No available table entries");
return -1;
2006-10-16 02:34:15 +04:00
}
}
if (alignment > 0) {
alignsecs = alignment / gpt->secsz;
map = map_alloc(gpt, block, sectors, alignsecs);
if (map == NULL) {
gpt_warnx(gpt, "Not enough space available on "
"device for an aligned partition");
return -1;
}
} else {
map = map_alloc(gpt, block, sectors, 0);
if (map == NULL) {
gpt_warnx(gpt, "Not enough space available on device");
return -1;
}
2006-10-16 02:34:15 +04:00
}
ent_set(ent, map, type, name);
2015-12-03 23:58:08 +03:00
if (gpt_write_primary(gpt) == -1)
return -1;
2006-10-16 02:34:15 +04:00
ent = gpt_ent_backup(gpt, i);
ent_set(ent, map, type, name);
2015-12-03 23:58:08 +03:00
if (gpt_write_backup(gpt) == -1)
return -1;
2006-10-16 02:34:15 +04:00
2015-12-01 22:25:24 +03:00
gpt_uuid_snprintf(buf, sizeof(buf), "%d", type);
gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64, i + 1,
buf, map->map_start, map->map_size);
return 0;
2006-10-16 02:34:15 +04:00
}
static int
cmd_add(gpt_t gpt, int argc, char *argv[])
2006-10-16 02:34:15 +04:00
{
int ch;
off_t alignment = 0, block = 0, sectors = 0, size = 0;
unsigned int entry = 0;
uint8_t *name = NULL;
gpt_uuid_t type;
gpt_uuid_copy(type, gpt_uuid_nil);
2006-10-16 02:34:15 +04:00
while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
2006-10-16 02:34:15 +04:00
switch(ch) {
case 'b':
if (gpt_human_get(gpt, &block) == -1)
2015-12-04 00:43:25 +03:00
goto usage;
2006-10-16 02:34:15 +04:00
break;
case 'l':
if (gpt_name_get(gpt, &name) == -1)
2015-12-04 00:43:25 +03:00
goto usage;
break;
2006-10-16 02:34:15 +04:00
case 't':
if (gpt_uuid_get(gpt, &type) == -1)
2015-12-04 00:43:25 +03:00
goto usage;
2006-10-16 02:34:15 +04:00
break;
default:
2015-12-01 22:25:24 +03:00
if (gpt_add_ais(gpt, &alignment, &entry, &size, ch)
== -1)
2015-12-04 00:43:25 +03:00
goto usage;
2015-12-01 22:25:24 +03:00
break;
2006-10-16 02:34:15 +04:00
}
}
2015-12-01 22:25:24 +03:00
if (argc != optind)
return usage();
2006-10-16 02:34:15 +04:00
/* Create NetBSD FFS partitions by default. */
if (gpt_uuid_is_nil(type))
gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
2006-10-16 02:34:15 +04:00
if (optind != argc)
2015-12-04 00:43:25 +03:00
goto cleanup;
2006-10-16 02:34:15 +04:00
2015-12-03 05:02:43 +03:00
if ((sectors = gpt_check_ais(gpt, alignment, ~0U, size)) == -1)
2015-12-04 00:43:25 +03:00
goto cleanup;
2006-10-16 02:34:15 +04:00
return add(gpt, alignment, block, sectors, size, entry, name, type);
2015-12-04 00:46:22 +03:00
usage:
return usage();
2015-12-04 00:43:25 +03:00
cleanup:
free(name);
return -1;
2006-10-16 02:34:15 +04:00
}