geom_sunlabel.c revision 92372
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 92372 2002-03-15 21:44:08Z 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 <sys/errno.h>
56#include <geom/geom.h>
57#include <geom/geom_slice.h>
58#include <machine/endian.h>
59
60#define BSD_METHOD_NAME "SUNLABEL-method"
61
62static u_int
63g_u16be(u_char *p)
64{
65	return ((p[0] << 8) | p[1]);
66}
67
68static u_int
69g_u32be(u_char *p)
70{
71	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
72}
73
74
75struct g_sunlabel_softc {
76	int foo;
77};
78
79static int
80g_sunlabel_start(struct bio *bp)
81{
82	struct g_geom *gp;
83	struct g_sunlabel_softc *ms;
84	struct g_slicer *gsp;
85
86	gp = bp->bio_to->geom;
87	gsp = gp->softc;
88	ms = gsp->softc;
89	return (0);
90}
91
92static void
93g_sunlabel_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
94{
95
96	g_slice_dumpconf(sb, indent, gp, cp, pp);
97}
98
99static struct g_geom *
100g_sunlabel_taste(struct g_method *mp, struct g_provider *pp, struct thread *tp, int flags)
101{
102	struct g_geom *gp;
103	struct g_consumer *cp;
104	struct g_provider *pp2;
105	int error, i, j, npart;
106	u_char *buf;
107	struct g_sunlabel_softc *ms;
108	u_int secsize, u, v, csize;
109	off_t mediasize;
110
111	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
112	g_topology_assert();
113	if (flags == G_TF_NORMAL &&
114	    !strcmp(pp->geom->method->name, BSD_METHOD_NAME))
115		return (NULL);
116	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
117	if (gp == NULL)
118		return (NULL);
119	g_topology_unlock();
120	gp->dumpconf = g_sunlabel_dumpconf;
121	npart = 0;
122	while (1) {	/* a trick to allow us to use break */
123		if (gp->rank != 2 && flags == G_TF_NORMAL)
124			break;
125		j = sizeof secsize;
126		error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize, tp);
127		if (error) {
128			secsize = 512;
129			printf("g_sunlabel_taste: error %d Sectors are %d bytes\n",
130			    error, secsize);
131		}
132		j = sizeof mediasize;
133		error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize, tp);
134		if (error) {
135			mediasize = 0;
136			printf("g_error %d Mediasize is %lld bytes\n",
137			    error, (long long)mediasize);
138		}
139		buf = g_read_data(cp, 0, secsize, &error);
140		if (buf == NULL || error != 0)
141			break;
142
143		/* The second last short is a magic number */
144		if (g_u16be(buf + 508) != 0xdabe)
145			break;
146		/* The shortword parity of the entire thing must be even */
147		u = 0;
148		for (i = 0; i < 512; i += 2)
149			u ^= g_u16be(buf + i);
150		if (u != 0)
151			break;
152		if (bootverbose) {
153			g_hexdump(buf, 128);
154			for (i = 0; i < 8; i++) {
155				printf("part %d %u %u\n", i,
156				    g_u32be(buf + 444 + i * 8),
157				    g_u32be(buf + 448 + i * 8));
158			}
159			printf("v_version = %d\n", g_u32be(buf + 128));
160			printf("v_nparts = %d\n", g_u16be(buf + 140));
161			for (i = 0; i < 8; i++) {
162				printf("v_part[%d] = %d %d\n",
163				    i, g_u16be(buf + 142 + i * 4),
164				    g_u16be(buf + 144 + i * 4));
165			}
166			printf("v_sanity %x\n", g_u32be(buf + 186));
167			printf("v_version = %d\n", g_u32be(buf + 128));
168			printf("v_rpm %d\n", g_u16be(buf + 420));
169			printf("v_totalcyl %d\n", g_u16be(buf + 422));
170			printf("v_cyl %d\n", g_u16be(buf + 432));
171			printf("v_alt %d\n", g_u16be(buf + 434));
172			printf("v_head %d\n", g_u16be(buf + 436));
173			printf("v_sec %d\n", g_u16be(buf + 438));
174		}
175
176		csize = g_u16be(buf + 436) * g_u16be(buf + 438);
177
178		for (i = 0; i < 8; i++) {
179			v = g_u32be(buf + 444 + i * 8);
180			u = g_u32be(buf + 448 + i * 8);
181			if (u == 0)
182				continue;
183			npart++;
184			pp2 = g_slice_addslice(gp, i,
185			    ((off_t)v * csize) << 9ULL,
186			    ((off_t)u) << 9ULL,
187			    "%s%c", pp->name, 'a' + i);
188			g_error_provider(pp2, 0);
189		}
190		break;
191	}
192	g_topology_lock();
193	error = g_access_rel(cp, -1, 0, 0);
194	if (npart > 0)
195		return (gp);
196	g_std_spoiled(cp);
197	return (NULL);
198}
199
200static struct g_method g_sunlabel_method = {
201	BSD_METHOD_NAME,
202	g_sunlabel_taste,
203	g_slice_access,
204	g_slice_orphan,
205	NULL,
206	G_METHOD_INITSTUFF
207};
208
209DECLARE_GEOM_METHOD(g_sunlabel_method, g_sunlabel);
210