Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F150700477
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
45 KB
Referenced Files
None
Subscribers
None
View Options
Index: stable/7/sys/cddl/contrib/opensolaris
===================================================================
--- stable/7/sys/cddl/contrib/opensolaris (revision 209073)
+++ stable/7/sys/cddl/contrib/opensolaris (revision 209074)
Property changes on: stable/7/sys/cddl/contrib/opensolaris
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
Merged /head/sys/cddl/contrib/opensolaris:r208746
Index: stable/7/sys/contrib/dev/acpica
===================================================================
--- stable/7/sys/contrib/dev/acpica (revision 209073)
+++ stable/7/sys/contrib/dev/acpica (revision 209074)
Property changes on: stable/7/sys/contrib/dev/acpica
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
Merged /head/sys/contrib/dev/acpica:r208746
Index: stable/7/sys/contrib/pf
===================================================================
--- stable/7/sys/contrib/pf (revision 209073)
+++ stable/7/sys/contrib/pf (revision 209074)
Property changes on: stable/7/sys/contrib/pf
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
Merged /head/sys/contrib/pf:r208746
Index: stable/7/sys/geom/part/g_part_bsd.c
===================================================================
--- stable/7/sys/geom/part/g_part_bsd.c (revision 209073)
+++ stable/7/sys/geom/part/g_part_bsd.c (revision 209074)
@@ -1,490 +1,496 @@
/*-
* 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>
#include <geom/geom.h>
#include <geom/part/g_part.h>
#include "g_part_if.h"
struct g_part_bsd_table {
struct g_part_table base;
u_char *bbarea;
uint32_t offset;
};
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 *);
static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
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 *);
static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
struct sbuf *, const char *);
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 *);
static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
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 *);
static kobj_method_t g_part_bsd_methods[] = {
KOBJMETHOD(g_part_add, g_part_bsd_add),
KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
KOBJMETHOD(g_part_create, g_part_bsd_create),
KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
KOBJMETHOD(g_part_modify, g_part_bsd_modify),
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,
.gps_maxent = 20,
.gps_bootcodesz = BBSIZE,
};
G_PART_SCHEME_DECLARE(g_part_bsd);
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);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
if (!strcasecmp(type, alias)) {
*fstype = FS_ZFS;
return (0);
}
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;
entry->part.p_size = gpp->gpp_size;
entry->part.p_offset = gpp->gpp_start + table->offset;
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));
}
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);
}
static int
g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
{
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;
u_char *ptr;
uint64_t msize;
uint32_t ncyls, secpercyl;
pp = gpp->gpp_provider;
cp = LIST_FIRST(&pp->consumers);
if (pp->sectorsize < sizeof(struct disklabel))
return (ENOSPC);
if (BBSIZE % pp->sectorsize)
return (ENOTBLK);
msize = pp->mediasize / pp->sectorsize;
secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
ncyls = msize / secpercyl;
table = (struct g_part_bsd_table *)basetable;
table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
ptr = table->bbarea + pp->sectorsize;
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 */
le32enc(ptr + 60, msize); /* d_secperunit */
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;
basetable->gpt_last = msize - 1;
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;
entry->part.p_offset = table->offset;
return (0);
}
static int
g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
{
+ struct g_part_bsd_table *table;
+
+ table = (struct g_part_bsd_table *)basetable;
+ if (table->bbarea != NULL)
+ g_free(table->bbarea);
+ table->bbarea = NULL;
/* Wipe the second sector to clear the partitioning. */
basetable->gpt_smhead |= 2;
return (0);
}
static void
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;
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 */
}
}
static int
g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
{
struct g_part_bsd_entry *entry;
/* Allow dumping to a swap partition or an unused partition. */
entry = (struct g_part_bsd_entry *)baseentry;
return ((entry->part.p_fstype == FS_UNUSED ||
entry->part.p_fstype == FS_SWAP) ? 1 : 0);
}
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);
}
static const char *
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);
if (BBSIZE % pp->sectorsize)
return (ENOTBLK);
/* 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)
? G_PART_PROBE_PRI_HIGH : ENXIO);
}
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;
table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
if (table->bbarea == NULL)
return (error);
buf = table->bbarea + pp->sectorsize;
if (le32dec(buf + 40) != pp->sectorsize)
goto invalid_label;
sectors = le32dec(buf + 44);
if (sectors < 1 || sectors > 255)
goto invalid_label;
if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
g_part_geometry_heads(msize, sectors, &chs, &heads);
if (chs != 0) {
basetable->gpt_sectors = sectors;
basetable->gpt_heads = heads;
}
}
heads = le32dec(buf + 48);
if (heads < 1 || heads > 255)
goto invalid_label;
if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
basetable->gpt_heads = heads;
if (sectors != basetable->gpt_sectors ||
heads != basetable->gpt_heads)
printf("GEOM: %s: geometry does not match label.\n", pp->name);
chs = le32dec(buf + 60);
if (chs < 1)
goto invalid_label;
/* Fix-up a sysinstall bug. */
if (chs > msize) {
chs = msize;
le32enc(buf + 60, msize);
}
if (chs != msize)
printf("GEOM: %s: media size does not match label.\n",
pp->name);
basetable->gpt_first = 0;
basetable->gpt_last = msize - 1;
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;
table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
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;
if (part.p_offset < table->offset)
continue;
baseentry = g_part_new_entry(basetable, index + 1,
part.p_offset - table->offset,
part.p_offset - table->offset + part.p_size - 1);
entry = (struct g_part_bsd_entry *)baseentry;
entry->part = part;
if (index == RAW_PART)
baseentry->gpe_internal = 1;
}
return (0);
invalid_label:
printf("GEOM: %s: invalid disklabel.\n", pp->name);
g_free(table->bbarea);
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));
if (type == FS_ZFS)
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
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;
u_char *label, *p, *pe;
int error, index;
pp = cp->provider;
table = (struct g_part_bsd_table *)basetable;
baseentry = LIST_FIRST(&basetable->gpt_entry);
label = table->bbarea + pp->sectorsize;
for (index = 1; index <= basetable->gpt_entries; index++) {
p = label + 148 + (index - 1) * 16;
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. */
le16enc(label + 136, 0);
pe = label + 148 + basetable->gpt_entries * 16;
sum = 0;
for (p = label; p < pe; p += 2)
sum ^= le16dec(p);
le16enc(label + 136, sum);
error = g_write_data(cp, 0, table->bbarea, BBSIZE);
return (error);
}
Index: stable/7/sys/geom/part/g_part_gpt.c
===================================================================
--- stable/7/sys/geom/part/g_part_gpt.c (revision 209073)
+++ stable/7/sys/geom/part/g_part_gpt.c (revision 209074)
@@ -1,938 +1,944 @@
/*-
* Copyright (c) 2002, 2005, 2006, 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/diskmbr.h>
#include <sys/endian.h>
#include <sys/gpt.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>
#include <sys/uuid.h>
#include <geom/geom.h>
#include <geom/part/g_part.h>
#include "g_part_if.h"
CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
CTASSERT(sizeof(struct gpt_ent) == 128);
#define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0)
#define MBRSIZE 512
enum gpt_elt {
GPT_ELT_PRIHDR,
GPT_ELT_PRITBL,
GPT_ELT_SECHDR,
GPT_ELT_SECTBL,
GPT_ELT_COUNT
};
enum gpt_state {
GPT_STATE_UNKNOWN, /* Not determined. */
GPT_STATE_MISSING, /* No signature found. */
GPT_STATE_CORRUPT, /* Checksum mismatch. */
GPT_STATE_INVALID, /* Nonconformant/invalid. */
GPT_STATE_OK /* Perfectly fine. */
};
struct g_part_gpt_table {
struct g_part_table base;
u_char mbr[MBRSIZE];
struct gpt_hdr *hdr;
quad_t lba[GPT_ELT_COUNT];
enum gpt_state state[GPT_ELT_COUNT];
};
struct g_part_gpt_entry {
struct g_part_entry base;
struct gpt_ent ent;
};
static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
struct g_part_parms *);
static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
struct sbuf *, const char *);
static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
struct g_part_parms *);
static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
char *, size_t);
static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
char *, size_t);
static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
static kobj_method_t g_part_gpt_methods[] = {
KOBJMETHOD(g_part_add, g_part_gpt_add),
KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode),
KOBJMETHOD(g_part_create, g_part_gpt_create),
KOBJMETHOD(g_part_destroy, g_part_gpt_destroy),
KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf),
KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto),
KOBJMETHOD(g_part_modify, g_part_gpt_modify),
KOBJMETHOD(g_part_name, g_part_gpt_name),
KOBJMETHOD(g_part_probe, g_part_gpt_probe),
KOBJMETHOD(g_part_read, g_part_gpt_read),
KOBJMETHOD(g_part_type, g_part_gpt_type),
KOBJMETHOD(g_part_write, g_part_gpt_write),
{ 0, 0 }
};
static struct g_part_scheme g_part_gpt_scheme = {
"GPT",
g_part_gpt_methods,
sizeof(struct g_part_gpt_table),
.gps_entrysz = sizeof(struct g_part_gpt_entry),
.gps_minent = 128,
.gps_maxent = INT_MAX,
.gps_bootcodesz = MBRSIZE,
};
G_PART_SCHEME_DECLARE(g_part_gpt);
static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
static struct gpt_hdr *
gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
enum gpt_elt elt)
{
struct gpt_hdr *buf, *hdr;
struct g_provider *pp;
quad_t lba, last;
int error;
uint32_t crc, sz;
pp = cp->provider;
last = (pp->mediasize / pp->sectorsize) - 1;
table->lba[elt] = (elt == GPT_ELT_PRIHDR) ? 1 : last;
table->state[elt] = GPT_STATE_MISSING;
buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
&error);
if (buf == NULL)
return (NULL);
hdr = NULL;
if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
goto fail;
table->state[elt] = GPT_STATE_CORRUPT;
sz = le32toh(buf->hdr_size);
if (sz < 92 || sz > pp->sectorsize)
goto fail;
hdr = g_malloc(sz, M_WAITOK | M_ZERO);
bcopy(buf, hdr, sz);
hdr->hdr_size = sz;
crc = le32toh(buf->hdr_crc_self);
buf->hdr_crc_self = 0;
if (crc32(buf, sz) != crc)
goto fail;
hdr->hdr_crc_self = crc;
table->state[elt] = GPT_STATE_INVALID;
hdr->hdr_revision = le32toh(buf->hdr_revision);
if (hdr->hdr_revision < 0x00010000)
goto fail;
hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
if (hdr->hdr_lba_self != table->lba[elt])
goto fail;
hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
/* Check the managed area. */
hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
goto fail;
hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
goto fail;
/* Check the table location and size of the table. */
hdr->hdr_entries = le32toh(buf->hdr_entries);
hdr->hdr_entsz = le32toh(buf->hdr_entsz);
if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
(hdr->hdr_entsz & 7) != 0)
goto fail;
hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
goto fail;
if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
hdr->hdr_lba_table <= hdr->hdr_lba_end)
goto fail;
lba = hdr->hdr_lba_table +
(hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
pp->sectorsize - 1;
if (lba >= last)
goto fail;
if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
goto fail;
table->state[elt] = GPT_STATE_OK;
le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
g_free(buf);
return (hdr);
fail:
if (hdr != NULL)
g_free(hdr);
g_free(buf);
return (NULL);
}
static struct gpt_ent *
gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
enum gpt_elt elt, struct gpt_hdr *hdr)
{
struct g_provider *pp;
struct gpt_ent *ent, *tbl;
char *buf, *p;
unsigned int idx, sectors, tblsz;
int error;
if (hdr == NULL)
return (NULL);
pp = cp->provider;
table->lba[elt] = hdr->hdr_lba_table;
table->state[elt] = GPT_STATE_MISSING;
tblsz = hdr->hdr_entries * hdr->hdr_entsz;
sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
buf = g_read_data(cp, table->lba[elt] * pp->sectorsize,
sectors * pp->sectorsize, &error);
if (buf == NULL)
return (NULL);
table->state[elt] = GPT_STATE_CORRUPT;
if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
g_free(buf);
return (NULL);
}
table->state[elt] = GPT_STATE_OK;
tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
M_WAITOK | M_ZERO);
for (idx = 0, ent = tbl, p = buf;
idx < hdr->hdr_entries;
idx++, ent++, p += hdr->hdr_entsz) {
le_uuid_dec(p, &ent->ent_type);
le_uuid_dec(p + 16, &ent->ent_uuid);
ent->ent_lba_start = le64dec(p + 32);
ent->ent_lba_end = le64dec(p + 40);
ent->ent_attr = le64dec(p + 48);
/* Keep UTF-16 in little-endian. */
bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
}
g_free(buf);
return (tbl);
}
static int
gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
{
if (pri == NULL || sec == NULL)
return (0);
if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
return (0);
return ((pri->hdr_revision == sec->hdr_revision &&
pri->hdr_size == sec->hdr_size &&
pri->hdr_lba_start == sec->hdr_lba_start &&
pri->hdr_lba_end == sec->hdr_lba_end &&
pri->hdr_entries == sec->hdr_entries &&
pri->hdr_entsz == sec->hdr_entsz &&
pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
}
static int
gpt_parse_type(const char *type, struct uuid *uuid)
{
struct uuid tmp;
const char *alias;
int error;
if (type[0] == '!') {
error = parse_uuid(type + 1, &tmp);
if (error)
return (error);
if (EQUUID(&tmp, &gpt_uuid_unused))
return (EINVAL);
*uuid = tmp;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_EFI);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_efi;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd_boot;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd_swap;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd_ufs;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd_vinum;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_freebsd_zfs;
return (0);
}
alias = g_part_alias_name(G_PART_ALIAS_MBR);
if (!strcasecmp(type, alias)) {
*uuid = gpt_uuid_mbr;
return (0);
}
return (EINVAL);
}
static int
g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
struct g_part_parms *gpp)
{
struct g_part_gpt_entry *entry;
int error;
entry = (struct g_part_gpt_entry *)baseentry;
error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
if (error)
return (error);
kern_uuidgen(&entry->ent.ent_uuid, 1);
entry->ent.ent_lba_start = baseentry->gpe_start;
entry->ent.ent_lba_end = baseentry->gpe_end;
if (baseentry->gpe_deleted) {
entry->ent.ent_attr = 0;
bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
}
if (gpp->gpp_parms & G_PART_PARM_LABEL)
g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
sizeof(entry->ent.ent_name));
return (0);
}
static int
g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
{
struct g_part_gpt_table *table;
size_t codesz;
codesz = DOSPARTOFF;
table = (struct g_part_gpt_table *)basetable;
bzero(table->mbr, codesz);
codesz = MIN(codesz, gpp->gpp_codesize);
if (codesz > 0)
bcopy(gpp->gpp_codeptr, table->mbr, codesz);
/* Mark the PMBR active since some BIOS require it */
table->mbr[DOSPARTOFF] = 0x80; /* status */
return (0);
}
static int
g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
{
struct g_provider *pp;
struct g_part_gpt_table *table;
quad_t last;
size_t tblsz;
table = (struct g_part_gpt_table *)basetable;
pp = gpp->gpp_provider;
tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
pp->sectorsize - 1) / pp->sectorsize;
if (pp->sectorsize < MBRSIZE ||
pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
pp->sectorsize)
return (ENOSPC);
last = (pp->mediasize / pp->sectorsize) - 1;
le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
table->mbr[DOSPARTOFF + 1] = 0xff; /* shd */
table->mbr[DOSPARTOFF + 2] = 0xff; /* ssect */
table->mbr[DOSPARTOFF + 3] = 0xff; /* scyl */
table->mbr[DOSPARTOFF + 4] = 0xee; /* typ */
table->mbr[DOSPARTOFF + 5] = 0xff; /* ehd */
table->mbr[DOSPARTOFF + 6] = 0xff; /* esect */
table->mbr[DOSPARTOFF + 7] = 0xff; /* ecyl */
le32enc(table->mbr + DOSPARTOFF + 8, 1); /* start */
le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL));
table->lba[GPT_ELT_PRIHDR] = 1;
table->lba[GPT_ELT_PRITBL] = 2;
table->lba[GPT_ELT_SECHDR] = last;
table->lba[GPT_ELT_SECTBL] = last - tblsz;
/* Allocate space for the header */
table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
table->hdr->hdr_revision = GPT_HDR_REVISION;
table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
table->hdr->hdr_lba_start = 2 + tblsz;
table->hdr->hdr_lba_end = last - tblsz - 1;
kern_uuidgen(&table->hdr->hdr_uuid, 1);
table->hdr->hdr_entries = basetable->gpt_entries;
table->hdr->hdr_entsz = sizeof(struct gpt_ent);
basetable->gpt_first = table->hdr->hdr_lba_start;
basetable->gpt_last = table->hdr->hdr_lba_end;
return (0);
}
static int
g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
{
+ struct g_part_gpt_table *table;
+
+ table = (struct g_part_gpt_table *)basetable;
+ if (table->hdr != NULL)
+ g_free(table->hdr);
+ table->hdr = NULL;
/*
* Wipe the first 2 sectors as well as the last to clear the
* partitioning.
*/
basetable->gpt_smhead |= 3;
basetable->gpt_smtail |= 1;
return (0);
}
static void
g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
struct sbuf *sb, const char *indent)
{
struct g_part_gpt_entry *entry;
entry = (struct g_part_gpt_entry *)baseentry;
if (indent == NULL) {
/* conftxt: libdisk compatibility */
sbuf_printf(sb, " xs GPT xt ");
sbuf_printf_uuid(sb, &entry->ent.ent_type);
} else if (entry != NULL) {
/* confxml: partition entry information */
sbuf_printf(sb, "%s<label>", indent);
g_gpt_printf_utf16(sb, entry->ent.ent_name,
sizeof(entry->ent.ent_name) >> 1);
sbuf_printf(sb, "</label>\n");
sbuf_printf(sb, "%s<rawtype>", indent);
sbuf_printf_uuid(sb, &entry->ent.ent_type);
sbuf_printf(sb, "</rawtype>\n");
} else {
/* confxml: scheme information */
}
}
static int
g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
{
struct g_part_gpt_entry *entry;
entry = (struct g_part_gpt_entry *)baseentry;
return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0);
}
static int
g_part_gpt_modify(struct g_part_table *basetable,
struct g_part_entry *baseentry, struct g_part_parms *gpp)
{
struct g_part_gpt_entry *entry;
int error;
entry = (struct g_part_gpt_entry *)baseentry;
if (gpp->gpp_parms & G_PART_PARM_TYPE) {
error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
if (error)
return (error);
}
if (gpp->gpp_parms & G_PART_PARM_LABEL)
g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
sizeof(entry->ent.ent_name));
return (0);
}
static const char *
g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
char *buf, size_t bufsz)
{
struct g_part_gpt_entry *entry;
char c;
entry = (struct g_part_gpt_entry *)baseentry;
c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
return (buf);
}
static int
g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
{
struct g_provider *pp;
char *buf;
int error, res;
/* We don't nest, which means that our depth should be 0. */
if (table->gpt_depth != 0)
return (ENXIO);
pp = cp->provider;
/*
* Sanity-check the provider. Since the first sector on the provider
* must be a PMBR and a PMBR is 512 bytes large, the sector size
* must be at least 512 bytes. Also, since the theoretical minimum
* number of sectors needed by GPT is 6, any medium that has less
* than 6 sectors is never going to be able to hold a GPT. The
* number 6 comes from:
* 1 sector for the PMBR
* 2 sectors for the GPT headers (each 1 sector)
* 2 sectors for the GPT tables (each 1 sector)
* 1 sector for an actual partition
* It's better to catch this pathological case early than behaving
* pathologically later on...
*/
if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
return (ENOSPC);
/* Check that there's a MBR. */
buf = g_read_data(cp, 0L, pp->sectorsize, &error);
if (buf == NULL)
return (error);
res = le16dec(buf + DOSMAGICOFFSET);
g_free(buf);
if (res != DOSMAGIC)
return (ENXIO);
/* Check that there's a primary header. */
buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
if (buf == NULL)
return (error);
res = memcmp(buf, GPT_HDR_SIG, 8);
g_free(buf);
if (res == 0)
return (G_PART_PROBE_PRI_HIGH);
/* No primary? Check that there's a secondary. */
buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
&error);
if (buf == NULL)
return (error);
res = memcmp(buf, GPT_HDR_SIG, 8);
g_free(buf);
return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
}
static int
g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
{
struct gpt_hdr *prihdr, *sechdr;
struct gpt_ent *tbl, *pritbl, *sectbl;
struct g_provider *pp;
struct g_part_gpt_table *table;
struct g_part_gpt_entry *entry;
u_char *buf;
int error, index;
table = (struct g_part_gpt_table *)basetable;
pp = cp->provider;
/* Read the PMBR */
buf = g_read_data(cp, 0, pp->sectorsize, &error);
if (buf == NULL)
return (error);
bcopy(buf, table->mbr, MBRSIZE);
g_free(buf);
/* Read the primary header and table. */
prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
} else {
table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
pritbl = NULL;
}
/* Read the secondary header and table. */
sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
} else {
table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
sectbl = NULL;
}
/* Fail if we haven't got any good tables at all. */
if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
printf("GEOM: %s: corrupt or invalid GPT detected.\n",
pp->name);
printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
pp->name);
return (EINVAL);
}
/*
* If both headers are good but they disagree with each other,
* then invalidate one. We prefer to keep the primary header,
* unless the primary table is corrupt.
*/
if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
!gpt_matched_hdrs(prihdr, sechdr)) {
if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
g_free(sechdr);
sechdr = NULL;
} else {
table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
g_free(prihdr);
prihdr = NULL;
}
}
if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
printf("GEOM: %s: the primary GPT table is corrupt or "
"invalid.\n", pp->name);
printf("GEOM: %s: using the secondary instead -- recovery "
"strongly advised.\n", pp->name);
table->hdr = sechdr;
if (prihdr != NULL)
g_free(prihdr);
tbl = sectbl;
if (pritbl != NULL)
g_free(pritbl);
} else {
if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
printf("GEOM: %s: the secondary GPT table is corrupt "
"or invalid.\n", pp->name);
printf("GEOM: %s: using the primary only -- recovery "
"suggested.\n", pp->name);
}
table->hdr = prihdr;
if (sechdr != NULL)
g_free(sechdr);
tbl = pritbl;
if (sectbl != NULL)
g_free(sectbl);
}
basetable->gpt_first = table->hdr->hdr_lba_start;
basetable->gpt_last = table->hdr->hdr_lba_end;
basetable->gpt_entries = table->hdr->hdr_entries;
for (index = basetable->gpt_entries - 1; index >= 0; index--) {
if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
continue;
entry = (struct g_part_gpt_entry *)g_part_new_entry(basetable,
index+1, tbl[index].ent_lba_start, tbl[index].ent_lba_end);
entry->ent = tbl[index];
}
g_free(tbl);
return (0);
}
static const char *
g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
char *buf, size_t bufsz)
{
struct g_part_gpt_entry *entry;
struct uuid *type;
entry = (struct g_part_gpt_entry *)baseentry;
type = &entry->ent.ent_type;
if (EQUUID(type, &gpt_uuid_efi))
return (g_part_alias_name(G_PART_ALIAS_EFI));
if (EQUUID(type, &gpt_uuid_freebsd))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
if (EQUUID(type, &gpt_uuid_freebsd_boot))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT));
if (EQUUID(type, &gpt_uuid_freebsd_swap))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
if (EQUUID(type, &gpt_uuid_freebsd_ufs))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
if (EQUUID(type, &gpt_uuid_freebsd_vinum))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
if (EQUUID(type, &gpt_uuid_freebsd_zfs))
return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
if (EQUUID(type, &gpt_uuid_mbr))
return (g_part_alias_name(G_PART_ALIAS_MBR));
buf[0] = '!';
snprintf_uuid(buf + 1, bufsz - 1, type);
return (buf);
}
static int
g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
{
unsigned char *buf, *bp;
struct g_provider *pp;
struct g_part_entry *baseentry;
struct g_part_gpt_entry *entry;
struct g_part_gpt_table *table;
size_t tlbsz;
uint32_t crc;
int error, index;
pp = cp->provider;
table = (struct g_part_gpt_table *)basetable;
tlbsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
pp->sectorsize - 1) / pp->sectorsize;
/* Write the PMBR */
buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
bcopy(table->mbr, buf, MBRSIZE);
error = g_write_data(cp, 0, buf, pp->sectorsize);
g_free(buf);
if (error)
return (error);
/* Allocate space for the header and entries. */
buf = g_malloc((tlbsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
le32enc(buf + 8, table->hdr->hdr_revision);
le32enc(buf + 12, table->hdr->hdr_size);
le64enc(buf + 40, table->hdr->hdr_lba_start);
le64enc(buf + 48, table->hdr->hdr_lba_end);
le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
le32enc(buf + 80, table->hdr->hdr_entries);
le32enc(buf + 84, table->hdr->hdr_entsz);
LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
if (baseentry->gpe_deleted)
continue;
entry = (struct g_part_gpt_entry *)baseentry;
index = baseentry->gpe_index - 1;
bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
le_uuid_enc(bp, &entry->ent.ent_type);
le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
le64enc(bp + 32, entry->ent.ent_lba_start);
le64enc(bp + 40, entry->ent.ent_lba_end);
le64enc(bp + 48, entry->ent.ent_attr);
memcpy(bp + 56, entry->ent.ent_name,
sizeof(entry->ent.ent_name));
}
crc = crc32(buf + pp->sectorsize,
table->hdr->hdr_entries * table->hdr->hdr_entsz);
le32enc(buf + 88, crc);
/* Write primary meta-data. */
le32enc(buf + 16, 0); /* hdr_crc_self. */
le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
crc = crc32(buf, table->hdr->hdr_size);
le32enc(buf + 16, crc);
error = g_write_data(cp, table->lba[GPT_ELT_PRITBL] * pp->sectorsize,
buf + pp->sectorsize, tlbsz * pp->sectorsize);
if (error)
goto out;
error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
buf, pp->sectorsize);
if (error)
goto out;
/* Write secondary meta-data. */
le32enc(buf + 16, 0); /* hdr_crc_self. */
le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
crc = crc32(buf, table->hdr->hdr_size);
le32enc(buf + 16, crc);
error = g_write_data(cp, table->lba[GPT_ELT_SECTBL] * pp->sectorsize,
buf + pp->sectorsize, tlbsz * pp->sectorsize);
if (error)
goto out;
error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
buf, pp->sectorsize);
out:
g_free(buf);
return (error);
}
static void
g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
{
u_int bo;
uint32_t ch;
uint16_t c;
bo = LITTLE_ENDIAN; /* GPT is little-endian */
while (len > 0 && *str != 0) {
ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
str++, len--;
if ((ch & 0xf800) == 0xd800) {
if (len > 0) {
c = (bo == BIG_ENDIAN) ? be16toh(*str)
: le16toh(*str);
str++, len--;
} else
c = 0xfffd;
if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
ch += 0x10000;
} else
ch = 0xfffd;
} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
continue;
} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
continue;
/* Write the Unicode character in UTF-8 */
if (ch < 0x80)
sbuf_printf(sb, "%c", ch);
else if (ch < 0x800)
sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6),
0x80 | (ch & 0x3f));
else if (ch < 0x10000)
sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12),
0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
else if (ch < 0x200000)
sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18),
0x80 | ((ch >> 12) & 0x3f),
0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
}
}
static void
g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
{
size_t s16idx, s8idx;
uint32_t utfchar;
unsigned int c, utfbytes;
s8idx = s16idx = 0;
utfchar = 0;
utfbytes = 0;
bzero(s16, s16len << 1);
while (s8[s8idx] != 0 && s16idx < s16len) {
c = s8[s8idx++];
if ((c & 0xc0) != 0x80) {
/* Initial characters. */
if (utfbytes != 0) {
/* Incomplete encoding of previous char. */
s16[s16idx++] = htole16(0xfffd);
}
if ((c & 0xf8) == 0xf0) {
utfchar = c & 0x07;
utfbytes = 3;
} else if ((c & 0xf0) == 0xe0) {
utfchar = c & 0x0f;
utfbytes = 2;
} else if ((c & 0xe0) == 0xc0) {
utfchar = c & 0x1f;
utfbytes = 1;
} else {
utfchar = c & 0x7f;
utfbytes = 0;
}
} else {
/* Followup characters. */
if (utfbytes > 0) {
utfchar = (utfchar << 6) + (c & 0x3f);
utfbytes--;
} else if (utfbytes == 0)
utfbytes = ~0;
}
/*
* Write the complete Unicode character as UTF-16 when we
* have all the UTF-8 charactars collected.
*/
if (utfbytes == 0) {
/*
* If we need to write 2 UTF-16 characters, but
* we only have room for 1, then we truncate the
* string by writing a 0 instead.
*/
if (utfchar >= 0x10000 && s16idx < s16len - 1) {
s16[s16idx++] =
htole16(0xd800 | ((utfchar >> 10) - 0x40));
s16[s16idx++] =
htole16(0xdc00 | (utfchar & 0x3ff));
} else
s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
htole16(utfchar);
}
}
/*
* If our input string was truncated, append an invalid encoding
* character to the output string.
*/
if (utfbytes != 0 && s16idx < s16len)
s16[s16idx++] = htole16(0xfffd);
}
Index: stable/7/sys
===================================================================
--- stable/7/sys (revision 209073)
+++ stable/7/sys (revision 209074)
Property changes on: stable/7/sys
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
Merged /head/sys:r208746
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Sun, Apr 5, 11:24 AM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
30816017
Default Alt Text
(45 KB)
Attached To
Mode
rS FreeBSD src repository - subversion
Attached
Detach File
Event Timeline
Log In to Comment