diff options
author | Mario Limonciello <mario.limonciello@dell.com> | 2017-11-01 15:25:37 -0400 |
---|---|---|
committer | Darren Hart (VMware) <dvhart@infradead.org> | 2017-11-03 19:34:00 -0400 |
commit | 9d64fc08f6fe59a7d71e84f650dd2c0f080254dd (patch) | |
tree | 9c1129c2bb3ef63e0f58e8cb914b38ac43f46a15 /tools | |
parent | f2645fa317b8905b8934f06a0601d5b7fa66aba0 (diff) |
tools/wmi: add a sample for dell smbios communication over WMI
This application uses the character device /dev/wmi/dell-smbios
to perform SMBIOS communications from userspace.
It offers demonstrations of a few simple tasks:
- Running a class/select command
- Querying a token value
- Activating a token
Signed-off-by: Mario Limonciello <mario.limonciello@dell.com>
Reviewed-by: Edward O'Callaghan <quasisec@google.com>
Signed-off-by: Darren Hart (VMware) <dvhart@infradead.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Makefile | 14 | ||||
-rw-r--r-- | tools/wmi/Makefile | 18 | ||||
-rw-r--r-- | tools/wmi/dell-smbios-example.c | 210 |
3 files changed, 236 insertions, 6 deletions
diff --git a/tools/Makefile b/tools/Makefile index 9dfede37c8ff..9d2fd2606810 100644 --- a/tools/Makefile +++ b/tools/Makefile | |||
@@ -29,6 +29,7 @@ help: | |||
29 | @echo ' usb - USB testing tools' | 29 | @echo ' usb - USB testing tools' |
30 | @echo ' virtio - vhost test module' | 30 | @echo ' virtio - vhost test module' |
31 | @echo ' vm - misc vm tools' | 31 | @echo ' vm - misc vm tools' |
32 | @echo ' wmi - WMI interface examples' | ||
32 | @echo ' x86_energy_perf_policy - Intel energy policy tool' | 33 | @echo ' x86_energy_perf_policy - Intel energy policy tool' |
33 | @echo '' | 34 | @echo '' |
34 | @echo 'You can do:' | 35 | @echo 'You can do:' |
@@ -57,7 +58,7 @@ acpi: FORCE | |||
57 | cpupower: FORCE | 58 | cpupower: FORCE |
58 | $(call descend,power/$@) | 59 | $(call descend,power/$@) |
59 | 60 | ||
60 | cgroup firewire hv guest spi usb virtio vm net iio gpio objtool leds: FORCE | 61 | cgroup firewire hv guest spi usb virtio vm net iio gpio objtool leds wmi: FORCE |
61 | $(call descend,$@) | 62 | $(call descend,$@) |
62 | 63 | ||
63 | liblockdep: FORCE | 64 | liblockdep: FORCE |
@@ -92,7 +93,7 @@ kvm_stat: FORCE | |||
92 | all: acpi cgroup cpupower gpio hv firewire liblockdep \ | 93 | all: acpi cgroup cpupower gpio hv firewire liblockdep \ |
93 | perf selftests spi turbostat usb \ | 94 | perf selftests spi turbostat usb \ |
94 | virtio vm net x86_energy_perf_policy \ | 95 | virtio vm net x86_energy_perf_policy \ |
95 | tmon freefall iio objtool kvm_stat | 96 | tmon freefall iio objtool kvm_stat wmi |
96 | 97 | ||
97 | acpi_install: | 98 | acpi_install: |
98 | $(call descend,power/$(@:_install=),install) | 99 | $(call descend,power/$(@:_install=),install) |
@@ -100,7 +101,7 @@ acpi_install: | |||
100 | cpupower_install: | 101 | cpupower_install: |
101 | $(call descend,power/$(@:_install=),install) | 102 | $(call descend,power/$(@:_install=),install) |
102 | 103 | ||
103 | cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install net_install objtool_install: | 104 | cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install net_install objtool_install wmi_install: |
104 | $(call descend,$(@:_install=),install) | 105 | $(call descend,$(@:_install=),install) |
105 | 106 | ||
106 | liblockdep_install: | 107 | liblockdep_install: |
@@ -125,7 +126,8 @@ install: acpi_install cgroup_install cpupower_install gpio_install \ | |||
125 | hv_install firewire_install iio_install liblockdep_install \ | 126 | hv_install firewire_install iio_install liblockdep_install \ |
126 | perf_install selftests_install turbostat_install usb_install \ | 127 | perf_install selftests_install turbostat_install usb_install \ |
127 | virtio_install vm_install net_install x86_energy_perf_policy_install \ | 128 | virtio_install vm_install net_install x86_energy_perf_policy_install \ |
128 | tmon_install freefall_install objtool_install kvm_stat_install | 129 | tmon_install freefall_install objtool_install kvm_stat_install \ |
130 | wmi_install | ||
129 | 131 | ||
130 | acpi_clean: | 132 | acpi_clean: |
131 | $(call descend,power/acpi,clean) | 133 | $(call descend,power/acpi,clean) |
@@ -133,7 +135,7 @@ acpi_clean: | |||
133 | cpupower_clean: | 135 | cpupower_clean: |
134 | $(call descend,power/cpupower,clean) | 136 | $(call descend,power/cpupower,clean) |
135 | 137 | ||
136 | cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean net_clean iio_clean gpio_clean objtool_clean leds_clean: | 138 | cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean wmi_clean net_clean iio_clean gpio_clean objtool_clean leds_clean: |
137 | $(call descend,$(@:_clean=),clean) | 139 | $(call descend,$(@:_clean=),clean) |
138 | 140 | ||
139 | liblockdep_clean: | 141 | liblockdep_clean: |
@@ -171,6 +173,6 @@ clean: acpi_clean cgroup_clean cpupower_clean hv_clean firewire_clean \ | |||
171 | perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \ | 173 | perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \ |
172 | vm_clean net_clean iio_clean x86_energy_perf_policy_clean tmon_clean \ | 174 | vm_clean net_clean iio_clean x86_energy_perf_policy_clean tmon_clean \ |
173 | freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \ | 175 | freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \ |
174 | gpio_clean objtool_clean leds_clean | 176 | gpio_clean objtool_clean leds_clean wmi_clean |
175 | 177 | ||
176 | .PHONY: FORCE | 178 | .PHONY: FORCE |
diff --git a/tools/wmi/Makefile b/tools/wmi/Makefile new file mode 100644 index 000000000000..e664f1167388 --- /dev/null +++ b/tools/wmi/Makefile | |||
@@ -0,0 +1,18 @@ | |||
1 | PREFIX ?= /usr | ||
2 | SBINDIR ?= sbin | ||
3 | INSTALL ?= install | ||
4 | CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include | ||
5 | CC = $(CROSS_COMPILE)gcc | ||
6 | |||
7 | TARGET = dell-smbios-example | ||
8 | |||
9 | all: $(TARGET) | ||
10 | |||
11 | %: %.c | ||
12 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< | ||
13 | |||
14 | clean: | ||
15 | $(RM) $(TARGET) | ||
16 | |||
17 | install: dell-smbios-example | ||
18 | $(INSTALL) -D -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/$(SBINDIR)/$(TARGET) | ||
diff --git a/tools/wmi/dell-smbios-example.c b/tools/wmi/dell-smbios-example.c new file mode 100644 index 000000000000..9d3bde081249 --- /dev/null +++ b/tools/wmi/dell-smbios-example.c | |||
@@ -0,0 +1,210 @@ | |||
1 | /* | ||
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 | ||
7 | * | ||
8 | * Copyright (C) 2017 Dell, Inc. | ||
9 | * | ||
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. | ||
13 | */ | ||
14 | |||
15 | #include <errno.h> | ||
16 | #include <fcntl.h> | ||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <sys/ioctl.h> | ||
20 | #include <unistd.h> | ||
21 | |||
22 | /* if uapi header isn't installed, this might not yet exist */ | ||
23 | #ifndef __packed | ||
24 | #define __packed __attribute__((packed)) | ||
25 | #endif | ||
26 | #include <linux/wmi.h> | ||
27 | |||
28 | /* It would be better to discover these using udev, but for a simple | ||
29 | * application they're hardcoded | ||
30 | */ | ||
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"; | ||
34 | |||
35 | static void show_buffer(struct dell_wmi_smbios_buffer *buffer) | ||
36 | { | ||
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]); | ||
43 | } | ||
44 | |||
45 | static int run_wmi_smbios_cmd(struct dell_wmi_smbios_buffer *buffer) | ||
46 | { | ||
47 | int fd; | ||
48 | int ret; | ||
49 | |||
50 | fd = open(ioctl_devfs, O_NONBLOCK); | ||
51 | ret = ioctl(fd, DELL_WMI_SMBIOS_CMD, buffer); | ||
52 | close(fd); | ||
53 | return ret; | ||
54 | } | ||
55 | |||
56 | static int find_token(__u16 token, __u16 *location, __u16 *value) | ||
57 | { | ||
58 | char location_sysfs[60]; | ||
59 | char value_sysfs[57]; | ||
60 | char buf[4096]; | ||
61 | FILE *f; | ||
62 | int ret; | ||
63 | |||
64 | ret = sprintf(value_sysfs, "%s/%04x_value", token_sysfs, token); | ||
65 | if (ret < 0) { | ||
66 | printf("sprintf value failed\n"); | ||
67 | return 2; | ||
68 | } | ||
69 | f = fopen(value_sysfs, "rb"); | ||
70 | if (!f) { | ||
71 | printf("failed to open %s\n", value_sysfs); | ||
72 | return 2; | ||
73 | } | ||
74 | fread(buf, 1, 4096, f); | ||
75 | fclose(f); | ||
76 | *value = (__u16) strtol(buf, NULL, 16); | ||
77 | |||
78 | ret = sprintf(location_sysfs, "%s/%04x_location", token_sysfs, token); | ||
79 | if (ret < 0) { | ||
80 | printf("sprintf location failed\n"); | ||
81 | return 1; | ||
82 | } | ||
83 | f = fopen(location_sysfs, "rb"); | ||
84 | if (!f) { | ||
85 | printf("failed to open %s\n", location_sysfs); | ||
86 | return 2; | ||
87 | } | ||
88 | fread(buf, 1, 4096, f); | ||
89 | fclose(f); | ||
90 | *location = (__u16) strtol(buf, NULL, 16); | ||
91 | |||
92 | if (*location) | ||
93 | return 0; | ||
94 | return 2; | ||
95 | } | ||
96 | |||
97 | static int token_is_active(__u16 *location, __u16 *cmpvalue, | ||
98 | struct dell_wmi_smbios_buffer *buffer) | ||
99 | { | ||
100 | int ret; | ||
101 | |||
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) | ||
107 | return ret; | ||
108 | ret = (buffer->std.output[1] == *cmpvalue); | ||
109 | return ret; | ||
110 | } | ||
111 | |||
112 | static int query_token(__u16 token, struct dell_wmi_smbios_buffer *buffer) | ||
113 | { | ||
114 | __u16 location; | ||
115 | __u16 value; | ||
116 | int ret; | ||
117 | |||
118 | ret = find_token(token, &location, &value); | ||
119 | if (ret != 0) { | ||
120 | printf("unable to find token %04x\n", token); | ||
121 | return 1; | ||
122 | } | ||
123 | return token_is_active(&location, &value, buffer); | ||
124 | } | ||
125 | |||
126 | static int activate_token(struct dell_wmi_smbios_buffer *buffer, | ||
127 | __u16 token) | ||
128 | { | ||
129 | __u16 location; | ||
130 | __u16 value; | ||
131 | int ret; | ||
132 | |||
133 | ret = find_token(token, &location, &value); | ||
134 | if (ret != 0) { | ||
135 | printf("unable to find token %04x\n", token); | ||
136 | return 1; | ||
137 | } | ||
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); | ||
143 | return ret; | ||
144 | } | ||
145 | |||
146 | static int query_buffer_size(__u64 *buffer_size) | ||
147 | { | ||
148 | FILE *f; | ||
149 | |||
150 | f = fopen(ioctl_devfs, "rb"); | ||
151 | if (!f) | ||
152 | return -EINVAL; | ||
153 | fread(buffer_size, sizeof(__u64), 1, f); | ||
154 | fclose(f); | ||
155 | return EXIT_SUCCESS; | ||
156 | } | ||
157 | |||
158 | int main(void) | ||
159 | { | ||
160 | struct dell_wmi_smbios_buffer *buffer; | ||
161 | int ret; | ||
162 | __u64 value = 0; | ||
163 | |||
164 | ret = query_buffer_size(&value); | ||
165 | if (ret == EXIT_FAILURE || !value) { | ||
166 | printf("Unable to read buffer size\n"); | ||
167 | goto out; | ||
168 | } | ||
169 | printf("Detected required buffer size %lld\n", value); | ||
170 | |||
171 | buffer = malloc(value); | ||
172 | if (buffer == NULL) { | ||
173 | printf("failed to alloc memory for ioctl\n"); | ||
174 | ret = -ENOMEM; | ||
175 | goto out; | ||
176 | } | ||
177 | buffer->length = value; | ||
178 | |||
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); | ||
184 | if (ret) { | ||
185 | printf("smbios ioctl failed: %d\n", ret); | ||
186 | ret = EXIT_FAILURE; | ||
187 | goto out; | ||
188 | } | ||
189 | show_buffer(buffer); | ||
190 | |||
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); | ||
196 | |||
197 | /* activate UEFI capsule token if disabled */ | ||
198 | if (ret) { | ||
199 | printf("Enabling UEFI capsule token"); | ||
200 | if (activate_token(buffer, CAPSULE_EN_TOKEN)) { | ||
201 | printf("activate failed\n"); | ||
202 | ret = -1; | ||
203 | goto out; | ||
204 | } | ||
205 | } | ||
206 | ret = EXIT_SUCCESS; | ||
207 | out: | ||
208 | free(buffer); | ||
209 | return ret; | ||
210 | } | ||