3 #define DONT_INCLUDE_SYSCALL_NAMES 1
14 #define DEBUG(str, x...) Debug(str, x)
23 FILE *gaSyscall_LocalFPs[MAX_FPS];
26 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
30 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
35 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
36 if( *ArgTypes == '\0' ) return ArgTypes;
38 // DEBUG("ArgTypes = '%s'", ArgTypes);
43 default: // Defaults to output
44 case '>': direction = 1; break;
45 case '<': direction = 2; break;
46 case '?': direction = 3; break;
51 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
52 if( *ArgTypes == '\0' ) return ArgTypes;
60 if( direction != 1 ) {
61 Warning("ReadEntry: Recieving an integer is not defined");
65 val32 = va_arg(*Args, uint32_t);
67 Dest->Type = ARG_TYPE_INT32;
68 Dest->Length = sizeof(uint32_t);
72 *(uint32_t*)DataDest = val32;
77 if( direction != 1 ) {
78 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
82 val64 = va_arg(*Args, uint64_t);
84 Dest->Type = ARG_TYPE_INT64;
85 Dest->Length = sizeof(uint64_t);
88 *(uint64_t*)DataDest = val64;
92 // Input string makes no sense!
93 if( direction != 1 ) {
94 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
98 str = va_arg(*Args, char*);
100 Dest->Type = ARG_TYPE_STRING;
101 Dest->Length = strlen(str) + 1;
106 memcpy(DataDest, str, Dest->Length);
109 // Data (special handling)
111 len = va_arg(*Args, size_t);
112 str = va_arg(*Args, char*);
114 // Save the pointer for later
115 if( PtrDest ) *PtrDest = str;
117 // Create parameter block
118 Dest->Type = ARG_TYPE_DATA;
119 Dest->Length = str ? len : 0;
122 Dest->Flags |= ARG_FLAG_RETURN;
127 if( DataDest && str )
128 memcpy(DataDest, str, len);
131 Dest->Flags |= ARG_FLAG_ZEROED;
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 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
158 int paramCount, dataLength;
159 int retCount = 1, retLength = sizeof(uint64_t);
160 void **retPtrs; // Pointers to return buffers
168 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
169 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
170 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
173 va_start(args, ArgTypes);
179 tRequestValue tmpVal;
181 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
183 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
187 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
188 dataLength += tmpVal.Length;
190 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
191 retLength += tmpVal.Length;
197 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
198 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
201 retPtrs = malloc( sizeof(void*) * (retCount+1) );
202 if( dataLength > retLength)
203 req = malloc( dataLength );
205 req = malloc( retLength );
206 req->ClientID = 0; //< Filled later
207 req->CallID = SyscallID;
208 req->NParams = paramCount;
209 req->MessageLength = dataLength;
210 dataPtr = &req->Params[paramCount];
212 // Fill `output` and `input`
213 va_start(args, ArgTypes);
215 // - re-zero so they can be used as indicies
220 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
223 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
224 dataPtr += req->Params[paramCount].Length;
225 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
232 // Send syscall request
233 if( SendRequest(req, dataLength, retLength) < 0 ) {
234 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
238 // Parse return value
239 dataPtr = &req->Params[req->NParams];
241 if( req->NParams >= 1 )
243 switch(req->Params[0].Type)
246 retValue = *(uint64_t*)dataPtr;
247 dataPtr += req->Params[0].Length;
250 retValue = *(uint32_t*)dataPtr;
251 dataPtr += req->Params[0].Length;
256 // Write changes to buffers
257 if( req->NParams - 1 != retCount ) {
258 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
259 req->NParams, retCount, SyscallID);
263 for( i = 1; i < req->NParams; i ++ )
267 printf("Return Data %i: (%i)", i, req->Params[i].Length);
268 for( j = 0; j < req->Params[i].Length; j ++ )
269 printf(" %02x", ((uint8_t*)dataPtr)[j]);
272 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
273 dataPtr += req->Params[i].Length;
279 DEBUG(": %i 0x%llx", SyscallID, retValue);
285 int native_open(const char *Path, int Flags)
288 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
289 if(ret == MAX_FPS) return -1;
290 // TODO: Handle directories
291 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
292 if(!gaSyscall_LocalFPs[ret]) return -1;
296 void native_close(int FD)
298 fclose( gaSyscall_LocalFPs[FD] );
299 gaSyscall_LocalFPs[FD] = NULL;
302 size_t native_read(int FD, void *Dest, size_t Bytes)
304 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
307 size_t native_write(int FD, const void *Src, size_t Bytes)
309 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
312 int native_seek(int FD, int64_t Ofs, int Dir)
315 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
317 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
319 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
322 uint64_t native_tell(int FD)
324 return ftell( gaSyscall_LocalFPs[FD] );
327 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
330 ret = execve(filename, (void*)argv, (void*)envp);
331 perror("native_execve");