Be more systematic about conversion of on-disk formats in a endian/width

agnostic way.

Collapse the MBR and MBREXT methods into one file and make them endian/width
agnostic.

Sponsored by:	DARPA & NAI Labs.
This commit is contained in:
Poul-Henning Kamp 2002-03-24 11:21:41 +00:00
parent f4af24d55d
commit 3773a65c0b
6 changed files with 310 additions and 235 deletions

View File

@ -690,11 +690,11 @@ geom/geom_bsd.c optional geom
geom/geom_dev.c optional geom
geom/geom_disk.c optional geom
geom/geom_dump.c optional geom
geom/geom_enc.c optional geom
geom/geom_event.c optional geom
geom/geom_io.c optional geom
geom/geom_kern.c optional geom
geom/geom_mbr.c optional geom
geom/geom_mbrext.c optional geom
geom/geom_slice.c optional geom
geom/geom_subr.c optional geom
geom/geom_sunlabel.c optional geom

View File

@ -198,6 +198,13 @@ void g_trace(int level, char *, ...);
# define G_T_BIO 2
# define G_T_ACCESS 4
/* geom_enc.c */
uint32_t g_dec_be2(u_char *p);
uint32_t g_dec_be4(u_char *p);
uint32_t g_dec_le2(u_char *p);
uint32_t g_dec_le4(u_char *p);
void g_enc_le4(u_char *p, uint32_t u);
/* geom_event.c */
void g_event_init(void);
void g_orphan_provider(struct g_provider *pp, int error);

101
sys/geom/geom_enc.c Normal file
View File

