forked from Experiment5X/XBDM
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXbdm.cpp
More file actions
1284 lines (1062 loc) · 38.6 KB
/
Copy pathXbdm.cpp
File metadata and controls
1284 lines (1062 loc) · 38.6 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "xbdm.h"
using namespace std;
using namespace XBDM;
XBDM::DevConsole::DevConsole(std::string consoleIp) :
ip(consoleIp), connected(false), debugMemStatus(DebugMemStatus::Undefined), debugMemSize(0xFFFFFFFF),
dumpMode(DumpMode::Undefined), dumpSettings({ DumpReport::Undefined })
{
}
bool XBDM::DevConsole::OpenConnection()
{
// The windows code for this function was taken off
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx
// and simply modified to connect to a devkit, and for OS X/linux
xsocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
#ifdef __WIN32
// Initialize Winsock
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
return false;
#endif
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(ip.c_str(), "730", &hints, &result);
if (iResult != 0)
{
#ifdef __WIN32
WSACleanup();
#endif
return false;
}
// Attempt to connect to an address until one succeeds
for(ptr = result; ptr != NULL; ptr = ptr->ai_next)
{
// Create a socket for connecting to server
xsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (xsocket == INVALID_SOCKET)
{
#ifdef __WIN32
WSACleanup();
#endif
return false;
}
// set timeouts
struct timeval tv = { 5, 0 };
setsockopt(xsocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
//setsockopt(xsocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(struct timeval));
// Connect to console
iResult = connect(xsocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
#ifdef __WIN32
closesocket(xsocket);
#else
// the second argument tells it to stop recieving and transmitting
close(xsocket);
#endif
xsocket = INVALID_SOCKET;
continue;
}
break;
}
// make sure that the connection succeeded
freeaddrinfo(result);
if (xsocket == INVALID_SOCKET)
{
#ifdef __WIN32
WSACleanup();
#endif
return false;
}
// read the crap placed on teh queue, should be "201- connected"
BYTE buffer[0x80] = {0};
RecieveTextBuffer(buffer, 0x80);
DWORD cmp = strcmp((char*)buffer, "201- connected\r\n");
return connected = (cmp == 0);
}
bool XBDM::DevConsole::CloseConnection()
{
std::string response;
SendCommand("bye", response);
shutdown(xsocket, 1);
close(xsocket);
#ifdef __WIN32
WSACleanup();
#endif
return true;
}
void XBDM::DevConsole::ResetConnection()
{
CloseConnection();
OpenConnection();
}
bool XBDM::DevConsole::SendBinary(const BYTE *buffer, DWORD length)
{
int iResult = send(xsocket, (char*)buffer, length, 0);
if (iResult == SOCKET_ERROR)
{
close(xsocket);
#ifdef __WIN32
WSACleanup();
#endif
return false;
}
return true;
}
bool XBDM::DevConsole::RecieveBinary(BYTE *buffer, DWORD length, DWORD &bytesRecieved)
{
return recvTimeout(xsocket, (char*)buffer, length, 0, bytesRecieved);
}
bool XBDM::DevConsole::RecieveTextBuffer(BYTE *buffer, DWORD length)
{
DWORD bytesReceived;
if (!recvTimeout(xsocket, (char*)buffer, length, MSG_PEEK, bytesReceived))
return false;
DWORD lenToGet = 0;
do
{
if (buffer[lenToGet] == 0)
break;
lenToGet++;
}
while (lenToGet < length);
// now actually read the bytes off the queue
ZeroMemory(buffer, length);
if (!recvTimeout(xsocket, (char*)buffer, lenToGet, 0, bytesReceived))
return false;
return true;
}
bool XBDM::DevConsole::SendCommand(string command, string &response, DWORD responseLength, DWORD statusLength)
{
ResponseStatus status;
return SendCommand(command, response, status, responseLength, statusLength);
}
bool XBDM::DevConsole::SendCommand(string command, string &response, ResponseStatus &status, DWORD responseLength, DWORD statusLength)
{
response = "";
// send the command to the devkit
command += "\r\n";
if (!SendBinary((BYTE*)command.c_str(), command.length()))
return false;
// weeeeeeellllll, i guess the xbox needs some time to compile the response
#ifdef __WIN32
Sleep(20);
#else
usleep(20);
#endif
return RecieveResponse(response, status, responseLength, statusLength);
}
bool XBDM::DevConsole::RecieveResponse(string &response, ResponseStatus &status, DWORD responseLength, DWORD statusLength)
{
// get the response from the console, first just read the status
unique_ptr<BYTE[]> buffer(new BYTE[responseLength]);
ZeroMemory(buffer.get(), responseLength);
// read the status
DWORD bytesRecieved;
if (!RecieveBinary(buffer.get(), 5, bytesRecieved))
return false;
// get the status from the response
int statusInt;
istringstream(string((char*)buffer.get()).substr(0, 3)) >> statusInt;
status = (ResponseStatus)statusInt;
// parse the response
switch ((ResponseStatus)statusInt)
{
case ResponseStatus::OK:
case ResponseStatus::ReadyToAcceptData:
if (!RecieveTextBuffer(buffer.get(), responseLength))
return false;
response = std::string((char*)buffer.get());
break;
case ResponseStatus::Multiline:
// THEY ARE SO INCONSISTENT! holy shit, i don't understand
// when requesting threads, it says "thread info follows" instead of the
// normal multiine response thing, it makes no sense
// read off "mulitline response follows"
if (!RecieveBinary(buffer.get(), (statusLength == -1) ? 0x1C : statusLength, bytesRecieved))
return false;
ZeroMemory(buffer.get(), responseLength);
// the end of the response always contains "\r\n." or ".\r\n"
while (response.find("\r\n.") == std::string::npos && response.find(".\r\n") == std::string::npos)
{
ZeroMemory(buffer.get(), 0x400);
if (!RecieveTextBuffer(buffer.get(), 0x400))
return false;
response += std::string((char*)buffer.get(), 0x400);
}
break;
case ResponseStatus::Binary:
// read off "binary response follows"
if (!RecieveBinary(buffer.get(), (statusLength == -1) ? 0x19 : statusLength, bytesRecieved))
return false;
// let the caller deal with reading the stream
break;
case ResponseStatus::Error:
if (!RecieveTextBuffer(buffer.get(), responseLength))
return false;
response = std::string((char*)buffer.get());
break;
default:
return false;
}
// trim off the leading and trailing whitespace
response.erase(response.begin(), std::find_if(response.begin(), response.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
response.erase(std::find_if(response.rbegin(), response.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), response.end());
return true;
}
bool XBDM::DevConsole::IsHddEnabled(bool &ok, bool forceResend)
{
SystemInformation info = GetSystemInformation(ok, forceResend);
return info.hddEnabled;
}
std::unique_ptr<BYTE[]> XBDM::DevConsole::GetScreenshot(bool &ok)
{
std::string response;
SendCommand("screenshot", response);
DWORD size = GetIntegerProperty(response, "framebuffersize", ok, true);
// get the screenshot from the console
DWORD bytesRecieved;
std::unique_ptr<BYTE[]> imageBuffer(new BYTE[size]);
RecieveBinary(imageBuffer.get(), size, bytesRecieved);
return imageBuffer;
}
std::unique_ptr<BYTE[]> XBDM::DevConsole::GetMemory(DWORD address, DWORD length, bool &ok)
{
// format the command to send to the console
char command[0x30] = {0};
snprintf(command, 0x30, "getmemex addr=0x%08x length=0x%08x", address, length);
std::string response;
SendCommand(std::string(command), response);
// allocate memory for the buffer, better be safe with these new fancy safe pointers
std::unique_ptr<BYTE[]> buffer(new BYTE[length]);
// we have to read the memory in 1kb, 0x400 byte, chunks
DWORD offset = 0;
DWORD bytesRecieved;
while (length >= 0x400)
{
// there's always these 2 bytes at the beginning, i think they're flags
// not gonna worry about them now, if there are issues then there
// might be some status flag here, i see in their code they do something
// with the flag 0x8000, i think they might quit early? i dunno
RecieveBinary(buffer.get() + offset, 2, bytesRecieved);
// read the memory from the console
RecieveBinary(buffer.get() + offset, 0x400, bytesRecieved);
length -= 0x400;
offset += 0x400;
}
if (length > 0)
{
// read off those flags again
RecieveBinary(buffer.get() + offset, 2, bytesRecieved);
RecieveBinary(buffer.get() + offset, length, bytesRecieved);
length -= length;
}
return buffer;
}
void XBDM::DevConsole::ReceiveFile(string remotePath, string localPath, bool &ok)
{
// send the command to get the file, only reading the '203 - binary response follows' or whatever
std::string response = "";
SendCommand("getfile name=\"" + remotePath + "\"", response, 0x1E);
// read the file length
BYTE temp[4];
DWORD bytesRecieved;
if (!RecieveBinary(temp, 4, bytesRecieved))
{
ok = false;
return;
}
// it's in little endian on the xbox, i have no idea what
// endian the system will be that this runs on, so we gotta swap it
DWORD fileLength = ((DWORD)temp[3] << 24) | ((DWORD)temp[2] << 16) | ((DWORD)temp[1] << 8) | (DWORD)temp[0];
// create the file on the local machine to write to
fstream outFile(localPath.c_str(), ios_base::out | ios_base::trunc | ios_base::binary);
// read the file in 0x10000 byte chunks
BYTE *fileBuffer = new BYTE[0x10000];
while (fileLength > 0)
{
// read, at most 0x10000 bytes, off the queue
if (!RecieveBinary(fileBuffer, ((fileLength >= 0x10000) ? 0x10000 : fileLength), bytesRecieved))
{
ok = false;
delete[] fileBuffer;
return;
}
outFile.write((char*)fileBuffer, bytesRecieved);
fileLength -= bytesRecieved;
}
// cleanup, everyone's gotta do their share
outFile.close();
delete[] fileBuffer;
ok = true;
}
void XBDM::DevConsole::ReceiveDirectory(string remoteDirectory, string localLocation, bool &ok)
{
// get the contents of the directory requested
vector<FileEntry> dirContents = GetDirectoryContents(remoteDirectory, ok);
if (!ok)
return;
// create the folder on the local file system
string requestedDirectoryName = remoteDirectory.substr(remoteDirectory.rfind('\\', remoteDirectory.size() - 2) + 1, remoteDirectory.rfind('\\'));
#ifdef __WIN32
ok = CreateDirectoryA((localLocation + requestedDirectoryName).c_str(), NULL);
if (!ok)
return;
#endif
// get all of the dirents from the console
localLocation += requestedDirectoryName;
for (FileEntry dirent : dirContents)
{
// if there are folders inside the folder requested, then we need to recursively get them
if (dirent.directory)
{
ReceiveDirectory(remoteDirectory + dirent.name + "\\", localLocation, ok);
if (!ok)
return;
}
else
{
ReceiveFile(remoteDirectory + dirent.name, localLocation + dirent.name, ok);
if (!ok)
return;
}
}
}
void XBDM::DevConsole::DumpMemory(DWORD address, DWORD length, string dumpPath)
{
// read the memory from the console
bool ok;
auto memory = GetMemory(address, length, ok);
// write the memory to the file the caller specified
fstream dumpFile(dumpPath.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
dumpFile.write((char*)memory.get(), length);
dumpFile.close();
}
void XBDM::DevConsole::RebootToXShell()
{
std::string response;
SendCommand("magicboot", response);
}
void XBDM::DevConsole::RebootToCurrentTitle()
{
bool ok;
LaunchXEX(GetActiveTitle(ok));
}
void XBDM::DevConsole::ColdReboot()
{
std::string response;
SendCommand("magicboot COLD", response);
}
void XBDM::DevConsole::LaunchXEX(string xexPath)
{
// for whatever reason the xbox needs the directory that the xex is in
std::string directory = xexPath.substr(0, xexPath.find_last_of('\\') + 1);
std::string response;
SendCommand("magicboot title=\"" + xexPath + "\" directory=\"" + directory + "\"", response);
}
void XBDM::DevConsole::StartAutomatingInput(DWORD userIndex, bool &ok, DWORD queueLen)
{
// validate the user index
if (userIndex > 3)
{
ok = false;
return;
}
// get the message to send to the user
char message[50] = {0};
snprintf(message, 50, "autoinput user=%d bind queuelen=%d", userIndex, queueLen);
// tell the console to start with the input
std::string response;
SendCommand(std::string(message), response);
}
void XBDM::DevConsole::StopAutomatingInput(DWORD userIndex, bool &ok)
{
// validate the user index
if (userIndex > 3)
{
ok = false;
return;
}
// get the message to send to the user
char message[30];
snprintf(message, 30, "autoinput user=%d unbind", userIndex);
// tell the console to start with the input
std::string response;
SendCommand(std::string(message), response);
}
void XBDM::DevConsole::AddGamepadToQueue(DWORD userIndex, GamepadState gamepad, bool &ok)
{
// validate the user index
if (userIndex > 3)
{
ok = false;
return;
}
// get the command to send to the xbox we'll always
// send one at a time because I don't think that they
// can handle more than one at a time
char command[60] = {0};
snprintf(command, 60, "autoinput user=%d queuepackets count=1 timearray countarray", userIndex);
std::string response;
ResponseStatus status;
SendCommand(std::string(command), response, status);
// make sure that the console processed the request properly
if (status != ResponseStatus::ReadyToAcceptData)
{
ok = false;
return;
}
// the xbox sends 2 null bytes following both the gamepad structure and the
// other data it needs. If this is true for all of the sending of binary data
// then ill change the function, but for now ill just do it here. I think those
// 2 bytes might be flags, not sure though
BYTE temp[14] = {0};
memcpy(temp, &gamepad, sizeof(GamepadState));
SendBinary((BYTE*)temp, sizeof(GamepadState) + 2);
// this is the time array and count array, which the xbox seems to pay no attention to
// it says that it's the amount of time that the button is down, but it doesn't work
DWORD args[3] = { 1, 1, 0};
SendBinary((BYTE*)args, 10);
// the console will send back the amount of gamepads queued
RecieveResponse(response, status);
response = "";
}
void XBDM::DevConsole::SendGamepads(DWORD userIndex, std::vector<GamepadState> &gamepads, bool &ok)
{
// send all of the gamepads to the console
for (GamepadState g : gamepads)
{
// tell the console to stop recieving input from the controller
StartAutomatingInput(userIndex, ok, gamepads.size());
AddGamepadToQueue(userIndex, g, ok);
ResetConnection();
// return control to the physical controller
StopAutomatingInput(userIndex, ok);
}
}
void XBDM::DevConsole::ClearGamepadQueue(DWORD userIndex, bool &ok)
{
// validate the user index
if (userIndex > 3)
{
ok = false;
return;
}
char command[40];
snprintf(command, 40, "autoinput user=%d clearqueue", userIndex);
std::string response;
SendCommand(std::string(command), response);
}
void XBDM::DevConsole::RenameFile(string oldName, string newName, bool &ok)
{
string command = "RENAME NAME=\"" + oldName + "\"" + " NEWNAME=\"" + newName + "\"";
string response;
ok = SendCommand(command, response);
}
void XBDM::DevConsole::MakeDirectory(string path, bool &ok)
{
string response;
ok = SendCommand("mkdir name=\"" + path + "\"", response);
}
void XBDM::DevConsole::SendFile(string localPath, string remotePath, bool &ok)
{
// open the file for reading, and it could be anything so: binary
ifstream localFile(localPath.c_str(), ios_base::in | ios_base::binary);
// get the length of the file
localFile.seekg(0, ios_base::end);
DWORD fileLength = localFile.tellg();
// format the command to send to the console
stringstream command;
command << "sendfile name=\"" << remotePath << "\" ";
command << "length=0x" << std::hex << fileLength;
string response;
ResponseStatus status;
SendCommand(command.str(), response, status);
if (status != ResponseStatus::ReadyToAcceptData)
{
ok = false;
return;
}
// seek back to the beginning of the file
localFile.seekg(0, ios_base::beg);
// send it in chunks, this way it can handle huge files
BYTE *fileBuffer = new BYTE[0x10000];
while (fileLength >= 0x10000)
{
localFile.read((char*)fileBuffer, 0x10000);
SendBinary(fileBuffer, 0x10000);
fileLength -= 0x10000;
}
if (fileLength != 0)
{
localFile.read((char*)fileBuffer, fileLength);
SendBinary(fileBuffer, fileLength);
}
delete[] fileBuffer;
return;
}
void XBDM::DevConsole::SendDirectory(string localDirectory, string remoteLocation, bool &ok)
{
}
void XBDM::DevConsole::DeleteDirectory(string path, bool &ok)
{
DeleteDirent(path, true, ok);
}
void XBDM::DevConsole::SetMemory(DWORD address, const BYTE *buffer, DWORD length, bool &ok)
{
while (length > 0)
{
// the xbox can only receive 128 bytes at once
DWORD bytesToSend = (length > 128) ? 128 : length;
// build the command to send
stringstream command;
command << "setmem ";
command << "addr=0x" << std::hex << address << " ";
command << "data=";
for (int i = 0; i < bytesToSend; i++)
command << std::hex << (DWORD)buffer[i];
// send the command
string response;
ResponseStatus status;
SendCommand(command.str(), response, status);
// if the status isn't 200, then you can't write to that location
if (status != ResponseStatus::OK)
{
ok = false;
return;
}
// update our values for the next iteration
buffer += bytesToSend;
address += bytesToSend;
length -= bytesToSend;
}
ok = true;
}
void XBDM::DevConsole::DeleteDirent(string path, bool isDirectory, bool &ok)
{
string response;
ok = SendCommand("delete name=\"" + path + "\"" + ((isDirectory) ? " DIR" : ""), response);
}
void XBDM::DevConsole::DeleteFile(string path, bool &ok)
{
DeleteDirent(path, false, ok);
}
void XBDM::DevConsole::MoveFile(string oldName, string newName, bool &ok)
{
RenameFile(oldName, newName, ok);
}
DWORD XBDM::DevConsole::GetDebugMemorySize(bool &ok, bool forceResend)
{
if (debugMemSize == 0xFFFFFFFF || forceResend)
{
std::string response;
SendCommand("debugmemsize", response);
bool ok;
debugMemSize = GetIntegerProperty(response, "debugmemsize", ok, true);
}
ok = true;
return debugMemSize;
}
XBDM::DebugMemStatus XBDM::DevConsole::GetDebugMemoryStatus(bool &ok, bool forceResend)
{
if (debugMemStatus == DebugMemStatus::Undefined || forceResend)
{
ResponseStatus status;
std::string temp;
ok = SendCommand("consolemem", temp, status);
if (status != ResponseStatus::OK)
ok = false;
// get the actual status from the response
// it's formated like this: consolemem=0xVALUE
int memStatus;
istringstream(temp.substr(13)) >> std::hex >> memStatus;
debugMemStatus = (DebugMemStatus)memStatus;
}
ok = true;
return debugMemStatus;
}
DumpMode XBDM::DevConsole::GetDumpMode(bool &ok, bool forceResend)
{
if (dumpMode == DumpMode::Undefined || forceResend)
{
std::string response;
SendCommand("dumpmode", response);
if (response == "smart")
dumpMode = DumpMode::Smart;
else if (response == "enabled")
dumpMode = DumpMode::Enabled;
else if (response == "disabled")
dumpMode = DumpMode::Disabled;
else
{
ok = false;
return DumpMode::Undefined;
}
}
ok = true;
return dumpMode;
}
DumpSettings XBDM::DevConsole::GetDumpSettings(bool &ok, bool forceResend)
{
if (dumpSettings.report == DumpReport::Undefined || forceResend)
{
bool ok;
std::string response;
SendCommand("dumpsettings", response);
// get the report
std::string report = GetEnumProperty(response, "rpt", ok);
if (report == "prompt")
dumpSettings.report = DumpReport::Prompt;
else if (report == "always")
dumpSettings.report = DumpReport::Always;
else if (report == "never")
dumpSettings.report = DumpReport::Never;
else
{
ok = false;
return dumpSettings;
}
// get the destination
std::string destination = GetEnumProperty(response, "dst", ok);
if (destination == "local")
dumpSettings.destination = DumpDestination::Local;
else if (destination == "remote")
dumpSettings.destination = DumpDestination::Remote;
else
{
ok = false;
return dumpSettings;
}
// get the report
std::string format = GetEnumProperty(response, "fmt", ok);
if (format == "full")
dumpSettings.format = DumpFormat::FullHeap;
else if (format == "partial")
dumpSettings.format = DumpFormat::PartialHeap;
else if (format == "noheap")
dumpSettings.format = DumpFormat::NoHeap;
else if (format == "retail")
dumpSettings.format = DumpFormat::Retail;
else
{
ok = false;
return dumpSettings;
}
dumpSettings.path = GetStringProperty(response, "path", ok);
}
return dumpSettings;
}
SystemInformation XBDM::DevConsole::GetSystemInformation(bool &ok, bool forceResend)
{
if (systemInformation.platform == "" || forceResend)
{
bool ok;
std::string response;
SendCommand("systeminfo", response);
// parse through the response
systemInformation.hddEnabled = GetEnumProperty(response, "HDD", ok) == "Enabled";
systemInformation.platform = GetEnumProperty(response, "Platform", ok);
systemInformation.system = GetEnumProperty(response, "System", ok);
systemInformation.baseKernelVersion = GetEnumProperty(response, "BaseKrnl", ok);
systemInformation.kernelVersion = GetEnumProperty(response, "Krnl", ok);
systemInformation.recoveryVersion = GetEnumProperty(response, "XDK", ok);
}
return systemInformation;
}
std::vector<Drive> XBDM::DevConsole::GetDrives(bool &ok, bool forceResend)
{
if (drives.size() == 0 || forceResend)
{
// clear the existing drives
drives.erase(drives.begin(), drives.end());
std::string response;
SendCommand("drivelist", response);
// get all of the drive names from the response
bool ok;
do
{
std::string driveName = GetStringProperty(response, "drivename", ok, true);
if (ok)
{
Drive d = { driveName, 0, 0, 0 };
drives.push_back(d);
}
}
while (ok);
// now that all of the drive names are loaded, let's get the drive size information
for (Drive &d : drives)
{
SendCommand("drivefreespace name=\"" + d.name + ":\\\"", response);
d.freeBytesAvailable = ((UINT64)GetIntegerProperty(response, "freetocallerhi", ok, true) << 32) |
(UINT64)GetIntegerProperty(response, "freetocallerlo", ok, true);
d.totalBytes = ((UINT64)GetIntegerProperty(response, "totalbyteshi", ok, true) << 32) |
(UINT64)GetIntegerProperty(response, "totalbyteslo", ok, true);
d.totalFreeBytes = ((UINT64)GetIntegerProperty(response, "totalfreebyteshi", ok, true) << 32) |
(UINT64)GetIntegerProperty(response, "totalfreebyteslo", ok, true);
// get the friendly name for volume, these are from neighborhood
if (d.name == "DEVKIT" || d.name == "E")
d.friendlyName = "Game Development Volume (" + d.name + ":)";
else if (d.name == "HDD")
d.friendlyName = "Retail Hard Drive Emulation (" + d.name + ":)";
else if (d.name == "Y")
d.friendlyName = "Xbox360 Dashboard Volume (" + d.name + ":)";
else if (d.name == "Z")
d.friendlyName = "Devkit Drive (" + d.name + ":)";
else
d.friendlyName = "Volume (" + d.name + ":)";
}
}
ok = true;
return drives;
}
std::vector<FileEntry> XBDM::DevConsole::GetDirectoryContents(string directory, bool &ok)
{
std::string response;
ok = SendCommand("dirlist name=\"" + directory + "\"", response);
std::vector<FileEntry> toReturn;
if (!ok || response.find("file not found\r\n") == 0)
{
ok = false;
return toReturn;
}
do
{
FileEntry entry;
entry.name = GetStringProperty(response, "name", ok, true);
entry.size = ((UINT64)GetIntegerProperty(response, "sizehi", ok, true, true) << 32) | GetIntegerProperty(response, "sizelo", ok, true, true);
entry.creationTime = FILETIME_TO_TIMET(((UINT64)GetIntegerProperty(response, "createhi", ok, true, true) << 32) | GetIntegerProperty(response, "createlo", ok, true, true));
entry.modifiedTime = FILETIME_TO_TIMET(((UINT64)GetIntegerProperty(response, "changehi", ok, true, true) << 32) | GetIntegerProperty(response, "changelo", ok, true, true));
entry.directory = response.find(" directory") == 0;
if (ok)
toReturn.push_back(entry);
}
while (ok);
ok = true;
return toReturn;
}
std::vector<Module> XBDM::DevConsole::GetLoadedModules(bool &ok, bool forceResend)
{
if (loadedModules.size() == 0 || forceResend)
{
// clear the existing modules
loadedModules.erase(loadedModules.begin(), loadedModules.end());
std::string response;
SendCommand("modules", response);
do
{
Module m;
m.name = GetStringProperty(response, "name", ok, true);
m.baseAddress = GetIntegerProperty(response, "base", ok, true, true);
m.size = GetIntegerProperty(response, "size", ok, true, true);
m.checksum = GetIntegerProperty(response, "check", ok, true, true);
m.timestamp = GetIntegerProperty(response, "timestamp", ok, true, true);
m.dataAddress = GetIntegerProperty(response, "pdata", ok, true, true);
m.dataSize = GetIntegerProperty(response, "psize", ok, true, true);
m.threadId = GetIntegerProperty(response, "thread", ok, true, true);
m.originalSize = GetIntegerProperty(response, "osize", ok, true, true);
if (ok)
loadedModules.push_back(m);
}
while (ok);
// now let's request all of the sections for each of the module
for (Module &m : loadedModules)
{
std::string response;
SendCommand("modsections name=\"" + m.name + "\"", response);
do
{
ModuleSection section;
section.name = GetStringProperty(response, "name", ok, true);
section.baseAddress = GetIntegerProperty(response, "base", ok, true, true);
section.size = GetIntegerProperty(response, "size", ok, true, true);
section.index = GetIntegerProperty(response, "index", ok, false, true);
section.flags = GetIntegerProperty(response, "flags", ok, false, true);
if (ok)
m.sections.push_back(section);
}
while (ok);
}
}
return loadedModules;
}
std::vector<Thread> XBDM::DevConsole::GetThreads(bool &ok, bool forceResend)
{
if (threads.size() == 0 || forceResend)
{
// clear the existing threads
threads.erase(threads.begin(), threads.end());
std::string response;
SendCommand("threads", response);
// parse all of the thread IDs from the response
std::vector<DWORD> threadIDs;
bool isFirst = true;
while (response.length() > 1)
{
// split the string up by the delimeter \r\n
std::string curID = response.substr(0, response.find("\r\n"));
response = response.substr(response.find("\r\n") + 2);
// we have to skip the first one, it's not a thread
// i have no idea what it actually is though
if (isFirst)
{
isFirst = false;
continue;
}
// convert the string ID to a DWORD
DWORD dwId;
istringstream(curID) >> dwId;
threadIDs.push_back(dwId);
}
// request information about all of the threads
for (DWORD id : threadIDs)
{
char command[0x50] = {0};
snprintf(command, 0x50, "threadinfo thread=0x%0.8x", id);
// request the thread info from the console
SendCommand(std::string(command), response, 0x400, 21);
// parse the response
Thread threadInfo;
threadInfo.id = id;
threadInfo.suspendCount = GetIntegerProperty(response, "suspend", ok);
threadInfo.priority = GetIntegerProperty(response, "priority", ok);
threadInfo.tlsBaseAddress = GetIntegerProperty(response, "tlsbase", ok, true);
threadInfo.baseAddress = GetIntegerProperty(response, "base", ok, true);
threadInfo.tlsBaseAddress = GetIntegerProperty(response, "limit", ok, true);
threadInfo.tlsBaseAddress = GetIntegerProperty(response, "slack", ok, true);