From patchwork Tue Apr 16 21:08:36 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 237110 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E432A2C0145 for ; Wed, 17 Apr 2013 07:09:33 +1000 (EST) Received: from localhost ([::1]:40618 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USD87-0000Tf-Na for incoming@patchwork.ozlabs.org; Tue, 16 Apr 2013 17:09:31 -0400 Received: from eggs.gnu.org ([208.118.235.92]:44779) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USD7o-0000TF-8y for qemu-devel@nongnu.org; Tue, 16 Apr 2013 17:09:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USD7l-0002th-3B for qemu-devel@nongnu.org; Tue, 16 Apr 2013 17:09:12 -0400 Received: from e9.ny.us.ibm.com ([32.97.182.139]:57342) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USD7k-0002tV-VZ for qemu-devel@nongnu.org; Tue, 16 Apr 2013 17:09:09 -0400 Received: from /spool/local by e9.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 16 Apr 2013 17:09:08 -0400 Received: from d01dlp02.pok.ibm.com (9.56.250.167) by e9.ny.us.ibm.com (192.168.1.109) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 16 Apr 2013 17:09:06 -0400 Received: from d01relay06.pok.ibm.com (d01relay06.pok.ibm.com [9.56.227.116]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id B87D76E8048 for ; Tue, 16 Apr 2013 17:09:02 -0400 (EDT) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay06.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r3GL95BJ29622498 for ; Tue, 16 Apr 2013 17:09:05 -0400 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r3GL8hdR026700 for ; Tue, 16 Apr 2013 17:08:44 -0400 Received: from k-d941f-5.watson.ibm.com (k-d941f-5.watson.ibm.com [9.2.141.165]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r3GL8hNP026649 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 16 Apr 2013 17:08:43 -0400 Received: from k-d941f-5.watson.ibm.com (localhost.localdomain [127.0.0.1]) by k-d941f-5.watson.ibm.com (8.14.5/8.14.3) with ESMTP id r3GL8fnN023856; Tue, 16 Apr 2013 17:08:42 -0400 Received: (from root@localhost) by k-d941f-5.watson.ibm.com (8.14.5/8.14.5/Submit) id r3GL8eq3023855; Tue, 16 Apr 2013 17:08:40 -0400 From: Stefan Berger To: Stefan Berger , qemu-devel@nongnu.org Date: Tue, 16 Apr 2013 17:08:36 -0400 Message-Id: <1366146516-23814-1-git-send-email-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.11.7 X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13041621-7182-0000-0000-000006450366 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 32.97.182.139 Cc: anthony@codemonkey.ws Subject: [Qemu-devel] [PATCH v2] tpm: Simplify creation of cancel path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Simplify the creation of the cancel path given the TPM's device path. Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel. Signed-off-by: Stefan Berger Reviewed-by: Eric Blake --- v1->v2: - followed changes in tree --- hw/tpm/tpm_passthrough.c | 74 ++++++++++------------------------------------- 1 file changed, 17 insertions(+), 57 deletions(-) Index: qemu-git.pt/hw/tpm/tpm_passthrough.c =================================================================== --- qemu-git.pt.orig/hw/tpm/tpm_passthrough.c +++ qemu-git.pt/hw/tpm/tpm_passthrough.c @@ -352,45 +352,17 @@ static int tpm_passthrough_test_tpmdev(i } /* - * Check whether the given base path, e.g., /sys/class/misc/tpm0/device, - * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely - * recognizable by the file entries 'pcrs' and 'cancel'. - * Upon success 'true' is returned and the basebath buffer has '/cancel' - * appended. - */ -static bool tpm_passthrough_check_sysfs_cancel(char *basepath, size_t bufsz) -{ - char path[PATH_MAX]; - struct stat statbuf; - - snprintf(path, sizeof(path), "%s/pcrs", basepath); - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) { - return false; - } - - snprintf(path, sizeof(path), "%s/cancel", basepath); - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) { - return false; - } - - strncpy(basepath, path, bufsz); - - return true; -} - -/* * Unless path or file descriptor set has been provided by user, * determine the sysfs cancel file following kernel documentation * in Documentation/ABI/stable/sysfs-class-tpm. + * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel */ static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) { + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); int fd = -1; - unsigned int idx; - DIR *pnp_dir; + char *dev; char path[PATH_MAX]; - struct dirent entry, *result; - int len; if (tb->cancel_path) { fd = qemu_open(tb->cancel_path, O_WRONLY); @@ -401,34 +373,22 @@ static int tpm_passthrough_open_sysfs_ca return fd; } - snprintf(path, sizeof(path), "/sys/class/misc"); - pnp_dir = opendir(path); - if (pnp_dir != NULL) { - while (readdir_r(pnp_dir, &entry, &result) == 0 && - result != NULL) { - /* - * only allow /sys/class/misc/tpm%u type of paths - */ - if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 || - len <= strlen("tpm") || - len != strlen(entry.d_name)) { - continue; - } - - snprintf(path, sizeof(path), "/sys/class/misc/%s/device", - entry.d_name); - if (!tpm_passthrough_check_sysfs_cancel(path, sizeof(path))) { - continue; - } - + dev = strrchr(tpm_pt->tpm_dev, '/'); + if (dev) { + dev++; + if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel", + dev) < sizeof(path)) { fd = qemu_open(path, O_WRONLY); - break; + if (fd >= 0) { + tb->cancel_path = g_strdup(path); + } else { + error_report("tpm_passthrough: Could not open TPM cancel " + "path %s : %s", path, strerror(errno)); + } } - closedir(pnp_dir); - } - - if (fd >= 0) { - tb->cancel_path = g_strdup(path); + } else { + error_report("tpm_passthrough: Bad TPM device path %s", + tpm_pt->tpm_dev); } return fd;