diskio.c revision 1.1.1.1 1 /* $NetBSD: diskio.c,v 1.1.1.1 1996/02/29 11:36:12 leo Exp $ */
2
3 /*
4 * Copyright (c) 1995 Waldi Ravens.
5 * 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 by Waldi Ravens.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "stand.h"
34 #include <sys/disklabel.h>
35
36 typedef int (*rdsec_f)__P((void *buffer, u_int offset, u_int count));
37 typedef struct { rdsec_f rds; u_int rst; u_int rend; } bdevd_t;
38
39 static int rootstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
40 static int rootopen __P((struct open_file *, ...));
41 static int rootclose __P((struct open_file *));
42 static int rootioctl __P((struct open_file *, u_long, void *));
43
44 static struct devsw devsw[] = {
45 { "root", rootstrategy, rootopen, rootclose, rootioctl }
46 };
47 static bdevd_t bootdev;
48
49 /*
50 * Initialise boot device info.
51 */
52 int
53 init_dskio (func, label, root)
54 void *func, *label;
55 int root;
56 {
57 struct disklabel *dl = label;
58 struct partition *pd = &dl->d_partitions[root];
59
60 if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC
61 || dl->d_npartitions > MAXPARTITIONS || dkcksum(dl) != 0) {
62 printf("Invalid disk label.\n");
63 return(-1);
64 }
65 if (root >= 0) {
66 if (root >= dl->d_npartitions
67 || pd->p_fstype != FS_BSDFFS || pd->p_size == 0) {
68 printf("No suitable root.\n");
69 return(-1);
70 }
71 bootdev.rds = func;
72 bootdev.rst = pd->p_offset;
73 bootdev.rend = pd->p_offset + pd->p_size - 1;
74 }
75 return(0);
76 }
77
78 /*
79 * No choice, the kernel is loaded from the
80 * same device as the bootstrap.
81 */
82 int
83 devopen (f, fname, file)
84 struct open_file *f;
85 const char *fname;
86 char **file;
87 {
88 f->f_devdata = &bootdev;
89 f->f_dev = &devsw[0];
90 *file = (char *)fname;
91 return(0);
92 }
93
94 static int
95 rootstrategy (devd, flag, dblk, size, buf, rsize)
96 void *devd;
97 int flag;
98 daddr_t dblk;
99 size_t size;
100 void *buf;
101 size_t *rsize;
102 {
103 bdevd_t *dd = devd;
104 daddr_t stb = dd->rst + dblk;
105 size_t nb = size >> 9;
106
107 if ((flag == F_READ) && !(size & 511) && (stb + nb <= dd->rend)) {
108 if (!dd->rds(buf, stb, nb)) {
109 *rsize = size;
110 return(0);
111 }
112 }
113 *rsize = 0;
114 return(EIO);
115 }
116
117 static int
118 rootopen (f)
119 struct open_file *f;
120 {
121 return(0);
122 }
123
124 static int
125 rootclose (f)
126 struct open_file *f;
127 {
128 return(EIO);
129 }
130
131 static int
132 rootioctl (f, cmd, data)
133 struct open_file *f;
134 u_long cmd;
135 void *data;
136 {
137 return(EIO);
138 }
139