-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfg.c
More file actions
executable file
·2610 lines (2212 loc) · 69.4 KB
/
mfg.c
File metadata and controls
executable file
·2610 lines (2212 loc) · 69.4 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
/*
* Microsemi Switchtec(tm) PCIe Management Command Line Interface
* Copyright (c) 2019, Microsemi Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifdef __linux__
#include "commands.h"
#include "argconfig.h"
#include "suffix.h"
#include "progress.h"
#include "gui.h"
#include "common.h"
#include "progress.h"
#include "config.h"
#include <switchtec/switchtec.h>
#include <switchtec/utils.h>
#include <switchtec/mfg.h>
#include <switchtec/ini.h>
#include <switchtec/endian.h>
#include <locale.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/sha.h>
#include <lib/switchtec_priv.h>
#define PCI100X_DEV_FAM 0
#define PING_SPI_CLK_SEL_MASK 0x30000
#define PING_SPI_CLK_SEL_OFFSET 16
#define PING_SPI_BUS_RATE_MASK 0xFFFF
#define MIN_PORT 1
#define MAX_PORT 65535
static const struct argconfig_choice recovery_mode_choices[] = {
{"I2C", SWITCHTEC_BL2_RECOVERY_I2C, "I2C"},
{"XMODEM", SWITCHTEC_BL2_RECOVERY_XMODEM, "XModem"},
{"BOTH", SWITCHTEC_BL2_RECOVERY_I2C_AND_XMODEM,
"both I2C and XModem (default)"},
{}
};
static const struct argconfig_choice secure_state_choices[] = {
{"INITIALIZED_UNSECURED", SWITCHTEC_INITIALIZED_UNSECURED,
"unsecured state"},
{"INITIALIZED_SECURED", SWITCHTEC_INITIALIZED_SECURED,
"secured state"},
{}
};
static const char* parse_recovery_reason(enum stmfd_rcvry_reason phase_id)
{
const char * reason_str[] = { "Unknown Phase",
"BL1 - Strap Assertion",
"BL1 - Firmware Assertion",
"BL2 - Strap Assertion",
"BL2 - Firmware Assertion",
"BL2 - Image Execute",
"Main FW",
"Minimal Mode"};
uint8_t reason_idx = 0;
if ((phase_id >= STMFD_RCVRY_BL1_STRAP) && (phase_id <= STMFD_RCVRY_MINIMAL_MODE))
{
reason_idx = phase_id;
}
return reason_str[reason_idx];
}
/**
* @brief Parses SPI frequency parameters got from ping command and calculates
* the clock rate.
*
* This function takes the SPI parameters and computes the corresponding
* SPI clock rate. The result is returned from the function through pointer clk_rate.
*
* @param[in] spi_params The SPI parameters used to calculate the clock rate.
* @param[out] clk_rate Pointer to a float where the calculated clock rate will be stored.
*
* @return Returns true if the parsing was successful, false otherwise.
*
* @note This function assumes that spi_params contains valid data.
*/
static bool spi_frequency_parser(uint32_t spi_params, float * clk_rate)
{
float dcsu_lookup[] = {400.0, 501.0, 550.0, 600.0};
int bus_rate_max[] = {7, 9, 10, 11};
float dcsu_value = 0;
int clk_sel = 0;
int bus_rate = 0;
clk_sel = (spi_params & PING_SPI_CLK_SEL_MASK) >> PING_SPI_CLK_SEL_OFFSET;
bus_rate = (spi_params & PING_SPI_BUS_RATE_MASK);
if (bus_rate > bus_rate_max[clk_sel])
{
return false;
}
dcsu_value = dcsu_lookup[clk_sel];
*clk_rate = dcsu_value / (2 * (bus_rate + 1));
return true;
}
#define CMD_DESC_PING "ping device and get current boot phase"
static int ping(int argc, char **argv)
{
int ret;
static struct {
struct switchtec_dev *dev;
bool extended_cmd;
} cfg = {
.extended_cmd = false};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"extended_cmd", 'x', "", CFG_NONE, &cfg.extended_cmd, no_argument,
"print extended ping command information"},
{NULL}
};
struct ping_dev_info ping_info;
char version_str[12];
float clk_rate;
argconfig_parse(argc, argv, CMD_DESC_PING, opts, &cfg, sizeof(cfg));
ret = switchtec_get_device_info(cfg.dev, NULL, NULL, NULL, &ping_info);
if (ret) {
switchtec_perror("mfg ping");
return ret;
}
printf("Mfg Ping: \tSUCCESS\n");
if (ping_info.ping_rev != 0)
{
version_to_string(ping_info.fw_version, version_str, sizeof(version_str));
printf("FW Version: \t%s\n", version_str);
printf("Device family: \t%s\n", (ping_info.dev_family == PCI100X_DEV_FAM) ? "PCI100x" : "Unknown");
if (true == spi_frequency_parser(ping_info.spi_freq, &clk_rate))
{
printf("SPI Frequency: \t%.2f MHz\n", clk_rate);
}
else
{
printf("SPI Frequency: \tUnknown [SPI_CLK_SEL: %x, SPI_CLK_RATE: %x]\n", \
(ping_info.spi_freq & PING_SPI_CLK_SEL_MASK) >> PING_SPI_CLK_SEL_OFFSET,
(ping_info.spi_freq & PING_SPI_BUS_RATE_MASK));
}
printf("Ping entry: \t%s\n\n", parse_recovery_reason(ping_info.rcvry_entry_reason));
printf("Strap status\n-------------\n");
printf("SPI QUAD MODE:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , SPI_QUAD_MODE));
printf("INLINE ECC MODE:\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , INLINE_ECC_MODE));
if (true == cfg.extended_cmd)
{
printf("SPI 3B/4B MODE:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , SPI_3B_4B_MODE));
printf("XIP MODE:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , XIP_MODE));
}
printf("WATCHDOG:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , WATCHDOG_EN));
printf("TWI RECOVERY ADDRESS BIT0:\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , TWI_RCVRY_ADDR0));
printf("TWI RECOVERY ADDRESS BIT1:\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , TWI_RCVRY_ADDR1));
printf("SECURE BOOT DRY RUN:\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , SEC_BOOT_DRY_RUN));
printf("PE MODE:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , PE_MODE_EN));
printf("MEMBIST:\t\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , MEMBIST_EN));
printf("MEMBIST CPU RESET:\t\t%s\n", PCI100X_STRAP_STAT(ping_info.strap_stat , MEMBIST_CPU_RESET));
}
return 0;
}
static const char* program_status_to_string(enum switchtec_otp_program_status s)
{
switch(s) {
case SWITCHTEC_OTP_PROGRAMMABLE:
return "R/W (Programmable)";
case SWITCHTEC_OTP_UNPROGRAMMABLE:
return "R/O (Unprogrammable)";
default:
return "Unknown";
}
}
static void print_security_config(struct switchtec_security_cfg_state *state,
struct switchtec_security_cfg_otp_region *otp)
{
int key_idx;
int i;
printf("\nBasic Secure Settings %s\n",
state->basic_setting_valid? "(Valid)":"(Invalid)");
printf("\tSecure State: \t\t\t");
switch(state->secure_state) {
case SWITCHTEC_UNINITIALIZED_UNSECURED:
printf("UNINITIALIZED_UNSECURED\n");
break;
case SWITCHTEC_INITIALIZED_UNSECURED:
printf("INITIALIZED_UNSECURED\n");
break;
case SWITCHTEC_INITIALIZED_SECURED:
printf("INITIALIZED_SECURED\n");
break;
default:
printf("Unsupported State\n");
break;
}
printf("\tSPI Clock Rate [In OTP]: \t%.2f MHz\n", state->spi_clk_rate);
printf("\tI2C Recovery TMO: \t\t%d Second(s)\n",
state->i2c_recovery_tmo);
printf("\tI2C Port: \t\t\t%d\n", state->i2c_port);
printf("\tI2C Address (7-bits): \t\t0x%02x\n", state->i2c_addr);
printf("\tI2C Command Map: \t\t0x%08x\n\n", state->i2c_cmd_map);
printf("Exponent Hex Data %s: \t\t0x%08x\n",
state->basic_setting_valid? "(Valid)":"(Invalid)",
state->public_key_exponent);
printf("KMSK Entry Number %s: \t\t%d\n",
state->public_key_num_valid? "(Valid)":"(Invalid)",
state->public_key_num);
if (state->public_key_ver)
printf("Current KMSK index %s: \t\t%d\n",
state->public_key_ver_valid? "(Valid)":"(Invalid)",
state->public_key_ver);
else
printf("Current KMSK index %s: \t\tNot Set\n",
state->public_key_ver_valid? "(Valid)":"(Invalid)");
for(key_idx = 0; key_idx < state->public_key_num; key_idx++) {
printf("KMSK Entry %d: ", key_idx + 1);
for(i = 0; i < SWITCHTEC_KMSK_LEN; i++)
printf("%02x", state->public_key[key_idx][i]);
printf("\n");
}
if (otp) {
printf("\nOTP Region Program Status\n");
printf("\tBasic Secure Settings %s%s\n",
otp->basic_valid? "(Valid): ": "(Invalid):",
program_status_to_string(otp->basic));
printf("\tMixed Version %s\t%s\n",
otp->mixed_ver_valid? "(Valid): ": "(Invalid):",
program_status_to_string(otp->mixed_ver));
printf("\tMain FW Version %s\t%s\n",
otp->main_fw_ver_valid? "(Valid): ": "(Invalid):",
program_status_to_string(otp->main_fw_ver));
printf("\tSecure Unlock Version %s%s\n",
otp->sec_unlock_ver_valid? "(Valid): ": "(Invalid):",
program_status_to_string(otp->sec_unlock_ver));
for (i = 0; i < 4; i++) {
printf("\tKMSK%d %s\t\t%s\n", i,
otp->kmsk_valid[i]? "(Valid): ": "(Invalid):",
program_status_to_string(otp->kmsk[i]));
}
}
}
static void print_security_cfg_set(struct switchtec_security_cfg_set *set)
{
printf("\nBasic Secure Settings\n");
printf("\tSPI Clock Rate: \t\t%.2f MHz\n", set->spi_clk_rate);
printf("\tI2C Recovery TMO: \t\t%d Second(s)\n",
set->i2c_recovery_tmo);
printf("\tI2C Port: \t\t\t%d\n", set->i2c_port);
printf("\tI2C Address (7-bits): \t\t0x%02x\n", set->i2c_addr);
printf("\tI2C Command Map: \t\t0x%08x\n", set->i2c_cmd_map);
printf("Exponent Hex Data: \t\t\t0x%08x\n", set->public_key_exponent);
}
#define CMD_DESC_VERSIONS_GET "display chip serial number, customer ID, secure versions of Key manifest, BL2, Main FW and SUV"
static int secure_version_get(int argc, char **argv)
{
int ret;
struct switchtec_sn_ver_info sn_info = {};
static struct {
struct switchtec_dev *dev;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_VERSIONS_GET, opts, &cfg, sizeof(cfg));
ret = switchtec_sn_ver_get(cfg.dev, &sn_info);
if (ret) {
switchtec_perror("mfg versions-get");
return ret;
}
printf("Chip Serial Number: \t\t\t0x%08x\n", sn_info.chip_serial);
printf("Key Manifest Secure Version: \t\t0x%08x\n", sn_info.ver_km);
printf("BL2 Secure Version: \t\t\t0x%08x\n", sn_info.ver_bl2);
printf("Main Secure Version: \t\t\t0x%08x\n", sn_info.ver_main);
printf("Secure Unlock Version: \t\t\t0x%08x\n", sn_info.ver_sec_unlock);
printf("Customer ID: \t\t\t\t0x%04x\n", sn_info.customer_id);
return 0;
}
#define CMD_DESC_INFO "display security settings"
static int info(int argc, char **argv)
{
int ret;
enum switchtec_boot_phase phase_id;
struct switchtec_sn_ver_info sn_info = {};
static struct {
struct switchtec_dev *dev;
int verbose;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"verbose", 'v', "", CFG_NONE, &cfg.verbose, no_argument,
"print additional chip information"},
{NULL}};
struct switchtec_security_cfg_state state;
argconfig_parse(argc, argv, CMD_DESC_INFO, opts, &cfg, sizeof(cfg));
ret = switchtec_security_config_get(cfg.dev, &state);
if (ret) {
switchtec_perror("mfg info");
return ret;
}
phase_id = switchtec_boot_phase(cfg.dev);
printf("Current Boot Phase: \t\t\t%s\n",
switchtec_phase_id_str(phase_id));
ret = switchtec_sn_ver_get(cfg.dev, &sn_info);
if (ret) {
switchtec_perror("mfg info");
return ret;
}
printf("Chip Serial: \t\t\t\t0x%08x\n", sn_info.chip_serial);
printf("Key Manifest Secure Version: \t\t0x%08x\n", sn_info.ver_km);
printf("BL2 Secure Version: \t\t\t0x%08x\n", sn_info.ver_bl2);
printf("Main Secure Version: \t\t\t0x%08x\n", sn_info.ver_main);
printf("Secure Unlock Version: \t\t\t0x%08x\n", sn_info.ver_sec_unlock);
printf("Customer ID: \t\t\t\t0x%04x\n", sn_info.customer_id);
if (phase_id == SWITCHTEC_BOOT_PHASE_BL2) {
printf("\nOther secure settings are only shown in the BL1 or Main Firmware phase.\n\n");
return 0;
}
if (cfg.verbose) {
if (!state.otp_valid) {
print_security_config(&state, NULL);
fprintf(stderr,
"\nAdditional (verbose) chip info is not available on this chip!\n\n");
} else if (switchtec_gen(cfg.dev) == SWITCHTEC_GEN4 &&
((phase_id != SWITCHTEC_BOOT_PHASE_FW) || (phase_id != SWITCHTEC_BOOT_PHASE_MM))){
print_security_config(&state, NULL);
fprintf(stderr,
"\nAdditional (verbose) chip info is only available in the Main Firmware phase!\n\n");
} else {
print_security_config(&state, &state.otp);
}
return 0;
}
print_security_config(&state, NULL);
return 0;
}
#define CMD_DESC_MAILBOX "retrieve mailbox logs"
static int mailbox(int argc, char **argv)
{
int ret;
static struct {
struct switchtec_dev *dev;
int out_fd;
const char *out_filename;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"filename", .cfg_type=CFG_FD_WR, .value_addr=&cfg.out_fd,
.argument_type=optional_positional,
.force_default="switchtec_mailbox.log",
.help="file to log mailbox data"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_MAILBOX, opts, &cfg, sizeof(cfg));
ret = switchtec_mailbox_to_file(cfg.dev, cfg.out_fd);
if (ret) {
switchtec_perror("mfg mailbox");
close(cfg.out_fd);
return ret;
}
close(cfg.out_fd);
fprintf(stderr, "\nLog saved to %s.\n", cfg.out_filename);
return 0;
}
static void print_image_list(struct switchtec_active_index *idx)
{
printf("IMAGE\t\tINDEX\n");
printf("Key Manifest\t%d\n", idx->keyman);
printf("BL2\t\t%d\n", idx->bl2);
printf("Config\t\t%d\n", idx->config);
printf("Firmware\t%d\n", idx->firmware);
}
#define CMD_DESC_IMAGE_LIST "display active image list (BL1 only)"
static int image_list(int argc, char **argv)
{
int ret;
struct switchtec_active_index index;
static struct {
struct switchtec_dev *dev;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_IMAGE_LIST, opts, &cfg, sizeof(cfg));
if (switchtec_boot_phase(cfg.dev) != SWITCHTEC_BOOT_PHASE_BL1) {
fprintf(stderr, "This command is only available in BL1!\n");
return -1;
}
ret = switchtec_active_image_index_get(cfg.dev, &index);
if (ret) {
switchtec_perror("image list");
return ret;
}
print_image_list(&index);
return 0;
}
#define CMD_DESC_IMAGE_SELECT "select active image index (BL1 only)"
static int image_select(int argc, char **argv)
{
int ret;
struct switchtec_active_index index;
static struct {
struct switchtec_dev *dev;
unsigned char bl2;
unsigned char firmware;
unsigned char config;
unsigned char keyman;
} cfg = {
.bl2 = SWITCHTEC_ACTIVE_INDEX_NOT_SET,
.firmware = SWITCHTEC_ACTIVE_INDEX_NOT_SET,
.config = SWITCHTEC_ACTIVE_INDEX_NOT_SET,
.keyman = SWITCHTEC_ACTIVE_INDEX_NOT_SET
};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"bl2", 'b', "", CFG_BYTE, &cfg.bl2,
required_argument, "active image index for BL2"},
{"firmware", 'm', "", CFG_BYTE, &cfg.firmware,
required_argument, "active image index for FIRMWARE"},
{"config", 'c', "", CFG_BYTE, &cfg.config,
required_argument, "active image index for CONFIG"},
{"keyman", 'k', "", CFG_BYTE, &cfg.keyman, required_argument,
"active image index for KEY MANIFEST"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_IMAGE_SELECT, opts, &cfg, sizeof(cfg));
if (cfg.bl2 == SWITCHTEC_ACTIVE_INDEX_NOT_SET &&
cfg.firmware == SWITCHTEC_ACTIVE_INDEX_NOT_SET &&
cfg.config == SWITCHTEC_ACTIVE_INDEX_NOT_SET &&
cfg.keyman == SWITCHTEC_ACTIVE_INDEX_NOT_SET) {
fprintf(stderr,
"One of BL2, Config, Key Manifest or Firmware indices must be set in this command!\n");
return -1;
}
if (switchtec_boot_phase(cfg.dev) != SWITCHTEC_BOOT_PHASE_BL1) {
fprintf(stderr,
"This command is only available in BL1!\n");
return -2;
}
if (cfg.bl2 > 1 && cfg.bl2 != SWITCHTEC_ACTIVE_INDEX_NOT_SET) {
fprintf(stderr, "Active index of BL2 must be within 0-1!\n");
return -3;
}
index.bl2 = cfg.bl2;
if (cfg.firmware > 1 &&
cfg.firmware != SWITCHTEC_ACTIVE_INDEX_NOT_SET) {
fprintf(stderr,
"Active index of FIRMWARE must be within 0-1!\n");
return -4;
}
index.firmware = cfg.firmware;
if (cfg.config > 1 && cfg.config != SWITCHTEC_ACTIVE_INDEX_NOT_SET) {
fprintf(stderr,
"Active index of CONFIG must be within 0-1!\n");
return -5;
}
index.config = cfg.config;
if (cfg.keyman > 1 && cfg.keyman != SWITCHTEC_ACTIVE_INDEX_NOT_SET) {
fprintf(stderr,
"Active index of KEY MANIFEST must be within 0-1!\n");
return -6;
}
index.keyman = cfg.keyman;
ret = switchtec_active_image_index_set(cfg.dev, &index);
if (ret) {
switchtec_perror("image select");
return ret;
}
return ret;
}
#define CMD_DESC_BOOT_RESUME "resume device boot process (BL1 and BL2 only)"
static int boot_resume(int argc, char **argv)
{
const char *desc = CMD_DESC_BOOT_RESUME "\n\n"
"A normal device boot process includes the BL1, "
"BL2 and Main Firmware boot phases. In the case "
"when the boot process is paused at the BL1 or BL2 phase "
"(due to boot failure or BOOT_RECOVERY PIN[0:1] "
"being set to LOW), sending this command requests "
"the device to try resuming a normal boot process.\n\n"
"NOTE: if your system does not support hotplug, "
"your device might not be immediately accessible "
"after a normal boot process. In this case, be sure "
"to reboot your system after sending this command.";
int ret;
static struct {
struct switchtec_dev *dev;
int assume_yes;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
"assume yes when prompted"},
{NULL}
};
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (switchtec_boot_phase(cfg.dev) == SWITCHTEC_BOOT_PHASE_FW) {
fprintf(stderr,
"This command is only available in BL1 or BL2!\n");
return -1;
}
if (!cfg.assume_yes)
fprintf(stderr,
"WARNING: if your system does not support hotplug,\n"
"your device might not be immediately accessible\n"
"after a normal boot process. In this case, be sure\n"
"to reboot your system after sending this command.\n\n");
ret = ask_if_sure(cfg.assume_yes);
if (ret)
return ret;
ret = switchtec_boot_resume(cfg.dev);
if (ret) {
switchtec_perror("mfg boot-resume");
return ret;
}
return 0;
}
#define CMD_DESC_FW_TRANSFER "transfer a firmware image to device (BL1 only)"
static int fw_transfer(int argc, char **argv)
{
const char *desc = CMD_DESC_FW_TRANSFER "\n\n"
"This command only supports transferring a firmware "
"image when the device is in the BL1 boot phase. Use "
"'fw-execute' after this command to excute the "
"transferred image. Note that the image is stored "
"in device RAM and is lost after device reboot.\n\n"
"To update an image in the BL2 or MAIN boot phase, use "
"the 'fw-update' command instead.\n\n"
BOOT_PHASE_HELP_TEXT;
int ret;
enum switchtec_fw_type type;
static struct {
struct switchtec_dev *dev;
FILE *fimg;
const char *img_filename;
int assume_yes;
int force;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"img_file", .cfg_type=CFG_FILE_R, .value_addr=&cfg.fimg,
.argument_type=required_positional,
.help="firmware image file to transfer"},
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
"assume yes when prompted"},
{"force", 'f', "", CFG_NONE, &cfg.force, no_argument,
"force interrupting an existing fw-update command "
"in case firmware is stuck in a busy state"},
{NULL}
};
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (switchtec_boot_phase(cfg.dev) != SWITCHTEC_BOOT_PHASE_BL1) {
fprintf(stderr,
"This command is only available in the BL1 boot phase!\n");
fprintf(stderr,
"Use 'fw-update' instead to update an image in other boot phases.\n");
return -1;
}
printf("Writing the following firmware image to %s:\n",
switchtec_name(cfg.dev));
type = check_and_print_fw_image(fileno(cfg.fimg), cfg.img_filename);
if (type != SWITCHTEC_FW_TYPE_BL2) {
fprintf(stderr,
"This command only supports transferring a BL2 image.\n");
return -2;
}
ret = ask_if_sure(cfg.assume_yes);
if (ret) {
fclose(cfg.fimg);
return ret;
}
progress_start();
ret = switchtec_fw_write_file(cfg.dev, cfg.fimg, 1, cfg.force,
progress_update);
fclose(cfg.fimg);
if (ret) {
printf("\n");
switchtec_fw_perror("mfg fw-transfer", ret);
return -3;
}
progress_finish(0);
printf("\n");
return 0;
}
#define CMD_DESC_FW_EXECUTE "execute previously transferred firmware image (BL1 only)"
static int fw_execute(int argc, char **argv)
{
const char *desc = CMD_DESC_FW_EXECUTE "\n\n"
"This command is only supported when the device is "
"in the BL1 boot phase. The firmware image must have "
"been transferred using the 'fw-transfer' command. "
"After firmware initializes, it listens for activity from "
"I2C, UART (XModem), or both interfaces for input. "
"Once activity is detected from an interface, "
"firmware falls into recovery mode on that interface. "
"The interface to listen on can be specified using "
"the 'bl2_recovery_mode' option. \n\n"
"To activate an image in the BL2 or MAIN boot "
"phase, use the 'fw-toggle' command instead.\n\n"
BOOT_PHASE_HELP_TEXT;
int ret;
static struct {
struct switchtec_dev *dev;
int assume_yes;
enum switchtec_bl2_recovery_mode bl2_rec_mode;
} cfg = {
.bl2_rec_mode = SWITCHTEC_BL2_RECOVERY_I2C_AND_XMODEM
};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
"assume yes when prompted"},
{"bl2_recovery_mode", 'm', "MODE",
CFG_CHOICES, &cfg.bl2_rec_mode,
required_argument, "BL2 recovery mode",
.choices = recovery_mode_choices},
{NULL}
};
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (switchtec_boot_phase(cfg.dev) != SWITCHTEC_BOOT_PHASE_BL1) {
fprintf(stderr,
"This command is only available in the BL1 phase!\n");
return -2;
}
if (!cfg.assume_yes)
printf("This command will execute the previously transferred image.\n");
ret = ask_if_sure(cfg.assume_yes);
if (ret) {
return ret;
}
ret = switchtec_fw_exec(cfg.dev, cfg.bl2_rec_mode);
if (ret) {
switchtec_fw_perror("mfg fw-execute", ret);
return ret;
}
return 0;
}
#define CMD_DESC_STATE_SET "set device secure state (BL1 and Main Firmware only)"
static int state_set(int argc, char **argv)
{
int ret;
struct switchtec_security_cfg_state state = {};
const char *desc = CMD_DESC_STATE_SET "\n\n"
"This command can only be used when the device "
"secure state is UNINITIALIZED_UNSECURED.\n\n"
"NOTE - A device can be in one of these "
"three secure states: \n"
"UNINITIALIZED_UNSECURED: this is the default state "
"when the chip is shipped. All security-related settings "
"are 'uninitialized', and the chip is in the 'unsecured' "
"state. \n"
"INITIALIZED_UNSECURED: this is the state when "
"security-related settings are 'initialized', and "
"the chip is set to the 'unsecured' state. \n"
"INITIALIZED_SECURED: this is the state when "
"security-related settings are 'initialized', and "
"the chip is set to the 'secured' state. \n\n"
"Use 'config-set' or other mfg commands to "
"initialize security settings when the chip is in "
"the UNINITIALIZED_UNSECURED state, then use this "
"command to switch the chip to the INITIALIZED_SECURED "
"or INITIALIZED_UNSECURED state. \n\n"
"WARNING: ONCE THE CHIP STATE IS SUCCESSFULLY SET, "
"IT CAN NO LONGER BE CHANGED. USE CAUTION WHEN ISSUING "
"THIS COMMAND.";
static struct {
struct switchtec_dev *dev;
enum switchtec_secure_state state;
int assume_yes;
} cfg = {
.state = SWITCHTEC_SECURE_STATE_UNKNOWN,
};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"state", 't', "state",
CFG_CHOICES, &cfg.state,
required_argument, "secure state",
.choices=secure_state_choices},
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
"assume yes when prompted"},
{NULL}
};
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (cfg.state == SWITCHTEC_SECURE_STATE_UNKNOWN) {
fprintf(stderr,
"Secure state must be set in this command!\n");
return -1;
}
if (switchtec_boot_phase(cfg.dev) == SWITCHTEC_BOOT_PHASE_BL2) {
fprintf(stderr,
"This command is only available in BL1 or Main Firmware!\n");
return -2;
}
ret = switchtec_security_config_get(cfg.dev, &state);
if (ret) {
switchtec_perror("mfg state-set");
return ret;
}
if (state.secure_state != SWITCHTEC_UNINITIALIZED_UNSECURED) {
fprintf(stderr,
"This command is only valid when secure state is UNINITIALIZED_UNSECURED!\n");
return -3;
}
print_security_config(&state, NULL);
if (!cfg.assume_yes) {
fprintf(stderr,
"\nWARNING: This operation makes changes to the device OTP memory and is IRREVERSIBLE!\n");
ret = ask_if_sure(cfg.assume_yes);
if (ret)
return -4;
}
ret = switchtec_secure_state_set(cfg.dev, cfg.state);
if (ret) {
switchtec_perror("mfg state-set");
return ret;
}
return 0;
}
#define CMD_DESC_DIE_TRACE_PROG "program die trace into OTP"
static int die_trace_prog(int argc, char **argv)
{
int ret = 0;
struct otp_die_trace otp_die_trace_str;
static struct {
struct switchtec_dev *dev;
FILE *die_trace_fimg;
char *die_trace_file;
int assume_yes;
} cfg = {};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"die_trace", 'd', "die_trace",
.cfg_type=CFG_FILE_R,
.value_addr=&cfg.die_trace_fimg,
.argument_type=required_argument,
.help="Die Trace file.bin\n" \
"Should contain Die Trace settings\n"},
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
"assume yes when prompted"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_DIE_TRACE_PROG, opts, &cfg, sizeof(cfg));
if (cfg.die_trace_file == NULL)
{
fprintf(stderr,
"Die Trace bin file must be supplied in this command!\n");
return -1;
}
fseek(cfg.die_trace_fimg, 0, SEEK_END);
if (ftell(cfg.die_trace_fimg) < OTP_DIE_TRACE_LENGTH) {
fprintf(stderr, "\nSize of Die Trace file %s is invalid!\n",
cfg.die_trace_file);
ret = -1;
}
fseek(cfg.die_trace_fimg, 0, SEEK_SET);
if (cfg.die_trace_file) {
ret = switchtec_read_die_trace_file(cfg.die_trace_fimg, &otp_die_trace_str);
fclose(cfg.die_trace_fimg);
if (ret) {
fprintf(stderr, "Invalid Die Trace Bin file %s!\n",
cfg.die_trace_file);
return -6;
}
}
if (!cfg.assume_yes)
fprintf(stderr,
"\nWARNING: This operation makes changes to the device OTP memory and is IRREVERSIBLE!\n");
ret = ask_if_sure(cfg.assume_yes);
if (ret)
return ret;
ret = switchtec_otp_die_trace_prog(cfg.dev, otp_die_trace_str.die_trace);
if(ret)
switchtec_perror("mfg otp-die-trace-prog");
else
printf("Die Trace programmed successfully into OTP\n");
return ret;
}
#define CMD_DESC_CHIP_SERIAL_PROG "program chip serial number into OTP"
static int chip_serial_prog(int argc, char **argv)
{
int ret;
static struct {
struct switchtec_dev *dev;
uint32_t chip_serial_num;
} cfg = {
.chip_serial_num = 0
};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"chip_serial_num", 'c', "chip_serial_num",
.cfg_type=CFG_LONG,
.value_addr=&cfg.chip_serial_num,
.argument_type=required_argument,
.help="Enter 4-byte Chip Serial Number\n"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_CHIP_SERIAL_PROG, opts, &cfg, sizeof(cfg));
if (cfg.chip_serial_num == 0)
{
fprintf(stderr,
"Chip Serial Number must be supplied in this command!\n");
return -1;
}
ret = switchtec_chip_serial_num_prog(cfg.dev, cfg.chip_serial_num);
if(ret)
switchtec_perror("mfg otp-chip-serial-prog");
else
printf("Chip Serial Number programmed successfully into OTP\n");
return ret;
}
#define CMD_DESC_SKU_INFO_SET "write SKU info settings into OTP"
static int sku_set(int argc, char **argv)
{
struct otp_sku_info_set sku_info_set_str = {};
int ret;
static struct {
struct switchtec_dev *dev;
uint8_t block_num;
uint8_t sku_info;
} cfg = {
.block_num = 0,
.sku_info = 0