From patchwork Wed Oct 23 14:38:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13847178 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19C781E2607 for ; Wed, 23 Oct 2024 14:41:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.53 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; cv=pass; b=uWV6HyocvmUk/oQHv3QJdb0AiN8fmkBWgQMCox4FUqSg/u2+nXoZG1m7k9PXDLE4JK2YyYIARSAmgznCQ4eICI/Wd6nIi1OTr/DY/iN4oAPWT5iU3i/lvBH/tq8Q/2t99LJyit8WRECV6DSZ2AGdj1Nz3QbymFjlMe6aqriRQ08= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; c=relaxed/simple; bh=bFBF1fU/1hCUug9kd0yo6iuELjwChWvS4TF228+hrmc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ljcIluY6HT62IDctwOpYj3mbhBePrTLxeTMWaHPw+N69en3Gox7WmXgMd3SepsLTvzfGiGfgMycCBAePvpDV4XO2bcDZEcRJmsLrNEal6CL6yZoQ/A0aLCYL7VM5ROkkkioW+JLrsSZoHxGfs1vHdwUItxaq84bPDcbFhAIJ3m4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de; spf=none smtp.mailfrom=iokpp.de; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=Gz1ZA6r6; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=j6HOZBzt; arc=pass smtp.client-ip=85.215.255.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=iokpp.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="Gz1ZA6r6"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="j6HOZBzt" ARC-Seal: i=1; a=rsa-sha256; t=1729694329; cv=none; d=strato.com; s=strato-dkim-0002; b=K0EIZXGsufZcqzAMZM+Cy5N+0VFQbvHc44NYsAriunXlLvCOj/GER0gcYq0pbgh1xE 3/lo/ydiXiEF/VQgsVwnpu8x+TPwLT9WkvxKUInEVMFmWu9xMPSkz1BFginiay5iGEIu fT0nBEplG+bR9EMPZr/D4E6r2VczTeRJiWYD93RHEaxQpczgJhIe0WYMoSVKTiVUR2Km WoDlMQL1fZMHwxhbIDEPkN4w9PcPmXWLfz6TZPHOmQ/QjHVxTXyf+Ss815IzS2BD/0Py gcRGfi2L0mpckIf+SB5yzoC6QJkOpkyE7UlZehaMMyhLcj4ROinc2K0WR6SEN6dJbkv6 bcMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=k3Sjiggo0lZRu9KiyTS7uTb1sqtbe6N9KUJ3E0dKihc=; b=rrzzp5GwPNxwAXJYwN1lgCnCCXRmjzkL0jnPHl4SZE1pB97bo/ArJtsbBCubMEsrgy 4QA/+H+7CBLo2P3flKoFM4ji2clNA0IOyyQ/fg7wlkTefMBNH4O6UEa1A/xvylADIsPB pd5k+UcvFePAY6CtcSBN3iaTpO4jtm4rzPmQxTToj/oIQQ6sC7AhQz8XS+8JhqreXoy7 y94Ent2oRMXNTCkCT/5GA5QIPJpctDEhiiM/ZVi6Lw0Om7l1QeXIO9dn9Gxv1T1OP0f1 tOS8hHwPq8/t1sgUGAO4DMhdvmh4DgkEwOwZTh1keeMyYdkSLwIp+IPLjNu8VVzblkVV P6ZA== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0002; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=k3Sjiggo0lZRu9KiyTS7uTb1sqtbe6N9KUJ3E0dKihc=; b=Gz1ZA6r6NDZvi5Si5LoK1endvpG3U6HRhlAxoC6PZcrmgf63pO5zAt5Z/ncCnoKT+l 1JfwtA1gQvFxD2QJqoSO/ZLrJwRzbeITvcucRkeQ7+0eVCah4PoQychL5T+QCUBa/lTm O0NQFXc031Q5506jjzX8u+4t25XRnEVJlTocAwpxs3x+geU2DFvQ10lmvGcGmg6Aa9Ax 2NacnzQI8yTKsV10IEu8tQKju8A0kqTOO6GFSJ/oqwpjyJJvVqrSvGzdCD5PV0TOA5Iw UAQ0YrzP93M2IwvXRDUsEpHHj6x4CXg2TFciNqban/rYnNIT9VOX7yAdi2Py1xdtsYjB K1/w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0003; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=k3Sjiggo0lZRu9KiyTS7uTb1sqtbe6N9KUJ3E0dKihc=; b=j6HOZBztSvlqmfJRsZB4VI5r79gXZJxRDQYgekXfm7uVTuLFnhsbN8R8Kcjn5RIiyy zbAX567q7Ht4HlSXXHCw== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O2J2YOom0XQaPis+nU/xK" Received: from Munilab01-lab.micron.com by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209NEcnTm8 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Wed, 23 Oct 2024 16:38:49 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org, vfazio@xes-inc.com Cc: Bean Huo Subject: [PATCH v4 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes Date: Wed, 23 Oct 2024 16:38:35 +0200 Message-Id: <20241023143839.108572-2-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241023143839.108572-1-beanhuo@iokpp.de> References: <20241023143839.108572-1-beanhuo@iokpp.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bean Huo Refactor common FFU code into functions to support additional FFU modes. Follow-up patches will focus on implementing additional FFU modes and enhancements. Signed-off-by: Bean Huo Acked-by: Avri Altman --- mmc_cmds.c | 326 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 207 insertions(+), 119 deletions(-) diff --git a/mmc_cmds.c b/mmc_cmds.c index 3b1bcf4..1a35431 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -29,6 +29,7 @@ #include #include #include /* for BLKGETSIZE */ +#include #include "mmc.h" #include "mmc_cmds.h" @@ -2810,7 +2811,7 @@ out: return ret; } -static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, +static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_csd, unsigned int bytes, __u8 *buf, off_t offset) { @@ -2831,91 +2832,81 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf + offset); } -int do_ffu(int nargs, char **argv) +/* + * Retrieves the number of sectors programmed during FFU download. + * + * @dev_fd: File descriptor for the eMMC device. + * @ext_csd: Pointer to the buffer holding the Extended CSD register data of the eMMC device. + * + * Return: The number of sectors programmed, or -1 if reading the EXT_CSD fails. + */ +static int get_ffu_sectors_programmed(int dev_fd, __u8 *ext_csd) { - int dev_fd, img_fd; - int retry = 3, ret = -EINVAL; - unsigned int sect_size; - __u8 ext_csd[512]; - __u8 *buf = NULL; - off_t fw_size, bytes_left, off; - char *device; - struct mmc_ioc_multi_cmd *multi_cmd = NULL; - unsigned int default_chunk = MMC_IOC_MAX_BYTES; - __u32 sect_done = 0; - - assert (nargs == 3 || nargs == 4); - 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); + if (read_extcsd(dev_fd, ext_csd)) { + fprintf(stderr, "Could not read EXT_CSD\n"); + return -1; } - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); - goto out; - } + return per_byte_htole32((__u8 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]); +} + +static bool ffu_is_supported(__u8 *ext_csd, char *device) +{ if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) { - fprintf(stderr, - "The FFU feature is only available on devices >= " + fprintf(stderr, "The FFU feature is only available on devices >= " "MMC 5.0, not supported in %s\n", device); - goto out; + return false; } if (!(ext_csd[EXT_CSD_SUPPORTED_MODES] & EXT_CSD_FFU)) { fprintf(stderr, "FFU is not supported in %s\n", device); - goto out; + return false; } if (ext_csd[EXT_CSD_FW_CONFIG] & EXT_CSD_UPDATE_DISABLE) { fprintf(stderr, "Firmware update was disabled in %s\n", device); - goto out; + return false; } - fw_size = lseek(img_fd, 0, SEEK_END); - if (fw_size == 0) { - fprintf(stderr, "Wrong firmware size"); - goto out; - } + return true; +} +/* + * Performs FFU download of the firmware bundle. + * + * @dev_fd: File descriptor for the eMMC device on which the ioctl command will be performed. + * @ext_csd: Extended CSD register data of the eMMC device. + * @fw_buf: Pointer to the firmware buffer containing the firmware data to be downloaded. + * @fw_size: Size of the firmware in bytes. + * @chunk_size: Size of the chunks in which the firmware is sent to the device. + * + * Return: If successful, returns the number of sectors programmed. + * On failure, returns a negative error number. + */ +static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_size, + unsigned int chunk_size) +{ + int ret; + __u8 num_of_cmds = 4; + off_t bytes_left, off; + unsigned int bytes_per_loop, retry = 3; + struct mmc_ioc_multi_cmd *multi_cmd = NULL; + + if (!fw_buf || !ext_csd) { + fprintf(stderr, "unexpected NULL pointer\n"); + return -EINVAL; + } /* allocate maximum required */ - buf = malloc(fw_size); multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + - 4 * sizeof(struct mmc_ioc_cmd)); - if (!buf || !multi_cmd) { + num_of_cmds * sizeof(struct mmc_ioc_cmd)); + if (!multi_cmd) { perror("failed to allocate memory"); - goto out; - } - - /* ensure fw is multiple of native sector size */ - 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; - } - - if (nargs == 4) { - default_chunk = strtol(argv[3], NULL, 10); - if (default_chunk > MMC_IOC_MAX_BYTES || default_chunk % 512) { - fprintf(stderr, "Invalid chunk size"); - goto out; - } + return -ENOMEM; } /* prepare multi_cmd for FFU based on cmd to be used */ - - multi_cmd->num_of_cmds = 4; - /* put device into ffu mode */ fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); @@ -2924,25 +2915,18 @@ int do_ffu(int nargs, char **argv) fill_switch_cmd(&multi_cmd->cmds[3], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); - /* read firmware */ - lseek(img_fd, 0, SEEK_SET); - if (read(img_fd, buf, fw_size) != fw_size) { - perror("Could not read the firmware file: "); - ret = -ENOSPC; - goto out; - } - do_retry: bytes_left = fw_size; off = 0; + multi_cmd->num_of_cmds = num_of_cmds; + while (bytes_left) { - unsigned int chunk_size = bytes_left < default_chunk ? - bytes_left : default_chunk; + bytes_per_loop = bytes_left < chunk_size ? bytes_left : chunk_size; /* prepare multi_cmd for FFU based on cmd to be used */ - set_ffu_single_cmd(multi_cmd, ext_csd, chunk_size, buf, off); + set_ffu_download_cmd(multi_cmd, ext_csd, bytes_per_loop, fw_buf, off); - /* send ioctl with multi-cmd */ + /* send ioctl with multi-cmd, download firmware bundle */ ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); if (ret) { @@ -2955,83 +2939,187 @@ do_retry: goto out; } - bytes_left -= chunk_size; - off += chunk_size; + ret = get_ffu_sectors_programmed(dev_fd, ext_csd); + if (ret <= 0) { + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); + /* + * By spec, host should re-start download from the first sector if + * programmed count is 0 + */ + if (ret == 0 && 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", ret * 512, (intmax_t)fw_size); + } + + bytes_left -= bytes_per_loop; + off += bytes_per_loop; } - /* - * By spec - check if mode operation codes are supported in ffu features, - * if not then skip checking number of sectors programmed after install - */ - if (!ext_csd[EXT_CSD_FFU_FEATURES]) { - fprintf(stderr, "Please reboot to complete firmware installation on %s\n", device); - ret = 0; + ret = get_ffu_sectors_programmed(dev_fd, ext_csd); +out: + free(multi_cmd); + return ret; +} + +static int do_ffu_install(int dev_fd, const char *device) +{ + int ret; + __u8 ext_csd[512]; + struct mmc_ioc_multi_cmd *multi_cmd = NULL; + + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + 2 * sizeof(struct mmc_ioc_cmd)); + if (!multi_cmd) { + perror("failed to allocate memory"); + return -ENOMEM; + } + + /* Re-enter ffu mode and install the firmware */ + multi_cmd->num_of_cmds = 2; + fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); + fill_switch_cmd(&multi_cmd->cmds[1], EXT_CSD_MODE_OPERATION_CODES, EXT_CSD_FFU_INSTALL); + + /* 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"); + fill_switch_cmd(&multi_cmd->cmds[1], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); + /* In case multi-cmd ioctl failed before exiting from ffu mode */ + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[1]); goto out; } + /* Check FFU install status */ 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 = per_byte_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]); - /* By spec, host should re-start download from the first sector if sect_done is 0 */ - if (sect_done == 0) { - if (retry--) { - fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry); - goto do_retry; + /* Return status */ + ret = ext_csd[EXT_CSD_FFU_STATUS]; +out: + free(multi_cmd); + return ret; +} + +int do_ffu(int nargs, char **argv) +{ + int dev_fd, img_fd; + int ret = -EINVAL; + unsigned int sect_size; + __u8 ext_csd[512]; + __u8 *fw_buf = NULL; + off_t fw_size; + char *device; + unsigned int default_chunk = MMC_IOC_MAX_BYTES; + + assert(nargs == 3 || nargs == 4); + if (nargs == 4) { + default_chunk = strtol(argv[3], NULL, 10); + if (default_chunk > MMC_IOC_MAX_BYTES || default_chunk % 512) { + fprintf(stderr, "Invalid chunk size"); + exit(1); } - fprintf(stderr, "Programming failed! Aborting...\n"); + } + + 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); + } + + fw_size = lseek(img_fd, 0, SEEK_END); + if (fw_size == 0) { + fprintf(stderr, "Wrong firmware size"); goto out; } - 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); + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; } - else { - fprintf(stderr, "FW size and number of sectors written mismatch. Status return %d\n", ret); + + /* Check if FFU is supported by eMMC device */ + if (!ffu_is_supported(ext_csd, device)) { + ret = -ENOTSUP; goto out; } - fprintf(stderr, "Installing firmware on %s...\n", device); - /* Re-enter ffu mode and install the firmware */ - multi_cmd->num_of_cmds = 2; + /* Ensure FW is multiple of native sector size */ + 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); + ret = -EINVAL; + goto out; + } - /* set ext_csd to install mode */ - fill_switch_cmd(&multi_cmd->cmds[1], EXT_CSD_MODE_OPERATION_CODES, - EXT_CSD_FFU_INSTALL); + /* Allocate the firmware buffer with the maximum required size */ + fw_buf = malloc(fw_size); + if (!fw_buf) { + perror("failed to allocate memory"); + ret = -ENOMEM; + goto out; + } - /* send ioctl with multi-cmd */ - ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + /* Read firmware */ + lseek(img_fd, 0, SEEK_SET); + if (read(img_fd, fw_buf, fw_size) != fw_size) { + perror("Could not read the firmware file: "); + ret = -ENOSPC; + goto out; + } - 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[3]); + /* Download firmware bundle */ + ret = do_ffu_download(dev_fd, ext_csd, fw_buf, fw_size, default_chunk); + /* Check programmed sectors */ + if (ret > 0 && (ret * 512) == fw_size) { + fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)fw_size, (intmax_t)fw_size); + } else { + if (ret > 0 && (ret * 512) != fw_size) + fprintf(stderr, "FW size %jd and bytes %d programmed mismatch.\n", + (intmax_t)fw_size, ret * 512); + else + fprintf(stderr, "Firmware bundle download failed with status %d\n", ret); + + ret = -EIO; goto out; } - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + /* + * By spec - check if MODE_OPERATION_CODES is supported in FFU_FEATURES, if not, proceed + * with CMD0/HW Reset/Power cycle to complete the installation + */ + if (!ext_csd[EXT_CSD_FFU_FEATURES]) { + fprintf(stderr, "Please reboot to complete firmware installation on %s\n", device); + ret = 0; goto out; } - /* return status */ - ret = ext_csd[EXT_CSD_FFU_STATUS]; - if (ret) { + fprintf(stderr, "Installing firmware on %s...\n", device); + ret = do_ffu_install(dev_fd, device); + if (ret) fprintf(stderr, "%s: error %d during FFU install:\n", device, ret); - goto out; - } else { + else fprintf(stderr, "FFU finished successfully\n"); - } out: - free(buf); - free(multi_cmd); + if (fw_buf) + free(fw_buf); close(img_fd); close(dev_fd); return ret; From patchwork Wed Oct 23 14:38:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13847177 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01A7D1E2604 for ; Wed, 23 Oct 2024 14:41:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.53 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; cv=pass; b=EoxQmnxCC3WsY5siRu3ldq+AXQLWEzuANQGSIznJEDo1gRYjBopxRPxURs8ZHA5UTx5ATxDssYnFdU0V5gClD2sw0Lih8obxtHcfv8I+1Rm5F2RT9+cRkLxNGhws+Z0Uc6LAGmWIvGzP1wqI9HfA/rkmC9UUn/w38QgxiZi9HJk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; c=relaxed/simple; bh=pOEkrvIZrlmY1ohTqJlokFeiG/QYgPns/4D/kUf+QLU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=t/kbS5mFZoX5IXBKtXlUNud7wWhCBsQOMt5Q+d+lk+J6zXB6rppzyaNl2yS6weDDYYlI1AxSJzjrNiPpyb+6KFOUJOMYn3sL6QZs5MDZbOC3SbSNaMjNz0AQGJndAp20TFz/CongyHixBXS26lZkbrFSTnGsnbbHFsyEd/Qzi58= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de; spf=none smtp.mailfrom=iokpp.de; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=kj+uu/rV; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=Z5+zlVrS; arc=pass smtp.client-ip=85.215.255.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=iokpp.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="kj+uu/rV"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="Z5+zlVrS" ARC-Seal: i=1; a=rsa-sha256; t=1729694329; cv=none; d=strato.com; s=strato-dkim-0002; b=LCxn8BalzfGXVfWWLOjGEDPvIq+1s8SenSNXx8SCAmR1oL22igIFmZwsAfL/C4WXVC 72knNw4tKNmyMMsabKpIeaftTr7z5IHsVyO7Gfb/Xo2eT01IFtMFNV2fGv10SNW3o2Jn mDXumds11Wwzl0FucVoNKlggTl1hhVeXOfKZBWJeh58ZvW6x1HciZ0pidTuQW44OJLUj RXDS/gHM20sfhTKFIg94DZj0/4q2JD7PM5xI4BAl4C7uXbcE19Ta487XFaKcq+oMn3Vi qnboGzRmICvGHSvP9xN1AaJit7RfDEXz3P72ceDI0lyS8wJD19rLtpkt1yjLA1a7YA4n px0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=vzR3H7P+FOprNzNIrZ9JI6UECdvh7ijlmk5vCwjA5Zw=; b=XyR+l4omxEy6mkotdhhWP+dYr/Z1sGZUKLFht3+tk8yILLzzZndaA8QPb+irHXJZLZ IeVz++wCP16+yhp6MOVbEAhwFP+2LTBwBPPU4eQYFxM+wHL5jCW+ZAKfHGEJmITJGsp4 i4fx7YiG/lLBYhqyWyRx79hMlDC1SjsZG/PCsuOroAp/LV9kNh1cNzsYLiypbA4nXILk vO2gbA6ltKdXovGx5jCQf/mfvhuSWbEGeVzLpCuOPoQIlHdZnXchZRxQ7qEwjqU77Xfv QyVykAb2aXTEW5qMGeQML3wc8eOCzmupJKOnXHYQTG3BdSoGz+3eLl7IrErT3rcGwzQD ZD2g== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0002; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=vzR3H7P+FOprNzNIrZ9JI6UECdvh7ijlmk5vCwjA5Zw=; b=kj+uu/rV/c5dBe1KlVRE/ixLgc71sAOW/lIh+u5E22Pr4CONwgq1YGu0xdY8Hwpb2L BfOS2beDZFUeVTUhKH8yJ57HrQLNDnfriYcKCfDbcoL2pePPgIedgdrnl74c9kJdPoQu 84Kv/WsTjOrVdiuUo+JDcqo0jIZWe492mATFr2ozR08Ap2gPMbXpdAI9cN6In0y/vxyI CUL02DdxXvtZxkSPT0wmjgyU4ifXdw0YCLuyKWI3pg4vFhUDPH7exqXXgMYxQtXd/nnz NCulxiVAc0qpBkiCVnNddEs6tsNTeJ6ugCYRlplKFzcsYsvTunzoZGgTZv42S00z9uPY 1USQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729694329; s=strato-dkim-0003; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=vzR3H7P+FOprNzNIrZ9JI6UECdvh7ijlmk5vCwjA5Zw=; b=Z5+zlVrS29eDgCfM4lO93wonW4ZUbg1GeiNYAep2H8Ls1fL4dUZhHA6E64QgIMsLlK GvBnnQd9uN2yuTm/BJAA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O2J2YOom0XQaPis+nU/xK" Received: from Munilab01-lab.micron.com by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209NEcnTm9 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Wed, 23 Oct 2024 16:38:49 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org, vfazio@xes-inc.com Cc: Bean Huo Subject: [PATCH v4 2/5] mmc-utils: Add FFU mode 2 Date: Wed, 23 Oct 2024 16:38:36 +0200 Message-Id: <20241023143839.108572-3-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241023143839.108572-1-beanhuo@iokpp.de> References: <20241023143839.108572-1-beanhuo@iokpp.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bean Huo Added a new FFU mode 2, in this mode, begins with CMD6, followed by repeated CMD23+CMD25 for downloading the firmware bundle. Once the entire firmware image is downloaded, the FFU mode is exited with CMD6, ensuring the download is treated as an atomic operation. Signed-off-by: Bean Huo --- mmc.1 | 5 ++- mmc.c | 9 +++- mmc_cmds.c | 126 +++++++++++++++++++++++++++++++++++++++++------------ mmc_cmds.h | 3 +- 4 files changed, 112 insertions(+), 31 deletions(-) diff --git a/mmc.1 b/mmc.1 index e153557..256852b 100644 --- a/mmc.1 +++ b/mmc.1 @@ -185,13 +185,16 @@ The device path should specify the cid sysfs file directory. Print SCR data from \fIdevice\-path\fR. The device path should specify the scr sysfs file directory. .TP -.BI ffu " " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +.BI ffu1 " " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Run Field Firmware Update with \fIimage\-file\-name\fR on the device. .br [\fIchunk\-bytes\fR] is optional and defaults to its max - 512k. should be in decimal bytes and sector aligned. .br if [\fIchunk\-bytes\fR] is omitted, mmc-utils will try to run ffu using the largest possible chunks: max(image-file, 512k). .TP +.BI ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br diff --git a/mmc.c b/mmc.c index 2c5b9b5..ffc98dc 100644 --- a/mmc.c +++ b/mmc.c @@ -227,13 +227,18 @@ static struct Command commands[] = { "The device path should specify the scr file directory.", NULL }, - { do_ffu, -2, - "ffu", " [chunk-bytes]\n" + { do_ffu1, -2, + "ffu1", " [chunk-bytes]\n" "Run Field Firmware Update with on .\n" "[chunk-bytes] is optional and defaults to its max - 512k. " "should be in decimal bytes and sector aligned.\n", NULL }, + { do_ffu2, -2, + "ffu2", " [chunk-bytes]\n" + "Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion.\n", + NULL + }, { do_erase, -4, "erase", " " " " " " "\n" "Send Erase CMD38 with specific argument to the \n\n" diff --git a/mmc_cmds.c b/mmc_cmds.c index 1a35431..c183a2d 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -2813,23 +2813,40 @@ out: static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_csd, unsigned int bytes, __u8 *buf, - off_t offset) + off_t offset, __u8 ffu_mode) { __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]); - /* send block count */ - set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0, - bytes / 512); - multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; - - /* - * send image chunk: blksz and blocks essentially do not matter, as - * long as the product is fw_size, but some hosts don't handle larger - * blksz well. - */ - set_single_cmd(&multi_cmd->cmds[2], MMC_WRITE_MULTIPLE_BLOCK, 1, - bytes / 512, arg); - mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf + offset); + /* prepare multi_cmd for FFU based on cmd to be used */ + if (ffu_mode == 1) { + /* put device into ffu mode */ + fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); + /* send block count */ + set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0, + bytes / 512); + multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; + + /* + * send image chunk: blksz and blocks essentially do not matter, as + * long as the product is fw_size, but some hosts don't handle larger + * blksz well. + */ + set_single_cmd(&multi_cmd->cmds[2], MMC_WRITE_MULTIPLE_BLOCK, 1, + bytes / 512, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf + offset); + /* return device into normal mode */ + fill_switch_cmd(&multi_cmd->cmds[3], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); + } else if (ffu_mode == 2) { + /* + * FFU mode 2 uses CMD23+CMD25 for repeated downloads and remains in FFU mode + * during FW bundle downloading until completion. In this mode, multi_cmd only + * has 2 sub-commands. + */ + set_single_cmd(&multi_cmd->cmds[0], MMC_SET_BLOCK_COUNT, 0, 0, bytes / 512); + multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; + set_single_cmd(&multi_cmd->cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf + offset); + } } /* @@ -2873,6 +2890,36 @@ static bool ffu_is_supported(__u8 *ext_csd, char *device) return true; } +static int enter_ffu_mode(int dev_fd) +{ + int ret; + struct mmc_ioc_cmd cmd; + + memset(&cmd, 0, sizeof(cmd)); + + fill_switch_cmd(&cmd, EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); + ret = ioctl(dev_fd, MMC_IOC_CMD, &cmd); + if (ret) + perror("enter FFU mode failed!"); + + return ret; +} + +static int exit_ffu_mode(int dev_fd) +{ + int ret; + struct mmc_ioc_cmd cmd; + + memset(&cmd, 0, sizeof(cmd)); + + fill_switch_cmd(&cmd, EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); + ret = ioctl(dev_fd, MMC_IOC_CMD, &cmd); + if (ret) + perror("exit FFU mode failed!"); + + return ret; +} + /* * Performs FFU download of the firmware bundle. * @@ -2886,7 +2933,7 @@ static bool ffu_is_supported(__u8 *ext_csd, char *device) * On failure, returns a negative error number. */ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_size, - unsigned int chunk_size) + unsigned int chunk_size, __u8 ffu_mode) { int ret; __u8 num_of_cmds = 4; @@ -2898,6 +2945,10 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz fprintf(stderr, "unexpected NULL pointer\n"); return -EINVAL; } + + if (ffu_mode != 1) /* in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */ + num_of_cmds = 2; + /* allocate maximum required */ multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + num_of_cmds * sizeof(struct mmc_ioc_cmd)); @@ -2906,14 +2957,15 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -ENOMEM; } - /* prepare multi_cmd for FFU based on cmd to be used */ - /* put device into ffu mode */ - fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, - EXT_CSD_FFU_MODE); - - /* return device into normal mode */ - fill_switch_cmd(&multi_cmd->cmds[3], EXT_CSD_MODE_CONFIG, - EXT_CSD_NORMAL_MODE); + if (ffu_mode != 1) { + /* + * If the device is not in FFU mode 1, the command to enter FFU mode will be sent + * independently, separate from the firmware bundle download command. + */ + ret = enter_ffu_mode(dev_fd); + if (ret) + goto out; + } do_retry: bytes_left = fw_size; @@ -2924,7 +2976,7 @@ do_retry: bytes_per_loop = bytes_left < chunk_size ? bytes_left : chunk_size; /* prepare multi_cmd for FFU based on cmd to be used */ - set_ffu_download_cmd(multi_cmd, ext_csd, bytes_per_loop, fw_buf, off); + set_ffu_download_cmd(multi_cmd, ext_csd, bytes_per_loop, fw_buf, off, ffu_mode); /* send ioctl with multi-cmd, download firmware bundle */ ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); @@ -2935,7 +2987,7 @@ do_retry: * In case multi-cmd ioctl failed before exiting from * ffu mode */ - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); + exit_ffu_mode(dev_fd); goto out; } @@ -2962,6 +3014,16 @@ do_retry: off += bytes_per_loop; } + if (ffu_mode != 1) { + /* + * If the device is not in FFU mode 1, the command to exit FFU mode will be sent + * independently, separate from the firmware bundle download command. + */ + ret = exit_ffu_mode(dev_fd); + if (ret) + goto out; + } + ret = get_ffu_sectors_programmed(dev_fd, ext_csd); out: free(multi_cmd); @@ -3009,7 +3071,7 @@ out: return ret; } -int do_ffu(int nargs, char **argv) +static int __do_ffu(int nargs, char **argv, __u8 ffu_mode) { int dev_fd, img_fd; int ret = -EINVAL; @@ -3085,7 +3147,7 @@ int do_ffu(int nargs, char **argv) } /* Download firmware bundle */ - ret = do_ffu_download(dev_fd, ext_csd, fw_buf, fw_size, default_chunk); + ret = do_ffu_download(dev_fd, ext_csd, fw_buf, fw_size, default_chunk, ffu_mode); /* Check programmed sectors */ if (ret > 0 && (ret * 512) == fw_size) { fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)fw_size, (intmax_t)fw_size); @@ -3125,6 +3187,16 @@ out: return ret; } +int do_ffu1(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, 1); +} + +int do_ffu2(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, 2); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 5f2bef1..e2eba91 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -41,7 +41,8 @@ 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); +int do_ffu1(int nargs, char **argv); +int do_ffu2(int nargs, char **argv); int do_read_scr(int argc, char **argv); int do_read_cid(int argc, char **argv); int do_read_csd(int argc, char **argv); From patchwork Wed Oct 23 14:38:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13847175 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC0F71E22FC for ; Wed, 23 Oct 2024 14:41:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; cv=pass; b=cMizAnXmZQmjD1B2UXc5JdQY3u2xHd5ro/j13bRjQQM3q+/7uTS3+A9ZUz7rLb2buJ2kfQWiuKbgeDtZ7oQlr20YEPcSa0g1D+FnfQizpnKnlFV8Ipwn7rCCeJXimnteik00ausqiqYLqJDXS5vf5rU0YTwlVy+kRAJipObeH5s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694518; c=relaxed/simple; bh=0A8k9TWam9yB/uf9TNe+kd6pqtKxOQyEhwAXy1k8/1s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=DL0J869LfAumBJBVW3ldbJxBkUVW+0G44LO3L2iEfy+ygOHvnKyPcoQw7FUGCjD/x8FMweyztApUob4Wq/zZIkF5Qft5xUjoEskKrTwX+zRX7G1nDVGWDwbVzQF0sO00EwkSrsrFgmDwPAxDKvWF2rFhMHYMa+Sub8e1QA3pboo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de; spf=none smtp.mailfrom=iokpp.de; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=GGn+SSZM; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=0qRlk2q/; arc=pass smtp.client-ip=85.215.255.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=iokpp.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="GGn+SSZM"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="0qRlk2q/" ARC-Seal: i=1; a=rsa-sha256; t=1729694330; cv=none; d=strato.com; s=strato-dkim-0002; b=gkbQl8tbNBfyWYmkZrZvTaPeQl1Z7698WOmRlF7mLsw5B12nTqUCmRwjQyTtIgCOMr jXgJf6a07TxeCyW2ITlcPKgX6Nw2Pj6ddWDeEAGFLLsV1S7uA161qfnM2B7LudI9zpfi lw2+5Gggc6i0urJGOqqzO0wWD1uTtiv4XHW5LFMRvbrors1802dm5Ri0X7nSjWgcG88S NGEScENVpnHPWo3pDR1QVzm6s2LO7CUImfJbMlvcmuST3fzODHhSQEwTB6y9wWiHWuva 6W9FisYtUlS3kK9GYKAAl3yV51bCeW5sazxBrAGy0d3kzqxmKGIyXIJitVnh7y40lFXC //xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=4UMj/6L75msqfl4ZuzDIdWw+0IgWgStcnjxHHsqRRO4=; b=IfzW2n9KvOCm0FFCnzPSJGpYsHF9ygV7H1Y3A+bfWUJFIUgThH+55p6RoKKt72rSQq 57K/onRcATF5Gb8cxf8J8Jr3t/3TXPiWChhvgZkHSYmRkl0R31gK2VXsrBOUzDPd/HZh 6g+rnrSe8GJlLXimqQ+g9k36yJ2x7ZEAV+SWLLUmYQ1F5QxJFGhUs5G1rKLQyhKkhRC+ Pp2DOuDk7dH3+69556JiEPBmtwh0kjDQpyTp3feyVOhV2HN2SRRLfPjcPZyVloxJ+MS2 UmzlbeTBrtazgJJehpbKeW2Bsqq1sKtJqUe1evrISvhM1WXiLaQ2lnUF7RV1PYZQTV3b MlGg== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=4UMj/6L75msqfl4ZuzDIdWw+0IgWgStcnjxHHsqRRO4=; b=GGn+SSZMdCHYQgpkptvKk5HcxTRn3a5kzGXD+Myn66H7CjalQ0F1LEC2fg9pUMmQes nAFSXDUDQAL2QLhyUgfjP381GTsh3nDU/p71wRvxvfYSQcrZtws1Wtmqty79vNWlWqQx LyUYVrDsioG3fnBvdmW8rcz7PLHJY0u/BpEyUwTwiMVPFGwcxzSsNkWgdmV2WWKPpkZD WfOEdz7Rz9+TwJ7E3oZGZefgqiRTH3I23Jb4Nz40q1iocCIaFu50q6s8Gs4yP/Jv2hSw GuyBbZMIg8GzFFd+JSIo6ikb3SzrSTHPBoWuJq+run8ns23dwsWyFryJwCBdN/NUMAtw 6fkQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0003; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=4UMj/6L75msqfl4ZuzDIdWw+0IgWgStcnjxHHsqRRO4=; b=0qRlk2q/oU2At0n2eAn+SpkEhvAh9mOEQiuQ7NFXxoYO+d6SusBbSM4NUrARkDQEDM uu+AVE1lmI6DEck2HpAw== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O2J2YOom0XQaPis+nU/xK" Received: from Munilab01-lab.micron.com by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209NEcnTmA (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Wed, 23 Oct 2024 16:38:49 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org, vfazio@xes-inc.com Cc: Bean Huo Subject: [PATCH v4 3/5] mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write download FW Date: Wed, 23 Oct 2024 16:38:37 +0200 Message-Id: <20241023143839.108572-4-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241023143839.108572-1-beanhuo@iokpp.de> References: <20241023143839.108572-1-beanhuo@iokpp.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bean Huo Introduced a new FFU mode 3 of leveraging CMD25+CMD12 for Open-ended Multiple-block write to download the firmware bundle. In this mmode, the device remains in FFU mode during firmware download until the downloading is completed. Signed-off-by: Bean Huo Acked-by: Avri Altman --- mmc.1 | 3 +++ mmc.c | 5 +++++ mmc.h | 1 + mmc_cmds.c | 11 +++++++++++ mmc_cmds.h | 1 + 5 files changed, 21 insertions(+) diff --git a/mmc.1 b/mmc.1 index 256852b..220427b 100644 --- a/mmc.1 +++ b/mmc.1 @@ -195,6 +195,9 @@ if [\fIchunk\-bytes\fR] is omitted, mmc-utils will try to run ffu using the larg .BI ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion. .TP +.BI ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu1', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br diff --git a/mmc.c b/mmc.c index ffc98dc..e013fc6 100644 --- a/mmc.c +++ b/mmc.c @@ -239,6 +239,11 @@ static struct Command commands[] = { "Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion.\n", NULL }, + { do_ffu3, -2, + "ffu3", " [chunk-bytes]\n" + "Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n", + NULL + }, { do_erase, -4, "erase", " " " " " " "\n" "Send Erase CMD38 with specific argument to the \n\n" diff --git a/mmc.h b/mmc.h index 6f1bf3e..14dac38 100644 --- a/mmc.h +++ b/mmc.h @@ -27,6 +27,7 @@ #define MMC_BOOT_INITIATION_ARG 0xFFFFFFFA #define MMC_SWITCH 6 /* ac [31:0] See below R1b */ #define MMC_SEND_EXT_CSD 8 /* adtc R1 */ +#define MMC_STOP_TRANSMISSION 12 /* ac R1b */ #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */ #define R1_SWITCH_ERROR (1 << 7) /* sx, c */ #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */ diff --git a/mmc_cmds.c b/mmc_cmds.c index c183a2d..4bec56d 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -2846,6 +2846,12 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; set_single_cmd(&multi_cmd->cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg); mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf + offset); + } else if (ffu_mode == 3) { + set_single_cmd(&multi_cmd->cmds[0], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg); + multi_cmd->cmds[0].flags = MMC_RSP_R1 | MMC_CMD_ADTC; + mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset); + set_single_cmd(&multi_cmd->cmds[1], MMC_STOP_TRANSMISSION, 0, 0, 0); + multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; } } @@ -3197,6 +3203,11 @@ int do_ffu2(int nargs, char **argv) return __do_ffu(nargs, argv, 2); } +int do_ffu3(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, 3); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index e2eba91..413bc85 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -43,6 +43,7 @@ int do_cache_en(int nargs, char **argv); int do_cache_dis(int nargs, char **argv); int do_ffu1(int nargs, char **argv); int do_ffu2(int nargs, char **argv); +int do_ffu3(int nargs, char **argv); int do_read_scr(int argc, char **argv); int do_read_cid(int argc, char **argv); int do_read_csd(int argc, char **argv); From patchwork Wed Oct 23 14:38:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13847182 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [85.215.255.84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 632691FEFB3 for ; Wed, 23 Oct 2024 14:44:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694698; cv=pass; b=gNtjz4CRNvi6gGI8hwXcRMzRhfMNIu3TTkJI4qolXPEFKzptOBbSrc6P5EQVL9188NyZsDl+7DsdFYMHzMyyGVaEzjTciPbGPEGlmVSHyXAHYCQV9Sn9kqstk57kVSe0n+iQO/X1KsfnlXIe7v2TGI01U40om5NJi6QjmMOenWo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694698; c=relaxed/simple; bh=shhSIsRVGjC3jIHT+UAyXdmQ0iGV4spNHE0S6qbgvNA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=QrPmRv0DMWLUbV/nyFkFQra/jYeFp5SBqeO0rTu729gvHUkkUoHl+NYxpBigvsE75BJVRzhdwilg3E+vytbozVt/0yWtDk4EyC++VqUFJAiuwu30FYXqDWokW0Z+Rf/0a5YgBOvJwgNP82mpnohMmY14QcrGT9seQuV9pQUbx5M= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de; spf=none smtp.mailfrom=iokpp.de; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=r4wr/TQj; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=ZDmP1t0K; arc=pass smtp.client-ip=85.215.255.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=iokpp.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="r4wr/TQj"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="ZDmP1t0K" ARC-Seal: i=1; a=rsa-sha256; t=1729694330; cv=none; d=strato.com; s=strato-dkim-0002; b=YnR6hTkWFkLGXSmN8GkQWs6J/lN9JB8f7wpcVZT72Wv0pxfT6TMNxmbnlMIVvvFxWA 5+GiglpgHVXL1RB8ZMaXmCO3liIXom7b09mbc3o1ZTZRNKgPRTz9zGeYTMa8dAZg8+BF k4vazdXoOQXxmQWZlHJtZ2EMktBc/aB6faCVZrt2shaecm6wyTiFubnNIFAK3WoaI4ik 4xX5EiRYTor6vJWslp/ihSVol6j35hq8Czumb2sr+8Er+XdO028vsr7X3XivNG/iGb0L HaNnYzYlGJr17t7/VeX8HXwcSJ62DU5zsnzgxrO964f3R0bC1te5zd8ASSBnKs468Yll 5l7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=HOfLSgPXwF+te9GVD5VBXThzLDka1ZwQ3T0UbMa7rR4=; b=Kv0rST8H3fhfHZiq1bUp7Mh7KX3clWqgr51Bj41SUusD6tZ92gjMlfYIqE07t/JX/v SeqG4UBEVZPUYkoAg07rLqpRd9B749Av6D5YbeAgSv40pGJXVPNt2yVa3kmMfaDzixOS jgX7U1EknXRbd675gzHQ/VYkacfy8XlMf9hEuJC8GfK7ijNkUKXofSWazD6TW6b+MBxM HajV7dOMdpEEspqlPfcAOfJ1necXSqgVnXTpGeJ0khg3uBAnIUQmJch0RTmUGnLbta3v 51QhZeYndsJDxcbmm1xW+DEHDFMhnPcE6DKp/QlFEObYL0w1vFKvUwETVSN3MNzgJQQC 1X4A== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=HOfLSgPXwF+te9GVD5VBXThzLDka1ZwQ3T0UbMa7rR4=; b=r4wr/TQjDLnEnFqU4zq+JWEmjpheZHM6idUzTDT3C5KcpWtCqHcKJ+5q2lqeZgfl+o tdIqt0eulF+6BTwf43ru79ytYl33CaxWwXc/7Et4LIYpyZxa+hyOMKysISq7vwsXKamz kZDN/wa9gbYtkze/op2ywMrt3PrNC30ztuZcIZW5V8Y5MW8RKmJZfCGg1YWhLh4znncg gvcKX8dS+MZat2Y3HAVrkjQLyYmEPFX9m0+YbqgnNDVTCpeLWeb9dSBbKW5RUmm3Ctpp 3Qm+Xl70A5qPz7qYulo1nZdo1lHv1CpXpZuad0+kLchHDbV7NdbDo8hI8xfhuqmiX47i jjTQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0003; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=HOfLSgPXwF+te9GVD5VBXThzLDka1ZwQ3T0UbMa7rR4=; b=ZDmP1t0Kb5UlyokgbQVhWLWFICQsNkqiSP3FM2v2ARXEIWIWLRjp6KY9PPupyQ41xr 1idedk6iUfy6MxvzEaDA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O2J2YOom0XQaPis+nU/xK" Received: from Munilab01-lab.micron.com by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209NEcoTmB (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Wed, 23 Oct 2024 16:38:50 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org, vfazio@xes-inc.com Cc: Bean Huo Subject: [PATCH v4 4/5] mmc-utils: Add FFU mode 4 that uses CMD6 and CMD24 single-block write to download firmware Date: Wed, 23 Oct 2024 16:38:38 +0200 Message-Id: <20241023143839.108572-5-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241023143839.108572-1-beanhuo@iokpp.de> References: <20241023143839.108572-1-beanhuo@iokpp.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bean Huo FFU mode 4 initiates firmware download with CMD6 to enter FFU mode, followed by CMD24 for single-block write, after each CMD24 single-block write, CMD6 is issued to exit FFU mode. Which means in this mode, during FW downloading, the device will not remain FFU mode. Signed-off-by: Bean Huo Acked-by: Avri Altman --- mmc.1 | 3 +++ mmc.c | 7 ++++++- mmc_cmds.c | 25 ++++++++++++++++++++----- mmc_cmds.h | 1 + 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mmc.1 b/mmc.1 index 220427b..273bdcc 100644 --- a/mmc.1 +++ b/mmc.1 @@ -198,6 +198,9 @@ Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU m .BI ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Same as 'ffu1', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. .TP +.BI ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br diff --git a/mmc.c b/mmc.c index e013fc6..a6474db 100644 --- a/mmc.c +++ b/mmc.c @@ -241,7 +241,12 @@ static struct Command commands[] = { }, { do_ffu3, -2, "ffu3", " [chunk-bytes]\n" - "Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n", + "Same as 'ffu1', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n", + NULL + }, + { do_ffu4, -2, + "ffu4", " [chunk-bytes]\n" + "Same as 'ffu1', but uses CMD24 Single-block write to download, exiting FFU mode after each block written.\n", NULL }, { do_erase, -4, diff --git a/mmc_cmds.c b/mmc_cmds.c index 4bec56d..c9db4b4 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -2852,6 +2852,11 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset); set_single_cmd(&multi_cmd->cmds[1], MMC_STOP_TRANSMISSION, 0, 0, 0); multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; + } else if (ffu_mode == 4) { + fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); + set_single_cmd(&multi_cmd->cmds[1], MMC_WRITE_BLOCK, 1, 1, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf + offset); + fill_switch_cmd(&multi_cmd->cmds[2], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); } } @@ -2952,8 +2957,13 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -EINVAL; } - if (ffu_mode != 1) /* in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */ + if (ffu_mode == 2 || ffu_mode == 3) { + /* in FFU mode 2, 3, mmc_ioc_multi_cmd contains 2 commands */ num_of_cmds = 2; + } else if (ffu_mode == 4) { + num_of_cmds = 3; /* in FFU mode 4, mmc_ioc_multi_cmd contains 3 commands */ + chunk_size = 512; /* FFU mode 4 uses CMD24 single-block write */ + } /* allocate maximum required */ multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + @@ -2963,9 +2973,9 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -ENOMEM; } - if (ffu_mode != 1) { + if (ffu_mode == 2 || ffu_mode == 3) { /* - * If the device is not in FFU mode 1, the command to enter FFU mode will be sent + * In FFU mode 2, mode 3, the command to enter FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = enter_ffu_mode(dev_fd); @@ -3020,9 +3030,9 @@ do_retry: off += bytes_per_loop; } - if (ffu_mode != 1) { + if (ffu_mode == 2 || ffu_mode == 3) { /* - * If the device is not in FFU mode 1, the command to exit FFU mode will be sent + * In FFU mode 2, FFU mode 3, the command to exit FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = exit_ffu_mode(dev_fd); @@ -3208,6 +3218,11 @@ int do_ffu3(int nargs, char **argv) return __do_ffu(nargs, argv, 3); } +int do_ffu4(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, 4); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 413bc85..a382349 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -44,6 +44,7 @@ int do_cache_dis(int nargs, char **argv); int do_ffu1(int nargs, char **argv); int do_ffu2(int nargs, char **argv); int do_ffu3(int nargs, char **argv); +int do_ffu4(int nargs, char **argv); int do_read_scr(int argc, char **argv); int do_read_cid(int argc, char **argv); int do_read_csd(int argc, char **argv); From patchwork Wed Oct 23 14:38:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13847183 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [85.215.255.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CF8F1FE100 for ; Wed, 23 Oct 2024 14:44:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694698; cv=pass; b=ko7KzeIICjTt7g/1v3iNIvu2rrNzkiUqBGPK+bV3CaSPHOQ0f6ooo5kFtT9vt2VbQewvaXsZa5ENhynRYGGcZhkOXYORDhL0HEHEPG/O20HTNIfR4x5MHWCnFIesQ5p95QmY6FAnPzUhvrrbiWEdJTjUREnr1mil2XUv3ume0EQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729694698; c=relaxed/simple; bh=fVW7IZTwIyBE9MTvqTUTdHtyGoxR7vh3uxjDgnaUJqs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=myzmTUJSYo33zgQN+6jXzRDzqNa6rfoGvZqbHmTRlOaBSYGVHJDUSWT8jIlgdMt4eFj7qYnsOP4ibPPYQ4jNkTsp5CsIyAVu7r+dEt4kURKT05UaLd+dqhi2LPtV0f2QSSgiFVsYp65qFvWrZrwK23wQRfwB3K+YkbSTLXqoCIs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de; spf=none smtp.mailfrom=iokpp.de; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=KUE1K3uQ; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=Gu0kH7My; arc=pass smtp.client-ip=85.215.255.82 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iokpp.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=iokpp.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="KUE1K3uQ"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="Gu0kH7My" ARC-Seal: i=1; a=rsa-sha256; t=1729694330; cv=none; d=strato.com; s=strato-dkim-0002; b=rdWb8LsGN1YvyOrREwGuCRBatBIZiqMkvCqGQHvE+2fPuDkwu/ROfaOjwUGHDKdZ45 7AGBNN0/Xfpjpg8k6IJnyeN0V8QpmKWy0sowr9YuToUolHPj/m+3z04Y+mYT/76mTyKd 7i5KNMI5xc3UUs7Df00acCdlFQrar5piH7ZebQqVQrxMWQdWFnaPRfDfTll5DtPQUS4O X6m7PDjdSWq/1iKXC4ZA4+5Z4LD5Lsq45CvwrtRemuOlwTrTt3BLoGJBUl7MsIAgjkGA go0WnD9VgFfvG7gbcjHGTzgxEV9tTLXlGIa7URVyxYOsXHzynaatEMgvQC3xJMmOHC59 +TQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=p5d81eT5j/qaZCSFlS0xBK0MfycK2mpIH8fZZtgB+hk=; b=rRuVWDEOw6hs4ZoaRIiBvzuJp2k5L4v2HtglEhCwY13ClIRE/49QG30ngGxjooJb7T Tqa++mwGsgSFeYJp2rXa+2k1DVWS6/6iP8M9BPFt5b1UAFzCRag9ejjmaTQ+DhRrfZui SfzHe7ZbXL3w3RGtHDk7+vKYYF8CKEBLP4fsbJXKnMxNu0c06PzTEGrGDMgpuVB+myf6 SyLfVs7w98rlwCLYzOhFBBANMR39lR20LFb979RFW1Am32roFJM7Szvv44Li1WJ+CCRv cUG1o6srgk84MFa3tSTzwbj4ZFB/cRZbYKIdiouO22HN/nZrSFyXiOMFwr9E6LaOKsr1 ojRw== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0002; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=p5d81eT5j/qaZCSFlS0xBK0MfycK2mpIH8fZZtgB+hk=; b=KUE1K3uQV9M9H0SbUzQ7O6LXwlDDRiKjA7nYWJgJvpmAFggzaw/KWCfQA4vYyE2RYF 08aFO73HgD5F8aqfvBSnSgWuARhAY8x6AVZ4JKGycDAROUo8nYw9NlQLA00229f1BrJt u82PJUgoagSvee22vwzCNH3t0md5e+QoGODDESKTKrqeVzn/j2dbeIWXQAcPEF49xjTe PfU58LZtrxXHa8aaCgczmEQW/fS3C44gIp3AnQRhxh0t5HlsOeiRl1B4nEI4y0OzZW06 Hx2YriNrsDsBpOhJD6l1qqug6mtMWCH5ZCwp0iJAQs6aXi9/2iobMYNaPnpcfJv5xeJG mV2g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729694330; s=strato-dkim-0003; d=iokpp.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=p5d81eT5j/qaZCSFlS0xBK0MfycK2mpIH8fZZtgB+hk=; b=Gu0kH7MySBxPx5NUxNlbNmIoB5JK+Ov7OMccgTeAKHdPPi/xj1or07VAeIlelLCxnO gNox9McREPMV2JGJcYDg== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O2J2YOom0XQaPis+nU/xK" Received: from Munilab01-lab.micron.com by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209NEcoTmC (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Wed, 23 Oct 2024 16:38:50 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org, vfazio@xes-inc.com Cc: Bean Huo Subject: [PATCH v4 5/5] mmc-utils: Add FFU mode 5 for firmware download using repeated CMD24 single-block write command Date: Wed, 23 Oct 2024 16:38:39 +0200 Message-Id: <20241023143839.108572-6-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241023143839.108572-1-beanhuo@iokpp.de> References: <20241023143839.108572-1-beanhuo@iokpp.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bean Huo Add FFU mode 5 which enters FFU mode with CMD6, followed by repeated CMD24 commands to perform single-block writes for the firmware download. After downloading all firmware data, CMD6 is issued to exit FFU mode. Signed-off-by: Bean Huo Acked-by: Avri Altman --- mmc.1 | 5 ++++- mmc.c | 5 +++++ mmc_cmds.c | 28 +++++++++++++++++++++------- mmc_cmds.h | 1 + 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/mmc.1 b/mmc.1 index 273bdcc..052d2d1 100644 --- a/mmc.1 +++ b/mmc.1 @@ -199,7 +199,10 @@ Same as 'ffu1', but uses CMD23+CMD25 for repeated downloads and remains in FFU m Same as 'ffu1', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. .TP .BI ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] -Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written. +Same as 'ffu1', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written. +.TP +.BI ffu5 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu1', but uses CMD24 Single-block write for repeated downloads, remaining in FFU mode until completion. .TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. diff --git a/mmc.c b/mmc.c index a6474db..928003e 100644 --- a/mmc.c +++ b/mmc.c @@ -249,6 +249,11 @@ static struct Command commands[] = { "Same as 'ffu1', but uses CMD24 Single-block write to download, exiting FFU mode after each block written.\n", NULL }, + { do_ffu5, -2, + "ffu5", " [chunk-bytes]\n" + "Same as 'ffu1', but uses CMD24 Single-block write for repeated downloads, remaining in FFU mode until completion.\n", + NULL + }, { do_erase, -4, "erase", " " " " " " "\n" "Send Erase CMD38 with specific argument to the \n\n" diff --git a/mmc_cmds.c b/mmc_cmds.c index c9db4b4..024f9d7 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -2857,6 +2857,9 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, set_single_cmd(&multi_cmd->cmds[1], MMC_WRITE_BLOCK, 1, 1, arg); mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf + offset); fill_switch_cmd(&multi_cmd->cmds[2], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_MODE); + } else if (ffu_mode == 5) { + set_single_cmd(&multi_cmd->cmds[0], MMC_WRITE_BLOCK, 1, 1, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset); } } @@ -2963,6 +2966,9 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz } else if (ffu_mode == 4) { num_of_cmds = 3; /* in FFU mode 4, mmc_ioc_multi_cmd contains 3 commands */ chunk_size = 512; /* FFU mode 4 uses CMD24 single-block write */ + } else if (ffu_mode == 5) { + num_of_cmds = 1; /* FFU mode 5, it is single command mode */ + chunk_size = 512; /* FFU mode 5 uses CMD24 single-block write */ } /* allocate maximum required */ @@ -2973,9 +2979,9 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -ENOMEM; } - if (ffu_mode == 2 || ffu_mode == 3) { + if (ffu_mode == 2 || ffu_mode == 3 || ffu_mode == 5) { /* - * In FFU mode 2, mode 3, the command to enter FFU mode will be sent + * In FFU mode 2, mode 3, mode 5, the command to enter FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = enter_ffu_mode(dev_fd); @@ -2994,11 +3000,14 @@ do_retry: /* prepare multi_cmd for FFU based on cmd to be used */ set_ffu_download_cmd(multi_cmd, ext_csd, bytes_per_loop, fw_buf, off, ffu_mode); - /* send ioctl with multi-cmd, download firmware bundle */ - ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + if (num_of_cmds > 1) + /* send ioctl with multi-cmd, download firmware bundle */ + ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + else + ret = ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[0]); if (ret) { - perror("Multi-cmd ioctl"); + perror("ioctl failed"); /* * In case multi-cmd ioctl failed before exiting from * ffu mode @@ -3030,9 +3039,9 @@ do_retry: off += bytes_per_loop; } - if (ffu_mode == 2 || ffu_mode == 3) { + if (ffu_mode == 2 || ffu_mode == 3 || ffu_mode == 5) { /* - * In FFU mode 2, FFU mode 3, the command to exit FFU mode will be sent + * In FFU mode 2, FFU mode 3, FFU mode 5, the command to exit FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = exit_ffu_mode(dev_fd); @@ -3223,6 +3232,11 @@ int do_ffu4(int nargs, char **argv) return __do_ffu(nargs, argv, 4); } +int do_ffu5(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, 5); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index a382349..3cb7480 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -45,6 +45,7 @@ int do_ffu1(int nargs, char **argv); int do_ffu2(int nargs, char **argv); int do_ffu3(int nargs, char **argv); int do_ffu4(int nargs, char **argv); +int do_ffu5(int nargs, char **argv); int do_read_scr(int argc, char **argv); int do_read_cid(int argc, char **argv); int do_read_csd(int argc, char **argv);