geom_sunlabel.c revision 93358
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 93358 2002-03-28 22:00:24Z 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 <sys/errno.h>
5692372Sphk#include <geom/geom.h>
5792372Sphk#include <geom/geom_slice.h>
5892372Sphk#include <machine/endian.h>
5992372Sphk
6093358Sphk#define SUNLABEL_CLASS_NAME "SUNLABEL-class"
6192372Sphk
6292372Sphkstruct g_sunlabel_softc {
6392372Sphk	int foo;
6492372Sphk};
6592372Sphk
6692372Sphkstatic int
6792372Sphkg_sunlabel_start(struct bio *bp)
6892372Sphk{
6992372Sphk	struct g_geom *gp;
7092372Sphk	struct g_sunlabel_softc *ms;
7192372Sphk	struct g_slicer *gsp;
7292372Sphk
7392372Sphk	gp = bp->bio_to->geom;
7492372Sphk	gsp = gp->softc;
7592372Sphk	ms = gsp->softc;
7692372Sphk	return (0);
7792372Sphk}
7892372Sphk
7992372Sphkstatic void
8092372Sphkg_sunlabel_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
8192372Sphk{
8292372Sphk
8392372Sphk	g_slice_dumpconf(sb, indent, gp, cp, pp);
8492372Sphk}
8592372Sphk
8692372Sphkstatic struct g_geom *
8793250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
8892372Sphk{
8992372Sphk	struct g_geom *gp;
9092372Sphk	struct g_consumer *cp;
9192372Sphk	struct g_provider *pp2;
9292372Sphk	int error, i, j, npart;
9392372Sphk	u_char *buf;
9492372Sphk	struct g_sunlabel_softc *ms;
9592372Sphk	u_int secsize, u, v, csize;
9692372Sphk	off_t mediasize;
9792372Sphk
9892372Sphk	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
9992372Sphk	g_topology_assert();
10092372Sphk	if (flags == G_TF_NORMAL &&
10193358Sphk	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
10292372Sphk		return (NULL);
10392372Sphk	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
10492372Sphk	if (gp == NULL)
10592372Sphk		return (NULL);
10692372Sphk	g_topology_unlock();
10792372Sphk	gp->dumpconf = g_sunlabel_dumpconf;
10892372Sphk	npart = 0;
10992372Sphk	while (1) {	/* a trick to allow us to use break */
11092372Sphk		if (gp->rank != 2 && flags == G_TF_NORMAL)
11192372Sphk			break;
11292372Sphk		j = sizeof secsize;
11393250Sphk		error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
11492372Sphk		if (error) {
11592372Sphk			secsize = 512;
11692372Sphk			printf("g_sunlabel_taste: error %d Sectors are %d bytes\n",
11792372Sphk			    error, secsize);
11892372Sphk		}
11992372Sphk		j = sizeof mediasize;
12093250Sphk		error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
12192372Sphk		if (error) {
12292372Sphk			mediasize = 0;
12392372Sphk			printf("g_error %d Mediasize is %lld bytes\n",
12492372Sphk			    error, (long long)mediasize);
12592372Sphk		}
12692372Sphk		buf = g_read_data(cp, 0, secsize, &error);
12792372Sphk		if (buf == NULL || error != 0)
12892372Sphk			break;
12992372Sphk
13092372Sphk		/* The second last short is a magic number */
13193090Sphk		if (g_dec_be2(buf + 508) != 0xdabe)
13292372Sphk			break;
13392372Sphk		/* The shortword parity of the entire thing must be even */
13492372Sphk		u = 0;
13592372Sphk		for (i = 0; i < 512; i += 2)
13693090Sphk			u ^= g_dec_be2(buf + i);
13792372Sphk		if (u != 0)
13892372Sphk			break;
13992372Sphk		if (bootverbose) {
14092372Sphk			g_hexdump(buf, 128);
14192372Sphk			for (i = 0; i < 8; i++) {
14292372Sphk				printf("part %d %u %u\n", i,
14393090Sphk				    g_dec_be4(buf + 444 + i * 8),
14493090Sphk				    g_dec_be4(buf + 448 + i * 8));
14592372Sphk			}
14693090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
14793090Sphk			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
14892372Sphk			for (i = 0; i < 8; i++) {
14992372Sphk				printf("v_part[%d] = %d %d\n",
15093090Sphk				    i, g_dec_be2(buf + 142 + i * 4),
15193090Sphk				    g_dec_be2(buf + 144 + i * 4));
15292372Sphk			}
15393090Sphk			printf("v_sanity %x\n", g_dec_be4(buf + 186));
15493090Sphk			printf("v_version = %d\n", g_dec_be4(buf + 128));
15593090Sphk			printf("v_rpm %d\n", g_dec_be2(buf + 420));
15693090Sphk			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
15793090Sphk			printf("v_cyl %d\n", g_dec_be2(buf + 432));
15893090Sphk			printf("v_alt %d\n", g_dec_be2(buf + 434));
15993090Sphk			printf("v_head %d\n", g_dec_be2(buf + 436));
16093090Sphk			printf("v_sec %d\n", g_dec_be2(buf + 438));
16192372Sphk		}
16292372Sphk
16393090Sphk		csize = g_dec_be2(buf + 436) * g_dec_be2(buf + 438);
16492372Sphk
16592372Sphk		for (i = 0; i < 8; i++) {
16693090Sphk			v = g_dec_be4(buf + 444 + i * 8);
16793090Sphk			u = g_dec_be4(buf + 448 + i * 8);
16892372Sphk			if (u == 0)
16992372Sphk				continue;
17092372Sphk			npart++;
17192372Sphk			pp2 = g_slice_addslice(gp, i,
17292372Sphk			    ((off_t)v * csize) << 9ULL,
17392372Sphk			    ((off_t)u) << 9ULL,
17492372Sphk			    "%s%c", pp->name, 'a' + i);
17592372Sphk			g_error_provider(pp2, 0);
17692372Sphk		}
17792372Sphk		break;
17892372Sphk	}
17992372Sphk	g_topology_lock();
18092372Sphk	error = g_access_rel(cp, -1, 0, 0);
18192372Sphk	if (npart > 0)
18292372Sphk		return (gp);
18392372Sphk	g_std_spoiled(cp);
18492372Sphk	return (NULL);
18592372Sphk}
18692372Sphk
18793248Sphkstatic struct g_class g_sunlabel_class = {
18893358Sphk	SUNLABEL_CLASS_NAME,
18992372Sphk	g_sunlabel_taste,
19092372Sphk	g_slice_access,
19192372Sphk	g_slice_orphan,
19292372Sphk	NULL,
19393248Sphk	G_CLASS_INITSTUFF
19492372Sphk};
19592372Sphk
19693248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
197