3 #define DONT_INCLUDE_SYSCALL_NAMES 1
13 # include <spawn.h> // posix_spawn
18 #define DEBUG(str, x...) Debug(str, x)
20 #define DEBUG(...) do{}while(0)
30 FILE *gaSyscall_LocalFPs[MAX_FPS];
33 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
37 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
42 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
43 if( *ArgTypes == '\0' ) return ArgTypes;
45 // DEBUG("ArgTypes = '%s'", ArgTypes);
50 default: // Defaults to output
51 case '>': direction = 1; break;
52 case '<': direction = 2; break;
53 case '?': direction = 3; break;
58 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
59 if( *ArgTypes == '\0' ) return ArgTypes;
67 if( direction != 1 ) {
68 Warning("ReadEntry: Recieving an integer is not defined");
72 val32 = va_arg(*Args, uint32_t);
74 Dest->Type = ARG_TYPE_INT32;
75 Dest->Length = sizeof(uint32_t);
79 *(uint32_t*)DataDest = val32;
84 if( direction != 1 ) {
85 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
89 val64 = va_arg(*Args, uint64_t);
91 Dest->Type = ARG_TYPE_INT64;
92 Dest->Length = sizeof(uint64_t);
95 *(uint64_t*)DataDest = val64;
99 // Input string makes no sense!
100 if( direction != 1 ) {
101 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
105 str = va_arg(*Args, char*);
107 Dest->Type = ARG_TYPE_STRING;
108 Dest->Length = strlen(str) + 1;
113 memcpy(DataDest, str, Dest->Length);
116 // Data (special handling)
118 len = va_arg(*Args, size_t);
119 str = va_arg(*Args, char*);
121 // Save the pointer for later
122 if( PtrDest ) *PtrDest = str;
124 // Create parameter block
125 Dest->Type = ARG_TYPE_DATA;
126 Dest->Length = str ? len : 0;
129 Dest->Flags |= ARG_FLAG_RETURN;
134 if( DataDest && str )
135 memcpy(DataDest, str, len);
138 Dest->Flags |= ARG_FLAG_ZEROED;
152 * Whitespace is ignored
153 * >i: Input Integer (32-bits)
154 * >I: Input Long Integer (64-bits)
156 * >d: Input Buffer (Preceded by valid size)
157 * <I: Output long integer
158 * <d: Output Buffer (Preceded by valid size)
159 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
162 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
165 int paramCount, dataLength;
166 int retCount = 1, retLength = sizeof(uint64_t);
167 void **retPtrs; // Pointers to return buffers
175 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
176 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
177 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
180 va_start(args, ArgTypes);
186 tRequestValue tmpVal;
188 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
190 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
194 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
195 dataLength += tmpVal.Length;
197 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
198 retLength += tmpVal.Length;
204 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
205 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
208 retPtrs = malloc( sizeof(void*) * (retCount+1) );
209 if( dataLength > retLength)
210 req = malloc( dataLength );
212 req = malloc( retLength );
213 req->ClientID = 0; //< Filled later
214 req->CallID = SyscallID;
215 req->NParams = paramCount;
216 req->MessageLength = dataLength;
217 dataPtr = &req->Params[paramCount];
219 // Fill `output` and `input`
220 va_start(args, ArgTypes);
222 // - re-zero so they can be used as indicies
227 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
230 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
231 dataPtr += req->Params[paramCount].Length;
232 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
239 // Send syscall request
240 if( SendRequest(req, dataLength, retLength) < 0 ) {
241 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
245 // Parse return value
246 dataPtr = &req->Params[req->NParams];
248 if( req->NParams >= 1 )
250 switch(req->Params[0].Type)
253 retValue = *(uint64_t*)dataPtr;
254 dataPtr += req->Params[0].Length;
257 retValue = *(uint32_t*)dataPtr;
258 dataPtr += req->Params[0].Length;
263 // Write changes to buffers
264 if( req->NParams - 1 != retCount ) {
265 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
266 req->NParams, retCount, SyscallID);
270 for( i = 1; i < req->NParams; i ++ )
274 printf("Return Data %i: (%i)", i, req->Params[i].Length);
275 for( j = 0; j < req->Params[i].Length; j ++ )
276 printf(" %02x", ((uint8_t*)dataPtr)[j]);
279 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
280 dataPtr += req->Params[i].Length;
286 DEBUG(": %i 0x%llx", SyscallID, retValue);
292 int native_open(const char *Path, int Flags)
295 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
296 if(ret == MAX_FPS) return -1;
297 // TODO: Handle directories
298 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
299 if(!gaSyscall_LocalFPs[ret]) return -1;
303 void native_close(int FD)
305 fclose( gaSyscall_LocalFPs[FD] );
306 gaSyscall_LocalFPs[FD] = NULL;
309 size_t native_read(int FD, void *Dest, size_t Bytes)
311 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
314 size_t native_write(int FD, const void *Src, size_t Bytes)
316 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
319 int native_seek(int FD, int64_t Ofs, int Dir)
322 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
324 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
326 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
329 uint64_t native_tell(int FD)
331 return ftell( gaSyscall_LocalFPs[FD] );
334 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
337 ret = execve(filename, (void*)argv, (void*)envp);
338 perror("native_execve");
342 int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
347 rv = _spawnve(_P_NOWAIT, filename, argv, envp);
349 rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);