geom_pc98.c revision 107970
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 * $FreeBSD: head/sys/geom/geom_pc98.c 107970 2002-12-17 15:50:51Z phk $ 33 */ 34 35#include <sys/param.h> 36#ifndef _KERNEL 37#include <signal.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <err.h> 42#else 43#include <sys/systm.h> 44#include <sys/kernel.h> 45#include <sys/malloc.h> 46#include <sys/bio.h> 47#include <sys/lock.h> 48#include <sys/mutex.h> 49#endif 50 51#include <sys/diskpc98.h> 52#include <geom/geom.h> 53#include <geom/geom_slice.h> 54 55#define PC98_CLASS_NAME "PC98" 56 57static void 58g_dec_dos_partition(u_char *ptr, struct dos_partition *d) 59{ 60 u_int u; 61 62 d->dp_mid = ptr[0]; 63 d->dp_sid = ptr[1]; 64 d->dp_dum1 = ptr[2]; 65 d->dp_dum2 = ptr[3]; 66 d->dp_ipl_sct = ptr[4]; 67 d->dp_ipl_head = ptr[5]; 68 d->dp_ipl_cyl = g_dec_le2(ptr + 6); 69 d->dp_ssect = ptr[8]; 70 d->dp_shd = ptr[9]; 71 d->dp_scyl = g_dec_le2(ptr + 10); 72 d->dp_esect = ptr[12]; 73 d->dp_ehd = ptr[13]; 74 d->dp_ecyl = g_dec_le2(ptr + 14); 75 for (u = 0; u < sizeof(d->dp_name); u++) 76 d->dp_name[u] = ptr[16 + u]; 77} 78 79struct g_pc98_softc { 80 int type [NDOSPART]; 81 struct dos_partition dp[NDOSPART]; 82}; 83 84static int 85g_pc98_start(struct bio *bp) 86{ 87 struct g_provider *pp; 88 struct g_geom *gp; 89 struct g_pc98_softc *mp; 90 struct g_slicer *gsp; 91 int idx; 92 93 pp = bp->bio_to; 94 idx = pp->index; 95 gp = pp->geom; 96 gsp = gp->softc; 97 mp = gsp->softc; 98 if (bp->bio_cmd == BIO_GETATTR) { 99 if (g_handleattr_int(bp, "PC98::type", mp->type[idx])) 100 return (1); 101 if (g_handleattr_off_t(bp, "PC98::offset", 102 gsp->slices[idx].offset)) 103 return (1); 104 } 105 return (0); 106} 107 108static void 109g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 110 struct g_consumer *cp __unused, struct g_provider *pp) 111{ 112 struct g_pc98_softc *mp; 113 struct g_slicer *gsp; 114 char sname[17]; 115 116 gsp = gp->softc; 117 mp = gsp->softc; 118 g_slice_dumpconf(sb, indent, gp, cp, pp); 119 if (pp != NULL) { 120 strncpy(sname, mp->dp[pp->index].dp_name, 16); 121 sname[16] = '\0'; 122 if (indent == NULL) { 123 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 124 sbuf_printf(sb, " sn %s", sname); 125 } else { 126 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 127 mp->type[pp->index]); 128 sbuf_printf(sb, "%s<sname>%s</sname>\n", indent, 129 sname); 130 } 131 } 132} 133 134static void 135g_pc98_print(int i, struct dos_partition *dp) 136{ 137 char sname[17]; 138 139 strncpy(sname, dp->dp_name, 16); 140 sname[16] = '\0'; 141 142 g_hexdump(dp, sizeof(dp[0])); 143 printf("[%d] mid:%d(0x%x) sid:%d(0x%x)", 144 i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid); 145 printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 146 printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 147 printf(" sname:%s\n", sname); 148} 149 150static struct g_geom * 151g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 152{ 153 struct g_geom *gp; 154 struct g_consumer *cp; 155 int error, i, npart; 156 struct g_pc98_softc *ms; 157 struct g_slicer *gsp; 158 u_int fwsectors, fwheads, sectorsize; 159 u_char *buf; 160 off_t spercyl; 161 162 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 163 g_topology_assert(); 164 if (flags == G_TF_NORMAL && 165 !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 166 return (NULL); 167 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start); 168 if (gp == NULL) 169 return (NULL); 170 gsp = gp->softc; 171 g_topology_unlock(); 172 gp->dumpconf = g_pc98_dumpconf; 173 npart = 0; 174 while (1) { /* a trick to allow us to use break */ 175 if (gp->rank != 2 && flags == G_TF_NORMAL) 176 break; 177 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 178 if (error || fwsectors == 0) { 179 fwsectors = 17; 180 printf("g_pc98_taste: error %d guessing %d sectors\n", 181 error, fwsectors); 182 } 183 error = g_getattr("GEOM::fwheads", cp, &fwheads); 184 if (error || fwheads == 0) { 185 fwheads = 8; 186 printf("g_pc98_taste: error %d guessing %d heads\n", 187 error, fwheads); 188 } 189 sectorsize = cp->provider->sectorsize; 190 if (sectorsize < 512) 191 break; 192 gsp->frontstuff = sectorsize * fwsectors; 193 spercyl = (off_t)fwsectors * fwheads * sectorsize; 194 buf = g_read_data(cp, 0, 195 sectorsize < 1024 ? 1024 : sectorsize, &error); 196 if (buf == NULL || error != 0) 197 break; 198 if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) { 199 g_free(buf); 200 break; 201 } 202#if 0 203/* 204 * XXX: Some sources indicate this is a magic sequence, but appearantly 205 * XXX: it is not universal. Documentation would be wonderfule to have. 206 */ 207 if (buf[4] != 'I' || buf[5] != 'P' || 208 buf[6] != 'L' || buf[7] != '1') { 209 g_free(buf); 210 break; 211 } 212#endif 213 for (i = 0; i < NDOSPART; i++) 214 g_dec_dos_partition( 215 buf + 512 + i * sizeof(struct dos_partition), 216 ms->dp + i); 217 g_free(buf); 218 for (i = 0; i < NDOSPART; i++) { 219 /* If start and end are identical it's bogus */ 220 if (ms->dp[i].dp_ssect == ms->dp[i].dp_esect && 221 ms->dp[i].dp_shd == ms->dp[i].dp_ehd && 222 ms->dp[i].dp_scyl == ms->dp[i].dp_ecyl) 223 continue; 224 if (ms->dp[i].dp_ecyl == 0) 225 continue; 226 if (bootverbose) { 227 printf("PC98 Slice %d on %s:\n", 228 i + 1, gp->name); 229 g_pc98_print(i, ms->dp + i); 230 } 231 npart++; 232 ms->type[i] = (ms->dp[i].dp_sid << 8) | 233 ms->dp[i].dp_mid; 234 g_topology_lock(); 235 g_slice_config(gp, i, G_SLICE_CONFIG_SET, 236 ms->dp[i].dp_scyl * spercyl, 237 (ms->dp[i].dp_ecyl - ms->dp[i].dp_scyl + 1) * 238 spercyl, 239 sectorsize, 240 "%ss%d", gp->name, i + 1); 241 g_topology_unlock(); 242 } 243 break; 244 } 245 g_topology_lock(); 246 g_access_rel(cp, -1, 0, 0); 247 if (LIST_EMPTY(&gp->provider)) { 248 g_std_spoiled(cp); 249 return (NULL); 250 } 251 return (gp); 252} 253 254static struct g_class g_pc98_class = { 255 PC98_CLASS_NAME, 256 g_pc98_taste, 257 NULL, 258 G_CLASS_INITIALIZER 259}; 260 261DECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 262