-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbbloader.pas
More file actions
565 lines (523 loc) · 10.8 KB
/
bbloader.pas
File metadata and controls
565 lines (523 loc) · 10.8 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
unit bbLoader;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, bbusb;
const
MAX_FLASH_BLOCK = $3FF4;
type
TBBLoader = class
private
fUSB: TBBUSB;
public
constructor Create(usb: TBBUSB);
function BugdispLog: TBytes;
function FlashRegionsInfo: TBytes;
function BlockedOS: TBytes;
function BlockedRadio: TBytes;
function PIN: Dword;
function BSN: Dword;
function VendorID: word;
function HWID_OVERRIDE: TBytes;
function DRAMInfo: TBytes;
function SendBlock(var Data: TBytes): boolean;
function Complete(): boolean;
function GRS_Wipe(): boolean;
function PreFlash(x: byte): TBytes;
function PersistentData: TBytes;
function GetMCT: TBytes;
function OSMetrics: TBytes;
function GetBLog: TBytes;
function GetLAL: TBytes;
function GetOSBoot: TBytes;
procedure EnableLED;
procedure RemoveInstaller;
procedure EraseMCT;
function SendSignature(var Data: TBytes): boolean;
function Reboot(): boolean;
end;
implementation
uses
CLI.Interfaces, // Core interfaces
CLI.Command, // Base command implementation
CLI.Progress, // Optional: Progress indicators
CLI.Console; // Optional: Colored console output
(*
Command codes documentation:
20 - Retrieving persistent data struct
21 - Retrieving Bootrom Log
39 - (Reserved)
3A - (Reserved)
50 - Get TZ & etc info
AB - Getting the JDM BSN
AC - (Reserved)
AD - Retrieving PMIC Info
AE - Erase blocked FW versions lists
AF - Write DWORD to 8023977c
B0 - Bugdisp log
B1 - Retrieving Flash IDs
B2 - Retrieving DSP OS Metrics
B3 - Retrieving Loader Action Log
B4 - Retrieving Flash Regions Info
B5 - Retrieving Flash Info
B6 - USBMS
B7 - HASH_BOOTROM
BD - Retrieving Hardware Override ID
BE - Setting Hardware Override ID
BF - Retrieving DRAM Info
C0 40 - Requesting COMPLETE
C2 - (Reserved)
C3 - Enable bootrom led
C4 - READVERIFY 00 dword(addr) dword(size) byte(val)
C6 - FACTORY_WIPE (BBOS)
C8 - GRS_WIPE (BBOS)
C9 - USB Test
CB - Retrieving OS boot error info
CC - (Reserved)
CD - (Reserved)
D0 - (Reserved)
D1 - Retrieving SVN
D2 - Retrieving Kernel Metrics
D3 - Retrieving App Metrics
D4 - Appstore NUKE
D5 - SUPER_NUKE
D7 - SetActiveMCT
D8 - OS metrics
D9 - Retrieving MCT
DA - HIS
DB - Retrieving Vendor ID
DC - DO_CRC_VERIFY
DD - FLASH_DUMP
DE - BOOT_MODE
E0 - REMOVE_INSTALLER
E1 - Retrieving Installer Metrics
E3 - ADD_OS_EXTENDED
E4 - CREAD INIT
E5 - CREAD 00 dword(addr?) dword (size<=0x3FA0)
E6 - (Reserved)
E7 - Getting the Device PIN
E8 - WIPE_SECURITY
E9 - Retrieving boot count info
EA - Getting the Device BSN
EB - (Reserved)
EC - Retrieving Blocked OS CFP
ED - Retrieving Blocked Radio OS CFP
EE - ERASE_SECTOR
EE 20 - (Reserved)
EF 80 - Reboot
F1 - Upgrading MCT to resizable partitions
F2 - Resizing MCT partition
F3 - ERASE_ALL_MCT
F4 - Retrieving the Number of UMPs
F5 - Retrieving UMP Information
F6 - Downgrading a resizable MCT to dynamic
F7 - Write data
F8 - Write data
F9 40 - SIGNATURE_TRAILER
FD - (Reserved)
FF - Retrieving DDR Info
*)
{ TBBLoader }
constructor TBBLoader.Create(usb: TBBUSB);
begin
inherited Create;
fUSB := usb;
end;
function TBBLoader.BugdispLog: TBytes;
var
Cmd: word;
o, l: integer;
Data: TBytes;
begin
SetLength(Result, 0);
o := 0;
repeat
try
Data := fUSB.Channel2($B0, Cmd, [00, 00, 00, 00, 00, 00, 00, 00]);
l := Length(Data);
if (Cmd = $B5) and (l > 0) then
begin
SetLength(Result, l + o);
Move(Data[0], Result[o], l);
Inc(o, l);
end;
except
on E: Exception do
begin
TConsole.WriteLn('Error in BugdispLog: ' + E.Message, ccYellow);
Break;
end;
end;
until Cmd = $D0;
end;
function TBBLoader.PreFlash(x: byte): TBytes;
var
Cmd: word;
Data: TBytes;
begin
SetLength(Data, 36);
FillChar(Data[0], Length(Data), 0);
Data[0] := x;
Data[1] := $28;
Data[6] := $02;
Data[28] := $01;
Data[32] := $02;
try
Result := fUSB.Channel2($20EE, Cmd, Data);
if Cmd <> $39 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in PreFlash: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.PersistentData: TBytes;
var
Cmd: word;
Data: TBytes;
begin
SetLength(Data, 1024);
FillChar(Data[0], Length(Data), 0);
PDword(@Data[4])^ := $3c6806c;
PDword(@Data[8])^ := $36159469;
PDword(@Data[84])^ := $240a2dff;
try
Result := fUSB.Channel2($20, Cmd, Data);
if Cmd <> $39 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in PersistentData: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.GetBLog: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($21, Cmd, []);
except
on E: Exception do
begin
TConsole.WriteLn('Error in GetBlog: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.GetOSBoot: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($CB, Cmd, []);
except
on E: Exception do
begin
TConsole.WriteLn('Error in GetLAL: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.GetLAL: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($B3, Cmd, [0, 0, 0, 0]);
except
on E: Exception do
begin
TConsole.WriteLn('Error in GetLAL: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.GetMCT: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($D9, Cmd, []);
if Cmd <> $C9 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in GetMCT: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.FlashRegionsInfo: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($B4, Cmd, []);
if Cmd <> $D2 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in FlashRegionsInfo: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.PIN: Dword;
var
Cmd: word;
Buff: TBytes;
begin
Result := 0;
try
Buff := fUSB.Channel2($E7, Cmd, []);
if (Cmd = $D1) and (Length(Buff) >= SizeOf(DWord)) then
Result := PDword(@Buff[0])^;
except
on E: Exception do
begin
TConsole.WriteLn('Error in PIN: ' + E.Message, ccRed);
Result := 0;
end;
end;
end;
function TBBLoader.BSN: Dword;
var
Cmd: word;
Buff: TBytes;
begin
Result := 0;
try
Buff := fUSB.Channel2($EA, Cmd, []);
if (Cmd = $FC) and (Length(Buff) >= SizeOf(DWord)) then
Result := PDword(@Buff[0])^;
except
on E: Exception do
begin
TConsole.WriteLn('Error in BSN: ' + E.Message, ccRed);
Result := 0;
end;
end;
end;
function TBBLoader.VendorID: word;
var
Cmd: word;
Buff: TBytes;
begin
Result := 0;
try
Buff := fUSB.Channel2($DB, Cmd, []);
if (Cmd = $CB) and (Length(Buff) >= SizeOf(DWord)) then
Result := Pword(@Buff[2])^;
except
on E: Exception do
begin
TConsole.WriteLn('Error in VendorID: ' + E.Message, ccRed);
Result := 0;
end;
end;
end;
function TBBLoader.HWID_OVERRIDE: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($BD, Cmd, []);
if (Cmd <> $BE) then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in HWID_OVERRIDE: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.SendBlock(var Data: TBytes): boolean;
var
Cmd: word;
begin
Result := False;
try
fUSB.Channel2($F7, Cmd, Data);
Result := (Cmd = $DF);
except
on E: Exception do
begin
TConsole.WriteLn('Error in SendBlock: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.SendSignature(var Data: TBytes): boolean;
var
Cmd: word;
begin
Result := False;
try
fUSB.Channel2($40F9, Cmd, Data);
Result := (Cmd = $4006);
except
on E: Exception do
begin
TConsole.WriteLn('Error in SendSignature: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.Complete(): boolean;
var
Cmd: word;
begin
Result := False;
try
fUSB.Channel2($40C0, Cmd, []);
Result := (Cmd = $4006);
except
on E: Exception do
begin
TConsole.WriteLn('Error in Complete: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.GRS_Wipe(): boolean;
var
Cmd: word;
Buff: TBytes;
begin
Result := False;
try
SetLength(Buff, MAX_FLASH_BLOCK);
fUSB.Channel2($C8, Cmd, Buff);
Result := (Cmd = $D8);
except
on E: Exception do
begin
TConsole.WriteLn('Error in GRS_Wipe: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.Reboot(): boolean;
var
Cmd: word;
begin
Result := False;
try
fUSB.Channel2($80EF, Cmd, []);
Result := (Cmd = $80C7);
except
on E: Exception do
begin
TConsole.WriteLn('Error in Reboot: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.BlockedOS: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($EC, Cmd, []);
if Cmd <> $FD then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in BlockedOS: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.DRAMInfo: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($BF, Cmd, []);
if Cmd <> $D9 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in DRAMInfo: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
procedure TBBLoader.EnableLED;
var
Cmd: word;
begin
try
fUSB.Channel2($C3, Cmd, []);
except
on E: Exception do
begin
TConsole.WriteLn('Error in EnableLED: ' + E.Message, ccRed);
end;
end;
end;
procedure TBBLoader.RemoveInstaller;
var
Cmd: word;
begin
try
fUSB.Channel2($E0, Cmd, []);
except
on E: Exception do
begin
TConsole.WriteLn('Error in RemoveInstaller: ' + E.Message, ccRed);
end;
end;
end;
procedure TBBLoader.EraseMCT;
var
Cmd: word;
begin
try
fUSB.Channel2($F3, Cmd, []);
except
on E: Exception do
begin
TConsole.WriteLn('Error in EraseMCT: ' + E.Message, ccRed);
end;
end;
end;
function TBBLoader.BlockedRadio: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($ED, Cmd, []);
if Cmd <> $FE then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in BlockedRadio: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
function TBBLoader.OSMetrics: TBytes;
var
Cmd: word;
begin
try
Result := fUSB.Channel2($D8, Cmd, []);
if Cmd <> $C8 then
SetLength(Result, 0);
except
on E: Exception do
begin
TConsole.WriteLn('Error in OSMetrics: ' + E.Message, ccRed);
SetLength(Result, 0);
end;
end;
end;
end.