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