geom_sunlabel.c revision 105542
119000Sbde/*-
219000Sbde * Copyright (c) 2002 Poul-Henning Kamp
319000Sbde * Copyright (c) 2002 Networks Associates Technology, Inc.
415146Swollman * All rights reserved.
519000Sbde *
619000Sbde * This software was developed for the FreeBSD Project by Poul-Henning Kamp
719000Sbde * and NAI Labs, the Security Research Division of Network Associates, Inc.
819000Sbde * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
919000Sbde * DARPA CHATS research program.
1019000Sbde *
1119000Sbde * Redistribution and use in source and binary forms, with or without
1219000Sbde * modification, are permitted provided that the following conditions
1319000Sbde * are met:
1419000Sbde * 1. Redistributions of source code must retain the above copyright
1519000Sbde *    notice, this list of conditions and the following disclaimer.
1619000Sbde * 2. Redistributions in binary form must reproduce the above copyright
1719000Sbde *    notice, this list of conditions and the following disclaimer in the
1819000Sbde *    documentation and/or other materials provided with the distribution.
1919000Sbde * 3. The names of the authors may not be used to endorse or promote
2019000Sbde *    products derived from this software without specific prior written
2119000Sbde *    permission.
2219000Sbde *
2319000Sbde * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2419000Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2515146Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2619000Sbde * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27115703Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28115703Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29115703Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3019000Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3119000Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3219000Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3319000Sbde * SUCH DAMAGE.
34163727Sbde *
35163727Sbde * $FreeBSD: head/sys/geom/geom_sunlabel.c 105542 2002-10-20 19:18:07Z phk $
36167905Snjl */
37167905Snjl
38167905Snjl
3919000Sbde#include <sys/param.h>
4031395Sbde#ifndef _KERNEL
41163756Sbde#include <stdio.h>
4231395Sbde#include <string.h>
4319000Sbde#include <stdlib.h>
4413107Sbde#include <signal.h>
4519000Sbde#include <err.h>
46163727Sbde#else
4719000Sbde#include <sys/systm.h>
4819000Sbde#include <sys/kernel.h>
4919000Sbde#include <sys/conf.h>
5032005Sphk#include <sys/bio.h>
5119000Sbde#include <sys/malloc.h>
5219000Sbde#include <sys/lock.h>
5319000Sbde#include <sys/mutex.h>
5419000Sbde#endif
5519000Sbde#include <geom/geom.h>
5619000Sbde#include <geom/geom_slice.h>
57163756Sbde#include <machine/endian.h>
5819000Sbde
5919000Sbde#define SUNLABEL_CLASS_NAME "SUN"
6019000Sbde
6113107Sbdestruct g_sunlabel_softc {
62167905Snjl	int foo;
63167905Snjl};
64167905Snjl
6519000Sbdestatic int
6613107Sbdeg_sunlabel_start(struct bio *bp)
67143063Sjoerg{
6835303Sbde	struct g_geom *gp;
6935303Sbde	struct g_sunlabel_softc *ms;
7035303Sbde	struct g_slicer *gsp;
7135303Sbde
7235303Sbde	gp = bp->bio_to->geom;
7350379Speter	gsp = gp->softc;
7435303Sbde	ms = gsp->softc;
7546548Sbde	return (0);
7635303Sbde}
7735303Sbde
7835303Sbdestatic void
7935303Sbdeg_sunlabel_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
80163726Sbde{
81167905Snjl
82167905Snjl	g_slice_dumpconf(sb, indent, gp, cp, pp);
83167905Snjl}
84167905Snjl
8535303Sbdestatic struct g_geom *
8635303Sbdeg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
87167905Snjl{
88167905Snjl	struct g_geom *gp;
89167905Snjl	struct g_consumer *cp;
90167905Snjl	struct g_provider *pp2;
91167905Snjl	int error, i, npart;
92163726Sbde	u_char *buf;
93163726Sbde	struct g_sunlabel_softc *ms;
9446548Sbde	u_int sectorsize, u, v, csize;
9535303Sbde	off_t mediasize;
9635303Sbde	struct g_slicer *gsp;
9735303Sbde
9835303Sbde	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
9935303Sbde	g_topology_assert();
10035303Sbde	if (flags == G_TF_NORMAL &&
10135303Sbde	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
10246548Sbde		return (NULL);
10335303Sbde	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
10435303Sbde	if (gp == NULL)
10535303Sbde		return (NULL);
10635303Sbde	gsp = gp->softc;
10735303Sbde	g_topology_unlock();
10835303Sbde	gp->dumpconf = g_sunlabel_dumpconf;
10935303Sbde	npart = 0;
11035303Sbde	while (1) {	/* a trick to allow us to use break */
11135303Sbde		if (gp->rank != 2 && flags == G_TF_NORMAL)
112163726Sbde			break;
11335303Sbde		error = g_getattr("GEOM::sectorsize", cp, &sectorsize);
11435303Sbde		if (error) {
11546548Sbde			sectorsize = 512;
116174067Sbde			printf("g_sunlabel_taste: error %d Sectors are %d bytes\n",
11713107Sbde			    error, sectorsize);
118143063Sjoerg		}
119163727Sbde		gsp->frontstuff = 16 * sectorsize;
120143063Sjoerg		error = g_getattr("GEOM::mediasize", cp, &mediasize);
12113107Sbde		if (error) {
12213107Sbde			mediasize = 0;
12313107Sbde			printf("g_error %d Mediasize is %lld bytes\n",
12446548Sbde			    error, (long long)mediasize);
12513107Sbde		}
12646548Sbde		buf = g_read_data(cp, 0, sectorsize, &error);
12713107Sbde		if (buf == NULL || error != 0)
12813107Sbde			break;
129143063Sjoerg
13035303Sbde		/* The second last short is a magic number */
13135303Sbde		if (g_dec_be2(buf + 508) != 0xdabe)
13235303Sbde			break;
13346548Sbde		/* The shortword parity of the entire thing must be even */
13435303Sbde		u = 0;
13550379Speter		for (i = 0; i < 512; i += 2)
13635303Sbde			u ^= g_dec_be2(buf + i);
13746548Sbde		if (u != 0)
13835303Sbde			break;
13935303Sbde		if (bootverbose) {
14035303Sbde			g_hexdump(buf, 128);
14135303Sbde			for (i = 0; i < 8; i++) {
14235303Sbde				printf("part %d %u %u\n", i,
14350379Speter				    g_dec_be4(buf + 444 + i * 8),
144163726Sbde				    g_dec_be4(buf + 448 + i * 8));
145163726Sbde			}
146163726Sbde			printf("v_version = %d\n", g_dec_be4(buf + 128));
14746548Sbde			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
14835303Sbde			for (i = 0; i < 8; i++) {
14935303Sbde				printf("v_part[%d] = %d %d\n",
15035303Sbde				    i, g_dec_be2(buf + 142 + i * 4),
15135303Sbde				    g_dec_be2(buf + 144 + i * 4));
15235303Sbde			}
15335303Sbde			printf("v_sanity %x\n", g_dec_be4(buf + 186));
154163726Sbde			printf("v_version = %d\n", g_dec_be4(buf + 128));
15535303Sbde			printf("v_rpm %d\n", g_dec_be2(buf + 420));
15635303Sbde			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
15735303Sbde			printf("v_cyl %d\n", g_dec_be2(buf + 432));
15835303Sbde			printf("v_alt %d\n", g_dec_be2(buf + 434));
15946548Sbde			printf("v_head %d\n", g_dec_be2(buf + 436));
160174067Sbde			printf("v_sec %d\n", g_dec_be2(buf + 438));
16113107Sbde		}
162143063Sjoerg
16313107Sbde		csize = g_dec_be2(buf + 436) * g_dec_be2(buf + 438);
16413107Sbde
16513107Sbde		for (i = 0; i < 8; i++) {
16613107Sbde			v = g_dec_be4(buf + 444 + i * 8);
16713107Sbde			u = g_dec_be4(buf + 448 + i * 8);
16819000Sbde			if (u == 0)
16913107Sbde				continue;
17013107Sbde			npart++;
17113107Sbde			g_topology_lock();
17219000Sbde			pp2 = g_slice_addslice(gp, i,
173220433Sjkim			    ((off_t)v * csize) << 9ULL,
174220433Sjkim			    ((off_t)u) << 9ULL,
17519000Sbde			    sectorsize,
17619000Sbde			    "%s%c", pp->name, 'a' + i);
17719000Sbde			g_topology_unlock();
17813107Sbde			g_error_provider(pp2, 0);
17913107Sbde		}
180163756Sbde		break;
18132005Sphk	}
182129744Sbde	g_topology_lock();
183129744Sbde	error = g_access_rel(cp, -1, 0, 0);
184129744Sbde	if (npart > 0)
185129744Sbde		return (gp);
186129744Sbde	g_std_spoiled(cp);
187129744Sbde	return (NULL);
188129744Sbde}
189129744Sbde
190129744Sbdestatic struct g_class g_sunlabel_class = {
19119000Sbde	SUNLABEL_CLASS_NAME,
19219000Sbde	g_sunlabel_taste,
19319000Sbde	NULL,
194163756Sbde	G_CLASS_INITIALIZER
19519000Sbde};
19619000Sbde
19719000SbdeDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
19819000Sbde