3 #include "../../Usermode/include/acess/sys.h"
17 const char *ReadEntry(tOutValue **OutDest, tInValue **InDest,
18 int *Direction, const char *ArgTypes, va_list Args)
20 uint64_t val64, *ptr64;
21 uint32_t val32, *ptr32;
22 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
27 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
28 if( *ArgTypes == '\0' ) return ArgTypes;
33 case '>': direction = 1; break;
34 case '<': direction = 2; break;
35 case '?': direction = 3; break;
42 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
43 if( *ArgTypes == '\0' ) return ArgTypes;
45 // Internal helper macro
46 #define MAKE_OUT(_dest,_typeChar,_typeName,_value) do{if((_dest)){\
47 *(_dest) = (tOutValue*)malloc(sizeof(tOutValue)+sizeof(_typeName));\
48 (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\
49 *(_typeName*)((*(_dest))->Data) = (_value);\
51 #define MAKE_IN(_dest,_typeChar,_typeName,_value) do{if((_dest)){\
52 *(_dest) = (tInValue*)malloc(sizeof(tInValue));\
53 (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\
54 (*(_dest))->Data = (_value);\
60 case 'i': // 32-bit integer
64 ptr32 = va_arg(Args, uint32_t*);
65 MAKE_IN(InDest, 'i', uint32_t*, ptr32);
67 MAKE_OUT(OutDest, 'i', uint32_t, *ptr32);
71 val32 = va_arg(Args, uint32_t);
72 MAKE_OUT(OutDest, 'i', uint32_t, val32);
75 case 'I': // 64-bit integer
79 ptr64 = va_arg(Args, uint64_t*);
80 MAKE_IN(InDest, 'I', uint64_t*, ptr64);
82 MAKE_OUT(OutDest, 'I', uint64_t, *ptr64);
86 val64 = va_arg(Args, uint64_t);
87 MAKE_OUT(OutDest, 'I', uint64_t, val64);
91 // Input string makes no sense!
93 fprintf(stderr, "ReadEntry: Incoming string is not defined\n");
97 str = va_arg(Args, char*);
100 int len = strlen(str) + 1;
101 *OutDest = malloc( sizeof(tOutValue) + len );
102 (*OutDest)->Type = 's';
103 (*OutDest)->Length = len;
104 memcpy((*OutDest)->Data, str, len);
109 len = va_arg(Args, int);
110 str = va_arg(Args, char*);
113 if( (direction & 2) && InDest )
115 *InDest = (tInValue*)malloc( sizeof(tInValue) );
116 (*InDest)->Type = 'd';
117 (*InDest)->Length = len;
118 (*InDest)->Data = str;
122 if( (direction & 1) && InDest )
124 *OutDest = (tOutValue*)malloc( sizeof(tOutValue) + len );
125 (*OutDest)->Type = 'd';
126 (*OutDest)->Length = len;
127 memcpy((*OutDest)->Data, str, len);
137 *Direction = direction;
145 * Whitespace is ignored
146 * >i: Input Integer (32-bits)
147 * >I: Input Long Integer (64-bits)
149 * >d: Input Buffer (Preceded by valid size)
150 * <I: Output long integer
151 * <d: Output Buffer (Preceded by valid size)
152 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
155 int _Syscall(const char *ArgTypes, ...)
166 va_start(args, ArgTypes);
172 str = ReadEntry(NULL, NULL, &dir, str, args);
176 if( dir & 1 ) outCount ++;
179 if( dir & 2 ) inCount ++;
184 output = malloc( outCount*sizeof(tOutValue*) );
185 input = malloc( inCount*sizeof(tInValue*) );
187 // - re-zero so they can be used as indicies
191 // Fill `output` and `input`
192 va_start(args, ArgTypes);
200 str = ReadEntry(&outParam, &inParam, &dir, str, args);
204 output[outCount++] = outParam;
206 input[inCount++] = inParam;
210 // Send syscall request
214 while(outCount--) free(output[outCount]);
216 while(inCount--) free(input[inCount]);
223 int open(const char *Path, int Flags) {
224 return _Syscall(">s >i", Path, Flags);
231 size_t read(int FD, size_t Bytes, void *Dest) {
232 return _Syscall(">i >i <d", FD, Bytes, Bytes, Dest);
235 size_t write(int FD, size_t Bytes, void *Src) {
236 return _Syscall(">i >i >d", FD, Bytes, Bytes, Src);
239 int seek(int FD, int64_t Ofs, int Dir) {
240 return _Syscall(">i >I >i", FD, Ofs, Dir);
243 uint64_t tell(int FD) {
245 _Syscall("<I >i", &ret, FD);
249 int ioctl(int fd, int id, void *data) {
250 // NOTE: 1024 byte size is a hack
251 return _Syscall(">i >i ?d", fd, id, 1024, data);
253 int finfo(int fd, t_sysFInfo *info, int maxacls) {
254 return _Syscall(">i <d >i", fd, maxacls*sizeof(t_sysFInfo), info, maxacls);
257 int readdir(int fd, char *dest) {
258 return _Syscall(">i <s", fd, dest);
261 int _SysOpenChild(int fd, char *name, int flags) {
262 return _Syscall(">i >s >i", fd, name, flags);
265 int _SysGetACL(int fd, t_sysACL *dest) {
266 return _Syscall(">i <d", fd, sizeof(t_sysACL), dest);
269 int _SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
270 return _Syscall(">s >s >s >s", Device, Directory, Type, Options);
275 int _SysSetFaultHandler(int (*Handler)(int)) {
280 // === Symbol List ===
281 #define DEFSYM(name) {#name, name}
282 const tSym caBuiltinSymbols[] = {
294 DEFSYM(_SysOpenChild),
298 {"_SysSetFaultHandler", _SysSetFaultHandler}
301 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);