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/alpha/libalpha/srmdisk.c,v 1.8.2.2 2001/08/01 20:52:03 mjacob Exp $
28 * $DragonFly: src/sys/boot/alpha/libalpha/Attic/srmdisk.c,v 1.3 2003/11/09 02:22:29 dillon Exp $
32 * SRM 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>
47 #include <machine/prom.h>
49 #include "bootstrap.h"
52 #define SRMDISK_SECSIZE 512
54 #define BUFSIZE (1 * SRMDISK_SECSIZE)
55 #define MAXBDDEV MAXDEV
63 static int bd_init(void);
64 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
65 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
66 static int bd_open(struct open_file *f, ...);
67 static int bd_close(struct open_file *f);
68 static void bd_print(int verbose);
72 int od_unit; /* our unit number */
73 int od_boff; /* block offset from beginning of SRM disk */
75 #define BD_FLOPPY (1<<2)
76 u_char od_buf[BUFSIZE]; /* transfer buffer (do we want/need this?) */
79 struct devsw srmdisk = {
91 * List of SRM devices, translation from disk unit number to
97 int bd_unit; /* SRM unit number */
103 static int nbdinfo = 0;
106 * Quiz SRM for disk devices, save a little info about them.
114 bdinfo[0].bd_unit = 0; /* XXX */
115 bdinfo[0].bd_flags = 0; /* XXX */
116 ret.bits = prom_getenv(PROM_E_BOOTED_DEV,
117 bdinfo[0].bd_name, sizeof(bdinfo[0].bd_name));
118 bdinfo[0].bd_namelen = ret.u.retval;
119 bdinfo[0].bd_fd = -1;
120 bdinfo[0].bd_opencount = 0;
127 * Print information about disks
130 bd_print(int verbose)
135 for (i = 0; i < nbdinfo; i++) {
136 sprintf(line, " disk%d: SRM drive %s", i, bdinfo[i].bd_name);
138 /* XXX more detail? */
144 * Attempt to open the disk described by (dev) for use by (f).
146 * Note that the philosophy here is "give them exactly what
147 * they ask for". This is necessary because being too "smart"
148 * about what the user might want leads to complications.
149 * (eg. given no slice or partition value, with a disk that is
150 * sliced - are they after the first BSD slice, or the DOS
154 bd_open(struct open_file *f, ...)
157 struct alpha_devdesc *dev;
158 struct dos_partition *dptr;
159 struct open_disk *od;
160 struct disklabel *lp;
161 int sector, slice, i;
167 dev = __va_arg(args, struct alpha_devdesc*);
170 unit = dev->d_kind.srmdisk.unit;
171 if (unit >= nbdinfo) {
172 D(printf("attempt to open nonexistent disk\n"));
176 /* Call the prom to open the disk. */
177 if (bdinfo[unit].bd_fd < 0) {
178 ret.bits = prom_open(bdinfo[unit].bd_name, bdinfo[unit].bd_namelen);
179 if (ret.u.status == 2)
181 if (ret.u.status == 3)
183 bdinfo[unit].bd_fd = fd = ret.u.retval;
185 fd = bdinfo[unit].bd_fd;
187 bdinfo[unit].bd_opencount++;
189 od = (struct open_disk *) malloc(sizeof(struct open_disk));
191 D(printf("srmdiskopen: no memory\n"));
195 /* Look up SRM unit number, intialise open_disk structure */
197 od->od_unit = dev->d_kind.srmdisk.unit;
198 od->od_flags = bdinfo[od->od_unit].bd_flags;
203 /* Get geometry for this open (removable device may have changed) */
204 if (set_geometry(&od->od_ll)) {
205 D(printf("bd_open: can't get geometry\n"));
212 * Following calculations attempt to determine the correct value
213 * for d->od_boff by looking for the slice and partition specified,
214 * or searching for reasonable defaults.
219 * Find the slice in the DOS slice table.
221 if (readsects(&od->od_ll, 0, 1, od->od_buf, 0)) {
222 D(printf("bd_open: error reading MBR\n"));
228 * Check the slice table magic.
230 if ((od->od_buf[0x1fe] != 0xff) || (od->od_buf[0x1ff] != 0xaa)) {
231 /* If a slice number was explicitly supplied, this is an error */
232 if (dev->d_kind.srmdisk.slice > 0) {
233 D(printf("bd_open: no slice table/MBR (no magic)\n"));
238 goto unsliced; /* may be a floppy */
240 dptr = (struct dos_partition *) & od->od_buf[DOSPARTOFF];
243 * XXX No support here for 'extended' slices
245 if (dev->d_kind.srmdisk.slice <= 0) {
247 * Search for the first FreeBSD slice; this also works on "unsliced"
248 * disks, as they contain a "historically bogus" MBR.
250 for (i = 0; i < NDOSPART; i++, dptr++)
251 if (dptr->dp_typ == DOSPTYP_386BSD) {
252 sector = dptr->dp_start;
255 /* Did we find something? */
262 * Accept the supplied slice number unequivocally (we may be looking
263 * for a DOS partition) if we can handle it.
265 if ((dev->d_kind.srmdisk.slice > NDOSPART) || (dev->d_kind.srmdisk.slice < 1)) {
269 dptr += (dev->d_kind.srmdisk.slice - 1);
270 sector = dptr->dp_start;
278 * Now we have the slice, look for the partition in the disklabel if we have
279 * a partition to start with.
281 if (dev->d_kind.srmdisk.partition < 0) {
282 od->od_boff = sector; /* no partition, must be after the slice */
284 if (bd_strategy(od, F_READ, sector + LABELSECTOR, 512, od->od_buf, 0)) {
285 D(printf("bd_open: error reading disklabel\n"));
289 lp = (struct disklabel *) (od->od_buf + LABELOFFSET);
290 if (lp->d_magic != DISKMAGIC) {
291 D(printf("bd_open: no disklabel\n"));
296 } else if (dev->d_kind.srmdisk.partition >= lp->d_npartitions) {
299 * The partition supplied is out of bounds; this is fatal.
301 D(printf("partition '%c' exceeds partitions in table (a-'%c')\n",
302 'a' + dev->d_kind.srmdisk.partition, 'a' + lp->d_npartitions));
309 * Complain if the partition type is wrong and it shouldn't be, but
310 * regardless accept this partition.
312 D(if ((lp->d_partitions[dev->d_kind.srmdisk.partition].p_fstype == FS_UNUSED) &&
313 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */
314 printf("bd_open: warning, partition marked as unused\n"););
316 od->od_boff = lp->d_partitions[dev->d_kind.srmdisk.partition].p_offset;
331 bd_close(struct open_file *f)
333 struct open_disk *od = f->f_devdata;
335 bdinfo[od->od_unit].bd_opencount--;
336 if (bdinfo[od->od_unit].bd_opencount == 0) {
337 (void)prom_close(od->od_fd);
338 bdinfo[od->od_unit].bd_fd = -1;
347 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
349 struct bcache_devdata bcd;
350 struct open_disk *od = (struct open_disk *)devdata;
352 bcd.dv_strategy = bd_realstrategy;
353 bcd.dv_devdata = devdata;
354 return(bcache_strategy(&bcd, od->od_unit, rw, dblk + od->od_boff, size, buf, rsize));
358 bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
361 struct open_disk *od = (struct open_disk *)devdata;
363 if (size % SRMDISK_SECSIZE)
364 panic("bd_strategy: I/O not block multiple");
372 ret.bits = prom_read(od->od_fd, size, buf, dblk);
374 D(printf("read error\n"));