xref: /netbsd-src/sys/arch/sun68k/stand/tapeboot/rawfs.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: rawfs.c,v 1.5 2005/12/11 12:19:29 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Gordon W. Ross
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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  * 4. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Gordon W. Ross
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 /*
34  * Raw file system - for stream devices like tapes.
35  * No random access, only sequential read allowed.
36  * This exists only to allow upper level code to be
37  * shielded from the fact that the device must be
38  * read only with whole block position and size.
39  */
40 
41 #include <sys/param.h>
42 #include <stand.h>
43 
44 #include "rawfs.h"
45 
46 extern int debug;
47 
48 /* Our devices are generally willing to do 8K transfers. */
49 #define	RAWFS_BSIZE	0x2000
50 
51 /*
52  * In-core open file.
53  */
54 struct file {
55 	daddr_t		fs_nextblk;	/* block number to read next */
56 	off_t		fs_off;		/* seek offset in file */
57 	int		fs_len;		/* amount left in f_buf */
58 	char *		fs_ptr;		/* read pointer into f_buf */
59 	char		fs_buf[RAWFS_BSIZE];
60 };
61 
62 static int rawfs_get_block(struct open_file *);
63 
64 int
65 rawfs_open(const char *path, struct open_file *f)
66 {
67 	struct file *fs;
68 
69 	/*
70 	 * The actual PROM driver has already been opened.
71 	 * Just allocate the I/O buffer, etc.
72 	 */
73 	fs = alloc(sizeof(struct file));
74 	fs->fs_nextblk = 0;
75 	fs->fs_off = 0;
76 	fs->fs_len = 0;
77 	fs->fs_ptr = fs->fs_buf;
78 
79 #ifdef	DEBUG_RAWFS
80 	printf("rawfs_open: fs=0x%x\n", fs);
81 #endif
82 
83 	f->f_fsdata = fs;
84 	return (0);
85 }
86 
87 int
88 rawfs_close(struct open_file *f)
89 {
90 	struct file *fs;
91 
92 	fs = (struct file *) f->f_fsdata;
93 	f->f_fsdata = NULL;
94 
95 #ifdef	DEBUG_RAWFS
96 	if (debug) {
97 		printf("rawfs_close: breakpoint...", fs->fs_buf);
98 		__asm ("	trap #0");
99 	}
100 #endif
101 
102 	if (fs != NULL)
103 		free(fs, sizeof(*fs));
104 
105 	return (0);
106 }
107 
108 int
109 rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
110 {
111 	struct file *fs = (struct file *)f->f_fsdata;
112 	char *addr = start;
113 	int error = 0;
114 	size_t csize;
115 
116 	while (size != 0) {
117 		if (fs->fs_len == 0)
118 			if ((error = rawfs_get_block(f)) != 0)
119 				break;
120 
121 		if (fs->fs_len <= 0)
122 			break;	/* EOF */
123 
124 		csize = size;
125 		if (csize > fs->fs_len)
126 			csize = fs->fs_len;
127 
128 		memcpy(addr, fs->fs_ptr, csize);
129 		fs->fs_off += csize;
130 		fs->fs_ptr += csize;
131 		fs->fs_len -= csize;
132 		addr += csize;
133 		size -= csize;
134 	}
135 	if (resid)
136 		*resid = size;
137 	return (error);
138 }
139 
140 int
141 rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
142 {
143 #ifdef	DEBUG_RAWFS
144 	panic("rawfs_write");
145 #endif
146 	return (EROFS);
147 }
148 
149 off_t
150 rawfs_seek(struct open_file *f, off_t offset, int where)
151 {
152 	struct file *fs = (struct file *)f->f_fsdata;
153 	off_t csize;
154 
155 	switch (where) {
156 	case SEEK_SET:
157 		offset -= fs->fs_off;
158 		/* FALLTHROUGH */
159 	case SEEK_CUR:
160 		if (offset >= 0)
161 			break;
162 		/* FALLTHROUGH */
163 	case SEEK_END:
164 	default:
165 		return (-1);
166 	}
167 
168 	while (offset != 0) {
169 
170 		if (fs->fs_len == 0)
171 			if (rawfs_get_block(f) != 0)
172 				return (-1);
173 
174 		if (fs->fs_len <= 0)
175 			break;	/* EOF */
176 
177 		csize = offset;
178 		if (csize > fs->fs_len)
179 			csize = fs->fs_len;
180 
181 		fs->fs_off += csize;
182 		fs->fs_ptr += csize;
183 		fs->fs_len -= csize;
184 		offset -= csize;
185 	}
186 	return (fs->fs_off);
187 }
188 
189 int
190 rawfs_stat(struct open_file *f, struct stat *sb)
191 {
192 #ifdef	DEBUG_RAWFS
193 	panic("rawfs_stat");
194 #endif
195 	return (EFTYPE);
196 }
197 
198 
199 /*
200  * Read a block from the underlying stream device
201  * (In our case, a tape drive.)
202  */
203 static int
204 rawfs_get_block(struct open_file *f)
205 {
206 	struct file *fs;
207 	int error, len;
208 
209 	fs = (struct file *)f->f_fsdata;
210 	fs->fs_ptr = fs->fs_buf;
211 
212 	twiddle();
213 	error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
214 		fs->fs_nextblk, RAWFS_BSIZE,	fs->fs_buf, &len);
215 
216 	if (!error) {
217 		fs->fs_len = len;
218 		fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
219 	}
220 
221 	return (error);
222 }
223