2007-12-06 02:32:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/kobj.h>
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/systm.h>
|
2011-02-25 10:24:35 +00:00
|
|
|
#include <sys/sysctl.h>
|
2007-12-06 02:32:42 +00:00
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/part/g_part.h>
|
|
|
|
|
|
|
|
#include "g_part_if.h"
|
|
|
|
|
2011-02-25 10:24:35 +00:00
|
|
|
FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
|
|
|
|
|
2007-12-06 02:32:42 +00:00
|
|
|
struct g_part_bsd_table {
|
|
|
|
struct g_part_table base;
|
2009-02-21 07:01:21 +00:00
|
|
|
u_char *bbarea;
|
2007-12-24 01:01:59 +00:00
|
|
|
uint32_t offset;
|
2007-12-06 02:32:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct g_part_bsd_entry {
|
|
|
|
struct g_part_entry base;
|
|
|
|
struct partition part;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
|
|
|
|
struct g_part_parms *);
|
2009-02-21 07:01:21 +00:00
|
|
|
static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
|
2007-12-06 02:32:42 +00:00
|
|
|
static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
|
|
|
|
static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
|
2009-02-08 07:05:23 +00:00
|
|
|
static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
|
2008-04-23 20:13:05 +00:00
|
|
|
struct sbuf *, const char *);
|
2007-12-06 02:32:42 +00:00
|
|
|
static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
|
|
|
|
static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
|
|
|
|
struct g_part_parms *);
|
2009-02-08 07:05:23 +00:00
|
|
|
static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
|
2007-12-06 02:32:42 +00:00
|
|
|
char *, size_t);
|
|
|
|
static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
|
|
|
|
static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
|
|
|
|
static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
|
|
|
|
char *, size_t);
|
|
|
|
static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
|
2010-04-23 03:11:39 +00:00
|
|
|
static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
|
|
|
|
struct g_part_parms *);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
static kobj_method_t g_part_bsd_methods[] = {
|
|
|
|
KOBJMETHOD(g_part_add, g_part_bsd_add),
|
2009-02-21 07:01:21 +00:00
|
|
|
KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
|
2007-12-06 02:32:42 +00:00
|
|
|
KOBJMETHOD(g_part_create, g_part_bsd_create),
|
|
|
|
KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
|
2008-04-23 20:13:05 +00:00
|
|
|
KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
|
2007-12-06 02:32:42 +00:00
|
|
|
KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
|
|
|
|
KOBJMETHOD(g_part_modify, g_part_bsd_modify),
|
2010-04-23 03:11:39 +00:00
|
|
|
KOBJMETHOD(g_part_resize, g_part_bsd_resize),
|
2007-12-06 02:32:42 +00:00
|
|
|
KOBJMETHOD(g_part_name, g_part_bsd_name),
|
|
|
|
KOBJMETHOD(g_part_probe, g_part_bsd_probe),
|
|
|
|
KOBJMETHOD(g_part_read, g_part_bsd_read),
|
|
|
|
KOBJMETHOD(g_part_type, g_part_bsd_type),
|
|
|
|
KOBJMETHOD(g_part_write, g_part_bsd_write),
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct g_part_scheme g_part_bsd_scheme = {
|
|
|
|
"BSD",
|
|
|
|
g_part_bsd_methods,
|
|
|
|
sizeof(struct g_part_bsd_table),
|
|
|
|
.gps_entrysz = sizeof(struct g_part_bsd_entry),
|
|
|
|
.gps_minent = 8,
|
2010-12-02 19:47:27 +00:00
|
|
|
.gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */
|
2009-02-21 07:01:21 +00:00
|
|
|
.gps_bootcodesz = BBSIZE,
|
2007-12-06 02:32:42 +00:00
|
|
|
};
|
2008-03-23 01:31:59 +00:00
|
|
|
G_PART_SCHEME_DECLARE(g_part_bsd);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
bsd_parse_type(const char *type, uint8_t *fstype)
|
|
|
|
{
|
|
|
|
const char *alias;
|
|
|
|
char *endp;
|
|
|
|
long lt;
|
|
|
|
|
|
|
|
if (type[0] == '!') {
|
|
|
|
lt = strtol(type + 1, &endp, 0);
|
|
|
|
if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
|
|
|
|
return (EINVAL);
|
|
|
|
*fstype = (u_int)lt;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
|
|
|
|
if (!strcasecmp(type, alias)) {
|
|
|
|
*fstype = FS_SWAP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
|
|
|
|
if (!strcasecmp(type, alias)) {
|
|
|
|
*fstype = FS_BSDFFS;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
|
|
|
|
if (!strcasecmp(type, alias)) {
|
|
|
|
*fstype = FS_VINUM;
|
|
|
|
return (0);
|
|
|
|
}
|
2007-12-08 07:01:10 +00:00
|
|
|
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
|
|
|
|
if (!strcasecmp(type, alias)) {
|
|
|
|
*fstype = FS_ZFS;
|
|
|
|
return (0);
|
|
|
|
}
|
2007-12-06 02:32:42 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
|
|
|
|
struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_LABEL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
|
|
|
|
2007-12-24 01:01:59 +00:00
|
|
|
entry->part.p_size = gpp->gpp_size;
|
|
|
|
entry->part.p_offset = gpp->gpp_start + table->offset;
|
2007-12-06 02:32:42 +00:00
|
|
|
entry->part.p_fsize = 0;
|
|
|
|
entry->part.p_frag = 0;
|
|
|
|
entry->part.p_cpg = 0;
|
|
|
|
return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
|
|
|
|
}
|
|
|
|
|
2009-02-21 07:01:21 +00:00
|
|
|
static int
|
|
|
|
g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
const u_char *codeptr;
|
|
|
|
size_t hdsz, tlsz;
|
|
|
|
size_t codesz, tlofs;
|
|
|
|
|
|
|
|
hdsz = 512;
|
|
|
|
tlofs = hdsz + 148 + basetable->gpt_entries * 16;
|
|
|
|
tlsz = BBSIZE - tlofs;
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
|
|
|
bzero(table->bbarea, hdsz);
|
|
|
|
bzero(table->bbarea + tlofs, tlsz);
|
|
|
|
codeptr = gpp->gpp_codeptr;
|
|
|
|
codesz = MIN(hdsz, gpp->gpp_codesize);
|
|
|
|
if (codesz > 0)
|
|
|
|
bcopy(codeptr, table->bbarea, codesz);
|
|
|
|
codesz = MIN(tlsz, gpp->gpp_codesize - tlofs);
|
|
|
|
if (codesz > 0)
|
|
|
|
bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-12-06 02:32:42 +00:00
|
|
|
static int
|
|
|
|
g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_part_entry *baseentry;
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
u_char *ptr;
|
2009-03-27 05:48:42 +00:00
|
|
|
uint32_t msize, ncyls, secpercyl;
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
pp = gpp->gpp_provider;
|
|
|
|
|
|
|
|
if (pp->sectorsize < sizeof(struct disklabel))
|
|
|
|
return (ENOSPC);
|
2009-02-21 07:01:21 +00:00
|
|
|
if (BBSIZE % pp->sectorsize)
|
|
|
|
return (ENOTBLK);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
2009-03-27 05:48:42 +00:00
|
|
|
msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
|
2007-12-06 02:32:42 +00:00
|
|
|
secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
|
|
|
|
ncyls = msize / secpercyl;
|
|
|
|
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
2009-02-21 07:01:21 +00:00
|
|
|
table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
|
|
|
|
ptr = table->bbarea + pp->sectorsize;
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
le32enc(ptr + 0, DISKMAGIC); /* d_magic */
|
|
|
|
le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
|
|
|
|
le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
|
|
|
|
le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
|
|
|
|
le32enc(ptr + 52, ncyls); /* d_ncylinders */
|
|
|
|
le32enc(ptr + 56, secpercyl); /* d_secpercyl */
|
2007-12-24 01:01:59 +00:00
|
|
|
le32enc(ptr + 60, msize); /* d_secperunit */
|
2007-12-06 02:32:42 +00:00
|
|
|
le16enc(ptr + 72, 3600); /* d_rpm */
|
|
|
|
le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
|
|
|
|
le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
|
|
|
|
le32enc(ptr + 140, BBSIZE); /* d_bbsize */
|
|
|
|
|
|
|
|
basetable->gpt_first = 0;
|
2007-12-24 01:01:59 +00:00
|
|
|
basetable->gpt_last = msize - 1;
|
2007-12-06 02:32:42 +00:00
|
|
|
basetable->gpt_isleaf = 1;
|
|
|
|
|
|
|
|
baseentry = g_part_new_entry(basetable, RAW_PART + 1,
|
|
|
|
basetable->gpt_first, basetable->gpt_last);
|
|
|
|
baseentry->gpe_internal = 1;
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
entry->part.p_size = basetable->gpt_last + 1;
|
2007-12-24 01:01:59 +00:00
|
|
|
entry->part.p_offset = table->offset;
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
2010-06-02 17:17:11 +00:00
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
|
|
|
if (table->bbarea != NULL)
|
|
|
|
g_free(table->bbarea);
|
|
|
|
table->bbarea = NULL;
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
/* Wipe the second sector to clear the partitioning. */
|
|
|
|
basetable->gpt_smhead |= 2;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-02-08 07:05:23 +00:00
|
|
|
static void
|
2008-04-23 20:13:05 +00:00
|
|
|
g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
|
|
|
|
struct sbuf *sb, const char *indent)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
2008-06-12 05:27:23 +00:00
|
|
|
if (indent == NULL) {
|
|
|
|
/* conftxt: libdisk compatibility */
|
|
|
|
sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
|
|
|
|
} else if (entry != NULL) {
|
|
|
|
/* confxml: partition entry information */
|
|
|
|
sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
|
|
|
|
entry->part.p_fstype);
|
|
|
|
} else {
|
|
|
|
/* confxml: scheme information */
|
|
|
|
}
|
2008-04-23 20:13:05 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 02:32:42 +00:00
|
|
|
static int
|
|
|
|
g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
|
2008-11-26 05:18:27 +00:00
|
|
|
/* Allow dumping to a swap partition or an unused partition. */
|
2007-12-06 02:32:42 +00:00
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
2008-11-26 05:18:27 +00:00
|
|
|
return ((entry->part.p_fstype == FS_UNUSED ||
|
|
|
|
entry->part.p_fstype == FS_SWAP) ? 1 : 0);
|
2007-12-06 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_modify(struct g_part_table *basetable,
|
|
|
|
struct g_part_entry *baseentry, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_LABEL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_TYPE)
|
|
|
|
return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-04-23 03:11:39 +00:00
|
|
|
static int
|
|
|
|
g_part_bsd_resize(struct g_part_table *basetable,
|
|
|
|
struct g_part_entry *baseentry, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
|
|
|
|
entry->part.p_size = gpp->gpp_size;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-02-08 07:05:23 +00:00
|
|
|
static const char *
|
2007-12-06 02:32:42 +00:00
|
|
|
g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
|
|
|
|
char *buf, size_t bufsz)
|
|
|
|
{
|
|
|
|
|
|
|
|
snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct g_provider *pp;
|
|
|
|
u_char *buf;
|
|
|
|
uint32_t magic1, magic2;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
pp = cp->provider;
|
|
|
|
|
|
|
|
/* Sanity-check the provider. */
|
|
|
|
if (pp->sectorsize < sizeof(struct disklabel) ||
|
|
|
|
pp->mediasize < BBSIZE)
|
|
|
|
return (ENOSPC);
|
2009-02-21 07:01:21 +00:00
|
|
|
if (BBSIZE % pp->sectorsize)
|
|
|
|
return (ENOTBLK);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
/* Check that there's a disklabel. */
|
|
|
|
buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
|
|
|
|
if (buf == NULL)
|
|
|
|
return (error);
|
|
|
|
magic1 = le32dec(buf + 0);
|
|
|
|
magic2 = le32dec(buf + 132);
|
|
|
|
g_free(buf);
|
|
|
|
return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
|
2008-09-29 02:48:22 +00:00
|
|
|
? G_PART_PROBE_PRI_HIGH : ENXIO);
|
2007-12-06 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
struct g_part_entry *baseentry;
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
struct partition part;
|
|
|
|
u_char *buf, *p;
|
|
|
|
off_t chs, msize;
|
|
|
|
u_int sectors, heads;
|
|
|
|
int error, index;
|
|
|
|
|
|
|
|
pp = cp->provider;
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
|
|
|
msize = pp->mediasize / pp->sectorsize;
|
|
|
|
|
2009-02-21 07:01:21 +00:00
|
|
|
table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
|
|
|
|
if (table->bbarea == NULL)
|
2007-12-06 02:32:42 +00:00
|
|
|
return (error);
|
|
|
|
|
2009-02-21 07:01:21 +00:00
|
|
|
buf = table->bbarea + pp->sectorsize;
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
if (le32dec(buf + 40) != pp->sectorsize)
|
|
|
|
goto invalid_label;
|
|
|
|
sectors = le32dec(buf + 44);
|
2008-09-27 15:28:15 +00:00
|
|
|
if (sectors < 1 || sectors > 255)
|
2007-12-06 02:32:42 +00:00
|
|
|
goto invalid_label;
|
2007-12-24 01:01:59 +00:00
|
|
|
if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
|
2007-12-06 02:32:42 +00:00
|
|
|
g_part_geometry_heads(msize, sectors, &chs, &heads);
|
2007-12-24 01:01:59 +00:00
|
|
|
if (chs != 0) {
|
|
|
|
basetable->gpt_sectors = sectors;
|
|
|
|
basetable->gpt_heads = heads;
|
|
|
|
}
|
2007-12-06 02:32:42 +00:00
|
|
|
}
|
|
|
|
heads = le32dec(buf + 48);
|
|
|
|
if (heads < 1 || heads > 255)
|
|
|
|
goto invalid_label;
|
2007-12-24 01:01:59 +00:00
|
|
|
if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
|
2007-12-06 02:32:42 +00:00
|
|
|
basetable->gpt_heads = heads;
|
2008-12-26 20:27:32 +00:00
|
|
|
if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
|
|
|
|
printf("GEOM: %s: geometry does not match label"
|
|
|
|
" (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
|
|
|
|
basetable->gpt_heads, basetable->gpt_sectors);
|
2007-12-24 01:01:59 +00:00
|
|
|
|
|
|
|
chs = le32dec(buf + 60);
|
2008-10-25 17:21:46 +00:00
|
|
|
if (chs < 1)
|
2007-12-06 02:32:42 +00:00
|
|
|
goto invalid_label;
|
2008-10-25 17:21:46 +00:00
|
|
|
/* Fix-up a sysinstall bug. */
|
|
|
|
if (chs > msize) {
|
|
|
|
chs = msize;
|
|
|
|
le32enc(buf + 60, msize);
|
|
|
|
}
|
2007-12-24 01:01:59 +00:00
|
|
|
if (chs != msize)
|
|
|
|
printf("GEOM: %s: media size does not match label.\n",
|
|
|
|
pp->name);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
|
|
|
basetable->gpt_first = 0;
|
2007-12-24 01:01:59 +00:00
|
|
|
basetable->gpt_last = msize - 1;
|
2007-12-06 02:32:42 +00:00
|
|
|
basetable->gpt_isleaf = 1;
|
|
|
|
|
|
|
|
basetable->gpt_entries = le16dec(buf + 138);
|
|
|
|
if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
|
|
|
|
basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
|
|
|
|
goto invalid_label;
|
|
|
|
|
2007-12-24 01:01:59 +00:00
|
|
|
table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
|
2007-12-06 02:32:42 +00:00
|
|
|
for (index = basetable->gpt_entries - 1; index >= 0; index--) {
|
|
|
|
p = buf + 148 + index * 16;
|
|
|
|
part.p_size = le32dec(p + 0);
|
|
|
|
part.p_offset = le32dec(p + 4);
|
|
|
|
part.p_fsize = le32dec(p + 8);
|
|
|
|
part.p_fstype = p[12];
|
|
|
|
part.p_frag = p[13];
|
|
|
|
part.p_cpg = le16dec(p + 14);
|
|
|
|
if (part.p_size == 0)
|
|
|
|
continue;
|
2007-12-24 01:01:59 +00:00
|
|
|
if (part.p_offset < table->offset)
|
2007-12-06 02:32:42 +00:00
|
|
|
continue;
|
2011-01-27 08:02:26 +00:00
|
|
|
if (part.p_offset - table->offset > basetable->gpt_last)
|
|
|
|
goto invalid_label;
|
2007-12-06 02:32:42 +00:00
|
|
|
baseentry = g_part_new_entry(basetable, index + 1,
|
2007-12-24 01:01:59 +00:00
|
|
|
part.p_offset - table->offset,
|
|
|
|
part.p_offset - table->offset + part.p_size - 1);
|
2007-12-06 02:32:42 +00:00
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
entry->part = part;
|
2008-11-18 05:55:58 +00:00
|
|
|
if (index == RAW_PART)
|
2007-12-06 02:32:42 +00:00
|
|
|
baseentry->gpe_internal = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
invalid_label:
|
|
|
|
printf("GEOM: %s: invalid disklabel.\n", pp->name);
|
2009-02-21 07:01:21 +00:00
|
|
|
g_free(table->bbarea);
|
2011-01-27 08:02:26 +00:00
|
|
|
table->bbarea = NULL;
|
2007-12-06 02:32:42 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
|
|
|
|
char *buf, size_t bufsz)
|
|
|
|
{
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
entry = (struct g_part_bsd_entry *)baseentry;
|
|
|
|
type = entry->part.p_fstype;
|
|
|
|
if (type == FS_SWAP)
|
|
|
|
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
|
|
|
|
if (type == FS_BSDFFS)
|
|
|
|
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
|
|
|
|
if (type == FS_VINUM)
|
|
|
|
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
|
2007-12-08 07:01:10 +00:00
|
|
|
if (type == FS_ZFS)
|
|
|
|
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
|
2007-12-06 02:32:42 +00:00
|
|
|
snprintf(buf, bufsz, "!%d", type);
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_part_entry *baseentry;
|
|
|
|
struct g_part_bsd_entry *entry;
|
|
|
|
struct g_part_bsd_table *table;
|
|
|
|
uint16_t sum;
|
2009-02-21 07:01:21 +00:00
|
|
|
u_char *label, *p, *pe;
|
2007-12-06 02:32:42 +00:00
|
|
|
int error, index;
|
|
|
|
|
|
|
|
pp = cp->provider;
|
|
|
|
table = (struct g_part_bsd_table *)basetable;
|
|
|
|
baseentry = LIST_FIRST(&basetable->gpt_entry);
|
2009-02-21 07:01:21 +00:00
|
|
|
label = table->bbarea + pp->sectorsize;
|
2007-12-06 02:32:42 +00:00
|
|
|
for (index = 1; index <= basetable->gpt_entries; index++) {
|
2009-02-21 07:01:21 +00:00
|
|
|
p = label + 148 + (index - 1) * 16;
|
2007-12-06 02:32:42 +00:00
|
|
|
entry = (baseentry != NULL && index == baseentry->gpe_index)
|
|
|
|
? (struct g_part_bsd_entry *)baseentry : NULL;
|
|
|
|
if (entry != NULL && !baseentry->gpe_deleted) {
|
|
|
|
le32enc(p + 0, entry->part.p_size);
|
|
|
|
le32enc(p + 4, entry->part.p_offset);
|
|
|
|
le32enc(p + 8, entry->part.p_fsize);
|
|
|
|
p[12] = entry->part.p_fstype;
|
|
|
|
p[13] = entry->part.p_frag;
|
|
|
|
le16enc(p + 14, entry->part.p_cpg);
|
|
|
|
} else
|
|
|
|
bzero(p, 16);
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
baseentry = LIST_NEXT(baseentry, gpe_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate checksum. */
|
2009-02-21 07:01:21 +00:00
|
|
|
le16enc(label + 136, 0);
|
|
|
|
pe = label + 148 + basetable->gpt_entries * 16;
|
2007-12-06 02:32:42 +00:00
|
|
|
sum = 0;
|
2009-02-21 07:01:21 +00:00
|
|
|
for (p = label; p < pe; p += 2)
|
2007-12-06 02:32:42 +00:00
|
|
|
sum ^= le16dec(p);
|
2009-02-21 07:01:21 +00:00
|
|
|
le16enc(label + 136, sum);
|
2007-12-06 02:32:42 +00:00
|
|
|
|
2009-02-21 07:01:21 +00:00
|
|
|
error = g_write_data(cp, 0, table->bbarea, BBSIZE);
|
2007-12-06 02:32:42 +00:00
|
|
|
return (error);
|
|
|
|
}
|