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