]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/sunlabel/sunlabel_enc.c
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / sbin / sunlabel / sunlabel_enc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Jake Burkholder
5  * Copyright (c) 2003 Poul-Henning Kamp
6  * Copyright (c) 2004,2005 Joerg Wunsch
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 /* Functions to encode or decode struct sun_disklabel into a bytestream
31  * of correct endianness and packing.
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/endian.h>
41 #include <sys/errno.h>
42 #ifdef _KERNEL
43 #include <sys/systm.h>
44 #else
45 #include <string.h>
46 #endif
47 #include "sun_disklabel.h"
48
49 #define SL_TEXT         0x0
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
56 #define SL_RPM          0x1a4
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
64 #define SL_PART         0x1bc
65 #define SL_MAGIC        0x1fc
66 #define SL_CKSUM        0x1fe
67
68 #define SDKP_CYLOFFSET  0
69 #define SDKP_NSECTORS   0x4
70 #define SDKP_SIZEOF     0x8     /* size of a partition entry */
71
72 #define SVTOC_TAG       0
73 #define SVTOC_FLAG      0x2
74 #define SVTOC_SIZEOF    0x4     /* size of a VTOC tag/flag entry */
75
76 /*
77  * Decode the relevant fields of a sun disk label, and return zero if the
78  * magic and checksum works out OK.
79  */
80 int
81 sunlabel_dec(void const *pp, struct sun_disklabel *sl)
82 {
83         const uint8_t *p;
84         size_t i;
85         u_int u;
86         uint32_t vtocsane;
87         uint16_t npart;
88
89         p = pp;
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);
105         }
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) {
110                 /*
111                  * Seems we've got SVR4-compatible VTOC information
112                  * as well, decode it.
113                  */
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,
117                     SUN_VOLNAME_LEN);
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);
124                 }
125         }
126         for (i = u = 0; i < SUN_SIZE; i += 2)
127                 u ^= be16dec(p + i);
128         if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
129                 return (0);
130         else
131                 return (EINVAL);
132 }
133
134 /*
135  * Encode the relevant fields into a sun disklabel, compute new checksum.
136  */
137 void
138 sunlabel_enc(void *pp, struct sun_disklabel *sl)
139 {
140         uint8_t *p;
141         size_t i;
142         u_int u;
143
144         p = pp;
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);
160         }
161         be16enc(p + SL_MAGIC, sl->sl_magic);
162         if (sl->sl_vtoc_sane == SUN_VTOC_SANE
163             && sl->sl_vtoc_nparts == SUN_NPART) {
164                 /*
165                  * Write SVR4-compatible VTOC elements.
166                  */
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,
170                     SUN_VOLNAME_LEN);
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)
174                                 + SVTOC_TAG,
175                                 sl->sl_vtoc_map[i].svtoc_tag);
176                         be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
177                                 + SVTOC_FLAG,
178                                 sl->sl_vtoc_map[i].svtoc_flag);
179                 }
180         }
181         for (i = u = 0; i < SUN_SIZE; i += 2)
182                 u ^= be16dec(p + i);
183         be16enc(p + SL_CKSUM, u);
184 }