geom_pc98.c revision 223921
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/geom/geom_pc98.c 223921 2011-07-11 05:22:31Z ae $");
35
36#include <sys/param.h>
37#include <sys/endian.h>
38#include <sys/systm.h>
39#include <sys/sysctl.h>
40#include <sys/kernel.h>
41#include <sys/fcntl.h>
42#include <sys/malloc.h>
43#include <sys/bio.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/sbuf.h>
47
48#include <sys/diskpc98.h>
49#include <geom/geom.h>
50#include <geom/geom_slice.h>
51
52FEATURE(geom_pc98, "GEOM NEC PC9800 partitioning support");
53
54#define PC98_CLASS_NAME "PC98"
55
56struct g_pc98_softc {
57	u_int fwsectors, fwheads, sectorsize;
58	int type[NDOSPART];
59	u_char sec[8192];
60};
61
62static void
63g_pc98_print(int i, struct pc98_partition *dp)
64{
65	char sname[17];
66
67	strncpy(sname, dp->dp_name, 16);
68	sname[16] = '\0';
69
70	hexdump(dp, sizeof(dp[0]), NULL, 0);
71	printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
72	       i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
73	printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
74	printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
75	printf(" sname:%s\n", sname);
76}
77
78/*
79 * XXX: Add gctl_req arg and give good error msgs.
80 * XXX: Check that length argument does not bring boot code inside any slice.
81 */
82static int
83g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec, int len __unused)
84{
85	int i, error;
86	off_t s[NDOSPART], l[NDOSPART];
87	struct pc98_partition dp[NDOSPART];
88
89	g_topology_assert();
90
91	if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
92		return (EBUSY);
93
94#if 0
95	/*
96	 * By convetion, it seems that the ipl program has a jump at location
97	 * 0 to the real start of the boot loader.  By convetion, it appears
98	 * that after this jump, there's a string, terminated by at last one,
99	 * if not more, zeros, followed by the target of the jump.  FreeBSD's
100	 * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for
101	 * compatibility with some older boot loader.  Linux98's boot loader
102	 * appears to use 'Linux 98' followed by only two.  GRUB/98 appears to
103	 * use 'GRUB/98 ' followed by none.  These last two appear to be
104	 * ported from the ia32 versions, but appear to show similar
105	 * convention.  Grub/98 has an additional NOP after the jmp, which
106	 * isn't present in others.
107	 *
108	 * The following test was inspired by looking only at partitions
109	 * with FreeBSD's boot0 (or one that it is compatible with).  As
110	 * such, if failed when other IPL programs were used.
111	 */
112	if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
113		return (EBUSY);
114#endif
115
116	for (i = 0; i < NDOSPART; i++)
117		pc98_partition_dec(
118			sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
119
120	for (i = 0; i < NDOSPART; i++) {
121		/* If start and end are identical it's bogus */
122		if (dp[i].dp_ssect == dp[i].dp_esect &&
123		    dp[i].dp_shd == dp[i].dp_ehd &&
124		    dp[i].dp_scyl == dp[i].dp_ecyl)
125			s[i] = l[i] = 0;
126		else if (dp[i].dp_ecyl == 0)
127			s[i] = l[i] = 0;
128		else {
129			s[i] = (off_t)dp[i].dp_scyl *
130				ms->fwsectors * ms->fwheads * ms->sectorsize;
131			l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
132				ms->fwsectors * ms->fwheads * ms->sectorsize;
133		}
134		if (bootverbose) {
135			printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
136			g_pc98_print(i, dp + i);
137		}
138		if (s[i] < 0 || l[i] < 0)
139			error = EBUSY;
140		else
141			error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
142				       s[i], l[i], ms->sectorsize,
143				       "%ss%d", gp->name, i + 1);
144		if (error)
145			return (error);
146	}
147
148	for (i = 0; i < NDOSPART; i++) {
149		ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
150		g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
151			       ms->sectorsize, "%ss%d", gp->name, i + 1);
152	}
153
154	bcopy(sec, ms->sec, sizeof (ms->sec));
155
156	return (0);
157}
158
159static int
160g_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
161{
162	struct g_geom *gp;
163	struct g_pc98_softc *ms;
164	struct g_slicer *gsp;
165	struct g_consumer *cp;
166	int error, opened;
167
168	gp = pp->geom;
169	gsp = gp->softc;
170	ms = gsp->softc;
171
172	opened = 0;
173	error = 0;
174	switch(cmd) {
175	case DIOCSPC98: {
176		if (!(fflag & FWRITE))
177			return (EPERM);
178		DROP_GIANT();
179		g_topology_lock();
180		cp = LIST_FIRST(&gp->consumer);
181		if (cp->acw == 0) {
182			error = g_access(cp, 0, 1, 0);
183			if (error == 0)
184				opened = 1;
185		}
186		if (!error)
187			error = g_pc98_modify(gp, ms, data, 8192);
188		if (!error)
189			error = g_write_data(cp, 0, data, 8192);
190		if (opened)
191			g_access(cp, 0, -1 , 0);
192		g_topology_unlock();
193		PICKUP_GIANT();
194		return(error);
195	}
196	default:
197		return (ENOIOCTL);
198	}
199}
200
201static int
202g_pc98_start(struct bio *bp)
203{
204	struct g_provider *pp;
205	struct g_geom *gp;
206	struct g_pc98_softc *mp;
207	struct g_slicer *gsp;
208	int idx;
209
210	pp = bp->bio_to;
211	idx = pp->index;
212	gp = pp->geom;
213	gsp = gp->softc;
214	mp = gsp->softc;
215	if (bp->bio_cmd == BIO_GETATTR) {
216		if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
217			return (1);
218		if (g_handleattr_off_t(bp, "PC98::offset",
219				       gsp->slices[idx].offset))
220			return (1);
221	}
222
223	return (0);
224}
225
226static void
227g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
228		struct g_consumer *cp __unused, struct g_provider *pp)
229{
230	struct g_pc98_softc *mp;
231	struct g_slicer *gsp;
232	struct pc98_partition dp;
233	char sname[17];
234
235	gsp = gp->softc;
236	mp = gsp->softc;
237	g_slice_dumpconf(sb, indent, gp, cp, pp);
238	if (pp != NULL) {
239		pc98_partition_dec(
240			mp->sec + 512 +
241			pp->index * sizeof(struct pc98_partition), &dp);
242		strncpy(sname, dp.dp_name, 16);
243		sname[16] = '\0';
244		if (indent == NULL) {
245			sbuf_printf(sb, " ty %d", mp->type[pp->index]);
246			sbuf_printf(sb, " sn %s", sname);
247		} else {
248			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
249				    mp->type[pp->index]);
250			sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
251				    sname);
252		}
253	}
254}
255
256static struct g_geom *
257g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
258{
259	struct g_geom *gp;
260	struct g_consumer *cp;
261	int error;
262	struct g_pc98_softc *ms;
263	u_int fwsectors, fwheads, sectorsize;
264	u_char *buf;
265
266	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
267	g_topology_assert();
268	if (flags == G_TF_NORMAL &&
269	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
270		return (NULL);
271	gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
272	if (gp == NULL)
273		return (NULL);
274	g_topology_unlock();
275	do {
276		if (gp->rank != 2 && flags == G_TF_NORMAL)
277			break;
278		error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
279		if (error || fwsectors == 0) {
280			fwsectors = 17;
281			if (bootverbose)
282				printf("g_pc98_taste: guessing %d sectors\n",
283				    fwsectors);
284		}
285		error = g_getattr("GEOM::fwheads", cp, &fwheads);
286		if (error || fwheads == 0) {
287			fwheads = 8;
288			if (bootverbose)
289				printf("g_pc98_taste: guessing %d heads\n",
290				    fwheads);
291		}
292		sectorsize = cp->provider->sectorsize;
293		if (sectorsize % 512 != 0)
294			break;
295		buf = g_read_data(cp, 0, 8192, NULL);
296		if (buf == NULL)
297			break;
298		ms->fwsectors = fwsectors;
299		ms->fwheads = fwheads;
300		ms->sectorsize = sectorsize;
301		g_topology_lock();
302		g_pc98_modify(gp, ms, buf, 8192);
303		g_topology_unlock();
304		g_free(buf);
305		break;
306	} while (0);
307	g_topology_lock();
308	g_access(cp, -1, 0, 0);
309	if (LIST_EMPTY(&gp->provider)) {
310		g_slice_spoiled(cp);
311		return (NULL);
312	}
313	return (gp);
314}
315
316static void
317g_pc98_config(struct gctl_req *req, struct g_class *mp, const char *verb)
318{
319	struct g_geom *gp;
320	struct g_consumer *cp;
321	struct g_pc98_softc *ms;
322	struct g_slicer *gsp;
323	int opened = 0, error = 0;
324	void *data;
325	int len;
326
327	g_topology_assert();
328	gp = gctl_get_geom(req, mp, "geom");
329	if (gp == NULL)
330		return;
331	if (strcmp(verb, "write PC98")) {
332		gctl_error(req, "Unknown verb");
333		return;
334	}
335	gsp = gp->softc;
336	ms = gsp->softc;
337	data = gctl_get_param(req, "data", &len);
338	if (data == NULL)
339		return;
340	if (len < 8192 || (len % 512)) {
341		gctl_error(req, "Wrong request length");
342		return;
343	}
344	cp = LIST_FIRST(&gp->consumer);
345	if (cp->acw == 0) {
346		error = g_access(cp, 0, 1, 0);
347		if (error == 0)
348			opened = 1;
349	}
350	if (!error)
351		error = g_pc98_modify(gp, ms, data, len);
352	if (error)
353		gctl_error(req, "conflict with open slices");
354	if (!error)
355		error = g_write_data(cp, 0, data, len);
356	if (error)
357		gctl_error(req, "sector zero write failed");
358	if (opened)
359		g_access(cp, 0, -1 , 0);
360	return;
361}
362
363static struct g_class g_pc98_class = {
364	.name = PC98_CLASS_NAME,
365	.version = G_VERSION,
366	.taste = g_pc98_taste,
367	.dumpconf = g_pc98_dumpconf,
368	.ctlreq = g_pc98_config,
369	.ioctl = g_pc98_ioctl,
370};
371
372DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
373