xref: /freebsd/sbin/fdisk/fdisk_mbr_enc.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Poul-Henning Kamp
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* Functions to encode or decode struct dos_partition into a bytestream
30  * of correct endianness and packing.  These functions do no validation
31  * or sanity checking, they only pack/unpack the fields correctly.
32  *
33  * NB!  This file must be usable both in kernel and userland.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/diskmbr.h>
38 #include <sys/endian.h>
39 
40 #include "fdisk_mbr_enc.h"
41 
42 void
43 dos_partition_dec(void const *pp, struct dos_partition *d)
44 {
45 	unsigned char const *p = pp;
46 
47 	d->dp_flag = p[0];
48 	d->dp_shd = p[1];
49 	d->dp_ssect = p[2];
50 	d->dp_scyl = p[3];
51 	d->dp_typ = p[4];
52 	d->dp_ehd = p[5];
53 	d->dp_esect = p[6];
54 	d->dp_ecyl = p[7];
55 	d->dp_start = le32dec(p + 8);
56 	d->dp_size = le32dec(p + 12);
57 }
58 
59 void
60 dos_partition_enc(void *pp, struct dos_partition *d)
61 {
62 	unsigned char *p = pp;
63 
64 	p[0] = d->dp_flag;
65 	p[1] = d->dp_shd;
66 	p[2] = d->dp_ssect;
67 	p[3] = d->dp_scyl;
68 	p[4] = d->dp_typ;
69 	p[5] = d->dp_ehd;
70 	p[6] = d->dp_esect;
71 	p[7] = d->dp_ecyl;
72 	le32enc(p + 8, d->dp_start);
73 	le32enc(p + 12, d->dp_size);
74 }
75