geom_pc98.c revision 114507
1122842Sdas/*- 2126904Scperciva * Copyright (c) 2002 Poul-Henning Kamp 3126904Scperciva * Copyright (c) 2002 Networks Associates Technology, Inc. 4126904Scperciva * All rights reserved. 5126904Scperciva * 6126904Scperciva * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7126904Scperciva * and NAI Labs, the Security Research Division of Network Associates, Inc. 8126904Scperciva * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9126904Scperciva * DARPA CHATS research program. 10126904Scperciva * 11126904Scperciva * Redistribution and use in source and binary forms, with or without 12126904Scperciva * modification, are permitted provided that the following conditions 13126904Scperciva * are met: 14126904Scperciva * 1. Redistributions of source code must retain the above copyright 15126904Scperciva * notice, this list of conditions and the following disclaimer. 16126904Scperciva * 2. Redistributions in binary form must reproduce the above copyright 17126904Scperciva * notice, this list of conditions and the following disclaimer in the 18126904Scperciva * documentation and/or other materials provided with the distribution. 19126904Scperciva * 20126904Scperciva * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21126904Scperciva * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22126904Scperciva * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23126904Scperciva * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24126904Scperciva * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25122842Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26122842Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27122842Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28122842Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29122842Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30126904Scperciva * SUCH DAMAGE. 31126117Scperciva * 32122842Sdas * $FreeBSD: head/sys/geom/geom_pc98.c 114507 2003-05-02 06:34:51Z phk $ 33122842Sdas */ 34122842Sdas 35122842Sdas#include <sys/param.h> 36122842Sdas#include <sys/endian.h> 37139685Sdelphij#include <sys/systm.h> 38122842Sdas#include <sys/kernel.h> 39139656Sdelphij#include <sys/malloc.h> 40122842Sdas#include <sys/bio.h> 41126117Scperciva#include <sys/lock.h> 42126117Scperciva#include <sys/mutex.h> 43126117Scperciva 44126117Scperciva#include <sys/diskpc98.h> 45126117Scperciva#include <geom/geom.h> 46126117Scperciva#include <geom/geom_slice.h> 47126117Scperciva 48126117Scperciva#define PC98_CLASS_NAME "PC98" 49126904Scperciva 50126904Scpercivastruct g_pc98_softc { 51122842Sdas u_int fwsectors, fwheads, sectorsize; 52 int type[NDOSPART]; 53 u_char sec[8192]; 54}; 55 56static void 57g_pc98_print(int i, struct pc98_partition *dp) 58{ 59 char sname[17]; 60 61 strncpy(sname, dp->dp_name, 16); 62 sname[16] = '\0'; 63 64 g_hexdump(dp, sizeof(dp[0])); 65 printf("[%d] mid:%d(0x%x) sid:%d(0x%x)", 66 i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid); 67 printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 68 printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 69 printf(" sname:%s\n", sname); 70} 71 72static int 73g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec) 74{ 75 int i, error; 76 off_t s[NDOSPART], l[NDOSPART]; 77 struct pc98_partition dp[NDOSPART]; 78 79 g_topology_assert(); 80 81 if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa) 82 return (EBUSY); 83 84#if 0 85 /* 86 * XXX: Some sources indicate this is a magic sequence, but appearantly 87 * XXX: it is not universal. Documentation would be wonderful to have. 88 */ 89 if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1') 90 return (EBUSY); 91#endif 92 93 for (i = 0; i < NDOSPART; i++) 94 pc98_partition_dec( 95 sec + 512 + i * sizeof(struct pc98_partition), &dp[i]); 96 97 for (i = 0; i < NDOSPART; i++) { 98 /* If start and end are identical it's bogus */ 99 if (dp[i].dp_ssect == dp[i].dp_esect && 100 dp[i].dp_shd == dp[i].dp_ehd && 101 dp[i].dp_scyl == dp[i].dp_ecyl) 102 s[i] = l[i] = 0; 103 else if (dp[i].dp_ecyl == 0) 104 s[i] = l[i] = 0; 105 else { 106 s[i] = (off_t)dp[i].dp_scyl * 107 ms->fwsectors * ms->fwheads * ms->sectorsize; 108 l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) * 109 ms->fwsectors * ms->fwheads * ms->sectorsize; 110 } 111 if (bootverbose) { 112 printf("PC98 Slice %d on %s:\n", i + 1, gp->name); 113 g_pc98_print(i, dp + i); 114 } 115 if (s[i] < 0 || l[i] < 0) 116 error = EBUSY; 117 else 118 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 119 s[i], l[i], ms->sectorsize, 120 "%ss%d", gp->name, i + 1); 121 if (error) 122 return (error); 123 } 124 125 for (i = 0; i < NDOSPART; i++) { 126 ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid; 127 g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i], 128 ms->sectorsize, "%ss%d", gp->name, i + 1); 129 } 130 131 bcopy(sec, ms->sec, sizeof (ms->sec)); 132 133 return (0); 134} 135 136static void 137g_pc98_ioctl(void *arg, int flag) 138{ 139 struct bio *bp; 140 struct g_geom *gp; 141 struct g_slicer *gsp; 142 struct g_pc98_softc *ms; 143 struct g_ioctl *gio; 144 struct g_consumer *cp; 145 u_char *sec; 146 int error; 147 148 bp = arg; 149 if (flag == EV_CANCEL) { 150 g_io_deliver(bp, ENXIO); 151 return; 152 } 153 gp = bp->bio_to->geom; 154 gsp = gp->softc; 155 ms = gsp->softc; 156 gio = (struct g_ioctl *)bp->bio_data; 157 158 /* The disklabel to set is the ioctl argument. */ 159 sec = gio->data; 160 161 error = g_pc98_modify(gp, ms, sec); 162 if (error) { 163 g_io_deliver(bp, error); 164 return; 165 } 166 cp = LIST_FIRST(&gp->consumer); 167 error = g_write_data(cp, 0, sec, 8192); 168 g_io_deliver(bp, error); 169} 170 171static int 172g_pc98_start(struct bio *bp) 173{ 174 struct g_provider *pp; 175 struct g_geom *gp; 176 struct g_pc98_softc *mp; 177 struct g_slicer *gsp; 178 struct g_ioctl *gio; 179 int idx, error; 180 181 pp = bp->bio_to; 182 idx = pp->index; 183 gp = pp->geom; 184 gsp = gp->softc; 185 mp = gsp->softc; 186 if (bp->bio_cmd == BIO_GETATTR) { 187 if (g_handleattr_int(bp, "PC98::type", mp->type[idx])) 188 return (1); 189 if (g_handleattr_off_t(bp, "PC98::offset", 190 gsp->slices[idx].offset)) 191 return (1); 192 } 193 194 /* We only handle ioctl(2) requests of the right format. */ 195 if (strcmp(bp->bio_attribute, "GEOM::ioctl")) 196 return (0); 197 else if (bp->bio_length != sizeof(*gio)) 198 return (0); 199 /* Get hold of the ioctl parameters. */ 200 gio = (struct g_ioctl *)bp->bio_data; 201 202 switch (gio->cmd) { 203 case DIOCSPC98: 204 /* 205 * These we cannot do without the topology lock and some 206 * some I/O requests. Ask the event-handler to schedule 207 * us in a less restricted environment. 208 */ 209 error = g_post_event(g_pc98_ioctl, bp, M_NOWAIT, gp, NULL); 210 if (error) 211 g_io_deliver(bp, error); 212 /* 213 * We must return non-zero to indicate that we will deal 214 * with this bio, even though we have not done so yet. 215 */ 216 return (1); 217 default: 218 return (0); 219 } 220 221 return (0); 222} 223 224static void 225g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 226 struct g_consumer *cp __unused, struct g_provider *pp) 227{ 228 struct g_pc98_softc *mp; 229 struct g_slicer *gsp; 230 struct pc98_partition dp; 231 char sname[17]; 232 233 gsp = gp->softc; 234 mp = gsp->softc; 235 g_slice_dumpconf(sb, indent, gp, cp, pp); 236 if (pp != NULL) { 237 pc98_partition_dec( 238 mp->sec + 512 + 239 pp->index * sizeof(struct pc98_partition), &dp); 240 strncpy(sname, dp.dp_name, 16); 241 sname[16] = '\0'; 242 if (indent == NULL) { 243 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 244 sbuf_printf(sb, " sn %s", sname); 245 } else { 246 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 247 mp->type[pp->index]); 248 sbuf_printf(sb, "%s<sname>%s</sname>\n", indent, 249 sname); 250 } 251 } 252} 253 254static struct g_geom * 255g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 256{ 257 struct g_geom *gp; 258 struct g_consumer *cp; 259 int error; 260 struct g_pc98_softc *ms; 261 struct g_slicer *gsp; 262 u_int fwsectors, fwheads, sectorsize; 263 u_char *buf; 264 265 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 266 g_topology_assert(); 267 if (flags == G_TF_NORMAL && 268 !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 269 return (NULL); 270 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start); 271 if (gp == NULL) 272 return (NULL); 273 gsp = gp->softc; 274 g_topology_unlock(); 275 gp->dumpconf = g_pc98_dumpconf; 276 do { 277 if (gp->rank != 2 && flags == G_TF_NORMAL) 278 break; 279 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 280 if (error || fwsectors == 0) { 281 fwsectors = 17; 282 if (bootverbose) 283 printf("g_pc98_taste: guessing %d sectors\n", 284 fwsectors); 285 } 286 error = g_getattr("GEOM::fwheads", cp, &fwheads); 287 if (error || fwheads == 0) { 288 fwheads = 8; 289 if (bootverbose) 290 printf("g_pc98_taste: guessing %d heads\n", 291 fwheads); 292 } 293 sectorsize = cp->provider->sectorsize; 294 if (sectorsize < 512) 295 break; 296 buf = g_read_data(cp, 0, 8192, &error); 297 if (buf == NULL || error != 0) 298 break; 299 ms->fwsectors = fwsectors; 300 ms->fwheads = fwheads; 301 ms->sectorsize = sectorsize; 302 g_topology_lock(); 303 g_pc98_modify(gp, ms, buf); 304 g_topology_unlock(); 305 g_free(buf); 306 break; 307 } while (0); 308 g_topology_lock(); 309 g_access_rel(cp, -1, 0, 0); 310 if (LIST_EMPTY(&gp->provider)) { 311 g_slice_spoiled(cp); 312 return (NULL); 313 } 314 return (gp); 315} 316 317static struct g_class g_pc98_class = { 318 .name = PC98_CLASS_NAME, 319 .taste = g_pc98_taste, 320 G_CLASS_INITIALIZER 321}; 322 323DECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 324