No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / stand / common / rawfs.c
blobc74dc81c011bf1508b90d2164baf841b2f3bab4d
1 /* $NetBSD: rawfs.c,v 1.7 2006/06/25 17:36:07 tsutsui Exp $ */
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Raw file system - for stream devices like tapes.
30 * No random access, only sequential read allowed.
31 * This exists only to allow upper level code to be
32 * shielded from the fact that the device must be
33 * read only with whole block position and size.
36 #include <sys/param.h>
38 #include <lib/libsa/stand.h>
39 #include <hp300/stand/common/rawfs.h>
41 extern int debug;
43 /* Our devices are generally willing to do 8K transfers. */
44 #define RAWFS_BSIZE 0x2000
47 * In-core open file.
49 struct rawfs_file {
50 daddr_t fs_nextblk; /* block number to read next */
51 int fs_len; /* amount left in f_buf */
52 char * fs_ptr; /* read pointer into f_buf */
53 char fs_buf[RAWFS_BSIZE];
56 static int rawfs_get_block(struct open_file *);
58 int
59 rawfs_open(const char *path, struct open_file *f)
61 struct rawfs_file *fs;
64 * The actual tape driver has already been opened.
65 * Just allocate the I/O buffer, etc.
67 fs = alloc(sizeof(struct rawfs_file));
68 fs->fs_nextblk = 0;
69 fs->fs_len = 0;
70 fs->fs_ptr = fs->fs_buf;
72 #ifdef DEBUG_RAWFS
73 printf("rawfs_open: fs=0x%x\n", (u_long)fs);
74 #endif
76 f->f_fsdata = fs;
77 return 0;
80 int
81 rawfs_close(struct open_file *f)
83 struct rawfs_file *fs;
85 fs = (struct rawfs_file *) f->f_fsdata;
86 f->f_fsdata = (void *)0;
88 #ifdef DEBUG_RAWFS
89 printf("rawfs_close: fs=0x%x\n", (u_long)fs);
90 #endif
92 if (fs != (struct rawfs_file *)0)
93 dealloc(fs, sizeof(*fs));
95 return 0;
98 int
99 rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
101 struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
102 char *addr = start;
103 int error = 0;
104 size_t csize;
106 #ifdef DEBUG_RAWFS
107 printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
108 (u_long)f, (u_long)start, size, resid);
109 printf(" fs=0x%x\n", (u_long)fs);
110 #endif
112 while (size != 0) {
114 if (fs->fs_len == 0)
115 if ((error = rawfs_get_block(f)) != 0)
116 break;
118 if (fs->fs_len <= 0)
119 break; /* EOF */
121 csize = size;
122 if (csize > fs->fs_len)
123 csize = fs->fs_len;
125 memcpy(addr, fs->fs_ptr, csize);
126 fs->fs_ptr += csize;
127 fs->fs_len -= csize;
128 addr += csize;
129 size -= csize;
131 if (resid)
132 *resid = size;
133 return error;
137 rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
140 #ifdef DEBUG_RAWFS
141 printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
142 #endif
143 return EROFS;
146 off_t
147 rawfs_seek(struct open_file *f, off_t offset, int where)
150 #ifdef DEBUG_RAWFS
151 printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
152 #endif
153 return EFTYPE;
157 rawfs_stat(struct open_file *f, struct stat *sb)
160 #ifdef DEBUG_RAWFS
161 printf("rawfs_stat: I'll let you live only because of exec.c\n");
162 #endif
164 * Clear out the stat buffer so that the uid check
165 * won't fail. See sys/lib/libsa/exec.c
167 memset(sb, 0, sizeof(*sb));
169 return EFTYPE;
173 * Read a block from the underlying stream device
174 * (In our case, a tape drive.)
176 static int
177 rawfs_get_block(struct open_file *f)
179 struct rawfs_file *fs;
180 int error;
181 size_t len;
183 fs = (struct rawfs_file *)f->f_fsdata;
184 fs->fs_ptr = fs->fs_buf;
186 twiddle();
187 #ifdef DEBUG_RAWFS
188 printf("rawfs_get_block: calling strategy\n");
189 #endif
190 error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
191 fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
192 #ifdef DEBUG_RAWFS
193 printf("rawfs_get_block: strategy returned %d\n", error);
194 #endif
196 if (!error) {
197 fs->fs_len = len;
198 fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
201 return error;