]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/boot/i386/libi386/bioscd.c
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / sys / boot / i386 / libi386 / bioscd.c
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$");
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 #include <machine/psl.h>
46
47 #include <stdarg.h>
48
49 #include <bootstrap.h>
50 #include <btxv86.h>
51 #include "libi386.h"
52
53 #define BIOSCD_SECSIZE          2048
54 #define BUFSIZE                 (1 * BIOSCD_SECSIZE)
55 #define MAXBCDEV                1
56
57 /* Major numbers for devices we frontend for. */
58 #define ACDMAJOR                117
59 #define CDMAJOR                 15
60
61 #ifdef DISK_DEBUG
62 # define DEBUG(fmt, args...)    printf("%s: " fmt "\n" , __func__ , ## args)
63 #else
64 # define DEBUG(fmt, args...)
65 #endif
66
67 struct specification_packet {
68         u_char  sp_size;
69         u_char  sp_bootmedia;
70         u_char  sp_drive;
71         u_char  sp_controller;
72         u_int   sp_lba;
73         u_short sp_devicespec;
74         u_short sp_buffersegment;
75         u_short sp_loadsegment;
76         u_short sp_sectorcount;
77         u_short sp_cylsec;
78         u_char  sp_head;
79 };
80
81 /*
82  * List of BIOS devices, translation from disk unit number to
83  * BIOS unit number.
84  */
85 static struct bcinfo {
86         int     bc_unit;                /* BIOS unit number */
87         struct specification_packet bc_sp;
88 } bcinfo [MAXBCDEV];
89 static int nbcinfo = 0;
90
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);
98
99 struct devsw bioscd = {
100         "cd", 
101         DEVT_CD, 
102         bc_init,
103         bc_strategy, 
104         bc_open, 
105         bc_close, 
106         noioctl,
107         bc_print,
108         NULL
109 };
110
111 /*
112  * Translate between BIOS device numbers and our private unit numbers.
113  */
114 int
115 bc_bios2unit(int biosdev)
116 {
117         int i;
118     
119         DEBUG("looking for bios device 0x%x", biosdev);
120         printf("looking for bios device 0x%x, nbcinfo=%d\n", biosdev, nbcinfo);
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)
124                         return(i);
125         }
126         return(-1);
127 }
128
129 int
130 bc_unit2bios(int unit)
131 {
132         if ((unit >= 0) && (unit < nbcinfo))
133                 return(bcinfo[unit].bc_unit);
134         return(-1);
135 }
136
137 /*    
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.
141  */
142 static int
143 bc_init(void) 
144 {
145
146         return (0);
147 }
148
149 int
150 bc_add(int biosdev)
151 {
152         printf("bc_add(%d)\n", biosdev);
153
154         if (nbcinfo >= MAXBCDEV)
155                 return (-1);
156         bcinfo[nbcinfo].bc_unit = biosdev;
157         v86.ctl = V86_FLAGS;
158         v86.addr = 0x13;
159         v86.eax = 0x4b01;
160         v86.edx = biosdev;
161         v86.ds = VTOPSEG(&bcinfo[nbcinfo].bc_sp);
162         v86.esi = VTOPOFF(&bcinfo[nbcinfo].bc_sp);
163         v86int();
164         if ((v86.eax & 0xff00) != 0) {
165                 printf("CD probe failed, eax=0x%08x\n", v86.eax);
166                 return (-1);
167         }
168
169         printf("BIOS CD is cd%d\n", nbcinfo);
170         nbcinfo++;
171         return(0);
172 }
173
174 /*
175  * Print information about disks
176  */
177 static void
178 bc_print(int verbose)
179 {
180         char line[80];
181         int i;
182
183         for (i = 0; i < nbcinfo; i++) {
184                 sprintf(line, "    cd%d: Device 0x%x\n", i,
185                     bcinfo[i].bc_sp.sp_devicespec);
186                 pager_output(line);
187         }
188 }
189
190 /*
191  * Attempt to open the disk described by (dev) for use by (f).
192  */
193 static int 
194 bc_open(struct open_file *f, ...)
195 {
196         va_list ap;
197         struct i386_devdesc *dev;
198
199         va_start(ap, f);
200         dev = va_arg(ap, struct i386_devdesc *);
201         va_end(ap);
202         if (dev->d_unit >= nbcinfo) {
203                 DEBUG("attempt to open nonexistent disk");
204                 return(ENXIO);
205         }
206
207         return(0);
208 }
209  
210 static int 
211 bc_close(struct open_file *f)
212 {
213
214         return(0);
215 }
216
217 static int 
218 bc_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
219     size_t *rsize)
220 {
221         struct i386_devdesc *dev;
222         int unit;
223         int blks;
224 #ifdef BD_SUPPORT_FRAGS
225         char fragbuf[BIOSCD_SECSIZE];
226         size_t fragsize;
227
228         fragsize = size % BIOSCD_SECSIZE;
229 #else
230         if (size % BIOSCD_SECSIZE)
231                 return (EINVAL);
232 #endif
233
234         if (rw != F_READ)
235                 return(EROFS);
236         dev = (struct i386_devdesc *)devdata;
237         unit = dev->d_unit;
238         blks = size / BIOSCD_SECSIZE;
239         if (dblk % (BIOSCD_SECSIZE / DEV_BSIZE) != 0)
240                 return (EINVAL);
241         dblk /= (BIOSCD_SECSIZE / DEV_BSIZE);
242         DEBUG("read %d from %lld to %p", blks, dblk, buf);
243
244         if (rsize)
245                 *rsize = 0;
246         if (blks && bc_read(unit, dblk, blks, buf)) {
247                 DEBUG("read error");
248                 return (EIO);
249         }
250 #ifdef BD_SUPPORT_FRAGS
251         DEBUG("frag read %d from %lld+%d to %p", 
252             fragsize, dblk, blks, buf + (blks * BIOSCD_SECSIZE));
253         if (fragsize && bc_read(unit, dblk + blks, 1, fragbuf)) {
254                 DEBUG("frag read error");
255                 return(EIO);
256         }
257         bcopy(fragbuf, buf + (blks * BIOSCD_SECSIZE), fragsize);
258 #endif  
259         if (rsize)
260                 *rsize = size;
261         return (0);
262 }
263
264 /* Max number of sectors to bounce-buffer at a time. */
265 #define CD_BOUNCEBUF    8
266
267 static int
268 bc_read(int unit, daddr_t dblk, int blks, caddr_t dest)
269 {
270         u_int maxfer, resid, result, retry, x;
271         caddr_t bbuf, p, xp;
272         static struct edd_packet packet;
273         int biosdev;
274 #ifdef DISK_DEBUG
275         int error;
276 #endif
277     
278         /* Just in case some idiot actually tries to read -1 blocks... */
279         if (blks < 0)
280                 return (-1);
281
282         /* If nothing to do, just return succcess. */
283         if (blks == 0)
284                 return (0);
285
286         /* Decide whether we have to bounce */
287         if (VTOP(dest) >> 20 != 0) {
288                 /* 
289                  * The destination buffer is above first 1MB of
290                  * physical memory so we have to arrange a suitable
291                  * bounce buffer.
292                  */
293                 x = min(CD_BOUNCEBUF, (unsigned)blks);
294                 bbuf = alloca(x * BIOSCD_SECSIZE);
295                 maxfer = x;
296         } else {
297                 bbuf = NULL;
298                 maxfer = 0;
299         }
300         
301         biosdev = bc_unit2bios(unit);
302         resid = blks;
303         p = dest;
304
305         while (resid > 0) {
306                 if (bbuf)
307                         xp = bbuf;
308                 else
309                         xp = p;
310                 x = resid;
311                 if (maxfer > 0)
312                         x = min(x, maxfer);
313
314                 /*
315                  * Loop retrying the operation a couple of times.  The BIOS
316                  * may also retry.
317                  */
318                 for (retry = 0; retry < 3; retry++) {
319                         /* If retrying, reset the drive */
320                         if (retry > 0) {
321                                 v86.ctl = V86_FLAGS;
322                                 v86.addr = 0x13;
323                                 v86.eax = 0;
324                                 v86.edx = biosdev;
325                                 v86int();
326                         }
327
328                         packet.len = 0x10;
329                         packet.count = x;
330                         packet.offset = VTOPOFF(xp);
331                         packet.seg = VTOPSEG(xp);
332                         packet.lba = dblk;
333                         v86.ctl = V86_FLAGS;
334                         v86.addr = 0x13;
335                         v86.eax = 0x4200;
336                         v86.edx = biosdev;
337                         v86.ds = VTOPSEG(&packet);
338                         v86.esi = VTOPOFF(&packet);
339                         v86int();
340                         result = (v86.efl & PSL_C);
341                         if (result == 0)
342                                 break;
343                 }
344         
345 #ifdef DISK_DEBUG
346                 error = (v86.eax >> 8) & 0xff;
347 #endif
348                 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p,
349                     VTOP(p), result ? "failed" : "ok");
350                 DEBUG("unit %d  status 0x%x", unit, error);
351                 if (bbuf != NULL)
352                         bcopy(bbuf, p, x * BIOSCD_SECSIZE);
353                 p += (x * BIOSCD_SECSIZE);
354                 dblk += x;
355                 resid -= x;
356         }
357         
358 /*      hexdump(dest, (blks * BIOSCD_SECSIZE)); */
359         return(0);
360 }
361
362 /*
363  * Return a suitable dev_t value for (dev).
364  */
365 int
366 bc_getdev(struct i386_devdesc *dev)
367 {
368     int biosdev, unit;
369     int major;
370     int rootdev;
371
372     unit = dev->d_unit;
373     biosdev = bc_unit2bios(unit);
374     DEBUG("unit %d BIOS device %d", unit, biosdev);
375     if (biosdev == -1)                          /* not a BIOS device */
376         return(-1);
377
378     /*
379      * XXX: Need to examine device spec here to figure out if SCSI or
380      * ATAPI.  No idea on how to figure out device number.  All we can
381      * really pass to the kernel is what bus and device on which bus we
382      * were booted from, which dev_t isn't well suited to since those
383      * number don't match to unit numbers very well.  We may just need
384      * to engage in a hack where we pass -C to the boot args if we are
385      * the boot device.
386      */
387     major = ACDMAJOR;
388     unit = 0;   /* XXX */
389
390     /* XXX: Assume partition 'a'. */
391     rootdev = MAKEBOOTDEV(major, 0, unit, 0);
392     DEBUG("dev is 0x%x\n", rootdev);
393     return(rootdev);
394 }