From patchwork Thu Oct 3 18:38:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 832893 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 4009A1AAE37 for ; Thu, 3 Oct 2024 18:39:16 +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=1727980760; cv=pass; b=qyk57KO57TDATJALALzoTR5f6wwljDuX+Ft6Hj1KAZVYIjkyzBZZ9S4Y6BOlMHpxSG9TQZeBDMaT79014v9DUWj0OybLNMxMDNUgBTdpiqdxH9aZZmE0ZxC2YFneL+2AGlLaarP8a8eIgDlcmNRicsSswMjI7GE/LciTTIuxYAQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980760; c=relaxed/simple; bh=VwFqgswiA/G0KBdlrcOQ4cV5ZzlptNG38edXKtZMnas=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=H0GidpVfIuerM+Oqa4+sDfacSK3dVdMHDIjFtSWz7SzqHVIT1xhW9Hk74LH3Tm1Xgm0urDzN+LOnnth4CQooAr4FL3soDe5rQOtzXyiNEeufTYjMLwSOWzyaDo8HEOLgX17e7muHkqHkx6EUNzIx7VezLMgDO/sKkkQc8eKBJjg= 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=dyIVi8ON; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=7zIEJgSy; 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="dyIVi8ON"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="7zIEJgSy" ARC-Seal: i=1; a=rsa-sha256; t=1727980741; cv=none; d=strato.com; s=strato-dkim-0002; b=oTNBx19Mo6I2GnKM2Yl2Qgv7dpbDLui4Etxh0X8ybWQLRt8giYP6XfRddfgiugeVVq vKhRdCrIDyGXBajK3uBUxOt8dGg31qcldO3gfZ/3fPy+C0XYQ0lNqQiM/sBzmzqGVB1B hiJOcOD476S5cuIxo9VWkPMZXm1rUZhmsQgCf1lh1u7yRLx59rNUKs6FC5wxOmhkVyG8 8mUtk4oc+t8iFTtAfoI5lp2kXriiojJY/BhrH8vvHMxBCXw+IxKrCKAzdAReqRIihCFX deoeMG/42seLI+2jNl310KJfvS/l7/PDf1nCzCTJ69Fr7/Pm17JFCjGVaNF6zJ/gj1sI xHmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727980741; 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=FydnqNtOj+4IxmDNBZhTq8lsEJbWZaGtH3to29IHs4LcZvORhkoiB90qqC1oS2GY0p DuhneY5LzLKfqXDa6bSltjLv2/8Y/72o9t6CXkh3kEodwpbL/5n9AEPuBMi4OakKHl+v 7HzayS1XxTcGGc2O2Kay0/anIOiaNjyNf6z01VvFx2Frh4iTrkcxLKTZj4znUB5glDzD GQoVJOals21AefiPcynbCmDJAe/8XaeSIuVHUkaemzthJzoRdOUxGwc6O/1Fdk0vzyR4 fKQssaFeqHQUoGhwLDfreMKy6y5WaoxBZpD2i7RTVLAtehgUsonMl8nWCyFEftI7QgR0 sPjA== 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=1727980741; 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=dyIVi8ONxB+TTFVfPh3BJtTtJ+aIsTbJBQXrqnDXn/OaFik6reHmJJ/QuLyS/6D4QY N8wk01MCFVu8fR/e9MYsEsLtau/4r3IhT8WLKnihUXeFP8P3wBO9PeITcmsOMVuvlQg9 uBiBavm77IJ+r9+ggOp0EruUH7Du0JFzD7B8c3UqoX+W4hpBTsguDSrZIn22shmCVXyD fkNRfVmv+3Sl+qKsoEy9UGI7+Z02Xk0NYAG7smEplIOPLwCKTElx9PA3p6qbzaOyVODw N9CUFJS4VujeDRUQUysonKf5RFE/TCGnNdMMzsB2/t+FqWAXtgWwDi+/DvV9PKS/qKyh gizg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727980741; 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=7zIEJgSyfoPBHUj19kP0kBfomeCETURSiOnVSOJ/rZ1aSBDRZD9gwR0YKHgppJTl8h k51RkvZnoZHRdkeZgHDw== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTu50pjmwgQ1wz9b7T+Mhx2Sk00RyG5Tc=" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0093Id1iL7 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Oct 2024 20:39:01 +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 v2 1/5] mmc_utils: Extract FFU code to mmc_ffu.c for future FFU mode expansion Date: Thu, 3 Oct 2024 20:38:44 +0200 Message-Id: <20241003183848.58626-2-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241003183848.58626-1-beanhuo@iokpp.de> References: <20241003183848.58626-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 Thu Oct 3 18:38:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 832639 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.52]) (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 3F45A1A705E for ; Thu, 3 Oct 2024 18:39:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980760; cv=pass; b=YR2NCYa4oo7Tv7qTzJIp2+F96PNLbcVUqjTiK7kSRPON7R8tmNLP4nfNdTp1i8PgBjC3Rw9g89/LFV9EG//Kg5IwgtdGRvE9rcw3ubhFoCmRMEV4pZ2LUN/jUEMyaq6PFnkr5v0bdtCxilFfzkhy0eieA9gvoQ9xOUlgbA7l2hM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980760; c=relaxed/simple; bh=bhBhXsOg6DjAGu3LgRvweFiDwbDuIOLnqj4gwtNYpgU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=uDQ4jZyWqo7vuc9j0vHto57p+jadHlnRNpZftPO4wFG+SjNHZcWMIEUzZy0Lvxpk0Z4XjoBeQfDREycMMYoauEeu18BQQurEwL2PPw/zUbNnyUxXCeWgBquGEJqACcVxOvNNxacsCXJt53CSBAGDC+NUh0vcNlw1BmYGsYUlVyc= 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=QFyVnGev; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=azTMpIf3; arc=pass smtp.client-ip=85.215.255.52 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="QFyVnGev"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="azTMpIf3" ARC-Seal: i=1; a=rsa-sha256; t=1727980742; cv=none; d=strato.com; s=strato-dkim-0002; b=o/zIGinKLKqJhQZ4JP/OiMhFtuKV6yAtzJDv0qUAyVhqswq8qfH6wqA2rvfajBD3Uj olIxm9YS36QZyKbubkHKEXBW5yGcdQA+g23MXiRH6iEHg+Z4I0gzqhxLf58avFKJ6DU+ t/J4/7zsaz1LYznb5NjIhGOyG0BY+LFbQjDaR2adNyhGMFVJWCTR3uW8UZdsmZR2KZ87 qh/E34g/OfyaOxBup2Qe8XbuewrJDQtkzKavVk1tMmYUNlEdydatxJsS5iuFmHdmCnjD x/6XrdqjylkmRE6YNL2E3QHYDRomQRAZdsWDRrhLfBT7hUX1dbVYlpWhN2t6OrMe5kCy Y+sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727980742; 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=3LhQd2NzaIpZNPv7D6Tw0ngd1hVwt3JFs3SH+5goykg=; b=gONW5onDQSaXMr23Clm7z9PGzw06CS7voog15EbIpcAwe9QUSn3gTQClwAYoSHSWb7 FTPPsAN1MUgfAHWa5ae5RkD3qjUVDoX8Hhv6cVpGc9Mwpa2LUZCqtArtmmVRZCuTw7Xw 5pD4+BznjFjUYtqMGZK4mbl2TTeaXkuhlqP0GLpNofgN1fRIifll3G++GeBZYeFbo3o1 Br5/9c3Cs/ESBR4wgJqOQOUK+3GKHkdSlkRYE/cnTXIOu/9NzbHncCiPXZAN9rkahSSr Iv9nETNJaesGq0P/UOsNXne4ikmTCDghUP3LTTKCB/WGRWpKCnwYC4ED7gMNfKofshtH DFtQ== 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=1727980742; 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=3LhQd2NzaIpZNPv7D6Tw0ngd1hVwt3JFs3SH+5goykg=; b=QFyVnGevX/+lkoL8CTpERS02pUmQ1bilEQNykHqkheaQkd8Q23smFLTxVt5BGnvcPL UHXQQ/6NondxI2JBlnCI6cLzPKXID5itKRAXOeReRe3etLoofKv3FsdcYi75YAMuTaCy 9mSDlxPWH3krbfkPIauWI20Rj58N8hXsZMJPW7ZQTwHRP3YD+NgS3qyWl7dpkfjHj+om QCwwJb30NiZaLt0/dSv8p5VROLZnXZeAo9qxaIsiEMS/29af+Kr8eXf5UiIeKz+ba/W2 5MO0fUsiwMRF1Lua3sLXGkkFhKg+0MTXXyQ5/Z2APc3GGM863KGjgG6k5haJOLpR5nE0 dLww== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727980742; 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=3LhQd2NzaIpZNPv7D6Tw0ngd1hVwt3JFs3SH+5goykg=; b=azTMpIf3QG0iFL914ScHjFMBa4Z/Gu9JgRCdB0OEEWxT1Ti7Yj5C1yoVNMo+Rj7Y/G wSybLZiNR62UlO9R3nCA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTu50pjmwgQ1wz9b7T+Mhx2Sk00RyG5Tc=" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0093Id1iL8 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Oct 2024 20:39:01 +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 v2 2/5] mmc-utils: Add FFU mode 2 Date: Thu, 3 Oct 2024 20:38:45 +0200 Message-Id: <20241003183848.58626-3-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241003183848.58626-1-beanhuo@iokpp.de> References: <20241003183848.58626-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 | 95 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 70 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..52889bd 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,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 faled\n"); + + 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 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 +133,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 +155,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 +166,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 +190,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 +233,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 +294,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 Thu Oct 3 18:38:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 832894 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.52]) (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 3F4B31AAE2B for ; Thu, 3 Oct 2024 18:39:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980759; cv=pass; b=OGjl5wDaJ+1ucuNjC1JK1Sl3WHG1gawlI5UT+yOyfREdSD6LozcuNNoomFdpLZTzve1u04ImMEtQyLQa0e6RQ4AJIRau7Bf+N9IQ2w2rvDRNNFJO7NYXPFMTFaXTHQhIWfPYTbLeXkAA0rdbxIhUQF65i2POVztvFRox8eV/TUY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980759; c=relaxed/simple; bh=jkUGFiyp0lT5+oeJhC4TL/a/N6HWGMi5QAq4LYnDLcI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=HgQ7h5bG0PUoEg8J7fIlO1M8yL+6BNTN9E/nmMZ9B+f77pwszPiioRg/ZKvxYq9P1/zspBFt9ZWYjGUowg9D8rCTH+XTRfbNn3P0dMFRTTyCGC/hOD04ijHf8yrVsHPCpn99V1V4oFcEDZp8TuNqskQDSp5ARo7PjX23MfenBY4= 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=Otex/ao5; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=7wnKUb8U; arc=pass smtp.client-ip=85.215.255.52 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="Otex/ao5"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="7wnKUb8U" ARC-Seal: i=1; a=rsa-sha256; t=1727980742; cv=none; d=strato.com; s=strato-dkim-0002; b=oFrDoNg+GvzOx0/gbTKy0h64jN9p3+SdduY/RdH2Ud36PQoq2ZcGm3fgfenDfoBvTm reaFsgzuXhuIB+Pz6bmHj4/xHTU1Zp/5D4NKUGK4Y3vkoEhMUEPTbcHb0Xv4+231ENHY 6x5C6eDJKG3wGsKCuYJ9FW+tIyGt178ugoQbXmwDMHfs5qu65+KQqZ9Lsglqrg6v3luC T5o892lJzoVTKm6X7QnqkYY0y8keLBSOHbNXSWOgH9m9F03C7So0qLmuiXF9TtMh9210 N8Mgk6hRp9UY5ODOmjHcbaD6oQGx86Y1qOwVSAxFneAW3I34x9Nf6HjibsjUFIfbPICY KfPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727980742; 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=MkXoWQbAmwkIjmFoWl6Vgcn8P8Wm9hIrxAPyXeoXEgc=; b=nYK289YymuqQKDcR5hbU2JJ8ioofRupS5iPTHnUSm/oYlxd+D7o0NbChu5t0jxdOh6 GczoeeWC4nrZpThq5Lp/uGTLTqxjg6syzM2aElgYJ3xyf4sBq7VG8HszVR8DpdS0ZJDW xohO9HXjiYITTkVYjqDoXvY6bSKt5y50mnrCZsyDW96avqytp1+naykMD2dQAxsEkHdU RUFFTNHp/u/0QVn0dMCpzTPukJJ+aJhVVAGifazUG98ZPnBj4wATGZoCfBYXISeNJAG0 xRkr1lyiSpVdNzKrMJ9FSsd6a55rrx06e11ZbGw19154ySLNiWXELG6eaq875lR5qfRF 6jlg== 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=1727980742; 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=MkXoWQbAmwkIjmFoWl6Vgcn8P8Wm9hIrxAPyXeoXEgc=; b=Otex/ao53Mj5IkKGfCwXtT4fIY0qFCdWFf3Rw1w8Hkj89xV/gFqAD57RnkKtSuqo9M wOGpSH4SfW4VvhKjbFozKcQi+vAcFqGk8e1G8agZMwGQNIxdzPvyT8VVa9ntfiA6Th9G oV/3QChLH8OCpVEUTXDwg93WqbKBH7/s66ita9ihmrTp/dTn1R+ciWOr4GBYundEdd7X HTrusR5pI+dmR1nmDQZcAggAkE839Tzi9nBKHfUJhgBbCb5unIpGx6c0UeN5gH/ctH6E o4Z79HH3tCtLPV5IXMd8KHcJ2ke32NN6Fn4913C3PAX8L4+Eez3sruPzhftwo7GY+6Du iX8g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727980742; 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=MkXoWQbAmwkIjmFoWl6Vgcn8P8Wm9hIrxAPyXeoXEgc=; b=7wnKUb8Ujq9+iKb5OZoy4ZaZOBn4jpAuKIuTrycn9Ow8O/lqR6qpCwN4AyjGkj0qAx VG37kafezqM6BbsnF3CA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTu50pjmwgQ1wz9b7T+Mhx2Sk00RyG5Tc=" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0093Id2iL9 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Oct 2024 20:39: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 v2 3/5] mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write download FW Date: Thu, 3 Oct 2024 20:38:46 +0200 Message-Id: <20241003183848.58626-4-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241003183848.58626-1-beanhuo@iokpp.de> References: <20241003183848.58626-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 52889bd..7193a67 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; + } } @@ -233,7 +240,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 Thu Oct 3 18:38:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 832638 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 453471AAE31 for ; Thu, 3 Oct 2024 18:39:15 +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=1727980761; cv=pass; b=GrmkFuvOqbI7bJuGbLQF7tCMUugdx3KB6vn30rPlf9kHqzj1lPDvmO8MLfBSMX4VURm+hkYKr6JI5ObAKjAg3hJ0SWi+xeZNEHX2L5612BZn9QAxeWMrmPDVrQpv5Ujuxv4RQZnv3Je87tuUI/IqcL9BmNmpg9qQxKiSJw93MQ4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980761; c=relaxed/simple; bh=Y/OdHo9Jxg4+XwsqZgXrMTkcc6N0goH4wzV3HWJNOHk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=gQwGoVLbuItbfgaaQ1KsyC91fF7hU/7Exc947dP5mIpWU7924pnP8+9fMRJuV3AEjhqEtiYg81o0LjJ5XAUdKghYhkuy1opquLA5L3Ax3S/7A8dOsjbc25/EB7KWPdNZlmWl2lpaCU9Di36dJbr1Hb6c75kCd3nxpNOxbh0/ZVM= 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=OnCRY4Df; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=b20z5aCC; 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="OnCRY4Df"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="b20z5aCC" ARC-Seal: i=1; a=rsa-sha256; t=1727980742; cv=none; d=strato.com; s=strato-dkim-0002; b=cYvIQoxqn2Me7ffq2AX5Z0mxDBSW+B23J+MtywhXYU/WULX/g7WC8fZCYxwGOckSjr h4C9BTyOD9Sb8fDAkpni7LB0OmVtKbbViac0nAGOS2pvpGAoTRAEDi8jZ+QZVswnsL9Q fL2eEvixfNNjAkUtBnurcpLn9qPfv4q87t60GcOlIHIg8FnAO7FHydEymdDU+oem+vil dZOd65xaaCxf6qgXDk7KRpaKR46gs13LlaK/YhFSJ7hZIiRrNlls7dztIhRC1fXG0EV4 k09BseTscI5B3ZzvpA7nvdon9z+RJxS+OOVJse6dfd4kKVShdUjjKLZA/6QqlbVUdpEE JLMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727980742; 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=5ZM2Gmq8zvT1/ZvzuJODH1RqYsMulQJThH0EpUBZAWE=; b=F8T/qdCSKS3mj4Yn8F3sTXftimzQczJCiDL6qXcbZoQpmvWQO+vg6nsQiyt9pzXKMY TolpA9uCDMXiIPwPXMspuh0TsRtAcrnB1S2deG9zOFFtS3cVgGEn6wXYRGCvJzdLz2uV hg8wW1leLmkjisl0y3Fll+j88AMUNOeAzOSToZ5o+x5vGEjVpEAqm3azDnl0a72UcFV4 Dncb5udB0muG9hoptMttSPjG3wYIMJf2hTnpGFztG+E+u+Nf1D0HRfML2KjLfLnUQPPG uYZIc8LcBMgrhsuAJJdYvbEAj560n+eCCTqLIQaMJZ7tW2a36/t+iYvtkwh22IC7t2bw DcFg== 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=1727980742; 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=5ZM2Gmq8zvT1/ZvzuJODH1RqYsMulQJThH0EpUBZAWE=; b=OnCRY4Df1bX0LewwyP+Bu1lO0xaXe35osj+jZwcz4re4vjOuqI3WAcrT4i/OtHasX4 RzdJjyLMwH7geqd916TkG3JVCdt/XVqo18zGviIblTfLPBM0ZIUNhYfxlAcQhpZLMhtc HT2C+SLF0RT4D6PJu8bbOqquDh0lWACH3EaS2IDAIooFF3rQx5JO/HhQAY8A0YY6evKW 3WOXgx0hPcTeEJiNChwjnukuSAVjWf1YqYLiUkA6fKVNCa7dzE9pshAw9TMCjbLGWhWo M5d6QhpLRZhexE7eFaHMECNH8ZI0rfp7hrebYOGjYhcfB0gTvqW153TOuQM0QNbskGM1 N29Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727980742; 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=5ZM2Gmq8zvT1/ZvzuJODH1RqYsMulQJThH0EpUBZAWE=; b=b20z5aCCOgUUNIRHamTu8aESnDqbLmrv7ZpBApmkwlvorVY0y0FzTV4NmKBIAUcCWu JliZ3AW1XFPuv28jk0DQ== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTu50pjmwgQ1wz9b7T+Mhx2Sk00RyG5Tc=" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0093Id2iLA (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Oct 2024 20:39: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 v2 4/5] mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block firmware download Date: Thu, 3 Oct 2024 20:38:47 +0200 Message-Id: <20241003183848.58626-5-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241003183848.58626-1-beanhuo@iokpp.de> References: <20241003183848.58626-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 | 16 ++++++++++++++-- 2 files changed, 16 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 7193a67..d342c7b 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); } } @@ -141,8 +146,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)); @@ -240,7 +247,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(); } @@ -254,6 +261,11 @@ int do_ffu(int nargs, char **argv) abort(); } } + + if (ffu_mode ==4) + /* FFU mode 4 uses CMD24, the write is limited to single-block operations*/ + default_chunk = 512; + 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); From patchwork Thu Oct 3 18:38:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 832637 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 2F5841A3A9B for ; Thu, 3 Oct 2024 18:42:13 +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=1727980936; cv=pass; b=PONsRiYUVVDqB69DxdrmlFGHnX5AxrG5oj83rJ2svNmWp776mg4GfsHqNL5JCeV0npsK/MUmNd7B6P/t4wfLksFYuuyZ0TV4ZrhK9JltrfFbyD8X0UgO2oQh01xwNbYDoJXuSB6X+bojADFI7gSiyFtZCi3i51KVfhB9l8A0RMg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727980936; c=relaxed/simple; bh=6oAuR3Bzq0V4CSZOhoX/ETkHdfftfYHeRQyT59JK4qw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=Il+fSwNETqlvr6dvRYdDvJERWBaLK7wekEv8MG5kfKPCkOW4wzg+O6ZNYy+PhArEB8EaskNGUp2nyIy+25CiKoTze9//pZ2p4FXCe3TBy5nz/1EhY30Ge/fWLlLdvTQZInzrU+uzg0ecvf20P9ZsrDyzzZFZCLiaiCr5IHyvl9Q= 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=q8AuEU07; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b=GZfnsivO; 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="q8AuEU07"; dkim=permerror (0-bit key) header.d=iokpp.de header.i=@iokpp.de header.b="GZfnsivO" ARC-Seal: i=1; a=rsa-sha256; t=1727980742; cv=none; d=strato.com; s=strato-dkim-0002; b=FGopzb1Pdecx34GDvaeqiGf7A9lqeDD+ng2u1EX3mGlP7SeD74jILKkZqLrFyILRS2 lJf5cMoCaKmLgBsa7B2/56DawLpgogEBIeYqbHpFM/IMhlvg+5k3ZjVdDrlc8qZsnP/v EJ8r0WDrM51JTFerO9MWGexmTuaekYqE53A72XduJuEZa+1kmG5aE8q5TXbsBHdfLuhN uyYOSULVrWiyGRJWF+jLJvZ6M17IJUyaGg3NZwkK43Tmzigpa3yXNYK6C6dtMGNvE6x6 rYorcDDXLOg65mtj978RyT3YLscI8HJkIFbLfwp5oZtEdWU4rnkXywv/LH4wcf+nL+6G BGWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1727980742; 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=zaLkHDv7XSmN/0u6TBU/cvV9wWwvb0noyKbysLTrgWo=; b=tgYrqN6XD4iexCXa7JN9ZdmkDmFG0nFz/wbwzdbjamOThS0O9fcHKTfTXBQ08I3JlS L2LGJvy+vKj1nmNeGr0gzSFdE1DrLOdf+T2c7w05m4vQ16xzUjpFWuiBZbk2D1jVhmtg 1C4MQgBFTUzGonL45Ruk3Muk1bgLkLATgoFDOhetZCy9wHrcY5BdTSO4knOXkJ1oiw6P ol1scspHqe4VcUVMad+SmYc/naz8nYqoaLckJYrWrbfZzGOJIhmF8tuQvVsvY+NNAR0f BzZcAjXKJ0q6MUaWtxzvdAXwnn0iFUB7HHAjRUmYKYKw7etcNlv/SmGz8Uu7dsG3G094 Y76Q== 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=1727980742; 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=zaLkHDv7XSmN/0u6TBU/cvV9wWwvb0noyKbysLTrgWo=; b=q8AuEU07F1fU2ur5XqxvcB8u3Vxr9An+NV8DqgxpNs4s8IhxF+ZkEnJbdJaw3c8kay SpWerwchDhjm+qLcmEY6xDyH7MYlMNAifqe920KMilJcIbcSK+wWp1z01s41+m5Iwokv 2LZoAkNzAPVr3EKmdJi9+K3eilsLRcCY8gO3cdr7lVmuOlv/6yCyCeQJ5Q/t1XpahWUQ wsoE+AWI/Apw9B/aMMxRTxOaVqH2i7EopaRhXMtirJcgI6plidb1XWLVBC1msgqNApoR T0xhQvAQMjB9nTeWEKDefAb2uwFXyNJMICaAsgif9JkYBVNUlhsC+G8P+EP458Gqwxaz juHA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1727980742; 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=zaLkHDv7XSmN/0u6TBU/cvV9wWwvb0noyKbysLTrgWo=; b=GZfnsivOcdwKHbQtcKOKO3NA4zzz5GxBrZme3nVUsyNpwdzbORd6ZAPkHgAyZ19H78 T8yxGVEftv5NwMtQSeCA== X-RZG-AUTH: ":LmkFe0i9dN8c2t4QQyGBB/NDXvjDB6pBSfNuhhDSDt3O256fJ4HnWXON1RD06ISTu50pjmwgQ1wz9b7T+Mhx2Sk00RyG5Tc=" Received: from Munilab01-lab.speedport.ip by smtp.strato.de (RZmta 51.2.8 AUTH) with ESMTPSA id z0e6b0093Id2iLB (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 3 Oct 2024 20:39: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 v2 5/5] mmc-utils: add FFU mode 5 for firmware download using CMD6 and repeated CMD24 command Date: Thu, 3 Oct 2024 20:38:48 +0200 Message-Id: <20241003183848.58626-6-beanhuo@iokpp.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241003183848.58626-1-beanhuo@iokpp.de> References: <20241003183848.58626-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 | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 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 d342c7b..4b2f63b 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); } } @@ -150,6 +153,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)); @@ -171,11 +176,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 @@ -247,7 +255,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(); } @@ -262,8 +270,8 @@ int do_ffu(int nargs, char **argv) } } - if (ffu_mode ==4) - /* FFU mode 4 uses CMD24, the write is limited to single-block operations*/ + if (ffu_mode ==4 || ffu_mode == 5) + /* FFU mode 4/5 uses CMD24, the write is limited to single-block operations*/ default_chunk = 512; fprintf(stderr, "eMMC Devie: %s, fw %s, FFU mode %d, chunk size: %d\n",