xref: /netbsd-src/sys/arch/hp300/stand/common/rawfs.c (revision febb7cce65f113332bd6a43f6cb22809175a03b6)
1*febb7cceSsnj /*	$NetBSD: rawfs.c,v 1.8 2009/10/21 23:12:09 snj Exp $	*/
2a0864b3eSthorpej 
3a0864b3eSthorpej /*
4a0864b3eSthorpej  * Copyright (c) 1995 Gordon W. Ross
5a0864b3eSthorpej  * All rights reserved.
6a0864b3eSthorpej  *
7a0864b3eSthorpej  * Redistribution and use in source and binary forms, with or without
8a0864b3eSthorpej  * modification, are permitted provided that the following conditions
9a0864b3eSthorpej  * are met:
10a0864b3eSthorpej  * 1. Redistributions of source code must retain the above copyright
11a0864b3eSthorpej  *    notice, this list of conditions and the following disclaimer.
12a0864b3eSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
13a0864b3eSthorpej  *    notice, this list of conditions and the following disclaimer in the
14a0864b3eSthorpej  *    documentation and/or other materials provided with the distribution.
15a0864b3eSthorpej  *
16a0864b3eSthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a0864b3eSthorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a0864b3eSthorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a0864b3eSthorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a0864b3eSthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21a0864b3eSthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22a0864b3eSthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23a0864b3eSthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24a0864b3eSthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25a0864b3eSthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26a0864b3eSthorpej  */
27a0864b3eSthorpej 
28a0864b3eSthorpej /*
29a0864b3eSthorpej  * Raw file system - for stream devices like tapes.
30a0864b3eSthorpej  * No random access, only sequential read allowed.
31a0864b3eSthorpej  * This exists only to allow upper level code to be
32a0864b3eSthorpej  * shielded from the fact that the device must be
33a0864b3eSthorpej  * read only with whole block position and size.
34a0864b3eSthorpej  */
35a0864b3eSthorpej 
36a0864b3eSthorpej #include <sys/param.h>
37a0864b3eSthorpej 
38a0864b3eSthorpej #include <lib/libsa/stand.h>
39a0864b3eSthorpej #include <hp300/stand/common/rawfs.h>
40a0864b3eSthorpej 
41a0864b3eSthorpej extern int debug;
42a0864b3eSthorpej 
43a0864b3eSthorpej /* Our devices are generally willing to do 8K transfers. */
44a0864b3eSthorpej #define	RAWFS_BSIZE	0x2000
45a0864b3eSthorpej 
46a0864b3eSthorpej /*
47a0864b3eSthorpej  * In-core open file.
48a0864b3eSthorpej  */
49a0864b3eSthorpej struct rawfs_file {
50a0864b3eSthorpej 	daddr_t		fs_nextblk;	/* block number to read next */
51a0864b3eSthorpej 	int		fs_len;		/* amount left in f_buf */
52a0864b3eSthorpej 	char *		fs_ptr;		/* read pointer into f_buf */
53a0864b3eSthorpej 	char		fs_buf[RAWFS_BSIZE];
54a0864b3eSthorpej };
55a0864b3eSthorpej 
56212f884fStsutsui static int rawfs_get_block(struct open_file *);
57a0864b3eSthorpej 
58a0864b3eSthorpej int
rawfs_open(const char * path,struct open_file * f)59d3dc0553Stsutsui rawfs_open(const char *path, struct open_file *f)
60a0864b3eSthorpej {
61a0864b3eSthorpej 	struct rawfs_file *fs;
62a0864b3eSthorpej 
63a0864b3eSthorpej 	/*
64a0864b3eSthorpej 	 * The actual tape driver has already been opened.
65a0864b3eSthorpej 	 * Just allocate the I/O buffer, etc.
66a0864b3eSthorpej 	 */
67a0864b3eSthorpej 	fs = alloc(sizeof(struct rawfs_file));
68a0864b3eSthorpej 	fs->fs_nextblk = 0;
69a0864b3eSthorpej 	fs->fs_len = 0;
70a0864b3eSthorpej 	fs->fs_ptr = fs->fs_buf;
71a0864b3eSthorpej 
72a0864b3eSthorpej #ifdef	DEBUG_RAWFS
73a0864b3eSthorpej 	printf("rawfs_open: fs=0x%x\n", (u_long)fs);
74a0864b3eSthorpej #endif
75a0864b3eSthorpej 
76a0864b3eSthorpej 	f->f_fsdata = fs;
77212f884fStsutsui 	return 0;
78a0864b3eSthorpej }
79a0864b3eSthorpej 
80a0864b3eSthorpej int
rawfs_close(struct open_file * f)81d3dc0553Stsutsui rawfs_close(struct open_file *f)
82a0864b3eSthorpej {
83a0864b3eSthorpej 	struct rawfs_file *fs;
84a0864b3eSthorpej 
85a0864b3eSthorpej 	fs = (struct rawfs_file *) f->f_fsdata;
86a0864b3eSthorpej 	f->f_fsdata = (void *)0;
87a0864b3eSthorpej 
88a0864b3eSthorpej #ifdef	DEBUG_RAWFS
89a0864b3eSthorpej 	printf("rawfs_close: fs=0x%x\n", (u_long)fs);
90a0864b3eSthorpej #endif
91a0864b3eSthorpej 
92a0864b3eSthorpej 	if (fs != (struct rawfs_file *)0)
93606bb2caSchristos 		dealloc(fs, sizeof(*fs));
94a0864b3eSthorpej 
95212f884fStsutsui 	return 0;
96a0864b3eSthorpej }
97a0864b3eSthorpej 
98a0864b3eSthorpej int
rawfs_read(struct open_file * f,void * start,u_int size,u_int * resid)99d3dc0553Stsutsui rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
100a0864b3eSthorpej {
101a0864b3eSthorpej 	struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
102a0864b3eSthorpej 	char *addr = start;
103a0864b3eSthorpej 	int error = 0;
104a0864b3eSthorpej 	size_t csize;
105a0864b3eSthorpej 
106a0864b3eSthorpej #ifdef DEBUG_RAWFS
107a0864b3eSthorpej 	printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
108a0864b3eSthorpej 	    (u_long)f, (u_long)start, size, resid);
109a0864b3eSthorpej 	printf("            fs=0x%x\n", (u_long)fs);
110a0864b3eSthorpej #endif
111a0864b3eSthorpej 
112a0864b3eSthorpej 	while (size != 0) {
113a0864b3eSthorpej 
114a0864b3eSthorpej 		if (fs->fs_len == 0)
115a0864b3eSthorpej 			if ((error = rawfs_get_block(f)) != 0)
116a0864b3eSthorpej 				break;
117a0864b3eSthorpej 
118a0864b3eSthorpej 		if (fs->fs_len <= 0)
119a0864b3eSthorpej 			break;	/* EOF */
120a0864b3eSthorpej 
121a0864b3eSthorpej 		csize = size;
122a0864b3eSthorpej 		if (csize > fs->fs_len)
123a0864b3eSthorpej 			csize = fs->fs_len;
124a0864b3eSthorpej 
125212f884fStsutsui 		memcpy(addr, fs->fs_ptr, csize);
126a0864b3eSthorpej 		fs->fs_ptr += csize;
127a0864b3eSthorpej 		fs->fs_len -= csize;
128a0864b3eSthorpej 		addr += csize;
129a0864b3eSthorpej 		size -= csize;
130a0864b3eSthorpej 	}
131a0864b3eSthorpej 	if (resid)
132a0864b3eSthorpej 		*resid = size;
133212f884fStsutsui 	return error;
134a0864b3eSthorpej }
135a0864b3eSthorpej 
136a0864b3eSthorpej int
rawfs_write(struct open_file * f,void * start,size_t size,size_t * resid)137d3dc0553Stsutsui rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
138a0864b3eSthorpej {
139d3dc0553Stsutsui 
140a0864b3eSthorpej #ifdef	DEBUG_RAWFS
141a0864b3eSthorpej 	printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
142a0864b3eSthorpej #endif
143212f884fStsutsui 	return EROFS;
144a0864b3eSthorpej }
145a0864b3eSthorpej 
146a0864b3eSthorpej off_t
rawfs_seek(struct open_file * f,off_t offset,int where)147d3dc0553Stsutsui rawfs_seek(struct open_file *f, off_t offset, int where)
148a0864b3eSthorpej {
149212f884fStsutsui 
150a0864b3eSthorpej #ifdef	DEBUG_RAWFS
151a0864b3eSthorpej 	printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
152a0864b3eSthorpej #endif
153212f884fStsutsui 	return EFTYPE;
154a0864b3eSthorpej }
155a0864b3eSthorpej 
156a0864b3eSthorpej int
rawfs_stat(struct open_file * f,struct stat * sb)157d3dc0553Stsutsui rawfs_stat(struct open_file *f, struct stat *sb)
158a0864b3eSthorpej {
159212f884fStsutsui 
160a0864b3eSthorpej #ifdef	DEBUG_RAWFS
161a0864b3eSthorpej 	printf("rawfs_stat: I'll let you live only because of exec.c\n");
162a0864b3eSthorpej #endif
163a0864b3eSthorpej 	/*
164a0864b3eSthorpej 	 * Clear out the stat buffer so that the uid check
165a0864b3eSthorpej 	 * won't fail.  See sys/lib/libsa/exec.c
166a0864b3eSthorpej 	 */
167212f884fStsutsui 	memset(sb, 0, sizeof(*sb));
168a0864b3eSthorpej 
169212f884fStsutsui 	return EFTYPE;
170a0864b3eSthorpej }
171a0864b3eSthorpej 
172a0864b3eSthorpej /*
173a0864b3eSthorpej  * Read a block from the underlying stream device
174a0864b3eSthorpej  * (In our case, a tape drive.)
175a0864b3eSthorpej  */
176a0864b3eSthorpej static int
rawfs_get_block(struct open_file * f)177d3dc0553Stsutsui rawfs_get_block(struct open_file *f)
178a0864b3eSthorpej {
179a0864b3eSthorpej 	struct rawfs_file *fs;
1805845cc2eStsutsui 	int error;
1815845cc2eStsutsui 	size_t len;
182a0864b3eSthorpej 
183a0864b3eSthorpej 	fs = (struct rawfs_file *)f->f_fsdata;
184a0864b3eSthorpej 	fs->fs_ptr = fs->fs_buf;
185a0864b3eSthorpej 
186a0864b3eSthorpej 	twiddle();
187a0864b3eSthorpej #ifdef DEBUG_RAWFS
188a0864b3eSthorpej 	printf("rawfs_get_block: calling strategy\n");
189a0864b3eSthorpej #endif
190a0864b3eSthorpej 	error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
191a0864b3eSthorpej 		fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
192a0864b3eSthorpej #ifdef DEBUG_RAWFS
193a0864b3eSthorpej 	printf("rawfs_get_block: strategy returned %d\n", error);
194a0864b3eSthorpej #endif
195a0864b3eSthorpej 
196a0864b3eSthorpej 	if (!error) {
197a0864b3eSthorpej 		fs->fs_len = len;
198a0864b3eSthorpej 		fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
199a0864b3eSthorpej 	}
200a0864b3eSthorpej 
201212f884fStsutsui 	return error;
202a0864b3eSthorpej }
203