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
|
//==========================================================================
//
// lib/tftp_dummy_file.c
//
// Dummy [in memory] file I/O functions
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): gthomas
// Contributors: gthomas
// Date: 2000-04-06
// Purpose:
// Description:
//
//
//####DESCRIPTIONEND####
//
//==========================================================================
// TFTP file support - minimal "dummy" version
#include <network.h>
#include <tftp_support.h>
static int dummy_open(const char *, int);
static int dummy_close(int);
static int dummy_write(int, const void *, int);
static int dummy_read(int, void *, int);
struct tftpd_fileops dummy_fileops = {
dummy_open, dummy_close, dummy_write, dummy_read
};
struct _file_info;
struct _file {
unsigned char *pos, *eof;
int flags;
int mode;
struct _file_info *file;
};
#define FILE_OPEN 0x0001
#define NUM_FILES 8
static struct _file files[NUM_FILES];
struct _file_info {
char *name;
unsigned char *data;
int length, size;
};
// TEMP
static unsigned char _uu_data[] = "This is a test\n\
Four score and seven years ago,\n\
our forefathers brought forth a new nation,\n\
conceived in liberty and dedicated to the\n\
proposition that all men are created equal.\n\
Now we are engaged in a great civil war, testing\n\
whether that nation, or any nation so conceived\n\
and so dedicated, can long endure.\n\
1111111111111111111111111111111111\n\
2222222222222222222222222222222222\n\
3333333333333333333333333333333333\n\
4444444444444444444444444444444444\n\
5555555555555555555555555555555555\n\
6666666666666666666666666666666666\n\
";
static unsigned char _f0_data[1024*1024];
static unsigned char _f1_data[1024*1024];
static unsigned char _f2_data[1024*1024];
static char _name0[256] = "", _name1[256] = "", _name2[256] = "";
static struct _file_info file_list[] = {
{ "uu", _uu_data, sizeof(_uu_data)-1, sizeof(_uu_data)-1},
{ _name0, _f0_data, 0, sizeof(_f0_data)}, // Empty file
{ _name1, _f1_data, 0, sizeof(_f1_data)}, // Empty file
{ _name2, _f2_data, 0, sizeof(_f2_data)}, // Empty file
{ 0, 0, 0} // End of list
};
static inline struct _file *
dummy_fp(int fd)
{
struct _file *fp;
if ((fd < 0) || (fd >= NUM_FILES)) return (struct _file *)0;
fp = &files[fd];
if (!(fp->flags & FILE_OPEN)) return (struct _file *)0;
return fp;
}
static int
dummy_open(const char *fn, int flags)
{
int res = -1;
int fd;
struct _file *fp;
struct _file_info *fi;
fp = files;
for (fd = 0; fd < NUM_FILES; fd++, fp++) {
if (!(fp->flags & FILE_OPEN)) break;
}
if (fd == NUM_FILES) {
return -1; // No free files
}
if (flags & O_RDONLY) {
// Search for an extant file
fi = file_list;
while (fi->name) {
if (strcmp(fi->name, fn) == 0) {
// Found it!
fp->pos = fi->data;
fp->eof = fi->data + fi->length;
fp->flags = FILE_OPEN;
fp->mode = flags;
return fd;
}
fi++;
}
} else {
// Search for a non-existant file
fi = file_list;
while (fi->name) {
if (fi->name[0] == '\0' || strcmp(fi->name, fn) == 0) {
if ( !fi->name[0] )
// Empty slot found
strcpy(fi->name, fn);
fp->pos = fi->data;
fp->eof = fi->data + fi->size;
fp->file = fi; // So we can update file info later
fp->mode = flags;
fp->flags = FILE_OPEN;
return fd;
}
fi++;
}
}
// No such file
return res;
}
static int
dummy_close(int fd)
{
struct _file *fp = dummy_fp(fd);
if (!fp) return -1;
if (fp->mode & O_WRONLY) {
// Clean up - update file info for later
fp->file->length = fp->pos - fp->file->data;
}
fp->flags = 0; // No longer open
return 0;
}
static int
dummy_write(int fd, const void *buf, int len)
{
struct _file *fp = dummy_fp(fd);
int res;
if (!fp) return -1;
res = fp->eof - fp->pos; // Space left in file
if (res <= 0) return 0; // End of file
if (res > len) res = len;
bcopy(buf, fp->pos, res);
fp->pos += res;
return res;
}
static int
dummy_read(int fd, void *buf, int len)
{
struct _file *fp = dummy_fp(fd);
int res;
if (!fp) return -1;
res = fp->eof - fp->pos; // Number of bytes left in "file"
if (res > len) res = len;
if (res <= 0) return 0; // End of file
bcopy(fp->pos, buf, res);
fp->pos += res;
return res;
}
// EOF tftp_dummy_file.c
|