biosdisk.c revision 1.1 1 1.1 perry /* $NetBSD: biosdisk.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 *
7 1.1 perry * Redistribution and use in source and binary forms, with or without
8 1.1 perry * modification, are permitted provided that the following conditions
9 1.1 perry * are met:
10 1.1 perry * 1. Redistributions of source code must retain the above copyright
11 1.1 perry * notice, this list of conditions and the following disclaimer.
12 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer in the
14 1.1 perry * documentation and/or other materials provided with the distribution.
15 1.1 perry * 3. All advertising materials mentioning features or use of this software
16 1.1 perry * must display the following acknowledgement:
17 1.1 perry * This product includes software developed for the NetBSD Project
18 1.1 perry * by Matthias Drochner.
19 1.1 perry * 4. The name of the author may not be used to endorse or promote products
20 1.1 perry * derived from this software without specific prior written permission.
21 1.1 perry *
22 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 perry *
33 1.1 perry */
34 1.1 perry
35 1.1 perry /* raw BIOS disk device for libsa.
36 1.1 perry needs lowlevel parts from bios_disk.S and biosdisk_ll.c
37 1.1 perry partly from netbsd:sys/arch/i386/boot/disk.c
38 1.1 perry no bad144 handling!
39 1.1 perry
40 1.1 perry */
41 1.1 perry
42 1.1 perry /*
43 1.1 perry * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
44 1.1 perry *
45 1.1 perry * Mach Operating System
46 1.1 perry * Copyright (c) 1992, 1991 Carnegie Mellon University
47 1.1 perry * All Rights Reserved.
48 1.1 perry *
49 1.1 perry * Permission to use, copy, modify and distribute this software and its
50 1.1 perry * documentation is hereby granted, provided that both the copyright
51 1.1 perry * notice and this permission notice appear in all copies of the
52 1.1 perry * software, derivative works or modified versions, and any portions
53 1.1 perry * thereof, and that both notices appear in supporting documentation.
54 1.1 perry *
55 1.1 perry * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 1.1 perry * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
57 1.1 perry * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 1.1 perry *
59 1.1 perry * Carnegie Mellon requests users of this software to return to
60 1.1 perry *
61 1.1 perry * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
62 1.1 perry * School of Computer Science
63 1.1 perry * Carnegie Mellon University
64 1.1 perry * Pittsburgh PA 15213-3890
65 1.1 perry *
66 1.1 perry * any improvements or extensions that they make and grant Carnegie Mellon
67 1.1 perry * the rights to redistribute these changes.
68 1.1 perry */
69 1.1 perry
70 1.1 perry #include <sys/types.h>
71 1.1 perry #include <sys/disklabel.h>
72 1.1 perry
73 1.1 perry #include <lib/libsa/stand.h>
74 1.1 perry #include <lib/libsa/saerrno.h>
75 1.1 perry
76 1.1 perry #include "libi386.h"
77 1.1 perry #include "biosdisk_ll.h"
78 1.1 perry /* XXX don't include biosdisk.h for now - vararg prototype */
79 1.1 perry
80 1.1 perry #define BUFSIZE (1 * BIOSDISK_SECSIZE)
81 1.1 perry
82 1.1 perry struct biosdisk {
83 1.1 perry struct biosdisk_ll ll;
84 1.1 perry #ifdef COMPAT_OLDBOOT
85 1.1 perry int disktype;
86 1.1 perry #endif
87 1.1 perry int boff;
88 1.1 perry char buf[BUFSIZE];
89 1.1 perry };
90 1.1 perry
91 1.1 perry int biosdiskstrategy(devdata, flag, dblk, size, buf, rsize)
92 1.1 perry void *devdata;
93 1.1 perry int flag;
94 1.1 perry daddr_t dblk;
95 1.1 perry size_t size;
96 1.1 perry void *buf;
97 1.1 perry size_t *rsize;
98 1.1 perry {
99 1.1 perry struct biosdisk *d;
100 1.1 perry int blks, frag;
101 1.1 perry
102 1.1 perry if(flag != F_READ) return(EROFS);
103 1.1 perry
104 1.1 perry d = (struct biosdisk*)devdata;
105 1.1 perry
106 1.1 perry dblk += d->boff;
107 1.1 perry
108 1.1 perry blks = size / BIOSDISK_SECSIZE;
109 1.1 perry if(blks && readsects(&d->ll, dblk, blks, buf, 0)){
110 1.1 perry if(rsize) *rsize = 0;
111 1.1 perry return(EIO);
112 1.1 perry }
113 1.1 perry
114 1.1 perry /* do we really need this? */
115 1.1 perry frag = size % BIOSDISK_SECSIZE;
116 1.1 perry if(frag) {
117 1.1 perry if(readsects(&d->ll, dblk + blks, 1, d->buf, 0)){
118 1.1 perry if(rsize) *rsize = blks * BIOSDISK_SECSIZE;
119 1.1 perry return(EIO);
120 1.1 perry }
121 1.1 perry bcopy(d->buf, buf + blks * BIOSDISK_SECSIZE, frag);
122 1.1 perry }
123 1.1 perry
124 1.1 perry if(rsize) *rsize = size;
125 1.1 perry return(0);
126 1.1 perry }
127 1.1 perry
128 1.1 perry #ifdef COMPAT_OLDBOOT
129 1.1 perry int biosdisk_gettype(f)
130 1.1 perry struct open_file *f;
131 1.1 perry {
132 1.1 perry struct biosdisk *d = f->f_devdata;
133 1.1 perry return(d->disktype);
134 1.1 perry }
135 1.1 perry #endif
136 1.1 perry
137 1.1 perry int biosdiskopen(f, biosdev, partition)
138 1.1 perry struct open_file *f;
139 1.1 perry int biosdev;
140 1.1 perry unsigned int partition;
141 1.1 perry {
142 1.1 perry struct biosdisk *d;
143 1.1 perry struct dos_partition *dptr;
144 1.1 perry int sector;
145 1.1 perry int error = 0, i;
146 1.1 perry #ifndef NO_DISKLABEL
147 1.1 perry struct disklabel *lp;
148 1.1 perry #endif
149 1.1 perry
150 1.1 perry d = (struct biosdisk*)alloc(sizeof(struct biosdisk));
151 1.1 perry if(!d) {
152 1.1 perry #ifdef DEBUG
153 1.1 perry printf("biosdiskopen: no memory\n");
154 1.1 perry #endif
155 1.1 perry return(ENOMEM);
156 1.1 perry }
157 1.1 perry
158 1.1 perry d->ll.dev = biosdev;
159 1.1 perry if(set_geometry(&d->ll)) {
160 1.1 perry #ifdef DISK_DEBUG
161 1.1 perry printf("no geometry information\n");
162 1.1 perry #endif
163 1.1 perry error = ENXIO;
164 1.1 perry goto out;
165 1.1 perry }
166 1.1 perry
167 1.1 perry /* find NetBSD Partition in DOS partition table
168 1.1 perry XXX check magic??? */
169 1.1 perry if(readsects(&d->ll, 0, 1, d->buf, 0)){
170 1.1 perry #ifdef DISK_DEBUG
171 1.1 perry printf("error reading mbr\n");
172 1.1 perry #endif
173 1.1 perry error = EIO;
174 1.1 perry goto out;
175 1.1 perry }
176 1.1 perry dptr = (struct dos_partition *)&d->buf[DOSPARTOFF];
177 1.1 perry sector = -1;
178 1.1 perry for (i = 0; i < NDOSPART; i++, dptr++)
179 1.1 perry if (dptr->dp_typ == DOSPTYP_NETBSD) {
180 1.1 perry sector = dptr->dp_start;
181 1.1 perry break;
182 1.1 perry }
183 1.1 perry if(sector == -1){
184 1.1 perry #ifdef DISK_DEBUG
185 1.1 perry printf("no BSD partition\n");
186 1.1 perry #endif
187 1.1 perry error = EUNLAB; /* ??? */
188 1.1 perry goto out;
189 1.1 perry }
190 1.1 perry
191 1.1 perry #ifdef NO_DISKLABEL
192 1.1 perry d->boff = sector;
193 1.1 perry #else
194 1.1 perry /* find partition in NetBSD disklabel */
195 1.1 perry if(readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)){
196 1.1 perry #ifdef DISK_DEBUG
197 1.1 perry printf("Error reading disklabel\n");
198 1.1 perry #endif
199 1.1 perry error = EIO;
200 1.1 perry goto out;
201 1.1 perry }
202 1.1 perry lp = (struct disklabel *)(d->buf + LABELOFFSET);
203 1.1 perry if(lp->d_magic != DISKMAGIC) {
204 1.1 perry #ifdef DISK_DEBUG
205 1.1 perry printf("warning: no disklabel\n");
206 1.1 perry #endif
207 1.1 perry d->boff = sector;
208 1.1 perry } else if(partition >= lp->d_npartitions ||
209 1.1 perry lp->d_partitions[partition].p_fstype == FS_UNUSED) {
210 1.1 perry #ifdef DISK_DEBUG
211 1.1 perry printf("illegal partition\n");
212 1.1 perry #endif
213 1.1 perry error = EPART;
214 1.1 perry goto out;
215 1.1 perry } else {
216 1.1 perry d->boff = lp->d_partitions[partition].p_offset;
217 1.1 perry #ifdef COMPAT_OLDBOOT
218 1.1 perry d->disktype = lp->d_type;
219 1.1 perry #endif
220 1.1 perry }
221 1.1 perry #endif /* NO_DISKLABEL */
222 1.1 perry
223 1.1 perry #ifdef DISK_DEBUG
224 1.1 perry printf("partition @%d\n", d->boff);
225 1.1 perry #endif
226 1.1 perry
227 1.1 perry f->f_devdata = d;
228 1.1 perry out:
229 1.1 perry if(error)
230 1.1 perry free(d, sizeof(struct biosdisk));
231 1.1 perry return(error);
232 1.1 perry }
233 1.1 perry
234 1.1 perry int biosdiskclose(f)
235 1.1 perry struct open_file *f;
236 1.1 perry {
237 1.1 perry struct biosdisk *d = f->f_devdata;
238 1.1 perry
239 1.1 perry if(!(d->ll.dev & 0x80)) /* let the floppy drive go off */
240 1.1 perry delay(3000000); /* 2s is enough on all PCs I found */
241 1.1 perry
242 1.1 perry free(d, sizeof(struct biosdisk));
243 1.1 perry f->f_devdata = NULL;
244 1.1 perry return(0);
245 1.1 perry }
246 1.1 perry
247 1.1 perry int biosdiskioctl(f, cmd, arg)
248 1.1 perry struct open_file *f;
249 1.1 perry u_long cmd;
250 1.1 perry void *arg;
251 1.1 perry {
252 1.1 perry return EIO;
253 1.1 perry }
254 1.1 perry
255 1.1 perry
256