geom_sunlabel.c revision 93776
190792Sgshapiro/*-
290792Sgshapiro * Copyright (c) 2002 Poul-Henning Kamp
390792Sgshapiro * Copyright (c) 2002 Networks Associates Technology, Inc.
490792Sgshapiro * All rights reserved.
590792Sgshapiro *
690792Sgshapiro * This software was developed for the FreeBSD Project by Poul-Henning Kamp
790792Sgshapiro * and NAI Labs, the Security Research Division of Network Associates, Inc.
890792Sgshapiro * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
990792Sgshapiro * DARPA CHATS research program.
1090792Sgshapiro *
1190792Sgshapiro * Redistribution and use in source and binary forms, with or without
1290792Sgshapiro * modification, are permitted provided that the following conditions
13120256Sgshapiro * are met:
14120256Sgshapiro * 1. Redistributions of source code must retain the above copyright
15120256Sgshapiro *    notice, this list of conditions and the following disclaimer.
1690792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
17120256Sgshapiro *    notice, this list of conditions and the following disclaimer in the
18120256Sgshapiro *    documentation and/or other materials provided with the distribution.
19120256Sgshapiro * 3. The names of the authors may not be used to endorse or promote
20120256Sgshapiro *    products derived from this software without specific prior written
21120256Sgshapiro *    permission.
22120256Sgshapiro *
23120256Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2490792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2590792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2690792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27120256Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2890792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2990792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3090792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3190792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3290792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3390792Sgshapiro * SUCH DAMAGE.
3490792Sgshapiro *
3590792Sgshapiro * $FreeBSD: head/sys/geom/geom_sunlabel.c 93776 2002-04-04 09:54:13Z phk $
3690792Sgshapiro */
3790792Sgshapiro
3890792Sgshapiro
3990792Sgshapiro#include <sys/param.h>
4090792Sgshapiro#ifndef _KERNEL
4190792Sgshapiro#include <stdio.h>
4290792Sgshapiro#include <string.h>
4390792Sgshapiro#include <stdlib.h>
4490792Sgshapiro#include <signal.h>
4590792Sgshapiro#include <err.h>
4690792Sgshapiro#else
4790792Sgshapiro#include <sys/systm.h>
4890792Sgshapiro#include <sys/kernel.h>
4990792Sgshapiro#include <sys/conf.h>
5090792Sgshapiro#include <sys/bio.h>
5190792Sgshapiro#include <sys/malloc.h>
5290792Sgshapiro#include <sys/lock.h>
5390792Sgshapiro#include <sys/mutex.h>
5490792Sgshapiro#endif
5590792Sgshapiro#include <sys/errno.h>
5690792Sgshapiro#include <geom/geom.h>
5790792Sgshapiro#include <geom/geom_slice.h>
5890792Sgshapiro#include <machine/endian.h>
5990792Sgshapiro
6090792Sgshapiro#define SUNLABEL_CLASS_NAME "SUNLABEL-class"
6190792Sgshapiro
6290792Sgshapirostruct g_sunlabel_softc {
6390792Sgshapiro	int foo;
6490792Sgshapiro};
6590792Sgshapiro
6690792Sgshapirostatic int
6790792Sgshapirog_sunlabel_start(struct bio *bp)
6890792Sgshapiro{
6990792Sgshapiro	struct g_geom *gp;
7090792Sgshapiro	struct g_sunlabel_softc *ms;
7190792Sgshapiro	struct g_slicer *gsp;
7290792Sgshapiro
7390792Sgshapiro	gp = bp->bio_to->geom;
7490792Sgshapiro	gsp = gp->softc;
7590792Sgshapiro	ms = gsp->softc;
7690792Sgshapiro	return (0);
7790792Sgshapiro}
7890792Sgshapiro
7990792Sgshapirostatic void
8090792Sgshapirog_sunlabel_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
8190792Sgshapiro{
8290792Sgshapiro
8390792Sgshapiro	g_slice_dumpconf(sb, indent, gp, cp, pp);
8490792Sgshapiro}
8590792Sgshapiro
8690792Sgshapirostatic struct g_geom *
8790792Sgshapirog_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
8890792Sgshapiro{
8990792Sgshapiro	struct g_geom *gp;
9090792Sgshapiro	struct g_consumer *cp;
9190792Sgshapiro	struct g_provider *pp2;
9290792Sgshapiro	int error, i, j, npart;
9390792Sgshapiro	u_char *buf;
9490792Sgshapiro	struct g_sunlabel_softc *ms;
9590792Sgshapiro	u_int secsize, u, v, csize;
9690792Sgshapiro	off_t mediasize;
9790792Sgshapiro
9890792Sgshapiro	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
9990792Sgshapiro	g_topology_assert();
10090792Sgshapiro	if (flags == G_TF_NORMAL &&
10190792Sgshapiro	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
10290792Sgshapiro		return (NULL);
10390792Sgshapiro	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
10490792Sgshapiro	if (gp == NULL)
105		return (NULL);
106	g_topology_unlock();
107	gp->dumpconf = g_sunlabel_dumpconf;
108	npart = 0;
109	while (1) {	/* a trick to allow us to use break */
110		if (gp->rank != 2 && flags == G_TF_NORMAL)
111			break;
112		j = sizeof secsize;
113		error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
114		if (error) {
115			secsize = 512;
116			printf("g_sunlabel_taste: error %d Sectors are %d bytes\n",
117			    error, secsize);
118		}
119		j = sizeof mediasize;
120		error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
121		if (error) {
122			mediasize = 0;
123			printf("g_error %d Mediasize is %lld bytes\n",
124			    error, (long long)mediasize);
125		}
126		buf = g_read_data(cp, 0, secsize, &error);
127		if (buf == NULL || error != 0)
128			break;
129
130		/* The second last short is a magic number */
131		if (g_dec_be2(buf + 508) != 0xdabe)
132			break;
133		/* The shortword parity of the entire thing must be even */
134		u = 0;
135		for (i = 0; i < 512; i += 2)
136			u ^= g_dec_be2(buf + i);
137		if (u != 0)
138			break;
139		if (bootverbose) {
140			g_hexdump(buf, 128);
141			for (i = 0; i < 8; i++) {
142				printf("part %d %u %u\n", i,
143				    g_dec_be4(buf + 444 + i * 8),
144				    g_dec_be4(buf + 448 + i * 8));
145			}
146			printf("v_version = %d\n", g_dec_be4(buf + 128));
147			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
148			for (i = 0; i < 8; i++) {
149				printf("v_part[%d] = %d %d\n",
150				    i, g_dec_be2(buf + 142 + i * 4),
151				    g_dec_be2(buf + 144 + i * 4));
152			}
153			printf("v_sanity %x\n", g_dec_be4(buf + 186));
154			printf("v_version = %d\n", g_dec_be4(buf + 128));
155			printf("v_rpm %d\n", g_dec_be2(buf + 420));
156			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
157			printf("v_cyl %d\n", g_dec_be2(buf + 432));
158			printf("v_alt %d\n", g_dec_be2(buf + 434));
159			printf("v_head %d\n", g_dec_be2(buf + 436));
160			printf("v_sec %d\n", g_dec_be2(buf + 438));
161		}
162
163		csize = g_dec_be2(buf + 436) * g_dec_be2(buf + 438);
164
165		for (i = 0; i < 8; i++) {
166			v = g_dec_be4(buf + 444 + i * 8);
167			u = g_dec_be4(buf + 448 + i * 8);
168			if (u == 0)
169				continue;
170			npart++;
171			pp2 = g_slice_addslice(gp, i,
172			    ((off_t)v * csize) << 9ULL,
173			    ((off_t)u) << 9ULL,
174			    "%s%c", pp->name, 'a' + i);
175			g_error_provider(pp2, 0);
176		}
177		break;
178	}
179	g_topology_lock();
180	error = g_access_rel(cp, -1, 0, 0);
181	if (npart > 0)
182		return (gp);
183	g_std_spoiled(cp);
184	return (NULL);
185}
186
187static struct g_class g_sunlabel_class = {
188	SUNLABEL_CLASS_NAME,
189	g_sunlabel_taste,
190	NULL,
191	G_CLASS_INITSTUFF
192};
193
194DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
195