@ -0,0 +1,101 @@
/*-
* Copyright (c) 2002 Poul-Henning Kamp
* Copyright (c) 2002 Networks Associates Technology, Inc.
* All rights reserved.
*
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
* and NAI Labs, the Security Research Division of Network Associates, Inc.
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
* DARPA CHATS research program.
*
* 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.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*
* $FreeBSD$
*/
/*
* This file contains routines to pack and unpack integerfields of varying
* width and endianess with. Ideally the C language would have this built
* in but I guess Dennis and Brian forgot that back in the early 70ies.
*
* The function names are systematic: g_{enc|dec}_{be|le}%d
* enc -> encode
* dec -> decode
* be -> big endian
* le -> little endian
* %d -> width in bytes
*
* Please keep the functions sorted:
* decode before encode
* big endian before little endian
* small width before larger width
* (this happens to be alphabetically)
*/
#include <sys/param.h>
#ifdef _KERNEL
#include <sys/malloc.h>
#endif
#include <geom/geom.h>
uint32_t
g_dec_be2(u_char *p)
{
return((p[0] << 8) | p[1]);
}
uint32_t
g_dec_be4(u_char *p)
{
return((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
}
uint32_t
g_dec_le2(u_char *p)
{
return((p[1] << 8) | p[0]);
}
uint32_t
g_dec_le4(u_char *p)
{
return((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
}
void
g_enc_le4(u_char *p, uint32_t u)
{
p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
p[2] = (u >> 16) & 0xff;
p[3] = (u >> 24) & 0xff;
}

View File

@ -35,7 +35,6 @@
* $FreeBSD$
*/
#include <sys/param.h>
#ifndef _KERNEL
#include <stdio.h>
@ -60,6 +59,41 @@
#include <geom/geom_slice.h>
#define MBR_METHOD_NAME "MBR-method"
#define MBREXT_METHOD_NAME "MBREXT-method"
static void
g_dec_dos_partition(u_char *ptr, struct dos_partition *d)
{
d->dp_flag = ptr[0];
d->dp_shd = ptr[1];
d->dp_ssect = ptr[2];
d->dp_scyl = ptr[3];
d->dp_typ = ptr[4];
d->dp_ehd = ptr[5];
d->dp_esect = ptr[6];
d->dp_ecyl = ptr[7];
d->dp_start = g_dec_le4(ptr + 8);
d->dp_size = g_dec_le4(ptr + 12);
}
#if 0
static void
g_enc_dos_partition(u_char *ptr, struct dos_partition *d)
{
ptr[0] = d->dp_flag;
ptr[1] = d->dp_shd;
ptr[2] = d->dp_ssect;
ptr[3] = d->dp_scyl;
ptr[4] = d->dp_typ;
ptr[5] = d->dp_ehd;
ptr[6] = d->dp_esect;
ptr[7] = d->dp_ecyl;
g_enc_le4(ptr + 8, d->dp_start);
g_enc_le4(ptr + 12, d->dp_size);
}
#endif
struct g_mbr_softc {
int type [NDOSPART];
@ -140,6 +174,11 @@ g_mbr_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp, int i
struct g_mbr_softc *ms;
u_char *buf;
if (sizeof(struct dos_partition) != 16) {
printf("WARNING: struct dos_partition compiles to %d bytes, should be 16.\n",
sizeof(struct dos_partition));
return (NULL);
}
g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name);
g_topology_assert();
gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start);
@ -163,7 +202,10 @@ g_mbr_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp, int i
g_free(buf);
break;
}
bcopy(buf + DOSPARTOFF, dp, sizeof(dp));
for (i = 0; i < NDOSPART; i++)
g_dec_dos_partition(
buf + DOSPARTOFF + i * sizeof(struct dos_partition),
dp + i);
g_free(buf);
if (bcmp(dp, historical_bogus_partition_table,
sizeof historical_bogus_partition_table) == 0)
@ -209,3 +251,141 @@ static struct g_method g_mbr_method = {
};
DECLARE_GEOM_METHOD(g_mbr_method, g_mbr);
#define NDOSEXTPART 32
struct g_mbrext_softc {
int type [NDOSEXTPART];
};
static int
g_mbrext_start(struct bio *bp)
{
struct g_provider *pp;
struct g_geom *gp;
struct g_mbrext_softc *mp;
struct g_slicer *gsp;
int index;
pp = bp->bio_to;
index = pp->index;
gp = pp->geom;
gsp = gp->softc;
mp = gsp->softc;
if (bp->bio_cmd == BIO_GETATTR) {
if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
return (1);
}
return (0);
}
static void
g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
{
struct g_mbrext_softc *mp;
struct g_slicer *gsp;
g_slice_dumpconf(sb, indent, gp, cp, pp);
gsp = gp->softc;
mp = gsp->softc;
if (pp != NULL) {
sbuf_printf(sb, "%s<type>%d</type>\n",
indent, mp->type[pp->index]);
}
}
static void
g_mbrext_print(int i, struct dos_partition *dp)
{
g_hexdump(dp, sizeof(dp[0]));
printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
}
static struct g_geom *
g_mbrext_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp __unused, int insist __unused)
{
struct g_geom *gp;
struct g_consumer *cp;
struct g_provider *pp2;
int error, i, j, slice;
struct g_mbrext_softc *ms;
off_t off;
u_char *buf;
struct dos_partition dp[4];
g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
g_topology_assert();
if (strcmp(pp->geom->method->name, MBR_METHOD_NAME))
return (NULL);
gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
if (gp == NULL)
return (NULL);
g_topology_unlock();
gp->dumpconf = g_mbrext_dumpconf;
off = 0;
slice = 0;
while (1) { /* a trick to allow us to use break */
j = sizeof i;
error = g_io_getattr("MBR::type", cp, &j, &i, tp);
if (error || i != DOSPTYP_EXT)
break;
for (;;) {
buf = g_read_data(cp, off, DEV_BSIZE, &error);
if (buf == NULL || error != 0)
break;
if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
break;
for (i = 0; i < NDOSPART; i++)
g_dec_dos_partition(
buf + DOSPARTOFF + i * sizeof(struct dos_partition),
dp + i);
g_free(buf);
g_mbrext_print(0, dp);
g_mbrext_print(1, dp + 1);
if (dp[0].dp_flag == 0 && dp[0].dp_size != 0) {
pp2 = g_slice_addslice(gp, slice,
(((off_t)dp[0].dp_start) << 9ULL) + off,
((off_t)dp[0].dp_size) << 9ULL,
"%*.*s%d",
strlen(gp->name) - 1,
strlen(gp->name) - 1,
gp->name,
slice + 5);
ms->type[slice] = dp[0].dp_typ;
slice++;
g_error_provider(pp2, 0);
}
if (dp[1].dp_flag != 0)
break;
if (dp[1].dp_typ != DOSPTYP_EXT)
break;
if (dp[1].dp_size == 0)
break;
off = ((off_t)dp[1].dp_start) << 9ULL;
}
break;
}
g_topology_lock();
error = g_access_rel(cp, -1, 0, 0);
if (slice > 0)
return (gp);
g_topology_assert();
g_std_spoiled(cp);
g_topology_assert();
return (NULL);
}
static struct g_method g_mbrext_method = {
MBREXT_METHOD_NAME,
g_mbrext_taste,
g_slice_access,
g_slice_orphan,
NULL,
G_METHOD_INITSTUFF
};
DECLARE_GEOM_METHOD(g_mbrext_method, g_mbrext);

View File

@ -1,200 +0,0 @@
/*-
* Copyright (c) 2002 Poul-Henning Kamp
* Copyright (c) 2002 Networks Associates Technology, Inc.
* All rights reserved.
*
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
* and NAI Labs, the Security Research Division of Network Associates, Inc.
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
* DARPA CHATS research program.
*
* 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.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*
* $FreeBSD$
*/
#ifndef _KERNEL
#include <sys/param.h>
#ifndef _KERNEL
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/param.h>
#include <stdlib.h>
#include <err.h>
#else
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/bio.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#endif
#include <sys/errno.h>
#include <sys/disklabel.h>
#include <sys/sbuf.h>
#include <geom/geom.h>
#include <geom/geom_slice.h>
#define MBR_METHOD_NAME "MBR-method"
#define MBREXT_METHOD_NAME "MBREXT-method"
#define NDOSEXTPART 32
struct g_mbrext_softc {
int type [NDOSEXTPART];
};
static int
g_mbrext_start(struct bio *bp)
{
struct g_provider *pp;
struct g_geom *gp;
struct g_mbrext_softc *mp;
struct g_slicer *gsp;
int index;
pp = bp->bio_to;
index = pp->index;
gp = pp->geom;
gsp = gp->softc;
mp = gsp->softc;
if (bp->bio_cmd == BIO_GETATTR) {
if (g_haveattr_int(bp, "MBR::type", mp->type[index]))
return (1);
}
return (0);
}
static void
g_mbrext_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
{
struct g_mbrext_softc *mp;
struct g_slicer *gsp;
g_slice_dumpconf(sb, indent, gp, cp, pp);
gsp = gp->softc;
mp = gsp->softc;
if (pp != NULL) {
sbuf_printf(sb, "%s<type>%d</type>\n",
indent, mp->type[pp->index]);
}
}
static void
g_mbrext_print(int i, struct dos_partition *dp)
{
g_hexdump(dp, sizeof(dp[0]));
printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ);
printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
}
static struct g_geom *
g_mbrext_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp __unused, int insist __unused)
{
struct g_geom *gp;
struct g_consumer *cp;
struct g_provider *pp2;
int error, i, j, slice;
struct g_mbrext_softc *ms;
off_t off;
u_char *buf;
struct dos_partition dp[4];
g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name);
g_topology_assert();
if (strcmp(pp->geom->method->name, MBR_METHOD_NAME))
return (NULL);
gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start);
if (gp == NULL)
return (NULL);
g_topology_unlock();
gp->dumpconf = g_mbrext_dumpconf;
off = 0;
slice = 0;
while (1) { /* a trick to allow us to use break */
j = sizeof i;
error = g_io_getattr("MBR::type", cp, &j, &i, tp);
if (error || i != DOSPTYP_EXT)
break;
for (;;) {
buf = g_read_data(cp, off, DEV_BSIZE, &error);
if (buf == NULL || error != 0)
break;
if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
break;
bcopy(buf + DOSPARTOFF, dp, sizeof(dp));
g_free(buf);
g_mbrext_print(0, dp);
g_mbrext_print(1, dp + 1);
if (dp[0].dp_flag == 0 && dp[0].dp_size != 0) {
pp2 = g_slice_addslice(gp, slice,
(((off_t)dp[0].dp_start) << 9ULL) + off,
((off_t)dp[0].dp_size) << 9ULL,
"%*.*s%d",
strlen(gp->name) - 1,
strlen(gp->name) - 1,
gp->name,
slice + 5);
ms->type[slice] = dp[0].dp_typ;
slice++;
g_error_provider(pp2, 0);
}
if (dp[1].dp_flag != 0)
break;
if (dp[1].dp_typ != DOSPTYP_EXT)
break;
if (dp[1].dp_size == 0)
break;
off = ((off_t)dp[1].dp_start) << 9ULL;
}
break;
}
g_topology_lock();
error = g_access_rel(cp, -1, 0, 0);
if (slice > 0)
return (gp);
g_topology_assert();
g_std_spoiled(cp);
g_topology_assert();
return (NULL);
}
static struct g_method g_mbrext_method = {
MBREXT_METHOD_NAME,
g_mbrext_taste,
g_slice_access,
g_slice_orphan,
NULL,
G_METHOD_INITSTUFF
};
DECLARE_GEOM_METHOD(g_mbrext_method, g_mbrext);
#endif

