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