-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdfs_yaffs_s.c
339 lines (266 loc) · 7.4 KB
/
dfs_yaffs_s.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <rtthread.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#include "yaffs/yaffs_guts.h"
#include "yaffs/direct/yaffsfs.h"
#include "yaffs/direct/yaffs_flashif.h"
static int dfs_yfile_open(struct dfs_fd *file)
{
struct dfs_filesystem *fs;
struct yaffs_obj *obj;
int fd;
int oflag;
int result;
fs = (struct dfs_filesystem *)file->data;
obj = (struct yaffs_obj *)fs->data;
oflag = file->flags;
if (oflag & O_DIRECTORY)
{
yaffs_DIR *dir;
if (oflag & O_CREAT)
{
result = yaffs_mkdir_reldir(obj, file->path, 0x777);
if (result < 0)
return yaffsfs_GetLastError();
}
/* open dir */
dir = yaffs_opendir_reldir(obj, file->path);
if (dir == RT_NULL)
return yaffsfs_GetLastError();
/* save this pointer,will used by dfs_yaffs_getdents*/
file->data = dir;
return 0;
}
/* regular file operations */
fd = yaffs_open_reldir(obj, file->path, oflag, S_IREAD | S_IWRITE);
if (fd < 0)
return yaffsfs_GetLastError();
file->data = (void *)fd;
file->pos = yaffs_lseek(fd, 0, SEEK_CUR);
file->size = yaffs_lseek(fd, 0, SEEK_END);
yaffs_lseek(fd, file->pos, SEEK_SET);
if (oflag & O_APPEND)
{
file->pos = file->size;
file->size = yaffs_lseek(fd, 0, SEEK_END);
}
return 0;
}
static int dfs_yfile_close(struct dfs_fd *file)
{
int oflag;
int fd;
oflag = file->flags;
if (oflag & O_DIRECTORY) /* operations about dir */
{
if (yaffs_closedir((yaffs_DIR *)(file->data)) < 0)
return yaffsfs_GetLastError();
return 0;
}
/* regular file operations */
fd = (int)(file->data);
if (yaffs_close(fd) == 0)
return 0;
/* release memory */
return yaffsfs_GetLastError();
}
static int dfs_yfile_ioctl(struct dfs_fd *file, int cmd, void *args)
{
return -ENOSYS;
}
static int dfs_yfile_read(struct dfs_fd *file, void *buf, size_t len)
{
int fd;
int char_read;
fd = (int)(file->data);
char_read = yaffs_read(fd, buf, len);
if (char_read < 0)
return yaffsfs_GetLastError();
/* update position */
file->pos = yaffs_lseek(fd, 0, SEEK_CUR);
return char_read;
}
static int dfs_yfile_write(struct dfs_fd *file, const void *buf, size_t len)
{
int fd;
int char_write;
fd = (int)(file->data);
char_write = yaffs_write(fd, buf, len);
if (char_write < 0)
return yaffsfs_GetLastError();
/* update position */
file->pos = yaffs_lseek(fd, 0, SEEK_CUR);
return char_write;
}
static int dfs_yfile_flush(struct dfs_fd *file)
{
int fd;
int result;
fd = (int)(file->data);
result = yaffs_flush(fd);
if (result < 0)
return yaffsfs_GetLastError();
return 0;
}
static int dfs_yfile_lseek(struct dfs_fd *file, rt_off_t offset)
{
int fd;
int result;
fd = (int)(file->data);
/* set offset as current offset */
result = yaffs_lseek(fd, offset, SEEK_SET);
if (result < 0)
return yaffsfs_GetLastError();
return result;
}
static int dfs_yfile_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
{
rt_uint32_t index;
struct dirent *d;
yaffs_DIR *dir;
struct yaffs_dirent *yaffs_d;
dir = (yaffs_DIR *)(file->data);
RT_ASSERT(dir != RT_NULL);
/* make integer count, usually count is 1 */
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
if (count == 0)
return -EINVAL;
index = 0;
/* usually, the while loop should only be looped only once! */
while (1)
{
d = dirp + index;
yaffs_d = yaffs_readdir(dir);
if (yaffs_d == RT_NULL)
{
if (yaffsfs_GetLastError() == EBADF)
return -EBADF;
return -1; /* a general error */
}
/* write the rest feilds of struct dirent* dirp */
d->d_namlen = rt_strlen(yaffs_d->d_name);
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, yaffs_d->d_name, rt_strlen(yaffs_d->d_name) + 1);
index++;
if (index * sizeof(struct dirent) >= count)
break;
}
if (index == 0)
return yaffsfs_GetLastError();
return index * sizeof(struct dirent);
}
static int dfs_yaffs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
if (yaffs_mount(fs->path) < 0)
return yaffsfs_GetLastError();
return 0;
}
static int dfs_yaffs_unmount(struct dfs_filesystem *fs)
{
if (yaffs_unmount(fs->path) < 0)
return yaffsfs_GetLastError();
return 0;
}
static int dfs_yaffs_mkfs(rt_device_t dev_id)
{
extern int yaffs_format_reldev(struct yaffs_dev * dev,
int unmount_flag,
int force_unmount_flag,
int remount_flag);
rt_mtd_nand_t mtd;
mtd = (rt_mtd_nand_t)dev_id;
RT_ASSERT(mtd);
if (!mtd->priv)
return -1;
return yaffs_format_reldev((struct yaffs_dev *)mtd->priv, 1, 1, 1);
}
static int dfs_yaffs_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{
rt_mtd_nand_t mtd;
RT_ASSERT(fs);
mtd = (rt_mtd_nand_t)fs->dev_id;
RT_ASSERT(mtd);
buf->f_bsize = mtd->page_size;
buf->f_blocks = mtd->block_end - mtd->block_start;
buf->f_bfree = yaffs_freespace_reldev(yaffs_getdev(fs->path)) / mtd->page_size;
return 0;
}
static int dfs_yaffs_unlink(struct dfs_filesystem *fs, const char *path)
{
int result;
struct yaffs_stat s;
/* judge file type, dir is to be delete by yaffs_rmdir, others by yaffs_unlink */
if (yaffs_lstat_reldir(fs->data, path, &s) < 0)
{
return yaffsfs_GetLastError();
}
switch (s.st_mode & S_IFMT)
{
case S_IFREG:
result = yaffs_unlink_reldir(fs->data, path);
break;
case S_IFDIR:
result = yaffs_rmdir_reldir(fs->data, path);
break;
default:
/* unknown file type */
return -1;
}
if (result < 0)
return yaffsfs_GetLastError();
return 0;
}
static int dfs_yaffs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
int result;
struct yaffs_stat s;
result = yaffs_stat_reldir(fs->data, path, &s);
if (result < 0)
return yaffsfs_GetLastError();
/* convert to dfs stat structure */
st->st_dev = 0;
st->st_mode = s.st_mode;
st->st_size = s.st_size;
st->st_mtime = s.yst_mtime;
return 0;
}
static int dfs_yaffs_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
{
int result;
result = yaffs_rename_reldir(fs->data, oldpath, newpath);
if (result < 0)
return yaffsfs_GetLastError();
return 0;
}
static const struct dfs_file_ops _fops =
{
dfs_yfile_open,
dfs_yfile_close,
dfs_yfile_ioctl,
dfs_yfile_read,
dfs_yfile_write,
dfs_yfile_flush,
dfs_yfile_lseek,
dfs_yfile_getdents,
RT_NULL, /* poll interface */
};
static const struct dfs_filesystem_ops dfs_yaffs_ops =
{
"yaffs",
DFS_FS_FLAG_FULLPATH,
&_fops,
dfs_yaffs_mount,
dfs_yaffs_unmount,
dfs_yaffs_mkfs,
dfs_yaffs_statfs,
dfs_yaffs_unlink,
dfs_yaffs_stat,
dfs_yaffs_rename,
};
int dfs_yaffs_init(void)
{
/* Register yaffs file system */
yaffsfs_OSInitialisation();
return dfs_register(&dfs_yaffs_ops);
}
INIT_COMPONENT_EXPORT(dfs_yaffs_init);