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
|
2015-12-04 00:46:22 +03:00
|
|
|
__RCSID("$NetBSD: add.c,v 1.42 2015/12/03 21:46:22 christos Exp $");
|
2006-10-16 02:36:29 +04:00
|
|
|
#endif
|
2006-10-16 02:34:15 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2015-12-01 12:05:33 +03:00
|
|
|
#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"
|
2015-12-01 12:05:33 +03:00
|
|
|
#include "gpt_private.h"
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static int cmd_add(gpt_t, int, char *[]);
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static const char *addhelp[] = {
|
2015-12-03 04:07:28 +03:00
|
|
|
"[-a alignment] [-b blocknr] [-i index] [-l label]",
|
|
|
|
"[-s size] [-t type]",
|
2015-12-01 19:32:19 +03:00
|
|
|
};
|
2007-12-19 00:46:45 +03:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
struct gpt_cmd c_add = {
|
|
|
|
"add",
|
|
|
|
cmd_add,
|
|
|
|
addhelp, __arraycount(addhelp),
|
|
|
|
0,
|
|
|
|
};
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
#define usage() gpt_usage(NULL, &c_add)
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2015-12-02 04:01:55 +03: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));
|
2015-12-02 07:07:11 +03:00
|
|
|
if (xname == NULL)
|
|
|
|
return;
|
|
|
|
utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
|
2015-12-02 04:01:55 +03:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
static int
|
2015-12-03 04:07:28 +03:00
|
|
|
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
|
|
|
{
|
2015-12-01 12:05:33 +03:00
|
|
|
map_t map;
|
2006-10-16 02:34:15 +04:00
|
|
|
struct gpt_hdr *hdr;
|
2015-12-02 04:01:55 +03:00
|
|
|
struct gpt_ent *ent;
|
2006-10-16 02:34:15 +04:00
|
|
|
unsigned int i;
|
2013-11-19 09:03:41 +04:00
|
|
|
off_t alignsecs;
|
2015-12-01 22:25:24 +03:00
|
|
|
char buf[128];
|
2013-11-19 09:03:41 +04:00
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
if ((hdr = gpt_hdr(gpt)) == NULL)
|
|
|
|
return -1;
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2006-10-18 01:14:03 +04:00
|
|
|
ent = NULL;
|
2006-10-16 02:34:15 +04:00
|
|
|
|
|
|
|
if (entry > le32toh(hdr->hdr_entries)) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "index %u out of range (%u max)",
|
2006-10-16 02:34:15 +04:00
|
|
|
entry, le32toh(hdr->hdr_entries));
|
2015-12-01 12:05:33 +03:00
|
|
|
return -1;
|
2006-10-16 02:34:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry > 0) {
|
|
|
|
i = entry - 1;
|
2015-12-01 12:05:33 +03:00
|
|
|
ent = gpt_ent_primary(gpt, i);
|
2014-09-30 21:59:59 +04:00
|
|
|
if (!gpt_uuid_is_nil(ent->ent_type)) {
|
2015-12-01 12:05:33 +03:00
|
|
|
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++) {
|
2015-12-01 12:05:33 +03:00
|
|
|
ent = gpt_ent_primary(gpt, i);
|
2014-09-30 21:59:59 +04:00
|
|
|
if (gpt_uuid_is_nil(ent->ent_type))
|
2006-10-16 02:34:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == le32toh(hdr->hdr_entries)) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "No available table entries");
|
|
|
|
return -1;
|
2006-10-16 02:34:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 09:03:41 +04:00
|
|
|
if (alignment > 0) {
|
2015-12-01 12:05:33 +03:00
|
|
|
alignsecs = alignment / gpt->secsz;
|
|
|
|
map = map_alloc(gpt, block, sectors, alignsecs);
|
2013-11-19 09:03:41 +04:00
|
|
|
if (map == NULL) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Not enough space available on "
|
|
|
|
"device for an aligned partition");
|
|
|
|
return -1;
|
2013-11-19 09:03:41 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-01 12:05:33 +03:00
|
|
|
map = map_alloc(gpt, block, sectors, 0);
|
2013-11-19 09:03:41 +04:00
|
|
|
if (map == NULL) {
|
2015-12-01 12:05:33 +03:00
|
|
|
gpt_warnx(gpt, "Not enough space available on device");
|
|
|
|
return -1;
|
2013-11-19 09:03:41 +04:00
|
|
|
}
|
2006-10-16 02:34:15 +04:00
|
|
|
}
|
|
|
|
|
2015-12-02 04:01:55 +03: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
|
|
|
|
2015-12-01 12:05:33 +03:00
|
|
|
ent = gpt_ent_backup(gpt, i);
|
2015-12-02 04:01:55 +03:00
|
|
|
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);
|
2015-12-01 12:05:33 +03:00
|
|
|
return 0;
|
2006-10-16 02:34:15 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:32:19 +03:00
|
|
|
static int
|
2015-12-01 12:05:33 +03:00
|
|
|
cmd_add(gpt_t gpt, int argc, char *argv[])
|
2006-10-16 02:34:15 +04:00
|
|
|
{
|
2015-12-01 12:05:33 +03:00
|
|
|
int ch;
|
2015-12-03 04:07:28 +03:00
|
|
|
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
|
|
|
|
2015-12-02 14:20:34 +03:00
|
|
|
while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
|
2006-10-16 02:34:15 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'b':
|
2015-12-02 02:29:07 +03:00
|
|
|
if (gpt_human_get(&block) == -1)
|
2015-12-04 00:43:25 +03:00
|
|
|
goto usage;
|
2006-10-16 02:34:15 +04:00
|
|
|
break;
|
2013-11-19 09:03:41 +04:00
|
|
|
case 'l':
|
2015-12-02 02:29:07 +03:00
|
|
|
if (gpt_name_get(gpt, &name) == -1)
|
2015-12-04 00:43:25 +03:00
|
|
|
goto usage;
|
2013-11-19 09:03:41 +04:00
|
|
|
break;
|
2006-10-16 02:34:15 +04:00
|
|
|
case 't':
|
2015-12-02 02:29:07 +03:00
|
|
|
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)
|
2015-12-01 19:32:19 +03:00
|
|
|
return usage();
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2011-01-06 19:30:40 +03:00
|
|
|
/* Create NetBSD FFS partitions by default. */
|
2015-12-02 02:29:07 +03:00
|
|
|
if (gpt_uuid_is_nil(type))
|
2014-09-30 21:59:59 +04:00
|
|
|
gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
|
2006-10-16 02:34:15 +04:00
|
|
|
|
2015-12-01 12:05:33 +03: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
|
|
|
|
2015-12-03 04:07:28 +03: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
|
|
|
}
|