geom_pc98.c revision 105551
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2002 Poul-Henning Kamp 3219820Sjeff * Copyright (c) 2002 Networks Associates Technology, Inc. 4219820Sjeff * All rights reserved. 5219820Sjeff * 6219820Sjeff * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7219820Sjeff * and NAI Labs, the Security Research Division of Network Associates, Inc. 8219820Sjeff * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9219820Sjeff * DARPA CHATS research program. 10219820Sjeff * 11219820Sjeff * Redistribution and use in source and binary forms, with or without 12219820Sjeff * modification, are permitted provided that the following conditions 13219820Sjeff * are met: 14219820Sjeff * 1. Redistributions of source code must retain the above copyright 15219820Sjeff * notice, this list of conditions and the following disclaimer. 16219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 17219820Sjeff * notice, this list of conditions and the following disclaimer in the 18219820Sjeff * documentation and/or other materials provided with the distribution. 19219820Sjeff * 3. The names of the authors may not be used to endorse or promote 20219820Sjeff * products derived from this software without specific prior written 21219820Sjeff * permission. 22219820Sjeff * 23219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33219820Sjeff * SUCH DAMAGE. 34219820Sjeff * 35219820Sjeff * $FreeBSD: head/sys/geom/geom_pc98.c 105551 2002-10-20 20:28:24Z phk $ 36219820Sjeff */ 37219820Sjeff 38219820Sjeff 39219820Sjeff#include <sys/param.h> 40219820Sjeff#include <sys/stdint.h> 41219820Sjeff#ifndef _KERNEL 42219820Sjeff#include <stdio.h> 43219820Sjeff#include <string.h> 44219820Sjeff#include <stdlib.h> 45219820Sjeff#include <signal.h> 46219820Sjeff#include <err.h> 47219820Sjeff#else 48219820Sjeff#include <sys/systm.h> 49219820Sjeff#include <sys/kernel.h> 50219820Sjeff#include <sys/conf.h> 51219820Sjeff#include <sys/bio.h> 52219820Sjeff#include <sys/malloc.h> 53219820Sjeff#include <sys/lock.h> 54219820Sjeff#include <sys/mutex.h> 55219820Sjeff#endif 56219820Sjeff#include <sys/stdint.h> 57219820Sjeff#include <geom/geom.h> 58219820Sjeff#include <geom/geom_slice.h> 59219820Sjeff#include <machine/endian.h> 60219820Sjeff 61219820Sjeff#define PC98_CLASS_NAME "PC98" 62219820Sjeff 63219820Sjeffstruct g_pc98_softc { 64219820Sjeff int foo; 65219820Sjeff}; 66219820Sjeff 67219820Sjeffstatic int 68219820Sjeffg_pc98_start(struct bio *bp) 69219820Sjeff{ 70219820Sjeff struct g_geom *gp; 71219820Sjeff struct g_pc98_softc *ms; 72219820Sjeff struct g_slicer *gsp; 73219820Sjeff 74219820Sjeff gp = bp->bio_to->geom; 75219820Sjeff gsp = gp->softc; 76219820Sjeff ms = gsp->softc; 77219820Sjeff return (0); 78219820Sjeff} 79219820Sjeff 80219820Sjeffstatic void 81219820Sjeffg_pc98_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 82219820Sjeff{ 83219820Sjeff 84219820Sjeff g_slice_dumpconf(sb, indent, gp, cp, pp); 85219820Sjeff} 86219820Sjeff 87219820Sjeffstatic struct g_geom * 88219820Sjeffg_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 89219820Sjeff{ 90219820Sjeff struct g_geom *gp; 91219820Sjeff struct g_consumer *cp; 92219820Sjeff struct g_provider *pp2; 93219820Sjeff int error, i, npart; 94219820Sjeff u_char *buf; 95219820Sjeff struct g_pc98_softc *ms; 96219820Sjeff u_int sectorsize, u, v; 97219820Sjeff u_int fwsect, fwhead; 98219820Sjeff off_t mediasize, start, length; 99219820Sjeff struct g_slicer *gsp; 100219820Sjeff 101219820Sjeff g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 102219820Sjeff g_topology_assert(); 103219820Sjeff if (flags == G_TF_NORMAL && 104219820Sjeff !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 105219820Sjeff return (NULL); 106219820Sjeff gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_pc98_start); 107219820Sjeff if (gp == NULL) 108219820Sjeff return (NULL); 109219820Sjeff gsp = gp->softc; 110219820Sjeff g_topology_unlock(); 111219820Sjeff gp->dumpconf = g_pc98_dumpconf; 112219820Sjeff npart = 0; 113219820Sjeff while (1) { /* a trick to allow us to use break */ 114219820Sjeff if (gp->rank != 2 && flags == G_TF_NORMAL) 115219820Sjeff break; 116219820Sjeff sectorsize = cp->provider->sectorsize; 117219820Sjeff if (sectorsize < 512) 118219820Sjeff break; 119219820Sjeff mediasize = cp->provider->mediasize; 120219820Sjeff error = g_getattr("GEOM::fwsectors", cp, &fwsect); 121219820Sjeff if (error || fwsect == 0) { 122219820Sjeff fwsect = 17; 123219820Sjeff printf("g_pc98_taste: error %d guessing %d sectors\n", 124219820Sjeff error, fwsect); 125219820Sjeff } 126219820Sjeff error = g_getattr("GEOM::fwheads", cp, &fwhead); 127219820Sjeff if (error || fwhead == 0) { 128219820Sjeff fwhead = 8; 129219820Sjeff printf("g_pc98_taste: error %d guessing %d heads\n", 130219820Sjeff error, fwhead); 131219820Sjeff } 132219820Sjeff gsp->frontstuff = fwsect * sectorsize; 133219820Sjeff buf = g_read_data(cp, 0, 134219820Sjeff sectorsize < 1024 ? 1024 : sectorsize, &error); 135219820Sjeff if (buf == NULL || error != 0) 136219820Sjeff break; 137219820Sjeff 138219820Sjeff if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) 139219820Sjeff break; 140219820Sjeff if (buf[4] != 'I' || buf[5] != 'P' || 141219820Sjeff buf[6] != 'L' || buf[7] != '1') 142219820Sjeff break; 143219820Sjeff 144219820Sjeff 145219820Sjeff for (i = 0; i < 16; i++) { 146219820Sjeff v = g_dec_le2(buf + 512 + 10 + i * 32); 147219820Sjeff u = g_dec_le2(buf + 512 + 14 + i * 32); 148219820Sjeff if (u == 0) 149219820Sjeff continue; 150219820Sjeff g_hexdump(buf+512 + i * 32, 32); 151219820Sjeff start = v * fwsect * fwhead * sectorsize; 152219820Sjeff length = (1 + u - v) * fwsect * fwhead * sectorsize; 153219820Sjeff npart++; 154219820Sjeff g_topology_lock(); 155219820Sjeff pp2 = g_slice_addslice(gp, i, 156219820Sjeff start, length, 157219820Sjeff sectorsize, 158219820Sjeff "%ss%d", pp->name, 1 + i); 159219820Sjeff g_topology_unlock(); 160219820Sjeff g_error_provider(pp2, 0); 161219820Sjeff } 162219820Sjeff break; 163219820Sjeff } 164219820Sjeff g_topology_lock(); 165219820Sjeff error = g_access_rel(cp, -1, 0, 0); 166219820Sjeff if (npart > 0) 167219820Sjeff return (gp); 168219820Sjeff g_std_spoiled(cp); 169219820Sjeff return (NULL); 170219820Sjeff} 171219820Sjeff 172219820Sjeffstatic struct g_class g_pc98_class = { 173219820Sjeff PC98_CLASS_NAME, 174219820Sjeff g_pc98_taste, 175219820Sjeff NULL, 176219820Sjeff G_CLASS_INITIALIZER 177219820Sjeff}; 178219820Sjeff 179219820SjeffDECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 180219820Sjeff