-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhboot2ctl.c
More file actions
1265 lines (1164 loc) · 43.1 KB
/
Copy pathhboot2ctl.c
File metadata and controls
1265 lines (1164 loc) · 43.1 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
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define MEM_ADDR_NONE UINT64_MAX
#define FORCE_BOOT_NORAML_MAIN 0x10U
#define FORCE_BOOT_NORAML_SLAVE 0x14U
#define FORCE_BOOT_RECOVERY_MAIN 0x18U
#define FORCE_BOOT_RECOVERY_SLAVE 0x1CU
#define FORCE_BOOT_PXE 0x1EU
#define INDICATE_MDC_ADAPTIVE_VAL 0xB2U
#define SCB_CUSTOMER_HEADER_OFFSET 0x1000U
#define SCB_USERTYPE_HW 0x35933595U
#define SCB_USERTYPE_CUSTOMER 0x39593593U
#define ATF_SRAM_FATAL_FLAG 0x39U
#define FLASH_ADDR_BASE 0xC8000000ULL
#define L3_SRAM_BASE 0x90300000ULL
#define SYSCTRL_REG_BASE 0xC0140000ULL
#define UPDATE_FLAG_FLASH_ADDR_A (FLASH_ADDR_BASE + 0x40000U)
#define UPDATE_AREA_FLASH_ADDR_A (FLASH_ADDR_BASE + 0x40004U)
#define FLASH_RECOV_FORCE_MAIN (FLASH_ADDR_BASE + 0x40010U)
#define UPDATE_FLAG_FLASH_ADDR_B (FLASH_ADDR_BASE + 0x50000U)
#define UPDATE_AREA_FLASH_ADDR_B (FLASH_ADDR_BASE + 0x50004U)
#define FLASH_RECOV_FORCE_BAK (FLASH_ADDR_BASE + 0x50010U)
#define SC_EMMC_INIT_FLAG 0x5A5A5A5AU
#define SC_HSM_READY_FLAG 0xDCU
#define SRAM_RECOV_CNT_ADDR (L3_SRAM_BASE + 0x17654U)
#define SRAM_ADAPT_MOD_ADDR (L3_SRAM_BASE + 0x17650U)
#define SRAM_RECOV_INIT_FLAG (L3_SRAM_BASE + 0x17658U)
#define SC_POR_REG0 (SYSCTRL_REG_BASE + 0xEB00U)
#define SC_POR_REG1 (SYSCTRL_REG_BASE + 0xEB04U)
#define SC_POR_REG2 (SYSCTRL_REG_BASE + 0xEB08U)
#define SC_POR_REG3 (SYSCTRL_REG_BASE + 0xEB0CU)
#define SC_BAK_DATA0 (SYSCTRL_REG_BASE + 0xEC80U)
#define SC_BAK_DATA1 (SYSCTRL_REG_BASE + 0xEC84U)
#define SC_BAK_DATA2 (SYSCTRL_REG_BASE + 0xEC88U)
#define SC_BAK_DATA3 (SYSCTRL_REG_BASE + 0xEC8CU)
#define SC_BAK_DATA4 (SYSCTRL_REG_BASE + 0xEC90U)
#define SC_BAK_DATA6 (SYSCTRL_REG_BASE + 0xEC98U)
#define SC_BAK_DATA9 (SYSCTRL_REG_BASE + 0xECA4U)
#define SC_EMMC_INIT_ADDR (SYSCTRL_REG_BASE + 0xEC94U)
#define OS_VERIF_METHOD (SYSCTRL_REG_BASE + 0xECB4U)
#define SC_HSM_READY_ADDR (SYSCTRL_REG_BASE + 0xECBCU)
#define SC_SW_EXCEP_CODE_REG7 (SYSCTRL_REG_BASE + 0xF01CU)
#define SC_SW_EXCEP_CODE_REG8 (SYSCTRL_REG_BASE + 0xF020U)
#define SC_SW_EXCEP_CODE_REG9 (SYSCTRL_REG_BASE + 0xF024U)
#define SC_SW_EXCEP_CODE_REG10 (SYSCTRL_REG_BASE + 0xF028U)
#define SC_SW_EXCEP_CODE_REG11 (SYSCTRL_REG_BASE + 0xF02CU)
#define SC_SW_EXCEP_CODE_REG12 (SYSCTRL_REG_BASE + 0xF030U)
#define SC_VER_VER_REG_ADDR (SYSCTRL_REG_BASE + 0xFFFCU)
enum command_kind {
CMD_DUMP,
CMD_GET,
CMD_SET,
};
struct mem_field {
const char *name;
uint64_t address;
bool writable;
const char *description;
};
struct mem_snapshot {
bool available;
uint32_t board_id; bool board_id_valid;
uint32_t run_img_loc; bool run_img_loc_valid;
uint32_t reset_src; bool reset_src_valid;
uint32_t platform_info; bool platform_info_valid;
uint32_t force_boot; bool force_boot_valid;
uint32_t usb_efuse; bool usb_efuse_valid;
uint32_t pcie_boot_index; bool pcie_boot_index_valid;
uint32_t emmc_init_flag; bool emmc_init_flag_valid;
uint32_t os_verif_method; bool os_verif_method_valid;
uint32_t hsm_ready_flag; bool hsm_ready_flag_valid;
uint32_t firmware_resetcnt; bool firmware_resetcnt_valid;
uint32_t warmstart_flag; bool warmstart_flag_valid;
uint32_t os_resetcnt; bool os_resetcnt_valid;
uint32_t recovery_resetcnt; bool recovery_resetcnt_valid;
uint32_t sw_excep_code7; bool sw_excep_code7_valid;
uint32_t sw_excep_code8; bool sw_excep_code8_valid;
uint32_t sw_excep_code9; bool sw_excep_code9_valid;
uint32_t sw_excep_code10; bool sw_excep_code10_valid;
uint32_t sw_excep_code11; bool sw_excep_code11_valid;
uint32_t sw_excep_code12; bool sw_excep_code12_valid;
uint32_t ver_ver; bool ver_ver_valid;
uint32_t flash_update_flag_main; bool flash_update_flag_main_valid;
uint32_t flash_update_area_main; bool flash_update_area_main_valid;
uint32_t flash_recovery_force_main; bool flash_recovery_force_main_valid;
uint32_t flash_update_flag_back; bool flash_update_flag_back_valid;
uint32_t flash_update_area_back; bool flash_update_area_back_valid;
uint32_t flash_recovery_force_back; bool flash_recovery_force_back_valid;
uint32_t sram_adapt_mode; bool sram_adapt_mode_valid;
uint32_t sram_recovery_count; bool sram_recovery_count_valid;
uint32_t sram_recovery_init_flag; bool sram_recovery_init_flag_valid;
const char *failed_field;
uint64_t failed_address;
int failed_errno;
};
static const struct mem_field g_mem_fields[] = {
{"mem.board_id", SC_BAK_DATA0, true, "SC_BAK_DATA0"},
{"mem.run_img_loc", SC_BAK_DATA1, true, "SC_BAK_DATA1 / AVOIDBRICK_IMG_INDICATE_ADDR"},
{"mem.reset_src", SC_BAK_DATA2, true, "SC_BAK_DATA2"},
{"mem.platform_info", SC_BAK_DATA3, true, "SC_BAK_DATA3"},
{"mem.force_boot", SC_BAK_DATA4, true, "SC_BAK_DATA4"},
{"mem.usb_efuse", SC_BAK_DATA6, true, "SC_BAK_DATA6"},
{"mem.pcie_boot_index", SC_BAK_DATA9, true, "SC_BAK_DATA9"},
{"mem.emmc_init_flag", SC_EMMC_INIT_ADDR, true, "SC_EMMC_INIT_ADDR"},
{"mem.os_verif_method", OS_VERIF_METHOD, true, "OS_VERIF_METHOD"},
{"mem.hsm_ready_flag", SC_HSM_READY_ADDR, true, "SC_HSM_READY_ADDR"},
{"mem.firmware_resetcnt", SC_POR_REG0, true, "SC_POR_REG0"},
{"mem.warmstart_flag", SC_POR_REG1, true, "SC_POR_REG1"},
{"mem.os_resetcnt", SC_POR_REG2, true, "SC_POR_REG2"},
{"mem.recovery_resetcnt", SC_POR_REG3, true, "SC_POR_REG3"},
{"mem.sw_excep_code7", SC_SW_EXCEP_CODE_REG7, true, "SC_SW_EXCEP_CODE_REG7"},
{"mem.sw_excep_code8", SC_SW_EXCEP_CODE_REG8, true, "SC_SW_EXCEP_CODE_REG8"},
{"mem.sw_excep_code9", SC_SW_EXCEP_CODE_REG9, true, "SC_SW_EXCEP_CODE_REG9"},
{"mem.sw_excep_code10", SC_SW_EXCEP_CODE_REG10, true, "SC_SW_EXCEP_CODE_REG10"},
{"mem.sw_excep_code11", SC_SW_EXCEP_CODE_REG11, true, "SC_SW_EXCEP_CODE_REG11"},
{"mem.sw_excep_code12", SC_SW_EXCEP_CODE_REG12, true, "SC_SW_EXCEP_CODE_REG12"},
{"mem.ver_ver", SC_VER_VER_REG_ADDR, true, "SC_VER_VER_REG_ADDR"},
{"flash.update_flag.main", UPDATE_FLAG_FLASH_ADDR_A, true, "UPDATE_FLAG_FLASH_ADDR_A"},
{"flash.update_area.main", UPDATE_AREA_FLASH_ADDR_A, true, "UPDATE_AREA_FLASH_ADDR_A"},
{"flash.recovery_force.main", FLASH_RECOV_FORCE_MAIN, true, "FLASH_RECOV_FORCE_MAIN"},
{"flash.update_flag.back", UPDATE_FLAG_FLASH_ADDR_B, true, "UPDATE_FLAG_FLASH_ADDR_B"},
{"flash.update_area.back", UPDATE_AREA_FLASH_ADDR_B, true, "UPDATE_AREA_FLASH_ADDR_B"},
{"flash.recovery_force.back", FLASH_RECOV_FORCE_BAK, true, "FLASH_RECOV_FORCE_BAK"},
{"sram.adapt_mode", SRAM_ADAPT_MOD_ADDR, true, "SRAM_ADAPT_MOD_ADDR"},
{"sram.recovery_count", SRAM_RECOV_CNT_ADDR, true, "SRAM_RECOV_CNT_ADDR"},
{"sram.recovery_init_flag", SRAM_RECOV_INIT_FLAG, true, "SRAM_RECOV_INIT_FLAG"},
};
static uint32_t read_le32(const uint8_t *p)
{
return (uint32_t)p[0] |
((uint32_t)p[1] << 8) |
((uint32_t)p[2] << 16) |
((uint32_t)p[3] << 24);
}
static void write_le32(uint8_t *p, uint32_t value)
{
p[0] = (uint8_t)(value & 0xFFu);
p[1] = (uint8_t)((value >> 8) & 0xFFu);
p[2] = (uint8_t)((value >> 16) & 0xFFu);
p[3] = (uint8_t)((value >> 24) & 0xFFu);
}
static int read_full_at(int fd, uint8_t *buf, size_t len, uint64_t off)
{
size_t done = 0;
while (done < len) {
ssize_t ret = pread(fd, buf + done, len - done, (off_t)(off + done));
if (ret < 0) {
return -1;
}
if (ret == 0) {
errno = EIO;
return -1;
}
done += (size_t)ret;
}
return 0;
}
static int write_full_at(int fd, const uint8_t *buf, size_t len, uint64_t off)
{
size_t done = 0;
while (done < len) {
ssize_t ret = pwrite(fd, buf + done, len - done, (off_t)(off + done));
if (ret < 0) {
return -1;
}
if (ret == 0) {
errno = EIO;
return -1;
}
done += (size_t)ret;
}
return 0;
}
static int read_u32_at(int fd, uint64_t off, uint32_t *value_out)
{
uint8_t buf[4];
if (read_full_at(fd, buf, sizeof(buf), off) != 0) {
return -1;
}
*value_out = read_le32(buf);
return 0;
}
static int write_u32_at(int fd, uint64_t off, uint32_t value)
{
uint8_t buf[4];
write_le32(buf, value);
return write_full_at(fd, buf, sizeof(buf), off);
}
static bool mem_access_uses_mmap(const char *mem_path, int fd)
{
struct stat st;
if (mem_path == NULL || strcmp(mem_path, "/dev/mem") != 0) {
return false;
}
if (fstat(fd, &st) != 0) {
return false;
}
return S_ISCHR(st.st_mode);
}
static int read_u32_mmap(int fd, uint64_t off, uint32_t *value_out)
{
long page_size = sysconf(_SC_PAGE_SIZE);
uint64_t aligned;
size_t page_off;
size_t map_len;
void *map;
uint8_t buf[4];
if (page_size <= 0) {
errno = EINVAL;
return -1;
}
aligned = off - (off % (uint64_t)page_size);
page_off = (size_t)(off - aligned);
if (page_off > SIZE_MAX - sizeof(buf)) {
errno = EOVERFLOW;
return -1;
}
map_len = page_off + sizeof(buf);
map = mmap(NULL, map_len, PROT_READ, MAP_SHARED, fd, (off_t)aligned);
if (map == MAP_FAILED) {
return -1;
}
memcpy(buf, (const uint8_t *)map + page_off, sizeof(buf));
if (munmap(map, map_len) != 0) {
return -1;
}
*value_out = read_le32(buf);
return 0;
}
static int write_u32_mmap(int fd, uint64_t off, uint32_t value)
{
long page_size = sysconf(_SC_PAGE_SIZE);
uint64_t aligned;
size_t page_off;
size_t map_len;
void *map;
uint8_t buf[4];
int saved_errno;
if (page_size <= 0) {
errno = EINVAL;
return -1;
}
aligned = off - (off % (uint64_t)page_size);
page_off = (size_t)(off - aligned);
if (page_off > SIZE_MAX - sizeof(buf)) {
errno = EOVERFLOW;
return -1;
}
map_len = page_off + sizeof(buf);
map = mmap(NULL, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)aligned);
if (map == MAP_FAILED) {
return -1;
}
write_le32(buf, value);
memcpy((uint8_t *)map + page_off, buf, sizeof(buf));
if (msync(map, map_len, MS_SYNC) != 0) {
saved_errno = errno;
(void)munmap(map, map_len);
errno = saved_errno;
return -1;
}
if (munmap(map, map_len) != 0) {
return -1;
}
return 0;
}
static int read_phys_u32(int fd, uint64_t off, bool use_mmap, uint32_t *value_out)
{
if (use_mmap) {
return read_u32_mmap(fd, off, value_out);
}
return read_u32_at(fd, off, value_out);
}
static int write_phys_u32(int fd, uint64_t off, bool use_mmap, uint32_t value)
{
if (use_mmap) {
return write_u32_mmap(fd, off, value);
}
return write_u32_at(fd, off, value);
}
static int parse_integer_arg(const char *text, uint64_t *value_out)
{
char *endptr;
unsigned long long value;
errno = 0;
value = strtoull(text, &endptr, 0);
if (errno != 0 || endptr == text || *endptr != '\0') {
return -1;
}
*value_out = (uint64_t)value;
return 0;
}
static const char *force_boot_mode_name(uint32_t raw)
{
uint32_t val = raw & 0x1FU;
if (val == FORCE_BOOT_NORAML_MAIN) {
return "normal-main";
}
if (val == FORCE_BOOT_NORAML_SLAVE) {
return "normal-back";
}
if (val == FORCE_BOOT_RECOVERY_MAIN) {
return "recovery-main";
}
if (val == FORCE_BOOT_RECOVERY_SLAVE) {
return "recovery-back";
}
if (val == FORCE_BOOT_PXE) {
return "pxe";
}
return "non-force";
}
static const char *platform_type_name(uint32_t ver_ver)
{
if (ver_ver == 0) {
return "asic";
}
switch ((ver_ver >> 16) & 0xFFFFu) {
case 0x1U:
return "emu";
case 0x2U:
return "esl";
default:
return "fpga";
}
}
static uint32_t platform_version_value(uint32_t ver_ver)
{
return ver_ver & 0xFFFFu;
}
static const char *os_verif_method_name(uint32_t value)
{
if (value == SCB_USERTYPE_HW) {
return "huawei-header";
}
if (value == SCB_USERTYPE_CUSTOMER) {
return "customer-header";
}
return "non-hw/custom-like";
}
static uint32_t os_verif_head_offset(uint32_t value)
{
return value == SCB_USERTYPE_HW ? 0U : SCB_CUSTOMER_HEADER_OFFSET;
}
static const char *emmc_init_state_name(uint32_t value)
{
if (value == SC_EMMC_INIT_FLAG) {
return "init-flag-set";
}
if (value == 0) {
return "cleared";
}
return "unknown";
}
static const char *hsm_ready_state_name(uint32_t value)
{
if ((value & 0xFFu) == SC_HSM_READY_FLAG) {
return "ready";
}
if ((value & 0xFFu) == 0) {
return "cleared";
}
return "waiting";
}
static const char *run_img_medium_name(uint32_t value)
{
if ((value & 0x80U) != 0) {
return "ssd";
}
if ((value & 0x20U) != 0) {
return "emmc";
}
return "unknown-medium";
}
static const char *run_img_slot_name(uint32_t value)
{
return (value & 0x1FU) == 0 ? "main" : "back";
}
static const char *reset_src_state_name(uint32_t value)
{
return (value & 0xFFU) == 0 ? "cold-boot-like" : "non-cold-reset";
}
static const char *platform_mode_name(uint32_t value)
{
switch (value & 0x3U) {
case 0x0U:
return "dc";
case 0x2U:
return "mdc";
default:
return "other";
}
}
static const char *platform_chip_count_name(uint32_t value)
{
switch ((value >> 3) & 0x3U) {
case 0x1U:
return "1p";
case 0x2U:
return "2p";
default:
return "unknown";
}
}
static const char *platform_soc_name(uint32_t value)
{
return ((value >> 8) & 0x1U) == 0 ? "soc-a" : "soc-b";
}
static const char *sram_adapt_mode_name(uint32_t value)
{
if (value == INDICATE_MDC_ADAPTIVE_VAL) {
return "mdc-adaptive";
}
if (value == 0) {
return "normal";
}
return "unknown";
}
static const char *reboot_reason_name(uint32_t value)
{
switch (value & 0xFFu) {
case 0x00U: return "clear";
case 0x01U: return "bios-exception";
case 0x02U: return "hotboot";
case 0x17U: return "a-core-watchdog";
case 0x18U: return "lpm3-global-watchdog";
case 0x1AU: return "lpmcu-watchdog";
case 0x1FU: return "tsensor-reset";
case 0x20U: return "pmu-exception";
case 0x21U: return "ddr-watchdog";
case 0x22U: return "ddr-fatal";
case 0x24U: return "panic";
case 0x25U: return "noc-exception";
case 0x26U: return "soft-lockup";
case 0x27U: return "ddrc-sec";
case 0x29U: return "os-coredump";
case 0x2AU: return "oom";
case 0x2BU: return "hdc-disconnect";
case 0x2CU: return "startup-exception";
case 0x2DU: return "heartbeat-exception";
case 0x2EU: return "run-exception";
case 0x32U: return "lpm3-exception";
case 0x33U: return "ts-exception";
case 0x34U: return "aicpu-exception";
case 0x35U: return "dvpp-exception";
case 0x36U: return "driver-exception";
case 0x37U: return "zip-exception";
case 0x38U: return "tee-exception";
case 0x39U: return "lpfw-exception";
case 0x3AU: return "network-exception";
case 0x3BU: return "hsm-exception";
case 0x3CU: return "atf-exception";
case 0x3DU: return "isp-exception";
case 0x3EU: return "safetyisland-exception";
case 0x3FU: return "toolchain-exception";
case 0x40U: return "cluster-exception";
case 0x41U: return "comisolator-exception";
case 0x42U: return "sd-exception";
case 0x43U: return "dp-exception";
case 0x55U: return "cpu-buck-reboot";
case 0x60U: return "suspend-fail";
case 0x61U: return "resume-fail";
case 0x62U: return "cpucore-exception";
case 0x6AU: return "xloader-ddrinit-fail";
case 0x6CU: return "xloader-load-fail";
case 0x6DU: return "xloader-verify-fail";
case 0x6EU: return "xloader-watchdog";
case 0x75U: return "fastboot-panic";
case 0x76U: return "fastboot-watchdog";
case 0x77U: return "fastboot-ocv-voltage-error";
case 0x78U: return "uefi-pcie-no-file";
case 0x79U: return "uefi-pcie-file-too-large";
case 0x7AU: return "uefi-flash-enter-4b-fail";
case 0x7BU: return "uefi-flash-exit-4b-fail";
case 0x7CU: return "uefi-flash-copy-os-fail";
case 0x7DU: return "uefi-sd-ext2-read-fail";
case 0x7EU: return "uefi-sd-hw-recovery-fail";
case 0x7FU: return "uefi-log-atu-remap-err";
case 0x80U: return "uefi-hot-reset";
case 0x81U: return "uefi-sd-boot-fail";
case 0x82U: return "uefi-tee-verify-fail";
case 0x83U: return "uefi-lpmcu-verify-fail";
case 0x84U: return "uefi-dtb-verify-fail";
case 0x85U: return "uefi-img-verify-fail";
case 0x86U: return "uefi-fs-verify-fail";
case 0x87U: return "uefi-fdt-update-fail";
case 0x88U: return "uefi-shutdown-bs-fail";
case 0x8AU: return "device-load-timeout";
case 0x8BU: return "device-heartbeat-lost";
case 0x8CU: return "device-reset-inform";
case 0x8DU: return "device-aer";
case 0x90U: return "native-boot-fail";
case 0x91U: return "boot-timeout";
case 0x92U: return "framework-boot-fail";
case 0x93U: return "native-data-fail";
case 0xFFU: return "invalid";
default:
return "unknown";
}
}
static const char *usb_efuse_name(uint32_t value)
{
return (value & (1U << 1)) != 0 ? "usb-boot-not-forced" : "usb-boot-may-trigger";
}
static const char *flash_update_flag_name(uint32_t value)
{
return value == 0xD6C55BC1U ? "update-scene" : "inactive";
}
static const char *flash_update_area_name(uint32_t value)
{
return value == 0xC11CB55BU ? "main-a" : "back-b-or-other";
}
static const char *flash_recovery_force_name(uint32_t value)
{
if (value == UINT32_MAX) {
return "erased-or-unset";
}
if (value == 0) {
return "clear";
}
return "set";
}
static const char *warmstart_flag_name(uint32_t value)
{
return value == 0 ? "cold-entry" : "warm-start";
}
static const char *next_try_slot_name(uint32_t value)
{
uint32_t next = (value + 1U) % 8U;
return next < 4U ? "main" : "back";
}
static const char *recovery_cycle_name(uint32_t value)
{
uint32_t mod = value % 16U;
if (mod <= 7U) {
return "os-range";
}
if (mod <= 11U) {
return "recovery-main-range";
}
return "recovery-back-range";
}
static void print_reboot_reason_line(const char *label, uint32_t value, bool valid, const char *role)
{
if (!valid) {
printf(" %-19s: unavailable\n", label);
return;
}
if ((value & 0xFFu) == 0) {
printf(" %-19s: 0x%08" PRIx32 " (clear, %s)\n", label, value, role);
return;
}
printf(" %-19s: 0x%08" PRIx32 " (%s, %s)\n", label, value, reboot_reason_name(value), role);
}
static void print_mem_field_value(const struct mem_field *field, uint32_t value)
{
if (strcmp(field->name, "mem.run_img_loc") == 0) {
printf("0x%08" PRIx32 " (medium=%s, slot=%s)\n",
value, run_img_medium_name(value), run_img_slot_name(value));
return;
}
if (strcmp(field->name, "mem.reset_src") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, reset_src_state_name(value));
return;
}
if (strcmp(field->name, "mem.platform_info") == 0) {
printf("0x%08" PRIx32 " (mode=%s, chip=%s, %s)\n",
value, platform_mode_name(value), platform_chip_count_name(value), platform_soc_name(value));
return;
}
if (strcmp(field->name, "mem.force_boot") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, force_boot_mode_name(value));
return;
}
if (strcmp(field->name, "mem.usb_efuse") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, usb_efuse_name(value));
return;
}
if (strcmp(field->name, "mem.pcie_boot_index") == 0) {
printf("0x%08" PRIx32 " (mailbox-file-index=%" PRIu32 ")\n", value, value);
return;
}
if (strcmp(field->name, "mem.emmc_init_flag") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, emmc_init_state_name(value));
return;
}
if (strcmp(field->name, "mem.os_verif_method") == 0) {
printf("0x%08" PRIx32 " (%s, head_offset=0x%04" PRIx32 ")\n",
value, os_verif_method_name(value), os_verif_head_offset(value));
return;
}
if (strcmp(field->name, "mem.hsm_ready_flag") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, hsm_ready_state_name(value));
return;
}
if (strcmp(field->name, "mem.firmware_resetcnt") == 0) {
printf("0x%08" PRIx32 " (%" PRIu32 ", mod8=%" PRIu32 ")\n", value, value, value % 8U);
return;
}
if (strcmp(field->name, "mem.warmstart_flag") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, warmstart_flag_name(value));
return;
}
if (strcmp(field->name, "mem.os_resetcnt") == 0) {
printf("0x%08" PRIx32 " (%" PRIu32 ", next_try=%s)\n", value, value, next_try_slot_name(value));
return;
}
if (strcmp(field->name, "mem.recovery_resetcnt") == 0) {
printf("0x%08" PRIx32 " (%" PRIu32 ", next_try=%s)\n", value, value, next_try_slot_name(value));
return;
}
if (strcmp(field->name, "mem.ver_ver") == 0) {
printf("0x%08" PRIx32 " (platform=%s, version=0x%04" PRIx32 ")\n",
value, platform_type_name(value), platform_version_value(value));
return;
}
if (strcmp(field->name, "flash.update_flag.main") == 0 ||
strcmp(field->name, "flash.update_flag.back") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, flash_update_flag_name(value));
return;
}
if (strcmp(field->name, "flash.update_area.main") == 0 ||
strcmp(field->name, "flash.update_area.back") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, flash_update_area_name(value));
return;
}
if (strcmp(field->name, "flash.recovery_force.main") == 0 ||
strcmp(field->name, "flash.recovery_force.back") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, flash_recovery_force_name(value));
return;
}
if (strcmp(field->name, "sram.adapt_mode") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, sram_adapt_mode_name(value));
return;
}
if (strcmp(field->name, "mem.sw_excep_code8") == 0) {
printf("0x%08" PRIx32 " (%s)\n", value,
value == 0 ? "clear" : "boot-stage marker / expected OS_START_POINT");
return;
}
if (strcmp(field->name, "mem.sw_excep_code12") == 0) {
if ((value & 0xFFu) == ATF_SRAM_FATAL_FLAG) {
printf("0x%08" PRIx32 " (atf-sram-fatal)\n", value);
} else {
printf("0x%08" PRIx32 " (%s)\n", value, reboot_reason_name(value));
}
return;
}
if (strncmp(field->name, "mem.sw_excep_code", 17) == 0) {
printf("0x%08" PRIx32 " (%s)\n", value, reboot_reason_name(value));
return;
}
if (strcmp(field->name, "sram.recovery_init_flag") == 0) {
printf("0x%08" PRIx32 " (raw; no semantic use found in hboot)\n", value);
return;
}
if (strcmp(field->name, "sram.recovery_count") == 0) {
printf("0x%08" PRIx32 " (%" PRIu32 ", mod16=%" PRIu32 ", %s)\n",
value, value, value % 16U, recovery_cycle_name(value));
return;
}
printf("0x%08" PRIx32 " (%" PRIu32 ")\n", value, value);
}
static bool prompt_yes_no(const char *prompt)
{
char answer[32];
size_t len;
size_t i;
fprintf(stderr, "%s", prompt);
fflush(stderr);
if (fgets(answer, sizeof(answer), stdin) == NULL) {
return false;
}
len = strlen(answer);
while (len > 0 && isspace((unsigned char)answer[len - 1])) {
answer[--len] = '\0';
}
for (i = 0; i < len; ++i) {
answer[i] = (char)tolower((unsigned char)answer[i]);
}
return strcmp(answer, "y") == 0 || strcmp(answer, "yes") == 0;
}
static const struct mem_field *find_mem_field(const char *name)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(g_mem_fields); ++i) {
if (strcmp(g_mem_fields[i].name, name) == 0) {
return &g_mem_fields[i];
}
}
return NULL;
}
static int read_mem_field_value(int fd, const struct mem_field *field, bool use_mmap, uint32_t *value_out)
{
if (field->address == MEM_ADDR_NONE) {
errno = ENOTSUP;
return -1;
}
return read_phys_u32(fd, field->address, use_mmap, value_out);
}
static int write_mem_field_value(int fd, const struct mem_field *field, bool use_mmap, uint32_t value)
{
if (field->address == MEM_ADDR_NONE) {
errno = ENOTSUP;
return -1;
}
return write_phys_u32(fd, field->address, use_mmap, value);
}
static int load_mem_snapshot(int fd, bool use_mmap, struct mem_snapshot *snapshot)
{
struct {
const struct mem_field *field;
uint32_t *value_out;
bool *valid_out;
} fields[] = {
{&g_mem_fields[0], &snapshot->board_id, &snapshot->board_id_valid},
{&g_mem_fields[1], &snapshot->run_img_loc, &snapshot->run_img_loc_valid},
{&g_mem_fields[2], &snapshot->reset_src, &snapshot->reset_src_valid},
{&g_mem_fields[3], &snapshot->platform_info, &snapshot->platform_info_valid},
{&g_mem_fields[4], &snapshot->force_boot, &snapshot->force_boot_valid},
{&g_mem_fields[5], &snapshot->usb_efuse, &snapshot->usb_efuse_valid},
{&g_mem_fields[6], &snapshot->pcie_boot_index, &snapshot->pcie_boot_index_valid},
{&g_mem_fields[7], &snapshot->emmc_init_flag, &snapshot->emmc_init_flag_valid},
{&g_mem_fields[8], &snapshot->os_verif_method, &snapshot->os_verif_method_valid},
{&g_mem_fields[9], &snapshot->hsm_ready_flag, &snapshot->hsm_ready_flag_valid},
{&g_mem_fields[10], &snapshot->firmware_resetcnt, &snapshot->firmware_resetcnt_valid},
{&g_mem_fields[11], &snapshot->warmstart_flag, &snapshot->warmstart_flag_valid},
{&g_mem_fields[12], &snapshot->os_resetcnt, &snapshot->os_resetcnt_valid},
{&g_mem_fields[13], &snapshot->recovery_resetcnt, &snapshot->recovery_resetcnt_valid},
{&g_mem_fields[14], &snapshot->sw_excep_code7, &snapshot->sw_excep_code7_valid},
{&g_mem_fields[15], &snapshot->sw_excep_code8, &snapshot->sw_excep_code8_valid},
{&g_mem_fields[16], &snapshot->sw_excep_code9, &snapshot->sw_excep_code9_valid},
{&g_mem_fields[17], &snapshot->sw_excep_code10, &snapshot->sw_excep_code10_valid},
{&g_mem_fields[18], &snapshot->sw_excep_code11, &snapshot->sw_excep_code11_valid},
{&g_mem_fields[19], &snapshot->sw_excep_code12, &snapshot->sw_excep_code12_valid},
{&g_mem_fields[20], &snapshot->ver_ver, &snapshot->ver_ver_valid},
{&g_mem_fields[21], &snapshot->flash_update_flag_main, &snapshot->flash_update_flag_main_valid},
{&g_mem_fields[22], &snapshot->flash_update_area_main, &snapshot->flash_update_area_main_valid},
{&g_mem_fields[23], &snapshot->flash_recovery_force_main, &snapshot->flash_recovery_force_main_valid},
{&g_mem_fields[24], &snapshot->flash_update_flag_back, &snapshot->flash_update_flag_back_valid},
{&g_mem_fields[25], &snapshot->flash_update_area_back, &snapshot->flash_update_area_back_valid},
{&g_mem_fields[26], &snapshot->flash_recovery_force_back, &snapshot->flash_recovery_force_back_valid},
{&g_mem_fields[27], &snapshot->sram_adapt_mode, &snapshot->sram_adapt_mode_valid},
{&g_mem_fields[28], &snapshot->sram_recovery_count, &snapshot->sram_recovery_count_valid},
{&g_mem_fields[29], &snapshot->sram_recovery_init_flag, &snapshot->sram_recovery_init_flag_valid},
};
size_t i;
bool any_loaded = false;
memset(snapshot, 0, sizeof(*snapshot));
for (i = 0; i < ARRAY_SIZE(fields); ++i) {
if (read_mem_field_value(fd, fields[i].field, use_mmap, fields[i].value_out) == 0) {
*fields[i].valid_out = true;
any_loaded = true;
continue;
}
if (snapshot->failed_field == NULL) {
snapshot->failed_field = fields[i].field->name;
snapshot->failed_address = fields[i].field->address;
snapshot->failed_errno = errno;
}
}
snapshot->available = any_loaded;
if (any_loaded) {
return 0;
}
errno = snapshot->failed_errno != 0 ? snapshot->failed_errno : ENODEV;
return -1;
}
static void dump_value_line_hex(const char *label, uint32_t value, bool valid)
{
if (valid) {
printf(" %-19s: 0x%08" PRIx32 "\n", label, value);
} else {
printf(" %-19s: unavailable\n", label);
}
}
static void dump_mem(const struct mem_snapshot *mem)
{
printf("[mem]\n");
dump_value_line_hex("board_id", mem->board_id, mem->board_id_valid);
if (mem->run_img_loc_valid) {
printf(" %-19s: 0x%08" PRIx32 " (medium=%s, slot=%s)\n",
"run_img_loc", mem->run_img_loc,
run_img_medium_name(mem->run_img_loc), run_img_slot_name(mem->run_img_loc));
} else {
printf(" %-19s: unavailable\n", "run_img_loc");
}
if (mem->reset_src_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n",
"reset_src", mem->reset_src, reset_src_state_name(mem->reset_src));
} else {
printf(" %-19s: unavailable\n", "reset_src");
}
if (mem->platform_info_valid) {
printf(" %-19s: 0x%08" PRIx32 " (mode=%s, chip=%s, %s)\n",
"platform_info", mem->platform_info,
platform_mode_name(mem->platform_info),
platform_chip_count_name(mem->platform_info),
platform_soc_name(mem->platform_info));
} else {
printf(" %-19s: unavailable\n", "platform_info");
}
if (mem->force_boot_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n", "force_boot", mem->force_boot,
force_boot_mode_name(mem->force_boot));
} else {
printf(" %-19s: unavailable\n", "force_boot");
}
if (mem->usb_efuse_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n",
"usb_efuse", mem->usb_efuse, usb_efuse_name(mem->usb_efuse));
} else {
printf(" %-19s: unavailable\n", "usb_efuse");
}
if (mem->pcie_boot_index_valid) {
printf(" %-19s: 0x%08" PRIx32 " (mailbox-file-index=%" PRIu32 ")\n",
"pcie_boot_index", mem->pcie_boot_index, mem->pcie_boot_index);
} else {
printf(" %-19s: unavailable\n", "pcie_boot_index");
}
if (mem->emmc_init_flag_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n", "emmc_init_flag", mem->emmc_init_flag,
emmc_init_state_name(mem->emmc_init_flag));
} else {
printf(" %-19s: unavailable\n", "emmc_init_flag");
}
if (mem->os_verif_method_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s, head_offset=0x%04" PRIx32 ")\n",
"os_verif_method", mem->os_verif_method,
os_verif_method_name(mem->os_verif_method),
os_verif_head_offset(mem->os_verif_method));
} else {
printf(" %-19s: unavailable\n", "os_verif_method");
}
if (mem->hsm_ready_flag_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n", "hsm_ready_flag", mem->hsm_ready_flag,
hsm_ready_state_name(mem->hsm_ready_flag));
} else {
printf(" %-19s: unavailable\n", "hsm_ready_flag");
}
if (mem->firmware_resetcnt_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%" PRIu32 ", mod8=%" PRIu32 ")\n",
"firmware_resetcnt", mem->firmware_resetcnt,
mem->firmware_resetcnt, mem->firmware_resetcnt % 8U);
} else {
printf(" %-19s: unavailable\n", "firmware_resetcnt");
}
if (mem->warmstart_flag_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n",
"warmstart_flag", mem->warmstart_flag, warmstart_flag_name(mem->warmstart_flag));
} else {
printf(" %-19s: unavailable\n", "warmstart_flag");
}
if (mem->os_resetcnt_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%" PRIu32 ", next_try=%s)\n",
"os_resetcnt", mem->os_resetcnt, mem->os_resetcnt, next_try_slot_name(mem->os_resetcnt));
} else {
printf(" %-19s: unavailable\n", "os_resetcnt");
}
if (mem->recovery_resetcnt_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%" PRIu32 ", next_try=%s)\n",
"recovery_resetcnt", mem->recovery_resetcnt,
mem->recovery_resetcnt, next_try_slot_name(mem->recovery_resetcnt));
} else {
printf(" %-19s: unavailable\n", "recovery_resetcnt");
}
print_reboot_reason_line("sw_excep_code7", mem->sw_excep_code7, mem->sw_excep_code7_valid, "os");
if (mem->sw_excep_code8_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n", "sw_excep_code8", mem->sw_excep_code8,
mem->sw_excep_code8 == 0 ? "clear" : "boot-stage marker / expected OS_START_POINT");
} else {
printf(" %-19s: unavailable\n", "sw_excep_code8");
}
print_reboot_reason_line("sw_excep_code9", mem->sw_excep_code9, mem->sw_excep_code9_valid, "low-power");
print_reboot_reason_line("sw_excep_code10", mem->sw_excep_code10, mem->sw_excep_code10_valid,
"safetyisland-or-os");
print_reboot_reason_line("sw_excep_code11", mem->sw_excep_code11, mem->sw_excep_code11_valid, "aos");
if (mem->sw_excep_code12_valid) {
if ((mem->sw_excep_code12 & 0xFFu) == ATF_SRAM_FATAL_FLAG) {
printf(" %-19s: 0x%08" PRIx32 " (atf-sram-fatal)\n", "sw_excep_code12", mem->sw_excep_code12);
} else if ((mem->sw_excep_code12 & 0xFFu) == 0) {
printf(" %-19s: 0x%08" PRIx32 " (clear, atf)\n", "sw_excep_code12", mem->sw_excep_code12);
} else {
printf(" %-19s: 0x%08" PRIx32 " (%s, atf)\n", "sw_excep_code12", mem->sw_excep_code12,
reboot_reason_name(mem->sw_excep_code12));
}
} else {
printf(" %-19s: unavailable\n", "sw_excep_code12");
}
if (mem->ver_ver_valid) {
printf(" %-19s: 0x%08" PRIx32 " (platform=%s, version=0x%04" PRIx32 ")\n",
"ver_ver", mem->ver_ver, platform_type_name(mem->ver_ver), platform_version_value(mem->ver_ver));
} else {
printf(" %-19s: unavailable\n", "ver_ver");
}
if (mem->flash_update_flag_main_valid) {
printf(" %-19s: 0x%08" PRIx32 " (%s)\n",
"flash.upd_flag.a", mem->flash_update_flag_main, flash_update_flag_name(mem->flash_update_flag_main));
} else {
printf(" %-19s: unavailable\n", "flash.upd_flag.a");
}
if (mem->flash_update_area_main_valid) {