2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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.
35 * Ideas and algorithms from:
37 * - FreeBSD libi386/biosdisk.c
43 #include <sys/param.h>
44 #include <machine/bootinfo.h>
45 #include <machine/psl.h>
49 #include <bootstrap.h>
54 #define BIOSCD_SECSIZE 2048
55 #define BUFSIZE (1 * BIOSCD_SECSIZE)
58 /* Major numbers for devices we frontend for. */
63 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
65 # define DEBUG(fmt, args...)
68 struct specification_packet {
74 u_short sp_devicespec;
75 u_short sp_buffersegment;
76 u_short sp_loadsegment;
77 u_short sp_sectorcount;
83 * List of BIOS devices, translation from disk unit number to
86 static struct bcinfo {
87 int bc_unit; /* BIOS unit number */
88 struct specification_packet bc_sp;
90 static int nbcinfo = 0;
92 static int bc_read(int unit, daddr_t dblk, int blks, caddr_t dest);
93 static int bc_init(void);
94 static int bc_strategy(void *devdata, int flag, daddr_t dblk,
95 size_t size, char *buf, size_t *rsize);
96 static int bc_open(struct open_file *f, ...);
97 static int bc_close(struct open_file *f);
98 static void bc_print(int verbose);
100 struct devsw bioscd = {
113 * Translate between BIOS device numbers and our private unit numbers.
116 bc_bios2unit(int biosdev)
120 DEBUG("looking for bios device 0x%x", biosdev);
121 for (i = 0; i < nbcinfo; i++) {
122 DEBUG("bc unit %d is BIOS device 0x%x", i, bcinfo[i].bc_unit);
123 if (bcinfo[i].bc_unit == biosdev)
130 bc_unit2bios(int unit)
132 if ((unit >= 0) && (unit < nbcinfo))
133 return(bcinfo[unit].bc_unit);
138 * We can't quiz, we have to be told what device to use, so this functoin
139 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
140 * device number to add.
153 if (nbcinfo >= MAXBCDEV)
155 bcinfo[nbcinfo].bc_unit = biosdev;
160 v86.ds = VTOPSEG(&bcinfo[nbcinfo].bc_sp);
161 v86.esi = VTOPOFF(&bcinfo[nbcinfo].bc_sp);
163 if ((v86.eax & 0xff00) != 0)
166 printf("BIOS CD is cd%d\n", nbcinfo);
172 * Print information about disks
175 bc_print(int verbose)
180 for (i = 0; i < nbcinfo; i++) {
181 sprintf(line, " cd%d: Device 0x%x\n", i,
182 bcinfo[i].bc_sp.sp_devicespec);
188 * Attempt to open the disk described by (dev) for use by (f).
191 bc_open(struct open_file *f, ...)
194 struct i386_devdesc *dev;
197 dev = va_arg(ap, struct i386_devdesc *);
199 if (dev->d_unit >= nbcinfo) {
200 DEBUG("attempt to open nonexistent disk");
208 bc_close(struct open_file *f)
215 bc_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
218 struct i386_devdesc *dev;
221 #ifdef BD_SUPPORT_FRAGS
222 char fragbuf[BIOSCD_SECSIZE];
225 fragsize = size % BIOSCD_SECSIZE;
227 if (size % BIOSCD_SECSIZE)
233 dev = (struct i386_devdesc *)devdata;
235 blks = size / BIOSCD_SECSIZE;
236 if (dblk % (BIOSCD_SECSIZE / DEV_BSIZE) != 0)
238 dblk /= (BIOSCD_SECSIZE / DEV_BSIZE);
239 DEBUG("read %d from %lld to %p", blks, dblk, buf);
243 if (blks && bc_read(unit, dblk, blks, buf)) {
247 #ifdef BD_SUPPORT_FRAGS
248 DEBUG("frag read %d from %lld+%d to %p",
249 fragsize, dblk, blks, buf + (blks * BIOSCD_SECSIZE));
250 if (fragsize && bc_read(unit, dblk + blks, 1, fragbuf)) {
251 DEBUG("frag read error");
254 bcopy(fragbuf, buf + (blks * BIOSCD_SECSIZE), fragsize);
261 /* Max number of sectors to bounce-buffer at a time. */
262 #define CD_BOUNCEBUF 8
265 bc_read(int unit, daddr_t dblk, int blks, caddr_t dest)
267 u_int maxfer, resid, result, retry, x;
269 static struct edd_packet packet;
275 /* Just in case some idiot actually tries to read -1 blocks... */
279 /* If nothing to do, just return succcess. */
283 /* Decide whether we have to bounce */
284 if (VTOP(dest) >> 20 != 0) {
286 * The destination buffer is above first 1MB of
287 * physical memory so we have to arrange a suitable
290 x = min(CD_BOUNCEBUF, (unsigned)blks);
291 bbuf = alloca(x * BIOSCD_SECSIZE);
298 biosdev = bc_unit2bios(unit);
312 * Loop retrying the operation a couple of times. The BIOS
315 for (retry = 0; retry < 3; retry++) {
316 /* If retrying, reset the drive */
325 packet.len = sizeof(struct edd_packet);
327 packet.off = VTOPOFF(xp);
328 packet.seg = VTOPSEG(xp);
334 v86.ds = VTOPSEG(&packet);
335 v86.esi = VTOPOFF(&packet);
337 result = (v86.efl & PSL_C);
343 error = (v86.eax >> 8) & 0xff;
345 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p,
346 VTOP(p), result ? "failed" : "ok");
347 DEBUG("unit %d status 0x%x", unit, error);
349 bcopy(bbuf, p, x * BIOSCD_SECSIZE);
350 p += (x * BIOSCD_SECSIZE);
355 /* hexdump(dest, (blks * BIOSCD_SECSIZE)); */
360 * Return a suitable dev_t value for (dev).
363 bc_getdev(struct i386_devdesc *dev)
370 biosdev = bc_unit2bios(unit);
371 DEBUG("unit %d BIOS device %d", unit, biosdev);
372 if (biosdev == -1) /* not a BIOS device */
376 * XXX: Need to examine device spec here to figure out if SCSI or
377 * ATAPI. No idea on how to figure out device number. All we can
378 * really pass to the kernel is what bus and device on which bus we
379 * were booted from, which dev_t isn't well suited to since those
380 * number don't match to unit numbers very well. We may just need
381 * to engage in a hack where we pass -C to the boot args if we are
387 /* XXX: Assume partition 'a'. */
388 rootdev = MAKEBOOTDEV(major, 0, unit, 0);
389 DEBUG("dev is 0x%x\n", rootdev);