geom_mbr_enc.c revision 116196
1163953Srrs/*-
2169382Srrs * Copyright (c) 2003 Poul-Henning Kamp
3163953Srrs * All rights reserved.
4163953Srrs *
5163953Srrs * Redistribution and use in source and binary forms, with or without
6163953Srrs * modification, are permitted provided that the following conditions
7163953Srrs * are met:
8163953Srrs * 1. Redistributions of source code must retain the above copyright
9163953Srrs *    notice, this list of conditions and the following disclaimer.
10163953Srrs * 2. Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in the
12163953Srrs *    documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15163953Srrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16163953Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17163953Srrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18163953Srrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19163953Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20163953Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21163953Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22163953Srrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23163953Srrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24163953Srrs * SUCH DAMAGE.
25163953Srrs *
26163953Srrs * Functions to encode or decode struct dos_partition into a bytestream
27163953Srrs * of correct endianess and packing.  These functions do no validation
28163953Srrs * or sanity checking, they only pack/unpack the fields correctly.
29163953Srrs *
30163953Srrs * NB!  This file must be usable both in kernel and userland.
31163953Srrs */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD: head/sys/geom/geom_mbr_enc.c 116196 2003-06-11 06:49:16Z obrien $");
35163953Srrs
36163953Srrs#include <sys/types.h>
37191845Szec#include <sys/diskmbr.h>
38188067Srrs#include <sys/endian.h>
39163953Srrs
40188067Srrsvoid
41163953Srrsdos_partition_dec(void const *pp, struct dos_partition *d)
42191891Srrs{
43188067Srrs	unsigned char const *p = pp;
44163953Srrs
45163953Srrs	d->dp_flag = p[0];
46188605Srrs	d->dp_shd = p[1];
47163953Srrs	d->dp_ssect = p[2];
48163953Srrs	d->dp_scyl = p[3];
49163953Srrs	d->dp_typ = p[4];
50163953Srrs	d->dp_ehd = p[5];
51163953Srrs	d->dp_esect = p[6];
52163953Srrs	d->dp_ecyl = p[7];
53163953Srrs	d->dp_start = le32dec(p + 8);
54163953Srrs	d->dp_size = le32dec(p + 12);
55168709Srrs}
56163953Srrs
57163953Srrsvoid
58163953Srrsdos_partition_enc(void *pp, struct dos_partition *d)
59163953Srrs{
60163953Srrs	unsigned char *p = pp;
61163953Srrs
62163953Srrs	p[0] = d->dp_flag;
63163953Srrs	p[1] = d->dp_shd;
64163953Srrs	p[2] = d->dp_ssect;
65163953Srrs	p[3] = d->dp_scyl;
66168709Srrs	p[4] = d->dp_typ;
67163953Srrs	p[5] = d->dp_ehd;
68163953Srrs	p[6] = d->dp_esect;
69163953Srrs	p[7] = d->dp_ecyl;
70163953Srrs	le32enc(p + 8, d->dp_start);
71163953Srrs	le32enc(p + 12, d->dp_size);
72163953Srrs}
73168709Srrs