3 #define DONT_INCLUDE_SYSCALL_NAMES 1
15 #define DEBUG(str, x...) Debug(str, x)
17 #define DEBUG(...) do{}while(0)
27 FILE *gaSyscall_LocalFPs[MAX_FPS];
30 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
34 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
39 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
40 if( *ArgTypes == '\0' ) return ArgTypes;
42 // DEBUG("ArgTypes = '%s'", ArgTypes);
47 default: // Defaults to output
48 case '>': direction = 1; break;
49 case '<': direction = 2; break;
50 case '?': direction = 3; break;
55 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
56 if( *ArgTypes == '\0' ) return ArgTypes;
64 if( direction != 1 ) {
65 Warning("ReadEntry: Recieving an integer is not defined");
69 val32 = va_arg(*Args, uint32_t);
71 Dest->Type = ARG_TYPE_INT32;
72 Dest->Length = sizeof(uint32_t);
76 *(uint32_t*)DataDest = val32;
81 if( direction != 1 ) {
82 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
86 val64 = va_arg(*Args, uint64_t);
88 Dest->Type = ARG_TYPE_INT64;
89 Dest->Length = sizeof(uint64_t);
92 *(uint64_t*)DataDest = val64;
96 // Input string makes no sense!
97 if( direction != 1 ) {
98 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
102 str = va_arg(*Args, char*);
104 Dest->Type = ARG_TYPE_STRING;
105 Dest->Length = strlen(str) + 1;
110 memcpy(DataDest, str, Dest->Length);
113 // Data (special handling)
115 len = va_arg(*Args, size_t);
116 str = va_arg(*Args, char*);
118 // Save the pointer for later
119 if( PtrDest ) *PtrDest = str;
121 // Create parameter block
122 Dest->Type = ARG_TYPE_DATA;
123 Dest->Length = str ? len : 0;
126 Dest->Flags |= ARG_FLAG_RETURN;
131 if( DataDest && str )
132 memcpy(DataDest, str, len);
135 Dest->Flags |= ARG_FLAG_ZEROED;
149 * Whitespace is ignored
150 * >i: Input Integer (32-bits)
151 * >I: Input Long Integer (64-bits)
153 * >d: Input Buffer (Preceded by valid size)
154 * <I: Output long integer
155 * <d: Output Buffer (Preceded by valid size)
156 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
159 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
162 int paramCount, dataLength;
163 int retCount = 1, retLength = sizeof(uint64_t);
164 void **retPtrs; // Pointers to return buffers
172 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
173 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
174 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
177 va_start(args, ArgTypes);
183 tRequestValue tmpVal;
185 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
187 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
191 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
192 dataLength += tmpVal.Length;
194 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
195 retLength += tmpVal.Length;
201 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
202 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
205 retPtrs = malloc( sizeof(void*) * (retCount+1) );
206 if( dataLength > retLength)
207 req = malloc( dataLength );
209 req = malloc( retLength );
210 req->ClientID = 0; //< Filled later
211 req->CallID = SyscallID;
212 req->NParams = paramCount;
213 req->MessageLength = dataLength;
214 dataPtr = &req->Params[paramCount];
216 // Fill `output` and `input`
217 va_start(args, ArgTypes);
219 // - re-zero so they can be used as indicies
224 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
227 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
228 dataPtr += req->Params[paramCount].Length;
229 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
236 // Send syscall request
237 if( SendRequest(req, dataLength, retLength) < 0 ) {
238 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
242 // Parse return value
243 dataPtr = &req->Params[req->NParams];
245 if( req->NParams >= 1 )
247 switch(req->Params[0].Type)
250 retValue = *(uint64_t*)dataPtr;
251 dataPtr += req->Params[0].Length;
254 retValue = *(uint32_t*)dataPtr;
255 dataPtr += req->Params[0].Length;
260 // Write changes to buffers
261 if( req->NParams - 1 != retCount ) {
262 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
263 req->NParams, retCount, SyscallID);
267 for( i = 1; i < req->NParams; i ++ )
271 printf("Return Data %i: (%i)", i, req->Params[i].Length);
272 for( j = 0; j < req->Params[i].Length; j ++ )
273 printf(" %02x", ((uint8_t*)dataPtr)[j]);
276 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
277 dataPtr += req->Params[i].Length;
283 DEBUG(": %i 0x%llx", SyscallID, retValue);
289 int native_open(const char *Path, int Flags)
292 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
293 if(ret == MAX_FPS) return -1;
294 // TODO: Handle directories
295 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
296 if(!gaSyscall_LocalFPs[ret]) return -1;
300 void native_close(int FD)
302 fclose( gaSyscall_LocalFPs[FD] );
303 gaSyscall_LocalFPs[FD] = NULL;
306 size_t native_read(int FD, void *Dest, size_t Bytes)
308 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
311 size_t native_write(int FD, const void *Src, size_t Bytes)
313 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
316 int native_seek(int FD, int64_t Ofs, int Dir)
319 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
321 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
323 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
326 uint64_t native_tell(int FD)
328 return ftell( gaSyscall_LocalFPs[FD] );
331 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
334 ret = execve(filename, (void*)argv, (void*)envp);
335 perror("native_execve");