View File

@ -59,19 +59,6 @@
#define BSD_METHOD_NAME "SUNLABEL-method"
static u_int
g_u16be(u_char *p)
{
return ((p[0] << 8) | p[1]);
}
static u_int
g_u32be(u_char *p)
{
return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
}
struct g_sunlabel_softc {
int foo;
};
@ -141,43 +128,43 @@ g_sunlabel_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp,
break;
/* The second last short is a magic number */
if (g_u16be(buf + 508) != 0xdabe)
if (g_dec_be2(buf + 508) != 0xdabe)
break;
/* The shortword parity of the entire thing must be even */
u = 0;
for (i = 0; i < 512; i += 2)
u ^= g_u16be(buf + i);
u ^= g_dec_be2(buf + i);
if (u != 0)
break;
if (bootverbose) {
g_hexdump(buf, 128);
for (i = 0; i < 8; i++) {
printf("part %d %u %u\n", i,
g_u32be(buf + 444 + i * 8),
g_u32be(buf + 448 + i * 8));
g_dec_be4(buf + 444 + i * 8),
g_dec_be4(buf + 448 + i * 8));
}
printf("v_version = %d\n", g_u32be(buf + 128));
printf("v_nparts = %d\n", g_u16be(buf + 140));
printf("v_version = %d\n", g_dec_be4(buf + 128));
printf("v_nparts = %d\n", g_dec_be2(buf + 140));
for (i = 0; i < 8; i++) {
printf("v_part[%d] = %d %d\n",
i, g_u16be(buf + 142 + i * 4),
g_u16be(buf + 144 + i * 4));
i, g_dec_be2(buf + 142 + i * 4),
g_dec_be2(buf + 144 + i * 4));
}
printf("v_sanity %x\n", g_u32be(buf + 186));
printf("v_version = %d\n", g_u32be(buf + 128));
printf("v_rpm %d\n", g_u16be(buf + 420));
printf("v_totalcyl %d\n", g_u16be(buf + 422));
printf("v_cyl %d\n", g_u16be(buf + 432));
printf("v_alt %d\n", g_u16be(buf + 434));
printf("v_head %d\n", g_u16be(buf + 436));
printf("v_sec %d\n", g_u16be(buf + 438));
printf("v_sanity %x\n", g_dec_be4(buf + 186));
printf("v_version = %d\n", g_dec_be4(buf + 128));
printf("v_rpm %d\n", g_dec_be2(buf + 420));
printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
printf("v_cyl %d\n", g_dec_be2(buf + 432));
printf("v_alt %d\n", g_dec_be2(buf + 434));
printf("v_head %d\n", g_dec_be2(buf + 436));
printf("v_sec %d\n", g_dec_be2(buf + 438));
}
csize = g_u16be(buf + 436) * g_u16be(buf + 438);
csize = g_dec_be2(buf + 436) * g_dec_be2(buf + 438);
for (i = 0; i < 8; i++) {
v = g_u32be(buf + 444 + i * 8);
u = g_u32be(buf + 448 + i * 8);
v = g_dec_be4(buf + 444 + i * 8);
u = g_dec_be4(buf + 448 + i * 8);
if (u == 0)
continue;
npart++;