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