Move the functions for encoding decoding struct dos_partition into

a separate .c file so they can be used from userland as well.
This commit is contained in:
Poul-Henning Kamp 2003-04-12 08:34:40 +00:00
parent 5a1c2d4f62
commit 6e03422af0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=113389
5 changed files with 77 additions and 18 deletions

View File

@ -897,6 +897,7 @@ geom/geom_gpt.c optional geom_gpt
geom/geom_io.c standard
geom/geom_kern.c standard
geom/geom_mbr.c optional geom_mbr
geom/geom_mbr_enc.c optional geom_mbr
geom/geom_mirror.c optional geom_mirror
geom/geom_pc98.c optional geom_pc98
geom/geom_slice.c standard

View File

@ -148,6 +148,7 @@ dev/syscons/syscons.c optional sc
dev/syscons/sysmouse.c optional sc
geom/geom_bsd.c standard
geom/geom_mbr.c standard
geom/geom_mbr_enc.c standard
gnu/i386/fpemul/div_small.s optional gpl_math_emulate \
warning "kernel contains GPL contaminated math emulator"
gnu/i386/fpemul/errors.c optional gpl_math_emulate

View File

@ -95,6 +95,7 @@ dev/syscons/sysmouse.c optional sc
geom/geom_bsd.c standard
geom/geom_gpt.c standard
geom/geom_mbr.c standard
geom/geom_mbr_enc.c standard
isa/atkbd_isa.c optional atkbd
isa/atkbdc_isa.c optional atkbdc
isa/fd.c optional fdc

View File

@ -85,22 +85,6 @@ g_mbr_print(int i, struct dos_partition *dp)
printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size);
}
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 = le32dec(ptr + 8);
d->dp_size = le32dec(ptr + 12);
}
struct g_mbr_softc {
int type [NDOSPART];
u_int sectorsize;
@ -121,7 +105,7 @@ g_mbr_modify(struct g_geom *gp, struct g_mbr_softc *ms, u_char *sec0)
dp = ndp;
for (i = 0; i < NDOSPART; i++) {
g_dec_dos_partition(
dos_partition_dec(
sec0 + DOSPARTOFF + i * sizeof(struct dos_partition),
dp + i);
if (bootverbose)
@ -428,7 +412,7 @@ g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa)
break;
for (i = 0; i < NDOSPART; i++)
g_dec_dos_partition(
dos_partition_dec(
buf + DOSPARTOFF +
i * sizeof(struct dos_partition), dp + i);
g_free(buf);

72
sys/geom/geom_mbr_enc.c Normal file
View File

@ -0,0 +1,72 @@
/*-
* Copyright (c) 2003 Poul-Henning Kamp
* 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 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$
*
* Functions to encode or decode struct dos_partition into a bytestream
* of correct endianess and packing. These functions do no validation
* or sanity checking, they only pack/unpack the fields correctly.
*
* NB! This file must be usable both in kernel and userland.
*
*/
#include <sys/types.h>
#include <sys/diskmbr.h>
#include <sys/endian.h>
void
dos_partition_dec(void const *pp, struct dos_partition *d)
{
unsigned char const *p = pp;
d->dp_flag = p[0];
d->dp_shd = p[1];
d->dp_ssect = p[2];
d->dp_scyl = p[3];
d->dp_typ = p[4];
d->dp_ehd = p[5];
d->dp_esect = p[6];
d->dp_ecyl = p[7];
d->dp_start = le32dec(p + 8);
d->dp_size = le32dec(p + 12);
}
void
dos_partition_enc(void *pp, struct dos_partition *d)
{
unsigned char *p = pp;
p[0] = d->dp_flag;
p[1] = d->dp_shd;
p[2] = d->dp_ssect;
p[3] = d->dp_scyl;
p[4] = d->dp_typ;
p[5] = d->dp_ehd;
p[6] = d->dp_esect;
p[7] = d->dp_ecyl;
le32enc(p + 8, d->dp_start);
le32enc(p + 12, d->dp_size);
}