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