xref: /freebsd/sys/geom/geom_bsd_enc.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/types.h>
49 #include <sys/endian.h>
50 #include <sys/disklabel.h>
51 #include <sys/errno.h>
52 #ifdef _KERNEL
53 #include <sys/systm.h>
54 #else
55 #include <string.h>
56 #endif
57 
58 void
59 bsd_partition_le_dec(u_char *ptr, struct partition *d)
60 {
61 	d->p_size = le32dec(ptr + 0);
62 	d->p_offset = le32dec(ptr + 4);
63 	d->p_fsize = le32dec(ptr + 8);
64 	d->p_fstype = ptr[12];
65 	d->p_frag = ptr[13];
66 	d->p_cpg = le16dec(ptr + 14);
67 }
68 
69 int
70 bsd_disklabel_le_dec(u_char *ptr, struct disklabel *d, int maxpart)
71 {
72 	int i;
73 	u_char *p, *pe;
74 	uint16_t sum;
75 
76 	d->d_magic = le32dec(ptr + 0);
77 	if (d->d_magic != DISKMAGIC)
78 		return(EINVAL);
79 
80 	d->d_magic2 = le32dec(ptr + 132);
81 	if (d->d_magic2 != DISKMAGIC) {
82 		return(EINVAL);
83 	}
84 
85 	d->d_npartitions = le16dec(ptr + 138);
86 	if (d->d_npartitions > maxpart) {
87 		return(EINVAL);
88 	}
89 
90 	pe = ptr + 148 + 16 * d->d_npartitions;
91 	sum = 0;
92 	for (p = ptr; p < pe; p += 2)
93 		sum ^= le16dec(p);
94 	if (sum != 0) {
95 		return(EINVAL);
96 	}
97 
98 	d->d_type = le16dec(ptr + 4);
99 	d->d_subtype = le16dec(ptr + 6);
100 	bcopy(ptr + 8, d->d_typename, 16);
101 	bcopy(ptr + 24, d->d_packname, 16);
102 	d->d_secsize = le32dec(ptr + 40);
103 	d->d_nsectors = le32dec(ptr + 44);
104 	d->d_ntracks = le32dec(ptr + 48);
105 	d->d_ncylinders = le32dec(ptr + 52);
106 	d->d_secpercyl = le32dec(ptr + 56);
107 	d->d_secperunit = le32dec(ptr + 60);
108 	d->d_sparespertrack = le16dec(ptr + 64);
109 	d->d_sparespercyl = le16dec(ptr + 66);
110 	d->d_acylinders = le32dec(ptr + 68);
111 	d->d_rpm = le16dec(ptr + 72);
112 	d->d_interleave = le16dec(ptr + 74);
113 	d->d_trackskew = le16dec(ptr + 76);
114 	d->d_cylskew = le16dec(ptr + 78);
115 	d->d_headswitch = le32dec(ptr + 80);
116 	d->d_trkseek = le32dec(ptr + 84);
117 	d->d_flags = le32dec(ptr + 88);
118 	d->d_drivedata[0] = le32dec(ptr + 92);
119 	d->d_drivedata[1] = le32dec(ptr + 96);
120 	d->d_drivedata[2] = le32dec(ptr + 100);
121 	d->d_drivedata[3] = le32dec(ptr + 104);
122 	d->d_drivedata[4] = le32dec(ptr + 108);
123 	d->d_spare[0] = le32dec(ptr + 112);
124 	d->d_spare[1] = le32dec(ptr + 116);
125 	d->d_spare[2] = le32dec(ptr + 120);
126 	d->d_spare[3] = le32dec(ptr + 124);
127 	d->d_spare[4] = le32dec(ptr + 128);
128 	d->d_checksum = le16dec(ptr + 136);
129 	d->d_npartitions = le16dec(ptr + 138);
130 	d->d_bbsize = le32dec(ptr + 140);
131 	d->d_sbsize = le32dec(ptr + 144);
132 	for (i = 0; i < d->d_npartitions; i++)
133 		bsd_partition_le_dec(ptr + 148 + 16 * i, &d->d_partitions[i]);
134 	return(0);
135 }
136 
137 void
138 bsd_partition_le_enc(u_char *ptr, struct partition *d)
139 {
140 	le32enc(ptr + 0, d->p_size);
141 	le32enc(ptr + 4, d->p_offset);
142 	le32enc(ptr + 8, d->p_fsize);
143 	ptr[12] = d->p_fstype;
144 	ptr[13] = d->p_frag;
145 	le16enc(ptr + 14, d->p_cpg);
146 }
147 
148 void
149 bsd_disklabel_le_enc(u_char *ptr, struct disklabel *d)
150 {
151 	int i;
152 	u_char *p, *pe;
153 	uint16_t sum;
154 
155 	le32enc(ptr + 0, d->d_magic);
156 	le16enc(ptr + 4, d->d_type);
157 	le16enc(ptr + 6, d->d_subtype);
158 	bcopy(d->d_typename, ptr + 8, 16);
159 	bcopy(d->d_packname, ptr + 24, 16);
160 	le32enc(ptr + 40, d->d_secsize);
161 	le32enc(ptr + 44, d->d_nsectors);
162 	le32enc(ptr + 48, d->d_ntracks);
163 	le32enc(ptr + 52, d->d_ncylinders);
164 	le32enc(ptr + 56, d->d_secpercyl);
165 	le32enc(ptr + 60, d->d_secperunit);
166 	le16enc(ptr + 64, d->d_sparespertrack);
167 	le16enc(ptr + 66, d->d_sparespercyl);
168 	le32enc(ptr + 68, d->d_acylinders);
169 	le16enc(ptr + 72, d->d_rpm);
170 	le16enc(ptr + 74, d->d_interleave);
171 	le16enc(ptr + 76, d->d_trackskew);
172 	le16enc(ptr + 78, d->d_cylskew);
173 	le32enc(ptr + 80, d->d_headswitch);
174 	le32enc(ptr + 84, d->d_trkseek);
175 	le32enc(ptr + 88, d->d_flags);
176 	le32enc(ptr + 92, d->d_drivedata[0]);
177 	le32enc(ptr + 96, d->d_drivedata[1]);
178 	le32enc(ptr + 100, d->d_drivedata[2]);
179 	le32enc(ptr + 104, d->d_drivedata[3]);
180 	le32enc(ptr + 108, d->d_drivedata[4]);
181 	le32enc(ptr + 112, d->d_spare[0]);
182 	le32enc(ptr + 116, d->d_spare[1]);
183 	le32enc(ptr + 120, d->d_spare[2]);
184 	le32enc(ptr + 124, d->d_spare[3]);
185 	le32enc(ptr + 128, d->d_spare[4]);
186 	le32enc(ptr + 132, d->d_magic2);
187 	le16enc(ptr + 136, 0);
188 	le16enc(ptr + 138, d->d_npartitions);
189 	le32enc(ptr + 140, d->d_bbsize);
190 	le32enc(ptr + 144, d->d_sbsize);
191 	for (i = 0; i < d->d_npartitions; i++)
192 		bsd_partition_le_enc(ptr + 148 + 16 * i, &d->d_partitions[i]);
193 	pe = ptr + 148 + 16 * d->d_npartitions;
194 	sum = 0;
195 	for (p = ptr; p < pe; p += 2)
196 		sum ^= le16dec(p);
197 	le16enc(ptr + 136, sum);
198 }
199