geom_sunlabel.c revision 112552
180021Sjasone/*-
280021Sjasone * Copyright (c) 2002 Poul-Henning Kamp
380021Sjasone * Copyright (c) 2002 Networks Associates Technology, Inc.
480021Sjasone * All rights reserved.
580021Sjasone *
680021Sjasone * This software was developed for the FreeBSD Project by Poul-Henning Kamp
780021Sjasone * and NAI Labs, the Security Research Division of Network Associates, Inc.
880021Sjasone * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
980021Sjasone * DARPA CHATS research program.
1080021Sjasone *
1180021Sjasone * Redistribution and use in source and binary forms, with or without
1280021Sjasone * modification, are permitted provided that the following conditions
1380021Sjasone * are met:
1480021Sjasone * 1. Redistributions of source code must retain the above copyright
1580021Sjasone *    notice, this list of conditions and the following disclaimer.
1680021Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1780021Sjasone *    notice, this list of conditions and the following disclaimer in the
1880021Sjasone *    documentation and/or other materials provided with the distribution.
1980021Sjasone * 3. The names of the authors may not be used to endorse or promote
2080021Sjasone *    products derived from this software without specific prior written
2180021Sjasone *    permission.
2280021Sjasone *
2380021Sjasone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2480021Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2580021Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2680021Sjasone * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2780021Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2880021Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2980021Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3080021Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3180021Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3280021Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3380021Sjasone * SUCH DAMAGE.
3480021Sjasone *
3580021Sjasone * $FreeBSD: head/sys/geom/geom_sunlabel.c 112552 2003-03-24 19:30:15Z phk $
3680021Sjasone */
3780021Sjasone
3880021Sjasone
3980021Sjasone#include <sys/param.h>
4080021Sjasone#ifndef _KERNEL
4180021Sjasone#include <stdio.h>
4280021Sjasone#include <string.h>
4380021Sjasone#include <stdlib.h>
4480021Sjasone#include <signal.h>
4580021Sjasone#include <sys/errno.h>
4680021Sjasone#include <err.h>
4780021Sjasone#else
4880021Sjasone#include <sys/systm.h>
4980021Sjasone#include <sys/kernel.h>
5080021Sjasone#include <sys/conf.h>
5180021Sjasone#include <sys/bio.h>
5280021Sjasone#include <sys/malloc.h>
5380021Sjasone#include <sys/lock.h>
5480021Sjasone#include <sys/mutex.h>
5580021Sjasone#endif
5680021Sjasone#include <geom/geom.h>
5780021Sjasone#include <geom/geom_slice.h>
5880021Sjasone#include <machine/endian.h>
5980021Sjasone
6080021Sjasone#define SUNLABEL_CLASS_NAME "SUN"
6180021Sjasone
6280021Sjasonestruct g_sunlabel_softc {
6380021Sjasone	int sectorsize;
6480021Sjasone	int nheads;
6580021Sjasone	int nsects;
6680021Sjasone	int nalt;
6780021Sjasone};
6880021Sjasone
6980021Sjasonestatic int
70g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
71{
72	int i, error;
73	u_int u, v, csize;
74
75	/* The second last short is a magic number */
76	if (g_dec_be2(sec0 + 508) != 0xdabe)
77		return (EBUSY);
78
79	/* The shortword parity of the entire thing must be even */
80	u = 0;
81	for (i = 0; i < 512; i += 2)
82		u ^= g_dec_be2(sec0 + i);
83	if (u != 0)
84		return(EBUSY);
85
86	csize = g_dec_be2(sec0 + 436) * g_dec_be2(sec0 + 438);
87
88	for (i = 0; i < 8; i++) {
89		v = g_dec_be4(sec0 + 444 + i * 8);
90		u = g_dec_be4(sec0 + 448 + i * 8);
91		g_topology_lock();
92		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
93		    ((off_t)v * csize) << 9ULL,
94		    ((off_t)u) << 9ULL,
95		    ms->sectorsize,
96		    "%s%c", gp->name, 'a' + i);
97		g_topology_unlock();
98		if (error)
99			return (error);
100	}
101	for (i = 0; i < 8; i++) {
102		v = g_dec_be4(sec0 + 444 + i * 8);
103		u = g_dec_be4(sec0 + 448 + i * 8);
104		g_topology_lock();
105		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
106		    ((off_t)v * csize) << 9ULL,
107		    ((off_t)u) << 9ULL,
108		    ms->sectorsize,
109		    "%s%c", gp->name, 'a' + i);
110		g_topology_unlock();
111	}
112	ms->nalt = g_dec_be2(sec0 + 434);
113	ms->nheads = g_dec_be2(sec0 + 436);
114	ms->nsects = g_dec_be2(sec0 + 438);
115
116	return (0);
117}
118
119static int
120g_sunlabel_start(struct bio *bp)
121{
122	struct g_geom *gp;
123	struct g_sunlabel_softc *ms;
124	struct g_slicer *gsp;
125
126	gp = bp->bio_to->geom;
127	gsp = gp->softc;
128	ms = gsp->softc;
129	return (0);
130}
131
132static void
133g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
134{
135	struct g_slicer *gsp;
136	struct g_sunlabel_softc *ms;
137
138	gsp = gp->softc;
139	ms = gsp->softc;
140	g_slice_dumpconf(sb, indent, gp, cp, pp);
141	if (indent == NULL) {
142		sbuf_printf(sb, " sc %u hd %u alt %u",
143		    ms->nsects, ms->nheads, ms->nalt);
144	}
145}
146
147static struct g_geom *
148g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
149{
150	struct g_geom *gp;
151	struct g_consumer *cp;
152	int error, i, npart;
153	u_char *buf;
154	struct g_sunlabel_softc *ms;
155	off_t mediasize;
156	struct g_slicer *gsp;
157
158	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
159	g_topology_assert();
160	if (flags == G_TF_NORMAL &&
161	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
162		return (NULL);
163	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
164	if (gp == NULL)
165		return (NULL);
166	gsp = gp->softc;
167	g_topology_unlock();
168	gp->dumpconf = g_sunlabel_dumpconf;
169	npart = 0;
170	do {	/* a trick to allow us to use break */
171		if (gp->rank != 2 && flags == G_TF_NORMAL)
172			break;
173		ms->sectorsize = cp->provider->sectorsize;
174		if (ms->sectorsize < 512)
175			break;
176		gsp->frontstuff = 16 * ms->sectorsize;
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				    g_dec_be4(buf + 444 + i * 8),
187				    g_dec_be4(buf + 448 + i * 8));
188			}
189			printf("v_version = %d\n", g_dec_be4(buf + 128));
190			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
191			for (i = 0; i < 8; i++) {
192				printf("v_part[%d] = %d %d\n",
193				    i, g_dec_be2(buf + 142 + i * 4),
194				    g_dec_be2(buf + 144 + i * 4));
195			}
196			printf("v_sanity %x\n", g_dec_be4(buf + 186));
197			printf("v_version = %d\n", g_dec_be4(buf + 128));
198			printf("v_rpm %d\n", g_dec_be2(buf + 420));
199			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
200			printf("v_cyl %d\n", g_dec_be2(buf + 432));
201			printf("v_alt %d\n", g_dec_be2(buf + 434));
202			printf("v_head %d\n", g_dec_be2(buf + 436));
203			printf("v_sec %d\n", g_dec_be2(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