From patchwork Fri Oct 25 20:34:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 838607 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [81.169.146.169]) (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 C8110215C64 for ; Fri, 25 Oct 2024 20:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=81.169.146.169 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888693; cv=pass; b=a7LH71ddySxq1gBKtmI8CJoov7CaytZHPxITECk0BN3Zpp0i+9sMsgddWuE023J+/KM2BOAS0yJD3WJosM7rGu3zy+zl/+keLTTY1UFpM44XxDYBhe5QYMoTjyyeQPL25KkwxRpahw9+5fpDh8ON6Wv9/iIAr6blhKJxswNi7K4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888693; c=relaxed/simple; bh=bFBF1fU/1hCUug9kd0yo6iuELjwChWvS4TF228+hrmc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=st4+yZnf4OGu3xjPNuq5V+dQ8p3VdeF8t8Pql6Ms3M3FfWHLGc9wrgZNM7QNBvYIumuyHZ1RDBH5ox2DHw++SnygbzWqRn1TzKxpzCZDC2mCSUR3fd9LaBDtSp5nLcjslpZZffamAFWmIr2wvGn/Q7gic4Ew/HlPdcS7yRbnJDE= 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=Bks4MXQL; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=oZ0Uw09N; arc=pass smtp.client-ip=81.169.146.169 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="Bks4MXQL"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="oZ0Uw09N" ARC-Seal: i=1; a=rsa-sha256; t=1729888502; cv=none; d=strato.com; s=strato-dkim-0002; b=LkcxEyBM4hMrwgPsAZq6BgcGNnWXlGIKDKlnmOz5V2RQgrEfkg8iOKSKa84rRUADta /xO9vXkqk8ainu1kmxiVwGiCGLpn0NJV7xFY9wGSLohkANtYhED7Psjw4dokKkuMX0c8 42VNmi2qvIrf6e6/MVJT7v7ugQ8kYKDonpnEKPr2kuE4aAp8rHHVUPZdCrcwyBqAMGw+ a92/wfKSB+T2QztwIuwvEyMJ09Yd/98LayNia1iHhGvu8GBtA2NkfYzIy1db50TtYazK Jud5ssYPqjNaeCGu7Laj1mkZKU1ogAylTDNekUcqn2/EpFs4NLSFiQ7nYPBiLyCl/8Ai l++w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729888502; 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=WvIS4Xxg3qYlGSKWxRuhtvIUDd3XKzidoEvOVIJIlnkESmjqJPIwuHuPwuND9v1qKY 0cDJZogPjB83CVKomZwQr9RKTVV4aIbYt7mcbRK6HczKRdKXIcBYe2Wkz3baRzrJg1xc Vn6hMOIbsuat8iMPPgD/CiCFY3ANEktqYZu9w7vlL6Sw+QqYoFDFbwzbOAjJyQPn2B14 8ceoGyptQNp2ySbfsRA+elvQT2zCrwo3m9R2TV1lBH2YyOnCdU3m0zmUbagAFMlchGjJ G4fx+TU6+ty85dImfKtF6hki3/W1/vbbYPVpOCIUajkM0cvxnc3gfs/AGS40KjnmuT6O ph9w== 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=1729888502; 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=Bks4MXQLq8Tsp4EZic9esmY4rCJrQiSrbrT2/jhs3cBrUBT4g5RN5NcxxpVKNOkPPm qLem81yLGG/2tLBTHVG6Np4F0dgxE+Nje5TY0++pPvdVFyChWgeCB5FMxdj306fKlVMm KNfZ0os50S9AREvM7wglek6od4gX7yO5Vyhtds1HeKAKDhMknX+mqcUKBPNRIj4XLcs3 VBl9VuDCU495GrH7pVlGmHNZafW86kzlfA0l63QLWPGshU71JGcMe76vboB7vMWlgVus ure+TzeE5IU5v/zD3N2IM3WE3VHz7KAWQsa07HF1g0UXE7wGu8GFVdjSuxDLIlwFhEDe yI9g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729888502; 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=oZ0Uw09NP3N38/DRVO449Eatwi7v7hbqRsSP4IZajawXIbVgPQn8QFNZlR0fH76PlM qH9+1UohkVgAC0oIfYCQ== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD+6IXGo7S6YVZbGYm2ZHbmjWFWcm6Nbb9lHw==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209PKZ2bfd (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 25 Oct 2024 22:35:02 +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 v6 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes Date: Fri, 25 Oct 2024 22:34:50 +0200 Message-Id: <20241025203454.162710-2-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025203454.162710-1-beanhuo@iokpp.de> References: <20241025203454.162710-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 Fri Oct 25 20:34:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 839526 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [85.215.255.80]) (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 C8175215C65 for ; Fri, 25 Oct 2024 20:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.80 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888692; cv=pass; b=HNnE7zb7j2OUhU4G5GThgF6MvbMBfv8IP1TRGWVejQsK0iv7NEaw2C2sHrtcycZ5NlxF7vQtHGplzOQQwXqdeBpfn9ErKtbL4N7UqScdMxWngbgCEq0SIUz+4HDPFo2TCUuGQr4TvqG0XG4Tc9TwRz5++svxvKeqqPDpz4x2N2I= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888692; c=relaxed/simple; bh=MzQ2o0AyOM7uIUolx8vUHiqsKCHmSi3a5BdH2Meh/a8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=nuLaZ/Hah3S++URTveCIBIYIZ3cM41qj+uQqT42k+A47SKdAqSHFJegwYXIzoprd0HQ8vIIFJwFobMzmC/IT+5Iim6wgMnhU2p8+0bqE+iLtc3zhOTUzgt5XUdUKl6mwgmB/XFqMsZbJuHFzmpIZ8fijVlo11mxA70snU6mxMds= 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=oJFSLXNC; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=mHDPgt63; arc=pass smtp.client-ip=85.215.255.80 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="oJFSLXNC"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="mHDPgt63" ARC-Seal: i=1; a=rsa-sha256; t=1729888502; cv=none; d=strato.com; s=strato-dkim-0002; b=pupdJgMhFDHsYo4kEu/T/56UhEilrjjEu/4vg3rZDUyjFTS8EB4+JLWYCg8wWUsLTh lkLfRtvJZfk/gL98v4R22hGArvjfLCJMpf1wd4EtXSWJVhn6PSOVwl+4dqpq5GHeB0uy OQ65bGKKB5WG4S5UWL/zQDRhzQTywZx6cWp01fOiOK+IV1VZEKSb80iJ3omnqkhwJyY1 q+Tsu9GsgfnVQNUJuul9Vi0CQl81aB/bxpnSUkIuySgGalgmBT38OfmxCXJDTNJYH0A8 51ik/G3ybtjE5WR/gfDjETC2zcj9yfDN0QpDIUMmHPX59yykcAGB+tmlIwK0DEO5H4m0 jiYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729888502; 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=KyF9xri1DVk974O0eO8n43yCGmKk6r9EdSlmmqdziI4=; b=fPbXE4I3PgisgM4T0O2x03lkaLDQuqMr24eX4v749nlNwSzDW5ueIZ6Hhv1gfXG+Db ibfaedOKnflAjaHkBpxGDTdVoABUIi5Y9jC/kUrSz45OQG9t5dzRfjMXCFBMqk6VTKT5 6DHecfFUsNMgg9WrS23l5CP+hNAqYkhWbCMya468naj4oAkKq+tWzrWQ8a15k7fqhfGO cFYmEKB+CtneVwqshYmJ2QMNtnZAGJvuT2Swzxs+XaiIWfdu/Ae+YeE1JFJ2tXab2Zfy sP4ikDm8IHiHWZmHAmtOzArVGZhszTPASdieiAw384jE2l/S0hrn2AjCRMdUVfTm5H9Y khOQ== 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=1729888502; 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=KyF9xri1DVk974O0eO8n43yCGmKk6r9EdSlmmqdziI4=; b=oJFSLXNCK5a5lcZImzec6u9FeGV1Ym6Qoz3Tc800mhnizg3KG/9yK4iyTj12G4MjH2 fQpL+iiJ3TrmB7NA5IVfYYCAACWc1a3VXxYoxwp1Z8d9Qzky8gDH+peEK7E1T8Lgpf8Q 0fo5u6w0VpwvbLs9tAYq7oNuCvUCVlVyKQyOmibFPAVzu8SdHcyw9pL9TAiWIrUqA32O 2VF8kjG31F0DBWYmGrt+e6ALXFH5LNWGwIcvG9KLRUP0i16mhz4rYgQPmFzv7pRWO+uR pJJ14xCcJ5iFGVMkD8Bp/4tydMPtlsbJrxv1Vv9Uzpsf/43UnJNHXBEpo+F0428ilsOg 69EA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729888502; 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=KyF9xri1DVk974O0eO8n43yCGmKk6r9EdSlmmqdziI4=; b=mHDPgt63gDcUQdcVgFpFoJWLJ0uio5eI8yzlvOtsb8VUJ0CCxxOaDMQKYuLGn37LbZ WzgY33+i/TFpiU0Oa/Aw== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD+6IXGo7S6YVZbGYm2ZHbmjWFWcm6Nbb9lHw==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209PKZ2bfe (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 25 Oct 2024 22:35:02 +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 v6 2/5] mmc-utils: Add FFU optional mode 1 Date: Fri, 25 Oct 2024 22:34:51 +0200 Message-Id: <20241025203454.162710-3-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025203454.162710-1-beanhuo@iokpp.de> References: <20241025203454.162710-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 optional FFU mode 1, 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 Acked-by: Avri Altman --- mmc.1 | 3 ++ mmc.c | 5 ++ mmc_cmds.c | 134 ++++++++++++++++++++++++++++++++++++++++++----------- mmc_cmds.h | 1 + 4 files changed, 116 insertions(+), 27 deletions(-) diff --git a/mmc.1 b/mmc.1 index e153557..f69ab8f 100644 --- a/mmc.1 +++ b/mmc.1 @@ -192,6 +192,9 @@ Run Field Firmware Update with \fIimage\-file\-name\fR on the device. .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 opt_ffu1 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Optional FFU mode 1, it's the same as 'ffu', 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..67bd90b 100644 --- a/mmc.c +++ b/mmc.c @@ -234,6 +234,11 @@ static struct Command commands[] = { "should be in decimal bytes and sector aligned.\n", NULL }, + { do_opt_ffu1, -2, + "opt_ffu1", " [chunk-bytes]\n" + "Optional FFU mode 1, it's the same as 'ffu', 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..53a1658 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -59,6 +59,13 @@ #define WPTYPE_PWRON 2 #define WPTYPE_PERM 3 + +// Firmware Update (FFU) download modes +enum ffu_download_mode { + FFU_DEFAULT_MODE, // Default mode: Uses CMD23+CMD25; exits FFU mode after each loop. + FFU_OPT_MODE1 // Optional mode 1: Uses CMD23+CMD25; but stays in FFU mode during download. +}; + static inline __u32 per_byte_htole32(__u8 *arr) { return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24; @@ -2813,23 +2820,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, enum ffu_download_mode 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 == FFU_DEFAULT_MODE) { + /* 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 == FFU_OPT_MODE1) { + /* + * 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 +2897,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. * @@ -2881,12 +2935,13 @@ static bool ffu_is_supported(__u8 *ext_csd, char *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. + * @ffu_mode: FFU mode for firmware download mode * * 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) + unsigned int chunk_size, enum ffu_download_mode ffu_mode) { int ret; __u8 num_of_cmds = 4; @@ -2898,6 +2953,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 != FFU_DEFAULT_MODE) /* in default mode, 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 +2965,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 != FFU_DEFAULT_MODE) { + /* + * 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 +2984,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 +2995,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 +3022,16 @@ do_retry: off += bytes_per_loop; } + if (ffu_mode != FFU_DEFAULT_MODE) { + /* + * 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 +3079,7 @@ out: return ret; } -int do_ffu(int nargs, char **argv) +static int __do_ffu(int nargs, char **argv, enum ffu_download_mode ffu_mode) { int dev_fd, img_fd; int ret = -EINVAL; @@ -3085,7 +3155,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 +3195,16 @@ out: return ret; } +int do_ffu(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, FFU_DEFAULT_MODE); +} + +int do_opt_ffu1(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, FFU_OPT_MODE1); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 5f2bef1..351155b 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -42,6 +42,7 @@ 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_opt_ffu1(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 Fri Oct 25 20:34:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 838606 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [85.215.255.81]) (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 2473221441D for ; Fri, 25 Oct 2024 20:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.81 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888699; cv=pass; b=EstDB14jDMrQuRsLChCd0PxBOIeURmNDDrTdhnEZy37lZyJ5nenNWzRj6brXzMR0XB6qGIaiwh3WpCliUiYQ2hPStHgNGAQU0B+rB0cdP9bNbEiTeoD+jpn+lUHdt1KF5NbYllvamjG+qwzbqA/lgWGMIhjI8Penpm0nBirqOpM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888699; c=relaxed/simple; bh=fp1k/NkCPxwbukQcVqaIbDB9uCBxUDYLknbXvycq82g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WYaOHUukg6xmihjzzaFQKhgvw//pPcAGrymzMvTIhkCi1RdRsWH4MIJOERLHuv2QSxksGxdOBI/e9w40sW8pzkRUI4u5/rK2Ivgs45DtdGAMhjtEjH/aClXdAiCZZdgVKv9xO1gzITbXNgJItDzgBGCai9XYDkWuQUZlUEVd+gw= 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=g98my2sU; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=fAzrewtH; arc=pass smtp.client-ip=85.215.255.81 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="g98my2sU"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="fAzrewtH" ARC-Seal: i=1; a=rsa-sha256; t=1729888503; cv=none; d=strato.com; s=strato-dkim-0002; b=qkTZviqallvraxytc1HSvDEGka3oKTuSifa287b0fXs4+TZl3zrlX6jkYxRtoeoUlP kKOY+9ff7oNJvHHoNc+YsFPj4b/f2qd1szOpjN4yQy5RkUMrAuOXptQlonhcrZLTV+hF FIRs59yUoExttreDXt1D0+BEA2RcSCgmqWBo2i6oDfIYFmeA4v0bu65K/6bWzn9wo8P3 8cRYibPe9Ph75TulX4jFENJgACq2lHR/TxyaNtNzyzHwMQotUlmlBtVD2DLdIgkXepgv 2f9W/8n+zx2HJ/eQobzOk/qb9Z6MKyrFQmldbEQ1r8IE3Ovj5pPMg4eaFa9tzv1TU8yX v1Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729888503; 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=UH0q/lXb3bzTen8WCFIlB7A3eJqaXnA2J1FmsRftutM=; b=auFcfPxumQ/FRGuc14sorlkI5qxgrkOsbmMqLLt+BK9tmbWGv2M4LnBU1LY1fDMMCJ Q/Y+zSNT3OMGTSapfwtNVyxRX/4GZESgcn0uiUkRv5TYsL3OdQtgNuClfvzj0oM00bai qo2UeyXb+Cbpnz2zJKbKxzYe32x6iAf41U8L5nCGeD8iDmzjq7jJokFRcfzSUaQ8YqyJ 8ihNKPW8ikJCwR/iE1F/vJ0EdMml89HkKKdaoyGubey+bGVKYKZNfNNUKhcjZ1j5JJdQ v8iH6rRfD3OWbwjlPQyXv2kGvfXBchOz1ZW1umzqkY3uX/TGElET2lP2a6gk00x8MlKe +csA== 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=1729888503; 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=UH0q/lXb3bzTen8WCFIlB7A3eJqaXnA2J1FmsRftutM=; b=g98my2sUNzfNawrJ/yhmnXSqQAknvCVgp2wVGZSTgHG3HD/CTPSpP/ubhaDmIdSmjP wTkQWdMzcAiF5+dL5ry4Hr2Shgz5/ZB8cj4WJYcg/KLkJzBWqxIItDn91R7lpDN4B005 13S6ewZex92Nlb9JbNrPaKM5sB7eb0oOM6HtlYABrlUjiztYcQS77ZoZd1iNC0D5aep9 aS3fNktko1GO9XTM/ETW5rEje2PlfGT29aHZaJIJ0RAD0Hh/fGU3nVpin9b471m7mVkB 069CdKBjjqsOmABCFFzOXAb+fCgNrx7zyKZaO7lxnCxr+3s4FqSAfqPCXBW3/MZ+6XXj KQgg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729888503; 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=UH0q/lXb3bzTen8WCFIlB7A3eJqaXnA2J1FmsRftutM=; b=fAzrewtHQjieT4px/yOscpsllrsZYS5H/4az8A+P/BZ7UG/0elqhoGFWjXapjooelY 3vAbgoAwPMKNHxDEhsCQ== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD+6IXGo7S6YVZbGYm2ZHbmjWFWcm6Nbb9lHw==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209PKZ2bff (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 25 Oct 2024 22:35:02 +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 v6 3/5] mmc-utils: Add FFU optional mode 2 using CMD25+CMD12 for Open-ended write download FW Date: Fri, 25 Oct 2024 22:34:52 +0200 Message-Id: <20241025203454.162710-4-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025203454.162710-1-beanhuo@iokpp.de> References: <20241025203454.162710-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 2 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 | 14 +++++++++++++- mmc_cmds.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mmc.1 b/mmc.1 index f69ab8f..bc2f1b8 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 opt_ffu1 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Optional FFU mode 1, it's the same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion. .TP +.BI opt_ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Optional FFU mode 2, 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 67bd90b..53a708b 100644 --- a/mmc.c +++ b/mmc.c @@ -239,6 +239,11 @@ static struct Command commands[] = { "Optional FFU mode 1, it's the same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion.\n", NULL }, + { do_opt_ffu2, -2, + "opt_ffu2", " [chunk-bytes]\n" + "Optional FFU mode 2, 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 53a1658..fb04821 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -63,7 +63,8 @@ // Firmware Update (FFU) download modes enum ffu_download_mode { FFU_DEFAULT_MODE, // Default mode: Uses CMD23+CMD25; exits FFU mode after each loop. - FFU_OPT_MODE1 // Optional mode 1: Uses CMD23+CMD25; but stays in FFU mode during download. + FFU_OPT_MODE1, // Optional mode 1: Uses CMD23+CMD25; but stays in FFU mode during download. + FFU_OPT_MODE2 // Optional mode 2: Uses CMD25+CMD12 Open-ended Multiple-block write to download }; static inline __u32 per_byte_htole32(__u8 *arr) @@ -2853,6 +2854,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 == FFU_OPT_MODE2) { + 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; } } @@ -3205,6 +3212,11 @@ int do_opt_ffu1(int nargs, char **argv) return __do_ffu(nargs, argv, FFU_OPT_MODE1); } +int do_opt_ffu2(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, FFU_OPT_MODE2); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 351155b..4098931 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_ffu(int nargs, char **argv); int do_opt_ffu1(int nargs, char **argv); +int do_opt_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 Fri Oct 25 20:34:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 839525 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [85.215.255.81]) (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 A9569214415 for ; Fri, 25 Oct 2024 20:38:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.81 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888697; cv=pass; b=UC7Vqo7K9FPnxyDvn7hejFxH+Bh81aVRnJnsH1ZfQ2Dpo35NwJ8Ry21oLPVTSd4ValHdGVu25qk3qhKHeHUtxHqzE5QLYccsGGyyqyegyCM4DD8ylkj8Bkl3f7CDQF+0soEmJZ0FJ6xPJ4mZgGsMko1ObCxhU82yQYMSe8X1vAQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888697; c=relaxed/simple; bh=3BUn02X0fHs8bKvwX3NuCU9yIk3EnJF4E7rceUcVTcE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=Gdh4E4TPONUKlT6jZLi32KrTgbjVPvrBr256RRiYjt6oCjVKXUFIpjXtkZN5eADDxZ67Wje2ntzHfVRo5HKkPanTMmeBZ7mWpPcjT1KLEuhTTtIX7tK5qnr9sIm1wkOczOKbhD8ItVMzfeo3uhYctilBlgqNoVd+Zy/MWodwIfE= 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=J2EcnWOf; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=qdUqKD2m; arc=pass smtp.client-ip=85.215.255.81 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="J2EcnWOf"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="qdUqKD2m" ARC-Seal: i=1; a=rsa-sha256; t=1729888503; cv=none; d=strato.com; s=strato-dkim-0002; b=OX+89mLGdCpL/NILi8jz11qHmx/zaHAtiVZt7K8NC6vzZTHleW4vPa0evo90ZBQwnD x38zV/CZfeKRV9WB8I/xtwwEwdZAwmnB+1curwRz4M3y2UWNw3urSUh0F8ApaK8gVLmo 4r2dH/k3hi+VG8iJ0SyvlUMXY7RFnFH8LwmAH0evrCFI5+km9lRFN/76MiveRms4hbgv hx8rcZhc6nSqYThwt4SRYeEnZys4csBUwzuHP9bw+QUwo4IEDHblBc4PMyTlZ6JnrvNJ fqek25ntKKHzRYOOn6nXAxBUeZgqSGDRmkWH0/ttKPzjDhUr3qPcDYgcquXjNnB0xb2w ZohQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729888503; 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=W2jTWpp1FTgpkwAASe7cSc1004YrfOqCbSUm0hlMca0=; b=Wl1i85JsySnqscMtKRPcZa2o5+DGITLKGqFaRCzmb3vJi6dF/N8MLD6dAfsqrg2kNO j3dHPXmKPjGeefq7qctiMRx+OzDf/k+Tgpqm2Y9URD8mLym1YKqYx7SagYY/eNPB+hy2 Y7KPiGt6cELWXWMgquK4Cvm3hGq1Rjh1jrSCoSc6IZtt1gyUcW8DD5ge/e50C8p3AGXk 558vSSSlP0fXqLT5oA12179aixG1RVBZ6R36jyD0P0Xf86ZqlhuPqeDqChM9NEshKjfA c33dzQdGxT+l5vQTd/7B6svoKya/cJnGw1QqsQhgiE/ALL5/P0tMjjPRwr6Uzf66eKZw YCZw== 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=1729888503; 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=W2jTWpp1FTgpkwAASe7cSc1004YrfOqCbSUm0hlMca0=; b=J2EcnWOfzm0agZ4of6xVDFYghVx6UMWEzjh7GdH3scv+YOkSGIkuJwuL6iONraGSfj djgDL/rASSDbrOf27HSfwa/D0eykyb3iELRd0qlpzgnOhx0ksu5QVRF8sb+q3or5IoZt oBcF20GxWcqQo6vF9kyP0uTGNtz95IUBEjFa9WBIpv6HRsCYfiuLk6QxqEhK83kXJ65/ 7Buik2H+OiOtzpM/tPsrynwZ9yGDPQZIXqM7Sjkm7hK8IRrfAfOOIG2ae5N/5uIOtfIJ 7geuz6+c9nLJrzJ03DVVCBWUPPGORyc2v9fs/t1tmk0fDc1YfDgFGhplAiNrUlTCxRf7 OhqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729888503; 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=W2jTWpp1FTgpkwAASe7cSc1004YrfOqCbSUm0hlMca0=; b=qdUqKD2mZ2OzL+uLWn0gqwyOSmC4dxcmjSopmfoLQ5YxclWmvY9Ffa60NhFs2IRhoj NdJfmplvJxHziUFS5aBg== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD+6IXGo7S6YVZbGYm2ZHbmjWFWcm6Nbb9lHw==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209PKZ3bfg (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 25 Oct 2024 22:35:03 +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 v6 4/5] mmc-utils: Add FFU optional mode 3 that uses CMD6 and CMD24 single-block write to download firmware Date: Fri, 25 Oct 2024 22:34:53 +0200 Message-Id: <20241025203454.162710-5-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025203454.162710-1-beanhuo@iokpp.de> References: <20241025203454.162710-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 optional mode 3, which 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 | 5 +++++ mmc_cmds.c | 28 ++++++++++++++++++++++------ mmc_cmds.h | 1 + 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/mmc.1 b/mmc.1 index bc2f1b8..acb37c1 100644 --- a/mmc.1 +++ b/mmc.1 @@ -198,6 +198,9 @@ Optional FFU mode 1, it's the same as 'ffu', but uses CMD23+CMD25 for repeated d .BI opt_ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Optional FFU mode 2, uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. .TP +.BI opt_ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Optional FFU mode 3, uses CMD24 Single-block write for downloading, 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 53a708b..d2a4d2a 100644 --- a/mmc.c +++ b/mmc.c @@ -244,6 +244,11 @@ static struct Command commands[] = { "Optional FFU mode 2, uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n", NULL }, + { do_opt_ffu3, -2, + "opt_ffu3", " [chunk-bytes]\n" + "Optional FFU mode 3, uses CMD24 Single-block write for downloading, exiting FFU mode after each block written.\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 fb04821..2553de2 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -64,7 +64,8 @@ enum ffu_download_mode { FFU_DEFAULT_MODE, // Default mode: Uses CMD23+CMD25; exits FFU mode after each loop. FFU_OPT_MODE1, // Optional mode 1: Uses CMD23+CMD25; but stays in FFU mode during download. - FFU_OPT_MODE2 // Optional mode 2: Uses CMD25+CMD12 Open-ended Multiple-block write to download + FFU_OPT_MODE2, // Optional mode 2: Uses CMD25+CMD12 Open-ended Multiple-block write to download + FFU_OPT_MODE3 // Optional mode 3: Uses CMD24 Single-block write to download }; static inline __u32 per_byte_htole32(__u8 *arr) @@ -2860,6 +2861,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 == FFU_OPT_MODE3) { + 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); } } @@ -2961,8 +2967,13 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -EINVAL; } - if (ffu_mode != FFU_DEFAULT_MODE) /* in default mode, mmc_ioc_multi_cmd contains 4 commands */ + if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2) { + /* in FFU_OPT_MODE1 and FFU_OPT_MODE2, mmc_ioc_multi_cmd contains 2 commands */ num_of_cmds = 2; + } else if (ffu_mode == FFU_OPT_MODE3) { + num_of_cmds = 3; /* in FFU_OPT_MODE3, mmc_ioc_multi_cmd contains 3 commands */ + chunk_size = 512; /* FFU_OPT_MODE3 uses CMD24 single-block write */ + } /* allocate maximum required */ multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + @@ -2972,9 +2983,9 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -ENOMEM; } - if (ffu_mode != FFU_DEFAULT_MODE) { + if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2) { /* - * If the device is not in FFU mode 1, the command to enter FFU mode will be sent + * In FFU_OPT_MODE1 and FFU_OPT_MODE2, the command to enter FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = enter_ffu_mode(dev_fd); @@ -3029,9 +3040,9 @@ do_retry: off += bytes_per_loop; } - if (ffu_mode != FFU_DEFAULT_MODE) { + if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2) { /* - * If the device is not in FFU mode 1, the command to exit FFU mode will be sent + * In FFU_OPT_MODE1 and FFU_OPT_MODE2, the command to exit FFU mode will be sent * independently, separate from the firmware bundle download command. */ ret = exit_ffu_mode(dev_fd); @@ -3217,6 +3228,11 @@ int do_opt_ffu2(int nargs, char **argv) return __do_ffu(nargs, argv, FFU_OPT_MODE2); } +int do_opt_ffu3(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, FFU_OPT_MODE3); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 4098931..0fbb2b5 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -44,6 +44,7 @@ int do_cache_dis(int nargs, char **argv); int do_ffu(int nargs, char **argv); int do_opt_ffu1(int nargs, char **argv); int do_opt_ffu2(int nargs, char **argv); +int do_opt_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 Fri Oct 25 20:34:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 838608 Received: from mo4-p02-ob.smtp.rzone.de (mo4-p02-ob.smtp.rzone.de [81.169.146.171]) (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 C80AD1B21B1 for ; Fri, 25 Oct 2024 20:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=81.169.146.171 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888691; cv=pass; b=dJa+c8/cdFPxPLKLD1C3s+rGbRVUHaR3QrHO1pSCKT5INhX5k+3h99Ughoo7s525DJ7Jc6+arnqAEPbFN8OnHhCYoxVH1WsS3AHMw1Z1KTxtXA9Ulkf9LhhRNyRBjiz9p0sKwFnyWwg3/rXRz5EpYKYhKtf8jWvRtg12x7p+PZQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729888691; c=relaxed/simple; bh=6PQ39GKuXfAUME8HbJ0aQj6mKhy6lHTPYVmXvkVFBfM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=eNwVk86TUAd5JNWin17XfqzQ1t1fTVEmdaEOXZYnNp+PxNKi2LlzsnJXPZNoMIKlrntu3+aJkswNscdJWR2c7udW7uOj+v9+d0Revj/SkTMo1PTtcg0zD2pcXz20cMx8WNKRkUgsDeu9mxAwTEui8CgdiAgHPr4FTU9qwectMoI= 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=pcpwkxvu; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=/XtWl6rX; arc=pass smtp.client-ip=81.169.146.171 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="pcpwkxvu"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="/XtWl6rX" ARC-Seal: i=1; a=rsa-sha256; t=1729888503; cv=none; d=strato.com; s=strato-dkim-0002; b=SwHZNPRKLyKRcm85eTK/csiU5SNscSgB56ZQ2PohtOcCgbXqQOf3JzQ+EtL5gi4lEG WavU2JJhVVGf1Ua6i4mPOm68OuiX5i/JUy3CtbHTf1G6R1FjSaboorNlTV2YogkeTIqA ThaESYuUR2hRmN+qU7nuGtBwuOP0pO+/CvnDL0YirZIVxqqZNDoKnLjxm5rgHXFT0itR 6MsfvpR6OHYFhBTM/QeHut5iMa6rT9bSvqqKE8ZQXE2hGtqbzXPoErEP/fkLOvdq8+Ml dj0I+L8YEtLSJ2ogHxwrjtHWH3Swjax7ArgR3rDOOGW6OX2yZ+RXHIC7ctol3wOp1dJm SXqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1729888503; 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=Bjr2GODXtzmbEbFbBTd5R0ilCQZ78NJ3oqWW3iNC0a4=; b=Lm//5i9pfC2a56NAyKHUqgquSDXu8m3QnC7JDMtTWp7G0YciVfHfGu1br+hR1aKbLh LR+3YW85ZJWFF/DBDcjCSE6Q/vx0SDxQVGoe9YU1FdH60B7QLoV2rc+piwk1iQyEyrx9 gA0ik+jrg5mu++PqMRRVd/YnPsRPsyz/QtU3Kb7x8IzGXYMvZemyMDFslyneJvsLi+x4 o07Yu+pDsEP5aew8kAMdRORHM8qET1qI9L5u6pGOulCkH/5OIdKs48M6L6kUwYKuTlTp nbnjdZe3pxx3hyXwKddp1SklRNHaOfCr9/NlIl0JfArOrDwAbODSlZrf8G8d376zoTVn Qm3g== 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=1729888503; 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=Bjr2GODXtzmbEbFbBTd5R0ilCQZ78NJ3oqWW3iNC0a4=; b=pcpwkxvu+Sv60zQTwKIkOaO16K1cZBIxccMOhx7NllsXfuCtZzICVE4HRiw3+xKaS8 1lGDileTKoWGJzdzvi5VyRsex511sjL5bvCaZA49iMg6Jd1AaE3JMX9jNQDljp/nyT1t bNE6FUkEdKdjr156xKAT5jQJr6LY079QHI8pI/xloWGsRzldP32TDuyUt4yr23NrX96C e1axbquIV14xb0ksMJMOJcDDl52pFk3I/k4angoU6vCqh9zu26ETJpRdhNXYaDz8rgmm WD12yq3glNXVG3Hps1atr7oD1gc3hij1VLBx61w7zQ3icSXCerMBkAuvf1pdctX/OGAJ Um9A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1729888503; 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=Bjr2GODXtzmbEbFbBTd5R0ilCQZ78NJ3oqWW3iNC0a4=; b=/XtWl6rX42zYSEVo7LLXU8uigM9If0/E1mh0q9ev+lsnGqzkBwBq8wqX4wqVu0Woeo wFvf6uMIRGCwhqCWObBg== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD+6IXGo7S6YVZbGYm2ZHbmjWFWcm6Nbb9lHw==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.11 AUTH) with ESMTPSA id za0ed209PKZ3bfh (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 25 Oct 2024 22:35:03 +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 v6 5/5] mmc-utils: Add FFU optional mode 4 for firmware download using repeated CMD24 single-block write command Date: Fri, 25 Oct 2024 22:34:54 +0200 Message-Id: <20241025203454.162710-6-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025203454.162710-1-beanhuo@iokpp.de> References: <20241025203454.162710-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 4 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 | 3 +++ mmc.c | 5 +++++ mmc_cmds.c | 36 ++++++++++++++++++++++++++---------- mmc_cmds.h | 1 + 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/mmc.1 b/mmc.1 index acb37c1..543742c 100644 --- a/mmc.1 +++ b/mmc.1 @@ -201,6 +201,9 @@ Optional FFU mode 2, uses CMD25+CMD12 Open-ended Multiple-block write to downloa .BI opt_ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Optional FFU mode 3, uses CMD24 Single-block write for downloading, exiting FFU mode after each block is written. .TP +.BI opt_ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Optional FFU mode 4, 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. .br diff --git a/mmc.c b/mmc.c index d2a4d2a..077e901 100644 --- a/mmc.c +++ b/mmc.c @@ -249,6 +249,11 @@ static struct Command commands[] = { "Optional FFU mode 3, uses CMD24 Single-block write for downloading, exiting FFU mode after each block written.\n", NULL }, + { do_opt_ffu4, -2, + "opt_ffu4", " [chunk-bytes]\n" + "Optional FFU mode 4, 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 2553de2..dbe18ab 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -65,7 +65,8 @@ enum ffu_download_mode { FFU_DEFAULT_MODE, // Default mode: Uses CMD23+CMD25; exits FFU mode after each loop. FFU_OPT_MODE1, // Optional mode 1: Uses CMD23+CMD25; but stays in FFU mode during download. FFU_OPT_MODE2, // Optional mode 2: Uses CMD25+CMD12 Open-ended Multiple-block write to download - FFU_OPT_MODE3 // Optional mode 3: Uses CMD24 Single-block write to download + FFU_OPT_MODE3, // Optional mode 3: Uses CMD24 Single-block write to download + FFU_OPT_MODE4 // Optional mode 4: Uses CMD24 Single-block write to download, but stays in FFU mode during download. }; static inline __u32 per_byte_htole32(__u8 *arr) @@ -2866,6 +2867,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 == FFU_OPT_MODE4) { + set_single_cmd(&multi_cmd->cmds[0], MMC_WRITE_BLOCK, 1, 1, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset); } } @@ -2973,6 +2977,9 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz } else if (ffu_mode == FFU_OPT_MODE3) { num_of_cmds = 3; /* in FFU_OPT_MODE3, mmc_ioc_multi_cmd contains 3 commands */ chunk_size = 512; /* FFU_OPT_MODE3 uses CMD24 single-block write */ + } else if (ffu_mode == FFU_OPT_MODE4) { + num_of_cmds = 1; /* in FFU_OPT_MODE4, it is single command mode */ + chunk_size = 512; /* FFU_OPT_MODE4 uses CMD24 single-block write */ } /* allocate maximum required */ @@ -2983,10 +2990,11 @@ static int do_ffu_download(int dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_siz return -ENOMEM; } - if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2) { + if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2 || ffu_mode == FFU_OPT_MODE4) { /* - * In FFU_OPT_MODE1 and FFU_OPT_MODE2, the command to enter FFU mode will be sent - * independently, separate from the firmware bundle download command. + * In FFU_OPT_MODE1, FFU_OPT_MODE2 and FFU_OPT_MODE4, 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) @@ -3004,11 +3012,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 @@ -3040,10 +3051,10 @@ do_retry: off += bytes_per_loop; } - if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2) { + if (ffu_mode == FFU_OPT_MODE1 || ffu_mode == FFU_OPT_MODE2 || ffu_mode == FFU_OPT_MODE4) { /* - * In FFU_OPT_MODE1 and FFU_OPT_MODE2, the command to exit FFU mode will be sent - * independently, separate from the firmware bundle download command. + * In FFU_OPT_MODE1, FFU_OPT_MODE2 and FFU_OPT_MODE4, 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) @@ -3233,6 +3244,11 @@ int do_opt_ffu3(int nargs, char **argv) return __do_ffu(nargs, argv, FFU_OPT_MODE3); } +int do_opt_ffu4(int nargs, char **argv) +{ + return __do_ffu(nargs, argv, FFU_OPT_MODE4); +} + int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 0fbb2b5..407cbe6 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -45,6 +45,7 @@ int do_ffu(int nargs, char **argv); int do_opt_ffu1(int nargs, char **argv); int do_opt_ffu2(int nargs, char **argv); int do_opt_ffu3(int nargs, char **argv); +int do_opt_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);