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