-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathriscv_hostfs.c
More file actions
368 lines (312 loc) · 7.64 KB
/
riscv_hostfs.c
File metadata and controls
368 lines (312 loc) · 7.64 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/****************************************************************************
* arch/risc-v/src/common/riscv_hostfs.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/cache.h>
#include <nuttx/fs/hostfs.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define HOST_OPEN 0x01
#define HOST_CLOSE 0x02
#define HOST_WRITE 0x05
#define HOST_READ 0x06
#define HOST_SEEK 0x0a
#define HOST_FLEN 0x0c
#define HOST_REMOVE 0x0e
#define HOST_RENAME 0x0f
#define HOST_ERROR 0x13
/****************************************************************************
* Private Functions
****************************************************************************/
static long host_call(unsigned int nbr, void *parm, size_t size)
{
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(parm, parm + size);
#endif
long ret = smh_call(nbr, parm);
if (ret < 0)
{
long err = smh_call(HOST_ERROR, NULL);
if (err > 0)
{
ret = -err;
}
}
return ret;
}
static ssize_t host_flen(long fd)
{
return host_call(HOST_FLEN, &fd, sizeof(long));
}
static int host_flags_to_mode(int flags)
{
static const int modemasks = O_RDONLY | O_WRONLY | O_TEXT | O_RDWR |
O_CREAT | O_TRUNC | O_APPEND;
static const int modeflags[] =
{
O_RDONLY | O_TEXT,
O_RDONLY,
O_RDWR | O_TEXT,
O_RDWR,
O_WRONLY | O_CREAT | O_TRUNC | O_TEXT,
O_WRONLY | O_CREAT | O_TRUNC,
O_RDWR | O_CREAT | O_TRUNC | O_TEXT,
O_RDWR | O_CREAT | O_TRUNC,
O_WRONLY | O_CREAT | O_APPEND | O_TEXT,
O_WRONLY | O_CREAT | O_APPEND,
O_RDWR | O_CREAT | O_APPEND | O_TEXT,
O_RDWR | O_CREAT | O_APPEND,
0,
};
int i;
for (i = 0; modeflags[i] != 0; i++)
{
if ((modemasks & flags) == modeflags[i])
{
return i;
}
}
return -EINVAL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int host_open(const char *pathname, int flags, int mode)
{
struct
{
const char *pathname;
long mode;
size_t len;
} open =
{
.pathname = pathname,
.mode = host_flags_to_mode(flags),
.len = strlen(pathname),
};
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(pathname, pathname + open.len + 1);
#endif
return host_call(HOST_OPEN, &open, sizeof(open));
}
int host_close(int fd_)
{
long fd = fd_;
return host_call(HOST_CLOSE, &fd, sizeof(long));
}
ssize_t host_read(int fd, void *buf, size_t count)
{
struct
{
long fd;
void *buf;
size_t count;
} read =
{
.fd = fd,
.buf = buf,
.count = count,
};
ssize_t ret;
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_invalidate_dcache(buf, buf + count);
#endif
ret = host_call(HOST_READ, &read, sizeof(read));
return ret < 0 ? ret : count - ret;
}
ssize_t host_write(int fd, const void *buf, size_t count)
{
struct
{
long fd;
const void *buf;
size_t count;
} write =
{
.fd = fd,
.buf = buf,
.count = count,
};
ssize_t ret;
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(buf, buf + count);
#endif
ret = host_call(HOST_WRITE, &write, sizeof(write));
return ret < 0 ? ret : count - ret;
}
off_t host_lseek(int fd, off_t pos, off_t offset, int whence)
{
off_t ret = -ENOSYS;
if (whence == SEEK_END)
{
ret = host_flen(fd);
if (ret >= 0)
{
offset += ret;
whence = SEEK_SET;
}
}
else if (whence == SEEK_CUR)
{
offset += pos;
whence = SEEK_SET;
}
if (whence == SEEK_SET)
{
struct
{
long fd;
size_t pos;
} seek =
{
.fd = fd,
.pos = offset,
};
ret = host_call(HOST_SEEK, &seek, sizeof(seek));
if (ret >= 0)
{
ret = offset;
}
}
return ret;
}
int host_ioctl(int fd, int request, unsigned long arg)
{
return -ENOSYS;
}
void host_sync(int fd)
{
}
int host_dup(int fd)
{
return -ENOSYS;
}
int host_fstat(int fd, struct stat *buf)
{
memset(buf, 0, sizeof(*buf));
buf->st_mode = S_IFREG | 0777;
buf->st_size = host_flen(fd);
return buf->st_size < 0 ? buf->st_size : 0;
}
int host_fchstat(int fd, const struct stat *buf, int flags)
{
return -ENOSYS;
}
int host_ftruncate(int fd, off_t length)
{
return -ENOSYS;
}
void *host_opendir(const char *name)
{
return NULL;
}
int host_readdir(void *dirp, struct dirent *entry)
{
return -ENOSYS;
}
void host_rewinddir(void *dirp)
{
}
int host_closedir(void *dirp)
{
return -ENOSYS;
}
int host_statfs(const char *path, struct statfs *buf)
{
return 0;
}
int host_unlink(const char *pathname)
{
struct
{
const char *pathname;
size_t pathname_len;
} remove =
{
.pathname = pathname,
.pathname_len = strlen(pathname),
};
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(pathname, pathname +
remove.pathname_len + 1);
#endif
return host_call(HOST_REMOVE, &remove, sizeof(remove));
}
int host_mkdir(const char *pathname, int mode)
{
return -ENOSYS;
}
int host_rmdir(const char *pathname)
{
return host_unlink(pathname);
}
int host_rename(const char *oldpath, const char *newpath)
{
struct
{
const char *oldpath;
size_t oldpath_len;
const char *newpath;
size_t newpath_len;
} rename =
{
.oldpath = oldpath,
.oldpath_len = strlen(oldpath),
.newpath = newpath,
.newpath_len = strlen(newpath),
};
#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(oldpath, oldpath + rename.oldpath_len + 1);
up_clean_dcache(newpath, newpath + rename.newpath_len + 1);
#endif
return host_call(HOST_RENAME, &rename, sizeof(rename));
}
int host_stat(const char *path, struct stat *buf)
{
int ret = host_open(path, O_RDONLY, 0);
if (ret >= 0)
{
int fd = ret;
ret = host_fstat(fd, buf);
host_close(fd);
}
if (ret < 0)
{
/* Since semihosting doesn't support directory yet, */
ret = 0; /* we have to assume it's a directory here. */
memset(buf, 0, sizeof(*buf));
buf->st_mode = S_IFDIR | 0777;
}
return ret;
}
int host_chstat(const char *path, const struct stat *buf, int flags)
{
return -ENOSYS;
}