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