biosdisk.c revision 1.16 1 1.16 dsl /* $NetBSD: biosdisk.c,v 1.16 2003/02/01 14:48:17 dsl Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.8 drochner * Copyright (c) 1996, 1998
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.4 drochner * raw BIOS disk device for libsa.
37 1.4 drochner * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
38 1.4 drochner * partly from netbsd:sys/arch/i386/boot/disk.c
39 1.4 drochner * no bad144 handling!
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.3 thorpej *
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.3 thorpej *
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.3 thorpej *
59 1.1 perry * Carnegie Mellon requests users of this software to return to
60 1.3 thorpej *
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.3 thorpej *
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.5 drochner #include <machine/stdarg.h>
76 1.1 perry
77 1.1 perry #include "libi386.h"
78 1.1 perry #include "biosdisk_ll.h"
79 1.5 drochner #include "biosdisk.h"
80 1.9 drochner #ifdef _STANDALONE
81 1.5 drochner #include "bootinfo.h"
82 1.9 drochner #endif
83 1.1 perry
84 1.16 dsl extern void memset(void *, int, size_t);
85 1.16 dsl
86 1.1 perry #define BUFSIZE (1 * BIOSDISK_SECSIZE)
87 1.1 perry
88 1.3 thorpej struct biosdisk {
89 1.3 thorpej struct biosdisk_ll ll;
90 1.3 thorpej int boff;
91 1.3 thorpej char buf[BUFSIZE];
92 1.1 perry };
93 1.1 perry
94 1.9 drochner #ifdef _STANDALONE
95 1.5 drochner static struct btinfo_bootdisk bi_disk;
96 1.9 drochner #endif
97 1.5 drochner
98 1.13 lukem #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
99 1.13 lukem
100 1.16 dsl int boot_biossector; /* disk sector partition might have started in */
101 1.16 dsl
102 1.3 thorpej int
103 1.3 thorpej biosdiskstrategy(devdata, flag, dblk, size, buf, rsize)
104 1.3 thorpej void *devdata;
105 1.3 thorpej int flag;
106 1.3 thorpej daddr_t dblk;
107 1.3 thorpej size_t size;
108 1.3 thorpej void *buf;
109 1.3 thorpej size_t *rsize;
110 1.1 perry {
111 1.3 thorpej struct biosdisk *d;
112 1.3 thorpej int blks, frag;
113 1.1 perry
114 1.3 thorpej if (flag != F_READ)
115 1.3 thorpej return (EROFS);
116 1.1 perry
117 1.3 thorpej d = (struct biosdisk *) devdata;
118 1.1 perry
119 1.3 thorpej dblk += d->boff;
120 1.3 thorpej
121 1.3 thorpej blks = size / BIOSDISK_SECSIZE;
122 1.3 thorpej if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
123 1.3 thorpej if (rsize)
124 1.3 thorpej *rsize = 0;
125 1.3 thorpej return (EIO);
126 1.3 thorpej }
127 1.3 thorpej /* do we really need this? */
128 1.3 thorpej frag = size % BIOSDISK_SECSIZE;
129 1.3 thorpej if (frag) {
130 1.3 thorpej if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
131 1.3 thorpej if (rsize)
132 1.3 thorpej *rsize = blks * BIOSDISK_SECSIZE;
133 1.3 thorpej return (EIO);
134 1.3 thorpej }
135 1.15 perry memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
136 1.3 thorpej }
137 1.3 thorpej if (rsize)
138 1.3 thorpej *rsize = size;
139 1.3 thorpej return (0);
140 1.1 perry }
141 1.1 perry
142 1.16 dsl static struct biosdisk *
143 1.16 dsl alloc_biosdisk(int dev)
144 1.1 perry {
145 1.3 thorpej struct biosdisk *d;
146 1.1 perry
147 1.16 dsl d = (struct biosdisk *)alloc(sizeof *d);
148 1.16 dsl if (!d)
149 1.16 dsl return NULL;
150 1.16 dsl memset(d, 0, sizeof *d);
151 1.16 dsl
152 1.16 dsl d->ll.dev = dev;;
153 1.11 fvdl if (set_geometry(&d->ll, NULL)) {
154 1.1 perry #ifdef DISK_DEBUG
155 1.3 thorpej printf("no geometry information\n");
156 1.1 perry #endif
157 1.16 dsl free(d, sizeof *d);
158 1.16 dsl return NULL;
159 1.3 thorpej }
160 1.16 dsl return d;
161 1.16 dsl }
162 1.16 dsl
163 1.16 dsl #ifndef NO_DISKLABEL
164 1.16 dsl static int
165 1.16 dsl read_label(struct biosdisk *d)
166 1.16 dsl {
167 1.16 dsl struct mbr_partition *dptr;
168 1.16 dsl int sector, i;
169 1.16 dsl struct disklabel *lp;
170 1.5 drochner
171 1.7 drochner d->boff = 0;
172 1.7 drochner
173 1.16 dsl if (!(d->ll.dev & 0x80)) /* floppy */
174 1.16 dsl /* No label on floppy */
175 1.16 dsl return -1;
176 1.7 drochner
177 1.3 thorpej /*
178 1.7 drochner * find NetBSD Partition in DOS partition table
179 1.7 drochner * XXX check magic???
180 1.3 thorpej */
181 1.3 thorpej if (readsects(&d->ll, 0, 1, d->buf, 0)) {
182 1.1 perry #ifdef DISK_DEBUG
183 1.7 drochner printf("error reading MBR\n");
184 1.1 perry #endif
185 1.16 dsl return EIO;
186 1.3 thorpej }
187 1.8 drochner sector = -1;
188 1.10 thorpej dptr = (struct mbr_partition *) & d->buf[MBR_PARTOFF];
189 1.8 drochner /* Look for NetBSD partition ID */
190 1.10 thorpej for (i = 0; i < NMBRPART; i++, dptr++)
191 1.10 thorpej if (dptr->mbrp_typ == MBR_PTYPE_NETBSD) {
192 1.10 thorpej sector = dptr->mbrp_start;
193 1.3 thorpej break;
194 1.3 thorpej }
195 1.8 drochner #ifdef COMPAT_386BSD_MBRPART
196 1.8 drochner if (sector == -1) {
197 1.8 drochner /* If we didn't find one, look for 386BSD partition ID */
198 1.10 thorpej dptr = (struct mbr_partition *) & d->buf[MBR_PARTOFF];
199 1.10 thorpej for (i = 0; i < NMBRPART; i++, dptr++)
200 1.10 thorpej if (dptr->mbrp_typ == MBR_PTYPE_386BSD) {
201 1.8 drochner printf("old BSD partition ID!\n");
202 1.10 thorpej sector = dptr->mbrp_start;
203 1.8 drochner break;
204 1.8 drochner }
205 1.8 drochner }
206 1.8 drochner #endif
207 1.3 thorpej if (sector == -1) {
208 1.3 thorpej /*
209 1.3 thorpej * One of two things:
210 1.3 thorpej * 1. no MBR
211 1.3 thorpej * 2. no NetBSD partition in MBR
212 1.3 thorpej *
213 1.3 thorpej * We simply default to "start of disk" in this case and
214 1.3 thorpej * press on.
215 1.3 thorpej */
216 1.3 thorpej sector = 0;
217 1.3 thorpej }
218 1.7 drochner
219 1.16 dsl d->boff = sector;
220 1.16 dsl
221 1.3 thorpej /* find partition in NetBSD disklabel */
222 1.3 thorpej if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
223 1.1 perry #ifdef DISK_DEBUG
224 1.3 thorpej printf("Error reading disklabel\n");
225 1.1 perry #endif
226 1.16 dsl return EIO;
227 1.3 thorpej }
228 1.3 thorpej lp = (struct disklabel *) (d->buf + LABELOFFSET);
229 1.3 thorpej if (lp->d_magic != DISKMAGIC) {
230 1.1 perry #ifdef DISK_DEBUG
231 1.3 thorpej printf("warning: no disklabel\n");
232 1.1 perry #endif
233 1.16 dsl return -1;
234 1.16 dsl }
235 1.16 dsl return 0;
236 1.16 dsl }
237 1.16 dsl #endif /* NO_DISKLABEL */
238 1.16 dsl
239 1.16 dsl /* Determine likely partition for possible sector number of dos
240 1.16 dsl partition. */
241 1.16 dsl
242 1.16 dsl u_int
243 1.16 dsl biosdiskfindptn(int biosdev, u_int sector)
244 1.16 dsl {
245 1.16 dsl #ifdef NO_DISKLABEL
246 1.16 dsl return 0;
247 1.16 dsl #else
248 1.16 dsl struct biosdisk *d;
249 1.16 dsl u_int partition = 0;
250 1.16 dsl struct disklabel *lp;
251 1.16 dsl
252 1.16 dsl /* Look for netbsd partition that is the dos boot one */
253 1.16 dsl d = alloc_biosdisk(biosdev);
254 1.16 dsl if (d != NULL && read_label(d) == 0) {
255 1.16 dsl lp = (struct disklabel *)(d->buf + LABELOFFSET);
256 1.16 dsl for (partition = lp->d_npartitions; --partition;){
257 1.16 dsl if (lp->d_partitions[partition].p_fstype == FS_UNUSED)
258 1.16 dsl continue;
259 1.16 dsl if (lp->d_partitions[partition].p_offset == sector)
260 1.16 dsl break;
261 1.16 dsl }
262 1.16 dsl }
263 1.16 dsl
264 1.16 dsl if (d)
265 1.16 dsl free(d, sizeof *d);
266 1.16 dsl return partition;
267 1.16 dsl }
268 1.16 dsl #endif /* NO_DISKLABEL */
269 1.16 dsl
270 1.16 dsl int
271 1.16 dsl biosdiskopen(struct open_file *f, ...)
272 1.16 dsl /* file, biosdev, partition */
273 1.16 dsl {
274 1.16 dsl va_list ap;
275 1.16 dsl struct biosdisk *d;
276 1.16 dsl int partition;
277 1.16 dsl #ifndef NO_DISKLABEL
278 1.16 dsl struct disklabel *lp;
279 1.16 dsl #endif
280 1.16 dsl int error = 0;
281 1.16 dsl
282 1.16 dsl va_start(ap, f);
283 1.16 dsl d = alloc_biosdisk(va_arg(ap, int));
284 1.16 dsl if (!d) {
285 1.16 dsl error = ENXIO;
286 1.16 dsl goto out;
287 1.16 dsl }
288 1.16 dsl
289 1.16 dsl partition = va_arg(ap, int);
290 1.16 dsl #ifdef _STANDALONE
291 1.16 dsl bi_disk.biosdev = d->ll.dev;
292 1.16 dsl bi_disk.partition = partition;
293 1.16 dsl bi_disk.labelsector = -1;
294 1.16 dsl #endif
295 1.16 dsl
296 1.16 dsl #ifndef NO_DISKLABEL
297 1.16 dsl if (partition == RAW_PART)
298 1.16 dsl goto nolabel;
299 1.16 dsl error = read_label(d);
300 1.16 dsl if (error == -1) {
301 1.16 dsl error = 0;
302 1.16 dsl goto nolabel;
303 1.16 dsl }
304 1.16 dsl if (error)
305 1.16 dsl goto out;
306 1.16 dsl
307 1.16 dsl lp = (struct disklabel *) (d->buf + LABELOFFSET);
308 1.16 dsl if (partition >= lp->d_npartitions ||
309 1.16 dsl lp->d_partitions[partition].p_fstype == FS_UNUSED) {
310 1.1 perry #ifdef DISK_DEBUG
311 1.3 thorpej printf("illegal partition\n");
312 1.1 perry #endif
313 1.3 thorpej error = EPART;
314 1.3 thorpej goto out;
315 1.16 dsl }
316 1.9 drochner #ifdef _STANDALONE
317 1.16 dsl bi_disk.labelsector = d->boff + LABELSECTOR;
318 1.16 dsl bi_disk.label.type = lp->d_type;
319 1.16 dsl memcpy(bi_disk.label.packname, lp->d_packname, 16);
320 1.16 dsl bi_disk.label.checksum = lp->d_checksum;
321 1.16 dsl #endif
322 1.16 dsl d->boff = lp->d_partitions[partition].p_offset;
323 1.16 dsl if (lp->d_partitions[partition].p_fstype == FS_RAID)
324 1.16 dsl d->boff += RF_PROTECTED_SECTORS;
325 1.7 drochner nolabel:
326 1.7 drochner #endif /* NO_DISKLABEL */
327 1.1 perry
328 1.1 perry #ifdef DISK_DEBUG
329 1.3 thorpej printf("partition @%d\n", d->boff);
330 1.1 perry #endif
331 1.1 perry
332 1.9 drochner #ifdef _STANDALONE
333 1.5 drochner BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
334 1.9 drochner #endif
335 1.5 drochner
336 1.3 thorpej f->f_devdata = d;
337 1.1 perry out:
338 1.5 drochner va_end(ap);
339 1.3 thorpej if (error)
340 1.3 thorpej free(d, sizeof(struct biosdisk));
341 1.3 thorpej return (error);
342 1.1 perry }
343 1.1 perry
344 1.12 drochner #ifndef LIBSA_NO_FS_CLOSE
345 1.3 thorpej int
346 1.3 thorpej biosdiskclose(f)
347 1.3 thorpej struct open_file *f;
348 1.1 perry {
349 1.3 thorpej struct biosdisk *d = f->f_devdata;
350 1.1 perry
351 1.3 thorpej if (!(d->ll.dev & 0x80))/* let the floppy drive go off */
352 1.3 thorpej delay(3000000); /* 2s is enough on all PCs I found */
353 1.1 perry
354 1.3 thorpej free(d, sizeof(struct biosdisk));
355 1.3 thorpej f->f_devdata = NULL;
356 1.3 thorpej return (0);
357 1.1 perry }
358 1.12 drochner #endif
359 1.1 perry
360 1.3 thorpej int
361 1.3 thorpej biosdiskioctl(f, cmd, arg)
362 1.3 thorpej struct open_file *f;
363 1.3 thorpej u_long cmd;
364 1.3 thorpej void *arg;
365 1.1 perry {
366 1.3 thorpej return EIO;
367 1.1 perry }
368