From patchwork Tue Feb 2 16:12:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Shchislowski X-Patchwork-Id: 8191841 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 AA6CEBEEE5 for ; Tue, 2 Feb 2016 16:45:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4C8FC201F5 for ; Tue, 2 Feb 2016 16:45:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 992ED201FA for ; Tue, 2 Feb 2016 16:45:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754461AbcBBQph (ORCPT ); Tue, 2 Feb 2016 11:45:37 -0500 Received: from mail-bn1bon0083.outbound.protection.outlook.com ([157.56.111.83]:9766 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754403AbcBBQpg convert rfc822-to-8bit (ORCPT ); Tue, 2 Feb 2016 11:45:36 -0500 X-Greylist: delayed 1047 seconds by postgrey-1.27 at vger.kernel.org; Tue, 02 Feb 2016 11:45:36 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sandiskcorp.onmicrosoft.com; s=selector1-sandisk-com; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=XGlQo6wPKI5Lr+bHP7bGXA9SIxZ0tRv+W/8PXRMuY4Q=; b=jJs5x7ktxRPP5iM1XeMjIAyq0GamIb1yIyMGSWwqJRNuJSygJI9czo702fTrUudpta+eAgzuusHKqwH3RGCfXbgS4AYkIBWVleAxz9IplC9nKrDztrjI9BP3sE6DpKJuu0p78p69S4j7gsqlPfFvkR3G1qJoqXGBpyWjUKwxtig= Received: from SN1PR02MB1389.namprd02.prod.outlook.com (10.162.0.17) by SN1PR02MB1664.namprd02.prod.outlook.com (10.162.129.18) with Microsoft SMTP Server (TLS) id 15.1.396.15; Tue, 2 Feb 2016 16:12:09 +0000 Received: from SN1PR02MB1389.namprd02.prod.outlook.com ([10.162.0.17]) by SN1PR02MB1389.namprd02.prod.outlook.com ([10.162.0.17]) with mapi id 15.01.0390.019; Tue, 2 Feb 2016 16:12:10 +0000 From: Avi Shchislowski To: Chris Ball , Ulf Hansson CC: "linux-mmc@vger.kernel.org" , Alex Lemberg , Yaniv Agman Subject: [PATCH 1/1] mmc_utils: add ffu support Thread-Topic: [PATCH 1/1] mmc_utils: add ffu support Thread-Index: AdFd1HQu+YidAAW2TZaergMqn/JsuA== Date: Tue, 2 Feb 2016 16:12:09 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: printf.net; dkim=none (message not signed) header.d=none; printf.net; dmarc=none action=none header.from=sandisk.com; x-originating-ip: [212.25.79.133] x-microsoft-exchange-diagnostics: 1; SN1PR02MB1664; 5:F1Oo/sWYatBGLyAw+DB5bFLWT6qCgUienycquPNosa56L6Ct4QrhZUbVRLxVAhT6iUK31Rb9nKqcf78V9vebXnTFGopif/j1XkO9MPAktvJ0YVtJaIOeLo4Ece4VH87X9HhYBdVfx5m3sDSs2R0JWg==; 24:82y3ydUannmmJtJhoNZSJQS+iEbJ3G4rLgAQ1WCqeK4e1m78ffSyDERU5O1mglMDTob16sXwj6spFTVUIfCPPp8uxq6XuI/VkcHGIqOe7u4=; 20:ZjtN1ufPt92xTrKLoO+BsOdadHlhlBz0FY8UeqzM5OzFynNC1T8il8V1AMXz97mpw8miPIus2UFqiXY0U2jZodTUYbp6rdVrluaJo2gOgzeqT1W3k5DLXyYclWMHf2S6arrTkfW8ymGyqRhwHRF5YUg9xCamg3wf1spxfsUqnq2ivBAdMdUldafgGKRlGdQXqrMtDuXh7IqucOMMcZr5cNc0HOtWrBejZMD0n/MmQGs51xRZmjJIKIcEhAOtlV/Y x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN1PR02MB1664; x-ms-office365-filtering-correlation-id: 3278e3d4-953b-4f6d-4d19-08d32beb9abe x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(42932892334569); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046); SRVR:SN1PR02MB1664; BCL:0; PCL:0; RULEID:; SRVR:SN1PR02MB1664; x-forefront-prvs: 084080FC15 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(102836003)(2906002)(5002640100001)(77096005)(1096002)(3846002)(92566002)(40100003)(5001770100001)(1220700001)(3280700002)(122556002)(189998001)(6116002)(4001430100002)(3660700001)(586003)(3470700001)(10400500002)(2900100001)(5001960100002)(4326007)(5008740100001)(107886002)(5004730100002)(19580405001)(575784001)(33656002)(50986999)(54356999)(19580395003)(76576001)(87936001)(99286002)(5003600100002)(229853001)(86362001)(66066001)(74316001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR02MB1664; H:SN1PR02MB1389.namprd02.prod.outlook.com; FPR:; SPF:None; MLV:sfv; LANG:en; spamdiagnosticoutput: 1:23 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Feb 2016 16:12:10.0343 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR02MB1664 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham 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 Adding support for field firmware update over multiple command ioctl. As multiple command ioctl is supported only from kernel 4.4, this patch should be used against kernel 4.4 and above. Known issues: - There is no support for Multiple Block write commands (CMD25) in existing IOCTL implementation - In case MODE_OPERATION_CODES field is not supported by the device manual reset of the device/platform is required. The reset issue discussed in another email thread - " [RFC 0/6] mmc: Field Firmware Update" Signed-off-by: Yaniv Agman Signed-off-by: Avi Shchislowski diff --git a/mmc.c b/mmc.c index a13d9ae..b1a8b3a 100644 --- a/mmc.c +++ b/mmc.c @@ -13,6 +13,9 @@ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. * + * Modified Jan. 2016 by SanDisk Corp. + * Copyright (c) 2016 SanDisk Corp. + * * (This code is based on btrfs-progs/btrfs.c.) */ @@ -175,6 +178,11 @@ static struct Command commands[] = { "NOTE! The cache is an optional feature on devices >= eMMC4.5.", NULL }, + { do_ffu, -2, + "ffu", " \n" + "Run Field Firmware Update with on .\n", + NULL + }, { 0, 0, 0, 0 } }; diff --git a/mmc.h b/mmc.h index b7063fb..9de8bcc 100644 --- a/mmc.h +++ b/mmc.h @@ -12,6 +12,9 @@ * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. + * + * Modified Jan. 2016 by SanDisk Corp. + * Copyright (c) 2016 SanDisk Corp. */ #include @@ -34,6 +37,7 @@ #define R1_SWITCH_ERROR (1 << 7) /* sx, c */ #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */ #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */ +#define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */ #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */ /* @@ -42,6 +46,17 @@ #define EXT_CSD_S_CMD_SET 504 #define EXT_CSD_HPI_FEATURE 503 #define EXT_CSD_BKOPS_SUPPORT 502 /* RO */ +#define EXT_CSD_SUPPORTED_MODES 493 /* RO */ +#define EXT_CSD_FFU_FEATURES 492 /* RO */ +#define EXT_CSD_FFU_ARG_3 490 /* RO */ +#define EXT_CSD_FFU_ARG_2 489 /* RO */ +#define EXT_CSD_FFU_ARG_1 488 /* RO */ +#define EXT_CSD_FFU_ARG_0 487 /* RO */ +#define EXT_CSD_NUM_OF_FW_SEC_PROG_3 305 /* RO */ +#define EXT_CSD_NUM_OF_FW_SEC_PROG_2 304 /* RO */ +#define EXT_CSD_NUM_OF_FW_SEC_PROG_1 303 /* RO */ +#define EXT_CSD_NUM_OF_FW_SEC_PROG_0 302 /* RO */ +#define EXT_CSD_FIRMWARE_VERSION 254 /* RO */ #define EXT_CSD_CACHE_SIZE_3 252 #define EXT_CSD_CACHE_SIZE_2 251 #define EXT_CSD_CACHE_SIZE_1 250 @@ -58,6 +73,7 @@ #define EXT_CSD_BOOT_BUS_CONDITIONS 177 #define EXT_CSD_ERASE_GROUP_DEF 175 #define EXT_CSD_BOOT_WP 173 +#define EXT_CSD_FW_CONFIG 169 /* R/W */ #define EXT_CSD_WR_REL_SET 167 #define EXT_CSD_WR_REL_PARAM 166 #define EXT_CSD_SANITIZE_START 165 @@ -94,6 +110,9 @@ #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1 53 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0 52 #define EXT_CSD_CACHE_CTRL 33 +#define EXT_CSD_MODE_CONFIG 30 +#define EXT_CSD_MODE_OPERATION_CODES 29 /* W */ +#define EXT_CSD_FFU_STATUS 26 /* R */ /* * WR_REL_PARAM field definitions @@ -109,6 +128,11 @@ /* * EXT_CSD field definitions */ +#define EXT_CSD_FFU_INSTALL (0x01) +#define EXT_CSD_FFU_MODE (0x01) +#define EXT_CSD_NORMAL_MODE (0x00) +#define EXT_CSD_FFU (1<<0) +#define EXT_CSD_UPDATE_DISABLE (1<<0) #define EXT_CSD_HPI_SUPP (1<<0) #define EXT_CSD_HPI_IMPL (1<<1) #define EXT_CSD_CMD_SET_NORMAL (1<<0) diff --git a/mmc_cmds.c b/mmc_cmds.c index 77446b4..dec9b9e 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -12,6 +12,9 @@ * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. + * + * Modified Jan. 2016 by SanDisk Corp. + * Copyright (c) 2016 SanDisk Corp. */ #include @@ -1017,6 +1020,9 @@ int do_read_extcsd(int nargs, char **argv) ext_csd_rev = ext_csd[EXT_CSD_REV]; switch (ext_csd_rev) { + case 8: + str = "5.1"; + break; case 7: str = "5.0"; break; @@ -1430,6 +1436,10 @@ int do_read_extcsd(int nargs, char **argv) /*Reserved [31:0] */ } + if (ext_csd_rev >= 7) { + printf("eMMC Firmware Version: %s\n", + (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]); + } out_free: return ret; } @@ -2032,3 +2042,218 @@ int do_cache_dis(int nargs, char **argv) { return do_cache_ctrl(0, nargs, argv); } + +int do_ffu(int nargs, char **argv) +{ + int dev_fd, img_fd; + int sect_done = 0, retry = 3, ret = -EINVAL; + unsigned int sect_size; + __u8 ext_csd[512]; + __u8 *buf; + __u32 arg; + off_t fw_size; + ssize_t chunk_size; + char *device; + struct mmc_ioc_multi_cmd *multi_cmd; + + CHECK(nargs != 3, "Usage: ffu \n", + exit(1)); + + device = argv[2]; + dev_fd = open(device, O_RDWR); + if (dev_fd < 0) { + perror("device open failed"); + exit(1); + } + img_fd = open(argv[1], O_RDONLY); + if (img_fd < 0) { + perror("image open failed"); + close(dev_fd); + exit(1); + } + + buf = malloc(512); + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + + 3 * sizeof(struct mmc_ioc_cmd)); + if (!buf || !multi_cmd) { + perror("failed to allocate memory"); + goto out; + } + + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } + + if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) { + fprintf(stderr, + "The FFU feature is only available on devices >= " + "MMC 5.0, not supported in %s\n", device); + goto out; + } + + if (!(ext_csd[EXT_CSD_SUPPORTED_MODES] & EXT_CSD_FFU)) { + fprintf(stderr, "FFU is not supported in %s\n", device); + goto out; + } + + if (ext_csd[EXT_CSD_FW_CONFIG] & EXT_CSD_UPDATE_DISABLE) { + fprintf(stderr, "Firmware update was disabled in %s\n", device); + goto out; + } + + fw_size = lseek(img_fd, 0, SEEK_END); + + if (fw_size == 0) { + fprintf(stderr, "Firmware image is empty"); + goto out; + } + + sect_size = (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 0) ? 512 : 4096; + if (fw_size % sect_size) { + fprintf(stderr, "Firmware data size (%jd) is not aligned!\n", (intmax_t)fw_size); + goto out; + } + + /* set CMD ARG */ + arg = ext_csd[EXT_CSD_FFU_ARG_0] | + ext_csd[EXT_CSD_FFU_ARG_1] << 8 | + ext_csd[EXT_CSD_FFU_ARG_2] << 16 | + ext_csd[EXT_CSD_FFU_ARG_3] << 24; + + /* prepare multi_cmd to be sent */ + multi_cmd->num_of_cmds = 3; + + /* put device into ffu mode */ + multi_cmd->cmds[0].opcode = MMC_SWITCH; + multi_cmd->cmds[0].arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | + (EXT_CSD_MODE_CONFIG << 16) | + (EXT_CSD_FFU_MODE << 8) | + EXT_CSD_CMD_SET_NORMAL; + multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; + multi_cmd->cmds[0].write_flag = 1; + + /* send image chunk */ + multi_cmd->cmds[1].opcode = MMC_WRITE_BLOCK; + multi_cmd->cmds[1].blksz = sect_size; + multi_cmd->cmds[1].blocks = 1; + multi_cmd->cmds[1].arg = arg; + multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; + multi_cmd->cmds[1].write_flag = 1; + mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf); + + /* return device into normal mode */ + multi_cmd->cmds[2].opcode = MMC_SWITCH; + multi_cmd->cmds[2].arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | + (EXT_CSD_MODE_CONFIG << 16) | + (EXT_CSD_NORMAL_MODE << 8) | + EXT_CSD_CMD_SET_NORMAL; + multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; + multi_cmd->cmds[2].write_flag = 1; + +do_retry: + /* read firmware chunk */ + lseek(img_fd, 0, SEEK_SET); + chunk_size = read(img_fd, buf, 512); + + while (chunk_size > 0) { + /* send ioctl with multi-cmd */ + ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + + if (ret) { + perror("Multi-cmd ioctl"); + /* In case multi-cmd ioctl failed before exiting from ffu mode */ + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[2]); + goto out; + } + + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } + + /* Test if we need to restart the download */ + sect_done = ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0] | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_1] << 8 | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_2] << 16 | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_3] << 24; + /* By spec, host should re-start download from the first sector if sect_done is 0 */ + if (sect_done == 0) { + if (retry > 0) { + retry--; + fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry); + goto do_retry; + } + fprintf(stderr, "Programming failed! Aborting...\n"); + goto out; + } else { + fprintf(stderr, "Programmed %d/%jd bytes\r", sect_done * sect_size, (intmax_t)fw_size); + } + + /* read the next firmware chunk (if any) */ + chunk_size = read(img_fd, buf, 512); + } + + if ((sect_done * sect_size) == fw_size) { + fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)fw_size, (intmax_t)fw_size); + fprintf(stderr, "Programming finished with status %d \n", ret); + } + else { + fprintf(stderr, "FW size and number of sectors written mismatch. Status return %d\n", ret); + goto out; + } + + /* check mode operation for ffu install*/ + if (!ext_csd[EXT_CSD_FFU_FEATURES]) { + fprintf(stderr, "Please reboot to complete firmware installation on %s\n", device); + } else { + fprintf(stderr, "Installing firmware on %s...\n", device); + /* Re-enter ffu mode and install the firmware */ + multi_cmd->num_of_cmds = 2; + + /* set ext_csd to install mode */ + multi_cmd->cmds[1].opcode = MMC_SWITCH; + multi_cmd->cmds[1].blksz = 0; + multi_cmd->cmds[1].blocks = 0; + multi_cmd->cmds[1].arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | + (EXT_CSD_MODE_OPERATION_CODES << 16) | + (EXT_CSD_FFU_INSTALL << 8) | + EXT_CSD_CMD_SET_NORMAL; + multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; + multi_cmd->cmds[1].write_flag = 1; + + /* send ioctl with multi-cmd */ + ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + + if (ret) { + perror("Multi-cmd ioctl failed setting install mode"); + /* In case multi-cmd ioctl failed before exiting from ffu mode */ + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[2]); + goto out; + } + + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } + + /* return status */ + ret = ext_csd[EXT_CSD_FFU_STATUS]; + if (ret) { + fprintf(stderr, "%s: error %d during FFU install:\n", device, ret); + goto out; + } else { + fprintf(stderr, "FFU finished successfully\n"); + } + } + +out: + free(buf); + free(multi_cmd); + close(img_fd); + close(dev_fd); + return ret; +} diff --git a/mmc_cmds.h b/mmc_cmds.h index 75d8f8c..f645c38 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -12,6 +12,9 @@ * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. + * + * Modified Jan. 2016 by SanDisk Corp. + * Copyright (c) 2016 SanDisk Corp. */ /* mmc_cmds.c */ @@ -36,3 +39,4 @@ int do_rpmb_read_block(int nargs, char **argv); int do_rpmb_write_block(int nargs, char **argv); int do_cache_en(int nargs, char **argv); int do_cache_dis(int nargs, char **argv); +int do_ffu(int nargs, char **argv);