geom_sunlabel.c revision 113390
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 113390 2003-04-12 08:41:26Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#ifndef _KERNEL
42#include <stdio.h>
43#include <string.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <sys/errno.h>
47#include <err.h>
48#else
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/conf.h>
52#include <sys/bio.h>
53#include <sys/malloc.h>
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#endif
57#include <geom/geom.h>
58#include <geom/geom_slice.h>
59#include <machine/endian.h>
60
61#define SUNLABEL_CLASS_NAME "SUN"
62
63struct g_sunlabel_softc {
64	int sectorsize;
65	int nheads;
66	int nsects;
67	int nalt;
68};
69
70static int
71g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
72{
73	int i, error;
74	u_int u, v, csize;
75
76	/* The second last short is a magic number */
77	if (be16dec(sec0 + 508) != 0xdabe)
78		return (EBUSY);
79
80	/* The shortword parity of the entire thing must be even */
81	u = 0;
82	for (i = 0; i < 512; i += 2)
83		u ^= be16dec(sec0 + i);
84	if (u != 0)
85		return(EBUSY);
86
87	csize = be16dec(sec0 + 436) * be16dec(sec0 + 438);
88
89	for (i = 0; i < 8; i++) {
90		v = be32dec(sec0 + 444 + i * 8);
91		u = be32dec(sec0 + 448 + i * 8);
92		g_topology_lock();
93		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
94		    ((off_t)v * csize) << 9ULL,
95		    ((off_t)u) << 9ULL,
96		    ms->sectorsize,
97		    "%s%c", gp->name, 'a' + i);
98		g_topology_unlock();
99		if (error)
100			return (error);
101	}
102	for (i = 0; i < 8; i++) {
103		v = be32dec(sec0 + 444 + i * 8);
104		u = be32dec(sec0 + 448 + i * 8);
105		g_topology_lock();
106		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
107		    ((off_t)v * csize) << 9ULL,
108		    ((off_t)u) << 9ULL,
109		    ms->sectorsize,
110		    "%s%c", gp->name, 'a' + i);
111		g_topology_unlock();
112	}
113	ms->nalt = be16dec(sec0 + 434);
114	ms->nheads = be16dec(sec0 + 436);
115	ms->nsects = be16dec(sec0 + 438);
116
117	return (0);
118}
119
120static int
121g_sunlabel_start(struct bio *bp)
122{
123	struct g_geom *gp;
124	struct g_sunlabel_softc *ms;
125	struct g_slicer *gsp;
126
127	gp = bp->bio_to->geom;
128	gsp = gp->softc;
129	ms = gsp->softc;
130	return (0);
131}
132
133static void
134g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
135{
136	struct g_slicer *gsp;
137	struct g_sunlabel_softc *ms;
138
139	gsp = gp->softc;
140	ms = gsp->softc;
141	g_slice_dumpconf(sb, indent, gp, cp, pp);
142	if (indent == NULL) {
143		sbuf_printf(sb, " sc %u hd %u alt %u",
144		    ms->nsects, ms->nheads, ms->nalt);
145	}
146}
147
148static struct g_geom *
149g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
150{
151	struct g_geom *gp;
152	struct g_consumer *cp;
153	int error, i, npart;
154	u_char *buf;
155	struct g_sunlabel_softc *ms;
156	off_t mediasize;
157	struct g_slicer *gsp;
158
159	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
160	g_topology_assert();
161	if (flags == G_TF_NORMAL &&
162	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
163		return (NULL);
164	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
165	if (gp == NULL)
166		return (NULL);
167	gsp = gp->softc;
168	g_topology_unlock();
169	gp->dumpconf = g_sunlabel_dumpconf;
170	npart = 0;
171	do {
172		if (gp->rank != 2 && flags == G_TF_NORMAL)
173			break;
174		ms->sectorsize = cp->provider->sectorsize;
175		if (ms->sectorsize < 512)
176			break;
177		mediasize = cp->provider->mediasize;
178		buf = g_read_data(cp, 0, ms->sectorsize, &error);
179		if (buf == NULL || error != 0)
180			break;
181
182		if (bootverbose) {
183			g_hexdump(buf, 128);
184			for (i = 0; i < 8; i++) {
185				printf("part %d %u %u\n", i,
186				    be32dec(buf + 444 + i * 8),
187				    be32dec(buf + 448 + i * 8));
188			}
189			printf("v_version = %d\n", be32dec(buf + 128));
190			printf("v_nparts = %d\n", be16dec(buf + 140));
191			for (i = 0; i < 8; i++) {
192				printf("v_part[%d] = %d %d\n",
193				    i, be16dec(buf + 142 + i * 4),
194				    be16dec(buf + 144 + i * 4));
195			}
196			printf("v_sanity %x\n", be32dec(buf + 186));
197			printf("v_version = %d\n", be32dec(buf + 128));
198			printf("v_rpm %d\n", be16dec(buf + 420));
199			printf("v_totalcyl %d\n", be16dec(buf + 422));
200			printf("v_cyl %d\n", be16dec(buf + 432));
201			printf("v_alt %d\n", be16dec(buf + 434));
202			printf("v_head %d\n", be16dec(buf + 436));
203			printf("v_sec %d\n", be16dec(buf + 438));
204		}
205
206		g_sunlabel_modify(gp, ms, buf);
207
208		break;
209	} while (0);
210	g_topology_lock();
211	g_access_rel(cp, -1, 0, 0);
212	if (LIST_EMPTY(&gp->provider)) {
213		g_std_spoiled(cp);
214		return (NULL);
215#ifdef notyet
216	} else {
217		g_slice_conf_hot(gp, 0, 0, ms->sectorsize);
218#endif
219	}
220	return (gp);
221}
222
223static struct g_class g_sunlabel_class = {
224	.name = SUNLABEL_CLASS_NAME,
225	.taste = g_sunlabel_taste,
226	G_CLASS_INITIALIZER
227};
228
229DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
230