geom_sunlabel.c revision 106100
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 106100 2002-10-28 22:42:20Z 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
8192372Sphkg_sunlabel_dumpconf(struct sbuf *sb, 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;
10092372Sphk	struct g_provider *pp2;
10194287Sphk	int error, i, npart;
10292372Sphk	u_char *buf;
10392372Sphk	struct g_sunlabel_softc *ms;
10494285Sphk	u_int sectorsize, u, v, csize;
10592372Sphk	off_t mediasize;
10694287Sphk	struct g_slicer *gsp;
10792372Sphk
10892372Sphk	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
10992372Sphk	g_topology_assert();
11092372Sphk	if (flags == G_TF_NORMAL &&
11193358Sphk	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
11292372Sphk		return (NULL);
11392372Sphk	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
11492372Sphk	if (gp == NULL)
11592372Sphk		return (NULL);
11694287Sphk	gsp = gp->softc;
11792372Sphk	g_topology_unlock();
11892372Sphk	gp->dumpconf = g_sunlabel_dumpconf;
11992372Sphk	npart = 0;
12092372Sphk	while (1) {	/* a trick to allow us to use break */
12192372Sphk		if (gp->rank != 2 && flags == G_TF_NORMAL)
12292372Sphk			break;
123105551Sphk		sectorsize = cp->provider->sectorsize;
124105551Sphk		if (sectorsize < 512)
125105551Sphk			break;
12694287Sphk		gsp->frontstuff = 16 * sectorsize;
127105551Sphk		mediasize = cp->provider->mediasize;
12894285Sphk		buf = g_read_data(cp, 0, sectorsize, &error);
12992372Sphk		if (buf == NULL || error != 0)
13092372Sphk			break;
13192372Sphk
13292372Sphk		/* The second last short is a magic number */
13393090Sphk		if (g_dec_be2(buf + 508) != 0xdabe)
13492372Sphk			break;
13592372Sphk		/* The shortword parity of the entire thing must be even */
13692372Sphk		u = 0;
13792372Sphk		for (i = 0; i < 512; i += 2)
13893090Sphk			u ^= g_dec_be2(buf + i);
13992372Sphk		if (u != 0)
14092372Sphk			break;
14192372Sphk		if (bootverbose) {
14292372Sphk			g_hexdump(buf, 128);
14392372Sphk			for (i = 0; i < 8; i++) {
14492372Sphk				printf("part %d %u %u\n", i,
14593090Sphk				    g_dec_be4(buf + 444 + i * 8),
14693090Sphk				    g_dec_be4(buf + 448 + i * 8));
14792372Sphk			}
14893090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
14993090Sphk			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
15092372Sphk			for (i = 0; i < 8; i++) {
15192372Sphk				printf("v_part[%d] = %d %d\n",
15293090Sphk				    i, g_dec_be2(buf + 142 + i * 4),
15393090Sphk				    g_dec_be2(buf + 144 + i * 4));
15492372Sphk			}
15593090Sphk			printf("v_sanity %x\n", g_dec_be4(buf + 186));
15693090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
15793090Sphk			printf("v_rpm %d\n", g_dec_be2(buf + 420));
15893090Sphk			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
15993090Sphk			printf("v_cyl %d\n", g_dec_be2(buf + 432));
16093090Sphk			printf("v_alt %d\n", g_dec_be2(buf + 434));
16193090Sphk			printf("v_head %d\n", g_dec_be2(buf + 436));
16293090Sphk			printf("v_sec %d\n", g_dec_be2(buf + 438));
16392372Sphk		}
164106100Sphk		ms->nalt = g_dec_be2(buf + 434);
165106100Sphk		ms->nheads = g_dec_be2(buf + 436);
166106100Sphk		ms->nsects = g_dec_be2(buf + 438);
16792372Sphk
168106100Sphk		csize = ms->nheads * ms->nsects;
16992372Sphk
17092372Sphk		for (i = 0; i < 8; i++) {
17193090Sphk			v = g_dec_be4(buf + 444 + i * 8);
17293090Sphk			u = g_dec_be4(buf + 448 + i * 8);
17392372Sphk			if (u == 0)
17492372Sphk				continue;
17592372Sphk			npart++;
176104064Sphk			g_topology_lock();
17792372Sphk			pp2 = g_slice_addslice(gp, i,
17892372Sphk			    ((off_t)v * csize) << 9ULL,
17992372Sphk			    ((off_t)u) << 9ULL,
180105542Sphk			    sectorsize,
18192372Sphk			    "%s%c", pp->name, 'a' + i);
182104064Sphk			g_topology_unlock();
18392372Sphk			g_error_provider(pp2, 0);
18492372Sphk		}
18592372Sphk		break;
18692372Sphk	}
18792372Sphk	g_topology_lock();
18892372Sphk	error = g_access_rel(cp, -1, 0, 0);
189103009Sphk	if (npart > 0)
19092372Sphk		return (gp);
19192372Sphk	g_std_spoiled(cp);
19292372Sphk	return (NULL);
19392372Sphk}
19492372Sphk
19593248Sphkstatic struct g_class g_sunlabel_class = {
19693358Sphk	SUNLABEL_CLASS_NAME,
19792372Sphk	g_sunlabel_taste,
19892372Sphk	NULL,
19998066Sphk	G_CLASS_INITIALIZER
20092372Sphk};
20192372Sphk
20293248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
203