geom_pc98.c revision 138221
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 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/geom/geom_pc98.c 138221 2004-11-30 09:40:11Z imp $"); 35 36#include <sys/param.h> 37#include <sys/endian.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/malloc.h> 41#include <sys/bio.h> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44 45#include <sys/diskpc98.h> 46#include <geom/geom.h> 47#include <geom/geom_slice.h> 48 49#define PC98_CLASS_NAME "PC98" 50 51struct g_pc98_softc { 52 u_int fwsectors, fwheads, sectorsize; 53 int type[NDOSPART]; 54 u_char sec[8192]; 55}; 56 57static void 58g_pc98_print(int i, struct pc98_partition *dp) 59{ 60 char sname[17]; 61 62 strncpy(sname, dp->dp_name, 16); 63 sname[16] = '\0'; 64 65 hexdump(dp, sizeof(dp[0]), NULL, 0); 66 printf("[%d] mid:%d(0x%x) sid:%d(0x%x)", 67 i, dp->dp_mid, dp->dp_mid, dp->dp_sid, dp->dp_sid); 68 printf(" s:%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 69 printf(" e:%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 70 printf(" sname:%s\n", sname); 71} 72 73static int 74g_pc98_modify(struct g_geom *gp, struct g_pc98_softc *ms, u_char *sec) 75{ 76 int i, error; 77 off_t s[NDOSPART], l[NDOSPART]; 78 struct pc98_partition dp[NDOSPART]; 79 80 g_topology_assert(); 81 82 if (sec[0x1fe] != 0x55 || sec[0x1ff] != 0xaa) 83 return (EBUSY); 84 85#if 0 86 /* 87 * By convetion, it seems that the ipl program has a jump at location 88 * 0 to the real start of the boot loader. By convetion, it appears 89 * that after this jump, there's a string, terminated by at last one, 90 * if not more, zeros, followed by the target of the jump. FreeBSD's 91 * pc98 boot0 uses 'IPL1' followed by 3 zeros here, likely for 92 * compatibility with some older boot loader. Linux98's boot loader 93 * appears to use 'Linux 98' followed by only two. GRUB/98 appears to 94 * use 'GRUB/98 ' followed by none. These last two appear to be 95 * ported from the ia32 versions, but appear to show similar 96 * convention. Grub/98 has an additional NOP after the jmp, which 97 * isn't present in others. 98 * 99 * The following test was inspired by looking only at partitions 100 * with FreeBSD's boot0 (or one that it is compatible with). As 101 * such, if failed when other IPL programs were used. 102 */ 103 if (sec[4] != 'I' || sec[5] != 'P' || sec[6] != 'L' || sec[7] != '1') 104 return (EBUSY); 105#endif 106 107 for (i = 0; i < NDOSPART; i++) 108 pc98_partition_dec( 109 sec + 512 + i * sizeof(struct pc98_partition), &dp[i]); 110 111 for (i = 0; i < NDOSPART; i++) { 112 /* If start and end are identical it's bogus */ 113 if (dp[i].dp_ssect == dp[i].dp_esect && 114 dp[i].dp_shd == dp[i].dp_ehd && 115 dp[i].dp_scyl == dp[i].dp_ecyl) 116 s[i] = l[i] = 0; 117 else if (dp[i].dp_ecyl == 0) 118 s[i] = l[i] = 0; 119 else { 120 s[i] = (off_t)dp[i].dp_scyl * 121 ms->fwsectors * ms->fwheads * ms->sectorsize; 122 l[i] = (off_t)(dp[i].dp_ecyl - dp[i].dp_scyl + 1) * 123 ms->fwsectors * ms->fwheads * ms->sectorsize; 124 } 125 if (bootverbose) { 126 printf("PC98 Slice %d on %s:\n", i + 1, gp->name); 127 g_pc98_print(i, dp + i); 128 } 129 if (s[i] < 0 || l[i] < 0) 130 error = EBUSY; 131 else 132 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 133 s[i], l[i], ms->sectorsize, 134 "%ss%d", gp->name, i + 1); 135 if (error) 136 return (error); 137 } 138 139 for (i = 0; i < NDOSPART; i++) { 140 ms->type[i] = (dp[i].dp_sid << 8) | dp[i].dp_mid; 141 g_slice_config(gp, i, G_SLICE_CONFIG_SET, s[i], l[i], 142 ms->sectorsize, "%ss%d", gp->name, i + 1); 143 } 144 145 bcopy(sec, ms->sec, sizeof (ms->sec)); 146 147 return (0); 148} 149 150static int 151g_pc98_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td) 152{ 153 struct g_geom *gp; 154 struct g_pc98_softc *ms; 155 struct g_slicer *gsp; 156 struct g_consumer *cp; 157 int error; 158 159 gp = pp->geom; 160 gsp = gp->softc; 161 ms = gsp->softc; 162 163 switch(cmd) { 164 case DIOCSPC98: { 165 DROP_GIANT(); 166 g_topology_lock(); 167 /* Validate and modify our slicer instance to match. */ 168 error = g_pc98_modify(gp, ms, data); 169 cp = LIST_FIRST(&gp->consumer); 170 error = g_write_data(cp, 0, data, 8192); 171 g_topology_unlock(); 172 PICKUP_GIANT(); 173 return(error); 174 } 175 default: 176 return (ENOIOCTL); 177 } 178} 179 180static int 181g_pc98_start(struct bio *bp) 182{ 183 struct g_provider *pp; 184 struct g_geom *gp; 185 struct g_pc98_softc *mp; 186 struct g_slicer *gsp; 187 int idx; 188 189 pp = bp->bio_to; 190 idx = pp->index; 191 gp = pp->geom; 192 gsp = gp->softc; 193 mp = gsp->softc; 194 if (bp->bio_cmd == BIO_GETATTR) { 195 if (g_handleattr_int(bp, "PC98::type", mp->type[idx])) 196 return (1); 197 if (g_handleattr_off_t(bp, "PC98::offset", 198 gsp->slices[idx].offset)) 199 return (1); 200 } 201 202 return (0); 203} 204 205static void 206g_pc98_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 207 struct g_consumer *cp __unused, struct g_provider *pp) 208{ 209 struct g_pc98_softc *mp; 210 struct g_slicer *gsp; 211 struct pc98_partition dp; 212 char sname[17]; 213 214 gsp = gp->softc; 215 mp = gsp->softc; 216 g_slice_dumpconf(sb, indent, gp, cp, pp); 217 if (pp != NULL) { 218 pc98_partition_dec( 219 mp->sec + 512 + 220 pp->index * sizeof(struct pc98_partition), &dp); 221 strncpy(sname, dp.dp_name, 16); 222 sname[16] = '\0'; 223 if (indent == NULL) { 224 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 225 sbuf_printf(sb, " sn %s", sname); 226 } else { 227 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 228 mp->type[pp->index]); 229 sbuf_printf(sb, "%s<sname>%s</sname>\n", indent, 230 sname); 231 } 232 } 233} 234 235static struct g_geom * 236g_pc98_taste(struct g_class *mp, struct g_provider *pp, int flags) 237{ 238 struct g_geom *gp; 239 struct g_consumer *cp; 240 int error; 241 struct g_pc98_softc *ms; 242 u_int fwsectors, fwheads, sectorsize; 243 u_char *buf; 244 245 g_trace(G_T_TOPOLOGY, "g_pc98_taste(%s,%s)", mp->name, pp->name); 246 g_topology_assert(); 247 if (flags == G_TF_NORMAL && 248 !strcmp(pp->geom->class->name, PC98_CLASS_NAME)) 249 return (NULL); 250 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_pc98_start); 251 if (gp == NULL) 252 return (NULL); 253 g_topology_unlock(); 254 do { 255 if (gp->rank != 2 && flags == G_TF_NORMAL) 256 break; 257 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 258 if (error || fwsectors == 0) { 259 fwsectors = 17; 260 if (bootverbose) 261 printf("g_pc98_taste: guessing %d sectors\n", 262 fwsectors); 263 } 264 error = g_getattr("GEOM::fwheads", cp, &fwheads); 265 if (error || fwheads == 0) { 266 fwheads = 8; 267 if (bootverbose) 268 printf("g_pc98_taste: guessing %d heads\n", 269 fwheads); 270 } 271 sectorsize = cp->provider->sectorsize; 272 if (sectorsize % 512 != 0) 273 break; 274 if (!strncmp(gp->name, "ad", 2)) { 275 u_int total_secs = cp->provider->mediasize/sectorsize; 276 277 if (total_secs < 17*8*65535) { 278 fwsectors = 17; 279 fwheads = 8; 280 } 281 else if (total_secs < 63*16*65535) { 282 if (fwsectors > 63) 283 fwsectors = 63; 284 if (fwheads > 16) 285 fwheads = 16; 286 } 287 else if (total_secs < 255*16*65535) { 288 fwsectors = 255; 289 if (fwheads > 16) 290 fwheads = 16; 291 } 292 else { 293 fwsectors = 255; 294 fwheads = 255; 295 } 296 } 297 buf = g_read_data(cp, 0, 8192, &error); 298 if (buf == NULL || error != 0) 299 break; 300 ms->fwsectors = fwsectors; 301 ms->fwheads = fwheads; 302 ms->sectorsize = sectorsize; 303 g_topology_lock(); 304 g_pc98_modify(gp, ms, buf); 305 g_topology_unlock(); 306 g_free(buf); 307 break; 308 } while (0); 309 g_topology_lock(); 310 g_access(cp, -1, 0, 0); 311 if (LIST_EMPTY(&gp->provider)) { 312 g_slice_spoiled(cp); 313 return (NULL); 314 } 315 return (gp); 316} 317 318static struct g_class g_pc98_class = { 319 .name = PC98_CLASS_NAME, 320 .version = G_VERSION, 321 .taste = g_pc98_taste, 322 .dumpconf = g_pc98_dumpconf, 323 .ioctl = g_pc98_ioctl, 324}; 325 326DECLARE_GEOM_CLASS(g_pc98_class, g_pc98); 327