1 // SPDX-License-Identifier: GPL-2.0
3 * sdsi: Intel Software Defined Silicon tool for provisioning certificates
4 * and activation payloads on supported cpus.
6 * See https://github.com/intel/intel-sdsi/blob/master/os-interface.rst
7 * for register descriptions.
9 * Copyright (C) 2022 Intel Corporation. All rights reserved.
23 #include <sys/types.h>
25 #define SDSI_DEV "intel_vsec.sdsi"
26 #define AUX_DEV_PATH "/sys/bus/auxiliary/devices/"
27 #define SDSI_PATH (AUX_DEV_DIR SDSI_DEV)
29 #define REGISTERS_MIN_SIZE 72
31 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
32 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
34 struct enabled_features {
37 uint64_t reserved1:60;
40 struct auth_fail_count {
41 uint64_t key_failure_count:3;
42 uint64_t key_failure_threshold:3;
43 uint64_t auth_failure_count:3;
44 uint64_t auth_failure_threshold:3;
57 struct enabled_features en_features;
59 struct auth_fail_count auth_fail_count;
60 struct availability prov_avail;
67 struct sdsi_regs regs;
81 static void sdsi_list_devices(void)
87 aux_dir = opendir(AUX_DEV_PATH);
89 fprintf(stderr, "Cannot open directory %s\n", AUX_DEV_PATH);
93 while ((entry = readdir(aux_dir))) {
94 if (!strncmp(SDSI_DEV, entry->d_name, strlen(SDSI_DEV))) {
96 printf("%s\n", entry->d_name);
101 fprintf(stderr, "No sdsi devices found.\n");
104 static int sdsi_update_registers(struct sdsi_dev *s)
109 memset(&s->regs, 0, sizeof(s->regs));
111 /* Open the registers file */
112 ret = chdir(s->dev_path);
118 regs_ptr = fopen("registers", "r");
120 perror("Could not open 'registers' file");
124 if (s->guid != GUID) {
125 fprintf(stderr, "Unrecognized guid, 0x%x\n", s->guid);
130 /* Update register info for this guid */
131 ret = fread(&s->regs, sizeof(uint8_t), sizeof(s->regs), regs_ptr);
132 if (ret != sizeof(s->regs)) {
133 fprintf(stderr, "Could not read 'registers' file\n");
143 static int sdsi_read_reg(struct sdsi_dev *s)
147 ret = sdsi_update_registers(s);
151 /* Print register info for this guid */
153 printf("Socket information for device %s\n", s->dev_name);
155 printf("PPIN: 0x%lx\n", s->regs.ppin);
156 printf("Enabled Features\n");
157 printf(" SDSi: %s\n", !!s->regs.en_features.sdsi ? "Enabled" : "Disabled");
158 printf("Authorization Failure Count\n");
159 printf(" AKC Failure Count: %d\n", s->regs.auth_fail_count.key_failure_count);
160 printf(" AKC Failure Threshold: %d\n", s->regs.auth_fail_count.key_failure_threshold);
161 printf(" CAP Failure Count: %d\n", s->regs.auth_fail_count.auth_failure_count);
162 printf(" CAP Failure Threshold: %d\n", s->regs.auth_fail_count.auth_failure_threshold);
163 printf("Provisioning Availability\n");
164 printf(" Updates Available: %d\n", s->regs.prov_avail.available);
165 printf(" Updates Threshold: %d\n", s->regs.prov_avail.threshold);
166 printf("Socket ID: %ld\n", s->regs.socket_id & 0xF);
171 static int sdsi_certificate_dump(struct sdsi_dev *s)
173 uint64_t state_certificate[512] = {0};
179 ret = sdsi_update_registers(s);
183 if (!s->regs.en_features.sdsi) {
184 fprintf(stderr, "SDSi feature is present but not enabled.");
185 fprintf(stderr, " Unable to read state certificate");
189 ret = chdir(s->dev_path);
195 cert_ptr = fopen("state_certificate", "r");
197 perror("Could not open 'state_certificate' file");
201 size = fread(state_certificate, 1, sizeof(state_certificate), cert_ptr);
203 fprintf(stderr, "Could not read 'state_certificate' file\n");
208 printf("%3d: 0x%lx\n", 0, state_certificate[0]);
209 previous = state_certificate[0];
210 first_instance = true;
212 for (i = 1; i < (int)(round_up(size, sizeof(uint64_t))/sizeof(uint64_t)); i++) {
213 if (state_certificate[i] == previous) {
214 if (first_instance) {
216 first_instance = false;
220 printf("%3d: 0x%lx\n", i, state_certificate[i]);
221 previous = state_certificate[i];
222 first_instance = true;
231 static int sdsi_provision(struct sdsi_dev *s, char *bin_file, enum command command)
233 int bin_fd, prov_fd, size, ret;
234 char buf[4096] = { 0 };
235 char cap[] = "provision_cap";
236 char akc[] = "provision_akc";
240 fprintf(stderr, "No binary file provided\n");
244 /* Open the binary */
245 bin_fd = open(bin_file, O_RDONLY);
247 fprintf(stderr, "Could not open file %s: %s\n", bin_file, strerror(errno));
251 prov_file = (command == CMD_PROV_AKC) ? akc : cap;
253 ret = chdir(s->dev_path);
260 /* Open the provision file */
261 prov_fd = open(prov_file, O_WRONLY);
263 fprintf(stderr, "Could not open file %s: %s\n", prov_file, strerror(errno));
268 /* Read the binary file into the buffer */
269 size = read(bin_fd, buf, 4096);
276 ret = write(prov_fd, buf, size);
280 perror("Provisioning failed");
284 printf("Provisioned %s file %s successfully\n", prov_file, bin_file);
292 static int sdsi_provision_akc(struct sdsi_dev *s, char *bin_file)
296 ret = sdsi_update_registers(s);
300 if (!s->regs.en_features.sdsi) {
301 fprintf(stderr, "SDSi feature is present but not enabled. Unable to provision");
305 if (!s->regs.prov_avail.available) {
306 fprintf(stderr, "Maximum number of updates (%d) has been reached.\n",
307 s->regs.prov_avail.threshold);
311 if (s->regs.auth_fail_count.key_failure_count ==
312 s->regs.auth_fail_count.key_failure_threshold) {
313 fprintf(stderr, "Maximum number of AKC provision failures (%d) has been reached.\n",
314 s->regs.auth_fail_count.key_failure_threshold);
315 fprintf(stderr, "Power cycle the system to reset the counter\n");
319 return sdsi_provision(s, bin_file, CMD_PROV_AKC);
322 static int sdsi_provision_cap(struct sdsi_dev *s, char *bin_file)
326 ret = sdsi_update_registers(s);
330 if (!s->regs.en_features.sdsi) {
331 fprintf(stderr, "SDSi feature is present but not enabled. Unable to provision");
335 if (!s->regs.prov_avail.available) {
336 fprintf(stderr, "Maximum number of updates (%d) has been reached.\n",
337 s->regs.prov_avail.threshold);
341 if (s->regs.auth_fail_count.auth_failure_count ==
342 s->regs.auth_fail_count.auth_failure_threshold) {
343 fprintf(stderr, "Maximum number of CAP provision failures (%d) has been reached.\n",
344 s->regs.auth_fail_count.auth_failure_threshold);
345 fprintf(stderr, "Power cycle the system to reset the counter\n");
349 return sdsi_provision(s, bin_file, CMD_PROV_CAP);
352 static int read_sysfs_data(const char *file, int *value)
357 fp = fopen(file, "r");
363 if (!fgets(buff, 16, fp)) {
364 fprintf(stderr, "Failed to read file '%s'", file);
370 *value = strtol(buff, NULL, 0);
375 static struct sdsi_dev *sdsi_create_dev(char *dev_no)
377 int dev_name_len = sizeof(SDSI_DEV) + strlen(dev_no) + 1;
382 s = (struct sdsi_dev *)malloc(sizeof(*s));
388 s->dev_name = (char *)malloc(sizeof(SDSI_DEV) + strlen(dev_no) + 1);
395 snprintf(s->dev_name, dev_name_len, "%s.%s", SDSI_DEV, dev_no);
397 s->dev_path = (char *)malloc(sizeof(AUX_DEV_PATH) + dev_name_len);
405 snprintf(s->dev_path, sizeof(AUX_DEV_PATH) + dev_name_len, "%s%s", AUX_DEV_PATH,
407 dir = opendir(s->dev_path);
409 fprintf(stderr, "Could not open directory '%s': %s\n", s->dev_path,
417 if (chdir(s->dev_path) == -1) {
425 if (read_sysfs_data("guid", &guid)) {
437 static void sdsi_free_dev(struct sdsi_dev *s)
444 static void usage(char *prog)
446 printf("Usage: %s [-l] [-d DEVNO [-iD] [-a FILE] [-c FILE]]\n", prog);
449 static void show_help(void)
451 printf("Commands:\n");
452 printf(" %-18s\t%s\n", "-l, --list", "list available sdsi devices");
453 printf(" %-18s\t%s\n", "-d, --devno DEVNO", "sdsi device number");
454 printf(" %-18s\t%s\n", "-i --info", "show socket information");
455 printf(" %-18s\t%s\n", "-D --dump", "dump state certificate data");
456 printf(" %-18s\t%s\n", "-a --akc FILE", "provision socket with AKC FILE");
457 printf(" %-18s\t%s\n", "-c --cap FILE>", "provision socket with CAP FILE");
460 int main(int argc, char *argv[])
462 char bin_file[PATH_MAX], *dev_no = NULL;
464 enum command command = CMD_NONE;
467 int option_index = 0;
469 static struct option long_options[] = {
470 {"akc", required_argument, 0, 'a'},
471 {"cap", required_argument, 0, 'c'},
472 {"devno", required_argument, 0, 'd'},
473 {"dump", no_argument, 0, 'D'},
474 {"help", no_argument, 0, 'h'},
475 {"info", no_argument, 0, 'i'},
476 {"list", no_argument, 0, 'l'},
483 while ((opt = getopt_long_only(argc, argv, "+a:c:d:Da:c:h", long_options,
484 &option_index)) != -1) {
493 command = CMD_SOCKET_INFO;
496 command = CMD_DUMP_CERT;
500 if (!access(optarg, F_OK) == 0) {
501 fprintf(stderr, "Could not open file '%s': %s\n", optarg,
506 if (!realpath(optarg, bin_file)) {
511 command = (opt == 'a') ? CMD_PROV_AKC : CMD_PROV_CAP;
524 if (command != CMD_NONE)
525 fprintf(stderr, "Missing device number, DEVNO, for this command\n");
530 s = sdsi_create_dev(dev_no);
534 /* Run the command */
537 fprintf(stderr, "Missing command for device %s\n", dev_no);
540 case CMD_SOCKET_INFO:
541 ret = sdsi_read_reg(s);
544 ret = sdsi_certificate_dump(s);
547 ret = sdsi_provision_akc(s, bin_file);
550 ret = sdsi_provision_cap(s, bin_file);