biosdisk.c revision 1.23 1 /* $NetBSD: biosdisk.c,v 1.23 2005/06/22 05:30:13 junyoung Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 /*
30 * raw BIOS disk device for libsa.
31 * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
32 * partly from netbsd:sys/arch/i386/boot/disk.c
33 * no bad144 handling!
34 *
35 * A lot of this must match sys/kern/subr_disk_mbr.c
36 */
37
38 /*
39 * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
40 *
41 * Mach Operating System
42 * Copyright (c) 1992, 1991 Carnegie Mellon University
43 * All Rights Reserved.
44 *
45 * Permission to use, copy, modify and distribute this software and its
46 * documentation is hereby granted, provided that both the copyright
47 * notice and this permission notice appear in all copies of the
48 * software, derivative works or modified versions, and any portions
49 * thereof, and that both notices appear in supporting documentation.
50 *
51 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
53 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 *
55 * Carnegie Mellon requests users of this software to return to
56 *
57 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
58 * School of Computer Science
59 * Carnegie Mellon University
60 * Pittsburgh PA 15213-3890
61 *
62 * any improvements or extensions that they make and grant Carnegie Mellon
63 * the rights to redistribute these changes.
64 */
65
66 #include <sys/types.h>
67 #include <sys/disklabel.h>
68 #include <sys/md5.h>
69
70 #include <lib/libsa/stand.h>
71 #include <lib/libsa/saerrno.h>
72 #include <machine/stdarg.h>
73
74 #include "libi386.h"
75 #include "biosdisk_ll.h"
76 #include "biosdisk.h"
77 #ifdef _STANDALONE
78 #include "bootinfo.h"
79 #endif
80
81 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
82
83 struct biosdisk {
84 struct biosdisk_ll ll;
85 int boff;
86 char buf[BUFSIZE];
87 };
88
89 #ifdef _STANDALONE
90 static struct btinfo_bootdisk bi_disk;
91 static struct btinfo_bootwedge bi_wedge;
92 #endif
93
94 #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
95
96 int boot_biossector; /* disk sector partition might have started in */
97
98 int
99 biosdiskstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
100 void *buf, size_t *rsize)
101 {
102 struct biosdisk *d;
103 int blks, frag;
104
105 if (flag != F_READ)
106 return EROFS;
107
108 d = (struct biosdisk *) devdata;
109
110 dblk += d->boff;
111
112 blks = size / BIOSDISK_SECSIZE;
113 if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
114 if (rsize)
115 *rsize = 0;
116 return EIO;
117 }
118
119 /* do we really need this? */
120 frag = size % BIOSDISK_SECSIZE;
121 if (frag) {
122 if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
123 if (rsize)
124 *rsize = blks * BIOSDISK_SECSIZE;
125 return EIO;
126 }
127 memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
128 }
129
130 if (rsize)
131 *rsize = size;
132 return 0;
133 }
134
135 static struct biosdisk *
136 alloc_biosdisk(int biosdev)
137 {
138 struct biosdisk *d;
139
140 d = alloc(sizeof(*d));
141 if (d == NULL)
142 return NULL;
143 memset(d, 0, sizeof(*d));
144
145 d->ll.dev = biosdev;
146 if (set_geometry(&d->ll, NULL)) {
147 #ifdef DISK_DEBUG
148 printf("no geometry information\n");
149 #endif
150 free(d, sizeof(*d));
151 return NULL;
152 }
153 return d;
154 }
155
156 #ifndef NO_DISKLABEL
157 static int
158 check_label(struct biosdisk *d, int sector)
159 {
160 struct disklabel *lp;
161
162 /* find partition in NetBSD disklabel */
163 if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
164 #ifdef DISK_DEBUG
165 printf("Error reading disklabel\n");
166 #endif
167 return EIO;
168 }
169 lp = (struct disklabel *) (d->buf + LABELOFFSET);
170 if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
171 #ifdef DISK_DEBUG
172 printf("warning: no disklabel\n");
173 #endif
174 return -1;
175 }
176
177 d->boff = sector;
178 return 0;
179 }
180
181 static int
182 read_label(struct biosdisk *d)
183 {
184 struct disklabel dflt_lbl;
185 struct mbr_partition mbr[MBR_PART_COUNT];
186 struct partition *p;
187 int sector, i;
188 int error;
189 int typ;
190 int ext_base, this_ext, next_ext;
191 #ifdef COMPAT_386BSD_MBRPART
192 int sector_386bsd = -1;
193 #endif
194
195 memset(&dflt_lbl, 0, sizeof(dflt_lbl));
196 dflt_lbl.d_npartitions = 8;
197
198 d->boff = 0;
199
200 if (!(d->ll.dev & 0x80)) /* floppy */
201 /* No label on floppy */
202 return -1;
203
204 /*
205 * find NetBSD Partition in DOS partition table
206 * XXX check magic???
207 */
208 ext_base = 0;
209 next_ext = 0;
210 for (;;) {
211 this_ext = ext_base + next_ext;
212 next_ext = 0;
213 if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
214 #ifdef DISK_DEBUG
215 printf("error reading MBR sector %d\n", this_ext);
216 #endif
217 return EIO;
218 }
219 memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts,
220 sizeof(mbr));
221 /* Look for NetBSD partition ID */
222 for (i = 0; i < MBR_PART_COUNT; i++) {
223 typ = mbr[i].mbrp_type;
224 if (typ == 0)
225 continue;
226 sector = this_ext + mbr[i].mbrp_start;
227 if (typ == MBR_PTYPE_NETBSD) {
228 error = check_label(d, sector);
229 if (error >= 0)
230 return error;
231 }
232 if (MBR_IS_EXTENDED(typ)) {
233 next_ext = mbr[i].mbrp_start;
234 continue;
235 }
236 #ifdef COMPAT_386BSD_MBRPART
237 if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
238 sector_386bsd = sector;
239 #endif
240 if (this_ext != 0) {
241 if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
242 continue;
243 p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
244 } else
245 p = &dflt_lbl.d_partitions[i];
246 p->p_offset = sector;
247 p->p_size = mbr[i].mbrp_size;
248 p->p_fstype = xlat_mbr_fstype(typ);
249 }
250 if (next_ext == 0)
251 break;
252 if (ext_base == 0) {
253 ext_base = next_ext;
254 next_ext = 0;
255 }
256 }
257
258 sector = 0;
259 #ifdef COMPAT_386BSD_MBRPART
260 if (sector_386bsd != -1) {
261 printf("old BSD partition ID!\n");
262 sector = sector_386bsd;
263 }
264 #endif
265
266 /*
267 * One of two things:
268 * 1. no MBR
269 * 2. no NetBSD partition in MBR
270 *
271 * We simply default to "start of disk" in this case and
272 * press on.
273 */
274 error = check_label(d, sector);
275 if (error >= 0)
276 return error;
277
278 /*
279 * Nothing at start of disk, return info from mbr partitions.
280 */
281 /* XXX fill it to make checksum match kernel one */
282 dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
283 memcpy(d->buf, &dflt_lbl, sizeof(dflt_lbl));
284 return -1;
285 }
286 #endif /* NO_DISKLABEL */
287
288 /* Determine likely partition for possible sector number of dos
289 * partition.
290 */
291
292 u_int
293 biosdiskfindptn(int biosdev, u_int sector)
294 {
295 #ifdef NO_DISKLABEL
296 return 0;
297 #else
298 struct biosdisk *d;
299 u_int partition = 0;
300 struct disklabel *lp;
301
302 /* Look for netbsd partition that is the dos boot one */
303 d = alloc_biosdisk(biosdev);
304 if (d == NULL)
305 return 0;
306
307 if (read_label(d) == 0) {
308 lp = (struct disklabel *)(d->buf + LABELOFFSET);
309 for (partition = lp->d_npartitions; --partition;){
310 if (lp->d_partitions[partition].p_fstype == FS_UNUSED)
311 continue;
312 if (lp->d_partitions[partition].p_offset == sector)
313 break;
314 }
315 }
316
317 free(d, sizeof(*d));
318 return partition;
319 #endif /* NO_DISKLABEL */
320 }
321
322 int
323 biosdiskopen(struct open_file *f, ...)
324 /* struct open_file *f, int biosdev, int partition */
325 {
326 va_list ap;
327 struct biosdisk *d;
328 int biosdev;
329 int partition;
330 #ifndef NO_DISKLABEL
331 struct disklabel *lp;
332 #endif
333 int error = 0;
334
335 va_start(ap, f);
336 biosdev = va_arg(ap, int);
337 d = alloc_biosdisk(biosdev);
338 if (d == NULL) {
339 error = ENXIO;
340 goto out;
341 }
342
343 partition = va_arg(ap, int);
344 #ifdef _STANDALONE
345 bi_disk.biosdev = d->ll.dev;
346 bi_disk.partition = partition;
347 bi_disk.labelsector = -1;
348
349 bi_wedge.biosdev = d->ll.dev;
350 bi_wedge.matchblk = -1;
351 #endif
352
353 #ifndef NO_DISKLABEL
354 if (partition == RAW_PART)
355 goto nolabel;
356 error = read_label(d);
357 if (error == -1) {
358 error = 0;
359 goto nolabel;
360 }
361 if (error)
362 goto out;
363
364 lp = (struct disklabel *) (d->buf + LABELOFFSET);
365 if (partition >= lp->d_npartitions ||
366 lp->d_partitions[partition].p_fstype == FS_UNUSED) {
367 #ifdef DISK_DEBUG
368 printf("illegal partition\n");
369 #endif
370 error = EPART;
371 goto out;
372 }
373 #ifdef _STANDALONE
374 bi_disk.labelsector = d->boff + LABELSECTOR;
375 bi_disk.label.type = lp->d_type;
376 memcpy(bi_disk.label.packname, lp->d_packname, 16);
377 bi_disk.label.checksum = lp->d_checksum;
378
379 bi_wedge.startblk = lp->d_partitions[partition].p_offset;
380 bi_wedge.nblks = lp->d_partitions[partition].p_size;
381 bi_wedge.matchblk = d->boff + LABELSECTOR;
382 bi_wedge.matchnblks = 1;
383 {
384 MD5_CTX ctx;
385
386 MD5Init(&ctx);
387 MD5Update(&ctx, (void *) d->buf, 512);
388 MD5Final(bi_wedge.matchhash, &ctx);
389 }
390 #endif
391 d->boff = lp->d_partitions[partition].p_offset;
392 if (lp->d_partitions[partition].p_fstype == FS_RAID)
393 d->boff += RF_PROTECTED_SECTORS;
394 nolabel:
395 #endif /* NO_DISKLABEL */
396
397 #ifdef DISK_DEBUG
398 printf("partition @%d\n", d->boff);
399 #endif
400
401 #ifdef _STANDALONE
402 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
403 BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
404 #endif
405
406 f->f_devdata = d;
407 out:
408 va_end(ap);
409 if (error)
410 free(d, sizeof(struct biosdisk));
411 return error;
412 }
413
414 #ifndef LIBSA_NO_FS_CLOSE
415 int
416 biosdiskclose(struct open_file *f)
417 {
418 struct biosdisk *d = f->f_devdata;
419
420 if (!(d->ll.dev & 0x80))/* let the floppy drive go off */
421 delay(3000000); /* 2s is enough on all PCs I found */
422
423 free(d, sizeof(struct biosdisk));
424 f->f_devdata = NULL;
425 return 0;
426 }
427 #endif
428
429 int
430 biosdiskioctl(struct open_file *f, u_long cmd, void *arg)
431 {
432 return EIO;
433 }
434