geom_sunlabel_enc.c revision 113818
1/*-
2 * Copyright (c) 2003 Jake Burkholder
3 * Copyright (c) 2003 Poul-Henning Kamp
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/geom/geom_sunlabel_enc.c 113818 2003-04-21 19:42:36Z phk $
28 *
29 * Functions to encode or decode struct sun_disklabel into a bytestream
30 * of correct endianess and packing.
31 *
32 * NB!  This file must be usable both in kernel and userland.
33 *
34 */
35
36#include <sys/types.h>
37#include <sys/endian.h>
38#include <sys/errno.h>
39#include <sys/sun_disklabel.h>
40
41#define	SL_TEXT		0x0
42#define	SL_TEXT_SIZEOF	0x80
43#define	SL_RPM		0x1a4
44#define	SL_PCYLINDERS	0x1a6
45#define	SL_SPARESPERCYL	0x1a8
46#define	SL_INTERLEAVE	0x1ae
47#define	SL_NCYLINDERS	0x1b0
48#define	SL_ACYLINDERS	0x1b2
49#define	SL_NTRACKS	0x1b4
50#define	SL_NSECTORS	0x1b6
51#define	SL_PART		0x1bc
52#define	SL_MAGIC	0x1fc
53#define	SL_CKSUM	0x1fe
54
55#define	SDKP_CYLOFFSET	0
56#define	SDKP_NSECTORS	0x4
57#define	SDKP_SIZEOF	0x8
58
59/*
60 * Decode the relevant fields of a sun disk label, and return zero if the
61 * magic and checksum works out OK.
62 */
63int
64sunlabel_dec(void const *pp, struct sun_disklabel *sl)
65{
66	const uint8_t *p;
67	size_t i;
68	u_int u;
69
70	p = pp;
71	for (i = 0; i < sizeof(sl->sl_text); i++)
72		sl->sl_text[i] = p[SL_TEXT + i];
73	sl->sl_rpm = be16dec(p + SL_RPM);
74	sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS);
75	sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL);
76	sl->sl_interleave = be16dec(p + SL_INTERLEAVE);
77	sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS);
78	sl->sl_acylinders = be16dec(p + SL_ACYLINDERS);
79	sl->sl_ntracks = be16dec(p + SL_NTRACKS);
80	sl->sl_nsectors = be16dec(p + SL_NSECTORS);
81	for (i = 0; i < SUN_NPART; i++) {
82		sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART +
83		    (i * SDKP_SIZEOF) + SDKP_CYLOFFSET);
84		sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART +
85		    (i * SDKP_SIZEOF) + SDKP_NSECTORS);
86	}
87	sl->sl_magic = be16dec(p + SL_MAGIC);
88	for (i = u = 0; i < SUN_SIZE; i += 2)
89		u ^= be16dec(p + i);
90	if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
91		return (0);
92	else
93		return (EINVAL);
94}
95
96/*
97 * Encode the relevant fields into a sun disklabel, compute new checksum.
98 */
99void
100sunlabel_enc(void *pp, struct sun_disklabel *sl)
101{
102	uint8_t *p;
103	size_t i;
104	u_int u;
105
106	p = pp;
107	for (i = 0; i < SL_TEXT_SIZEOF; i++)
108		p[SL_TEXT + i] = sl->sl_text[i];
109	be16enc(p + SL_RPM, sl->sl_rpm);
110	be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
111	be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
112	be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
113	be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
114	be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
115	be16enc(p + SL_NTRACKS, sl->sl_ntracks);
116	be16enc(p + SL_NSECTORS, sl->sl_nsectors);
117	for (i = 0; i < SUN_NPART; i++) {
118		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
119		    sl->sl_part[i].sdkp_cyloffset);
120		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
121		    sl->sl_part[i].sdkp_nsectors);
122	}
123	be16enc(p + SL_MAGIC, sl->sl_magic);
124	for (i = u = 0; i < SUN_SIZE; i += 2)
125		u ^= be16dec(p + i);
126	be16enc(p + SL_CKSUM, u);
127}
128