1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2/*
3 * Sample code to test CAP protocol
4 *
5 * Copyright(c) 2016 Google Inc. All rights reserved.
6 * Copyright(c) 2016 Linaro Ltd. All rights reserved.
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12#include <sys/ioctl.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15
16#include "../../greybus_authentication.h"
17
18struct cap_ioc_get_endpoint_uid uid;
19struct cap_ioc_get_ims_certificate cert = {
20	.certificate_class = 0,
21	.certificate_id = 0,
22};
23
24struct cap_ioc_authenticate authenticate = {
25	.auth_type = 0,
26	.challenge = {0},
27};
28
29int main(int argc, char *argv[])
30{
31	unsigned int timeout = 10000;
32	char *capdev;
33	int fd, ret;
34
35	/* Make sure arguments are correct */
36	if (argc != 2) {
37		printf("\nUsage: ./firmware <Path of the gb-cap-X dev>\n");
38		return 0;
39	}
40
41	capdev = argv[1];
42
43	printf("Opening %s authentication device\n", capdev);
44
45	fd = open(capdev, O_RDWR);
46	if (fd < 0) {
47		printf("Failed to open: %s\n", capdev);
48		return -1;
49	}
50
51	/* Get UID */
52	printf("Get UID\n");
53
54	ret = ioctl(fd, CAP_IOC_GET_ENDPOINT_UID, &uid);
55	if (ret < 0) {
56		printf("Failed to get UID: %s (%d)\n", capdev, ret);
57		ret = -1;
58		goto close_fd;
59	}
60
61	printf("UID received: 0x%llx\n", *(unsigned long long int *)(uid.uid));
62
63	/* Get certificate */
64	printf("Get IMS certificate\n");
65
66	ret = ioctl(fd, CAP_IOC_GET_IMS_CERTIFICATE, &cert);
67	if (ret < 0) {
68		printf("Failed to get IMS certificate: %s (%d)\n", capdev, ret);
69		ret = -1;
70		goto close_fd;
71	}
72
73	printf("IMS Certificate size: %d\n", cert.cert_size);
74
75	/* Authenticate */
76	printf("Authenticate module\n");
77
78	memcpy(authenticate.uid, uid.uid, 8);
79
80	ret = ioctl(fd, CAP_IOC_AUTHENTICATE, &authenticate);
81	if (ret < 0) {
82		printf("Failed to authenticate module: %s (%d)\n", capdev, ret);
83		ret = -1;
84		goto close_fd;
85	}
86
87	printf("Authenticated, result (%02x), sig-size (%02x)\n",
88		authenticate.result_code, authenticate.signature_size);
89
90close_fd:
91	close(fd);
92
93	return ret;
94}
95