geom_sunlabel.c revision 113432
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_sunlabel.c 113432 2003-04-13 09:02:06Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/conf.h>
44#include <sys/bio.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <geom/geom.h>
49#include <geom/geom_slice.h>
50#include <machine/endian.h>
51
52#define SUNLABEL_CLASS_NAME "SUN"
53
54struct g_sunlabel_softc {
55	int sectorsize;
56	int nheads;
57	int nsects;
58	int nalt;
59};
60
61static int
62g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
63{
64	int i, error;
65	u_int u, v, csize;
66
67	/* The second last short is a magic number */
68	if (be16dec(sec0 + 508) != 0xdabe)
69		return (EBUSY);
70
71	/* The shortword parity of the entire thing must be even */
72	u = 0;
73	for (i = 0; i < 512; i += 2)
74		u ^= be16dec(sec0 + i);
75	if (u != 0)
76		return(EBUSY);
77
78	csize = be16dec(sec0 + 436) * be16dec(sec0 + 438);
79
80	for (i = 0; i < 8; i++) {
81		v = be32dec(sec0 + 444 + i * 8);
82		u = be32dec(sec0 + 448 + i * 8);
83		g_topology_lock();
84		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
85		    ((off_t)v * csize) << 9ULL,
86		    ((off_t)u) << 9ULL,
87		    ms->sectorsize,
88		    "%s%c", gp->name, 'a' + i);
89		g_topology_unlock();
90		if (error)
91			return (error);
92	}
93	for (i = 0; i < 8; i++) {
94		v = be32dec(sec0 + 444 + i * 8);
95		u = be32dec(sec0 + 448 + i * 8);
96		g_topology_lock();
97		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
98		    ((off_t)v * csize) << 9ULL,
99		    ((off_t)u) << 9ULL,
100		    ms->sectorsize,
101		    "%s%c", gp->name, 'a' + i);
102		g_topology_unlock();
103	}
104	ms->nalt = be16dec(sec0 + 434);
105	ms->nheads = be16dec(sec0 + 436);
106	ms->nsects = be16dec(sec0 + 438);
107
108	return (0);
109}
110
111static int
112g_sunlabel_start(struct bio *bp)
113{
114	struct g_geom *gp;
115	struct g_sunlabel_softc *ms;
116	struct g_slicer *gsp;
117
118	gp = bp->bio_to->geom;
119	gsp = gp->softc;
120	ms = gsp->softc;
121	return (0);
122}
123
124static void
125g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
126{
127	struct g_slicer *gsp;
128	struct g_sunlabel_softc *ms;
129
130	gsp = gp->softc;
131	ms = gsp->softc;
132	g_slice_dumpconf(sb, indent, gp, cp, pp);
133	if (indent == NULL) {
134		sbuf_printf(sb, " sc %u hd %u alt %u",
135		    ms->nsects, ms->nheads, ms->nalt);
136	}
137}
138
139static struct g_geom *
140g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
141{
142	struct g_geom *gp;
143	struct g_consumer *cp;
144	int error, i, npart;
145	u_char *buf;
146	struct g_sunlabel_softc *ms;
147	off_t mediasize;
148	struct g_slicer *gsp;
149
150	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
151	g_topology_assert();
152	if (flags == G_TF_NORMAL &&
153	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
154		return (NULL);
155	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
156	if (gp == NULL)
157		return (NULL);
158	gsp = gp->softc;
159	g_topology_unlock();
160	gp->dumpconf = g_sunlabel_dumpconf;
161	npart = 0;
162	do {
163		if (gp->rank != 2 && flags == G_TF_NORMAL)
164			break;
165		ms->sectorsize = cp->provider->sectorsize;
166		if (ms->sectorsize < 512)
167			break;
168		mediasize = cp->provider->mediasize;
169		buf = g_read_data(cp, 0, ms->sectorsize, &error);
170		if (buf == NULL || error != 0)
171			break;
172
173		if (bootverbose) {
174			g_hexdump(buf, 128);
175			for (i = 0; i < 8; i++) {
176				printf("part %d %u %u\n", i,
177				    be32dec(buf + 444 + i * 8),
178				    be32dec(buf + 448 + i * 8));
179			}
180			printf("v_version = %d\n", be32dec(buf + 128));
181			printf("v_nparts = %d\n", be16dec(buf + 140));
182			for (i = 0; i < 8; i++) {
183				printf("v_part[%d] = %d %d\n",
184				    i, be16dec(buf + 142 + i * 4),
185				    be16dec(buf + 144 + i * 4));
186			}
187			printf("v_sanity %x\n", be32dec(buf + 186));
188			printf("v_version = %d\n", be32dec(buf + 128));
189			printf("v_rpm %d\n", be16dec(buf + 420));
190			printf("v_totalcyl %d\n", be16dec(buf + 422));
191			printf("v_cyl %d\n", be16dec(buf + 432));
192			printf("v_alt %d\n", be16dec(buf + 434));
193			printf("v_head %d\n", be16dec(buf + 436));
194			printf("v_sec %d\n", be16dec(buf + 438));
195		}
196
197		g_sunlabel_modify(gp, ms, buf);
198
199		break;
200	} while (0);
201	g_topology_lock();
202	g_access_rel(cp, -1, 0, 0);
203	if (LIST_EMPTY(&gp->provider)) {
204		g_std_spoiled(cp);
205		return (NULL);
206#ifdef notyet
207	} else {
208		g_slice_conf_hot(gp, 0, 0, ms->sectorsize);
209#endif
210	}
211	return (gp);
212}
213
214static struct g_class g_sunlabel_class = {
215	.name = SUNLABEL_CLASS_NAME,
216	.taste = g_sunlabel_taste,
217	G_CLASS_INITIALIZER
218};
219
220DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
221