Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Sample application for SMBIOS communication over WMI interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Performs the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  - Simple cmd_class/cmd_select lookup for TPM information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  - Simple query of known tokens and their values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  - Simple activation of a token
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  Copyright (C) 2017 Dell, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* if uapi header isn't installed, this might not yet exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #ifndef __packed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define __packed __attribute__((packed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/wmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* It would be better to discover these using udev, but for a simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * application they're hardcoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static const char *ioctl_devfs = "/dev/wmi/dell-smbios";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static const char *token_sysfs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			"/sys/bus/platform/devices/dell-smbios.0/tokens";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static void show_buffer(struct dell_wmi_smbios_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	printf("Call: %x/%x [%x,%x,%x,%x]\nResults: [%8x,%8x,%8x,%8x]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	buffer->std.cmd_class, buffer->std.cmd_select,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	buffer->std.input[0], buffer->std.input[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	buffer->std.input[2], buffer->std.input[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	buffer->std.output[0], buffer->std.output[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	buffer->std.output[2], buffer->std.output[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int run_wmi_smbios_cmd(struct dell_wmi_smbios_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	fd = open(ioctl_devfs, O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	ret = ioctl(fd, DELL_WMI_SMBIOS_CMD, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int find_token(__u16 token, __u16 *location, __u16 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char location_sysfs[60];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	char value_sysfs[57];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	char buf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ret = sprintf(value_sysfs, "%s/%04x_value", token_sysfs, token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		printf("sprintf value failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	f = fopen(value_sysfs, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		printf("failed to open %s\n", value_sysfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	fread(buf, 1, 4096, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	*value = (__u16) strtol(buf, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	ret = sprintf(location_sysfs, "%s/%04x_location", token_sysfs, token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		printf("sprintf location failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	f = fopen(location_sysfs, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		printf("failed to open %s\n", location_sysfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	fread(buf, 1, 4096, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	*location = (__u16) strtol(buf, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (*location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int token_is_active(__u16 *location, __u16 *cmpvalue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			   struct dell_wmi_smbios_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	buffer->std.cmd_class = CLASS_TOKEN_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	buffer->std.cmd_select = SELECT_TOKEN_STD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	buffer->std.input[0] = *location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ret = run_wmi_smbios_cmd(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (ret != 0 || buffer->std.output[0] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ret = (buffer->std.output[1] == *cmpvalue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int query_token(__u16 token, struct dell_wmi_smbios_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	__u16 location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	__u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ret = find_token(token, &location, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		printf("unable to find token %04x\n", token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return token_is_active(&location, &value, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int activate_token(struct dell_wmi_smbios_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		   __u16 token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	__u16 location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	__u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ret = find_token(token, &location, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		printf("unable to find token %04x\n", token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	buffer->std.cmd_class = CLASS_TOKEN_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	buffer->std.cmd_select = SELECT_TOKEN_STD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	buffer->std.input[0] = location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	buffer->std.input[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ret = run_wmi_smbios_cmd(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int query_buffer_size(__u64 *buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	f = fopen(ioctl_devfs, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	fread(buffer_size, sizeof(__u64), 1, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct dell_wmi_smbios_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	__u64 value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = query_buffer_size(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (ret == EXIT_FAILURE || !value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		printf("Unable to read buffer size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	printf("Detected required buffer size %lld\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	buffer = malloc(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (buffer == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		printf("failed to alloc memory for ioctl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	buffer->length = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	/* simple SMBIOS call for looking up TPM info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	buffer->std.cmd_class = CLASS_FLASH_INTERFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	buffer->std.cmd_select = SELECT_FLASH_INTERFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	buffer->std.input[0] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = run_wmi_smbios_cmd(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		printf("smbios ioctl failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		ret = EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	show_buffer(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	/* query some tokens */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	ret = query_token(CAPSULE_EN_TOKEN, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	printf("UEFI Capsule enabled token is: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	ret = query_token(CAPSULE_DIS_TOKEN, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	printf("UEFI Capsule disabled token is: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/* activate UEFI capsule token if disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		printf("Enabling UEFI capsule token");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (activate_token(buffer, CAPSULE_EN_TOKEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			printf("activate failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ret = EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	free(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }