biosdisk.c revision 1.3 1 1.3 thorpej /* $NetBSD: biosdisk.c,v 1.3 1997/03/22 01:41:34 thorpej 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.3 thorpej /*
36 1.3 thorpej * raw BIOS disk device for libsa. needs lowlevel parts from bios_disk.S and
37 1.3 thorpej * biosdisk_ll.c partly from netbsd:sys/arch/i386/boot/disk.c no bad144
38 1.3 thorpej * handling!
39 1.1 perry */
40 1.1 perry
41 1.1 perry /*
42 1.1 perry * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
43 1.1 perry *
44 1.1 perry * Mach Operating System
45 1.1 perry * Copyright (c) 1992, 1991 Carnegie Mellon University
46 1.1 perry * All Rights Reserved.
47 1.3 thorpej *
48 1.1 perry * Permission to use, copy, modify and distribute this software and its
49 1.1 perry * documentation is hereby granted, provided that both the copyright
50 1.1 perry * notice and this permission notice appear in all copies of the
51 1.1 perry * software, derivative works or modified versions, and any portions
52 1.1 perry * thereof, and that both notices appear in supporting documentation.
53 1.3 thorpej *
54 1.1 perry * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 1.1 perry * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
56 1.1 perry * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 1.3 thorpej *
58 1.1 perry * Carnegie Mellon requests users of this software to return to
59 1.3 thorpej *
60 1.1 perry * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 1.1 perry * School of Computer Science
62 1.1 perry * Carnegie Mellon University
63 1.1 perry * Pittsburgh PA 15213-3890
64 1.3 thorpej *
65 1.1 perry * any improvements or extensions that they make and grant Carnegie Mellon
66 1.1 perry * the rights to redistribute these changes.
67 1.1 perry */
68 1.1 perry
69 1.1 perry #include <sys/types.h>
70 1.1 perry #include <sys/disklabel.h>
71 1.1 perry
72 1.1 perry #include <lib/libsa/stand.h>
73 1.1 perry #include <lib/libsa/saerrno.h>
74 1.1 perry
75 1.1 perry #include "libi386.h"
76 1.1 perry #include "biosdisk_ll.h"
77 1.1 perry /* XXX don't include biosdisk.h for now - vararg prototype */
78 1.1 perry
79 1.1 perry #define BUFSIZE (1 * BIOSDISK_SECSIZE)
80 1.1 perry
81 1.3 thorpej struct biosdisk {
82 1.3 thorpej struct biosdisk_ll ll;
83 1.1 perry #ifdef COMPAT_OLDBOOT
84 1.3 thorpej int disktype;
85 1.1 perry #endif
86 1.3 thorpej int boff;
87 1.3 thorpej char buf[BUFSIZE];
88 1.1 perry };
89 1.1 perry
90 1.3 thorpej int
91 1.3 thorpej biosdiskstrategy(devdata, flag, dblk, size, buf, rsize)
92 1.3 thorpej void *devdata;
93 1.3 thorpej int flag;
94 1.3 thorpej daddr_t dblk;
95 1.3 thorpej size_t size;
96 1.3 thorpej void *buf;
97 1.3 thorpej size_t *rsize;
98 1.1 perry {
99 1.3 thorpej struct biosdisk *d;
100 1.3 thorpej int blks, frag;
101 1.1 perry
102 1.3 thorpej if (flag != F_READ)
103 1.3 thorpej return (EROFS);
104 1.1 perry
105 1.3 thorpej d = (struct biosdisk *) devdata;
106 1.1 perry
107 1.3 thorpej dblk += d->boff;
108 1.3 thorpej
109 1.3 thorpej blks = size / BIOSDISK_SECSIZE;
110 1.3 thorpej if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
111 1.3 thorpej if (rsize)
112 1.3 thorpej *rsize = 0;
113 1.3 thorpej return (EIO);
114 1.3 thorpej }
115 1.3 thorpej /* do we really need this? */
116 1.3 thorpej frag = size % BIOSDISK_SECSIZE;
117 1.3 thorpej if (frag) {
118 1.3 thorpej if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
119 1.3 thorpej if (rsize)
120 1.3 thorpej *rsize = blks * BIOSDISK_SECSIZE;
121 1.3 thorpej return (EIO);
122 1.3 thorpej }
123 1.3 thorpej bcopy(d->buf, buf + blks * BIOSDISK_SECSIZE, frag);
124 1.3 thorpej }
125 1.3 thorpej if (rsize)
126 1.3 thorpej *rsize = size;
127 1.3 thorpej return (0);
128 1.1 perry }
129 1.1 perry
130 1.1 perry #ifdef COMPAT_OLDBOOT
131 1.3 thorpej int
132 1.3 thorpej biosdisk_gettype(f)
133 1.3 thorpej struct open_file *f;
134 1.1 perry {
135 1.3 thorpej struct biosdisk *d = f->f_devdata;
136 1.3 thorpej return (d->disktype);
137 1.1 perry }
138 1.1 perry #endif
139 1.1 perry
140 1.3 thorpej int
141 1.3 thorpej biosdiskopen(f, biosdev, partition)
142 1.3 thorpej struct open_file *f;
143 1.3 thorpej int biosdev;
144 1.3 thorpej unsigned int partition;
145 1.1 perry {
146 1.3 thorpej struct biosdisk *d;
147 1.3 thorpej struct dos_partition *dptr;
148 1.3 thorpej int sector;
149 1.3 thorpej int error = 0, i;
150 1.1 perry #ifndef NO_DISKLABEL
151 1.3 thorpej struct disklabel *lp;
152 1.1 perry #endif
153 1.1 perry
154 1.3 thorpej d = (struct biosdisk *) alloc(sizeof(struct biosdisk));
155 1.3 thorpej if (!d) {
156 1.1 perry #ifdef DEBUG
157 1.3 thorpej printf("biosdiskopen: no memory\n");
158 1.1 perry #endif
159 1.3 thorpej return (ENOMEM);
160 1.3 thorpej }
161 1.3 thorpej d->ll.dev = biosdev;
162 1.3 thorpej if (set_geometry(&d->ll)) {
163 1.1 perry #ifdef DISK_DEBUG
164 1.3 thorpej printf("no geometry information\n");
165 1.1 perry #endif
166 1.3 thorpej error = ENXIO;
167 1.3 thorpej goto out;
168 1.3 thorpej }
169 1.3 thorpej /*
170 1.3 thorpej * find NetBSD Partition in DOS partition table XXX check magic???
171 1.3 thorpej */
172 1.3 thorpej if (readsects(&d->ll, 0, 1, d->buf, 0)) {
173 1.1 perry #ifdef DISK_DEBUG
174 1.3 thorpej printf("error reading mbr\n");
175 1.1 perry #endif
176 1.3 thorpej error = EIO;
177 1.3 thorpej goto out;
178 1.3 thorpej }
179 1.3 thorpej dptr = (struct dos_partition *) & d->buf[DOSPARTOFF];
180 1.3 thorpej sector = -1;
181 1.3 thorpej for (i = 0; i < NDOSPART; i++, dptr++)
182 1.3 thorpej if (dptr->dp_typ == DOSPTYP_NETBSD) {
183 1.3 thorpej sector = dptr->dp_start;
184 1.3 thorpej break;
185 1.3 thorpej }
186 1.3 thorpej if (sector == -1) {
187 1.3 thorpej /*
188 1.3 thorpej * One of two things:
189 1.3 thorpej * 1. no MBR
190 1.3 thorpej * 2. no NetBSD partition in MBR
191 1.3 thorpej *
192 1.3 thorpej * We simply default to "start of disk" in this case and
193 1.3 thorpej * press on.
194 1.3 thorpej */
195 1.3 thorpej sector = 0;
196 1.3 thorpej }
197 1.1 perry #ifdef NO_DISKLABEL
198 1.3 thorpej d->boff = sector;
199 1.1 perry #else
200 1.3 thorpej /* find partition in NetBSD disklabel */
201 1.3 thorpej if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
202 1.1 perry #ifdef DISK_DEBUG
203 1.3 thorpej printf("Error reading disklabel\n");
204 1.1 perry #endif
205 1.3 thorpej error = EIO;
206 1.3 thorpej goto out;
207 1.3 thorpej }
208 1.3 thorpej lp = (struct disklabel *) (d->buf + LABELOFFSET);
209 1.3 thorpej if (lp->d_magic != DISKMAGIC) {
210 1.1 perry #ifdef DISK_DEBUG
211 1.3 thorpej printf("warning: no disklabel\n");
212 1.1 perry #endif
213 1.3 thorpej d->boff = sector;
214 1.3 thorpej } else if (partition >= lp->d_npartitions ||
215 1.3 thorpej lp->d_partitions[partition].p_fstype == FS_UNUSED) {
216 1.1 perry #ifdef DISK_DEBUG
217 1.3 thorpej printf("illegal partition\n");
218 1.1 perry #endif
219 1.3 thorpej error = EPART;
220 1.3 thorpej goto out;
221 1.3 thorpej } else {
222 1.3 thorpej d->boff = lp->d_partitions[partition].p_offset;
223 1.1 perry #ifdef COMPAT_OLDBOOT
224 1.3 thorpej d->disktype = lp->d_type;
225 1.1 perry #endif
226 1.3 thorpej }
227 1.3 thorpej #endif /* NO_DISKLABEL */
228 1.1 perry
229 1.1 perry #ifdef DISK_DEBUG
230 1.3 thorpej printf("partition @%d\n", d->boff);
231 1.1 perry #endif
232 1.1 perry
233 1.3 thorpej f->f_devdata = d;
234 1.1 perry out:
235 1.3 thorpej if (error)
236 1.3 thorpej free(d, sizeof(struct biosdisk));
237 1.3 thorpej return (error);
238 1.1 perry }
239 1.1 perry
240 1.3 thorpej int
241 1.3 thorpej biosdiskclose(f)
242 1.3 thorpej struct open_file *f;
243 1.1 perry {
244 1.3 thorpej struct biosdisk *d = f->f_devdata;
245 1.1 perry
246 1.3 thorpej if (!(d->ll.dev & 0x80))/* let the floppy drive go off */
247 1.3 thorpej delay(3000000); /* 2s is enough on all PCs I found */
248 1.1 perry
249 1.3 thorpej free(d, sizeof(struct biosdisk));
250 1.3 thorpej f->f_devdata = NULL;
251 1.3 thorpej return (0);
252 1.1 perry }
253 1.1 perry
254 1.3 thorpej int
255 1.3 thorpej biosdiskioctl(f, cmd, arg)
256 1.3 thorpej struct open_file *f;
257 1.3 thorpej u_long cmd;
258 1.3 thorpej void *arg;
259 1.1 perry {
260 1.3 thorpej return EIO;
261 1.1 perry }
262