geom_sunlabel_enc.c revision 116196
1113813Sphk/*-
2113813Sphk * Copyright (c) 2003 Jake Burkholder
3113813Sphk * Copyright (c) 2003 Poul-Henning Kamp
4113813Sphk * All rights reserved.
5113813Sphk *
6113813Sphk * Redistribution and use in source and binary forms, with or without
7113813Sphk * modification, are permitted provided that the following conditions
8113813Sphk * are met:
9113813Sphk * 1. Redistributions of source code must retain the above copyright
10113813Sphk *    notice, this list of conditions and the following disclaimer.
11113813Sphk * 2. Redistributions in binary form must reproduce the above copyright
12113813Sphk *    notice, this list of conditions and the following disclaimer in the
13113813Sphk *    documentation and/or other materials provided with the distribution.
14113813Sphk *
15113813Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16113813Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17113813Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18113813Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19113813Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20113813Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21113813Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22113813Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23113813Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24113813Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25113813Sphk * SUCH DAMAGE.
26113813Sphk *
27113813Sphk * Functions to encode or decode struct sun_disklabel into a bytestream
28113813Sphk * of correct endianess and packing.
29113813Sphk *
30113813Sphk * NB!  This file must be usable both in kernel and userland.
31113813Sphk */
32113813Sphk
33116196Sobrien#include <sys/cdefs.h>
34116196Sobrien__FBSDID("$FreeBSD: head/sys/geom/geom_sunlabel_enc.c 116196 2003-06-11 06:49:16Z obrien $");
35116196Sobrien
36113813Sphk#include <sys/types.h>
37113813Sphk#include <sys/endian.h>
38113813Sphk#include <sys/errno.h>
39113813Sphk#include <sys/sun_disklabel.h>
40113813Sphk
41113813Sphk#define	SL_TEXT		0x0
42113813Sphk#define	SL_TEXT_SIZEOF	0x80
43113813Sphk#define	SL_RPM		0x1a4
44113813Sphk#define	SL_PCYLINDERS	0x1a6
45113813Sphk#define	SL_SPARESPERCYL	0x1a8
46113813Sphk#define	SL_INTERLEAVE	0x1ae
47113813Sphk#define	SL_NCYLINDERS	0x1b0
48113813Sphk#define	SL_ACYLINDERS	0x1b2
49113813Sphk#define	SL_NTRACKS	0x1b4
50113813Sphk#define	SL_NSECTORS	0x1b6
51113813Sphk#define	SL_PART		0x1bc
52113813Sphk#define	SL_MAGIC	0x1fc
53113813Sphk#define	SL_CKSUM	0x1fe
54113813Sphk
55113813Sphk#define	SDKP_CYLOFFSET	0
56113813Sphk#define	SDKP_NSECTORS	0x4
57113813Sphk#define	SDKP_SIZEOF	0x8
58113813Sphk
59113813Sphk/*
60113813Sphk * Decode the relevant fields of a sun disk label, and return zero if the
61113813Sphk * magic and checksum works out OK.
62113813Sphk */
63113813Sphkint
64113813Sphksunlabel_dec(void const *pp, struct sun_disklabel *sl)
65113813Sphk{
66113813Sphk	const uint8_t *p;
67113813Sphk	size_t i;
68113813Sphk	u_int u;
69113813Sphk
70113813Sphk	p = pp;
71113813Sphk	for (i = 0; i < sizeof(sl->sl_text); i++)
72113813Sphk		sl->sl_text[i] = p[SL_TEXT + i];
73113813Sphk	sl->sl_rpm = be16dec(p + SL_RPM);
74113813Sphk	sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS);
75113813Sphk	sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL);
76113813Sphk	sl->sl_interleave = be16dec(p + SL_INTERLEAVE);
77113813Sphk	sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS);
78113813Sphk	sl->sl_acylinders = be16dec(p + SL_ACYLINDERS);
79113813Sphk	sl->sl_ntracks = be16dec(p + SL_NTRACKS);
80113813Sphk	sl->sl_nsectors = be16dec(p + SL_NSECTORS);
81113818Sphk	for (i = 0; i < SUN_NPART; i++) {
82113813Sphk		sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART +
83113813Sphk		    (i * SDKP_SIZEOF) + SDKP_CYLOFFSET);
84113813Sphk		sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART +
85113813Sphk		    (i * SDKP_SIZEOF) + SDKP_NSECTORS);
86113813Sphk	}
87113813Sphk	sl->sl_magic = be16dec(p + SL_MAGIC);
88113818Sphk	for (i = u = 0; i < SUN_SIZE; i += 2)
89113813Sphk		u ^= be16dec(p + i);
90113813Sphk	if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
91113813Sphk		return (0);
92113813Sphk	else
93113813Sphk		return (EINVAL);
94113813Sphk}
95113813Sphk
96113813Sphk/*
97113813Sphk * Encode the relevant fields into a sun disklabel, compute new checksum.
98113813Sphk */
99113813Sphkvoid
100113813Sphksunlabel_enc(void *pp, struct sun_disklabel *sl)
101113813Sphk{
102113813Sphk	uint8_t *p;
103113813Sphk	size_t i;
104113813Sphk	u_int u;
105113813Sphk
106113813Sphk	p = pp;
107113813Sphk	for (i = 0; i < SL_TEXT_SIZEOF; i++)
108113813Sphk		p[SL_TEXT + i] = sl->sl_text[i];
109113813Sphk	be16enc(p + SL_RPM, sl->sl_rpm);
110113813Sphk	be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
111113813Sphk	be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
112113813Sphk	be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
113113813Sphk	be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
114113813Sphk	be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
115113813Sphk	be16enc(p + SL_NTRACKS, sl->sl_ntracks);
116113813Sphk	be16enc(p + SL_NSECTORS, sl->sl_nsectors);
117113818Sphk	for (i = 0; i < SUN_NPART; i++) {
118113813Sphk		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
119113813Sphk		    sl->sl_part[i].sdkp_cyloffset);
120113813Sphk		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
121113813Sphk		    sl->sl_part[i].sdkp_nsectors);
122113813Sphk	}
123113813Sphk	be16enc(p + SL_MAGIC, sl->sl_magic);
124113818Sphk	for (i = u = 0; i < SUN_SIZE; i += 2)
125113813Sphk		u ^= be16dec(p + i);
126113813Sphk	be16enc(p + SL_CKSUM, u);
127113813Sphk}
128