1 // SPDX-License-Identifier: GPL-2.0
3 * sdsi: Intel On Demand (formerly Software Defined Silicon) tool for
4 * provisioning certificates 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>
26 #define __packed __attribute__((packed))
29 #define min(x, y) ({ \
30 typeof(x) _min1 = (x); \
31 typeof(y) _min2 = (y); \
32 (void) (&_min1 == &_min2); \
33 _min1 < _min2 ? _min1 : _min2; })
35 #define SDSI_DEV "intel_vsec.sdsi"
36 #define AUX_DEV_PATH "/sys/bus/auxiliary/devices/"
37 #define SDSI_PATH (AUX_DEV_DIR SDSI_DEV)
38 #define GUID_V1 0x6dd191
39 #define REGS_SIZE_GUID_V1 72
40 #define GUID_V2 0xF210D9EF
41 #define REGS_SIZE_GUID_V2 80
42 #define STATE_CERT_MAX_SIZE 4096
43 #define METER_CERT_MAX_SIZE 4096
44 #define STATE_MAX_NUM_LICENSES 16
45 #define STATE_MAX_NUM_IN_BUNDLE (uint32_t)8
46 #define METER_MAX_NUM_BUNDLES 8
48 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
49 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
51 struct nvram_content_auth_err_sts {
53 uint64_t sdsi_content_auth_err:1;
55 uint64_t sdsi_metering_auth_err:1;
56 uint64_t reserved2:58;
59 struct enabled_features {
63 uint64_t attestation:1;
64 uint64_t reserved2:13;
66 uint64_t reserved3:37;
69 struct key_provision_status {
71 uint64_t license_key_provisioned:1;
72 uint64_t reserved2:62;
75 struct auth_fail_count {
76 uint64_t key_failure_count:3;
77 uint64_t key_failure_threshold:3;
78 uint64_t auth_failure_count:3;
79 uint64_t auth_failure_threshold:3;
87 uint64_t reserved2:10;
90 struct nvram_update_limit {
92 uint64_t sdsi_50_pct:1;
93 uint64_t sdsi_75_pct:1;
94 uint64_t sdsi_90_pct:1;
95 uint64_t reserved2:49;
100 struct nvram_content_auth_err_sts auth_err_sts;
101 struct enabled_features en_features;
102 struct key_provision_status key_prov_sts;
103 struct auth_fail_count auth_fail_count;
104 struct availability prov_avail;
105 struct nvram_update_limit limits;
106 uint64_t pcu_cr3_capid_cfg;
118 #define CONTENT_TYPE_LK_ENC 0xD
119 #define CONTENT_TYPE_LK_BLOB_ENC 0xE
121 struct state_certificate {
122 uint32_t content_type;
123 uint32_t region_rev_id;
124 uint32_t header_size;
127 uint32_t num_licenses;
130 struct license_key_info {
132 uint64_t key_image_content[6];
135 #define LICENSE_BLOB_SIZE(l) (((l) & 0x7fffffff) * 4)
136 #define LICENSE_VALID(l) (!!((l) & 0x80000000))
138 // License Group Types
139 #define LBT_ONE_TIME_UPGRADE 1
140 #define LBT_METERED_UPGRADE 2
142 struct license_blob_content {
146 uint64_t previous_ppin;
148 uint32_t num_bundles;
151 struct bundle_encoding {
153 uint32_t encoding_rsvd[7];
156 struct meter_certificate {
157 uint32_t block_signature;
158 uint32_t counter_unit;
160 uint32_t bundle_length;
162 uint32_t mmrc_encoding;
163 uint32_t mmrc_counter;
166 struct bundle_encoding_counter {
172 struct sdsi_regs regs;
173 struct state_certificate sc;
187 static void sdsi_list_devices(void)
189 struct dirent *entry;
193 aux_dir = opendir(AUX_DEV_PATH);
195 fprintf(stderr, "Cannot open directory %s\n", AUX_DEV_PATH);
199 while ((entry = readdir(aux_dir))) {
200 if (!strncmp(SDSI_DEV, entry->d_name, strlen(SDSI_DEV))) {
202 printf("%s\n", entry->d_name);
207 fprintf(stderr, "No On Demand devices found.\n");
210 static int sdsi_update_registers(struct sdsi_dev *s)
215 memset(&s->regs, 0, sizeof(s->regs));
217 /* Open the registers file */
218 ret = chdir(s->dev_path);
224 regs_ptr = fopen("registers", "r");
226 perror("Could not open 'registers' file");
230 if (s->guid != GUID_V1 && s->guid != GUID_V2) {
231 fprintf(stderr, "Unrecognized guid, 0x%x\n", s->guid);
236 /* Update register info for this guid */
237 ret = fread(&s->regs, sizeof(uint8_t), sizeof(s->regs), regs_ptr);
238 if ((s->guid == GUID_V1 && ret != REGS_SIZE_GUID_V1) ||
239 (s->guid == GUID_V2 && ret != REGS_SIZE_GUID_V2)) {
240 fprintf(stderr, "Could not read 'registers' file\n");
250 static int sdsi_read_reg(struct sdsi_dev *s)
254 ret = sdsi_update_registers(s);
258 /* Print register info for this guid */
260 printf("Socket information for device %s\n", s->dev_name);
262 printf("PPIN: 0x%lx\n", s->regs.ppin);
263 printf("NVRAM Content Authorization Error Status\n");
264 printf(" SDSi Auth Err Sts: %s\n", !!s->regs.auth_err_sts.sdsi_content_auth_err ? "Error" : "Okay");
266 if (!!s->regs.en_features.metering)
267 printf(" Metering Auth Err Sts: %s\n", !!s->regs.auth_err_sts.sdsi_metering_auth_err ? "Error" : "Okay");
269 printf("Enabled Features\n");
270 printf(" On Demand: %s\n", !!s->regs.en_features.sdsi ? "Enabled" : "Disabled");
271 printf(" Attestation: %s\n", !!s->regs.en_features.attestation ? "Enabled" : "Disabled");
272 printf(" On Demand: %s\n", !!s->regs.en_features.sdsi ? "Enabled" : "Disabled");
273 printf(" Metering: %s\n", !!s->regs.en_features.metering ? "Enabled" : "Disabled");
274 printf("License Key (AKC) Provisioned: %s\n", !!s->regs.key_prov_sts.license_key_provisioned ? "Yes" : "No");
275 printf("Authorization Failure Count\n");
276 printf(" AKC Failure Count: %d\n", s->regs.auth_fail_count.key_failure_count);
277 printf(" AKC Failure Threshold: %d\n", s->regs.auth_fail_count.key_failure_threshold);
278 printf(" CAP Failure Count: %d\n", s->regs.auth_fail_count.auth_failure_count);
279 printf(" CAP Failure Threshold: %d\n", s->regs.auth_fail_count.auth_failure_threshold);
280 printf("Provisioning Availability\n");
281 printf(" Updates Available: %d\n", s->regs.prov_avail.available);
282 printf(" Updates Threshold: %d\n", s->regs.prov_avail.threshold);
283 printf("NVRAM Udate Limit\n");
284 printf(" 50%% Limit Reached: %s\n", !!s->regs.limits.sdsi_50_pct ? "Yes" : "No");
285 printf(" 75%% Limit Reached: %s\n", !!s->regs.limits.sdsi_75_pct ? "Yes" : "No");
286 printf(" 90%% Limit Reached: %s\n", !!s->regs.limits.sdsi_90_pct ? "Yes" : "No");
287 if (s->guid == GUID_V1)
288 printf("Socket ID: %ld\n", s->regs.extra.v1.socket_id & 0xF);
290 printf("Socket ID: %ld\n", s->regs.extra.v2.socket_id & 0xF);
295 static char *license_blob_type(uint32_t type)
298 case LBT_ONE_TIME_UPGRADE:
299 return "One time upgrade";
300 case LBT_METERED_UPGRADE:
301 return "Metered upgrade";
303 return "Unknown license blob type";
307 static char *content_type(uint32_t type)
310 case CONTENT_TYPE_LK_ENC:
311 return "Licencse key encoding";
312 case CONTENT_TYPE_LK_BLOB_ENC:
313 return "License key + Blob encoding";
315 return "Unknown content type";
319 static void get_feature(uint32_t encoding, char *feature)
321 char *name = (char *)&encoding;
323 feature[3] = name[0];
324 feature[2] = name[1];
325 feature[1] = name[2];
326 feature[0] = name[3];
329 static int sdsi_meter_cert_show(struct sdsi_dev *s)
331 char buf[METER_CERT_MAX_SIZE] = {0};
332 struct bundle_encoding_counter *bec;
333 struct meter_certificate *mc;
338 ret = sdsi_update_registers(s);
342 if (!s->regs.en_features.sdsi) {
343 fprintf(stderr, "SDSi feature is present but not enabled.\n");
344 fprintf(stderr, " Unable to read meter certificate\n");
348 if (!s->regs.en_features.metering) {
349 fprintf(stderr, "Metering not supporting on this socket.\n");
353 ret = chdir(s->dev_path);
359 cert_ptr = fopen("meter_certificate", "r");
361 perror("Could not open 'meter_certificate' file");
365 size = fread(buf, 1, sizeof(buf), cert_ptr);
367 fprintf(stderr, "Could not read 'meter_certificate' file\n");
373 mc = (struct meter_certificate *)buf;
376 printf("Meter certificate for device %s\n", s->dev_name);
378 printf("Block Signature: 0x%x\n", mc->block_signature);
379 printf("Count Unit: %dms\n", mc->counter_unit);
380 printf("PPIN: 0x%lx\n", mc->ppin);
381 printf("Feature Bundle Length: %d\n", mc->bundle_length);
382 printf("MMRC encoding: %d\n", mc->mmrc_encoding);
383 printf("MMRC counter: %d\n", mc->mmrc_counter);
384 if (mc->bundle_length % 8) {
385 fprintf(stderr, "Invalid bundle length\n");
389 if (mc->bundle_length > METER_MAX_NUM_BUNDLES * 8) {
390 fprintf(stderr, "More than %d bundles: %d\n",
391 METER_MAX_NUM_BUNDLES, mc->bundle_length / 8);
395 bec = (void *)(mc) + sizeof(mc);
397 printf("Number of Feature Counters: %d\n", mc->bundle_length / 8);
398 while (count++ < mc->bundle_length / 8) {
402 get_feature(bec[count].encoding, feature);
403 printf(" %s: %d\n", feature, bec[count].counter);
409 static int sdsi_state_cert_show(struct sdsi_dev *s)
411 char buf[STATE_CERT_MAX_SIZE] = {0};
412 struct state_certificate *sc;
413 struct license_key_info *lki;
419 ret = sdsi_update_registers(s);
423 if (!s->regs.en_features.sdsi) {
424 fprintf(stderr, "On Demand feature is present but not enabled.");
425 fprintf(stderr, " Unable to read state certificate");
429 ret = chdir(s->dev_path);
435 cert_ptr = fopen("state_certificate", "r");
437 perror("Could not open 'state_certificate' file");
441 size = fread(buf, 1, sizeof(buf), cert_ptr);
443 fprintf(stderr, "Could not read 'state_certificate' file\n");
449 sc = (struct state_certificate *)buf;
451 /* Print register info for this guid */
453 printf("State certificate for device %s\n", s->dev_name);
455 printf("Content Type: %s\n", content_type(sc->content_type));
456 printf("Region Revision ID: %d\n", sc->region_rev_id);
457 printf("Header Size: %d\n", sc->header_size * 4);
458 printf("Total Size: %d\n", sc->total_size);
459 printf("OEM Key Size: %d\n", sc->key_size * 4);
460 printf("Number of Licenses: %d\n", sc->num_licenses);
462 /* Skip over the license sizes 4 bytes per license) to get the license key info */
463 lki = (void *)sc + sizeof(*sc) + (4 * sc->num_licenses);
465 printf("License blob Info:\n");
466 printf(" License Key Revision ID: 0x%x\n", lki->key_rev_id);
467 printf(" License Key Image Content: 0x%lx%lx%lx%lx%lx%lx\n",
468 lki->key_image_content[5], lki->key_image_content[4],
469 lki->key_image_content[3], lki->key_image_content[2],
470 lki->key_image_content[1], lki->key_image_content[0]);
472 while (count++ < sc->num_licenses) {
473 uint32_t blob_size_field = *(uint32_t *)(buf + 0x14 + count * 4);
474 uint32_t blob_size = LICENSE_BLOB_SIZE(blob_size_field);
475 bool license_valid = LICENSE_VALID(blob_size_field);
476 struct license_blob_content *lbc =
477 (void *)(sc) + // start of the state certificate
478 sizeof(*sc) + // size of the state certificate
479 (4 * sc->num_licenses) + // total size of the blob size blocks
480 sizeof(*lki) + // size of the license key info
481 offset; // offset to this blob content
482 struct bundle_encoding *bundle = (void *)(lbc) + sizeof(*lbc);
486 printf(" Blob %d:\n", count - 1);
487 printf(" License blob size: %u\n", blob_size);
488 printf(" License is valid: %s\n", license_valid ? "Yes" : "No");
489 printf(" License blob type: %s\n", license_blob_type(lbc->type));
490 printf(" License blob ID: 0x%lx\n", lbc->id);
491 printf(" PPIN: 0x%lx\n", lbc->ppin);
492 printf(" Previous PPIN: 0x%lx\n", lbc->previous_ppin);
493 printf(" Blob revision ID: %u\n", lbc->rev_id);
494 printf(" Number of Features: %u\n", lbc->num_bundles);
498 for (i = 0; i < min(lbc->num_bundles, STATE_MAX_NUM_IN_BUNDLE); i++) {
499 get_feature(bundle[i].encoding, feature);
500 printf(" Feature %d: %s\n", i, feature);
503 if (lbc->num_bundles > STATE_MAX_NUM_IN_BUNDLE)
504 fprintf(stderr, " Warning: %d > %d licenses in bundle reported.\n",
505 lbc->num_bundles, STATE_MAX_NUM_IN_BUNDLE);
513 static int sdsi_provision(struct sdsi_dev *s, char *bin_file, enum command command)
515 int bin_fd, prov_fd, size, ret;
516 char buf[STATE_CERT_MAX_SIZE] = { 0 };
517 char cap[] = "provision_cap";
518 char akc[] = "provision_akc";
522 fprintf(stderr, "No binary file provided\n");
526 /* Open the binary */
527 bin_fd = open(bin_file, O_RDONLY);
529 fprintf(stderr, "Could not open file %s: %s\n", bin_file, strerror(errno));
533 prov_file = (command == CMD_PROV_AKC) ? akc : cap;
535 ret = chdir(s->dev_path);
542 /* Open the provision file */
543 prov_fd = open(prov_file, O_WRONLY);
545 fprintf(stderr, "Could not open file %s: %s\n", prov_file, strerror(errno));
550 /* Read the binary file into the buffer */
551 size = read(bin_fd, buf, STATE_CERT_MAX_SIZE);
558 ret = write(prov_fd, buf, size);
562 perror("Provisioning failed");
566 printf("Provisioned %s file %s successfully\n", prov_file, bin_file);
574 static int sdsi_provision_akc(struct sdsi_dev *s, char *bin_file)
578 ret = sdsi_update_registers(s);
582 if (!s->regs.en_features.sdsi) {
583 fprintf(stderr, "On Demand feature is present but not enabled. Unable to provision");
587 if (!s->regs.prov_avail.available) {
588 fprintf(stderr, "Maximum number of updates (%d) has been reached.\n",
589 s->regs.prov_avail.threshold);
593 if (s->regs.auth_fail_count.key_failure_count ==
594 s->regs.auth_fail_count.key_failure_threshold) {
595 fprintf(stderr, "Maximum number of AKC provision failures (%d) has been reached.\n",
596 s->regs.auth_fail_count.key_failure_threshold);
597 fprintf(stderr, "Power cycle the system to reset the counter\n");
601 return sdsi_provision(s, bin_file, CMD_PROV_AKC);
604 static int sdsi_provision_cap(struct sdsi_dev *s, char *bin_file)
608 ret = sdsi_update_registers(s);
612 if (!s->regs.en_features.sdsi) {
613 fprintf(stderr, "On Demand feature is present but not enabled. Unable to provision");
617 if (!s->regs.prov_avail.available) {
618 fprintf(stderr, "Maximum number of updates (%d) has been reached.\n",
619 s->regs.prov_avail.threshold);
623 if (s->regs.auth_fail_count.auth_failure_count ==
624 s->regs.auth_fail_count.auth_failure_threshold) {
625 fprintf(stderr, "Maximum number of CAP provision failures (%d) has been reached.\n",
626 s->regs.auth_fail_count.auth_failure_threshold);
627 fprintf(stderr, "Power cycle the system to reset the counter\n");
631 return sdsi_provision(s, bin_file, CMD_PROV_CAP);
634 static int read_sysfs_data(const char *file, int *value)
639 fp = fopen(file, "r");
645 if (!fgets(buff, 16, fp)) {
646 fprintf(stderr, "Failed to read file '%s'", file);
652 *value = strtol(buff, NULL, 0);
657 static struct sdsi_dev *sdsi_create_dev(char *dev_no)
659 int dev_name_len = sizeof(SDSI_DEV) + strlen(dev_no) + 1;
664 s = (struct sdsi_dev *)malloc(sizeof(*s));
670 s->dev_name = (char *)malloc(sizeof(SDSI_DEV) + strlen(dev_no) + 1);
677 snprintf(s->dev_name, dev_name_len, "%s.%s", SDSI_DEV, dev_no);
679 s->dev_path = (char *)malloc(sizeof(AUX_DEV_PATH) + dev_name_len);
687 snprintf(s->dev_path, sizeof(AUX_DEV_PATH) + dev_name_len, "%s%s", AUX_DEV_PATH,
689 dir = opendir(s->dev_path);
691 fprintf(stderr, "Could not open directory '%s': %s\n", s->dev_path,
699 if (chdir(s->dev_path) == -1) {
707 if (read_sysfs_data("guid", &guid)) {
719 static void sdsi_free_dev(struct sdsi_dev *s)
726 static void usage(char *prog)
728 printf("Usage: %s [-l] [-d DEVNO [-i] [-s] [-m] [-a FILE] [-c FILE]]\n", prog);
731 static void show_help(void)
733 printf("Commands:\n");
734 printf(" %-18s\t%s\n", "-l, --list", "list available On Demand devices");
735 printf(" %-18s\t%s\n", "-d, --devno DEVNO", "On Demand device number");
736 printf(" %-18s\t%s\n", "-i, --info", "show socket information");
737 printf(" %-18s\t%s\n", "-s, --state", "show state certificate");
738 printf(" %-18s\t%s\n", "-m, --meter", "show meter certificate");
739 printf(" %-18s\t%s\n", "-a, --akc FILE", "provision socket with AKC FILE");
740 printf(" %-18s\t%s\n", "-c, --cap FILE>", "provision socket with CAP FILE");
743 int main(int argc, char *argv[])
745 char bin_file[PATH_MAX], *dev_no = NULL;
746 bool device_selected = false;
748 enum command command = -1;
751 int option_index = 0;
753 static struct option long_options[] = {
754 {"akc", required_argument, 0, 'a'},
755 {"cap", required_argument, 0, 'c'},
756 {"devno", required_argument, 0, 'd'},
757 {"help", no_argument, 0, 'h'},
758 {"info", no_argument, 0, 'i'},
759 {"list", no_argument, 0, 'l'},
760 {"meter", no_argument, 0, 'm'},
761 {"state", no_argument, 0, 's'},
768 while ((opt = getopt_long_only(argc, argv, "+a:c:d:hilms", long_options,
769 &option_index)) != -1) {
773 device_selected = true;
779 command = CMD_SOCKET_INFO;
782 command = CMD_METER_CERT;
785 command = CMD_STATE_CERT;
789 if (!access(optarg, F_OK) == 0) {
790 fprintf(stderr, "Could not open file '%s': %s\n", optarg,
795 if (!realpath(optarg, bin_file)) {
800 command = (opt == 'a') ? CMD_PROV_AKC : CMD_PROV_CAP;
812 if (device_selected) {
813 s = sdsi_create_dev(dev_no);
818 case CMD_SOCKET_INFO:
819 ret = sdsi_read_reg(s);
822 ret = sdsi_meter_cert_show(s);
825 ret = sdsi_state_cert_show(s);
828 ret = sdsi_provision_akc(s, bin_file);
831 ret = sdsi_provision_cap(s, bin_file);
834 fprintf(stderr, "No command specified\n");
841 fprintf(stderr, "No device specified\n");