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>
53 #define BIOSCD_SECSIZE 2048
54 #define BUFSIZE (1 * BIOSCD_SECSIZE)
57 /* Major numbers for devices we frontend for. */
62 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
64 # define DEBUG(fmt, args...)
67 struct specification_packet {
73 u_short sp_devicespec;
74 u_short sp_buffersegment;
75 u_short sp_loadsegment;
76 u_short sp_sectorcount;
82 * List of BIOS devices, translation from disk unit number to
85 static struct bcinfo {
86 int bc_unit; /* BIOS unit number */
87 struct specification_packet bc_sp;
89 static int nbcinfo = 0;
91 static int bc_read(int unit, daddr_t dblk, int blks, caddr_t dest);
92 static int bc_init(void);
93 static int bc_strategy(void *devdata, int flag, daddr_t dblk,
94 size_t size, char *buf, size_t *rsize);
95 static int bc_open(struct open_file *f, ...);
96 static int bc_close(struct open_file *f);
97 static void bc_print(int verbose);
99 struct devsw bioscd = {
112 * Translate between BIOS device numbers and our private unit numbers.
115 bc_bios2unit(int biosdev)
119 DEBUG("looking for bios device 0x%x", biosdev);
120 for (i = 0; i < nbcinfo; i++) {
121 DEBUG("bc unit %d is BIOS device 0x%x", i, bcinfo[i].bc_unit);
122 if (bcinfo[i].bc_unit == biosdev)
129 bc_unit2bios(int unit)
131 if ((unit >= 0) && (unit < nbcinfo))
132 return(bcinfo[unit].bc_unit);
137 * We can't quiz, we have to be told what device to use, so this functoin
138 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
139 * device number to add.
152 if (nbcinfo >= MAXBCDEV)
154 bcinfo[nbcinfo].bc_unit = biosdev;
156 /* SCSI CD-ROM only */
157 if ((biosdev & 0xf0) != 0xa0)
159 if ((((uint32_t *)PTOV(0xA1460))[biosdev & 0x0f] & 0x1f) != 5)
162 printf("BIOS CD is cd%d\n", nbcinfo);
168 * Print information about disks
171 bc_print(int verbose)
176 for (i = 0; i < nbcinfo; i++) {
177 sprintf(line, " cd%d: Device 0x%x\n", i,
178 bcinfo[i].bc_sp.sp_devicespec);
184 * Attempt to open the disk described by (dev) for use by (f).
187 bc_open(struct open_file *f, ...)
190 struct i386_devdesc *dev;
193 dev = va_arg(ap, struct i386_devdesc *);
195 if (dev->d_unit >= nbcinfo) {
196 DEBUG("attempt to open nonexistent disk");
204 bc_close(struct open_file *f)
211 bc_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
214 struct i386_devdesc *dev;
217 #ifdef BD_SUPPORT_FRAGS
218 char fragbuf[BIOSCD_SECSIZE];
221 fragsize = size % BIOSCD_SECSIZE;
223 if (size % BIOSCD_SECSIZE)
229 dev = (struct i386_devdesc *)devdata;
231 blks = size / BIOSCD_SECSIZE;
232 if (dblk % (BIOSCD_SECSIZE / DEV_BSIZE) != 0)
234 dblk /= (BIOSCD_SECSIZE / DEV_BSIZE);
235 DEBUG("read %d from %d to %p", blks, dblk, buf);
239 if (blks && bc_read(unit, dblk, blks, buf)) {
243 #ifdef BD_SUPPORT_FRAGS
244 DEBUG("bc_strategy: frag read %d from %d+%d to %p",
245 fragsize, dblk, blks, buf + (blks * BIOSCD_SECSIZE));
246 if (fragsize && bc_read(unit, dblk + blks, 1, fragsize)) {
247 DEBUG("frag read error");
250 bcopy(fragbuf, buf + (blks * BIOSCD_SECSIZE), fragsize);
258 bc_read(int unit, daddr_t dblk, int blks, caddr_t dest)
261 static unsigned short packet[8];
267 /* Just in case some idiot actually tries to read -1 blocks... */
271 /* If nothing to do, just return succcess. */
275 biosdev = bc_unit2bios(unit);
277 * Loop retrying the operation a couple of times. The BIOS
280 for (retry = 0; retry < 3; retry++) {
281 /* If retrying, reset the drive */
285 v86.eax = 0x0300 | biosdev;
291 v86.eax = 0x0600 | (biosdev & 0x7f);
292 v86.ebx = blks * BIOSCD_SECSIZE;
293 v86.ecx = dblk & 0xffff;
294 v86.edx = (dblk >> 16) & 0xffff;
295 v86.ebp = VTOPOFF(dest);
296 v86.es = VTOPSEG(dest);
298 result = (v86.efl & PSL_C);
304 error = (v86.eax >> 8) & 0xff;
306 DEBUG("%d sectors from %ld to %p (0x%x) %s", blks, dblk, dest,
307 VTOP(dest), result ? "failed" : "ok");
308 DEBUG("unit %d status 0x%x", unit, error);
310 /* hexdump(dest, (blks * BIOSCD_SECSIZE)); */
315 * Return a suitable dev_t value for (dev).
318 bc_getdev(struct i386_devdesc *dev)
320 int biosdev, unit, device;
325 biosdev = bc_unit2bios(unit);
326 DEBUG("unit %d BIOS device %d", unit, biosdev);
327 if (biosdev == -1) /* not a BIOS device */
330 device = biosdev & 0xf0;
333 else if (device == 0xa0)
340 /* XXX: Assume partition 'a'. */
341 rootdev = MAKEBOOTDEV(major, 0, unit, 0);
342 DEBUG("dev is 0x%x\n", rootdev);