summaryrefslogtreecommitdiff
path: root/sys/arch/mvme88k/stand/tftpboot/tftpfs.c
blob: 42cef91e5839719e34da83288a91f703f5f9824a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*	$OpenBSD: tftpfs.c,v 1.1 2004/01/26 19:48:34 miod Exp $	*/

/*-
 * Copyright (c) 2001 Steve Murphree, Jr.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by TooLs GmbH.
 * 4. The name of TooLs GmbH may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *	TFTP file system.
 */

#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <ufs/ffs/fs.h>
#include <lib/libkern/libkern.h>

#include "stand.h"

/*
 * In-core open file.
 */
struct tftp_file {
	char 		filename[128];
	off_t		f_seekp;	/* seek pointer */
	char		*f_buf;		/* buffer for data block */
	off_t		f_off;		/* index into buffer for data block */
	daddr_t		f_buf_blkno;	/* block number of data block */
	size_t		f_buf_size;
};

#define TFTP_BLOCK_SHIFT	9
#define TFTP_BLOCK_SIZE		(1<<TFTP_BLOCK_SHIFT) /* 512 by tftp convention */
#define TFTP_BLOCK_NO(x)	((x >> TFTP_BLOCK_SHIFT) + 1)
#define TFTP_BLOCK_OFF(x)	(x % TFTP_BLOCK_SIZE)

static int	read_inode(ino_t, struct open_file *);
static int	block_map(struct open_file *, daddr_t, daddr_t *);
static int	tftp_read_file(struct open_file *, char **, size_t *);
#ifdef COMPAT_UFS
static void	ffs_oldfscompat(struct fs *);
#endif

/*
 * Read a portion of a file into an internal buffer.  Return
 * the location in the buffer and the amount in the buffer.
 */

char	tftp_buf[TFTP_BLOCK_SIZE];	/* static */
struct	tftp_file tftp_ctrl;

static int
tftp_read_file(f, buf_p, size_p)
	struct open_file *f;
	char **buf_p;		/* out */
	size_t *size_p;		/* out */
{
	register struct tftp_file *fp = (struct tftp_file *)f->f_fsdata;
	long off;
	register daddr_t file_block;
	size_t block_size;
	int i, rc;

	off = TFTP_BLOCK_OFF(fp->f_seekp);
	file_block = TFTP_BLOCK_NO(fp->f_seekp);
	block_size = TFTP_BLOCK_SIZE;

	if (file_block == fp->f_buf_blkno + 1) {
		/* 
		 * Normal, incremental block transfer.
		 */
		rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
			file_block, block_size, fp->f_buf, &fp->f_buf_size);
		if (rc)
			return (rc);
		if (!(file_block % 4)) /* twiddle every 4 blocks */
			twiddle();
		fp->f_buf_blkno = file_block;
	} else if (file_block > fp->f_buf_blkno + 1) {
		/* 
		 * Read ahead to the requested block;  If we need  
		 * those we skipped, see below.
		 */
		for (i = (fp->f_buf_blkno + 1); i <= file_block; i++) {
			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
				i, block_size, fp->f_buf, &fp->f_buf_size);
			if (rc)
				return (rc);
		}
		fp->f_buf_blkno = file_block;
	} else if (file_block < fp->f_buf_blkno) {
		/* 
		 * Uh oh...  We can't rewind.  Reopen the file 
		 * and start again.
		 */
		char filename[64];
		strlcpy(filename, fp->filename, sizeof filename);
                tftpfs_close(f);
                tftpfs_open(filename, f);
		for (i = 1; i <= file_block; i++) {
			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
				i, block_size, fp->f_buf, &fp->f_buf_size);
			if (rc)
				return (rc);
		}
		fp->f_buf_blkno = file_block;
	}

	/*
	 * Return address of byte in buffer corresponding to
	 * offset, and size of remainder of buffer after that
	 * byte.
	 */
	*buf_p = fp->f_buf + off;
	*size_p = fp->f_buf_size - off;

	/*
	 * But truncate buffer at end of file.
	 */
	if (fp->f_buf_size > block_size){
		twiddle();
		return(EIO);
	}


	return (0);
}

/*
 * Open a file.
 */
int
tftpfs_open(path, f)
	char *path;
	struct open_file *f;
{
	struct tftp_file *fp;
	int rc = 0;
	
	/* locate file system specific data structure and zero it.*/
	fp = &tftp_ctrl;
	bzero(fp, sizeof(struct tftp_file));
	f->f_fsdata = (void *)fp;
	fp->f_seekp = 0;
	fp->f_buf = tftp_buf;
	bzero(fp->f_buf, TFTP_BLOCK_SIZE);
	fp->f_buf_size = 0;

        strlcpy(fp->filename, path, sizeof fp->filename);
	
	if (f->f_dev->dv_open == NULL) {
		panic("No device open()!");
	}
	twiddle();
        rc = (f->f_dev->dv_open)(f, path);
	return (rc);
}

int
tftpfs_close(f)
	struct open_file *f;
{
	register struct tftp_file *fp = (struct tftp_file *)f->f_fsdata;

	fp->f_buf = (void *)0;
	f->f_fsdata = (void *)0;
        (f->f_dev->dv_close)(f);
	return (0);
}

/*
 * Copy a portion of a file into kernel memory.
 * Cross block boundaries when necessary.
 */
int
tftpfs_read(f, start, size, resid)
	struct open_file *f;
	void *start;
	size_t size;
	size_t *resid;	/* out */
{
	register struct tftp_file *fp = (struct tftp_file *)f->f_fsdata;
	register size_t csize;
	char *buf;
	size_t buf_size;
	int rc = 0;
	register char *addr = start;

	while (size != 0) {
		rc = tftp_read_file(f, &buf, &buf_size);
		if (rc)
			break;

		csize = size;
		if (csize > buf_size)
			csize = buf_size;

		bcopy(buf, addr, csize);

		fp->f_seekp += csize;
		addr += csize;
		size -= csize;
	}
	if (resid)
		*resid = size;
	return (rc);
}

/*
 * Not implemented.
 */
int
tftpfs_write(f, start, size, resid)
	struct open_file *f;
	void *start;
	size_t size;
	size_t *resid;	/* out */
{

	return (EROFS);
}

/*
 * We only see forward.  We can't rewind.
 */
off_t
tftpfs_seek(f, offset, where)
	struct open_file *f;
	off_t offset;
	int where;
{
	register struct tftp_file *fp = (struct tftp_file *)f->f_fsdata;

	switch (where) {
	case SEEK_SET:
		fp->f_seekp = offset;
		break;
	case SEEK_CUR:
		fp->f_seekp += offset;
		break;
	case SEEK_END:
		errno = EIO;
		return (-1);
		break;
	default:
		return (-1);
	}
	return (fp->f_seekp);
}

int
tftpfs_stat(f, sb)
	struct open_file *f;
	struct stat *sb;
{
	return EIO;
}

#ifndef NO_READDIR
int
tftpfs_readdir (struct open_file *f, char *name)
{
	return EIO;
}
#endif