geom_sunlabel.c revision 107956
192372Sphk/*-
292372Sphk * Copyright (c) 2002 Poul-Henning Kamp
392372Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
492372Sphk * All rights reserved.
592372Sphk *
692372Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
792372Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
892372Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
992372Sphk * DARPA CHATS research program.
1092372Sphk *
1192372Sphk * Redistribution and use in source and binary forms, with or without
1292372Sphk * modification, are permitted provided that the following conditions
1392372Sphk * are met:
1492372Sphk * 1. Redistributions of source code must retain the above copyright
1592372Sphk *    notice, this list of conditions and the following disclaimer.
1692372Sphk * 2. Redistributions in binary form must reproduce the above copyright
1792372Sphk *    notice, this list of conditions and the following disclaimer in the
1892372Sphk *    documentation and/or other materials provided with the distribution.
1992372Sphk * 3. The names of the authors may not be used to endorse or promote
2092372Sphk *    products derived from this software without specific prior written
2192372Sphk *    permission.
2292372Sphk *
2392372Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2492372Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2592372Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2692372Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2792372Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2892372Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2992372Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3092372Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3192372Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3292372Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3392372Sphk * SUCH DAMAGE.
3492372Sphk *
3592372Sphk * $FreeBSD: head/sys/geom/geom_sunlabel.c 107956 2002-12-16 23:08:48Z phk $
3692372Sphk */
3792372Sphk
3892372Sphk
3992372Sphk#include <sys/param.h>
4092372Sphk#ifndef _KERNEL
4192372Sphk#include <stdio.h>
4292372Sphk#include <string.h>
4392372Sphk#include <stdlib.h>
4492372Sphk#include <signal.h>
4592372Sphk#include <err.h>
4692372Sphk#else
4792372Sphk#include <sys/systm.h>
4892372Sphk#include <sys/kernel.h>
4992372Sphk#include <sys/conf.h>
5092372Sphk#include <sys/bio.h>
5192372Sphk#include <sys/malloc.h>
5292372Sphk#include <sys/lock.h>
5392372Sphk#include <sys/mutex.h>
5492372Sphk#endif
5592372Sphk#include <geom/geom.h>
5692372Sphk#include <geom/geom_slice.h>
5792372Sphk#include <machine/endian.h>
5892372Sphk
5997075Sphk#define SUNLABEL_CLASS_NAME "SUN"
6092372Sphk
6192372Sphkstruct g_sunlabel_softc {
62106100Sphk	int nheads;
63106100Sphk	int nsects;
64106100Sphk	int nalt;
6592372Sphk};
6692372Sphk
6792372Sphkstatic int
6892372Sphkg_sunlabel_start(struct bio *bp)
6992372Sphk{
7092372Sphk	struct g_geom *gp;
7192372Sphk	struct g_sunlabel_softc *ms;
7292372Sphk	struct g_slicer *gsp;
7392372Sphk
7492372Sphk	gp = bp->bio_to->geom;
7592372Sphk	gsp = gp->softc;
7692372Sphk	ms = gsp->softc;
7792372Sphk	return (0);
7892372Sphk}
7992372Sphk
8092372Sphkstatic void
81107953Sphkg_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
8292372Sphk{
83106100Sphk	struct g_slicer *gsp;
84106100Sphk	struct g_sunlabel_softc *ms;
8592372Sphk
86106100Sphk	gsp = gp->softc;
87106100Sphk	ms = gsp->softc;
8892372Sphk	g_slice_dumpconf(sb, indent, gp, cp, pp);
89106100Sphk	if (indent == NULL) {
90106100Sphk		sbuf_printf(sb, " sc %u hd %u alt %u",
91106100Sphk		    ms->nsects, ms->nheads, ms->nalt);
92106100Sphk	}
9392372Sphk}
9492372Sphk
9592372Sphkstatic struct g_geom *
9693250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
9792372Sphk{
9892372Sphk	struct g_geom *gp;
9992372Sphk	struct g_consumer *cp;
10094287Sphk	int error, i, npart;
10192372Sphk	u_char *buf;
10292372Sphk	struct g_sunlabel_softc *ms;
10394285Sphk	u_int sectorsize, u, v, csize;
10492372Sphk	off_t mediasize;
10594287Sphk	struct g_slicer *gsp;
10692372Sphk
10792372Sphk	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
10892372Sphk	g_topology_assert();
10992372Sphk	if (flags == G_TF_NORMAL &&
11093358Sphk	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
11192372Sphk		return (NULL);
11292372Sphk	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
11392372Sphk	if (gp == NULL)
11492372Sphk		return (NULL);
11594287Sphk	gsp = gp->softc;
11692372Sphk	g_topology_unlock();
11792372Sphk	gp->dumpconf = g_sunlabel_dumpconf;
11892372Sphk	npart = 0;
11992372Sphk	while (1) {	/* a trick to allow us to use break */
12092372Sphk		if (gp->rank != 2 && flags == G_TF_NORMAL)
12192372Sphk			break;
122105551Sphk		sectorsize = cp->provider->sectorsize;
123105551Sphk		if (sectorsize < 512)
124105551Sphk			break;
12594287Sphk		gsp->frontstuff = 16 * sectorsize;
126105551Sphk		mediasize = cp->provider->mediasize;
12794285Sphk		buf = g_read_data(cp, 0, sectorsize, &error);
12892372Sphk		if (buf == NULL || error != 0)
12992372Sphk			break;
13092372Sphk
13192372Sphk		/* The second last short is a magic number */
13293090Sphk		if (g_dec_be2(buf + 508) != 0xdabe)
13392372Sphk			break;
13492372Sphk		/* The shortword parity of the entire thing must be even */
13592372Sphk		u = 0;
13692372Sphk		for (i = 0; i < 512; i += 2)
13793090Sphk			u ^= g_dec_be2(buf + i);
13892372Sphk		if (u != 0)
13992372Sphk			break;
14092372Sphk		if (bootverbose) {
14192372Sphk			g_hexdump(buf, 128);
14292372Sphk			for (i = 0; i < 8; i++) {
14392372Sphk				printf("part %d %u %u\n", i,
14493090Sphk				    g_dec_be4(buf + 444 + i * 8),
14593090Sphk				    g_dec_be4(buf + 448 + i * 8));
14692372Sphk			}
14793090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
14893090Sphk			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
14992372Sphk			for (i = 0; i < 8; i++) {
15092372Sphk				printf("v_part[%d] = %d %d\n",
15193090Sphk				    i, g_dec_be2(buf + 142 + i * 4),
15293090Sphk				    g_dec_be2(buf + 144 + i * 4));
15392372Sphk			}
15493090Sphk			printf("v_sanity %x\n", g_dec_be4(buf + 186));
15593090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
15693090Sphk			printf("v_rpm %d\n", g_dec_be2(buf + 420));
15793090Sphk			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
15893090Sphk			printf("v_cyl %d\n", g_dec_be2(buf + 432));
15993090Sphk			printf("v_alt %d\n", g_dec_be2(buf + 434));
16093090Sphk			printf("v_head %d\n", g_dec_be2(buf + 436));
16193090Sphk			printf("v_sec %d\n", g_dec_be2(buf + 438));
16292372Sphk		}
163106100Sphk		ms->nalt = g_dec_be2(buf + 434);
164106100Sphk		ms->nheads = g_dec_be2(buf + 436);
165106100Sphk		ms->nsects = g_dec_be2(buf + 438);
16692372Sphk
167106100Sphk		csize = ms->nheads * ms->nsects;
16892372Sphk
16992372Sphk		for (i = 0; i < 8; i++) {
17093090Sphk			v = g_dec_be4(buf + 444 + i * 8);
17193090Sphk			u = g_dec_be4(buf + 448 + i * 8);
17292372Sphk			if (u == 0)
17392372Sphk				continue;
17492372Sphk			npart++;
175104064Sphk			g_topology_lock();
176107956Sphk			g_slice_config(gp, i, G_SLICE_CONFIG_SET,
17792372Sphk			    ((off_t)v * csize) << 9ULL,
17892372Sphk			    ((off_t)u) << 9ULL,
179105542Sphk			    sectorsize,
18092372Sphk			    "%s%c", pp->name, 'a' + i);
181104064Sphk			g_topology_unlock();
18292372Sphk		}
18392372Sphk		break;
18492372Sphk	}
18592372Sphk	g_topology_lock();
186107956Sphk	g_access_rel(cp, -1, 0, 0);
187107956Sphk	if (LIST_EMPTY(&gp->provider)) {
188107956Sphk		g_std_spoiled(cp);
189107956Sphk		return (NULL);
190107956Sphk	}
191107956Sphk	return (gp);
19292372Sphk}
19392372Sphk
19493248Sphkstatic struct g_class g_sunlabel_class = {
19593358Sphk	SUNLABEL_CLASS_NAME,
19692372Sphk	g_sunlabel_taste,
19792372Sphk	NULL,
19898066Sphk	G_CLASS_INITIALIZER
19992372Sphk};
20092372Sphk
20193248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
202