2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Jake Burkholder
5 * Copyright (c) 2003 Poul-Henning Kamp
6 * Copyright (c) 2004,2005 Joerg Wunsch
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Functions to encode or decode struct sun_disklabel into a bytestream
31 * of correct endianness and packing.
33 * NB! This file must be usable both in kernel and userland.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
40 #include <sys/endian.h>
41 #include <sys/errno.h>
42 #include <sys/sun_disklabel.h>
44 #include <sys/systm.h>
50 #define SL_TEXT_SIZEOF 0x80
51 #define SL_VTOC_VERS 0x80
52 #define SL_VTOC_VOLNAME 0x84
53 #define SL_VTOC_NPART 0x8c
54 #define SL_VTOC_MAP 0x8e
55 #define SL_VTOC_SANITY 0xbc
57 #define SL_PCYLINDERS 0x1a6
58 #define SL_SPARESPERCYL 0x1a8
59 #define SL_INTERLEAVE 0x1ae
60 #define SL_NCYLINDERS 0x1b0
61 #define SL_ACYLINDERS 0x1b2
62 #define SL_NTRACKS 0x1b4
63 #define SL_NSECTORS 0x1b6
65 #define SL_MAGIC 0x1fc
66 #define SL_CKSUM 0x1fe
68 #define SDKP_CYLOFFSET 0
69 #define SDKP_NSECTORS 0x4
70 #define SDKP_SIZEOF 0x8 /* size of a partition entry */
73 #define SVTOC_FLAG 0x2
74 #define SVTOC_SIZEOF 0x4 /* size of a VTOC tag/flag entry */
77 * Decode the relevant fields of a sun disk label, and return zero if the
78 * magic and checksum works out OK.
81 sunlabel_dec(void const *pp, struct sun_disklabel *sl)
90 for (i = 0; i < sizeof(sl->sl_text); i++)
91 sl->sl_text[i] = p[SL_TEXT + i];
92 sl->sl_rpm = be16dec(p + SL_RPM);
93 sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS);
94 sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL);
95 sl->sl_interleave = be16dec(p + SL_INTERLEAVE);
96 sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS);
97 sl->sl_acylinders = be16dec(p + SL_ACYLINDERS);
98 sl->sl_ntracks = be16dec(p + SL_NTRACKS);
99 sl->sl_nsectors = be16dec(p + SL_NSECTORS);
100 for (i = 0; i < SUN_NPART; i++) {
101 sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART +
102 (i * SDKP_SIZEOF) + SDKP_CYLOFFSET);
103 sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART +
104 (i * SDKP_SIZEOF) + SDKP_NSECTORS);
106 sl->sl_magic = be16dec(p + SL_MAGIC);
107 vtocsane = be32dec(p + SL_VTOC_SANITY);
108 npart = be16dec(p + SL_VTOC_NPART);
109 if (vtocsane == SUN_VTOC_SANE && npart == SUN_NPART) {
111 * Seems we've got SVR4-compatible VTOC information
112 * as well, decode it.
114 sl->sl_vtoc_sane = vtocsane;
115 sl->sl_vtoc_vers = be32dec(p + SL_VTOC_VERS);
116 memcpy(sl->sl_vtoc_volname, p + SL_VTOC_VOLNAME,
118 sl->sl_vtoc_nparts = SUN_NPART;
119 for (i = 0; i < SUN_NPART; i++) {
120 sl->sl_vtoc_map[i].svtoc_tag = be16dec(p +
121 SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_TAG);
122 sl->sl_vtoc_map[i].svtoc_flag = be16dec(p +
123 SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_FLAG);
126 for (i = u = 0; i < SUN_SIZE; i += 2)
128 if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
135 * Encode the relevant fields into a sun disklabel, compute new checksum.
138 sunlabel_enc(void *pp, struct sun_disklabel *sl)
145 for (i = 0; i < SL_TEXT_SIZEOF; i++)
146 p[SL_TEXT + i] = sl->sl_text[i];
147 be16enc(p + SL_RPM, sl->sl_rpm);
148 be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
149 be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
150 be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
151 be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
152 be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
153 be16enc(p + SL_NTRACKS, sl->sl_ntracks);
154 be16enc(p + SL_NSECTORS, sl->sl_nsectors);
155 for (i = 0; i < SUN_NPART; i++) {
156 be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
157 sl->sl_part[i].sdkp_cyloffset);
158 be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
159 sl->sl_part[i].sdkp_nsectors);
161 be16enc(p + SL_MAGIC, sl->sl_magic);
162 if (sl->sl_vtoc_sane == SUN_VTOC_SANE
163 && sl->sl_vtoc_nparts == SUN_NPART) {
165 * Write SVR4-compatible VTOC elements.
167 be32enc(p + SL_VTOC_VERS, sl->sl_vtoc_vers);
168 be32enc(p + SL_VTOC_SANITY, SUN_VTOC_SANE);
169 memcpy(p + SL_VTOC_VOLNAME, sl->sl_vtoc_volname,
171 be16enc(p + SL_VTOC_NPART, SUN_NPART);
172 for (i = 0; i < SUN_NPART; i++) {
173 be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
175 sl->sl_vtoc_map[i].svtoc_tag);
176 be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
178 sl->sl_vtoc_map[i].svtoc_flag);
181 for (i = u = 0; i < SUN_SIZE; i += 2)
183 be16enc(p + SL_CKSUM, u);