disk.c revision 1.1.18.2 1 1.1.18.2 yamt /* $NetBSD: disk.c,v 1.1.18.2 2006/06/21 14:51:20 yamt Exp $ */
2 1.1.18.2 yamt
3 1.1.18.2 yamt /*-
4 1.1.18.2 yamt * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 1.1.18.2 yamt * All rights reserved.
6 1.1.18.2 yamt *
7 1.1.18.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.18.2 yamt * by UCHIYAMA Yasushi.
9 1.1.18.2 yamt *
10 1.1.18.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.18.2 yamt * modification, are permitted provided that the following conditions
12 1.1.18.2 yamt * are met:
13 1.1.18.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.18.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.18.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.18.2 yamt * must display the following acknowledgement:
20 1.1.18.2 yamt * This product includes software developed by the NetBSD
21 1.1.18.2 yamt * Foundation, Inc. and its contributors.
22 1.1.18.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.18.2 yamt * contributors may be used to endorse or promote products derived
24 1.1.18.2 yamt * from this software without specific prior written permission.
25 1.1.18.2 yamt *
26 1.1.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.18.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.18.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.18.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.18.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.18.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.18.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.18.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.18.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.18.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.18.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.18.2 yamt */
38 1.1.18.2 yamt
39 1.1.18.2 yamt #include <lib/libsa/stand.h>
40 1.1.18.2 yamt #include <lib/libkern/libkern.h>
41 1.1.18.2 yamt
42 1.1.18.2 yamt #include <machine/param.h>
43 1.1.18.2 yamt #include <machine/sbd.h>
44 1.1.18.2 yamt #include <machine/sector.h>
45 1.1.18.2 yamt
46 1.1.18.2 yamt #include "local.h"
47 1.1.18.2 yamt #include "common.h"
48 1.1.18.2 yamt
49 1.1.18.2 yamt int dkopen(struct open_file *, ...);
50 1.1.18.2 yamt int dkclose(struct open_file *);
51 1.1.18.2 yamt int dkstrategy(void *, int, daddr_t, size_t, void *, size_t *);
52 1.1.18.2 yamt
53 1.1.18.2 yamt struct devsw dkdevsw = {
54 1.1.18.2 yamt "dk", dkstrategy, dkopen, dkclose, noioctl
55 1.1.18.2 yamt };
56 1.1.18.2 yamt
57 1.1.18.2 yamt struct disk {
58 1.1.18.2 yamt boolean_t active;
59 1.1.18.2 yamt int type; /* FD/HD */
60 1.1.18.2 yamt int unit;
61 1.1.18.2 yamt int format; /* 2D/2HD */
62 1.1.18.2 yamt int partition;
63 1.1.18.2 yamt int offset;
64 1.1.18.2 yamt int (*rw)(uint8_t *, int, int, int);
65 1.1.18.2 yamt } __disk;
66 1.1.18.2 yamt
67 1.1.18.2 yamt void sector_init(void);
68 1.1.18.2 yamt boolean_t __sector_rw(uint8_t *, int, int, int);
69 1.1.18.2 yamt int __hd_rw(uint8_t *, int, int, int);
70 1.1.18.2 yamt int __fd_2d_rw(uint8_t *, int, int, int);
71 1.1.18.2 yamt int __fd_2hd_rw(uint8_t *, int, int, int);
72 1.1.18.2 yamt void __fd_progress_msg(int);
73 1.1.18.2 yamt
74 1.1.18.2 yamt boolean_t
75 1.1.18.2 yamt device_attach(int type, int unit, int partition)
76 1.1.18.2 yamt {
77 1.1.18.2 yamt
78 1.1.18.2 yamt /* Inquire boot device type and unit from NVSRAM */
79 1.1.18.2 yamt boot_device(&__disk.type, &__disk.unit, &__disk.format);
80 1.1.18.2 yamt
81 1.1.18.2 yamt if (type >= 0)
82 1.1.18.2 yamt __disk.type = type;
83 1.1.18.2 yamt if (unit >= 0)
84 1.1.18.2 yamt __disk.unit = unit;
85 1.1.18.2 yamt
86 1.1.18.2 yamt __disk.partition = partition;
87 1.1.18.2 yamt
88 1.1.18.2 yamt __disk.active = TRUE;
89 1.1.18.2 yamt __disk.offset = 0;
90 1.1.18.2 yamt
91 1.1.18.2 yamt if (partition >= 0) {
92 1.1.18.2 yamt if (!find_partition_start(__disk.partition, &__disk.offset)) {
93 1.1.18.2 yamt printf("type %d, unit %d partition %d not found.\n",
94 1.1.18.2 yamt __disk.type, __disk.unit, __disk.partition);
95 1.1.18.2 yamt return FALSE;
96 1.1.18.2 yamt }
97 1.1.18.2 yamt }
98 1.1.18.2 yamt DEVICE_CAPABILITY.active_device = type;
99 1.1.18.2 yamt
100 1.1.18.2 yamt /* Set actual read/write routine */
101 1.1.18.2 yamt if (__disk.type == NVSRAM_BOOTDEV_HARDDISK) {
102 1.1.18.2 yamt __disk.rw = __hd_rw;
103 1.1.18.2 yamt } else if (__disk.type == NVSRAM_BOOTDEV_FLOPPYDISK) {
104 1.1.18.2 yamt if (__disk.format == FD_FORMAT_2HD) {
105 1.1.18.2 yamt __disk.rw = __fd_2hd_rw;
106 1.1.18.2 yamt } else if (__disk.format == FD_FORMAT_2D) {
107 1.1.18.2 yamt __disk.rw = __fd_2d_rw;
108 1.1.18.2 yamt } else {
109 1.1.18.2 yamt printf("unknown floppy disk format %d.\n",
110 1.1.18.2 yamt __disk.format);
111 1.1.18.2 yamt return FALSE;
112 1.1.18.2 yamt }
113 1.1.18.2 yamt } else {
114 1.1.18.2 yamt printf("unknown disk type %d.\n", __disk.type);
115 1.1.18.2 yamt return FALSE;
116 1.1.18.2 yamt }
117 1.1.18.2 yamt
118 1.1.18.2 yamt return TRUE;
119 1.1.18.2 yamt }
120 1.1.18.2 yamt
121 1.1.18.2 yamt int
122 1.1.18.2 yamt dkopen(struct open_file *f, ...)
123 1.1.18.2 yamt {
124 1.1.18.2 yamt
125 1.1.18.2 yamt return 0;
126 1.1.18.2 yamt }
127 1.1.18.2 yamt
128 1.1.18.2 yamt int
129 1.1.18.2 yamt dkclose(struct open_file *f)
130 1.1.18.2 yamt {
131 1.1.18.2 yamt
132 1.1.18.2 yamt return 0;
133 1.1.18.2 yamt }
134 1.1.18.2 yamt
135 1.1.18.2 yamt int
136 1.1.18.2 yamt dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
137 1.1.18.2 yamt size_t *rsize)
138 1.1.18.2 yamt {
139 1.1.18.2 yamt int n;
140 1.1.18.2 yamt
141 1.1.18.2 yamt if ((int)size < 0) {
142 1.1.18.2 yamt printf("%s: invalid request block %d size %d base %d\n",
143 1.1.18.2 yamt __FUNCTION__, blk, size, __disk.offset);
144 1.1.18.2 yamt return -1;
145 1.1.18.2 yamt }
146 1.1.18.2 yamt
147 1.1.18.2 yamt n = ROUND_SECTOR(size) >> DEV_BSHIFT;
148 1.1.18.2 yamt if (!sector_read_n(0, buf, __disk.offset + blk, n))
149 1.1.18.2 yamt return -1;
150 1.1.18.2 yamt
151 1.1.18.2 yamt *rsize = size;
152 1.1.18.2 yamt
153 1.1.18.2 yamt return 0;
154 1.1.18.2 yamt }
155 1.1.18.2 yamt
156 1.1.18.2 yamt void
157 1.1.18.2 yamt sector_init(void)
158 1.1.18.2 yamt {
159 1.1.18.2 yamt
160 1.1.18.2 yamt if (!__disk.active)
161 1.1.18.2 yamt device_attach(-1, -1, -1);
162 1.1.18.2 yamt }
163 1.1.18.2 yamt
164 1.1.18.2 yamt void
165 1.1.18.2 yamt sector_fini(void *self)
166 1.1.18.2 yamt {
167 1.1.18.2 yamt
168 1.1.18.2 yamt __disk.active = FALSE;
169 1.1.18.2 yamt }
170 1.1.18.2 yamt
171 1.1.18.2 yamt boolean_t
172 1.1.18.2 yamt sector_read_n(void *self, uint8_t *buf, int sector, int count)
173 1.1.18.2 yamt {
174 1.1.18.2 yamt
175 1.1.18.2 yamt if (!__sector_rw(buf, sector, 0, count))
176 1.1.18.2 yamt return FALSE;
177 1.1.18.2 yamt return TRUE;
178 1.1.18.2 yamt }
179 1.1.18.2 yamt
180 1.1.18.2 yamt boolean_t
181 1.1.18.2 yamt sector_read(void *self, uint8_t *buf, int sector)
182 1.1.18.2 yamt {
183 1.1.18.2 yamt
184 1.1.18.2 yamt return __sector_rw(buf, sector, 0, 1);
185 1.1.18.2 yamt }
186 1.1.18.2 yamt
187 1.1.18.2 yamt boolean_t
188 1.1.18.2 yamt sector_write_n(void *self, uint8_t *buf, int sector, int count)
189 1.1.18.2 yamt {
190 1.1.18.2 yamt
191 1.1.18.2 yamt if (!__sector_rw(buf, sector, 0x1000, count))
192 1.1.18.2 yamt return FALSE;
193 1.1.18.2 yamt return TRUE;
194 1.1.18.2 yamt }
195 1.1.18.2 yamt
196 1.1.18.2 yamt boolean_t
197 1.1.18.2 yamt sector_write(void *self, uint8_t *buf, int sector)
198 1.1.18.2 yamt {
199 1.1.18.2 yamt
200 1.1.18.2 yamt return __sector_rw(buf, sector, 0x1000, 1);
201 1.1.18.2 yamt }
202 1.1.18.2 yamt
203 1.1.18.2 yamt boolean_t
204 1.1.18.2 yamt __sector_rw(uint8_t *buf, int block, int flag, int count)
205 1.1.18.2 yamt {
206 1.1.18.2 yamt int err;
207 1.1.18.2 yamt
208 1.1.18.2 yamt if (!__disk.active)
209 1.1.18.2 yamt sector_init();
210 1.1.18.2 yamt
211 1.1.18.2 yamt if ((err = __disk.rw(buf, block, flag, count)) != 0)
212 1.1.18.2 yamt printf("%s: type=%d unit=%d offset=%d block=%d err=%d\n",
213 1.1.18.2 yamt __FUNCTION__, __disk.type, __disk.unit, __disk.offset,
214 1.1.18.2 yamt block, err);
215 1.1.18.2 yamt
216 1.1.18.2 yamt return err == 0;
217 1.1.18.2 yamt }
218 1.1.18.2 yamt
219 1.1.18.2 yamt int
220 1.1.18.2 yamt __hd_rw(uint8_t *buf, int block, int flag, int count)
221 1.1.18.2 yamt {
222 1.1.18.2 yamt
223 1.1.18.2 yamt return (ROM_DK_RW(flag | __disk.unit, block, count, buf) & 0x7f);
224 1.1.18.2 yamt }
225 1.1.18.2 yamt
226 1.1.18.2 yamt int
227 1.1.18.2 yamt __fd_2d_rw(uint8_t *buf, int block, int flag, int count)
228 1.1.18.2 yamt {
229 1.1.18.2 yamt int pos, cnt, i, err;
230 1.1.18.2 yamt
231 1.1.18.2 yamt if (!blk_to_2d_position(block, &pos, &cnt)) {
232 1.1.18.2 yamt printf("%s: invalid block #%d.\n", __FUNCTION__, block);
233 1.1.18.2 yamt return -1;
234 1.1.18.2 yamt }
235 1.1.18.2 yamt __fd_progress_msg(pos);
236 1.1.18.2 yamt
237 1.1.18.2 yamt for (i = 0; i < count; i++, buf += DEV_BSIZE) {
238 1.1.18.2 yamt err = ROM_FD_RW(flag | __disk.unit, pos, cnt, buf);
239 1.1.18.2 yamt if (err)
240 1.1.18.2 yamt return err;
241 1.1.18.2 yamt }
242 1.1.18.2 yamt return 0;
243 1.1.18.2 yamt }
244 1.1.18.2 yamt
245 1.1.18.2 yamt int
246 1.1.18.2 yamt __fd_2hd_rw(uint8_t *buf, int block, int flag, int count)
247 1.1.18.2 yamt {
248 1.1.18.2 yamt int pos, cnt, i, err;
249 1.1.18.2 yamt
250 1.1.18.2 yamt if (!blk_to_2hd_position(block, &pos, &cnt)) {
251 1.1.18.2 yamt printf("%s: invalid block #%d.\n", __FUNCTION__, block);
252 1.1.18.2 yamt return -1;
253 1.1.18.2 yamt }
254 1.1.18.2 yamt __fd_progress_msg(pos);
255 1.1.18.2 yamt
256 1.1.18.2 yamt for (i = 0; i < count; i++, buf += DEV_BSIZE) {
257 1.1.18.2 yamt err = ROM_FD_RW(flag | __disk.unit | 0x1000000, pos, cnt, buf);
258 1.1.18.2 yamt if (err)
259 1.1.18.2 yamt return err;
260 1.1.18.2 yamt }
261 1.1.18.2 yamt return 0;
262 1.1.18.2 yamt }
263 1.1.18.2 yamt
264 1.1.18.2 yamt void
265 1.1.18.2 yamt __fd_progress_msg(int pos)
266 1.1.18.2 yamt {
267 1.1.18.2 yamt char msg[16];
268 1.1.18.2 yamt
269 1.1.18.2 yamt memset(msg, 0, sizeof msg);
270 1.1.18.2 yamt sprintf(msg, "C%d H%d S%d\r", (pos >> 16) & 0xff, (pos >> 8) & 0xff,
271 1.1.18.2 yamt pos & 0xff);
272 1.1.18.2 yamt printf("%s", msg);
273 1.1.18.2 yamt }
274