xref: /freebsd/sys/geom/geom_bsd_enc.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*
39  * Functions to encode and decode struct disklabel and struct partition into
40  * a bytestream of little endianness and correct packing.
41  *
42  * NB!  This file must be usable both in kernel and userland.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/endian.h>
47 #include <sys/disklabel.h>
48 #include <sys/errno.h>
49 #ifdef _KERNEL
50 #include <sys/systm.h>
51 #else
52 #include <string.h>
53 #endif
54 
55 void
56 bsd_partition_le_dec(u_char *ptr, struct partition *d)
57 {
58 	d->p_size = le32dec(ptr + 0);
59 	d->p_offset = le32dec(ptr + 4);
60 	d->p_fsize = le32dec(ptr + 8);
61 	d->p_fstype = ptr[12];
62 	d->p_frag = ptr[13];
63 	d->p_cpg = le16dec(ptr + 14);
64 }
65 
66 int
67 bsd_disklabel_le_dec(u_char *ptr, struct disklabel *d, int maxpart)
68 {
69 	int i;
70 	u_char *p, *pe;
71 	uint16_t sum;
72 
73 	d->d_magic = le32dec(ptr + 0);
74 	if (d->d_magic != DISKMAGIC)
75 		return(EINVAL);
76 
77 	d->d_magic2 = le32dec(ptr + 132);
78 	if (d->d_magic2 != DISKMAGIC) {
79 		return(EINVAL);
80 	}
81 
82 	d->d_npartitions = le16dec(ptr + 138);
83 	if (d->d_npartitions > maxpart) {
84 		return(EINVAL);
85 	}
86 
87 	pe = ptr + 148 + 16 * d->d_npartitions;
88 	sum = 0;
89 	for (p = ptr; p < pe; p += 2)
90 		sum ^= le16dec(p);
91 	if (sum != 0) {
92 		return(EINVAL);
93 	}
94 
95 	d->d_type = le16dec(ptr + 4);
96 	d->d_subtype = le16dec(ptr + 6);
97 	bcopy(ptr + 8, d->d_typename, 16);
98 	bcopy(ptr + 24, d->d_packname, 16);
99 	d->d_secsize = le32dec(ptr + 40);
100 	d->d_nsectors = le32dec(ptr + 44);
101 	d->d_ntracks = le32dec(ptr + 48);
102 	d->d_ncylinders = le32dec(ptr + 52);
103 	d->d_secpercyl = le32dec(ptr + 56);
104 	d->d_secperunit = le32dec(ptr + 60);
105 	d->d_sparespertrack = le16dec(ptr + 64);
106 	d->d_sparespercyl = le16dec(ptr + 66);
107 	d->d_acylinders = le32dec(ptr + 68);
108 	d->d_rpm = le16dec(ptr + 72);
109 	d->d_interleave = le16dec(ptr + 74);
110 	d->d_trackskew = le16dec(ptr + 76);
111 	d->d_cylskew = le16dec(ptr + 78);
112 	d->d_headswitch = le32dec(ptr + 80);
113 	d->d_trkseek = le32dec(ptr + 84);
114 	d->d_flags = le32dec(ptr + 88);
115 	d->d_drivedata[0] = le32dec(ptr + 92);
116 	d->d_drivedata[1] = le32dec(ptr + 96);
117 	d->d_drivedata[2] = le32dec(ptr + 100);
118 	d->d_drivedata[3] = le32dec(ptr + 104);
119 	d->d_drivedata[4] = le32dec(ptr + 108);
120 	d->d_spare[0] = le32dec(ptr + 112);
121 	d->d_spare[1] = le32dec(ptr + 116);
122 	d->d_spare[2] = le32dec(ptr + 120);
123 	d->d_spare[3] = le32dec(ptr + 124);
124 	d->d_spare[4] = le32dec(ptr + 128);
125 	d->d_checksum = le16dec(ptr + 136);
126 	d->d_npartitions = le16dec(ptr + 138);
127 	d->d_bbsize = le32dec(ptr + 140);
128 	d->d_sbsize = le32dec(ptr + 144);
129 	for (i = 0; i < d->d_npartitions; i++)
130 		bsd_partition_le_dec(ptr + 148 + 16 * i, &d->d_partitions[i]);
131 	return(0);
132 }
133 
134 void
135 bsd_partition_le_enc(u_char *ptr, struct partition *d)
136 {
137 	le32enc(ptr + 0, d->p_size);
138 	le32enc(ptr + 4, d->p_offset);
139 	le32enc(ptr + 8, d->p_fsize);
140 	ptr[12] = d->p_fstype;
141 	ptr[13] = d->p_frag;
142 	le16enc(ptr + 14, d->p_cpg);
143 }
144 
145 void
146 bsd_disklabel_le_enc(u_char *ptr, struct disklabel *d)
147 {
148 	int i;
149 	u_char *p, *pe;
150 	uint16_t sum;
151 
152 	le32enc(ptr + 0, d->d_magic);
153 	le16enc(ptr + 4, d->d_type);
154 	le16enc(ptr + 6, d->d_subtype);
155 	bcopy(d->d_typename, ptr + 8, 16);
156 	bcopy(d->d_packname, ptr + 24, 16);
157 	le32enc(ptr + 40, d->d_secsize);
158 	le32enc(ptr + 44, d->d_nsectors);
159 	le32enc(ptr + 48, d->d_ntracks);
160 	le32enc(ptr + 52, d->d_ncylinders);
161 	le32enc(ptr + 56, d->d_secpercyl);
162 	le32enc(ptr + 60, d->d_secperunit);
163 	le16enc(ptr + 64, d->d_sparespertrack);
164 	le16enc(ptr + 66, d->d_sparespercyl);
165 	le32enc(ptr + 68, d->d_acylinders);
166 	le16enc(ptr + 72, d->d_rpm);
167 	le16enc(ptr + 74, d->d_interleave);
168 	le16enc(ptr + 76, d->d_trackskew);
169 	le16enc(ptr + 78, d->d_cylskew);
170 	le32enc(ptr + 80, d->d_headswitch);
171 	le32enc(ptr + 84, d->d_trkseek);
172 	le32enc(ptr + 88, d->d_flags);
173 	le32enc(ptr + 92, d->d_drivedata[0]);
174 	le32enc(ptr + 96, d->d_drivedata[1]);
175 	le32enc(ptr + 100, d->d_drivedata[2]);
176 	le32enc(ptr + 104, d->d_drivedata[3]);
177 	le32enc(ptr + 108, d->d_drivedata[4]);
178 	le32enc(ptr + 112, d->d_spare[0]);
179 	le32enc(ptr + 116, d->d_spare[1]);
180 	le32enc(ptr + 120, d->d_spare[2]);
181 	le32enc(ptr + 124, d->d_spare[3]);
182 	le32enc(ptr + 128, d->d_spare[4]);
183 	le32enc(ptr + 132, d->d_magic2);
184 	le16enc(ptr + 136, 0);
185 	le16enc(ptr + 138, d->d_npartitions);
186 	le32enc(ptr + 140, d->d_bbsize);
187 	le32enc(ptr + 144, d->d_sbsize);
188 	for (i = 0; i < d->d_npartitions; i++)
189 		bsd_partition_le_enc(ptr + 148 + 16 * i, &d->d_partitions[i]);
190 	pe = ptr + 148 + 16 * d->d_npartitions;
191 	sum = 0;
192 	for (p = ptr; p < pe; p += 2)
193 		sum ^= le16dec(p);
194 	le16enc(ptr + 136, sum);
195 }
196