biosdisk.c revision 1.3 1 /* $NetBSD: biosdisk.c,v 1.3 2011/07/17 20:54:43 joerg 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
69 #include <lib/libsa/stand.h>
70 #include <lib/libsa/saerrno.h>
71 #include <lib/libsa/loadfile.h>
72
73 #include <machine/disklabel.h>
74
75 #include "biosdisk.h"
76 #include "biosdisk_ll.h"
77
78 #include "bootinfo.h"
79
80 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
81
82 struct biosdisk {
83 int dev;
84 int boff;
85 char buf[BUFSIZE];
86 };
87
88 static struct btinfo_bootdisk bi_disk;
89
90 #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
91
92 static struct biosdisk *
93 alloc_biosdisk(int dev)
94 {
95 struct biosdisk *d;
96
97 d = (struct biosdisk *)ALLOC(sizeof(*d));
98 if (d == NULL)
99 return (NULL);
100
101 memset(d, 0, sizeof(*d));
102 d->dev = dev;
103
104 return (d);
105 }
106
107 static int
108 check_label(struct biosdisk *d, int sector)
109 {
110 struct disklabel *lp;
111
112 /* find partition in NetBSD disklabel */
113 if (readsects(d->dev, sector + LABELSECTOR, d->buf, 1)) {
114 #ifdef DISK_DEBUG
115 printf("Error reading disklabel\n");
116 #endif
117 return (EIO);
118 }
119
120 lp = (struct disklabel *)(d->buf + LABELOFFSET);
121 if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
122 #ifdef DISK_DEBUG
123 printf("warning: no disklabel\n");
124 #endif
125 return (-1);
126 }
127
128 d->boff = sector;
129 return (0);
130 }
131
132 static int
133 read_label(struct biosdisk *d)
134 {
135 struct mbr_sector *mbr_sect;
136 struct disklabel dflt_lbl;
137 struct mbr_partition mbr[MBR_PART_COUNT];
138 struct partition *p;
139 int sector, i;
140 int error;
141 int typ;
142 int ext_base, this_ext, next_ext;
143 #ifdef COMPAT_386BSD_MBRPART
144 int sector_386bsd = -1;
145 #endif
146
147 memset(&dflt_lbl, 0, sizeof dflt_lbl);
148 dflt_lbl.d_npartitions = 8;
149
150 d->boff = 0;
151
152 /*
153 * find NetBSD Partition in DOS partition table
154 * XXX check magic???
155 */
156 ext_base = 0;
157 next_ext = 0;
158 for (;;) {
159 this_ext = ext_base + next_ext;
160 next_ext = 0;
161 mbr_sect = (struct mbr_sector *)d->buf;
162 memcpy(&mbr, mbr_sect->mbr_parts, sizeof(mbr));
163 if (readsects(d->dev, this_ext, d->buf, 1)) {
164 #ifdef DISK_DEBUG
165 printf("error reading MBR sector %d\n", this_ext);
166 #endif
167 return (EIO);
168 }
169 memcpy(&mbr, mbr_sect->mbr_parts, sizeof(mbr));
170 /* Look for NetBSD partition ID */
171 for (i = 0; i < MBR_PART_COUNT; i++) {
172 typ = mbr[i].mbrp_type;
173 if (typ == 0)
174 continue;
175 sector = this_ext + mbr[i].mbrp_start;
176 if (typ == MBR_PTYPE_NETBSD) {
177 error = check_label(d, sector);
178 if (error >= 0)
179 return error;
180 }
181 if (MBR_IS_EXTENDED(typ)) {
182 next_ext = mbr[i].mbrp_start;
183 continue;
184 }
185 #ifdef COMPAT_386BSD_MBRPART
186 if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
187 sector_386bsd = sector;
188 #endif
189 if (this_ext != 0) {
190 if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
191 continue;
192 p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
193 } else
194 p = &dflt_lbl.d_partitions[i];
195 p->p_offset = sector;
196 p->p_size = mbr[i].mbrp_size;
197 p->p_fstype = xlat_mbr_fstype(typ);
198 }
199 if (next_ext == 0)
200 break;
201 if (ext_base == 0) {
202 ext_base = next_ext;
203 next_ext = 0;
204 }
205 }
206
207 sector = 0;
208 #ifdef COMPAT_386BSD_MBRPART
209 if (sector_386bsd != -1) {
210 printf("old BSD partition ID!\n");
211 sector = sector_386bsd;
212 }
213 #endif
214
215 /*
216 * One of two things:
217 * 1. no MBR
218 * 2. no NetBSD partition in MBR
219 *
220 * We simply default to "start of disk" in this case and
221 * press on.
222 */
223 error = check_label(d, sector);
224 if (error >= 0)
225 return (error);
226
227 /*
228 * Nothing at start of disk, return info from mbr partitions.
229 */
230 /* XXX fill it to make checksum match kernel one */
231 dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
232 memcpy(d->buf, &dflt_lbl, sizeof(dflt_lbl));
233 return (-1);
234 }
235
236
237 /*
238 * Determine likely partition for possible sector number of dos
239 * partition.
240 */
241 u_int
242 biosdisk_findptn(int biosdev, u_int sector)
243 {
244 struct biosdisk *d;
245 u_int partition = 0;
246 struct disklabel *lp;
247
248 /* Look for netbsd partition that is the dos boot one */
249 d = alloc_biosdisk(biosdev);
250 if (d == NULL)
251 return (0);
252
253 if (read_label(d) == 0) {
254 lp = (struct disklabel *)(d->buf + LABELOFFSET);
255 for (partition = lp->d_npartitions; --partition;){
256 if (lp->d_partitions[partition].p_fstype == FS_UNUSED)
257 continue;
258 if (lp->d_partitions[partition].p_offset == sector)
259 break;
260 }
261 }
262
263 DEALLOC(d, sizeof(*d));
264 return (partition);
265 }
266
267 int
268 biosdisk_open(struct open_file *f, ...)
269 {
270 va_list ap;
271 struct biosdisk *d;
272 struct disklabel *lp;
273 int dev;
274 int partition;
275 int error = 0;
276
277 va_start(ap, f);
278 dev = va_arg(ap, int);
279 d = alloc_biosdisk(dev);
280 if (d == NULL) {
281 error = ENXIO;
282 goto out;
283 }
284
285 partition = va_arg(ap, int);
286 bi_disk.biosdev = d->dev;
287 bi_disk.partition = partition;
288 bi_disk.labelsector = -1;
289
290 if (partition == RAW_PART)
291 goto nolabel;
292 error = read_label(d);
293 if (error == -1) {
294 error = 0;
295 goto nolabel;
296 }
297 if (error)
298 goto out;
299
300 lp = (struct disklabel *)(d->buf + LABELOFFSET);
301 if (partition >= lp->d_npartitions ||
302 lp->d_partitions[partition].p_fstype == FS_UNUSED) {
303 #ifdef DISK_DEBUG
304 printf("illegal partition\n");
305 #endif
306 error = EPART;
307 goto out;
308 }
309 bi_disk.labelsector = d->boff + LABELSECTOR;
310 bi_disk.label.type = lp->d_type;
311 memcpy(bi_disk.label.packname, lp->d_packname, 16);
312 bi_disk.label.checksum = lp->d_checksum;
313
314 d->boff = lp->d_partitions[partition].p_offset;
315 if (lp->d_partitions[partition].p_fstype == FS_RAID) {
316 d->boff += RF_PROTECTED_SECTORS;
317 }
318 nolabel:
319
320 #ifdef DISK_DEBUG
321 printf("partition @%d\n", d->boff);
322 #endif
323
324 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
325
326 f->f_devdata = d;
327 out:
328 va_end(ap);
329 if (error)
330 DEALLOC(d, sizeof(struct biosdisk));
331 return (error);
332 }
333
334 int
335 biosdisk_close(struct open_file *f)
336 {
337 struct biosdisk *d = f->f_devdata;
338
339 DEALLOC(d, sizeof(struct biosdisk));
340 f->f_devdata = NULL;
341
342 return (0);
343 }
344
345 int
346 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
347 {
348
349 return (EIO);
350 }
351
352 int
353 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
354 {
355 struct biosdisk *d;
356 int blks;
357 int frag;
358
359 if (flag != F_READ)
360 return EROFS;
361
362 d = (struct biosdisk *)devdata;
363
364 dblk += d->boff;
365
366 blks = size / BIOSDISK_SECSIZE;
367 if (blks && readsects(d->dev, dblk, buf, blks)) {
368 if (rsize)
369 *rsize = 0;
370 return (EIO);
371 }
372
373 /* do we really need this? */
374 frag = size % BIOSDISK_SECSIZE;
375 if (frag) {
376 if (readsects(d->dev, dblk + blks, d->buf, 1)) {
377 if (rsize)
378 *rsize = blks * BIOSDISK_SECSIZE;
379 return (EIO);
380 }
381 memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
382 }
383
384 if (rsize)
385 *rsize = size;
386 return (0);
387 }
388