xref: /freebsd/sbin/fdisk/fdisk_mbr_enc.c (revision 84b3c4547afa496b37e86c5e649e99237c8afc6e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/types.h>
40 #include <sys/diskmbr.h>
41 #include <sys/endian.h>
42 
43 #include "fdisk_mbr_enc.h"
44 
45 void
46 dos_partition_dec(void const *pp, struct dos_partition *d)
47 {
48 	unsigned char const *p = pp;
49 
50 	d->dp_flag = p[0];
51 	d->dp_shd = p[1];
52 	d->dp_ssect = p[2];
53 	d->dp_scyl = p[3];
54 	d->dp_typ = p[4];
55 	d->dp_ehd = p[5];
56 	d->dp_esect = p[6];
57 	d->dp_ecyl = p[7];
58 	d->dp_start = le32dec(p + 8);
59 	d->dp_size = le32dec(p + 12);
60 }
61 
62 void
63 dos_partition_enc(void *pp, struct dos_partition *d)
64 {
65 	unsigned char *p = pp;
66 
67 	p[0] = d->dp_flag;
68 	p[1] = d->dp_shd;
69 	p[2] = d->dp_ssect;
70 	p[3] = d->dp_scyl;
71 	p[4] = d->dp_typ;
72 	p[5] = d->dp_ehd;
73 	p[6] = d->dp_esect;
74 	p[7] = d->dp_ecyl;
75 	le32enc(p + 8, d->dp_start);
76 	le32enc(p + 12, d->dp_size);
77 }
78