1113389Sphk/*-
2113389Sphk * Copyright (c) 2003 Poul-Henning Kamp
3113389Sphk * All rights reserved.
4113389Sphk *
5113389Sphk * Redistribution and use in source and binary forms, with or without
6113389Sphk * modification, are permitted provided that the following conditions
7113389Sphk * are met:
8113389Sphk * 1. Redistributions of source code must retain the above copyright
9113389Sphk *    notice, this list of conditions and the following disclaimer.
10113389Sphk * 2. Redistributions in binary form must reproduce the above copyright
11113389Sphk *    notice, this list of conditions and the following disclaimer in the
12113389Sphk *    documentation and/or other materials provided with the distribution.
13113389Sphk *
14113389Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15113389Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16113389Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17113389Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18113389Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19113389Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20113389Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21113389Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22113389Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23113389Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24113389Sphk * SUCH DAMAGE.
25139778Simp */
26139778Simp
27139778Simp/* Functions to encode or decode struct dos_partition into a bytestream
28113389Sphk * of correct endianess and packing.  These functions do no validation
29113389Sphk * or sanity checking, they only pack/unpack the fields correctly.
30113389Sphk *
31113389Sphk * NB!  This file must be usable both in kernel and userland.
32113389Sphk */
33113389Sphk
34116196Sobrien#include <sys/cdefs.h>
35116196Sobrien__FBSDID("$FreeBSD$");
36116196Sobrien
37113389Sphk#include <sys/types.h>
38113389Sphk#include <sys/diskmbr.h>
39113389Sphk#include <sys/endian.h>
40113389Sphk
41113389Sphkvoid
42113389Sphkdos_partition_dec(void const *pp, struct dos_partition *d)
43113389Sphk{
44113389Sphk	unsigned char const *p = pp;
45113389Sphk
46113389Sphk	d->dp_flag = p[0];
47113389Sphk	d->dp_shd = p[1];
48113389Sphk	d->dp_ssect = p[2];
49113389Sphk	d->dp_scyl = p[3];
50113389Sphk	d->dp_typ = p[4];
51113389Sphk	d->dp_ehd = p[5];
52113389Sphk	d->dp_esect = p[6];
53113389Sphk	d->dp_ecyl = p[7];
54113389Sphk	d->dp_start = le32dec(p + 8);
55113389Sphk	d->dp_size = le32dec(p + 12);
56113389Sphk}
57113389Sphk
58113389Sphkvoid
59113389Sphkdos_partition_enc(void *pp, struct dos_partition *d)
60113389Sphk{
61113389Sphk	unsigned char *p = pp;
62113389Sphk
63113389Sphk	p[0] = d->dp_flag;
64113389Sphk	p[1] = d->dp_shd;
65113389Sphk	p[2] = d->dp_ssect;
66113389Sphk	p[3] = d->dp_scyl;
67113389Sphk	p[4] = d->dp_typ;
68113389Sphk	p[5] = d->dp_ehd;
69113389Sphk	p[6] = d->dp_esect;
70113389Sphk	p[7] = d->dp_ecyl;
71113389Sphk	le32enc(p + 8, d->dp_start);
72113389Sphk	le32enc(p + 12, d->dp_size);
73113389Sphk}
74