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