bioscd.c revision 298230
1/*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/boot/pc98/libpc98/bioscd.c 298230 2016-04-18 23:09:22Z allanjude $"); 30 31/* 32 * BIOS CD device handling for CD's that have been booted off of via no 33 * emulation booting as defined in the El Torito standard. 34 * 35 * Ideas and algorithms from: 36 * 37 * - FreeBSD libi386/biosdisk.c 38 * 39 */ 40 41#include <stand.h> 42 43#include <sys/param.h> 44#include <machine/bootinfo.h> 45 46#include <stdarg.h> 47 48#include <bootstrap.h> 49#include <btxv86.h> 50#include "libi386.h" 51 52#define BIOSCD_SECSIZE 2048 53#define BUFSIZE (1 * BIOSCD_SECSIZE) 54#define MAXBCDEV 1 55 56/* Major numbers for devices we frontend for. */ 57#define ACDMAJOR 117 58#define CDMAJOR 15 59 60#ifdef DISK_DEBUG 61# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args) 62#else 63# define DEBUG(fmt, args...) 64#endif 65 66struct specification_packet { 67 u_char sp_size; 68 u_char sp_bootmedia; 69 u_char sp_drive; 70 u_char sp_controller; 71 u_int sp_lba; 72 u_short sp_devicespec; 73 u_short sp_buffersegment; 74 u_short sp_loadsegment; 75 u_short sp_sectorcount; 76 u_short sp_cylsec; 77 u_char sp_head; 78}; 79 80/* 81 * List of BIOS devices, translation from disk unit number to 82 * BIOS unit number. 83 */ 84static struct bcinfo { 85 int bc_unit; /* BIOS unit number */ 86 struct specification_packet bc_sp; 87 int bc_open; /* reference counter */ 88 void *bc_bcache; /* buffer cache data */ 89} bcinfo [MAXBCDEV]; 90static int nbcinfo = 0; 91 92#define BC(dev) (bcinfo[(dev)->d_unit]) 93 94static int bc_read(int unit, daddr_t dblk, int blks, caddr_t dest); 95static int bc_init(void); 96static int bc_strategy(void *devdata, int flag, daddr_t dblk, 97 size_t offset, size_t size, char *buf, size_t *rsize); 98static int bc_realstrategy(void *devdata, int flag, daddr_t dblk, 99 size_t offset, size_t size, char *buf, size_t *rsize); 100static int bc_open(struct open_file *f, ...); 101static int bc_close(struct open_file *f); 102static void bc_print(int verbose); 103 104struct devsw bioscd = { 105 "cd", 106 DEVT_CD, 107 bc_init, 108 bc_strategy, 109 bc_open, 110 bc_close, 111 noioctl, 112 bc_print, 113 NULL 114}; 115 116/* 117 * Translate between BIOS device numbers and our private unit numbers. 118 */ 119int 120bc_bios2unit(int biosdev) 121{ 122 int i; 123 124 DEBUG("looking for bios device 0x%x", biosdev); 125 for (i = 0; i < nbcinfo; i++) { 126 DEBUG("bc unit %d is BIOS device 0x%x", i, bcinfo[i].bc_unit); 127 if (bcinfo[i].bc_unit == biosdev) 128 return(i); 129 } 130 return(-1); 131} 132 133int 134bc_unit2bios(int unit) 135{ 136 if ((unit >= 0) && (unit < nbcinfo)) 137 return(bcinfo[unit].bc_unit); 138 return(-1); 139} 140 141/* 142 * We can't quiz, we have to be told what device to use, so this functoin 143 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS 144 * device number to add. 145 */ 146static int 147bc_init(void) 148{ 149 150 return (0); 151} 152 153int 154bc_add(int biosdev) 155{ 156 157 if (nbcinfo >= MAXBCDEV) 158 return (-1); 159 bcinfo[nbcinfo].bc_unit = biosdev; 160 161 /* SCSI CD-ROM only */ 162 if ((biosdev & 0xf0) != 0xa0) 163 return (-1); 164 if ((((uint32_t *)PTOV(0xA1460))[biosdev & 0x0f] & 0x1f) != 5) 165 return (-1); 166 167 printf("BIOS CD is cd%d\n", nbcinfo); 168 nbcinfo++; 169 bcache_add_dev(nbcinfo); /* register cd device in bcache */ 170 return(0); 171} 172 173/* 174 * Print information about disks 175 */ 176static void 177bc_print(int verbose) 178{ 179 char line[80]; 180 int i; 181 182 for (i = 0; i < nbcinfo; i++) { 183 sprintf(line, " cd%d: Device 0x%x\n", i, 184 bcinfo[i].bc_sp.sp_devicespec); 185 pager_output(line); 186 } 187} 188 189/* 190 * Attempt to open the disk described by (dev) for use by (f). 191 */ 192static int 193bc_open(struct open_file *f, ...) 194{ 195 va_list ap; 196 struct i386_devdesc *dev; 197 198 va_start(ap, f); 199 dev = va_arg(ap, struct i386_devdesc *); 200 va_end(ap); 201 if (dev->d_unit >= nbcinfo) { 202 DEBUG("attempt to open nonexistent disk"); 203 return(ENXIO); 204 } 205 206 BC(dev).bc_open++; 207 if (BC(dev).bc_bcache == NULL) 208 BC(dev).bc_bcache = bcache_allocate(); 209 return(0); 210} 211 212static int 213bc_close(struct open_file *f) 214{ 215 struct i386_devdesc *dev; 216 217 dev = (struct i386_devdesc *)f->f_devdata; 218 BC(dev).bc_open--; 219 if (BC(dev).bc_open == 0) { 220 bcache_free(BC(dev).bc_bcache); 221 BC(dev).bc_bcache = NULL; 222 } 223 return(0); 224} 225 226static int 227bc_strategy(void *devdata, int rw, daddr_t dblk, size_t offset, size_t size, 228 char *buf, size_t *rsize) 229{ 230 struct bcache_devdata bcd; 231 struct i386_devdesc *dev; 232 233 dev = (struct i386_devdesc *)devdata; 234 bcd.dv_strategy = bc_realstrategy; 235 bcd.dv_devdata = devdata; 236 bcd.dv_cache = BC(dev).bc_bcache; 237 238 return (bcache_strategy(&bcd, rw, dblk, offset, size, buf, rsize)); 239} 240 241static int 242bc_realstrategy(void *devdata, int rw, daddr_t dblk, size_t offset, size_t size, 243 char *buf, size_t *rsize) 244{ 245 struct i386_devdesc *dev; 246 int unit; 247 int blks; 248#ifdef BD_SUPPORT_FRAGS 249 char fragbuf[BIOSCD_SECSIZE]; 250 size_t fragsize; 251 252 fragsize = size % BIOSCD_SECSIZE; 253#else 254 if (size % BIOSCD_SECSIZE) 255 return (EINVAL); 256#endif 257 258 if (rw != F_READ) 259 return(EROFS); 260 dev = (struct i386_devdesc *)devdata; 261 unit = dev->d_unit; 262 blks = size / BIOSCD_SECSIZE; 263 if (dblk % (BIOSCD_SECSIZE / DEV_BSIZE) != 0) 264 return (EINVAL); 265 dblk /= (BIOSCD_SECSIZE / DEV_BSIZE); 266 DEBUG("read %d from %lld to %p", blks, dblk, buf); 267 268 if (rsize) 269 *rsize = 0; 270 if (blks && bc_read(unit, dblk, blks, buf)) { 271 DEBUG("read error"); 272 return (EIO); 273 } 274#ifdef BD_SUPPORT_FRAGS 275 DEBUG("frag read %d from %lld+%d to %p", 276 fragsize, dblk, blks, buf + (blks * BIOSCD_SECSIZE)); 277 if (fragsize && bc_read(unit, dblk + blks, 1, fragbuf)) { 278 DEBUG("frag read error"); 279 return(EIO); 280 } 281 bcopy(fragbuf, buf + (blks * BIOSCD_SECSIZE), fragsize); 282#endif 283 if (rsize) 284 *rsize = size; 285 return (0); 286} 287 288/* Max number of sectors to bounce-buffer at a time. */ 289#define CD_BOUNCEBUF 8 290 291static int 292bc_read(int unit, daddr_t dblk, int blks, caddr_t dest) 293{ 294 u_int maxfer, resid, result, retry, x; 295 caddr_t bbuf, p, xp; 296 int biosdev; 297#ifdef DISK_DEBUG 298 int error; 299#endif 300 301 /* Just in case some idiot actually tries to read -1 blocks... */ 302 if (blks < 0) 303 return (-1); 304 305 /* If nothing to do, just return succcess. */ 306 if (blks == 0) 307 return (0); 308 309 /* Decide whether we have to bounce */ 310 if (VTOP(dest) >> 20 != 0) { 311 /* 312 * The destination buffer is above first 1MB of 313 * physical memory so we have to arrange a suitable 314 * bounce buffer. 315 */ 316 x = min(CD_BOUNCEBUF, (unsigned)blks); 317 bbuf = alloca(x * BIOSCD_SECSIZE); 318 maxfer = x; 319 } else { 320 bbuf = NULL; 321 maxfer = 0; 322 } 323 324 biosdev = bc_unit2bios(unit); 325 resid = blks; 326 p = dest; 327 328 while (resid > 0) { 329 if (bbuf) 330 xp = bbuf; 331 else 332 xp = p; 333 x = resid; 334 if (maxfer > 0) 335 x = min(x, maxfer); 336 337 /* 338 * Loop retrying the operation a couple of times. The BIOS 339 * may also retry. 340 */ 341 for (retry = 0; retry < 3; retry++) { 342 /* If retrying, reset the drive */ 343 if (retry > 0) { 344 v86.ctl = V86_FLAGS; 345 v86.addr = 0x1b; 346 v86.eax = 0x0300 | biosdev; 347 v86int(); 348 } 349 350 v86.ctl = V86_FLAGS; 351 v86.addr = 0x1b; 352 v86.eax = 0x0600 | (biosdev & 0x7f); 353 v86.ebx = x * BIOSCD_SECSIZE; 354 v86.ecx = dblk & 0xffff; 355 v86.edx = (dblk >> 16) & 0xffff; 356 v86.ebp = VTOPOFF(xp); 357 v86.es = VTOPSEG(xp); 358 v86int(); 359 result = V86_CY(v86.efl); 360 if (result == 0) 361 break; 362 } 363 364#ifdef DISK_DEBUG 365 error = (v86.eax >> 8) & 0xff; 366#endif 367 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p, 368 VTOP(p), result ? "failed" : "ok"); 369 DEBUG("unit %d status 0x%x", unit, error); 370 if (bbuf != NULL) 371 bcopy(bbuf, p, x * BIOSCD_SECSIZE); 372 p += (x * BIOSCD_SECSIZE); 373 dblk += x; 374 resid -= x; 375 } 376 377/* hexdump(dest, (blks * BIOSCD_SECSIZE)); */ 378 return(0); 379} 380 381/* 382 * Return a suitable dev_t value for (dev). 383 */ 384int 385bc_getdev(struct i386_devdesc *dev) 386{ 387 int biosdev, unit, device; 388 int major; 389 int rootdev; 390 391 unit = dev->d_unit; 392 biosdev = bc_unit2bios(unit); 393 DEBUG("unit %d BIOS device %d", unit, biosdev); 394 if (biosdev == -1) /* not a BIOS device */ 395 return(-1); 396 397 device = biosdev & 0xf0; 398 if (device == 0x80) 399 major = ACDMAJOR; 400 else if (device == 0xa0) 401 major = CDMAJOR; 402 else 403 return (-1); 404 405 unit = 0; /* XXX */ 406 407 /* XXX: Assume partition 'a'. */ 408 rootdev = MAKEBOOTDEV(major, 0, unit, 0); 409 DEBUG("dev is 0x%x\n", rootdev); 410 return(rootdev); 411} 412