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