2 * Sample application for SMBIOS communication over WMI interface
3 * Performs the following:
4 * - Simple cmd_class/cmd_select lookup for TPM information
5 * - Simple query of known tokens and their values
6 * - Simple activation of a token
8 * Copyright (C) 2017 Dell, Inc.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
19 #include <sys/ioctl.h>
22 /* if uapi header isn't installed, this might not yet exist */
24 #define __packed __attribute__((packed))
26 #include <linux/wmi.h>
28 /* It would be better to discover these using udev, but for a simple
29 * application they're hardcoded
31 static const char *ioctl_devfs
= "/dev/wmi/dell-smbios";
32 static const char *token_sysfs
=
33 "/sys/bus/platform/devices/dell-smbios.0/tokens";
35 static void show_buffer(struct dell_wmi_smbios_buffer
*buffer
)
37 printf("Call: %x/%x [%x,%x,%x,%x]\nResults: [%8x,%8x,%8x,%8x]\n",
38 buffer
->std
.cmd_class
, buffer
->std
.cmd_select
,
39 buffer
->std
.input
[0], buffer
->std
.input
[1],
40 buffer
->std
.input
[2], buffer
->std
.input
[3],
41 buffer
->std
.output
[0], buffer
->std
.output
[1],
42 buffer
->std
.output
[2], buffer
->std
.output
[3]);
45 static int run_wmi_smbios_cmd(struct dell_wmi_smbios_buffer
*buffer
)
50 fd
= open(ioctl_devfs
, O_NONBLOCK
);
51 ret
= ioctl(fd
, DELL_WMI_SMBIOS_CMD
, buffer
);
56 static int find_token(__u16 token
, __u16
*location
, __u16
*value
)
58 char location_sysfs
[60];
64 ret
= sprintf(value_sysfs
, "%s/%04x_value", token_sysfs
, token
);
66 printf("sprintf value failed\n");
69 f
= fopen(value_sysfs
, "rb");
71 printf("failed to open %s\n", value_sysfs
);
74 fread(buf
, 1, 4096, f
);
76 *value
= (__u16
) strtol(buf
, NULL
, 16);
78 ret
= sprintf(location_sysfs
, "%s/%04x_location", token_sysfs
, token
);
80 printf("sprintf location failed\n");
83 f
= fopen(location_sysfs
, "rb");
85 printf("failed to open %s\n", location_sysfs
);
88 fread(buf
, 1, 4096, f
);
90 *location
= (__u16
) strtol(buf
, NULL
, 16);
97 static int token_is_active(__u16
*location
, __u16
*cmpvalue
,
98 struct dell_wmi_smbios_buffer
*buffer
)
102 buffer
->std
.cmd_class
= CLASS_TOKEN_READ
;
103 buffer
->std
.cmd_select
= SELECT_TOKEN_STD
;
104 buffer
->std
.input
[0] = *location
;
105 ret
= run_wmi_smbios_cmd(buffer
);
106 if (ret
!= 0 || buffer
->std
.output
[0] != 0)
108 ret
= (buffer
->std
.output
[1] == *cmpvalue
);
112 static int query_token(__u16 token
, struct dell_wmi_smbios_buffer
*buffer
)
118 ret
= find_token(token
, &location
, &value
);
120 printf("unable to find token %04x\n", token
);
123 return token_is_active(&location
, &value
, buffer
);
126 static int activate_token(struct dell_wmi_smbios_buffer
*buffer
,
133 ret
= find_token(token
, &location
, &value
);
135 printf("unable to find token %04x\n", token
);
138 buffer
->std
.cmd_class
= CLASS_TOKEN_WRITE
;
139 buffer
->std
.cmd_select
= SELECT_TOKEN_STD
;
140 buffer
->std
.input
[0] = location
;
141 buffer
->std
.input
[1] = 1;
142 ret
= run_wmi_smbios_cmd(buffer
);
146 static int query_buffer_size(__u64
*buffer_size
)
150 f
= fopen(ioctl_devfs
, "rb");
153 fread(buffer_size
, sizeof(__u64
), 1, f
);
160 struct dell_wmi_smbios_buffer
*buffer
;
164 ret
= query_buffer_size(&value
);
165 if (ret
== EXIT_FAILURE
|| !value
) {
166 printf("Unable to read buffer size\n");
169 printf("Detected required buffer size %lld\n", value
);
171 buffer
= malloc(value
);
172 if (buffer
== NULL
) {
173 printf("failed to alloc memory for ioctl\n");
177 buffer
->length
= value
;
179 /* simple SMBIOS call for looking up TPM info */
180 buffer
->std
.cmd_class
= CLASS_FLASH_INTERFACE
;
181 buffer
->std
.cmd_select
= SELECT_FLASH_INTERFACE
;
182 buffer
->std
.input
[0] = 2;
183 ret
= run_wmi_smbios_cmd(buffer
);
185 printf("smbios ioctl failed: %d\n", ret
);
191 /* query some tokens */
192 ret
= query_token(CAPSULE_EN_TOKEN
, buffer
);
193 printf("UEFI Capsule enabled token is: %d\n", ret
);
194 ret
= query_token(CAPSULE_DIS_TOKEN
, buffer
);
195 printf("UEFI Capsule disabled token is: %d\n", ret
);
197 /* activate UEFI capsule token if disabled */
199 printf("Enabling UEFI capsule token");
200 if (activate_token(buffer
, CAPSULE_EN_TOKEN
)) {
201 printf("activate failed\n");