Skip to content

Commit a988af2

Browse files
Jie Yankelvin-cao
authored andcommitted
cli: Add 'mfg csr-get' command for Gen5
This command retrieves DevID CSR from MainFW and save it to a .der file.
1 parent 1cf1ddc commit a988af2

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

cli/mfg.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,58 @@ static int debug_unlock_token(int argc, char **argv)
15261526
return 0;
15271527
}
15281528

1529+
#define CMD_DESC_CSR_GET "get Device ID Certificate Signing Request (CSR)"
1530+
1531+
static int csr_get(int argc, char **argv)
1532+
{
1533+
int ret;
1534+
1535+
const char *desc = CMD_DESC_CSR_GET "\n\n"
1536+
"This command can only be used in Main Firmware.\n\n"
1537+
"It gets the DevID Certificate Signing Request (CSR)"
1538+
"in DER format and save it to a specified file\n";
1539+
1540+
static struct {
1541+
struct switchtec_dev *dev;
1542+
int csr_fd;
1543+
const char *csr_file_name;
1544+
int assume_yes;
1545+
} cfg = {
1546+
.csr_fd = 0,
1547+
};
1548+
const struct argconfig_options opts[] = {
1549+
DEVICE_OPTION_MFG,
1550+
{"csr_file", .cfg_type=CFG_FD_WR, .value_addr=&cfg.csr_fd,
1551+
.argument_type=optional_positional,
1552+
.force_default="devid_csr.der",
1553+
.help="The CSR file to save to disk"},
1554+
{"yes", 'y', "", CFG_NONE, &cfg.assume_yes, no_argument,
1555+
"assume yes when prompted"},
1556+
{NULL}
1557+
};
1558+
1559+
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
1560+
1561+
if (switchtec_boot_phase(cfg.dev) != SWITCHTEC_BOOT_PHASE_FW) {
1562+
fprintf(stderr,
1563+
"This command is only available in Main Firmware!\n");
1564+
return -1;
1565+
}
1566+
1567+
ret = switchtec_csr_to_file(cfg.dev, cfg.csr_fd);
1568+
if (ret) {
1569+
switchtec_perror("mfg csr_get");
1570+
close(cfg.csr_fd);
1571+
return ret;
1572+
}
1573+
1574+
close(cfg.csr_fd);
1575+
1576+
fprintf(stderr, "\nCertificate Signing Request (CSR) saved to %s.\n", cfg.csr_file_name);
1577+
1578+
return 0;
1579+
}
1580+
15291581
static const struct cmd commands[] = {
15301582
CMD(ping, CMD_DESC_PING),
15311583
CMD(info, CMD_DESC_INFO),
@@ -1541,6 +1593,7 @@ static const struct cmd commands[] = {
15411593
CMD(debug_unlock_token, CMD_DESC_DEBUG_TOKEN),
15421594
CMD(debug_unlock, CMD_DESC_DEBUG_UNLOCK),
15431595
CMD(debug_lock_update, CMD_DESC_DEBUG_LOCK_UPDATE),
1596+
CMD(csr_get, CMD_DESC_CSR_GET),
15441597
{}
15451598
};
15461599

inc/switchtec/mfg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,5 @@ int switchtec_read_uds_file(FILE *uds_file, struct switchtec_uds *uds);
275275
int
276276
switchtec_security_state_has_kmsk(struct switchtec_security_cfg_state *state,
277277
struct switchtec_kmsk *kmsk);
278-
278+
int switchtec_csr_to_file(struct switchtec_dev *dev, int fd);
279279
#endif // LIBSWITCHTEC_MFG_H

inc/switchtec/mrpc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ enum mrpc_cmd {
125125
MRPC_DBG_UNLOCK_GEN5 = 0x11A,
126126
MRPC_BOOTUP_RESUME_GEN5 = 0x11B,
127127
MRPC_FTDC_LOG_DUMP = 0x147,
128+
MRPC_CSR_GET = 0x14A,
128129

129-
MRPC_MAX_ID = 0x148,
130+
MRPC_MAX_ID = 0x14B,
130131
};
131132

132133
enum mrpc_bg_status {

lib/mfg.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,4 +1703,51 @@ int switchtec_sn_ver_get(struct switchtec_dev *dev,
17031703
return sn_ver_get_gen4(dev, info);
17041704
}
17051705

1706+
/**
1707+
* @brief Retrieve Certificate Signing Request (CSR)
1708+
* @param[in] dev Switchtec device handle
1709+
* @param[in] fd File handle to write the CSR data
1710+
* @return 0 on success, error code on failure
1711+
*/
1712+
int switchtec_csr_to_file(struct switchtec_dev *dev, int fd)
1713+
{
1714+
int ret;
1715+
uint16_t remaining_len;
1716+
struct csr_read {
1717+
uint8_t subcmd;
1718+
uint8_t reserved0;
1719+
uint16_t read_offset;
1720+
uint16_t read_len;
1721+
uint16_t reserved1;
1722+
} read = {};
1723+
struct csr_reply {
1724+
uint16_t read_offset;
1725+
uint16_t returned_data_len;
1726+
uint16_t total_data_len;
1727+
uint16_t reserved;
1728+
uint8_t data[800];
1729+
} reply = {};
1730+
1731+
read.subcmd = 0;
1732+
read.read_offset = 0;
1733+
read.read_len= 800;
1734+
1735+
do {
1736+
ret = switchtec_mfg_cmd(dev, MRPC_CSR_GET,
1737+
&read, sizeof(read),
1738+
&reply, sizeof(reply));
1739+
if (ret)
1740+
return ret;
1741+
1742+
read.read_offset = reply.read_offset + reply.returned_data_len;
1743+
remaining_len = reply.total_data_len - read.read_offset;
1744+
read.read_len = (remaining_len > 800) ? 800 : remaining_len;
1745+
ret = write(fd, reply.data, reply.returned_data_len);
1746+
if (ret < 0)
1747+
return ret;
1748+
} while (remaining_len);
1749+
1750+
return 0;
1751+
}
1752+
17061753
/**@}*/

0 commit comments

Comments
 (0)