-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneotron.h
More file actions
468 lines (448 loc) · 10.5 KB
/
Copy pathneotron.h
File metadata and controls
468 lines (448 loc) · 10.5 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Header file for the Neoton OS API.
*
* Copyright (c) 2023 Jonathan Pallant and the Neotron Developers
*
* This file is licensed under the MIT or Apache 2.0 licences, at your option.
*/
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* Maximum length of a filename (with no directory components), including the
* extension.
*/
#define MAX_FILENAME_LEN 11
/**
* The character that separates one directory name from another directory name.
*/
#define Path_PATH_SEP '/'
/**
* The character that separates drive specifiers from directories.
*/
#define Path_DRIVE_SEP ':'
/**
* Describes how something has failed
*/
typedef enum Error
{
/**
* The given file/directory path was not found
*/
NotFound,
/**
* Tried to write to a read-only file
*/
FileReadOnly,
/**
* Reached the end of the file
*/
EndOfFile,
/**
* The API has not been implemented
*/
Unimplemented,
/**
* An invalid argument was given to the API
*/
InvalidArg,
/**
* A bad handle was given to the API
*/
BadHandle,
/**
* An device-specific error occurred. Look at the BIOS source for more details.
*/
DeviceSpecific,
/**
* The OS does not have enough memory
*/
OutOfMemory,
/**
* The given path was invalid
*/
InvalidPath,
} Error;
/**
* Represents an open directory
*/
typedef struct Handle
{
uint8_t _0;
} Handle;
typedef enum FfiResult_Tag
{
/**
* The operation succeeded (like [`core::result::Result::Ok`]).
*/
FfiResult_Ok,
/**
* The operation failed (like [`core::result::Result::Err`]).
*/
FfiResult_Err,
} FfiResult_Tag;
typedef struct FfiResult_Handle
{
FfiResult_Tag tag;
union
{
struct
{
struct Handle ok;
};
struct
{
enum Error err;
};
};
} FfiResult_Handle;
/**
* A Rust u8 slice, but compatible with FFI. Assume the lifetime is only valid
* until the callee returns to the caller.
*/
typedef struct FfiByteSlice
{
/**
* A pointer to the data
*/
const uint8_t *data;
/**
* The number of bytes we are pointing at
*/
uintptr_t data_len;
} FfiByteSlice;
/**
* A Rust UTF-8 string, but compatible with FFI.
*
* Assume the lifetime is only valid until the callee returns to the caller. Is
* not null-terminated.
*/
typedef struct FfiString
{
struct FfiByteSlice _0;
} FfiString;
typedef struct FfiResult_usize
{
FfiResult_Tag tag;
union
{
struct
{
uintptr_t ok;
};
struct
{
enum Error err;
};
};
} FfiResult_usize;
/**
* A Rust u8 mutable slice, but compatible with FFI. Assume the lifetime is
* only valid until the callee returns to the caller.
*/
typedef struct FfiBuffer
{
/**
* A pointer to where the data can be put
*/
uint8_t *data;
/**
* The maximum number of bytes we can store in this buffer
*/
uintptr_t data_len;
} FfiBuffer;
typedef struct FfiResult_u64
{
FfiResult_Tag tag;
union
{
struct
{
uint64_t ok;
};
struct
{
enum Error err;
};
};
} FfiResult_u64;
/**
* Represents an instant in time, in the local time zone.
*/
typedef struct Time
{
/**
* Add 1970 to this file to get the calendar year
*/
uint8_t year_since_1970;
/**
* Add one to this value to get the calendar month
*/
uint8_t zero_indexed_month;
/**
* Add one to this value to get the calendar day
*/
uint8_t zero_indexed_day;
/**
* The number of hours past midnight
*/
uint8_t hours;
/**
* The number of minutes past the hour
*/
uint8_t minutes;
/**
* The number of seconds past the minute
*/
uint8_t seconds;
} Time;
/**
* Describes a file on disk.
*
* This is set up for 8.3 filenames on MS-DOS FAT32 partitions currently.
*/
typedef struct Stat
{
/**
* How big is this file
*/
uint64_t file_size;
/**
* When was the file created
*/
struct Time ctime;
/**
* When was the last modified
*/
struct Time mtime;
/**
* File attributes (Directory, Volume, etc)
*/
uint8_t attr;
} Stat;
/**
* Describes an entry in a directory.
*
* This is set up for 8.3 filenames on MS-DOS FAT32 partitions currently.
*/
typedef struct Entry
{
/**
* The name and extension of the file.
*
* The name and extension are separated by a single '.'.
*
* The filename will be in ASCII. Unicode filenames are not supported.
*/
uint8_t name[MAX_FILENAME_LEN];
/**
* The properties for the file/directory this entry represents.
*/
struct Stat properties;
} Entry;
typedef struct FfiResult_Entry
{
FfiResult_Tag tag;
union
{
struct
{
struct Entry ok;
};
struct
{
enum Error err;
};
};
} FfiResult_Entry;
typedef struct FfiResult_Stat
{
FfiResult_Tag tag;
union
{
struct
{
struct Stat ok;
};
struct
{
enum Error err;
};
};
} FfiResult_Stat;
typedef struct FfiResult_void
{
FfiResult_Tag tag;
union
{
struct
{
enum Error err;
};
};
} FfiResult_void;
/**
* The syscalls provided by the Neotron OS to a Neotron Application.
*/
typedef struct NeotronApi
{
/**
* Open a file, given a path as UTF-8 string.
*
* If the file does not exist, or is already open, it returns an error.
*
* Path may be relative to current directory, or it may be an absolute
* path.
*
* # Limitations
*
* * You cannot open a file if it is currently open.
* * Paths must confirm to the rules for the filesystem for the given drive.
* * Relative paths are taken relative to the current directory (see `Api::chdir`).
*/
struct FfiResult_Handle (*open)(struct FfiString path, uint8_t flags);
/**
* Close a previously opened file.
*
* Closing a file is important, as only this action will cause the
* directory entry for the file to be updated. Crashing the system without
* closing a file may cause the directory entry to be incorrect, and you
* may need to run `CHKDSK` (or similar) on your disk to fix it.
*/
struct FfiResult_void (*close)(struct Handle fd);
/**
* Write to an open file handle, blocking until everything is written.
*
* Some files do not support writing and will produce an error. You will
* also get an error if you run out of disk space.
*
* The `buffer` is only borrowed for the duration of the function call and
* is then forgotten.
*/
struct FfiResult_void (*write)(struct Handle fd, struct FfiByteSlice buffer);
/**
* Read from an open file, returning how much was actually read.
*
* You might get less data than you asked for. If you do an `Api::read` and
* you are already at the end of the file you will get
* `Err(Error::EndOfFile)`.
*
* Data is stored to the given `buffer. The `buffer` is only borrowed for
* the duration of the function call and is then forgotten.
*/
struct FfiResult_usize (*read)(struct Handle fd, struct FfiBuffer buffer);
/**
* Move the file offset (for the given file handle) to the given position.
*
* Some files do not support seeking and will produce an error.
*/
struct FfiResult_void (*seek_set)(struct Handle fd, uint64_t position);
/**
* Move the file offset (for the given file handle) relative to the current position.
*
* Returns the new file offset.
*
* Some files do not support seeking and will produce an error.
*/
struct FfiResult_u64 (*seek_cur)(struct Handle fd, int64_t offset);
/**
* Move the file offset (for the given file handle) to the end of the file
*
* Returns the new file offset.
*
* Some files do not support seeking and will produce an error.
*/
struct FfiResult_u64 (*seek_end)(struct Handle fd);
/**
* Rename a file.
*
* # Limitations
*
* * You cannot rename a file if it is currently open.
* * You cannot rename a file where the `old_path` and the `new_path` are
* not on the same drive.
* * Paths must confirm to the rules for the filesystem for the given drive.
*/
struct FfiResult_void (*rename)(struct FfiString old_path, struct FfiString new_path);
/**
* Perform a special I/O control operation.
*/
struct FfiResult_u64 (*ioctl)(struct Handle fd, uint64_t command, uint64_t value);
/**
* Open a directory, given a path as a UTF-8 string.
*/
struct FfiResult_Handle (*opendir)(struct FfiString path);
/**
* Close a previously opened directory.
*/
struct FfiResult_void (*closedir)(struct Handle dir);
/**
* Read from an open directory
*/
struct FfiResult_Entry (*readdir)(struct Handle dir);
/**
* Get information about a file.
*/
struct FfiResult_Stat (*stat)(struct FfiString path);
/**
* Get information about an open file.
*/
struct FfiResult_Stat (*fstat)(struct Handle fd);
/**
* Delete a file.
*
* # Limitations
*
* * You cannot delete a file if it is currently open.
*/
struct FfiResult_void (*deletefile)(struct FfiString path);
/**
* Delete a directory.
*
* # Limitations
*
* * You cannot delete a root directory.
* * You cannot delete a directory that has any files or directories in it.
*/
struct FfiResult_void (*deletedir)(struct FfiString path);
/**
* Change the current directory.
*
* Relative file paths (e.g. passed to `Api::open`) are taken to be relative to the current directory.
*
* Unlike on MS-DOS, there is only one current directory for the whole
* system, not one per drive.
*/
struct FfiResult_void (*chdir)(struct FfiString path);
/**
* Change the current directory to the given open directory.
*
* Unlike on MS-DOS, there is only one current directory for the whole
* system, not one per drive.
*/
struct FfiResult_void (*dchdir)(struct Handle dir);
/**
* Get the current directory.
*
* The current directory is stored as UTF-8 into the given buffer. The
* function returns the number of bytes written to the buffer, or an error.
* If the function did not return an error, the buffer can be assumed to
* contain a valid file path. That path will not be null terminated.
*/
struct FfiResult_usize (*pwd)(struct FfiBuffer path);
/**
* Allocate some memory.
*
* * `size` - the number of bytes required
* * `alignment` - the returned address will have this alignment, or
* better. For example, pass `4` if you are allocating an array of `u32`.
*/
struct FfiResult_void (*malloc)(uintptr_t size, uintptr_t alignment);
/**
* Free some previously allocated memory.
*
* You must pass the same `size` and `alignment` values that you passed to `malloc`.
*/
void (*free)(void *ptr, uintptr_t size, uintptr_t alignment);
} NeotronApi;