geom_sunlabel.c revision 112552
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 112552 2003-03-24 19:30:15Z 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 <sys/errno.h>
46#include <err.h>
47#else
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/conf.h>
51#include <sys/bio.h>
52#include <sys/malloc.h>
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#endif
56#include <geom/geom.h>
57#include <geom/geom_slice.h>
58#include <machine/endian.h>
59
60#define SUNLABEL_CLASS_NAME "SUN"
61
62struct g_sunlabel_softc {
63	int sectorsize;
64	int nheads;
65	int nsects;
66	int nalt;
67};
68
69static int
70g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
71{
72	int i, error;
73	u_int u, v, csize;
74
75	/* The second last short is a magic number */
76	if (g_dec_be2(sec0 + 508) != 0xdabe)
77		return (EBUSY);
78
79	/* The shortword parity of the entire thing must be even */
80	u = 0;
81	for (i = 0; i < 512; i += 2)
82		u ^= g_dec_be2(sec0 + i);
83	if (u != 0)
84		return(EBUSY);
85
86	csize = g_dec_be2(sec0 + 436) * g_dec_be2(sec0 + 438);
87
88	for (i = 0; i < 8; i++) {
89		v = g_dec_be4(sec0 + 444 + i * 8);
90		u = g_dec_be4(sec0 + 448 + i * 8);
91		g_topology_lock();
92		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
93		    ((off_t)v * csize) << 9ULL,
94		    ((off_t)u) << 9ULL,
95		    ms->sectorsize,
96		    "%s%c", gp->name, 'a' + i);
97		g_topology_unlock();
98		if (error)
99			return (error);
100	}
101	for (i = 0; i < 8; i++) {
102		v = g_dec_be4(sec0 + 444 + i * 8);
103		u = g_dec_be4(sec0 + 448 + i * 8);
104		g_topology_lock();
105		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
106		    ((off_t)v * csize) << 9ULL,
107		    ((off_t)u) << 9ULL,
108		    ms->sectorsize,
109		    "%s%c", gp->name, 'a' + i);
110		g_topology_unlock();
111	}
112	ms->nalt = g_dec_be2(sec0 + 434);
113	ms->nheads = g_dec_be2(sec0 + 436);
114	ms->nsects = g_dec_be2(sec0 + 438);
115
116	return (0);
117}
118
119static int
120g_sunlabel_start(struct bio *bp)
121{
122	struct g_geom *gp;
123	struct g_sunlabel_softc *ms;
124	struct g_slicer *gsp;
125
126	gp = bp->bio_to->geom;
127	gsp = gp->softc;
128	ms = gsp->softc;
129	return (0);
130}
131
132static void
133g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
134{
135	struct g_slicer *gsp;
136	struct g_sunlabel_softc *ms;
137
138	gsp = gp->softc;
139	ms = gsp->softc;
140	g_slice_dumpconf(sb, indent, gp, cp, pp);
141	if (indent == NULL) {
142		sbuf_printf(sb, " sc %u hd %u alt %u",
143		    ms->nsects, ms->nheads, ms->nalt);
144	}
145}
146
147static struct g_geom *
148g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
149{
150	struct g_geom *gp;
151	struct g_consumer *cp;
152	int error, i, npart;
153	u_char *buf;
154	struct g_sunlabel_softc *ms;
155	off_t mediasize;
156	struct g_slicer *gsp;
157
158	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
159	g_topology_assert();
160	if (flags == G_TF_NORMAL &&
161	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
162		return (NULL);
163	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
164	if (gp == NULL)
165		return (NULL);
166	gsp = gp->softc;
167	g_topology_unlock();
168	gp->dumpconf = g_sunlabel_dumpconf;
169	npart = 0;
170	do {	/* a trick to allow us to use break */
171		if (gp->rank != 2 && flags == G_TF_NORMAL)
172			break;
173		ms->sectorsize = cp->provider->sectorsize;
174		if (ms->sectorsize < 512)
175			break;
176		gsp->frontstuff = 16 * ms->sectorsize;
177		mediasize = cp->provider->mediasize;
178		buf = g_read_data(cp, 0, ms->sectorsize, &error);
179		if (buf == NULL || error != 0)
180			break;
181
182		if (bootverbose) {
183			g_hexdump(buf, 128);
184			for (i = 0; i < 8; i++) {
185				printf("part %d %u %u\n", i,
186				    g_dec_be4(buf + 444 + i * 8),
187				    g_dec_be4(buf + 448 + i * 8));
188			}
189			printf("v_version = %d\n", g_dec_be4(buf + 128));
190			printf("v_nparts = %d\n", g_dec_be2(buf + 140));
191			for (i = 0; i < 8; i++) {
192				printf("v_part[%d] = %d %d\n",
193				    i, g_dec_be2(buf + 142 + i * 4),
194				    g_dec_be2(buf + 144 + i * 4));
195			}
196			printf("v_sanity %x\n", g_dec_be4(buf + 186));
197			printf("v_version = %d\n", g_dec_be4(buf + 128));
198			printf("v_rpm %d\n", g_dec_be2(buf + 420));
199			printf("v_totalcyl %d\n", g_dec_be2(buf + 422));
200			printf("v_cyl %d\n", g_dec_be2(buf + 432));
201			printf("v_alt %d\n", g_dec_be2(buf + 434));
202			printf("v_head %d\n", g_dec_be2(buf + 436));
203			printf("v_sec %d\n", g_dec_be2(buf + 438));
204		}
205
206		g_sunlabel_modify(gp, ms, buf);
207
208		break;
209	} while (0);
210	g_topology_lock();
211	g_access_rel(cp, -1, 0, 0);
212	if (LIST_EMPTY(&gp->provider)) {
213		g_std_spoiled(cp);
214		return (NULL);
215#ifdef notyet
216	} else {
217		g_slice_conf_hot(gp, 0, 0, ms->sectorsize);
218#endif
219	}
220	return (gp);
221}
222
223static struct g_class g_sunlabel_class = {
224	.name = SUNLABEL_CLASS_NAME,
225	.taste = g_sunlabel_taste,
226	G_CLASS_INITIALIZER
227};
228
229DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
230