From patchwork Tue May 19 23:11:10 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Cooper X-Patchwork-Id: 6441061 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4B4AFC0432 for ; Tue, 19 May 2015 23:12:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6378D203DF for ; Tue, 19 May 2015 23:12:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6B413203E5 for ; Tue, 19 May 2015 23:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752198AbbESXMS (ORCPT ); Tue, 19 May 2015 19:12:18 -0400 Received: from mail-gw3-out.broadcom.com ([216.31.210.64]:44679 "EHLO mail-gw3-out.broadcom.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751509AbbESXMS (ORCPT ); Tue, 19 May 2015 19:12:18 -0400 X-IronPort-AV: E=Sophos;i="5.13,460,1427785200"; d="scan'208";a="65160065" Received: from irvexchcas08.broadcom.com (HELO IRVEXCHCAS08.corp.ad.broadcom.com) ([10.9.208.57]) by mail-gw3-out.broadcom.com with ESMTP; 19 May 2015 16:22:04 -0700 Received: from IRVEXCHSMTP2.corp.ad.broadcom.com (10.9.207.52) by IRVEXCHCAS08.corp.ad.broadcom.com (10.9.208.57) with Microsoft SMTP Server (TLS) id 14.3.235.1; Tue, 19 May 2015 16:12:08 -0700 Received: from mail-irva-13.broadcom.com (10.10.10.20) by IRVEXCHSMTP2.corp.ad.broadcom.com (10.9.207.52) with Microsoft SMTP Server id 14.3.235.1; Tue, 19 May 2015 16:12:08 -0700 Received: from stbsrv-and-3.and.broadcom.com (unknown [10.28.16.21]) by mail-irva-13.broadcom.com (Postfix) with ESMTP id 28B0640FE8; Tue, 19 May 2015 16:10:26 -0700 (PDT) From: Al Cooper To: , , CC: Al Cooper Subject: [PATCH V3 4/8] mmc: lock: Add card lock/unlock maintenance commands Date: Tue, 19 May 2015 19:11:10 -0400 Message-ID: <1432077074-8422-5-git-send-email-alcooperx@gmail.com> X-Mailer: git-send-email 1.9.0.138.g2de3478 In-Reply-To: <1432077074-8422-1-git-send-email-alcooperx@gmail.com> References: <1432077074-8422-1-git-send-email-alcooperx@gmail.com> MIME-Version: 1.0 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_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 Create a sysfs interface that allows a user to manage an inserted cards lock state. The sysfs attribute "lock" will be added to the device's sysfs directory. The following commands are supported: "setpw" - Set the cards password "clearpw" - Clear the cards password "lock" - Lock the card "unlock" - Unlock the card "erase" - Force erase the card, clear the password and unlock it Commands that require a password will request the password through the kernels KEYS subsystem. Signed-off-by: Al Cooper --- drivers/mmc/core/mmc.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index f36c76f..8e94eb8 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -697,6 +697,84 @@ static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width) return err; } +#ifdef CONFIG_MMC_LOCK + +ssize_t mmc_lock_show(struct device *dev, struct device_attribute *att, + char *buf) +{ + struct mmc_card *card = mmc_dev_to_card(dev); + + if (!mmc_card_lockable(card)) + return sprintf(buf, "unsupported\n"); + else + return sprintf(buf, "%slocked\n", mmc_card_locked(card) ? + "" : "un"); +} + + +static struct lock_cmd { + const char *name; + int io_cmd; + int locked_required; + int need_pw; +} lock_cmds[] = { + { "erase", MMC_LOCK_MODE_ERASE, true, false }, + { "clrpw", MMC_LOCK_MODE_CLR_PWD, false, true }, + { "setpw", MMC_LOCK_MODE_SET_PWD, false, true }, + { "lock", MMC_LOCK_MODE_LOCK, false, true }, + { "unlock", MMC_LOCK_MODE_UNLOCK, true, true }, +}; + +/* + * implement MMC password functions: force erase, set password, + * clear password, lock and unlock. + */ +ssize_t mmc_lock_store(struct device *dev, struct device_attribute *att, + const char *data, size_t len) +{ + struct mmc_card *card = mmc_dev_to_card(dev); + int res = -EINVAL; + int x; + struct mmc_password password; + + mmc_claim_host(card->host); + if (!mmc_card_lockable(card)) + goto out; + for (x = 0; x < ARRAY_SIZE(lock_cmds); x++) { + if (sysfs_streq(data, lock_cmds[x].name)) + break; + } + if (x >= ARRAY_SIZE(lock_cmds)) + goto out; + + if ((lock_cmds[x].locked_required && !mmc_card_locked(card)) || + (!lock_cmds[x].locked_required && mmc_card_locked(card))) { + dev_warn(dev, "%s requires %slocked card\n", + lock_cmds[x].name, + lock_cmds[x].locked_required ? "" : "un"); + goto out; + } + if (lock_cmds[x].need_pw) { + res = mmc_get_password(card, &password); + if (res) + goto out; + } + res = mmc_lock_unlock(card, &password, lock_cmds[x].io_cmd); +out: + mmc_release_host(card->host); + if (res == 0) + return len; + else + return res; +} + +static DEVICE_ATTR(lock, S_IWUSR | S_IRUGO, + mmc_lock_show, mmc_lock_store); + + +#endif /* CONFIG_MMC_LOCK */ + + MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], card->raw_cid[2], card->raw_cid[3]); MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], @@ -751,6 +829,9 @@ static struct attribute *mmc_std_attrs[] = { &dev_attr_enhanced_area_size.attr, &dev_attr_raw_rpmb_size_mult.attr, &dev_attr_rel_sectors.attr, +#ifdef CONFIG_MMC_LOCK + &dev_attr_lock.attr, +#endif /* CONFIG_MMC_LOCK */ NULL, }; ATTRIBUTE_GROUPS(mmc_std);