geom_pc98.c revision 256281
1155192Srwatson/*- 2155192Srwatson * Copyright (c) 2002 Poul-Henning Kamp 3155192Srwatson * Copyright (c) 2002 Networks Associates Technology, Inc. 4155192Srwatson * All rights reserved. 5155192Srwatson * 6155192Srwatson * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7155192Srwatson * and NAI Labs, the Security Research Division of Network Associates, Inc. 8155192Srwatson * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9155192Srwatson * DARPA CHATS research program. 10155192Srwatson * 11155192Srwatson * Redistribution and use in source and binary forms, with or without 12155192Srwatson * modification, are permitted provided that the following conditions 13155192Srwatson * are met: 14155192Srwatson * 1. Redistributions of source code must retain the above copyright 15155192Srwatson * notice, this list of conditions and the following disclaimer. 16155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright 17155192Srwatson * notice, this list of conditions and the following disclaimer in the 18155192Srwatson * documentation and/or other materials provided with the distribution. 19155192Srwatson * 20155192Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24155192Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28155192Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29155192Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30155192Srwatson * SUCH DAMAGE. 31155192Srwatson */ 32155192Srwatson 33155192Srwatson#include <sys/cdefs.h> 34155192Srwatson__FBSDID("$FreeBSD: stable/10/sys/geom/geom_pc98.c 254015 2013-08-07 00:00:48Z marcel $"); 35155192Srwatson 36155192Srwatson#include <sys/param.h> 37155192Srwatson#include <sys/endian.h> 38155192Srwatson#include <sys/systm.h> 39160136Swsalamon#include <sys/sysctl.h> 40155192Srwatson#include <sys/kernel.h> 41155192Srwatson#include <sys/fcntl.h> 42155192Srwatson#include <sys/malloc.h> 43155192Srwatson#include <sys/bio.h> 44155192Srwatson#include <sys/lock.h> 45155192Srwatson#include <sys/mutex.h> 46155192Srwatson#include <sys/proc.h> 47155192Srwatson#include <sys/sbuf.h> 48155192Srwatson 49155192Srwatson#include <sys/diskpc98.h> 50155192Srwatson#include <geom/geom.h> 51155192Srwatson#include <geom/geom_slice.h> 52155192Srwatson 53155192SrwatsonFEATURE(geom_pc98, "GEOM NEC PC9800 partitioning support"); 54155192Srwatson 55155192Srwatson#define PC98_CLASS_NAME "PC98" 56155192Srwatson 57155192Srwatsonstruct g_pc98_softc { 58155192Srwatson u_int fwsectors, fwheads, sectorsize; 59155192Srwatson int type[PC98_NPARTS]; 60155192Srwatson u_char sec[8192]; 61155192Srwatson}; 62155192Srwatson 63155192Srwatsonstatic void 64155192Srwatsong_pc98_print(int i, struct pc98_partition *dp) 65155192Srwatson{ 66155192Srwatson char sname[17]; 67155192Srwatson 68155192Srwatson strncpy(sname, dp->dp_name, 16); 69155192Srwatson sname[16] = '\0'; 70155192Srwatson 71155192Srwatson hexdump(dp, sizeof(dp[0]), NULL, 0); 72156889Srwatson printf("[%d] mid:%d(0x%x) sid:%d(0x%x)", 73156889Srwatson i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid); 74156889Srwatson printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 75156889Srwatson printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 76156889Srwatson printf(" sname:%s\n", sname); 77156889Srwatson} 78155192Srwatson 79156889Srwatson/* 80155192Srwatson * XXX: Add gctl_req arg and give good error msgs. 81156889Srwatson * XXX: Check that length argument does not bring boot code inside any slice. 82155192Srwatson */ 83162466Srwatsonstatic int 84155192Srwatsong_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec, int len __unused) 85155192Srwatson{ 86155192Srwatson int i, error; 87155192Srwatson off_t s[PC98_NPARTS], l[PC98_NPARTS]; 88155192Srwatson struct pc98_partition dp[PC98_NPARTS]; 89155192Srwatson 90155192Srwatson g_topology_assert(); 91155192Srwatson 92155192Srwatson if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa) 93156889Srwatson return (EBUSY); 94155192Srwatson 95155192Srwatson#if 0 96155192Srwatson /* 97155192Srwatson * By convetion, it seems that the ipl program has a jump at location 98155192Srwatson * 0 to the real start of the boot loader. By convetion, it appears 99155192Srwatson * that after this jump, there's a string, terminated by at last one, 100155192Srwatson * if not more, zeros, followed by the target of the jump. FreeBSD's 101155192Srwatson * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for 102155192Srwatson * compatibility with some older boot loader. Linux98's boot loader 103155192Srwatson * appears to use 'Linux 98' followed by only two. GRUB/98 appears to 104155192Srwatson * use 'GRUB/98 ' followed by none. These last two appear to be 105155192Srwatson * ported from the ia32 versions, but appear to show similar 106155192Srwatson * convention. Grub/98 has an additional NOP after the jmp, which 107155192Srwatson * isn't present in others. 108155192Srwatson * 109155192Srwatson * The following test was inspired by looking only at partitions 110155192Srwatson * with FreeBSD's boot0 (or one that it is compatible with). As 111155192Srwatson * such, if failed when other IPL programs were used. 112155192Srwatson */ 113155192Srwatson if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1') 114155192Srwatson return (EBUSY); 115156889Srwatson#endif 116161635Srwatson 117162466Srwatson for (i = 0; i < PC98_NPARTS; i++) 118170196Srwatson pc98_partition_dec( 119162466Srwatson sec + 512 + i * sizeof(struct pc98_partition), &dp[i]); 120162466Srwatson 121162466Srwatson for (i = 0; i < PC98_NPARTS; i++) { 122162466Srwatson /* If start and end are identical it's bogus */ 123155192Srwatson if (dp[i].dp_ssect == dp[i].dp_esect && 124162466Srwatson dp[i].dp_shd == dp[i].dp_ehd && 125162466Srwatson dp[i].dp_scyl == dp[i].dp_ecyl) 126155192Srwatson s[i] = l[i] = 0; 127162466Srwatson else if (dp[i].dp_ecyl == 0) 128162466Srwatson s[i] = l[i] = 0; 129162466Srwatson else { 130162466Srwatson s[i] = (off_t)dp[i].dp_scyl * 131162466Srwatson ms->fwsectors * ms->fwheads * ms->sectorsize; 132155192Srwatson l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) * 133155192Srwatson ms->fwsectors * ms->fwheads * ms->sectorsize; 134155192Srwatson } 135155192Srwatson if (bootverbose) { 136156889Srwatson printf("PC98 Slice %d on %s:\n", i + 1, gp->name); 137156889Srwatson g_pc98_print(i, dp + i); 138155192Srwatson } 139155192Srwatson if (s[i] < 0 || l[i] < 0) 140155192Srwatson error = EBUSY; 141155192Srwatson else 142155192Srwatson error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 143155192Srwatson s[i], l[i], ms->sectorsize, 144156889Srwatson "%ss%d", gp->name, i + 1); 145155192Srwatson if (error) 146155192Srwatson return (error); 147155192Srwatson } 148155192Srwatson 149156889Srwatson for (i = 0; i < PC98_NPARTS; i++) { 150155192Srwatson ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid; 151155192Srwatson g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i], 152156889Srwatson ms->sectorsize, "%ss%d", gp->name, i + 1); 153155192Srwatson } 154155192Srwatson 155155192Srwatson bcopy(sec, ms->sec, sizeof (ms->sec)); 156155192Srwatson 157155192Srwatson return (0); 158170196Srwatson} 159155192Srwatson 160155192Srwatsonstatic int 161155192Srwatsong_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td) 162155192Srwatson{ 163155192Srwatson struct g_geom *gp; 164155192Srwatson struct g_pc98_softc *ms; 165155192Srwatson struct g_slicer *gsp; 166155192Srwatson struct g_consumer *cp; 167155192Srwatson int error, opened; 168155192Srwatson 169155192Srwatson gp = pp->geom; 170155192Srwatson gsp = gp->softc; 171155192Srwatson ms = gsp->softc; 172155192Srwatson 173155192Srwatson opened = 0; 174155192Srwatson error = 0; 175155192Srwatson switch(cmd) { 176155192Srwatson case DIOCSPC98: { 177155192Srwatson if (!(fflag & FWRITE)) 178155192Srwatson return (EPERM); 179155192Srwatson DROP_GIANT(); 180155192Srwatson g_topology_lock(); 181155192Srwatson cp = LIST_FIRST(&gp->consumer); 182155192Srwatson if (cp->acw == 0) { 183155192Srwatson error = g_access(cp, 0, 1, 0); 184155192Srwatson if (error == 0) 185155192Srwatson opened = 1; 186155192Srwatson } 187155192Srwatson if (!error) 188155192Srwatson error = g_pc98_modify(gp, ms, data, 8192); 189155192Srwatson if (!error) 190155192Srwatson error = g_write_data(cp, 0, data, 8192); 191155192Srwatson if (opened) 192155192Srwatson g_access(cp, 0, -1 , 0); 193155192Srwatson g_topology_unlock(); 194155192Srwatson PICKUP_GIANT(); 195155192Srwatson return(error); 196155192Srwatson } 197155192Srwatson default: 198155192Srwatson return (ENOIOCTL); 199155192Srwatson } 200155192Srwatson} 201155192Srwatson 202155192Srwatsonstatic int 203155192Srwatsong_pc98_start(struct bio *bp) 204155192Srwatson{ 205155192Srwatson struct g_provider *pp; 206155192Srwatson struct g_geom *gp; 207155192Srwatson struct g_pc98_softc *mp; 208155192Srwatson struct g_slicer *gsp; 209155192Srwatson int idx; 210155192Srwatson 211155192Srwatson pp = bp->bio_to; 212156889Srwatson idx = pp->index; 213156889Srwatson gp = pp->geom; 214155192Srwatson gsp = gp->softc; 215155192Srwatson mp = gsp->softc; 216155192Srwatson if (bp->bio_cmd == BIO_GETATTR) { 217155192Srwatson if (g_handleattr_int(bp, "PC98::type", mp->type[idx])) 218155192Srwatson return (1); 219155192Srwatson if (g_handleattr_off_t(bp, "PC98::offset", 220159277Srwatson gsp->slices[idx].offset)) 221159277Srwatson return (1); 222172915Scsjp } 223159277Srwatson 224159277Srwatson return (0); 225159277Srwatson} 226172915Scsjp 227159277Srwatsonstatic void 228159277Srwatsong_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 229159277Srwatson struct g_consumer *cp __unused, struct g_provider *pp) 230159277Srwatson{ 231155192Srwatson struct g_pc98_softc *mp; 232155192Srwatson struct g_slicer *gsp; 233155192Srwatson struct pc98_partition dp; 234160136Swsalamon char sname[17]; 235160136Swsalamon 236160136Swsalamon gsp = gp->softc; 237160136Swsalamon mp = gsp->softc; 238160136Swsalamon g_slice_dumpconf(sb, indent, gp, cp, pp); 239160136Swsalamon if (pp != NULL) { 240160136Swsalamon pc98_partition_dec( 241160136Swsalamon mp->sec + 512 + 242160136Swsalamon pp->index * sizeof(struct pc98_partition), &dp); 243160136Swsalamon strncpy(sname, dp.dp_name, 16); 244160136Swsalamon sname[16] = '\0'; 245160136Swsalamon if (indent == NULL) { 246160136Swsalamon sbuf_printf(sb, " ty %d", mp->type[pp->index]); 247160136Swsalamon sbuf_printf(sb, " sn %s", sname); 248160136Swsalamon } else { 249160136Swsalamon sbuf_printf(sb, "%s<type>%d</type>\n", indent, 250160136Swsalamon mp->type[pp->index]); 251160136Swsalamon sbuf_printf(sb, "%s<sname>%s</sname>\n", indent, 252160136Swsalamon sname); 253160136Swsalamon } 254160136Swsalamon } 255160136Swsalamon} 256160136Swsalamon 257155192Srwatsonstatic struct g_geom * 258155192Srwatsong_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 259155192Srwatson{ 260155192Srwatson struct g_geom *gp; 261155192Srwatson struct g_consumer *cp; 262155192Srwatson int error; 263155192Srwatson struct g_pc98_softc *ms; 264155192Srwatson u_int fwsectors, fwheads, sectorsize; 265155192Srwatson u_char *buf; 266155192Srwatson 267155192Srwatson g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 268156889Srwatson g_topology_assert(); 269155192Srwatson if (flags == G_TF_NORMAL && 270156889Srwatson !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 271156889Srwatson return (NULL); 272155192Srwatson gp = g_slice_new(mp, PC98_NPARTS, pp, &cp, &ms, sizeof *ms, 273156889Srwatson g_pc98_start); 274156889Srwatson if (gp == NULL) 275155192Srwatson return (NULL); 276155192Srwatson g_topology_unlock(); 277155192Srwatson do { 278156889Srwatson if (gp->rank != 2 && flags == G_TF_NORMAL) 279156889Srwatson break; 280156889Srwatson error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 281155192Srwatson if (error || fwsectors == 0) { 282156889Srwatson fwsectors = 17; 283156889Srwatson if (bootverbose) 284155192Srwatson printf("g_pc98_taste: guessing %d sectors\n", 285155192Srwatson fwsectors); 286155192Srwatson } 287156889Srwatson error = g_getattr("GEOM::fwheads", cp, &fwheads); 288156889Srwatson if (error || fwheads == 0) { 289156889Srwatson fwheads = 8; 290155192Srwatson if (bootverbose) 291156889Srwatson printf("g_pc98_taste: guessing %d heads\n", 292156889Srwatson fwheads); 293155192Srwatson } 294156889Srwatson sectorsize = cp->provider->sectorsize; 295156889Srwatson if (sectorsize % 512 != 0) 296155192Srwatson break; 297156889Srwatson buf = g_read_data(cp, 0, 8192, NULL); 298156889Srwatson if (buf == NULL) 299155192Srwatson break; 300156889Srwatson ms->fwsectors = fwsectors; 301156889Srwatson ms->fwheads = fwheads; 302155192Srwatson ms->sectorsize = sectorsize; 303155192Srwatson g_topology_lock(); 304155192Srwatson g_pc98_modify(gp, ms, buf, 8192); 305156889Srwatson g_topology_unlock(); 306156889Srwatson g_free(buf); 307156889Srwatson break; 308155192Srwatson } while (0); 309156889Srwatson g_topology_lock(); 310156889Srwatson g_access(cp, -1, 0, 0); 311155192Srwatson if (LIST_EMPTY(&gp->provider)) { 312155192Srwatson g_slice_spoiled(cp); 313155192Srwatson return (NULL); 314156889Srwatson } 315156889Srwatson return (gp); 316156889Srwatson} 317155192Srwatson 318156889Srwatsonstatic void 319156889Srwatsong_pc98_config(struct gctl_req *req, struct g_class *mp, const char *verb) 320155192Srwatson{ 321155192Srwatson struct g_geom *gp; 322155192Srwatson struct g_consumer *cp; 323156889Srwatson struct g_pc98_softc *ms; 324155192Srwatson struct g_slicer *gsp; 325156889Srwatson int opened = 0, error = 0; 326156889Srwatson void *data; 327155192Srwatson int len; 328156889Srwatson 329156889Srwatson g_topology_assert(); 330155192Srwatson gp = gctl_get_geom(req, mp, "geom"); 331155192Srwatson if (gp == NULL) 332155192Srwatson return; 333156889Srwatson if (strcmp(verb, "write PC98")) { 334155192Srwatson gctl_error(req, "Unknown verb"); 335156889Srwatson return; 336155192Srwatson } 337155192Srwatson gsp = gp->softc; 338156889Srwatson ms = gsp->softc; 339155192Srwatson data = gctl_get_param(req, "data", &len); 340155192Srwatson if (data == NULL) 341155192Srwatson return; 342156889Srwatson if (len < 8192 || (len % 512)) { 343156889Srwatson gctl_error(req, "Wrong request length"); 344156889Srwatson return; 345155192Srwatson } 346156889Srwatson cp = LIST_FIRST(&gp->consumer); 347156889Srwatson if (cp->acw == 0) { 348155192Srwatson error = g_access(cp, 0, 1, 0); 349155192Srwatson if (error == 0) 350155192Srwatson opened = 1; 351156889Srwatson } 352156889Srwatson if (!error) 353156889Srwatson error = g_pc98_modify(gp, ms, data, len); 354155192Srwatson if (error) 355155192Srwatson gctl_error(req, "conflict with open slices"); 356155192Srwatson if (!error) 357155192Srwatson error = g_write_data(cp, 0, data, len); 358155192Srwatson if (error) 359155192Srwatson gctl_error(req, "sector zero write failed"); 360155192Srwatson if (opened) 361155192Srwatson g_access(cp, 0, -1 , 0); 362155192Srwatson return; 363156889Srwatson} 364156889Srwatson 365156889Srwatsonstatic struct g_class g_pc98_class = { 366156889Srwatson .name = PC98_CLASS_NAME, 367155192Srwatson .version = G_VERSION, 368155192Srwatson .taste = g_pc98_taste, 369155192Srwatson .dumpconf = g_pc98_dumpconf, 370156889Srwatson .ctlreq = g_pc98_config, 371155192Srwatson .ioctl = g_pc98_ioctl, 372155192Srwatson}; 373155192Srwatson 374155192SrwatsonDECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 375155192Srwatson