]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/bioscd.c
sfxge(4): rollback last seen VLAN TCI if Tx packet is dropped
[FreeBSD/FreeBSD.git] / stand / 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
46 #include <stdarg.h>
47
48 #include <bootstrap.h>
49 #include <btxv86.h>
50 #include <edd.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         int     bc_open;                /* reference counter */
89         void    *bc_bcache;             /* buffer cache data */
90 } bcinfo [MAXBCDEV];
91 static int nbcinfo = 0;
92
93 #define BC(dev) (bcinfo[(dev)->dd.d_unit])
94
95 static int      bc_read(int unit, daddr_t dblk, int blks, caddr_t dest);
96 static int      bc_init(void);
97 static int      bc_strategy(void *devdata, int flag, daddr_t dblk,
98     size_t size, char *buf, size_t *rsize);
99 static int      bc_realstrategy(void *devdata, int flag, daddr_t dblk,
100     size_t size, char *buf, size_t *rsize);
101 static int      bc_open(struct open_file *f, ...);
102 static int      bc_close(struct open_file *f);
103 static int      bc_print(int verbose);
104
105 struct devsw bioscd = {
106         "cd", 
107         DEVT_CD, 
108         bc_init,
109         bc_strategy, 
110         bc_open, 
111         bc_close, 
112         noioctl,
113         bc_print,
114         NULL
115 };
116
117 /*
118  * Translate between BIOS device numbers and our private unit numbers.
119  */
120 int
121 bc_bios2unit(int biosdev)
122 {
123         int i;
124     
125         DEBUG("looking for bios device 0x%x", biosdev);
126         for (i = 0; i < nbcinfo; i++) {
127                 DEBUG("bc unit %d is BIOS device 0x%x", i, bcinfo[i].bc_unit);
128                 if (bcinfo[i].bc_unit == biosdev)
129                         return(i);
130         }
131         return(-1);
132 }
133
134 int
135 bc_unit2bios(int unit)
136 {
137         if ((unit >= 0) && (unit < nbcinfo))
138                 return(bcinfo[unit].bc_unit);
139         return(-1);
140 }
141
142 /*    
143  * We can't quiz, we have to be told what device to use, so this functoin
144  * doesn't do anything.  Instead, the loader calls bc_add() with the BIOS
145  * device number to add.
146  */
147 static int
148 bc_init(void) 
149 {
150
151         return (0);
152 }
153
154 int
155 bc_add(int biosdev)
156 {
157
158         if (nbcinfo >= MAXBCDEV)
159                 return (-1);
160         bcinfo[nbcinfo].bc_unit = biosdev;
161         v86.ctl = V86_FLAGS;
162         v86.addr = 0x13;
163         v86.eax = 0x4b01;
164         v86.edx = biosdev;
165         v86.ds = VTOPSEG(&bcinfo[nbcinfo].bc_sp);
166         v86.esi = VTOPOFF(&bcinfo[nbcinfo].bc_sp);
167         v86int();
168         if ((v86.eax & 0xff00) != 0)
169                 return (-1);
170
171         printf("BIOS CD is cd%d\n", nbcinfo);
172         nbcinfo++;
173         bcache_add_dev(nbcinfo);        /* register cd device in bcache */
174         return(0);
175 }
176
177 /*
178  * Print information about disks
179  */
180 static int
181 bc_print(int verbose)
182 {
183         char line[80];
184         int i, ret = 0;
185
186         if (nbcinfo == 0)
187                 return (0);
188
189         printf("%s devices:", bioscd.dv_name);
190         if ((ret = pager_output("\n")) != 0)
191                 return (ret);
192
193         for (i = 0; i < nbcinfo; i++) {
194                 snprintf(line, sizeof(line), "    cd%d: Device 0x%x\n", i,
195                     bcinfo[i].bc_sp.sp_devicespec);
196                 if ((ret = pager_output(line)) != 0)
197                         break;
198         }
199         return (ret);
200 }
201
202 /*
203  * Attempt to open the disk described by (dev) for use by (f).
204  */
205 static int 
206 bc_open(struct open_file *f, ...)
207 {
208         va_list ap;
209         struct i386_devdesc *dev;
210
211         va_start(ap, f);
212         dev = va_arg(ap, struct i386_devdesc *);
213         va_end(ap);
214         if (dev->dd.d_unit >= nbcinfo) {
215                 DEBUG("attempt to open nonexistent disk");
216                 return(ENXIO);
217         }
218
219         BC(dev).bc_open++;
220         if (BC(dev).bc_bcache == NULL)
221                 BC(dev).bc_bcache = bcache_allocate();
222         return(0);
223 }
224  
225 static int 
226 bc_close(struct open_file *f)
227 {
228         struct i386_devdesc *dev;
229
230         dev = (struct i386_devdesc *)f->f_devdata;
231         BC(dev).bc_open--;
232         if (BC(dev).bc_open == 0) {
233                 bcache_free(BC(dev).bc_bcache);
234                 BC(dev).bc_bcache = NULL;
235         }
236         return(0);
237 }
238
239 static int
240 bc_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
241     char *buf, size_t *rsize)
242 {
243         struct bcache_devdata bcd;
244         struct i386_devdesc *dev;
245
246         dev = (struct i386_devdesc *)devdata;
247         bcd.dv_strategy = bc_realstrategy;
248         bcd.dv_devdata = devdata;
249         bcd.dv_cache = BC(dev).bc_bcache;
250
251         return (bcache_strategy(&bcd, rw, dblk, size, buf, rsize));
252 }
253
254 static int 
255 bc_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
256     char *buf, size_t *rsize)
257 {
258         struct i386_devdesc *dev;
259         int unit;
260         int blks;
261
262         if (size % BIOSCD_SECSIZE)
263                 return (EINVAL);
264
265         if ((rw & F_MASK) != F_READ)
266                 return(EROFS);
267         dev = (struct i386_devdesc *)devdata;
268         unit = dev->dd.d_unit;
269         blks = size / BIOSCD_SECSIZE;
270         if (dblk % (BIOSCD_SECSIZE / DEV_BSIZE) != 0)
271                 return (EINVAL);
272         dblk /= (BIOSCD_SECSIZE / DEV_BSIZE);
273         DEBUG("read %d from %lld to %p", blks, dblk, buf);
274
275         if (rsize)
276                 *rsize = 0;
277         if ((blks = bc_read(unit, dblk, blks, buf)) < 0) {
278                 DEBUG("read error");
279                 return (EIO);
280         } else {
281                 if (size / BIOSCD_SECSIZE > blks) {
282                         if (rsize)
283                                 *rsize = blks * BIOSCD_SECSIZE;
284                         return (0);
285                 }
286         }
287         if (rsize)
288                 *rsize = size;
289         return (0);
290 }
291
292 /* return negative value for an error, otherwise blocks read */
293 static int
294 bc_read(int unit, daddr_t dblk, int blks, caddr_t dest)
295 {
296         u_int maxfer, resid, result, retry, x;
297         caddr_t bbuf, p, xp;
298         static struct edd_packet packet;
299         int biosdev;
300 #ifdef DISK_DEBUG
301         int error;
302 #endif
303     
304         /* Just in case some idiot actually tries to read -1 blocks... */
305         if (blks < 0)
306                 return (-1);
307
308         /* If nothing to do, just return succcess. */
309         if (blks == 0)
310                 return (0);
311
312         /* Decide whether we have to bounce */
313         if (VTOP(dest) >> 20 != 0) {
314                 /* 
315                  * The destination buffer is above first 1MB of
316                  * physical memory so we have to arrange a suitable
317                  * bounce buffer.
318                  */
319                 x = V86_IO_BUFFER_SIZE / BIOSCD_SECSIZE;
320                 x = min(x, (unsigned)blks);
321                 bbuf = PTOV(V86_IO_BUFFER);
322                 maxfer = x;
323         } else {
324                 bbuf = NULL;
325                 maxfer = 0;
326         }
327         
328         biosdev = bc_unit2bios(unit);
329         resid = blks;
330         p = dest;
331
332         while (resid > 0) {
333                 if (bbuf)
334                         xp = bbuf;
335                 else
336                         xp = p;
337                 x = resid;
338                 if (maxfer > 0)
339                         x = min(x, maxfer);
340
341                 /*
342                  * Loop retrying the operation a couple of times.  The BIOS
343                  * may also retry.
344                  */
345                 for (retry = 0; retry < 3; retry++) {
346                         /* If retrying, reset the drive */
347                         if (retry > 0) {
348                                 v86.ctl = V86_FLAGS;
349                                 v86.addr = 0x13;
350                                 v86.eax = 0;
351                                 v86.edx = biosdev;
352                                 v86int();
353                         }
354
355                         packet.len = sizeof(struct edd_packet);
356                         packet.count = x;
357                         packet.off = VTOPOFF(xp);
358                         packet.seg = VTOPSEG(xp);
359                         packet.lba = dblk;
360                         v86.ctl = V86_FLAGS;
361                         v86.addr = 0x13;
362                         v86.eax = 0x4200;
363                         v86.edx = biosdev;
364                         v86.ds = VTOPSEG(&packet);
365                         v86.esi = VTOPOFF(&packet);
366                         v86int();
367                         result = V86_CY(v86.efl);
368                         if (result == 0)
369                                 break;
370                         /* fall back to 1 sector read */
371                         x = 1;
372                 }
373         
374 #ifdef DISK_DEBUG
375                 error = (v86.eax >> 8) & 0xff;
376 #endif
377                 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p,
378                     VTOP(p), result ? "failed" : "ok");
379                 DEBUG("unit %d  status 0x%x", unit, error);
380
381                 /* still an error? break off */
382                 if (result != 0)
383                         break;
384
385                 if (bbuf != NULL)
386                         bcopy(bbuf, p, x * BIOSCD_SECSIZE);
387                 p += (x * BIOSCD_SECSIZE);
388                 dblk += x;
389                 resid -= x;
390         }
391         
392 /*      hexdump(dest, (blks * BIOSCD_SECSIZE)); */
393
394         if (blks - resid == 0)
395                 return (-1);            /* read failed */
396
397         return (blks - resid);
398 }
399
400 /*
401  * Return a suitable dev_t value for (dev).
402  */
403 int
404 bc_getdev(struct i386_devdesc *dev)
405 {
406     int biosdev, unit;
407     int major;
408     int rootdev;
409
410     unit = dev->dd.d_unit;
411     biosdev = bc_unit2bios(unit);
412     DEBUG("unit %d BIOS device %d", unit, biosdev);
413     if (biosdev == -1)                          /* not a BIOS device */
414         return(-1);
415
416     /*
417      * XXX: Need to examine device spec here to figure out if SCSI or
418      * ATAPI.  No idea on how to figure out device number.  All we can
419      * really pass to the kernel is what bus and device on which bus we
420      * were booted from, which dev_t isn't well suited to since those
421      * number don't match to unit numbers very well.  We may just need
422      * to engage in a hack where we pass -C to the boot args if we are
423      * the boot device.
424      */
425     major = ACDMAJOR;
426     unit = 0;   /* XXX */
427
428     /* XXX: Assume partition 'a'. */
429     rootdev = MAKEBOOTDEV(major, 0, unit, 0);
430     DEBUG("dev is 0x%x\n", rootdev);
431     return(rootdev);
432 }