-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon.cpp
More file actions
2834 lines (2596 loc) · 94.2 KB
/
common.cpp
File metadata and controls
2834 lines (2596 loc) · 94.2 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 "common.h"
#include "ui/layout/bottom_bar.h"
#include "pages/page_pac_flash.h"
#include <functional>
#if defined(__linux__) || defined(__APPLE__)
#include <sys/stat.h>
#endif
#include <string>
#include "main.h"
#include "core/logging.h"
#include "core/app_state.h"
#include "core/XmlParser.hpp"
int isCancel = 0;
bool isHelperInit = false;
GtkWidgetHelper helper;
bool Err_Showed = false;
static std::string g_progress_desc;
#if defined(_MSC_VER) || defined(_WIN32)
void usleep(unsigned int us) {
Sleep(us / 1000);
}
#endif
extern int& m_bOpened;
extern AppState g_app_state;
char fn_partlist[40] = { 0 };
char savepath[ARGV_LEN] = { 0 };
#if defined(__APPLE__)
bool g_is_macos_bundle = false;
#endif
DA_INFO_T Da_Info;
partition_t gPartInfo;
bool isUseCptable = false;
int check_confirm(const char *name) {
if (isHelperInit) return 1;
char c;
DEG_LOG(OP,"Answer \"y\" to confirm the \"%s\" command: ", name);
fflush(stdout);
if (scanf(" %c", &c) != 1) return 0;
while (getchar() != '\n');
if (tolower(c) == 'y') return 1;
return 0;
}
uint8_t *loadfile(const char *fn, size_t *num, size_t extra) {
size_t n = 0, j = 0;
uint8_t *buf = nullptr;
EnhancedFile fi = oxfopen_enhanced(fn, "rb");
if (fi) {
if (fi.seek(0, SEEK_END) == 0) {
long n_long = fi.tell();
if (n_long > 0) {
n = static_cast<size_t>(n_long);
if (n <= SIZE_MAX - extra) {
fi.rewind();
buf = NEWN uint8_t[n + extra];
if (buf) {
j = fi.read(buf, 1, n);
}
}
}
}
}
if (num) *num = j;
return buf;
}
void send_buf(spdio_t *io,
uint32_t start_addr, int end_data,
unsigned step, uint8_t *mem, unsigned size) {
uint32_t i, n;
uint32_t *data = (uint32_t *)io->temp_buf;
WRITE32_BE(data, start_addr);
WRITE32_BE(data + 1, size);
encode_msg_nocpy(io, BSL_CMD_START_DATA, 4 * 2);
if (send_and_check(io)) return;
for (i = 0; i < size; i += n) {
n = size - i;
// n = spd_transcode_max(mem + i, size - i, 2048 - 2 - 6);
if (n > step) n = step;
encode_msg(io, BSL_CMD_MIDST_DATA, mem + i, n);
if (send_and_check(io)) return;
}
if (end_data) {
encode_msg_nocpy(io, BSL_CMD_END_DATA, 0);
send_and_check(io);
}
}
/*
void send_buf_1(spdio_t* io,
uint32_t start_addr, int end_data,
unsigned step, uint8_t* mem, unsigned size) {
static unsigned long long start_time = 0;
static uint64_t total_sent = 0;
static uint64_t total_size = 0;
// ������µĴ����������ü�ʱ���ͼ�����
if (start_time == 0) {
start_time = GetTickCount64();
total_sent = 0;
total_size = size;
}
uint32_t i, n;
uint32_t* data = (uint32_t*)io->temp_buf;
WRITE32_BE(data, start_addr);
WRITE32_BE(data + 1, size);
encode_msg_nocpy(io, BSL_CMD_START_DATA, 4 * 2);
if (send_and_check(io)) {
// ����ʱ��ʾ��ǰ����
print_progress_bar(total_sent, total_size, start_time);
printf("\n"); // ����
start_time = 0; // �����Ա��´�ʹ��
return;
}
for (i = 0; i < size; i += n) {
n = size - i;
if (n > step) n = step;
encode_msg(io, BSL_CMD_MIDST_DATA, mem + i, n);
if (send_and_check(io)) {
// ����ʱ��ʾ��ǰ����
print_progress_bar(total_sent, total_size, start_time);
printf("\n"); // ����
start_time = 0; // �����Ա��´�ʹ��
return;
}
// �����ѷ����ֽ�������ʾ����
total_sent += n;
print_progress_bar(total_sent, total_size, start_time);
}
if (end_data) {
encode_msg_nocpy(io, BSL_CMD_END_DATA, 0);
if (send_and_check(io)) {
// ����ʱ��ʾ��ǰ����
print_progress_bar(total_sent, total_size, start_time);
printf("\n"); // ����
start_time = 0; // �����Ա��´�ʹ��
return;
}
}
// ��ɴ��䣬��ʾ100%����
print_progress_bar(total_size, total_size, start_time);
printf("\n"); // ��ɻ���
// ���þ�̬�����Ա��´�ʹ��
start_time = 0;
}
*/
size_t send_file(spdio_t *io, const char *fn,
uint32_t start_addr, int end_data, unsigned step,
unsigned src_offs, unsigned src_size) {
uint8_t *mem; size_t size = 0;
mem = loadfile(fn, &size, 0);
if (!mem) ERR_EXIT("load file(\"%s\") failed\n", fn);
if ((uint64_t)size >> 32) ERR_EXIT("file too big\n");
if (size < src_offs) ERR_EXIT("required offset larger than file size\n");
size -= src_offs;
if (src_size) {
if (size < src_size) DEG_LOG(W,"required size larger than file size");
else size = src_size;
}
send_buf(io, start_addr, end_data, step, mem + src_offs, size);
delete[](mem);
DEG_LOG(OP,"Sent %s to 0x%x", fn, start_addr);
return size;
}
int GetStage() {
if (fdl2_executed > 0) return FDL2;
else if (fdl1_loaded > 0) return FDL1;
else return BROM;
}
unsigned read_flash(spdio_t *io,
uint32_t addr, uint32_t start, uint32_t len,
uint8_t *mem, FILE *fo, unsigned step) {
uint32_t n, offset, nread;
int ret;
for (offset = start; offset < start + len; ) {
uint32_t data[3];
n = start + len - offset;
if (n > step) n = step;
WRITE32_BE(data, addr);
WRITE32_BE(data + 1, n);
WRITE32_BE(data + 2, offset);
encode_msg(io, BSL_CMD_READ_FLASH, data, 4 * 3);
send_msg(io);
ret = recv_msg(io);
if ((ret = recv_type(io)) != BSL_REP_READ_FLASH) {
const char* name = get_bsl_enum_name(ret);
DEG_LOG(E,"excepted response (%s : 0x%04x)",name, ret);
break;
}
nread = READ16_BE(io->raw_buf + 2);
if (n < nread)
ERR_EXIT("unexpected length\n");
if (mem) {
memcpy(mem, io->raw_buf + 4, nread);
mem += nread;
}
if (fo && fwrite(io->raw_buf + 4, 1, nread, fo) != nread) {
ERR_EXIT("fwrite(dump) failed\n");
}
offset += nread;
if (n != nread) break;
}
return offset - start;
}
unsigned dump_flash(spdio_t *io,
uint32_t addr, uint32_t start, uint32_t len,
const char *fn, unsigned step, int mode) {
uint32_t nread = 0;
EnhancedFile fo = my_oxfopen_enhanced(fn, "wb");
if (!fo) ERR_EXIT("fopen(dump) failed\n");
if (mode == 1) {
uint8_t buf[0x34];
len = sizeof(buf);
nread = read_flash(io, addr, start, len, buf, fo.get(), step);
if (nread != len)
ERR_EXIT("can't read DHTB header\n");
// "DHTB" -> "BTHD", Boot Header
if (READ32_LE(buf) != 0x42544844 || READ32_LE(buf + 4) != 1)
ERR_EXIT("unexpected DHTB header\n");
len = READ32_LE(buf + 0x30);
if (len >> 31) ERR_EXIT("unexpected DHTB size (0x%x)\n", len);
len += 0x200;
}
nread += read_flash(io, addr, start + nread, len - nread, NULL, fo.get(), step);
if (mode == 1 && len == nread) do { // read DHTB signature
uint8_t buf[0x60];
uint32_t nread2, nread1;
nread2 = read_flash(io, addr, start + nread, 0x60, buf, NULL, step);
// can be unsigned
if (nread2 != 0x60) break;
if (!READ32_LE(buf + 0x10)) break; // all zeros
if (!~READ32_LE(buf + 0x10)) break; // all ones
if (fo.write(buf, 1, nread2) != nread2)
ERR_EXIT("fwrite(dump) failed\n");
nread1 = nread; len = nread += nread2;
if (READ32_LE(buf + 0x10) != (int)nread1 - 0x200 || // data size
READ32_LE(buf + 0x18) != 0x200 || // data offset
READ32_LE(buf + 0x20) >> 12 || // sign data size (0x254, 0x234)
READ32_LE(buf + 0x28) != (int)nread1 + 0x60) { // sign data offset
DEG_LOG(E,"unexpected DHTB signature\n");
break;
}
len += nread2 = READ32_LE(buf + 0x20);
nread += read_flash(io, addr, start + nread, nread2, NULL, fo.get(), step);
} while (0);
DEG_LOG(I,"Read flash successfully: 0x%08x+0x%x, target: 0x%x, read: 0x%x\n", addr, start, len, nread);
return nread;
}
unsigned dump_mem(spdio_t *io,
uint32_t start, uint32_t len, const char *fn, unsigned step) {
uint32_t n, offset, nread;
int ret;
EnhancedFile fo = my_oxfopen_enhanced(fn, "wb");
if (!fo) ERR_EXIT("fopen(dump) failed\n");
for (offset = start; offset < start + len; ) {
uint32_t *data = (uint32_t *)io->temp_buf;
n = start + len - offset;
if (n > step) n = step;
WRITE32_BE(data, offset);
WRITE32_BE(data + 1, n);
WRITE32_BE(data + 2, 0); // unused
encode_msg_nocpy(io, BSL_CMD_READ_FLASH, 12);
send_msg(io);
ret = recv_msg(io);
if (!ret) ERR_EXIT("timeout reached\n");
if ((ret = recv_type(io)) != BSL_REP_READ_FLASH) {
const char* name = get_bsl_enum_name(ret);
DEG_LOG(E,"excepted response (%s : 0x%04x)",name, ret);
break;
}
nread = READ16_BE(io->raw_buf + 2);
if (n < nread)
ERR_EXIT("excepted length\n");
if (fo.write(io->raw_buf + 4, 1, nread) != nread)
ERR_EXIT("fwrite(dump) failed\n");
offset += nread;
if (n != nread) break;
}
DEG_LOG(I,"Read mem successfully: 0x%08x, target: 0x%x, read: 0x%x", start, len, offset - start);
return offset;
}
int copy_to_wstr(uint16_t *d, size_t n, const char *s) {
size_t i; int a = -1;
for (i = 0; a && i < n; i++) { a = s[i]; WRITE16_LE(d + i, a); }
return a;
}
int copy_from_wstr(char *d, size_t n, const uint16_t *s) {
size_t i; int a = -1;
for (i = 0; a && i < n; i++) { d[i] = a = s[i]; if (a >> 8) break; }
return a;
}
void select_partition(spdio_t *io, const char *name,
uint64_t size, int mode64, int cmd) {
uint32_t t32; uint64_t n64;
struct pkt {
uint16_t name[36];
uint32_t size, size_hi; uint64_t dummy;
} *pkt_ptr;
int ret;
pkt_ptr = (struct pkt *) io->temp_buf;
ret = copy_to_wstr(pkt_ptr->name, 36, name);
if (ret) ERR_EXIT("name too long\n");
n64 = size;
WRITE32_LE(&pkt_ptr->size, n64);
if (mode64) {
t32 = n64 >> 32;
WRITE32_LE(&pkt_ptr->size_hi, t32);
}
encode_msg_nocpy(io, cmd, 72 + (mode64 ? 16 : 4));
}
#if !_WIN32
unsigned long long GetTickCount64() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000 + now.tv_nsec / 1000000;
}
#endif
#define PROGRESS_BAR_WIDTH 40
struct UiProgressData {
double percent;
uint64_t done;
double speed_mb_s;
};
void print_progress_bar(spdio_t* io, uint64_t done, uint64_t total, unsigned long long time0) {
unsigned long long time = GetTickCount64();
if (isCancel) {
return;
}
// 计算进度百分比(0.0 ~ 1.0)
double percent = total ? (done / (double)total) : 0.0;
if (percent < 0.0) percent = 0.0;
if (percent > 1.0) percent = 1.0;
// 终端文本进度条:每次调用都刷新一行,视觉上连续前进
int completed = (int)(PROGRESS_BAR_WIDTH * percent);
if (completed < 0) completed = 0;
if (completed > PROGRESS_BAR_WIDTH) completed = PROGRESS_BAR_WIDTH;
int remaining = PROGRESS_BAR_WIDTH - completed;
DBG_LOG("[");
for (int i = 0; i < completed; i++) {
DBG_LOG("=");
}
for (int i = 0; i < remaining; i++) {
DBG_LOG(" ");
}
double speed_mb_s = (time > time0)
? (double)1000 * done / (double)(time - time0) / 1024.0 / 1024.0
: 0.0;
DBG_LOG("]%6.1f%% Speed:%6.2fMb/s\r", percent * 100.0, speed_mb_s);
if (io->nor_bar) DBG_LOG("\n");
// GUI 进度条和状态文本:每个数据块触发一次更新,保证视觉连续
if (isHelperInit) {
g_main_context_invoke(nullptr, [](gpointer data) -> gboolean {
auto* progress_data = static_cast<UiProgressData*>(data);
double percent_val = progress_data->percent;
uint64_t done_value = progress_data->done;
double speed_val = progress_data->speed_mb_s;
if (isHelperInit) {
// 更新进度条 + 百分比
char percent_text[16];
snprintf(percent_text, sizeof(percent_text), "%.1f%%", percent_val * 100.0);
bottom_bar_set_progress(percent_val, percent_text);
// 更新底部连接状态文本,展示当前进度与速度
char status_text[160];
double mb_done = done_value / (1024.0 * 1024.0);
if (!g_progress_desc.empty()) {
snprintf(status_text, sizeof(status_text),
"%s | %.1f MB | %.2f MB/s",
g_progress_desc.c_str(),
mb_done,
speed_val);
} else {
snprintf(status_text, sizeof(status_text),
"read: %.1f MB | %.2f MB/s",
mb_done,
speed_val);
}
bottom_bar_set_io_status(status_text);
}
delete progress_data;
return G_SOURCE_REMOVE;
}, new UiProgressData{percent, done, speed_mb_s});
}
}
void set_progress_desc(const char* desc) {
if (desc) g_progress_desc = desc;
else g_progress_desc.clear();
}
extern uint64_t fblk_size;
uint64_t dump_partition(spdio_t *io,
const char *name, uint64_t start, uint64_t len,
const char *fn, unsigned step) {
uint32_t n, nread, t32; uint64_t offset, n64, saved_size = 0;
int ret, mode64 = (start + len) >> 32;
char name_tmp[36];
DEG_LOG(OP, "dump_partition: name=%s start=0x%llx len=0x%llx step=%u fblk_size=%llu",
name,
(unsigned long long)start,
(unsigned long long)len,
step,
(unsigned long long)fblk_size);
double rtime = get_time();
DEG_LOG(OP,"Start to read partition %s",name);
DEG_LOG(I,"Type CTRL + C to cancel...");
start_signal();
set_progress_desc(name);
if (!strcmp(name, "super")) {
dump_partition(io, "metadata", 0, check_partition(io, "metadata", 1), "metadata.bin", step);
set_progress_desc(name);
}
else if (!strncmp(name, "userdata", 8)) { if (!check_confirm("read userdata")) return 0; }
else if (strstr(name, "nv1")) {
strcpy(name_tmp, name);
char *dot = strrchr(name_tmp, '1');
if (dot != nullptr) *dot = '2';
name = name_tmp;
start = 512;
if (len > 512)
len -= 512;
}
if (isCancel) { return 0; }
select_partition(io, name, start + len, mode64, BSL_CMD_READ_START);
if (send_and_check(io)) {
encode_msg_nocpy(io, BSL_CMD_READ_END, 0);
send_and_check(io);
return 0;
}
if (isCancel) { return 0; }
EnhancedFile fo = my_oxfopen_enhanced(fn, "wb");
if (!fo) ERR_EXIT("fopen(dump) failed\n");
unsigned long long time_start = GetTickCount64();
for (offset = start; (n64 = start + len - offset); ) {
uint32_t *data = (uint32_t *)io->temp_buf;
n = (uint32_t)(n64 > step ? step : n64);
if (isCancel) {return offset - start; }
WRITE32_LE(data, n);
WRITE32_LE(data + 1, offset);
t32 = offset >> 32;
WRITE32_LE(data + 2, t32);
//if (isCancel) { isCancel = 0;signal(SIGINT, SIG_DFL); return; }
encode_msg_nocpy(io, BSL_CMD_READ_MIDST, mode64 ? 12 : 8);
send_msg(io);
ret = recv_msg(io);
if (!ret) ERR_EXIT("timeout reached\n");
if ((ret = recv_type(io)) != BSL_REP_READ_FLASH) {
const char* name = get_bsl_enum_name(ret);
DEG_LOG(E,"excepted response (%s : 0x%04x)",name, ret);
break;
}
nread = READ16_BE(io->raw_buf + 2);
if (n < nread)
ERR_EXIT("excepted length\n");
if (fo.write(io->raw_buf + 4, 1, nread) != nread)
ERR_EXIT("fwrite(dump) failed\n");
print_progress_bar(io,offset + nread - start, len, time_start);
offset += nread;
if (n != nread) break;
if (fblk_size) {
saved_size += nread;
if (saved_size >= fblk_size) { usleep(1000000); saved_size = 0; }
}
}
double etime = get_time();
double time_spent = etime - rtime;
double mb = len / (1024.0 * 1024.0);
double speed = time_spent > 0 ? (mb / time_spent) : 0.0;
DEG_LOG(I, "dump_partition done: name=%s len=%.1fMB time=%.3fs speed=%.2fMB/s",
name,
mb,
time_spent,
speed);
DEG_LOG(I,"Read partition %s(+0x%llx) successfully, target: 0x%llx, read: 0x%llx",
name, (long long)start, (long long)len,
(long long)(offset - start));
DEG_LOG(I, "Cost time %.6f seconds", time_spent);
set_progress_desc(nullptr);
encode_msg_nocpy(io, BSL_CMD_READ_END, 0);
send_and_check(io);
return offset - start;
}
uint64_t read_pactime(spdio_t *io) {
uint32_t n, offset = 0x81400, len = 8;
int ret; uint32_t *data = (uint32_t *)io->temp_buf;
unsigned long long time, unix1;
select_partition(io, "miscdata", offset + len, 0, BSL_CMD_READ_START);
if (send_and_check(io)) {
encode_msg_nocpy(io, BSL_CMD_READ_END, 0);
send_and_check(io);
return 0;
}
WRITE32_LE(data, len);
WRITE32_LE(data + 1, offset);
encode_msg_nocpy(io, BSL_CMD_READ_MIDST, 8);
send_msg(io);
ret = recv_msg(io);
if (!ret) ERR_EXIT("timeout reached\n");
if ((ret = recv_type(io)) != BSL_REP_READ_FLASH) {
const char* name = get_bsl_enum_name(ret);
DEG_LOG(E,"excepted response (%s : 0x%04x)",name, ret);
encode_msg_nocpy(io, BSL_CMD_READ_END, 0);
send_and_check(io);
return 0;
}
n = READ16_BE(io->raw_buf + 2);
if (n != len) ERR_EXIT("excepted length\n");
time = (uint32_t)READ32_LE(io->raw_buf + 4);
time |= (uint64_t)READ32_LE(io->raw_buf + 8) << 32;
unix1 = time ? time / 10000000 - 11644473600 : 0;
// $ date -d @unixtime
DEG_LOG(I,"pactime = 0x%llx (unix = %llu)", time, unix1);
encode_msg_nocpy(io, BSL_CMD_READ_END, 0);
send_and_check(io);
return time;
}
int scan_xml_partitions(spdio_t *io, const char *fn, uint8_t *buf, size_t buf_size) {
// 1. 读取文件内容
size_t fsize = 0;
char *src = (char *)loadfile(fn, &fsize, 1);
if (!src) ERR_EXIT("loadfile failed\n");
src[fsize] = 0;
// 2. 解析 XML
XmlParser parser;
auto root = parser.parseString(src);
if (!root) {
delete[] src;
ERR_EXIT("Failed to parse XML\n");
}
// 3. 查找 <Partitions> 节点(唯一)
auto partitionsNodes = root->getDescendants("Partitions");
if (partitionsNodes.empty()) {
delete[] src;
ERR_EXIT("No <Partitions> element\n");
}
if (partitionsNodes.size() > 1) {
delete[] src;
ERR_EXIT("xml: more than one partition lists\n");
}
auto partitions = partitionsNodes[0];
// 4. 获取所有 <Partition> 子节点
auto partitionNodes = partitions->getChildren("Partition");
// 5. 分配 ptable 如果需要
if (io->ptable == nullptr)
io->ptable = NEWN partition_t[128];
// 6. 遍历分区,填充 buf 和 ptable
uint8_t *buf_ptr = buf;
size_t remaining = buf_size;
int found = 0;
for (auto& partNode : partitionNodes) {
// 提取 id 属性
std::string id;
auto it_id = partNode->attributes.find("id");
if (it_id != partNode->attributes.end())
id = it_id->second;
if (id.empty()) {
delete[] src;
ERR_EXIT("Partition missing id attribute\n");
}
// 提取 size 属性(支持十进制和十六进制)
std::string sizeStr;
auto it_size = partNode->attributes.find("size");
if (it_size != partNode->attributes.end())
sizeStr = it_size->second;
if (sizeStr.empty()) {
delete[] src;
ERR_EXIT("Partition missing size attribute\n");
}
char *endptr;
long long size = strtoll(sizeStr.c_str(), &endptr, 0); // 自动识别 0x 前缀
if (*endptr != '\0') {
delete[] src;
ERR_EXIT("Invalid size value\n");
}
// 检查缓冲区剩余空间
if (remaining < 0x4c) {
delete[] src;
ERR_EXIT("xml: too many partitions\n");
}
remaining -= 0x4c;
// 清空名称区域(36个16位字符,即72字节)
memset(buf_ptr, 0, 36 * 2);
// 交错写入名称 ASCII(每个字符占用低字节)
for (size_t i = 0; i < id.size() && i < 36; ++i)
buf_ptr[i * 2] = static_cast<uint8_t>(id[i]);
if (id.empty()) {
delete[] src;
ERR_EXIT("empty partition name\n");
}
// 写入原始 size(小端,偏移 0x48)
WRITE32_LE(buf_ptr + 0x48, static_cast<uint32_t>(size));
// 记录到 ptable
strncpy(io->ptable[found].name, id.c_str(), sizeof(io->ptable[found].name) - 1);
io->ptable[found].name[sizeof(io->ptable[found].name) - 1] = '\0';
io->ptable[found].size = size << 20; // 左移 20 位(与原函数一致)
DBG_LOG("[%d] %s, %d\n", found + 1, io->ptable[found].name, (int)size);
buf_ptr += 0x4c;
++found;
}
io->part_count = found;
delete[] src;
return found;
}
#define SECTOR_SIZE 512
#define MAX_SECTORS 32
static int& selected_ab = g_app_state.flash.selected_ab;
int gpt_info(partition_t *ptable, const char *fn_xml, int *part_count_ptr) {
EnhancedFile fp = my_oxfopen_enhanced("pgpt.bin", "rb");
if (!fp) {
return -1;
}
efi_header header;
int bytes_read;
uint8_t buffer[SECTOR_SIZE];
int sector_index = 0;
int found = 0;
while (sector_index < MAX_SECTORS) {
bytes_read = fp.read(buffer, 1, SECTOR_SIZE);
if (bytes_read != SECTOR_SIZE) {
return -1;
}
if (memcmp(buffer, "EFI PART", 8) == 0) {
memcpy(&header, buffer, sizeof(header));
found = 1;
break;
}
sector_index++;
}
if (found == 0) {
return -1;
}
else {
if (sector_index == 1) Da_Info.dwStorageType = 0x102;
else Da_Info.dwStorageType = 0x103;
}
int real_SECTOR_SIZE = SECTOR_SIZE * sector_index;
efi_entry *entries = NEWN efi_entry[header.number_of_partition_entries * sizeof(efi_entry)];
if (entries == nullptr) {
return -1;
}
fp.seek((long)header.partition_entry_lba * real_SECTOR_SIZE, SEEK_SET);
bytes_read = fp.read(entries, 1, header.number_of_partition_entries * sizeof(efi_entry));
if (bytes_read != (int)(header.number_of_partition_entries * sizeof(efi_entry)))
DEG_LOG(I,"read %d/%d only.", bytes_read, (int)(header.number_of_partition_entries * sizeof(efi_entry)));
std::shared_ptr<XmlNode> root = nullptr;
bool needSave = (strcmp(fn_xml, "-") != 0);
if (needSave) {
root = std::make_shared<XmlNode>("Partitions");
}
int n = 0;
for (int i = 0; i < header.number_of_partition_entries; i++) {
efi_entry entry = *(entries + i);
if (entry.starting_lba == 0 && entry.ending_lba == 0) {
n = i;
break;
}
}
DBG_LOG(" 0 %36s %lldKB\n", "splloader", (long long)g_spl_size / 1024);
for (int i = 0; i < n; i++) {
efi_entry entry = *(entries + i);
copy_from_wstr((*(ptable + i)).name, 36, (uint16_t *)entry.partition_name);
uint64_t lba_count = entry.ending_lba - entry.starting_lba + 1;
(*(ptable + i)).size = lba_count * real_SECTOR_SIZE;
DBG_LOG("%3d %36s %7lldMB\n", i + 1, (*(ptable + i)).name,
((*(ptable + i)).size >> 20));
if (needSave) {
auto partitionNode = std::make_shared<XmlNode>("Partition");
partitionNode->setAttribute("id", (*(ptable + i)).name);
if (i + 1 == n) {
partitionNode->setAttribute("size", "0xffffffff");
} else {
char sizeStr[32];
snprintf(sizeStr, sizeof(sizeStr), "%lld", ((*(ptable + i)).size >> 20));
partitionNode->setAttribute("size", sizeStr);
}
root->addChild(partitionNode);
}
if (!selected_ab) {
size_t namelen = strlen((*(ptable + i)).name);
if (namelen > 2 && 0 == strcmp((*(ptable + i)).name + namelen - 2, "_a")) {
selected_ab = 1;
}
}
}
if (needSave) {
if (!root->saveXmlFile(fn_xml)) {
ERR_EXIT("Failed to save XML file\n");
}
}
delete[] entries;
*part_count_ptr = n;
DEG_LOG(I,"standard gpt table saved to pgpt.bin");
DEG_LOG(I,"skip saving sprd partition list packet");
return 0;
}
partition_t *partition_list(spdio_t *io, const char *fn, int *part_count_ptr) {
long size;
unsigned i, n = 0;
int ret; UniqueFile fo = nullptr; uint8_t *p;
partition_t *ptable = NEWN partition_t[128];
if (ptable == nullptr) return nullptr;
DEG_LOG(OP,"Reading partition table...\n");
if (selected_ab < 0) select_ab(io);
int verbose = io->verbose;
io->verbose = 0;
size = dump_partition(io, "user_partition", 0, 32 * 1024, "pgpt.bin", 4096);
io->verbose = verbose;
if (32 * 1024 == size)
g_app_state.flash.gpt_failed = gpt_info(ptable, fn, part_count_ptr);
if (g_app_state.flash.gpt_failed) {
remove("pgpt.bin");
encode_msg_nocpy(io, BSL_CMD_READ_PARTITION, 0);
send_msg(io);
ret = recv_msg(io);
if (!ret) ERR_EXIT("timeout reached\n");
ret = recv_type(io);
if (ret != BSL_REP_READ_PARTITION) {
const char* name = get_bsl_enum_name(ret);
DEG_LOG(E,"excepted response (%s : 0x%04x)",name, ret);
g_app_state.flash.gpt_failed = -1;
delete[](ptable);
return nullptr;
}
size = READ16_BE(io->raw_buf + 2);
if (size % 0x4c) {
DEG_LOG(I,"Not divisible by struct size (0x%04lx)", size);
g_app_state.flash.gpt_failed = -1;
delete[](ptable);
return nullptr;
}
std::shared_ptr<XmlNode> root = nullptr;
bool needSave = (strcmp(fn, "-") != 0);
EnhancedFile fpkt = my_oxfopen_enhanced("sprdpart.bin", "wb");
if (!fpkt) ERR_EXIT("fopen failed\n");
fpkt.write(io->raw_buf + 4, 1, size);
n = size / 0x4c;
if (needSave) {
root = std::make_shared<XmlNode>("Partitions");
}
int divisor = 10;
DEG_LOG(OP, "detecting sector size");
p = io->raw_buf + 4;
for (i = 0; i < n; i++, p += 0x4c) {
size = READ32_LE(p + 0x48);
while (!(size >> divisor)) divisor--;
}
if (Da_Info.dwStorageType == 0) {
if (divisor == 10) Da_Info.dwStorageType = 0x102; // emmc
else Da_Info.dwStorageType = 0x103; // ufs
}
p = io->raw_buf + 4;
DBG_LOG(" 0 %36s %lldKB\n", "splloader", (long long)g_spl_size / 1024);
for (i = 0; i < n; i++, p += 0x4c) {
ret = copy_from_wstr((*(ptable + i)).name, 36, (uint16_t *)p);
if (ret) ERR_EXIT("bad partition name\n");
size = READ32_LE(p + 0x48);
(*(ptable + i)).size = (long long)size << (20 - divisor);
DBG_LOG("%3d %36s %7lldMB\n", i + 1, (*(ptable + i)).name,
((*(ptable + i)).size >> 20));
if (needSave) {
auto partitionNode = std::make_shared<XmlNode>("Partition");
partitionNode->setAttribute("id", (*(ptable + i)).name);
if (i + 1 == n) {
partitionNode->setAttribute("size", "0xffffffff");
} else {
char sizeStr[32];
snprintf(sizeStr, sizeof(sizeStr), "%lld",
((*(ptable + i)).size >> 20));
partitionNode->setAttribute("size", sizeStr);
}
root->addChild(partitionNode);
}
if (!selected_ab) {
size_t namelen = strlen((*(ptable + i)).name);
if (namelen > 2 && 0 == strcmp((*(ptable + i)).name + namelen - 2, "_a")) {
selected_ab = 1;
}
}
}
if (needSave) {
if (!root->saveXmlFile(fn)) {
ERR_EXIT("Failed to save XML file\n");
}
}
*part_count_ptr = n;
DEG_LOG(W, "Unable to get standard gpt table");
DEG_LOG(I, "Sprd partition list packet saved to sprdpart.bin");
g_app_state.flash.gpt_failed = 0;
}
if (*part_count_ptr) {
if (strcmp(fn, "-")) DEG_LOG(I,"Partition list saved to %s\n", fn);
DEG_LOG(I,"Total number of partitions: %d\n", *part_count_ptr);
if (Da_Info.dwStorageType == 0x102) {
DEG_LOG(I,"Storage is emmc\n");
if (isHelperInit) gui_idle_call([]() mutable {
helper.setLabelText(helper.getWidget("storage_mode"),"Emmc");
});
}
else if (Da_Info.dwStorageType == 0x103) {
DEG_LOG(I,"Storage is ufs\n");
if (isHelperInit) gui_idle_call([]() mutable {
helper.setLabelText(helper.getWidget("storage_mode"),"Ufs");
});
}
return ptable;
}
else {
g_app_state.flash.gpt_failed = -1;
delete[](ptable);
return nullptr;
}
}
const char* get_bsl_enum_name(unsigned int value) {
switch (value) {
case 0x00: return "BSL_CMD_CONNECT";
case 0x01: return "BSL_CMD_START_DATA";
case 0x02: return "BSL_CMD_MIDST_DATA";
case 0x03: return "BSL_CMD_END_DATA";
case 0x04: return "BSL_CMD_EXEC_DATA";
case 0x05: return "BSL_CMD_NORMAL_RESET";
case 0x06: return "BSL_CMD_READ_FLASH";
case 0x07: return "BSL_CMD_READ_CHIP_TYPE";
case 0x08: return "BSL_CMD_READ_NVITEM";
case 0x09: return "BSL_CMD_CHANGE_BAUD";
case 0x0A: return "BSL_CMD_ERASE_FLASH";
case 0x0B: return "BSL_CMD_REPARTITION";
case 0x0C: return "BSL_CMD_READ_FLASH_TYPE";
case 0x0D: return "BSL_CMD_READ_FLASH_INFO";
case 0x0F: return "BSL_CMD_READ_SECTOR_SIZE";
case 0x10: return "BSL_CMD_READ_START";
case 0x11: return "BSL_CMD_READ_MIDST";
case 0x12: return "BSL_CMD_READ_END";
case 0x13: return "BSL_CMD_KEEP_CHARGE";
case 0x14: return "BSL_CMD_EXTTABLE";
case 0x15: return "BSL_CMD_READ_FLASH_UID";
case 0x16: return "BSL_CMD_READ_SOFTSIM_EID";
case 0x17: return "BSL_CMD_POWER_OFF";
case 0x19: return "BSL_CMD_CHECK_ROOT/YCC_CMD_SET_BL_A";
case 0x1A: return "BSL_CMD_READ_CHIP_UID/YCC_CMD_SET_BL_B";
case 0x1B: return "BSL_CMD_ENABLE_WRITE_FLASH";
case 0x1C: return "BSL_CMD_ENABLE_SECUREBOOT";
case 0x1D: return "BSL_CMD_IDENTIFY_START";
case 0x1E: return "BSL_CMD_IDENTIFY_END";
case 0x1F: return "BSL_CMD_READ_CU_REF";
case 0x20: return "BSL_CMD_READ_REFINFO";
case 0x21: return "BSL_CMD_DISABLE_TRANSCODE";
case 0x22: return "BSL_CMD_WRITE_APR_INFO";
case 0x23: return "BSL_CMD_CUST_DUMMY";
case 0x24: return "BSL_CMD_READ_RF_TRANSCEIVER_TYPE";
case 0x25: return "BSL_CMD_ENABLE_DEBUG_MODE";
case 0x26: return "BSL_CMD_DDR_CHECK";
case 0x27: return "BSL_CMD_SELF_REFRESH";
case 0x28: return "BSL_CMD_ENABLE_RAW_DATA";
case 0x29: return "BSL_CMD_READ_NAND_BLOCK_INFO";
case 0x2A: return "BSL_CMD_SET_FIRST_MODE";
case 0x2B: return "BSL_CMD_SET_RANDOM_DATA";
case 0x2C: return "BSL_CMD_SET_TIME_STAMP";
case 0x2D: return "BSL_CMD_READ_PARTITION";
case 0x2E: return "BSL_CMD_READ_VCUR_DATA";
case 0x2F: return "BSL_CMD_WRITE_VPAC_DATA";
case 0x31: return "BSL_CMD_MIDST_RAW_START";
case 0x32: return "BSL_CMD_FLUSH_DATA";
case 0x33: return "BSL_CMD_MIDST_RAW_START2";
case 0x34: return "BSL_CMD_ENABLE_UBOOT_LOG";
case 0x35: return "BSL_CMD_DUMP_UBOOT_LOG";
case 0x40: return "BSL_CMD_DISABLE_SELINUX";
case 0x41: return "BSL_CMD_AUTH_BEGIN";
case 0x42: return "BSL_CMD_AUTH_END";
case 0x43: return "BSL_CMD_EMMC_CID";
case 0x44: return "BSL_CMD_OPEN_WATCH_DOG";
case 0x45: return "BSL_CMD_CLOSE_WATCH_DOG";
case 0x46: return "BSL_CMD_POWEROFF_NOKEY";
case 0x47: return "BSL_CMD_WRITE_EFUSE";
case 0x48: return "BSL_CMD_READ_PARTITION_VALUE";
case 0x49: return "BSL_CMD_WRITE_PARTITION_VALUE";
case 0x50: return "BSL_CMD_WRITE_DOWNLOAD_TIMESTAMP";
case 0x51: return "BSL_CMD_PARTITION_SIGNATURE";
case 0xCC: return "BSL_CMD_SEND_FLAG/YCC_REP_SET_BL_SUCCESS";
case 0x7E: return "BSL_CMD_CHECK_BAUD";
case 0x7F: return "BSL_CMD_END_PROCESS";
// Response codes
case 0x80: return "BSL_REP_ACK";
case 0x81: return "BSL_REP_VER";
case 0x82: return "BSL_REP_INVALID_CMD";
case 0x83: return "BSL_REP_UNKNOW_CMD";
case 0x84: return "BSL_REP_OPERATION_FAILED";
case 0x85: return "BSL_REP_NOT_SUPPORT_BAUDRATE";
case 0x86: return "BSL_REP_DOWN_NOT_START";