2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Doug Rabson <dfr@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/boot/arc/lib/arcdisk.c,v 1.3.2.1 2001/01/05 17:37:51 mjacob Exp $
28 * $DragonFly: src/sys/boot/arc/lib/Attic/arcdisk.c,v 1.2 2003/06/17 04:28:16 dillon Exp $
32 * ARC disk device handling.
34 * Ideas and algorithms from:
36 * - NetBSD libi386/biosdisk.c
37 * - FreeBSD biosboot/disk.c
43 #include <sys/disklabel.h>
44 #include <sys/diskslice.h>
46 #include <machine/stdarg.h>
48 #include "bootstrap.h"
53 #define ARCDISK_SECSIZE 512
55 #define BUFSIZE (1 * ARCDISK_SECSIZE)
56 #define MAXBDDEV MAXDEV
64 static int bd_init(void);
65 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize);
66 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize);
67 static int bd_open(struct open_file *f, ...);
68 static int bd_close(struct open_file *f);
69 static void bd_print(int verbose);
73 int od_unit; /* our unit number */
74 int od_boff; /* block offset from beginning of ARC disk */
76 #define BD_FLOPPY (1<<2)
77 u_char od_buf[BUFSIZE]; /* transfer buffer (do we want/need this?) */
80 struct devsw arcdisk = {
92 * List of ARC devices, translation from disk unit number to
98 int bd_unit; /* ARC unit number */
102 static int nbdinfo = 0;
105 * Quiz ARC for disk devices, save a little info about them.
116 * Print information about disks
119 bd_print(int verbose)
124 for (i = 0; i < nbdinfo; i++) {
125 sprintf(line, " disk%d: ARC drive %s", i, bdinfo[i].bd_name);
127 /* XXX more detail? */
133 * Attempt to open the disk described by (dev) for use by (f).
135 * Note that the philosophy here is "give them exactly what
136 * they ask for". This is necessary because being too "smart"
137 * about what the user might want leads to complications.
138 * (eg. given no slice or partition value, with a disk that is
139 * sliced - are they after the first BSD slice, or the DOS
143 bd_open(struct open_file *f, ...)
145 struct arc_devdesc *dev;
146 struct dos_partition *dptr;
147 struct open_disk *od;
148 struct disklabel *lp;
149 int sector, slice, i;
156 dev = va_arg(ap, struct arc_devdesc *);
159 unit = dev->d_kind.arcdisk.unit;
160 if (unit >= nbdinfo) {
161 D(printf("attempt to open nonexistent disk\n"));
165 if (Open("scsi(0)disk(0)rdisk(0)partition(0)",
166 OpenReadOnly, &fd) != ESUCCESS)
167 if (Open("scsi(0)disk(1)rdisk(0)partition(0)",
168 OpenReadOnly, &fd) != ESUCCESS)
169 if (Open("multi(0)disk(0)fdisk(0)partition(0)",
170 OpenReadOnly, &fd) != ESUCCESS)
173 od = (struct open_disk *) malloc(sizeof(struct open_disk));
175 D(printf("arcdiskopen: no memory\n"));
179 /* Look up ARC unit number, intialise open_disk structure */
181 od->od_unit = dev->d_kind.arcdisk.unit;
182 od->od_flags = bdinfo[od->od_unit].bd_flags;
187 /* Get geometry for this open (removable device may have changed) */
188 if (set_geometry(&od->od_ll)) {
189 D(printf("bd_open: can't get geometry\n"));
196 * Following calculations attempt to determine the correct value
197 * for d->od_boff by looking for the slice and partition specified,
198 * or searching for reasonable defaults.
203 * Find the slice in the DOS slice table.
205 if (readsects(&od->od_ll, 0, 1, od->od_buf, 0)) {
206 D(printf("bd_open: error reading MBR\n"));
212 * Check the slice table magic.
214 if ((od->od_buf[0x1fe] != 0xff) || (od->od_buf[0x1ff] != 0xaa)) {
215 /* If a slice number was explicitly supplied, this is an error */
216 if (dev->d_kind.arcdisk.slice > 0) {
217 D(printf("bd_open: no slice table/MBR (no magic)\n"));
222 goto unsliced; /* may be a floppy */
224 dptr = (struct dos_partition *) & od->od_buf[DOSPARTOFF];
227 * XXX No support here for 'extended' slices
229 if (dev->d_kind.arcdisk.slice <= 0) {
231 * Search for the first FreeBSD slice; this also works on "unsliced"
232 * disks, as they contain a "historically bogus" MBR.
234 for (i = 0; i < NDOSPART; i++, dptr++)
235 if (dptr->dp_typ == DOSPTYP_386BSD) {
236 sector = dptr->dp_start;
239 /* Did we find something? */
246 * Accept the supplied slice number unequivocally (we may be looking
247 * for a DOS partition) if we can handle it.
249 if ((dev->d_kind.arcdisk.slice > NDOSPART) || (dev->d_kind.arcdisk.slice < 1)) {
253 dptr += (dev->d_kind.arcdisk.slice - 1);
254 sector = dptr->dp_start;
262 * Now we have the slice, look for the partition in the disklabel if we have
263 * a partition to start with.
265 if (dev->d_kind.arcdisk.partition < 0) {
266 od->od_boff = sector; /* no partition, must be after the slice */
268 if (bd_strategy(od, F_READ, sector + LABELSECTOR, 512, od->od_buf, 0)) {
269 D(printf("bd_open: error reading disklabel\n"));
273 lp = (struct disklabel *) (od->od_buf + LABELOFFSET);
274 if (lp->d_magic != DISKMAGIC) {
275 D(printf("bd_open: no disklabel\n"));
279 } else if (dev->d_kind.arcdisk.partition >= lp->d_npartitions) {
282 * The partition supplied is out of bounds; this is fatal.
284 D(printf("partition '%c' exceeds partitions in table (a-'%c')\n",
285 'a' + dev->d_kind.arcdisk.partition, 'a' + lp->d_npartitions));
292 * Complain if the partition type is wrong and it shouldn't be, but
293 * regardless accept this partition.
295 D(if ((lp->d_partitions[dev->d_kind.arcdisk.partition].p_fstype == FS_UNUSED) &&
296 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */
297 printf("bd_open: warning, partition marked as unused\n"););
299 od->od_boff = lp->d_partitions[dev->d_kind.arcdisk.partition].p_offset;
314 bd_close(struct open_file *f)
316 struct open_disk *od = f->f_devdata;
326 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
328 struct bcache_devdata bcd;
329 struct arc_devdesc *dev = (struct arc_devdesc *)devdata;
331 bcd.dv_strategy = bd_realstrategy;
332 bcd.dv_devdata = devdata;
333 return(bcache_strategy(&bcd, dev->d_kind.arcdisk.unit, rw, dblk, size,
338 bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize)
340 struct open_disk *od = (struct open_disk *)devdata;
344 if (size % ARCDISK_SECSIZE)
345 panic("bd_strategy: I/O not block multiple");
353 seek = 512 * (dblk + od->od_boff);
354 Seek(od->od_fd, &seek, SeekAbsolute);
355 if (Read(od->od_fd, buf, size, &count) != ESUCCESS) {
356 D(printf("read error\n"));