From patchwork Tue Oct 1 20:38:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13818738 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [81.169.146.165]) (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 3924C1925B8 for ; Tue, 1 Oct 2024 20:41:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=81.169.146.165 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815307; cv=pass; b=MLVvJhO6VosOPKtmguSBWerRHDnojiLCx7xGmiRoPQdR/82Xpr0vLJnYyKWeTSpk/jXx0XrXKY3pmoyCSQJ7j57Zt8SxeConCWTxwsF5CwjFEAPo1Q0PxcMpAKt2Hj4klQ1OvBcA18uAiwdIqk8X0Yob8z7JLdEmUQOnusg/LP4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815307; c=relaxed/simple; bh=VwFqgswiA/G0KBdlrcOQ4cV5ZzlptNG38edXKtZMnas=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=m/bHIEve+fQOjV7TwRRuyxZcohnHu5ySBEJMwO8h0b6+6Z/SnnqBqP8GzGoQitogUG97FSnlF5HPI9UXko4GfGwNzivhFldn0Nhw6p6cgRj8G15bQHuPqqSvw6WbHQK7cNgD1RQW42bQ/QtLbi1JS8+B/VA1AsFy9E5li0qkWMA= 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=DyrsegBE; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=40Sk90hw; arc=pass smtp.client-ip=81.169.146.165 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="DyrsegBE"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="40Sk90hw" ARC-Seal: i=1; a=rsa-sha256; t=1727815120; cv=none; d=strato.com; s=strato-dkim-0002; b=qq/kIL6o2/+1ydrGXYwLyIhRG2OjP+jEnm54kaEmDmxCGq53yZKbTzNt8YFGXN84Hs gVAEYDBCcbhIOIaqJ9h9ErFPUOMq/fY6/oBRdbFulB0J5A4ozIXVx4a/fn+p7ZVqYGpm 3Qo7RQeX0JS+s0EBzr9KhuECX0w6IEcDvjPYZ2RevbWmhr9AISoAjRN0LNyMlVGMPsJA ULp9StUr5vmzOJIUS26wknyZEPDgAwSARDg8lN8nddw6DijKcD9RRxofm8eiQBTNecM1 k2Dhlbag9mFBOXB2KzIsOcjT3TQBWwMHo5uQXO/GzUmXUY2N74G3IEdYSrmOWXLiwqWp R8fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727815120; 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=qofpX6R9XkkIFx8NFoA1GL1XUqXOxBJk0ms21KuXZbI=; b=iT/n1+EJBTI4uAvAoJV+CjnrvcaDCAF4rjkQ6HDQ19JtVZ7ni12TdD1UBKBFc0oWGt xeHz4rK9jyRBVLGsGdCOV0cXxjw6JUNdmLBMpCnMktR1dRETYURSFd/sRvMludTQTkkQ GcIbhrNevoIy2Ho0NAX7AQGIKqlI0zCbCPnReVzDC9Jy8KSiiy0Fddc6ijalPnZgRsVA LzekHm4Q5xObkuMQ1W8aVWli1ggPJanwqwIU4MJ1cj9QK7FTQpTjRpHH+jiswgltdHa6 XwX7E38C0nGIV82W5oIucmONLqdSqfqbrotvUmG1tIFN8q3iniAbiztSW9bM0+3aoniL Q5uA== 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=1727815120; 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=qofpX6R9XkkIFx8NFoA1GL1XUqXOxBJk0ms21KuXZbI=; b=DyrsegBEHtbqXurpELtqGDndGycRMUmdxqhpDpoQODESCnfHRtmyJQntJKMDjbVttl TFkfKzysNQayl243Syg02PWNphT45bZkREVkzd9F1Urn4tdzs7JT/queCdrKlAGrAgB2 qqohnHn66FjtvF3jVJqU0nmu38tyl8dm/JURPZVsJG+4hs5pefAIH1VBeV4vxBjqGASY sXkP7TgIJvjTe3OOrbqHbgMIqHLuxMEG3uQvAVWsGNi/hR+wxMC+OL3ToZNFjbj69Dkm ETI/hM9oumrA6r2PA35sj5pp+gUdxwXVY14GAcptKGZy+j1OYf7HWp39q2xnCQ651lGe 6T1g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727815120; 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=qofpX6R9XkkIFx8NFoA1GL1XUqXOxBJk0ms21KuXZbI=; b=40Sk90hwZH229MD+0KdWhZW7uHia0uZIVrUfOlx6pLmWM96YbxJsl4yScXK8VrsjRi Xi1HaXGtw2ZhMxHcgMDA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTv80p2D11QFwNypsTi9pEHpqtUzGQwQ==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0091Kcecdf (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Oct 2024 22:38:40 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org Cc: Bean Huo Subject: [PATCH v1 1/5] mmc_utils: Extract FFU code to mmc_ffu.c for future FFU mode expansion Date: Tue, 1 Oct 2024 22:38:07 +0200 Message-Id: <20241001203811.26279-2-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001203811.26279-1-beanhuo@iokpp.de> References: <20241001203811.26279-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 FFU functionality by extracting existing FFU code from `mmc_cmds.c` into a dedicated file, `mmc_ffu.c`. Updated `do_ffu` in `mmc_utils` to reflect the new structure and interface. This restructuring provides better code organization and a clearer separation of concerns, making it easier to maintain and extend FFU functionality. Follow-up patches will focus on implementing additional FFU modes and enhancements. Signed-off-by: Bean Huo --- Makefile | 1 + mmc.c | 11 +- mmc_cmds.c | 274 -------------------------------------------- mmc_cmds.h | 52 +++++++++ mmc_ffu.c | 324 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 384 insertions(+), 278 deletions(-) create mode 100644 mmc_ffu.c diff --git a/Makefile b/Makefile index 06ae0f7..35aca6c 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ objects = \ mmc.o \ mmc_cmds.o \ lsmmc.o \ + mmc_ffu.o \ 3rdparty/hmac_sha/hmac_sha2.o \ 3rdparty/hmac_sha/sha2.o diff --git a/mmc.c b/mmc.c index 2c5b9b5..52c5a89 100644 --- a/mmc.c +++ b/mmc.c @@ -227,11 +227,14 @@ static struct Command commands[] = { "The device path should specify the scr file directory.", NULL }, - { do_ffu, -2, - "ffu", " [chunk-bytes]\n" + { do_ffu, -4, + "ffu", "-i -p [ -c ] [-m ]\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", + "[-c ] is optional and defaults to its max - 512k. " + "should be in decimal bytes and sector aligned.\n" + "[-m ] Optional, provides five firmware bundle download command modes:\n" + " -m 1: Default mode, utilizing the CMD6+CMD23+CMD25+CMD6 command sequence. This \n" + " may exit FFU mode during firmware bundle downloading if FW size exceeds the chunk size. \n", NULL }, { do_erase, -4, diff --git a/mmc_cmds.c b/mmc_cmds.c index 3b1bcf4..22ab7d9 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -58,41 +58,6 @@ #define WPTYPE_PWRON 2 #define WPTYPE_PERM 3 -static inline __u32 per_byte_htole32(__u8 *arr) -{ - return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24; -} - -static int read_extcsd(int fd, __u8 *ext_csd) -{ - int ret = 0; - struct mmc_ioc_cmd idata; - memset(&idata, 0, sizeof(idata)); - memset(ext_csd, 0, sizeof(__u8) * 512); - idata.write_flag = 0; - idata.opcode = MMC_SEND_EXT_CSD; - idata.arg = 0; - idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; - idata.blksz = 512; - idata.blocks = 1; - mmc_ioc_cmd_set_data(idata, ext_csd); - - ret = ioctl(fd, MMC_IOC_CMD, &idata); - if (ret) - perror("ioctl"); - - return ret; -} - -static void fill_switch_cmd(struct mmc_ioc_cmd *cmd, __u8 index, __u8 value) -{ - cmd->opcode = MMC_SWITCH; - cmd->write_flag = 1; - cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) | - (value << 8) | EXT_CSD_CMD_SET_NORMAL; - cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; -} - static int write_extcsd_value(int fd, __u8 index, __u8 value, unsigned int timeout_ms) { @@ -2101,18 +2066,6 @@ struct rpmb_frame { u_int16_t req_resp; }; -static inline void set_single_cmd(struct mmc_ioc_cmd *ioc, __u32 opcode, - int write_flag, unsigned int blocks, - __u32 arg) -{ - ioc->opcode = opcode; - ioc->write_flag = write_flag; - ioc->arg = arg; - ioc->blksz = 512; - ioc->blocks = blocks; - ioc->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; -} - /* Performs RPMB operation. * * @fd: RPMB device on which we should perform ioctl command @@ -2810,233 +2763,6 @@ out: return ret; } -static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, - __u8 *ext_csd, unsigned int bytes, __u8 *buf, - off_t offset) -{ - __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); -} - -int do_ffu(int nargs, char **argv) -{ - 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); - } - - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); - goto out; - } - - if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) { - fprintf(stderr, - "The FFU feature is only available on devices >= " - "MMC 5.0, not supported in %s\n", device); - goto out; - } - - if (!(ext_csd[EXT_CSD_SUPPORTED_MODES] & EXT_CSD_FFU)) { - fprintf(stderr, "FFU is not supported in %s\n", device); - goto out; - } - - if (ext_csd[EXT_CSD_FW_CONFIG] & EXT_CSD_UPDATE_DISABLE) { - fprintf(stderr, "Firmware update was disabled in %s\n", device); - goto out; - } - - fw_size = lseek(img_fd, 0, SEEK_END); - if (fw_size == 0) { - fprintf(stderr, "Wrong firmware size"); - goto out; - } - - /* 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) { - 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; - } - } - - /* 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); - - /* return device into normal mode */ - 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; - while (bytes_left) { - unsigned int chunk_size = bytes_left < default_chunk ? - bytes_left : default_chunk; - - /* prepare multi_cmd for FFU based on cmd to be used */ - set_ffu_single_cmd(multi_cmd, ext_csd, chunk_size, buf, off); - - /* send ioctl with multi-cmd */ - ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); - - if (ret) { - perror("Multi-cmd ioctl"); - /* - * In case multi-cmd ioctl failed before exiting from - * ffu mode - */ - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); - goto out; - } - - bytes_left -= chunk_size; - off += chunk_size; - } - - /* - * 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; - goto out; - } - - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); - goto out; - } - - /* Test if we need to restart the download */ - sect_done = 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; - } - fprintf(stderr, "Programming failed! Aborting...\n"); - 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); - } - else { - fprintf(stderr, "FW size and number of sectors written mismatch. Status return %d\n", ret); - goto out; - } - - fprintf(stderr, "Installing firmware on %s...\n", device); - /* Re-enter ffu mode and install the firmware */ - multi_cmd->num_of_cmds = 2; - - /* set ext_csd to install mode */ - 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"); - /* In case multi-cmd ioctl failed before exiting from ffu mode */ - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); - goto out; - } - - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); - goto out; - } - - /* return status */ - ret = ext_csd[EXT_CSD_FFU_STATUS]; - if (ret) { - fprintf(stderr, "%s: error %d during FFU install:\n", device, ret); - goto out; - } else { - fprintf(stderr, "FFU finished successfully\n"); - } - -out: - free(buf); - free(multi_cmd); - close(img_fd); - close(dev_fd); - return ret; -} - int do_general_cmd_read(int nargs, char **argv) { int dev_fd; diff --git a/mmc_cmds.h b/mmc_cmds.h index 5f2bef1..40a137d 100644 --- a/mmc_cmds.h +++ b/mmc_cmds.h @@ -16,6 +16,57 @@ * Modified to add field firmware update support, * those modifications are Copyright (c) 2016 SanDisk Corp. */ +#include +#include +#include +#include "mmc.h" + +static inline __u32 per_byte_htole32(__u8 *arr) +{ + return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24; +} + + +static inline int read_extcsd(int fd, __u8 *ext_csd) +{ + int ret = 0; + struct mmc_ioc_cmd idata; + memset(&idata, 0, sizeof(idata)); + memset(ext_csd, 0, sizeof(__u8) * 512); + idata.write_flag = 0; + idata.opcode = MMC_SEND_EXT_CSD; + idata.arg = 0; + idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; + idata.blksz = 512; + idata.blocks = 1; + mmc_ioc_cmd_set_data(idata, ext_csd); + + ret = ioctl(fd, MMC_IOC_CMD, &idata); + if (ret) + perror("ioctl"); + + return ret; +} + +static inline void fill_switch_cmd(struct mmc_ioc_cmd *cmd, __u8 index, __u8 value) +{ + cmd->opcode = MMC_SWITCH; + cmd->write_flag = 1; + cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) | + (value << 8) | EXT_CSD_CMD_SET_NORMAL; + cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; +} + +static inline void set_single_cmd(struct mmc_ioc_cmd *ioc, __u32 opcode, int write_flag, + unsigned int blocks, __u32 arg) +{ + ioc->opcode = opcode; + ioc->write_flag = write_flag; + ioc->arg = arg; + ioc->blksz = 512; + ioc->blocks = blocks; + ioc->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; +} /* mmc_cmds.c */ int do_read_extcsd(int nargs, char **argv); @@ -50,3 +101,4 @@ int do_general_cmd_read(int nargs, char **argv); int do_softreset(int nargs, char **argv); int do_preidle(int nargs, char **argv); int do_alt_boot_op(int nargs, char **argv); +int do_ffu(int nargs, char **argv); diff --git a/mmc_ffu.c b/mmc_ffu.c new file mode 100644 index 0000000..5f7cb00 --- /dev/null +++ b/mmc_ffu.c @@ -0,0 +1,324 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Modified and add more FFU modes. + * And changed and updated by: Bean Huo + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mmc.h" +#include "mmc_cmds.h" + +static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_size, unsigned int chunk_size); + +static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_csd, + unsigned int bytes, __u8 *buf, off_t offset) +{ + __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); +} + +static int get_ffu_sectors_programmed(int *dev_fd, __u8 *ext_csd) +{ + int ret; + + ret = read_extcsd(*dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD\n"); + return ret; + } + + ret = per_byte_htole32((__u8 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]); + + return ret; +} + +static bool ffu_is_supported(__u8 *ext_csd, char *device) +{ + if (ext_csd == NULL) { + fprintf(stderr, "ext_cst is NULL\n"); + return false; + } + + if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) { + fprintf(stderr, "The FFU feature is only available on devices >= " + "MMC 5.0, not supported in %s\n", device); + return false; + } + + if (!(ext_csd[EXT_CSD_SUPPORTED_MODES] & EXT_CSD_FFU)) { + fprintf(stderr, "FFU is not supported in %s\n", device); + return false; + } + + if (ext_csd[EXT_CSD_FW_CONFIG] & EXT_CSD_UPDATE_DISABLE) { + fprintf(stderr, "Firmware update was disabled in %s\n", device); + return false; + } + + return true; +} + +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, sect_done, retry = 3; + struct mmc_ioc_multi_cmd *multi_cmd = NULL; + + if (!dev_fd || !fw_buf || !ext_csd) { + fprintf(stderr, "unexpected NULL pointer\n"); + return -EINVAL; + } + /* allocate maximum required */ + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + num_of_cmds * sizeof(struct mmc_ioc_cmd)); + if (!multi_cmd) { + perror("failed to allocate memory"); + 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); + +do_retry: + bytes_left = fw_size; + off = 0; + multi_cmd->num_of_cmds = num_of_cmds; + + while (bytes_left) { + 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); + + /* send ioctl with multi-cmd, download firmware bundle */ + ret = ioctl(*dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); + + if (ret) { + perror("Multi-cmd ioctl"); + /* + * In case multi-cmd ioctl failed before exiting from + * ffu mode + */ + ioctl(*dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); + goto out; + } + + sect_done = get_ffu_sectors_programmed(dev_fd, ext_csd); + if (sect_done <= 0) { + /* By spec, host should re-start download from the first sector if sect_done is 0 */ + ioctl(*dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); + if (retry > 0) { + retry--; + fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry); + goto do_retry; + } + fprintf(stderr, "Programming failed! Aborting...\n"); + ret = sect_done; + goto out; + } else { + fprintf(stderr, "Programmed %d/%jd bytes\r", sect_done * 512, (intmax_t)fw_size); + } + + bytes_left -= bytes_per_loop; + off += bytes_per_loop; + } + + ret = get_ffu_sectors_programmed(dev_fd, ext_csd); +out: + free(multi_cmd); + return ret; + +} + +int do_ffu(int nargs, char **argv) +{ + int opt; + 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, *fw_img; + struct mmc_ioc_multi_cmd *multi_cmd = NULL; + unsigned int default_chunk = MMC_IOC_MAX_BYTES; + int sect_done = 0; + __u8 ffu_mode = 1; + + assert(nargs >= 4); + + while ((opt = getopt(nargs, argv, "i:p:m:c:")) != -1) { + switch (opt) { + case 'i': + fw_img = optarg; + break; + case 'p': + device = optarg; + break; + case 'c': + default_chunk = atoi(optarg); + if (default_chunk > MMC_IOC_MAX_BYTES || default_chunk % 512) { + fprintf(stderr, "Invalid chunk size"); + exit(1); + } + break; + case 'm': + ffu_mode = atoi(optarg); + if (ffu_mode > 1) { + fprintf(stderr, "Unsupported ffu mode `%d'.\n", ffu_mode); + abort(); + } + break; + default: + /* Unknown option or missing argument for options */ + if (isprint(optopt)) + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); + abort(); + } + } + fprintf(stderr, "eMMC Devie: %s, fw %s, FFU mode %d, chunk size: %d\n", + device, fw_img, ffu_mode, default_chunk); + dev_fd = open(device, O_RDWR); + if (dev_fd < 0) { + perror("device open failed"); + exit(1); + } + img_fd = open(fw_img, O_RDONLY); + if (img_fd < 0) { + perror("image open failed"); + close(dev_fd); + exit(1); + } + + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } + if (ffu_is_supported(ext_csd, device) <= 0) + goto out; + + fw_size = lseek(img_fd, 0, SEEK_END); + if (fw_size == 0) { + fprintf(stderr, "Wrong firmware size"); + 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; + } + /* allocate maximum required */ + fw_buf = malloc(fw_size); + if (!fw_buf) { + perror("failed to allocate memory"); + goto out; + } + /* 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; + } + + sect_done = do_ffu_download((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk); + + /* Check programmed sectors */ + if (sect_done > 0 && (sect_done * 512) == fw_size) { + fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)fw_size, (intmax_t)fw_size); + fprintf(stderr, "Programming finished with status %d \n", ret); + } else { + fprintf(stderr, "Firmware bundle download failed. Operation status %d\n", sect_done); + ret = -EIO; + goto out; + } + /* + * 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; + goto out; + } + + fprintf(stderr, "Installing firmware on %s...\n", device); + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + 2 * sizeof(struct mmc_ioc_cmd)); + if (!multi_cmd) { + perror("failed to allocate memory"); + ret = -ENOMEM; + goto out; + } + + /* Re-enter ffu mode and install the firmware */ + multi_cmd->num_of_cmds = 2; + /* put device into ffu mode */ + fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG, EXT_CSD_FFU_MODE); + /* Re-enter ffu mode and set ext_csd to install 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; + } + + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } + + /* return status */ + ret = ext_csd[EXT_CSD_FFU_STATUS]; + if (ret) + fprintf(stderr, "%s: error %d during FFU install:\n", device, ret); + else + fprintf(stderr, "FFU finished successfully\n"); + +out: + if (fw_buf) + free(fw_buf); + if (multi_cmd) + free(multi_cmd); + close(img_fd); + close(dev_fd); + return ret; +} From patchwork Tue Oct 1 20:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13818742 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.50]) (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 E09021CCEDA for ; Tue, 1 Oct 2024 20:50:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815844; cv=pass; b=CgixmFb6TGVoEkhPT4vOkqbP5rhdU4AleoZzBoQiphZ24MwDstawhw/Rojke8SSufYHiyuZfjCrOPyjo8PO5Gnwehek8q6a2i8FGQUeh/q0tAIrZOzyUh50Y6qiyE5N+jixWrYNDvQqXlTpvPkOUGQGMj3VftkdFzJBmWPtXKoY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815844; c=relaxed/simple; bh=cNq00PhtdAW/w1eFeCEj03hARx7Oeywky+JlSt4bskg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=fqYqsV9kDpv4OgHxjlRtPmmy/i55jfEeB5Yton9rhmPv+X7/l4YTypNHPdOSRTbTpz6DgFO9LADpQPi1GhlVyMN/GxiMDX0Q5ZJh2eA23I3kSPGRShtnvEooWtO5a8gGzj9+qadfceVk47JGnj7qk4SuQmgjsHQ0mmu+FhA6qBw= 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=VWpONjZX; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=ZdwC1aoi; arc=pass smtp.client-ip=85.215.255.50 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="VWpONjZX"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="ZdwC1aoi" ARC-Seal: i=1; a=rsa-sha256; t=1727815120; cv=none; d=strato.com; s=strato-dkim-0002; b=cdiGcrRuKwALeh7WsGckQROmjCT8BN9WqwaJkO/VtZNKOCOdfPQJc5c/LyCOSGv++c JN87G9dcI5hsxaBzELXyfbsnIvXuS+YppaBZ7UFETRvpeSLEyuY4XdDJVvqW5+w0LaMP 1HFSZ7o0wW16f8pUSJJqU5wxqvio87C+eiANBWbgW/Lg3Em8nxExthtus6jbyWuDZUnr 6W+i3CdCHDqHzx4vHlxR9fv0UJa5hc45zQCuS4BXINiNq1NhTi8mlxoZ+DPQTmRIjZgp 2OYbIgwDrnmOgcrEfPZ+RRVM36GywseU6y+Skd5lYs6CBCoqxE56VtqAhu5aYWmSnf6J ZZyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727815120; 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=adV/AlN66yoFTZFGicsknLNwIfKjXGozOqwCuqrqIVQ=; b=JZ7lpvhKS486sH9215rP+CwUGxFV/6uqC19go+JqkREIeQ49MOv1aG8EtyDNr75ixn 8WhShp2K8HrbtFhjQiBsus9buTsqdeiOWmuAImY86P62a4/FZmC76iPeob9WXwxwJfyF 8Xr9atlYl05jCBx8Fu4CofrSWlBdXU+JBZZWVYF3YQso+7cVYbyBk9mlb76/3/tgH9Bs O3xcjODFveoJWimyh34Qv/fGwoLJS2KAjTllsszhto3abInDX4gUMyXC32MV8GQhrz+R DbCSC5ewkx/mrQAyHP1/odl9quvqwxh2kKDP88TWQRAhfD063lh87SLNJ2h4pZtbzdUu L6HQ== 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=1727815120; 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=adV/AlN66yoFTZFGicsknLNwIfKjXGozOqwCuqrqIVQ=; b=VWpONjZXraFBEfyhEtqxOEoqZ4PvOq2egBPymDvccQnvsa3mtgI0c2taA+ix3yuM21 JQStg6TStxJwSaUyh9qiLz0icUZTPABCI+/n7oXEAap+YsVwyiIsL2J9xUu6FAwkToN9 x2oXnE6e4s1MazNDT2hneDQXe4N8e9rXTOPPSM5q01H08UQjvCbpdN0iE9A5TVSK7wLo w4k3eA145KvDbnnafgjCFwhdVrR+Tuj1mT64n1in36zvkP60hraPD6JXQ0+0L8nJh6U/ zhdE/7bz8XuZFg9x9PVjFWpA3ia7os423fbdCHlfMc1zz/P10HhMm7XbXMdqEGv2n57S vAog== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727815120; 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=adV/AlN66yoFTZFGicsknLNwIfKjXGozOqwCuqrqIVQ=; b=ZdwC1aoiA5Yc1lv6iGcXFzzaIn0a8irzqIwyuK9EU7Ba2OmnEwA7K/zk35nt+QrALJ wmCmxWL1Lsb1W4zDJuDQ== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTv80p2D11QFwNypsTi9pEHpqtUzGQwQ==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0091Kcecdg (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Oct 2024 22:38:40 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org Cc: Bean Huo Subject: [PATCH v1 2/5] mmc-utils: Add FFU mode 2 Date: Tue, 1 Oct 2024 22:38:08 +0200 Message-Id: <20241001203811.26279-3-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001203811.26279-1-beanhuo@iokpp.de> References: <20241001203811.26279-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 that ensures atomic firmware image download to improve reliability and provide a smoother FFU process. In this mode, begins with CMD6, followed by repeated CMD23+CMD25 for downloading the firmware image. 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.c | 4 +-- mmc_ffu.c | 93 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/mmc.c b/mmc.c index 52c5a89..0a22f2e 100644 --- a/mmc.c +++ b/mmc.c @@ -233,8 +233,8 @@ static struct Command commands[] = { "[-c ] is optional and defaults to its max - 512k. " "should be in decimal bytes and sector aligned.\n" "[-m ] Optional, provides five firmware bundle download command modes:\n" - " -m 1: Default mode, utilizing the CMD6+CMD23+CMD25+CMD6 command sequence. This \n" - " may exit FFU mode during firmware bundle downloading if FW size exceeds the chunk size. \n", + " -m 1: Default mode (CMD6+CMD23+CMD25+CMD6 repeated), may exit FFU mode if the firmware size exceeds chunk size.\n" + " -m 2: FFU mode 2 (enter FFU mode, CMD23+CMD25 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n", NULL }, { do_erase, -4, diff --git a/mmc_ffu.c b/mmc_ffu.c index 5f7cb00..ac65130 100644 --- a/mmc_ffu.c +++ b/mmc_ffu.c @@ -19,24 +19,36 @@ #include "mmc.h" #include "mmc_cmds.h" -static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_size, unsigned int chunk_size); +static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_size, unsigned int chunk_size, __u8 ffu_mode); static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_csd, - unsigned int bytes, __u8 *buf, off_t offset) + unsigned int bytes, __u8 *buf, 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); + /* return device into normal mode */ + fill_switch_cmd(&multi_cmd->cmds[3], EXT_CSD_MODE_CONFIG, EXT_CSD_NORMAL_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); + } else if (ffu_mode == 2) { + 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); + } } static int get_ffu_sectors_programmed(int *dev_fd, __u8 *ext_csd) @@ -80,8 +92,34 @@ 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; + + 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 faled\n"); + + return ret; +} + +static int exit_ffu_mode(int *dev_fd) +{ + int ret; + struct mmc_ioc_cmd 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 faled\n"); + + return ret; +} + 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; @@ -93,21 +131,20 @@ static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_si fprintf(stderr, "unexpected NULL pointer\n"); return -EINVAL; } + + if (ffu_mode != 1) /* only 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)); if (!multi_cmd) { perror("failed to allocate memory"); 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); - do_retry: + if (num_of_cmds != 4 && enter_ffu_mode(dev_fd)) + goto out; + bytes_left = fw_size; off = 0; multi_cmd->num_of_cmds = num_of_cmds; @@ -116,7 +153,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); @@ -127,7 +164,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; } @@ -151,11 +188,13 @@ do_retry: off += bytes_per_loop; } + if (num_of_cmds != 4 && exit_ffu_mode(dev_fd)) + goto out; + ret = get_ffu_sectors_programmed(dev_fd, ext_csd); out: free(multi_cmd); return ret; - } int do_ffu(int nargs, char **argv) @@ -192,7 +231,7 @@ int do_ffu(int nargs, char **argv) break; case 'm': ffu_mode = atoi(optarg); - if (ffu_mode > 1) { + if (ffu_mode > 2) { fprintf(stderr, "Unsupported ffu mode `%d'.\n", ffu_mode); abort(); } @@ -253,7 +292,7 @@ int do_ffu(int nargs, char **argv) goto out; } - sect_done = do_ffu_download((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk); + sect_done = do_ffu_download((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk, ffu_mode); /* Check programmed sectors */ if (sect_done > 0 && (sect_done * 512) == fw_size) { From patchwork Tue Oct 1 20:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13818743 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 5256D2B9A5 for ; Tue, 1 Oct 2024 20:50:42 +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=1727815845; cv=pass; b=XnOvQYqtV1/ffBkcXKcKwNasE5iJkhseYbH0AiM2UQJiU0zCzfqo3f1Cspi4II8FfL0pRb/htuOnkuY3lTpx7MYGGnMcTILYEYu4fxQBQxah3mg5Bx2Kt8+BHeBfgs9um4/wrwwwO0nCDXpdkcM02BUtERi8JcUcsJ/sbYCNpTQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815845; c=relaxed/simple; bh=XWoZ5M9IJgvG7TVKVlYl5y7VxImpaDUWws/n3Lju9o0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=C9bj4COG0q/cI9dSAafaoJ0WM4Y+nLowvOYToBNl6MXzW9loqnhwjtQEdttnosPOQPvSiVT8xrBWTiAIR6i3QoDA3n8RXVYEAY5+7dDx94M5/MJDKhMXwuZ8HNcZMat3E27+aH7h/MXWEWei7lZHmDIglh1dy2C6447zprZhHpI= 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=oS1Qh0ei; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=96okupII; 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="oS1Qh0ei"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="96okupII" ARC-Seal: i=1; a=rsa-sha256; t=1727815120; cv=none; d=strato.com; s=strato-dkim-0002; b=hyTAEG5R2WtkUjAAqOOYd1sL+TEzOE5Uaymw0SCMqXfT2aURYsZMkGafVm2pJERpj1 lDDeiI4pcm9au7NYkATLdGzytcM+TaQfAp7lFjuwkQ/QBir8GZpIhusdYfdV91CmyaIP jvXl9WLsTX7ZXTTSKnUytp+PFs07mFxyGnA78CVRXH0Oar4flf3ACI6rAI0R5LRKCO2J DBIBJszAtVzpfATMq4zRiAo3s6p6QNjuN8M7rpiebPT5kWcPUQfjQjDDEGP9iCqiX9m2 5bNqCMlLIjvLNZfBWL8CfMnuqB3nVS9IVEcl2f/jzHFqNrxYuWMC/H1271387lNA8XkR 3trQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727815120; 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=4txe+iD50pMDpThtrvaW5COT5g0xJ1mL6n/zbUfQoYQ=; b=FmeRDb1k6UdM8zF6z1ToDU5ep6qVBlguMqsTPgQiyWX6NNFy5P+64NqWGznRS/Iecu 3UhnKAhSMttVFZa7VbkF1eJuwcJn841eXj/zGT9c4qaMk5nnGbr7TzkMz0fO5T42LivY xaV81Kqip65yfynYKh8BeUwUxExsPJjny7XeC/6E8esae0YwwcZ+5dMQBWU8gV0NbmPU BYKkTTznLMBQhp7ErusIL+o5Z2MyTM1BLKN+uhXs4kV4q6RGEJX8LuRW3ZEMJjngDBxF 5tPU6dgLpgtTAG6+yLm8ioS2DlKElf6Xl3gTVEbdIGcYuAeUxei2yGmGlhZMOXjFEu89 JFRw== 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=1727815120; 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=4txe+iD50pMDpThtrvaW5COT5g0xJ1mL6n/zbUfQoYQ=; b=oS1Qh0eipSjs5gAdKJc+aIbmZyEBkBgVF6RKSAh5UYWa/EE8ZtRM8rMbROSAIZkP63 mqKV6NpxStppKDijMA+xXi8mR8R3AoxEfG7VVwzX08dNe8V0fsdJI3ZXWp/ijWCIoOfx y3eoO76srsvJAdX2ouV3HeGPBeNXbytY2Jjlj7861iTxVUecP9NNjzsk1zb4+1g24x3s +Dc78JfvmX5J+fDmaWo+67AjlJeKPRIqOLv+3MhlnHHkBZVMR6gyPqcyKs89C5r1/02t 4Ctrd1zNu1a0YEng6ph3fP7eP3d5g6l/kVoQeKNry6BrpVKzEPWG16pmGoQsCKUNiPyI DTpg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727815120; 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=4txe+iD50pMDpThtrvaW5COT5g0xJ1mL6n/zbUfQoYQ=; b=96okupIIVqqdCNWL6s10ZtTqJSnr9n/L6mkWruqPYi3arTuZ4slcDZQJaqUOOdHi5D UTo0j1tfp0CprHfgIMAw== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTv80p2D11QFwNypsTi9pEHpqtUzGQwQ==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0091Kcecdh (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Oct 2024 22:38:40 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org Cc: Bean Huo Subject: [PATCH v1 3/5] mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write download FW Date: Tue, 1 Oct 2024 22:38:09 +0200 Message-Id: <20241001203811.26279-4-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001203811.26279-1-beanhuo@iokpp.de> References: <20241001203811.26279-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 leveraging CMD25+CMD12 for Open-ended Multiple-block write to download the firmware bundle. Signed-off-by: Bean Huo --- mmc.c | 3 ++- mmc.h | 1 + mmc_ffu.c | 9 ++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mmc.c b/mmc.c index 0a22f2e..73b694d 100644 --- a/mmc.c +++ b/mmc.c @@ -234,7 +234,8 @@ static struct Command commands[] = { "should be in decimal bytes and sector aligned.\n" "[-m ] Optional, provides five firmware bundle download command modes:\n" " -m 1: Default mode (CMD6+CMD23+CMD25+CMD6 repeated), may exit FFU mode if the firmware size exceeds chunk size.\n" - " -m 2: FFU mode 2 (enter FFU mode, CMD23+CMD25 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n", + " -m 2: FFU mode 2 (enter FFU mode, CMD23+CMD25 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n" + " -m 3: FFU mode 3 (enter FFU mode, CMD25+CMD12 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n", NULL }, { do_erase, -4, diff --git a/mmc.h b/mmc.h index 6f1bf3e..78cc140 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_ffu.c b/mmc_ffu.c index ac65130..239d3b0 100644 --- a/mmc_ffu.c +++ b/mmc_ffu.c @@ -48,6 +48,13 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_ 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; + } } @@ -231,7 +238,7 @@ int do_ffu(int nargs, char **argv) break; case 'm': ffu_mode = atoi(optarg); - if (ffu_mode > 2) { + if (ffu_mode > 3) { fprintf(stderr, "Unsupported ffu mode `%d'.\n", ffu_mode); abort(); } From patchwork Tue Oct 1 20:38:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13818740 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 82F391925B8 for ; Tue, 1 Oct 2024 20:44:38 +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=1727815482; cv=pass; b=FbCSTC3yyv8XKOkMWM2tpaSw/AadEyXzc0cDVMvCI0Nc7BD9RqoaqR2I/gbj84a9oTJJzyAII8AE4TNqyc3AnuLRnXYheTTn3TVMhlCZ710Vy/qYseNU/Qrwx5VOZnkEpYWV2G9JbVaiiR2V8dgODicfrdYmJNOxL4tYPH9JvVE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815482; c=relaxed/simple; bh=yKvIT1JEmObxd+barjNg3/LrvihZvwKOQgKKVHA2BHg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=k52taEM27dTOD0/WbOp8wv4tbDJqJhQp4873JoCxBb7K4e3kDI1d3xihQws2QWlvTCBeKI1ovvotAS5RfkS/ZqrZLFAeYvxoNTFJo4aXs1ln8WnVb62lr8Xw3QzbHvRDQkqMqGqTYjmbKxAsGMhJg/RIzqhiJAnO7IcEgLEaTJA= 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=pNFV9o00; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=Y55J9Sld; 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="pNFV9o00"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="Y55J9Sld" ARC-Seal: i=1; a=rsa-sha256; t=1727815120; cv=none; d=strato.com; s=strato-dkim-0002; b=CK2Jv+iWIIykhYWij+rfMfyJtjpRCDMt4nhtvdUkNojQpLd89Wuq8Ut2PE5g8i+jle AXjNTix3LwY1B0Q/E2XjfgxR9hF+fpGJht9J9jd0AMjM/EDIVhLFm4MdicjFXMUZEbKV yl0W3GeEZKd96+kc0kZvcDkTO2VAoXM/DJDaIBnQM8kXPv73s02zdaazfPsU5811t7VB 1Cw/DaLaEivPMVHkodNYSMgk4cWSGOcGe5+o9zwnt3ndIaEMH/N59pyZqlt+JefRRSoR fa3/s+NEZ1pFYJ4NXdcBzhPPCEf1jqEkcmrFcS6YjapXRgWbtszGnEOfTSrEqxPZInkn j59A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727815120; 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=vKTBXzM+PborSxwGXZE6/JsmEgq6H9BNtgDyEXIfEZQ=; b=IU33myOOMQqyuIdf49dzsIUaac/1oGtv+LW6UZVZG7W+tboRtgQVXFHy9EhkpKt5GL vKAmcwYcLLGV6Nfm1ls02OIR3rugpOwhcp7vLkH1kueP46ZWWdHkAvZu840v/fREJyQd b79e99+vkaqDqNv7cH2o+9F1l+q84LUvcSHI0zgMLv32pEVjfK4zkngy/6VNhd0+sE/R aOeGkRu5h3kxGYQ9H+ZlczWNMPvxwsnyH9HpIW+E7OlN4/u9xqsBlDGfaO+Fw+NObcIp tDUQAZ0aaGd46L5RepM4TinMYIzHAVSD196H5CrioX1+hqT9aQD0kBFtlI7NsSGd64Mw uBeg== 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=1727815120; 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=vKTBXzM+PborSxwGXZE6/JsmEgq6H9BNtgDyEXIfEZQ=; b=pNFV9o00aVYxvOfJJ0iOaTFE95bKS0eNV8XADQicyNWr52s+8iaagqM9FMcyrQD4vW +yKhph3ZlnJ3rKf362bCXgyRJN8hN2pLuo7g4KnQNtXbNaFfFI9P0MHRxg27I5Uyo6kJ LesNt6BmzXrbEAbuNYInGIjts9S35OV/kBof2FjXp35g1mZXAimrZODRn8gWrNBcMeEw J6Xo71dtZybM1tcKIFiV1exIT2CmbrOS8Qm+PGsuU9GKXmwjXoqXdaBSkglz2svWRIn5 0YwNI4Jq19BHuybCdEk5OeXRZxWbRmuQ4CGoVOY6+lYXW3vCxlv9BNcIG0SNjqXnqFct ErKQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727815120; 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=vKTBXzM+PborSxwGXZE6/JsmEgq6H9BNtgDyEXIfEZQ=; b=Y55J9Sldu45w9JC/o1wa2bE2hmGLH4kJWAcYUEqpikYXSfjh0cw1+5wFgDA8tYVZVj tiR6HQBB2BZSmBy/BtAQ== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTv80p2D11QFwNypsTi9pEHpqtUzGQwQ==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0091Kcecdi (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Oct 2024 22:38:40 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org Cc: Bean Huo Subject: [PATCH v1 4/5] mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block firmware download Date: Tue, 1 Oct 2024 22:38:10 +0200 Message-Id: <20241001203811.26279-5-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001203811.26279-1-beanhuo@iokpp.de> References: <20241001203811.26279-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 block is written, CMD6 is used to exit FFU mode. Signed-off-by: Bean Huo --- mmc.c | 3 ++- mmc_ffu.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mmc.c b/mmc.c index 73b694d..2fdfdce 100644 --- a/mmc.c +++ b/mmc.c @@ -235,7 +235,8 @@ static struct Command commands[] = { "[-m ] Optional, provides five firmware bundle download command modes:\n" " -m 1: Default mode (CMD6+CMD23+CMD25+CMD6 repeated), may exit FFU mode if the firmware size exceeds chunk size.\n" " -m 2: FFU mode 2 (enter FFU mode, CMD23+CMD25 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n" - " -m 3: FFU mode 3 (enter FFU mode, CMD25+CMD12 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n", + " -m 3: FFU mode 3 (enter FFU mode, CMD25+CMD12 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n" + " -m 4: FFU mode 4 (CMD6+CMD24+CMD6 repeated), exits FFU mode after each cycle.\n", NULL }, { do_erase, -4, diff --git a/mmc_ffu.c b/mmc_ffu.c index 239d3b0..fff7b11 100644 --- a/mmc_ffu.c +++ b/mmc_ffu.c @@ -55,6 +55,11 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_ 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); } } @@ -139,8 +144,10 @@ static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_si return -EINVAL; } - if (ffu_mode != 1) /* only in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */ + if (ffu_mode == 2 || ffu_mode == 3) /* only in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */ num_of_cmds = 2; + else if (ffu_mode == 4) + num_of_cmds = 3; /* allocate maximum required */ multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + num_of_cmds * sizeof(struct mmc_ioc_cmd)); @@ -238,7 +245,7 @@ int do_ffu(int nargs, char **argv) break; case 'm': ffu_mode = atoi(optarg); - if (ffu_mode > 3) { + if (ffu_mode > 4) { fprintf(stderr, "Unsupported ffu mode `%d'.\n", ffu_mode); abort(); } From patchwork Tue Oct 1 20:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 13818741 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 C7B1C1CB334 for ; Tue, 1 Oct 2024 20:50:39 +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=1727815843; cv=pass; b=gSc6zLKPd6lkGhEXcGxXqxDsgSLQRHqXTaKlpOVQnEXfgraiIMRoCvF9fp0SLYKVMzRy739p8kPoe6rrwVf3PKraOaHjCepM/gfeDEZRTYhTQzCEzweaUePhxWtn32IEr8c2lHw6pEvKAblwMRDeFFEPraME1HKBl+3Gk8CpSwI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727815843; c=relaxed/simple; bh=sRuG3TtNlkn/y8qwch2GpQp1dkIVC5APa/BDuC93j6o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=iIMRxXNWJDJUeOzPWub0xXTrzE88CG2HxRqPKagNknwnzsD0+5B1EOshrCCft63QC2YpQ40X/eYeTrFeW+60ePcnHxFrGWUpzrg8x4vNDgyZAiCjvoo/T4u6HyHkO2Pu6TyCNxA0oZZfzOeE5/GaM2lKd3PmxZjXvzkDUUlcEK8= 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=LtD2oHmI; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=FqDM8lmL; 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="LtD2oHmI"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="FqDM8lmL" ARC-Seal: i=1; a=rsa-sha256; t=1727815120; cv=none; d=strato.com; s=strato-dkim-0002; b=sM7oytJN1DJIqaQHyIRm0Us/+s+fDiCv4KAQbk/XtIST2lVhfvSWD+YpfM927MJURr pTPHOAAz2XaHUOFau4V+ZUQ1dxv88B5CdmAGXtM3uc7eKkk2r6ti3qSDeM7tiLkD7AZy rLC9pcws3aVk2hl587nemUWhL+tZUT8OyvSO4/LB67QHD9R8uYR8CvVfJZ3mO6wEvFD8 1UpbR4lHnnj7PwEYvUm7CgzCIBDn31qxJT2TlDJlulRjoyavYwdK13EyrP9Gdy1lbY3g 0zPs2gybr8GwcDOh6G6jRKF5bL5iXDilDzk43CUdVAJUGs9aH+ttueRNvpiDVNqjYXKF iVbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727815120; 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=UT8FxUtJ5s7Ga5tE7Zbjmhp9+ciwYY6lV/DLRVqS6bg=; b=F2oqMZBTQCVVW6OvtzaYNiEo7VwCvD3r54PtF3TY1JdXoU5o5XZ4c/ZNPrfaM4RHUc z492dhlEykhHxNBwDNnUb2SxRtZ0kRofQn0bTSAFX/7Hpw+SKkwYflEFxXQUddQeU8Mv DDOdEEY/VTdAOKgOmqfnxby1PQsSiM7UodfXSc7yLp4+RM7kt3mQpr4umW4we5yYUubN G5eeCKx96AmkovhcMpB3ggGYkviRkNccInS+csHc/gEX4U2lvwz04+z+l7YI1Bp2WOB3 qmiJ0vn6rbQCmQmdGK5n8an9V0zzDamAWHGN+KPJmJB/naCcFqJOyECT1lIg7CiYeAm5 fEHw== 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=1727815120; 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=UT8FxUtJ5s7Ga5tE7Zbjmhp9+ciwYY6lV/DLRVqS6bg=; b=LtD2oHmIx5LDTvwhOHq3Qg/O5D9TaXwJkouQ5+ujb3kqFibdd6W0QgddMtLA2MhM60 l9oBJJ01X67LSWBSma2HyG7HRcdfBccZ5WoGByndSTqRrL2wYk2iLP5xYC7xm0ey/VhH BjKngCFOKuJgMXD72lZZKA+0p9NypL3RYNhDDaF18kQ6sl4/XRZ5FT7WfwN7T78ILhNH X0APSTzztRUz6N74GxHBXDPeihpPFYUmfQZvqZ5JRyg5km7pEELubN8FOesop8tJsFdb bd0f8P7lSFsANP7WMVd4xILYjtGjzjvCxK4bwjpHAcxLrjm7jpmJJfACEmMUFAZYRbc4 7Iqg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727815120; 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=UT8FxUtJ5s7Ga5tE7Zbjmhp9+ciwYY6lV/DLRVqS6bg=; b=FqDM8lmLq9XLeZ6mLCWFDBKfEkzpK2CAPZpBhab3PcfcNDh3mwX9p4imzNwb0GlQ0r 10iYBiZzjQPoJjsi1dDg== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTv80p2D11QFwNypsTi9pEHpqtUzGQwQ==" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0091Kcecdj (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Oct 2024 22:38:40 +0200 (CEST) From: Bean Huo To: avri.altman@wdc.com, ulf.hansson@linaro.org, linux-mmc@vger.kernel.org Cc: Bean Huo Subject: [PATCH v1 5/5] mmc-utils: add FFU mode 5 for firmware download using CMD6 and repeated CMD24 commands Date: Tue, 1 Oct 2024 22:38:11 +0200 Message-Id: <20241001203811.26279-6-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241001203811.26279-1-beanhuo@iokpp.de> References: <20241001203811.26279-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 --- mmc.c | 3 ++- mmc_ffu.c | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/mmc.c b/mmc.c index 2fdfdce..8f71a51 100644 --- a/mmc.c +++ b/mmc.c @@ -236,7 +236,8 @@ static struct Command commands[] = { " -m 1: Default mode (CMD6+CMD23+CMD25+CMD6 repeated), may exit FFU mode if the firmware size exceeds chunk size.\n" " -m 2: FFU mode 2 (enter FFU mode, CMD23+CMD25 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n" " -m 3: FFU mode 3 (enter FFU mode, CMD25+CMD12 repeated, exit FFU mode), stays in FFU mode until firmware download completes.\n" - " -m 4: FFU mode 4 (CMD6+CMD24+CMD6 repeated), exits FFU mode after each cycle.\n", + " -m 4: FFU mode 4 (CMD6+CMD24+CMD6 repeated), exits FFU mode after each cycle.\n" + " -m 5: FFU mode 5 (CMD6 enter FFU mode, CMD24 repeated, CMD6 exit FFU mode) stays in FFU mode until firmware download completes.\n", NULL }, { do_erase, -4, diff --git a/mmc_ffu.c b/mmc_ffu.c index fff7b11..5e3c59f 100644 --- a/mmc_ffu.c +++ b/mmc_ffu.c @@ -60,6 +60,9 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_ 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 { + set_single_cmd(&multi_cmd->cmds[0], MMC_WRITE_BLOCK, 1, 1, arg); + mmc_ioc_cmd_set_data(multi_cmd->cmds[0], buf + offset); } } @@ -148,6 +151,8 @@ static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_si num_of_cmds = 2; else if (ffu_mode == 4) num_of_cmds = 3; + else if (ffu_mode == 5) + num_of_cmds = 1; /* allocate maximum required */ multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + num_of_cmds * sizeof(struct mmc_ioc_cmd)); @@ -169,11 +174,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 @@ -245,7 +253,7 @@ int do_ffu(int nargs, char **argv) break; case 'm': ffu_mode = atoi(optarg); - if (ffu_mode > 4) { + if (ffu_mode > 5) { fprintf(stderr, "Unsupported ffu mode `%d'.\n", ffu_mode); abort(); }