geom_pc98.c revision 107956
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 107956 2002-12-16 23:08:48Z 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 if (cp->provider->mediasize / sectorsize < 17 * 8 * 65536) { 193 fwsectors = 17; 194 fwheads = 8; 195 } 196 gsp->frontstuff = sectorsize * fwsectors; 197 spercyl = (off_t)fwsectors * fwheads * sectorsize; 198 buf = g_read_data(cp, 0, 199 sectorsize < 1024 ? 1024 : sectorsize, &error); 200 if (buf == NULL || error != 0) 201 break; 202 if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) { 203 g_free(buf); 204 break; 205 } 206#if 0 207/* 208 * XXX: Some sources indicate this is a magic sequence, but appearantly 209 * XXX: it is not universal. Documentation would be wonderfule to have. 210 */ 211 if (buf[4] != 'I' || buf[5] != 'P' || 212 buf[6] != 'L' || buf[7] != '1') { 213 g_free(buf); 214 break; 215 } 216#endif 217 for (i = 0; i < NDOSPART; i++) 218 g_dec_dos_partition( 219 buf + 512 + i * sizeof(struct dos_partition), 220 ms->dp + i); 221 g_free(buf); 222 for (i = 0; i < NDOSPART; i++) { 223 /* If start and end are identical it's bogus */ 224 if (ms->dp[i].dp_ssect == ms->dp[i].dp_esect && 225 ms->dp[i].dp_shd == ms->dp[i].dp_ehd && 226 ms->dp[i].dp_scyl == ms->dp[i].dp_ecyl) 227 continue; 228 if (ms->dp[i].dp_ecyl == 0) 229 continue; 230 if (bootverbose) { 231 printf("PC98 Slice %d on %s:\n", 232 i + 1, gp->name); 233 g_pc98_print(i, ms->dp + i); 234 } 235 npart++; 236 ms->type[i] = (ms->dp[i].dp_sid << 8) | 237 ms->dp[i].dp_mid; 238 g_topology_lock(); 239 g_slice_config(gp, i, G_SLICE_CONFIG_SET, 240 ms->dp[i].dp_scyl * spercyl, 241 (ms->dp[i].dp_ecyl - ms->dp[i].dp_scyl + 1) * 242 spercyl, 243 sectorsize, 244 "%ss%d", gp->name, i + 1); 245 g_topology_unlock(); 246 } 247 break; 248 } 249 g_topology_lock(); 250 g_access_rel(cp, -1, 0, 0); 251 if (LIST_EMPTY(&gp->provider)) { 252 g_std_spoiled(cp); 253 return (NULL); 254 } 255 return (gp); 256} 257 258static struct g_class g_pc98_class = { 259 PC98_CLASS_NAME, 260 g_pc98_taste, 261 NULL, 262 G_CLASS_INITIALIZER 263}; 264 265DECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 266