From patchwork Mon Feb 8 19:27:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 8254321 Return-Path: X-Original-To: patchwork-tpmdd-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0FA669F6DA for ; Mon, 8 Feb 2016 19:27:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E42EE203DB for ; Mon, 8 Feb 2016 19:27:29 +0000 (UTC) Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ABEB4203B8 for ; Mon, 8 Feb 2016 19:27:28 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-1.v29.ch3.sourceforge.com) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aSrT5-0007Ic-Cv; Mon, 08 Feb 2016 19:27:27 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aSrT3-0007IO-Rh for tpmdd-devel@lists.sourceforge.net; Mon, 08 Feb 2016 19:27:25 +0000 X-ACL-Warn: Received: from e36.co.us.ibm.com ([32.97.110.154]) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1aSrT2-00061B-TS for tpmdd-devel@lists.sourceforge.net; Mon, 08 Feb 2016 19:27:25 +0000 Received: from localhost by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 8 Feb 2016 12:27:19 -0700 Received: from d03dlp01.boulder.ibm.com (9.17.202.177) by e36.co.us.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 8 Feb 2016 12:27:17 -0700 X-IBM-Helo: d03dlp01.boulder.ibm.com X-IBM-MailFrom: stefanb@linux.vnet.ibm.com X-IBM-RcptTo: tpmdd-devel@lists.sourceforge.net Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 2E33E1FF0041 for ; Mon, 8 Feb 2016 12:15:27 -0700 (MST) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u18JRHr727132148 for ; Mon, 8 Feb 2016 12:27:17 -0700 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u18JRG9x016262 for ; Mon, 8 Feb 2016 12:27:17 -0700 Received: from dhcp-9-2-140-43.watson.ibm.com (dhcp-9-2-140-28.watson.ibm.com [9.2.140.28]) by d03av04.boulder.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u18JRBMC015717; Mon, 8 Feb 2016 12:27:15 -0700 From: Stefan Berger To: tpmdd-devel@lists.sourceforge.net Date: Mon, 8 Feb 2016 14:27:08 -0500 Message-Id: <1454959628-30582-6-git-send-email-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1454959628-30582-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1454959628-30582-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16020819-0021-0000-0000-000016DADBB9 X-Spam-Score: -0.2 (/) X-Headers-End: 1aSrT2-00061B-TS Cc: dhowells@redhat.com, dwmw2@infradead.org Subject: [tpmdd-devel] [PATCH v5 5/5] A test program for vTPM device creation X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, HK_RANDOM_ENVFROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch provides a program that is for testing purposes only. Build it using the following commands: make headers_install ARCH=x86_64 INSTALL_HDR_PATH=/usr gcc vtpmctrl.c -o vtpmctrl To use it: To create a device pair and have vtpmctrl listen for commands, display them and respond with TPM success messages do: #> ./vtpmctrl Created TPM device /dev/tpm0; vTPM device has fd 4, major/minor = 10/224. In another shell do #> exec 100<>/dev/tpm0 #> /bin/echo -en '\x00\xc1\x00\x00\x00\x0a\x00\x00\x00\x00' >&100 #> od -t x1 <&100 00000000 00 c4 00 00 00 0a 00 00 00 00 00000012 #> exec 100>&- Signed-off-by: Stefan Berger --- vtpmctrl.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 vtpmctrl.c diff --git a/vtpmctrl.c b/vtpmctrl.c new file mode 100644 index 0000000..d39cb23 --- /dev/null +++ b/vtpmctrl.c @@ -0,0 +1,117 @@ +/* + * vtpmctrl.c -- Linux vTPM driver control program + * + * (c) Copyright IBM Corporation 2015. + * + * Author: Stefan Berger + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the names of the IBM Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int vtpmctrl_create(void) +{ + int fd, n, option, li, serverfd, nn; + struct vtpm_new_pair vtpm_new_pair = { + .flags = 0, + }; + char tpmdev[16]; + unsigned char buffer[4096]; + const unsigned char response[] = { + 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00 + }; + + fd = open("/dev/vtpmx", O_RDWR); + if (fd < 0) { + perror("Could not open /dev/vtpmx"); + return 1; + } + + n = ioctl(fd, VTPM_NEW_DEV, &vtpm_new_pair); + if (n != 0) { + perror("ioctl to create dev pair failed"); + close(fd); + return 1; + } + + snprintf(tpmdev, sizeof(tpmdev), "/dev/tpm%u", + vtpm_new_pair.tpm_dev_num); + + serverfd = vtpm_new_pair.fd; + + printf("Created TPM device %s; vTPM device has fd %d, " + "major/minor = %u/%u.\n", + tpmdev, serverfd, vtpm_new_pair.major, vtpm_new_pair.minor); + + close(fd); + + while (1) { + n = read(serverfd, buffer, sizeof(buffer)); + if (n > 0) { + printf("Request with %d bytes:\n", n); + nn = 0; + while (nn < n) { + printf("0x%02x ", buffer[nn]); + nn++; + if (nn % 16 == 0) + printf("\n"); + } + printf("\n"); + n = write(serverfd, response, sizeof(response)); + if (n < 0) { + printf("Error from writing the response: %s\n", + strerror(errno)); + break; + } else { + printf("Sent response with %d bytes.\n", n); + } + } else { + break; + } + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + return vtpmctrl_create(); +}