geom_pc98.c revision 230643
1178676Ssam/*-
2178676Ssam * Copyright (c) 2002 Poul-Henning Kamp
3178676Ssam * Copyright (c) 2002 Networks Associates Technology, Inc.
4178676Ssam * All rights reserved.
5178676Ssam *
6178676Ssam * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7178676Ssam * and NAI Labs, the Security Research Division of Network Associates, Inc.
8178676Ssam * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9178676Ssam * DARPA CHATS research program.
10178676Ssam *
11178676Ssam * Redistribution and use in source and binary forms, with or without
12178676Ssam * modification, are permitted provided that the following conditions
13178676Ssam * are met:
14178676Ssam * 1. Redistributions of source code must retain the above copyright
15178676Ssam *    notice, this list of conditions and the following disclaimer.
16178676Ssam * 2. Redistributions in binary form must reproduce the above copyright
17178676Ssam *    notice, this list of conditions and the following disclaimer in the
18178676Ssam *    documentation and/or other materials provided with the distribution.
19178676Ssam *
20178676Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21178676Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22178676Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23178676Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24178676Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25178676Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26178676Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27178676Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28261971Sbrueffer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29206622Suqs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30178676Ssam * SUCH DAMAGE.
31178676Ssam */
32178676Ssam
33233568Sbschmidt#include <sys/cdefs.h>
34178676Ssam__FBSDID("$FreeBSD: head/sys/geom/geom_pc98.c 230643 2012-01-28 14:00:21Z attilio $");
35178676Ssam
36178676Ssam#include <sys/param.h>
37178676Ssam#include <sys/endian.h>
38178676Ssam#include <sys/systm.h>
39178676Ssam#include <sys/sysctl.h>
40178676Ssam#include <sys/kernel.h>
41178676Ssam#include <sys/fcntl.h>
42178676Ssam#include <sys/malloc.h>
43178676Ssam#include <sys/bio.h>
44178676Ssam#include <sys/lock.h>
45198521Sbrueffer#include <sys/mutex.h>
46198521Sbrueffer#include <sys/proc.h>
47198470Srpaulo#include <sys/sbuf.h>
48261971Sbrueffer
49261971Sbrueffer#include <sys/diskpc98.h>
50261971Sbrueffer#include <geom/geom.h>
51198470Srpaulo#include <geom/geom_slice.h>
52198470Srpaulo
53203691SbrucecFEATURE(geom_pc98, "GEOM NEC PC9800 partitioning support");
54203691Sbrucec
55233568Sbschmidt#define PC98_CLASS_NAME "PC98"
56233568Sbschmidt
57210289Sbschmidtstruct g_pc98_softc {
58198470Srpaulo	u_int fwsectors, fwheads, sectorsize;
59198470Srpaulo	int type[NDOSPART];
60198470Srpaulo	u_char sec[8192];
61198470Srpaulo};
62198470Srpaulo
63198470Srpaulostatic void
64198470Srpaulog_pc98_print(int i, struct pc98_partition *dp)
65198470Srpaulo{
66198470Srpaulo	char sname[17];
67178676Ssam
68198521Sbrueffer	strncpy(sname, dp->dp_name, 16);
69178676Ssam	sname[16] = '\0';
70178676Ssam
71178676Ssam	hexdump(dp, sizeof(dp[0]), NULL, 0);
72261971Sbrueffer	printf("[%d] mid:%d(0x%x) sid:%d(0x%x)",
73261971Sbrueffer	       i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid);
74261971Sbrueffer	printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect);
75198470Srpaulo	printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect);
76198470Srpaulo	printf(" sname:%s\n", sname);
77203691Sbrucec}
78203691Sbrucec
79233568Sbschmidt/*
80233568Sbschmidt * XXX: Add gctl_req arg and give good error msgs.
81210289Sbschmidt * XXX: Check that length argument does not bring boot code inside any slice.
82178676Ssam */
83178676Ssamstatic int
84178676Ssamg_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec, int len __unused)
85178676Ssam{
86233568Sbschmidt	int i, error;
87233568Sbschmidt	off_t s[NDOSPART], l[NDOSPART];
88233568Sbschmidt	struct pc98_partition dp[NDOSPART];
89233568Sbschmidt
90233568Sbschmidt	g_topology_assert();
91233568Sbschmidt
92233568Sbschmidt	if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa)
93233568Sbschmidt		return (EBUSY);
94233568Sbschmidt
95233568Sbschmidt#if 0
96233568Sbschmidt	/*
97233568Sbschmidt	 * By convetion, it seems that the ipl program has a jump at location
98233568Sbschmidt	 * 0 to the real start of the boot loader.  By convetion, it appears
99233568Sbschmidt	 * that after this jump, there's a string, terminated by at last one,
100233568Sbschmidt	 * if not more, zeros, followed by the target of the jump.  FreeBSD's
101233568Sbschmidt	 * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for
102233568Sbschmidt	 * compatibility with some older boot loader.  Linux98's boot loader
103233568Sbschmidt	 * appears to use 'Linux 98' followed by only two.  GRUB/98 appears to
104233568Sbschmidt	 * use 'GRUB/98 ' followed by none.  These last two appear to be
105178676Ssam	 * ported from the ia32 versions, but appear to show similar
106178676Ssam	 * convention.  Grub/98 has an additional NOP after the jmp, which
107248507Sglebius	 * isn't present in others.
108178676Ssam	 *
109178676Ssam	 * The following test was inspired by looking only at partitions
110178676Ssam	 * with FreeBSD's boot0 (or one that it is compatible with).  As
111178676Ssam	 * such, if failed when other IPL programs were used.
112178676Ssam	 */
113178676Ssam	if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1')
114178676Ssam		return (EBUSY);
115178676Ssam#endif
116178676Ssam
117178676Ssam	for (i = 0; i < NDOSPART; i++)
118178676Ssam		pc98_partition_dec(
119178676Ssam			sec + 512 + i * sizeof(struct pc98_partition), &dp[i]);
120178676Ssam
121178676Ssam	for (i = 0; i < NDOSPART; i++) {
122178676Ssam		/* If start and end are identical it's bogus */
123178676Ssam		if (dp[i].dp_ssect == dp[i].dp_esect &&
124178676Ssam		    dp[i].dp_shd == dp[i].dp_ehd &&
125178676Ssam		    dp[i].dp_scyl == dp[i].dp_ecyl)
126178676Ssam			s[i] = l[i] = 0;
127178676Ssam		else if (dp[i].dp_ecyl == 0)
128178676Ssam			s[i] = l[i] = 0;
129178676Ssam		else {
130178676Ssam			s[i] = (off_t)dp[i].dp_scyl *
131178676Ssam				ms->fwsectors * ms->fwheads * ms->sectorsize;
132178676Ssam			l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) *
133178676Ssam				ms->fwsectors * ms->fwheads * ms->sectorsize;
134178676Ssam		}
135178676Ssam		if (bootverbose) {
136178676Ssam			printf("PC98 Slice %d on %s:\n", i + 1, gp->name);
137178676Ssam			g_pc98_print(i, dp + i);
138178676Ssam		}
139178676Ssam		if (s[i] < 0 || l[i] < 0)
140178676Ssam			error = EBUSY;
141178676Ssam		else
142178676Ssam			error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
143178676Ssam				       s[i], l[i], ms->sectorsize,
144178676Ssam				       "%ss%d", gp->name, i + 1);
145178676Ssam		if (error)
146178676Ssam			return (error);
147178676Ssam	}
148178676Ssam
149178676Ssam	for (i = 0; i < NDOSPART; i++) {
150178676Ssam		ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid;
151178676Ssam		g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i],
152178676Ssam			       ms->sectorsize, "%ss%d", gp->name, i + 1);
153178676Ssam	}
154178676Ssam
155178676Ssam	bcopy(sec, ms->sec, sizeof (ms->sec));
156178676Ssam
157233648Seadler	return (0);
158178676Ssam}
159193884Ssam
160178676Ssamstatic int
161178676Ssamg_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
162178676Ssam{
163178676Ssam	struct g_geom *gp;
164178676Ssam	struct g_pc98_softc *ms;
165178676Ssam	struct g_slicer *gsp;
166178676Ssam	struct g_consumer *cp;
167178676Ssam	int error, opened;
168178676Ssam
169178676Ssam	gp = pp->geom;
170178676Ssam	gsp = gp->softc;
171178676Ssam	ms = gsp->softc;
172178676Ssam
173191950Ssam	opened = 0;
174178676Ssam	error = 0;
175178676Ssam	switch(cmd) {
176178676Ssam	case DIOCSPC98: {
177178676Ssam		if (!(fflag & FWRITE))
178178676Ssam			return (EPERM);
179178676Ssam		DROP_GIANT();
180178966Sbrueffer		g_topology_lock();
181178676Ssam		cp = LIST_FIRST(&gp->consumer);
182178676Ssam		if (cp->acw == 0) {
183178676Ssam			error = g_access(cp, 0, 1, 0);
184178676Ssam			if (error == 0)
185235317Sgjb				opened = 1;
186		}
187		if (!error)
188			error = g_pc98_modify(gp, ms, data, 8192);
189		if (!error)
190			error = g_write_data(cp, 0, data, 8192);
191		if (opened)
192			g_access(cp, 0, -1 , 0);
193		g_topology_unlock();
194		PICKUP_GIANT();
195		return(error);
196	}
197	default:
198		return (ENOIOCTL);
199	}
200}
201
202static int
203g_pc98_start(struct bio *bp)
204{
205	struct g_provider *pp;
206	struct g_geom *gp;
207	struct g_pc98_softc *mp;
208	struct g_slicer *gsp;
209	int idx;
210
211	pp = bp->bio_to;
212	idx = pp->index;
213	gp = pp->geom;
214	gsp = gp->softc;
215	mp = gsp->softc;
216	if (bp->bio_cmd == BIO_GETATTR) {
217		if (g_handleattr_int(bp, "PC98::type", mp->type[idx]))
218			return (1);
219		if (g_handleattr_off_t(bp, "PC98::offset",
220				       gsp->slices[idx].offset))
221			return (1);
222	}
223
224	return (0);
225}
226
227static void
228g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
229		struct g_consumer *cp __unused, struct g_provider *pp)
230{
231	struct g_pc98_softc *mp;
232	struct g_slicer *gsp;
233	struct pc98_partition dp;
234	char sname[17];
235
236	gsp = gp->softc;
237	mp = gsp->softc;
238	g_slice_dumpconf(sb, indent, gp, cp, pp);
239	if (pp != NULL) {
240		pc98_partition_dec(
241			mp->sec + 512 +
242			pp->index * sizeof(struct pc98_partition), &dp);
243		strncpy(sname, dp.dp_name, 16);
244		sname[16] = '\0';
245		if (indent == NULL) {
246			sbuf_printf(sb, " ty %d", mp->type[pp->index]);
247			sbuf_printf(sb, " sn %s", sname);
248		} else {
249			sbuf_printf(sb, "%s<type>%d</type>\n", indent,
250				    mp->type[pp->index]);
251			sbuf_printf(sb, "%s<sname>%s</sname>\n", indent,
252				    sname);
253		}
254	}
255}
256
257static struct g_geom *
258g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags)
259{
260	struct g_geom *gp;
261	struct g_consumer *cp;
262	int error;
263	struct g_pc98_softc *ms;
264	u_int fwsectors, fwheads, sectorsize;
265	u_char *buf;
266
267	g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name);
268	g_topology_assert();
269	if (flags == G_TF_NORMAL &&
270	    !strcmp(pp->geom->class->name, PC98_CLASS_NAME))
271		return (NULL);
272	gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start);
273	if (gp == NULL)
274		return (NULL);
275	g_topology_unlock();
276	do {
277		if (gp->rank != 2 && flags == G_TF_NORMAL)
278			break;
279		error = g_getattr("GEOM::fwsectors", cp, &fwsectors);
280		if (error || fwsectors == 0) {
281			fwsectors = 17;
282			if (bootverbose)
283				printf("g_pc98_taste: guessing %d sectors\n",
284				    fwsectors);
285		}
286		error = g_getattr("GEOM::fwheads", cp, &fwheads);
287		if (error || fwheads == 0) {
288			fwheads = 8;
289			if (bootverbose)
290				printf("g_pc98_taste: guessing %d heads\n",
291				    fwheads);
292		}
293		sectorsize = cp->provider->sectorsize;
294		if (sectorsize % 512 != 0)
295			break;
296		buf = g_read_data(cp, 0, 8192, NULL);
297		if (buf == NULL)
298			break;
299		ms->fwsectors = fwsectors;
300		ms->fwheads = fwheads;
301		ms->sectorsize = sectorsize;
302		g_topology_lock();
303		g_pc98_modify(gp, ms, buf, 8192);
304		g_topology_unlock();
305		g_free(buf);
306		break;
307	} while (0);
308	g_topology_lock();
309	g_access(cp, -1, 0, 0);
310	if (LIST_EMPTY(&gp->provider)) {
311		g_slice_spoiled(cp);
312		return (NULL);
313	}
314	return (gp);
315}
316
317static void
318g_pc98_config(struct gctl_req *req, struct g_class *mp, const char *verb)
319{
320	struct g_geom *gp;
321	struct g_consumer *cp;
322	struct g_pc98_softc *ms;
323	struct g_slicer *gsp;
324	int opened = 0, error = 0;
325	void *data;
326	int len;
327
328	g_topology_assert();
329	gp = gctl_get_geom(req, mp, "geom");
330	if (gp == NULL)
331		return;
332	if (strcmp(verb, "write PC98")) {
333		gctl_error(req, "Unknown verb");
334		return;
335	}
336	gsp = gp->softc;
337	ms = gsp->softc;
338	data = gctl_get_param(req, "data", &len);
339	if (data == NULL)
340		return;
341	if (len < 8192 || (len % 512)) {
342		gctl_error(req, "Wrong request length");
343		return;
344	}
345	cp = LIST_FIRST(&gp->consumer);
346	if (cp->acw == 0) {
347		error = g_access(cp, 0, 1, 0);
348		if (error == 0)
349			opened = 1;
350	}
351	if (!error)
352		error = g_pc98_modify(gp, ms, data, len);
353	if (error)
354		gctl_error(req, "conflict with open slices");
355	if (!error)
356		error = g_write_data(cp, 0, data, len);
357	if (error)
358		gctl_error(req, "sector zero write failed");
359	if (opened)
360		g_access(cp, 0, -1 , 0);
361	return;
362}
363
364static struct g_class g_pc98_class = {
365	.name = PC98_CLASS_NAME,
366	.version = G_VERSION,
367	.taste = g_pc98_taste,
368	.dumpconf = g_pc98_dumpconf,
369	.ctlreq = g_pc98_config,
370	.ioctl = g_pc98_ioctl,
371};
372
373DECLARE_GEOM_CLASS(g_pc98_class, g_pc98);
374