geom_sunlabel.c revision 105551
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_sunlabel.c 105551 2002-10-20 20:28:24Z phk $
36 */
37
38
39#include <sys/param.h>
40#ifndef _KERNEL
41#include <stdio.h>
42#include <string.h>
43#include <stdlib.h>
44#include <signal.h>
45#include <err.h>
46#else
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/conf.h>
50#include <sys/bio.h>
51#include <sys/malloc.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#endif
55#include <geom/geom.h>
56#include <geom/geom_slice.h>
57#include <machine/endian.h>
58
59#define SUNLABEL_CLASS_NAME "SUN"
60
61struct g_sunlabel_softc {
62	int foo;
63};
64
65static int
66g_sunlabel_start(struct bio *bp)
67{
68	struct g_geom *gp;
69	struct g_sunlabel_softc *ms;
70	struct g_slicer *gsp;
71
72	gp = bp->bio_to->geom;
73	gsp = gp->softc;
74	ms = gsp->softc;
75	return (0);
76}
77
78static void
79g_sunlabel_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
80{
81
82	g_slice_dumpconf(sb, indent, gp, cp, pp);
83}
84
85static struct g_geom *
86g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
87{
88	struct g_geom *gp;
89	struct g_consumer *cp;
90	struct g_provider *pp2;
91	int error, i, npart;
92	u_char *buf;
93	struct g_sunlabel_softc *ms;
94	u_int sectorsize, u, v, csize;
95	off_t mediasize;
96	struct g_slicer *gsp;
97
98	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
99	g_topology_assert();
100	if (flags == G_TF_NORMAL &&
101	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
102		return (NULL);
103	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
104	if (gp == NULL)
105		return (NULL);
106	gsp = gp->softc;
107	g_topology_unlock();
108	gp->dumpconf = g_sunlabel_dumpconf;
109	npart = 0;
110	while (1) {	/* a trick to allow us to use break */
111		if (gp->rank != 2 && flags == G_TF_NORMAL)
112			break;
113		sectorsize = cp->provider->sectorsize;
114		if (sectorsize < 512)
115			break;
116		gsp->frontstuff = 16 * sectorsize;
117		mediasize = cp->provider->mediasize;
118		buf = g_read_data(cp, 0, sectorsize, &error);
119		if (buf == NULL || error != 0)
120			break;
121
122		/* The second last short is a magic number */
123		if (g_dec_be2(buf + 508) != 0xdabe)
124			break;
125		/* The shortword parity of the entire thing must be even */
126		u = 0;
127		for (i = 0; i < 512; i += 2)
128			u ^= g_dec_be2(buf + i);
129		if (u != 0)
130			break;
131		if (bootverbose) {
132			g_hexdump(buf, 128);
133			for (i = 0; i < 8; i++) {
134				printf("part %d %u %u\n", i,
135				    g_dec_be4(buf + 444 + i * 8),
136				    g_dec_be4(buf + 448 + i * 8));
137			}
138			printf("v_version = %d\n", g_dec_be4(buf + 128));
139			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
140			for (i = 0; i < 8; i++) {
141				printf("v_part[%d] = %d %d\n",
142				    i, g_dec_be2(buf + 142 + i * 4),
143				    g_dec_be2(buf + 144 + i * 4));
144			}
145			printf("v_sanity %x\n", g_dec_be4(buf + 186));
146			printf("v_version = %d\n", g_dec_be4(buf + 128));
147			printf("v_rpm %d\n", g_dec_be2(buf + 420));
148			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
149			printf("v_cyl %d\n", g_dec_be2(buf + 432));
150			printf("v_alt %d\n", g_dec_be2(buf + 434));
151			printf("v_head %d\n", g_dec_be2(buf + 436));
152			printf("v_sec %d\n", g_dec_be2(buf + 438));
153		}
154
155		csize = g_dec_be2(buf + 436) * g_dec_be2(buf + 438);
156
157		for (i = 0; i < 8; i++) {
158			v = g_dec_be4(buf + 444 + i * 8);
159			u = g_dec_be4(buf + 448 + i * 8);
160			if (u == 0)
161				continue;
162			npart++;
163			g_topology_lock();
164			pp2 = g_slice_addslice(gp, i,
165			    ((off_t)v * csize) << 9ULL,
166			    ((off_t)u) << 9ULL,
167			    sectorsize,
168			    "%s%c", pp->name, 'a' + i);
169			g_topology_unlock();
170			g_error_provider(pp2, 0);
171		}
172		break;
173	}
174	g_topology_lock();
175	error = g_access_rel(cp, -1, 0, 0);
176	if (npart > 0)
177		return (gp);
178	g_std_spoiled(cp);
179	return (NULL);
180}
181
182static struct g_class g_sunlabel_class = {
183	SUNLABEL_CLASS_NAME,
184	g_sunlabel_taste,
185	NULL,
186	G_CLASS_INITIALIZER
187};
188
189DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
190