biosdisk_ll.c revision 1.1 1 1.1 perry /* $NetBSD: biosdisk_ll.c,v 1.1 1997/03/14 02:40:32 perry Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1996
5 1.1 perry * Matthias Drochner. All rights reserved.
6 1.1 perry * Copyright (c) 1996
7 1.1 perry * Perry E. Metzger. All rights reserved.
8 1.1 perry *
9 1.1 perry * Redistribution and use in source and binary forms, with or without
10 1.1 perry * modification, are permitted provided that the following conditions
11 1.1 perry * are met:
12 1.1 perry * 1. Redistributions of source code must retain the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer.
14 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perry * notice, this list of conditions and the following disclaimer in the
16 1.1 perry * documentation and/or other materials provided with the distribution.
17 1.1 perry * 3. All advertising materials mentioning features or use of this software
18 1.1 perry * must display the following acknowledgements:
19 1.1 perry * This product includes software developed for the NetBSD Project
20 1.1 perry * by Matthias Drochner.
21 1.1 perry * This product includes software developed for the NetBSD Project
22 1.1 perry * by Perry E. Metzger.
23 1.1 perry * 4. The names of the authors may not be used to endorse or promote products
24 1.1 perry * derived from this software without specific prior written permission.
25 1.1 perry *
26 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 1.1 perry *
37 1.1 perry */
38 1.1 perry
39 1.1 perry /* shared by bootsector startup (bootsectmain)
40 1.1 perry and biosdisk.c
41 1.1 perry needs lowlevel parts from bios_disk.S
42 1.1 perry */
43 1.1 perry
44 1.1 perry #include <lib/libsa/stand.h>
45 1.1 perry
46 1.1 perry #include "biosdisk_ll.h"
47 1.1 perry #include "diskbuf.h"
48 1.1 perry
49 1.1 perry extern int get_diskinfo __P((int));
50 1.1 perry extern int biosread __P((int, int, int, int, int, char*));
51 1.1 perry
52 1.1 perry #define SPT(di) ((di)&0xff)
53 1.1 perry #define HEADS(di) ((((di)>>8)&0xff)+1)
54 1.1 perry
55 1.1 perry int set_geometry(d)
56 1.1 perry struct biosdisk_ll *d;
57 1.1 perry {
58 1.1 perry int diskinfo;
59 1.1 perry
60 1.1 perry diskinfo = get_diskinfo(d->dev);
61 1.1 perry
62 1.1 perry d->spc = (d->spt = SPT(diskinfo)) * HEADS(diskinfo);
63 1.1 perry
64 1.1 perry /* get_diskinfo assumes floppy if BIOS call
65 1.1 perry fails. Check at least "valid" geometry. */
66 1.1 perry return(!d->spc || !d->spt);
67 1.1 perry }
68 1.1 perry
69 1.1 perry /* Global shared "diskbuf" is used as read ahead buffer. For
70 1.1 perry * reading from floppies, the bootstrap has to be loaded on a 64K boundary
71 1.1 perry * to ensure that this buffer doesn't cross a 64K DMA boundary.
72 1.1 perry */
73 1.1 perry #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE)
74 1.1 perry static int ra_dev;
75 1.1 perry static int ra_end;
76 1.1 perry static int ra_first;
77 1.1 perry
78 1.1 perry int readsects(d, dblk, num, buf, cold) /* reads ahead if (!cold) */
79 1.1 perry struct biosdisk_ll *d;
80 1.1 perry int dblk, num;
81 1.1 perry char *buf;
82 1.1 perry int cold; /* don't use data segment or bss, don't call library functions */
83 1.1 perry {
84 1.1 perry while(num) {
85 1.1 perry int nsec;
86 1.1 perry
87 1.1 perry /* check for usable data in read-ahead buffer */
88 1.1 perry if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
89 1.1 perry || dblk < ra_first || dblk >= ra_end) {
90 1.1 perry
91 1.1 perry /* no, read from disk */
92 1.1 perry int cyl, head, sec;
93 1.1 perry char *trbuf;
94 1.1 perry
95 1.1 perry cyl = dblk / d->spc;
96 1.1 perry head = (dblk % d->spc) / d->spt;
97 1.1 perry sec = dblk % d->spt;
98 1.1 perry nsec = d->spt - sec;
99 1.1 perry
100 1.1 perry if(cold) {
101 1.1 perry /* transfer directly to buffer */
102 1.1 perry trbuf = buf;
103 1.1 perry if (nsec > num)
104 1.1 perry nsec = num;
105 1.1 perry } else {
106 1.1 perry /* fill read-ahead buffer */
107 1.1 perry trbuf = diskbuf;
108 1.1 perry if (nsec > RA_SECTORS)
109 1.1 perry nsec = RA_SECTORS;
110 1.1 perry
111 1.1 perry ra_dev = d->dev;
112 1.1 perry ra_first = dblk;
113 1.1 perry ra_end = dblk + nsec;
114 1.1 perry diskbuf_user = &ra_dev;
115 1.1 perry }
116 1.1 perry
117 1.1 perry if (biosread(d->dev, cyl, head, sec, nsec, trbuf)) {
118 1.1 perry if(!cold) diskbuf_user = 0; /* mark invalid */
119 1.1 perry return(-1); /* XXX cannot output here if (cold) */
120 1.1 perry }
121 1.1 perry
122 1.1 perry } else /* can take blocks from end of read-ahead buffer */
123 1.1 perry nsec = ra_end - dblk;
124 1.1 perry
125 1.1 perry if(!cold) {
126 1.1 perry /* copy data from read-ahead to user buffer */
127 1.1 perry if(nsec > num) nsec = num;
128 1.1 perry bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE, buf,
129 1.1 perry nsec * BIOSDISK_SECSIZE);
130 1.1 perry }
131 1.1 perry
132 1.1 perry buf += nsec * BIOSDISK_SECSIZE;
133 1.1 perry num -= nsec;
134 1.1 perry dblk += nsec;
135 1.1 perry }
136 1.1 perry
137 1.1 perry return(0);
138 1.1 perry }
139