geom_pc98.c revision 152971
1254885Sdumbbell/*- 2254885Sdumbbell * Copyright (c) 2002 Poul-Henning Kamp 3254885Sdumbbell * Copyright (c) 2002 Networks Associates Technology, Inc. 4254885Sdumbbell * All rights reserved. 5254885Sdumbbell * 6254885Sdumbbell * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7254885Sdumbbell * and NAI Labs, the Security Research Division of Network Associates, Inc. 8254885Sdumbbell * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9254885Sdumbbell * DARPA CHATS research program. 10254885Sdumbbell * 11254885Sdumbbell * Redistribution and use in source and binary forms, with or without 12254885Sdumbbell * modification, are permitted provided that the following conditions 13254885Sdumbbell * are met: 14254885Sdumbbell * 1. Redistributions of source code must retain the above copyright 15254885Sdumbbell * notice, this list of conditions and the following disclaimer. 16254885Sdumbbell * 2. Redistributions in binary form must reproduce the above copyright 17254885Sdumbbell * notice, this list of conditions and the following disclaimer in the 18254885Sdumbbell * documentation and/or other materials provided with the distribution. 19254885Sdumbbell * 20254885Sdumbbell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21254885Sdumbbell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22254885Sdumbbell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23254885Sdumbbell * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24254885Sdumbbell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25254885Sdumbbell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26254885Sdumbbell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27254885Sdumbbell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28254885Sdumbbell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29254885Sdumbbell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30254885Sdumbbell * SUCH DAMAGE. 31254885Sdumbbell */ 32254885Sdumbbell 33254885Sdumbbell#include <sys/cdefs.h> 34254885Sdumbbell__FBSDID("$FreeBSD: head/sys/geom/geom_pc98.c 152971 2005-11-30 22:15:00Z sobomax $"); 35254885Sdumbbell 36254885Sdumbbell#include <sys/param.h> 37254885Sdumbbell#include <sys/endian.h> 38254885Sdumbbell#include <sys/systm.h> 39254885Sdumbbell#include <sys/kernel.h> 40254885Sdumbbell#include <sys/fcntl.h> 41254885Sdumbbell#include <sys/malloc.h> 42254885Sdumbbell#include <sys/bio.h> 43254885Sdumbbell#include <sys/lock.h> 44254885Sdumbbell#include <sys/mutex.h> 45254885Sdumbbell 46254885Sdumbbell#include <sys/diskpc98.h> 47254885Sdumbbell#include <geom/geom.h> 48254885Sdumbbell#include <geom/geom_slice.h> 49254885Sdumbbell 50254885Sdumbbell#define PC98_CLASS_NAME "PC98" 51254885Sdumbbell 52254885Sdumbbellstruct g_pc98_softc { 53254885Sdumbbell u_int fwsectors, fwheads, sectorsize; 54254885Sdumbbell int type[NDOSPART]; 55254885Sdumbbell u_char sec[8192]; 56254885Sdumbbell}; 57254885Sdumbbell 58254885Sdumbbellstatic void 59254885Sdumbbellg_pc98_print(int i, struct pc98_partition *dp) 60254885Sdumbbell{ 61254885Sdumbbell char sname[17]; 62254885Sdumbbell 63254885Sdumbbell strncpy(sname, dp->dp_name, 16); 64254885Sdumbbell sname[16] = '\0'; 65254885Sdumbbell 66254885Sdumbbell hexdump(dp, sizeof(dp[0]), NULL, 0); 67254885Sdumbbell printf("[%d] mid:%d(0x%x) sid:%d(0x%x)", 68254885Sdumbbell i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid); 69254885Sdumbbell printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 70254885Sdumbbell printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 71254885Sdumbbell printf(" sname:%s\n", sname); 72254885Sdumbbell} 73254885Sdumbbell 74254885Sdumbbell/* 75254885Sdumbbell * XXX: Add gctl_req arg and give good error msgs. 76254885Sdumbbell * XXX: Check that length argument does not bring boot code inside any slice. 77254885Sdumbbell */ 78254885Sdumbbellstatic int 79254885Sdumbbellg_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec, int len __unused) 80254885Sdumbbell{ 81254885Sdumbbell int i, error; 82254885Sdumbbell off_t s[NDOSPART], l[NDOSPART]; 83254885Sdumbbell struct pc98_partition dp[NDOSPART]; 84254885Sdumbbell 85254885Sdumbbell g_topology_assert(); 86254885Sdumbbell 87254885Sdumbbell if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa) 88254885Sdumbbell return (EBUSY); 89254885Sdumbbell 90254885Sdumbbell#if 0 91254885Sdumbbell /* 92254885Sdumbbell * By convetion, it seems that the ipl program has a jump at location 93254885Sdumbbell * 0 to the real start of the boot loader. By convetion, it appears 94254885Sdumbbell * that after this jump, there's a string, terminated by at last one, 95254885Sdumbbell * if not more, zeros, followed by the target of the jump. FreeBSD's 96254885Sdumbbell * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for 97254885Sdumbbell * compatibility with some older boot loader. Linux98's boot loader 98254885Sdumbbell * appears to use 'Linux 98' followed by only two. GRUB/98 appears to 99254885Sdumbbell * use 'GRUB/98 ' followed by none. These last two appear to be 100254885Sdumbbell * ported from the ia32 versions, but appear to show similar 101254885Sdumbbell * convention. Grub/98 has an additional NOP after the jmp, which 102254885Sdumbbell * isn't present in others. 103254885Sdumbbell * 104254885Sdumbbell * The following test was inspired by looking only at partitions 105254885Sdumbbell * with FreeBSD's boot0 (or one that it is compatible with). As 106254885Sdumbbell * such, if failed when other IPL programs were used. 107254885Sdumbbell */ 108254885Sdumbbell if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1') 109254885Sdumbbell return (EBUSY); 110254885Sdumbbell#endif 111254885Sdumbbell 112254885Sdumbbell for (i = 0; i < NDOSPART; i++) 113254885Sdumbbell pc98_partition_dec( 114254885Sdumbbell sec + 512 + i * sizeof(struct pc98_partition), &dp[i]); 115254885Sdumbbell 116254885Sdumbbell for (i = 0; i < NDOSPART; i++) { 117254885Sdumbbell /* If start and end are identical it's bogus */ 118254885Sdumbbell if (dp[i].dp_ssect == dp[i].dp_esect && 119254885Sdumbbell dp[i].dp_shd == dp[i].dp_ehd && 120254885Sdumbbell dp[i].dp_scyl == dp[i].dp_ecyl) 121254885Sdumbbell s[i] = l[i] = 0; 122254885Sdumbbell else if (dp[i].dp_ecyl == 0) 123254885Sdumbbell s[i] = l[i] = 0; 124254885Sdumbbell else { 125254885Sdumbbell s[i] = (off_t)dp[i].dp_scyl * 126254885Sdumbbell ms->fwsectors * ms->fwheads * ms->sectorsize; 127254885Sdumbbell l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) * 128254885Sdumbbell ms->fwsectors * ms->fwheads * ms->sectorsize; 129254885Sdumbbell } 130254885Sdumbbell if (bootverbose) { 131254885Sdumbbell printf("PC98 Slice %d on %s:\n", i + 1, gp->name); 132254885Sdumbbell g_pc98_print(i, dp + i); 133254885Sdumbbell } 134254885Sdumbbell if (s[i] < 0 || l[i] < 0) 135254885Sdumbbell error = EBUSY; 136254885Sdumbbell else 137254885Sdumbbell error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 138254885Sdumbbell s[i], l[i], ms->sectorsize, 139254885Sdumbbell "%ss%d", gp->name, i + 1); 140254885Sdumbbell if (error) 141254885Sdumbbell return (error); 142254885Sdumbbell } 143254885Sdumbbell 144254885Sdumbbell for (i = 0; i < NDOSPART; i++) { 145254885Sdumbbell ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid; 146254885Sdumbbell g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i], 147254885Sdumbbell ms->sectorsize, "%ss%d", gp->name, i + 1); 148254885Sdumbbell } 149254885Sdumbbell 150254885Sdumbbell bcopy(sec, ms->sec, sizeof (ms->sec)); 151254885Sdumbbell 152254885Sdumbbell return (0); 153254885Sdumbbell} 154254885Sdumbbell 155254885Sdumbbellstatic int 156254885Sdumbbellg_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td) 157254885Sdumbbell{ 158254885Sdumbbell struct g_geom *gp; 159254885Sdumbbell struct g_pc98_softc *ms; 160254885Sdumbbell struct g_slicer *gsp; 161254885Sdumbbell struct g_consumer *cp; 162254885Sdumbbell int error, opened; 163254885Sdumbbell 164254885Sdumbbell gp = pp->geom; 165254885Sdumbbell gsp = gp->softc; 166254885Sdumbbell ms = gsp->softc; 167254885Sdumbbell 168254885Sdumbbell opened = 0; 169254885Sdumbbell error = 0; 170254885Sdumbbell switch(cmd) { 171254885Sdumbbell case DIOCSPC98: { 172254885Sdumbbell if (!(fflag & FWRITE)) 173254885Sdumbbell return (EPERM); 174254885Sdumbbell DROP_GIANT(); 175254885Sdumbbell g_topology_lock(); 176254885Sdumbbell cp = LIST_FIRST(&gp->consumer); 177254885Sdumbbell if (cp->acw == 0) { 178254885Sdumbbell error = g_access(cp, 0, 1, 0); 179254885Sdumbbell if (error == 0) 180254885Sdumbbell opened = 1; 181254885Sdumbbell } 182254885Sdumbbell if (!error) 183254885Sdumbbell error = g_pc98_modify(gp, ms, data, 8192); 184254885Sdumbbell if (!error) 185254885Sdumbbell error = g_write_data(cp, 0, data, 8192); 186254885Sdumbbell if (opened) 187254885Sdumbbell g_access(cp, 0, -1 , 0); 188254885Sdumbbell g_topology_unlock(); 189254885Sdumbbell PICKUP_GIANT(); 190254885Sdumbbell return(error); 191254885Sdumbbell } 192254885Sdumbbell default: 193254885Sdumbbell return (ENOIOCTL); 194254885Sdumbbell } 195254885Sdumbbell} 196254885Sdumbbell 197254885Sdumbbellstatic int 198254885Sdumbbellg_pc98_start(struct bio *bp) 199254885Sdumbbell{ 200254885Sdumbbell struct g_provider *pp; 201254885Sdumbbell struct g_geom *gp; 202254885Sdumbbell struct g_pc98_softc *mp; 203254885Sdumbbell struct g_slicer *gsp; 204254885Sdumbbell int idx; 205254885Sdumbbell 206254885Sdumbbell pp = bp->bio_to; 207254885Sdumbbell idx = pp->index; 208254885Sdumbbell gp = pp->geom; 209254885Sdumbbell gsp = gp->softc; 210254885Sdumbbell mp = gsp->softc; 211254885Sdumbbell if (bp->bio_cmd == BIO_GETATTR) { 212254885Sdumbbell if (g_handleattr_int(bp, "PC98::type", mp->type[idx])) 213254885Sdumbbell return (1); 214254885Sdumbbell if (g_handleattr_off_t(bp, "PC98::offset", 215254885Sdumbbell gsp->slices[idx].offset)) 216254885Sdumbbell return (1); 217254885Sdumbbell } 218254885Sdumbbell 219254885Sdumbbell return (0); 220254885Sdumbbell} 221254885Sdumbbell 222254885Sdumbbellstatic void 223254885Sdumbbellg_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 224254885Sdumbbell struct g_consumer *cp __unused, struct g_provider *pp) 225254885Sdumbbell{ 226254885Sdumbbell struct g_pc98_softc *mp; 227254885Sdumbbell struct g_slicer *gsp; 228254885Sdumbbell struct pc98_partition dp; 229254885Sdumbbell char sname[17]; 230254885Sdumbbell 231254885Sdumbbell gsp = gp->softc; 232254885Sdumbbell mp = gsp->softc; 233254885Sdumbbell g_slice_dumpconf(sb, indent, gp, cp, pp); 234254885Sdumbbell if (pp != NULL) { 235254885Sdumbbell pc98_partition_dec( 236254885Sdumbbell mp->sec + 512 + 237254885Sdumbbell pp->index * sizeof(struct pc98_partition), &dp); 238254885Sdumbbell strncpy(sname, dp.dp_name, 16); 239254885Sdumbbell sname[16] = '\0'; 240254885Sdumbbell if (indent == NULL) { 241254885Sdumbbell sbuf_printf(sb, " ty %d", mp->type[pp->index]); 242254885Sdumbbell sbuf_printf(sb, " sn %s", sname); 243254885Sdumbbell } else { 244254885Sdumbbell sbuf_printf(sb, "%s<type>%d</type>\n", indent, 245254885Sdumbbell mp->type[pp->index]); 246254885Sdumbbell sbuf_printf(sb, "%s<sname>%s</sname>\n", indent, 247254885Sdumbbell sname); 248254885Sdumbbell } 249254885Sdumbbell } 250254885Sdumbbell} 251254885Sdumbbell 252254885Sdumbbellstatic struct g_geom * 253254885Sdumbbellg_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 254254885Sdumbbell{ 255254885Sdumbbell struct g_geom *gp; 256254885Sdumbbell struct g_consumer *cp; 257254885Sdumbbell int error; 258254885Sdumbbell struct g_pc98_softc *ms; 259254885Sdumbbell u_int fwsectors, fwheads, sectorsize; 260254885Sdumbbell u_char *buf; 261254885Sdumbbell 262254885Sdumbbell g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 263254885Sdumbbell g_topology_assert(); 264254885Sdumbbell if (flags == G_TF_NORMAL && 265254885Sdumbbell !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 266254885Sdumbbell return (NULL); 267254885Sdumbbell gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start); 268254885Sdumbbell if (gp == NULL) 269254885Sdumbbell return (NULL); 270254885Sdumbbell g_topology_unlock(); 271254885Sdumbbell do { 272254885Sdumbbell if (gp->rank != 2 && flags == G_TF_NORMAL) 273254885Sdumbbell break; 274254885Sdumbbell error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 275254885Sdumbbell if (error || fwsectors == 0) { 276254885Sdumbbell fwsectors = 17; 277254885Sdumbbell if (bootverbose) 278254885Sdumbbell printf("g_pc98_taste: guessing %d sectors\n", 279254885Sdumbbell fwsectors); 280254885Sdumbbell } 281254885Sdumbbell error = g_getattr("GEOM::fwheads", cp, &fwheads); 282254885Sdumbbell if (error || fwheads == 0) { 283254885Sdumbbell fwheads = 8; 284254885Sdumbbell if (bootverbose) 285254885Sdumbbell printf("g_pc98_taste: guessing %d heads\n", 286254885Sdumbbell fwheads); 287254885Sdumbbell } 288254885Sdumbbell sectorsize = cp->provider->sectorsize; 289254885Sdumbbell if (sectorsize % 512 != 0) 290254885Sdumbbell break; 291254885Sdumbbell buf = g_read_data(cp, 0, 8192, NULL); 292254885Sdumbbell if (buf == NULL) 293254885Sdumbbell break; 294254885Sdumbbell ms->fwsectors = fwsectors; 295254885Sdumbbell ms->fwheads = fwheads; 296254885Sdumbbell ms->sectorsize = sectorsize; 297254885Sdumbbell g_topology_lock(); 298254885Sdumbbell g_pc98_modify(gp, ms, buf, 8192); 299254885Sdumbbell g_topology_unlock(); 300254885Sdumbbell g_free(buf); 301254885Sdumbbell break; 302254885Sdumbbell } while (0); 303254885Sdumbbell g_topology_lock(); 304254885Sdumbbell g_access(cp, -1, 0, 0); 305254885Sdumbbell if (LIST_EMPTY(&gp->provider)) { 306254885Sdumbbell g_slice_spoiled(cp); 307254885Sdumbbell return (NULL); 308254885Sdumbbell } 309254885Sdumbbell return (gp); 310254885Sdumbbell} 311254885Sdumbbell 312254885Sdumbbellstatic void 313254885Sdumbbellg_pc98_config(struct gctl_req *req, struct g_class *mp, const char *verb) 314254885Sdumbbell{ 315254885Sdumbbell struct g_geom *gp; 316254885Sdumbbell struct g_consumer *cp; 317254885Sdumbbell struct g_pc98_softc *ms; 318254885Sdumbbell struct g_slicer *gsp; 319254885Sdumbbell int opened = 0, error = 0; 320254885Sdumbbell void *data; 321254885Sdumbbell int len; 322254885Sdumbbell 323254885Sdumbbell g_topology_assert(); 324254885Sdumbbell gp = gctl_get_geom(req, mp, "geom"); 325254885Sdumbbell if (gp == NULL) 326254885Sdumbbell return; 327254885Sdumbbell if (strcmp(verb, "write PC98")) { 328254885Sdumbbell gctl_error(req, "Unknown verb"); 329254885Sdumbbell return; 330254885Sdumbbell } 331254885Sdumbbell gsp = gp->softc; 332254885Sdumbbell ms = gsp->softc; 333254885Sdumbbell data = gctl_get_param(req, "data", &len); 334254885Sdumbbell if (data == NULL) 335254885Sdumbbell return; 336254885Sdumbbell if (len < 8192 || (len % 512)) { 337254885Sdumbbell gctl_error(req, "Wrong request length"); 338254885Sdumbbell return; 339254885Sdumbbell } 340254885Sdumbbell cp = LIST_FIRST(&gp->consumer); 341254885Sdumbbell if (cp->acw == 0) { 342254885Sdumbbell error = g_access(cp, 0, 1, 0); 343254885Sdumbbell if (error == 0) 344254885Sdumbbell opened = 1; 345254885Sdumbbell } 346254885Sdumbbell if (!error) 347254885Sdumbbell error = g_pc98_modify(gp, ms, data, len); 348254885Sdumbbell if (error) 349254885Sdumbbell gctl_error(req, "conflict with open slices"); 350254885Sdumbbell if (!error) 351254885Sdumbbell error = g_write_data(cp, 0, data, len); 352254885Sdumbbell if (error) 353254885Sdumbbell gctl_error(req, "sector zero write failed"); 354254885Sdumbbell if (opened) 355254885Sdumbbell g_access(cp, 0, -1 , 0); 356254885Sdumbbell return; 357254885Sdumbbell} 358254885Sdumbbell 359254885Sdumbbellstatic struct g_class g_pc98_class = { 360254885Sdumbbell .name = PC98_CLASS_NAME, 361254885Sdumbbell .version = G_VERSION, 362254885Sdumbbell .taste = g_pc98_taste, 363254885Sdumbbell .dumpconf = g_pc98_dumpconf, 364254885Sdumbbell .ctlreq = g_pc98_config, 365254885Sdumbbell .ioctl = g_pc98_ioctl, 366254885Sdumbbell}; 367254885Sdumbbell 368254885SdumbbellDECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 369254885Sdumbbell