Add multiple FFU modes in mmc-utils based on eMMC specification 6.6.18 for flexible firmware updates
v3 cover Add multiple FFU modes in mmc-utils based on eMMC specification 6.6.18 for flexible firmware updates
[Resent since the wrong email address]
Following the discussions with Avri at the 2024 ALPSS, I am submitting these patches to introduce multiple FFU modes, as defined in the eMMC specification 6.6.18.
The new FFU implementation can support diverse vendor requirements and operational conditions. The key benefits include:
1, The host can select the most appropriate update mode based on device capabilities and firmware size.
2, The patches ensure that firmware downloads are treated as a single, uninterrupted operation, preventing partial updates that could compromise device stability.
3, Some new modes keep the device in FFU mode throughout the process, reducing the risk of premature exits and update failures.
By supporting these modes, we can better accommodate varying firmware sizes and ensure smoother, more reliable firmware updates across different scenarios.
Summary of Changes:
FFU mode 1:
Uses CMD6 to enter FFU mode, CMD23 to set the block count, CMD25 for multiple-block write, and CMD6 to exit FFU mode. This mode may exit FFU mode during the download if the firmware size exceeds the chunk size.
FFU mode 2:
Similar to mode 1, but repeats CMD23+CMD25 for each chunk, ensuring FFU mode is maintained throughout the firmware download. FFU mode is only exited after the entire firmware has been successfully downloaded.
FFU mode 3: use CMD25+CMD12 for open-ended multiple-block write
Introduces a method using CMD25 for open-ended multiple-block writes followed by CMD12 to stop the transmission. This allows for a smoother and more continuous firmware bundle download.
FFU mode 4: use CMD6 and CMD24 for single-block write
A new mode that uses CMD6 to enter FFU mode, CMD24 for single-block writes, and CMD6 to exit FFU mode after each write cycle. This ensures granular control over each block but may introduce more frequent mode transitions.
FFU mode 5: use CMD6 and repeated CMD24 sequence
In this mode, CMD6 is used to enter FFU mode, followed by repeated CMD24 single-block writes. After all firmware data is written, CMD6 is used to exit FFU mode, ensuring an atomic and uninterrupted download process.
Changelog:
v1 -- v2:
1. Added memset() to clean command structure
v2 -- V3:
1. Refactor patch, and remove ffu dedicated file mmc_ffu.c:w
Bean Huo (5):
mmc-utils: Refactor common FFU code into functions to support
additional FFU modes
mmc-utils: Add FFU mode 2
mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write
download FW
mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block
firmware download
mmc-utils: add FFU mode 5 for firmware download using repeated CMD24
command to download FW bundle
mmc.1 | 12 ++ mmc.c | 20 +++ mmc.h | 1 + mmc_cmds.c | 389 ++++++++++++++++++++++++++++++++++++----------------- mmc_cmds.h | 4 + 5 files changed, 302 insertions(+), 124 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/5 mmc-utils: Refactor common FFU code into functions to support additional FFU modes
From: Bean Huo <[email protected]>
Refactor common FFU code into functions to support additional FFU modes. Follow-up patches will focus on implementing additional FFU modes and enhancements.
--- mmc_cmds.c | 287 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 171 insertions(+), 116 deletions(-)
mmc_cmds.c +171 -116
@@ -29,6 +29,7 @@#include <stdint.h> #include <assert.h> #include <linux/fs.h> /* for BLKGETSIZE */ +#include <stdbool.h> #include "mmc.h" #include "mmc_cmds.h"
@@ -2810,15 +2811,13 @@ 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) +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 ); +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 ;/*
@@ -2826,23 +2825,141 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,* 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); +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) { +off_t fw_size; +char *device; +int sect_done =0 ;int dev_fd, img_fd; -int retry =3 , ret = -EINVAL ; +int ret = -EINVAL ;unsigned int sect_size;__u8 ext_csd[512 ]; -__u8 *buf =NULL ; -off_t fw_size, bytes_left, off; -char *device; +__u8 *fw_buf =NULL ;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 );
@@ -2852,6 +2969,7 @@ int do_ffu(int nargs, char **argv)perror ("device open failed" );exit (1 ); } + img_fd =open (argv[1 ],O_RDONLY );if (img_fd <0 ) {perror ("image open failed" );
@@ -2859,28 +2977,22 @@ int do_ffu(int nargs, char **argv)exit (1 ); } +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; + } + } + 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); +if (ffu_is_supported (ext_csd, device) != true) goto out; - } fw_size =lseek (img_fd,0 ,SEEK_END );if (fw_size ==0 ) {
@@ -2888,15 +3000,6 @@ int do_ffu(int nargs, char **argv) 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) {
@@ -2904,61 +3007,32 @@ int do_ffu(int nargs, char **argv) 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; - } +/* allocate maximum required */ + fw_buf =malloc (fw_size); +if (!fw_buf) { +perror ("failed to allocate memory" ); + 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) { +if (read (img_fd, fw_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); + sect_done =do_ffu_download ((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk); -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; +/* 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
@@ -2969,48 +3043,29 @@ do_retry: 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); +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; } -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 ); +/* 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[3 ]); +ioctl (dev_fd,MMC_IOC_CMD , &multi_cmd->cmds[1 ]); goto out; }
@@ -3022,16 +3077,16 @@ do_retry:/* return status */ ret = ext_csd[EXT_CSD_FFU_STATUS ]; -if (ret) { +if (ret)fprintf (stderr,"%s: error %d during FFU install:\n" , device, ret); - goto out; - }else { +else fprintf (stderr,"FFU finished successfully\n" ); - } out: -free (buf); -free (multi_cmd); +if (fw_buf) +free (fw_buf); +if (multi_cmd) +free (multi_cmd);close (img_fd);close (dev_fd);return ret;
Signature · 1 line
2.34.1
RE: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 40 lines
mmc_cmds.c | 287 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 171 insertions(+), 116 deletions(-)mmc_cmds.c +6
-5@@ -29,6 +29,7 @@#include <stdint.h> #include <assert.h> #include <linux/fs.h> /* for BLKGETSIZE */ +#include <stdbool.h> #include "mmc.h" #include "mmc_cmds.h" @@ -2810,15 +2811,13 @@ 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) +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 ); +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;/*mmc_cmds.c +0
-0@@ -2826,23 +2825,141 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
mmc_cmds.c static void set_ffu_single_cmd(struct · line 2833
Quoted 10 lines
mmc_cmds.c +2
-2* 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); +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); }
It is not recommended to mix functional & formatting changes.
Quoted 5 lines
mmc_cmds.c +4
-0+static int get_ffu_sectors_programmed (int *dev_fd,__u8 *ext_csd) { +int ret; + + ret =read_extcsd (*dev_fd, ext_csd);
mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, · line 2845
Quoted 10 lines
mmc_cmds.c +10
-0+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; +}
ret is not really needed.
It is not common for the return value to indicate both error and expected functionality.
mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, · line 2850
Quoted 4 lines
mmc_cmds.c +4
-0+ +static bool ffu_is_supported (__u8 *ext_csd,char *device) { +if (ext_csd ==NULL ) { +fprintf (stderr,"ext_cst is NULL\n" );
^^^^
s/cst/csd/g
Quoted 16 lines
mmc_cmds.c +16
-0+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);
mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
Quoted 10 lines
mmc_cmds.c +9
-0+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) {
Why dev_fd can't be just an integer?
Quoted 7 lines
mmc_cmds.c +7
-0+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) {
mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, · line 2891
Quoted 10 lines
mmc_cmds.c +9
-0+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 ; + }
I was expecting that do_ffu_download will be called with struct mmc_ioc_multi_cmd *multi_cmd argument as well. That each ffu<x> mode fills it according to its own logic.
This you won't be needing that ffu_mode additional parameter.
Quoted 73 lines
mmc_cmds.c +39
-0+ +/* 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_doneis 0 */mmc_cmds.c +4
-0+ioctl (*dev_fd,MMC_IOC_CMD , &multi_cmd->cmds[3 ]); +if (retry >0 ) { + retry--; + fprintf(stderr, "Programming failed. Retrying... (%d)\n",retry);
mmc_cmds.c +7
-0+ 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);
mmc_cmds.c +16
-1+ } + + 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) { +off_t fw_size; +char *device; +int sect_done =0 ;int dev_fd, img_fd; -int retry =3 , ret = -EINVAL ;
mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, · line 2845
Quoted 10 lines
mmc_cmds.c +2
-4+int ret = -EINVAL ;unsigned int sect_size;__u8 ext_csd[512 ]; -__u8 *buf =NULL ; -off_t fw_size, bytes_left, off; -char *device; +__u8 *fw_buf =NULL ;struct mmc_ioc_multi_cmd *multi_cmd =NULL ;unsigned int default_chunk =MMC_IOC_MAX_BYTES ; -__u32 sect_done =0 ;
It is not recommended to mix functional & formatting changes.
Quoted 19 lines
assert (nargs == 3 || nargs == 4);mmc_cmds.c +1
-0@@ -2852,6 +2969,7 @@ int do_ffu(int nargs, char **argv)perror ("device open failed" );exit (1 ); } + img_fd =open (argv[1 ],O_RDONLY );if (img_fd <0 ) { perror("image open failed"); @@ -2859,28 +2977,22 @@ int
do_ffu(int nargs, char **argv)exit(1); }mmc_cmds.c +3
-0+ if (nargs == 4) { + default_chunk = strtol(argv[3], NULL, 10); + if (default_chunk > MMC_IOC_MAX_BYTES || default_chunk %512) {
mmc_cmds.c int do_ffu(int nargs, char **argv) · line 2987
Quoted 5 lines
mmc_cmds.c +5
-0+fprintf (stderr,"Invalid chunk size" ); + goto out; + } + } +
Can the argument parsing part be common to all modes as well?
Quoted 5 lines
ret = read_extcsd(dev_fd, ext_csd);
if (ret) {
fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
goto out;
}can this part also be part of ffu_is_supported()?
Quoted 6 lines
mmc_cmds.c +0
-5-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;
mmc_cmds.c int do_ffu(int nargs, char **argv) · line 2994
Quoted 10 lines
mmc_cmds.c +1
-9- } - -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); +if (ffu_is_supported (ext_csd, device) != true)
If (!ffu_is_supported(ext_csd, device))
Quoted 154 lines
goto out;mmc_cmds.c +16
-63- } fw_size =lseek (img_fd,0 ,SEEK_END );if (fw_size ==0 ) {@@ -2888,15 +3000,6 @@ int do_ffu(int nargs, char **argv) 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) {@@ -2904,61 +3007,32 @@ int do_ffu(int nargs, char **argv) 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; - } +/* allocate maximum required */ + fw_buf =malloc (fw_size); +if (!fw_buf) { +perror ("failed to allocate memory" ); + 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) { +if (read (img_fd, fw_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); + sect_done =do_ffu_download ((int *)&dev_fd, ext_csd, fw_buf, + fw_size, default_chunk); -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; +/* 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 @@ -2969,48 +3043,29 @@ do_retry: 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_doneis 0 */mmc_cmds.c +0
-11-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);
mmc_cmds.c +0
-4-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);
mmc_cmds.c +2
-0+fprintf (stderr,"Installing firmware on %s...\n" , device); + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) + 2 *
sizeof(struct mmc_ioc_cmd));mmc_cmds.c +3
-4+if (!multi_cmd) { +perror ("failed to allocate memory" ); + ret = -ENOMEM ; 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,mmc_cmds.c +2
-1- EXT_CSD_FFU_INSTALL); +/* put device into ffu mode */ + fill_switch_cmd(&multi_cmd->cmds[0], EXT_CSD_MODE_CONFIG,
EXT_CSD_FFU_MODE);
mmc_cmds.c do_retry: · line 3062
Quoted 6 lines
mmc_cmds.c +3
-0+/* 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);
Can wrapping up be common to all modes as well?
Thanks,
Avri
Quoted 36 lines
if (ret) { perror("Multi-cmd ioctl failed setting install mode");mmc_cmds.c +9
-8+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[3 ]); +ioctl (dev_fd,MMC_IOC_CMD , &multi_cmd->cmds[1 ]); goto out; }@@ -3022,16 +3077,16 @@ do_retry:/* return status */ ret = ext_csd[EXT_CSD_FFU_STATUS ]; -if (ret) { +if (ret)fprintf (stderr,"%s: error %d during FFU install:\n" , device, ret); - goto out; - }else { +else fprintf (stderr,"FFU finished successfully\n" ); - } out: -free (buf); -free (multi_cmd); +if (fw_buf) +free (fw_buf); +if (multi_cmd) +free (multi_cmd);close (img_fd);close (dev_fd);return ret; --2.34.1
Re: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 16 lines · Avri Altman
Quoted 8 lines
+
__u8num_of_cmds= 4;
+off_tbytes_left, off;
+ unsigned intbytes_per_loop,sect_done, retry = 3; + structmmc_ioc_multi_cmd*multi_cmd= NULL; +
+ if (!dev_fd || !fw_buf || !ext_csd) {
+fprintf(stderr, "unexpected NULL pointer\n"); + return-EINVAL;Quoted 8 lines
+ }
+ /* 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;
+ }
Quoted 4 lines
I was expecting that
do_ffu_downloadwill be called with structmmc_ioc_multi_cmd*multi_cmdargument as well.
That each ffu<x> mode fills it according to its own logic. This you won't be needing thatffu_modeadditional parameter.
I wanted to clarify why the ffu_mode parameter is necessary during the download phase. By extracting the logic into a common approach and using ffu_mode to dynamically update the multi_cmd structure across iterations per ffu-mode, I can handle the variations between different FFU modes more effectively. This allows me to extract more common code and avoid duplication, as the ffu_mode helps determine which specific updates or adjustments need to be made to the multi_cmd at each stage.
Without this, it would be difficult to manage multiple loops or iterations of the download process, especially when the command structure needs to be modified in different ways depending on the mode. The use of ffu_mode centralizes this control, making the code cleaner and more maintainable.
Kind regards,
Bean
RE: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 30 lines · Avri Altman
Quoted 16 lines
Quoted 12 lines
mmc_cmds.c +11
-0+__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));mmc_cmds.c static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd, · line 2891
Quoted 4 lines
mmc_cmds.c +4
-0+if (!multi_cmd) { +perror ("failed to allocate memory" ); +return -ENOMEM ; + }Quoted 4 lines
I was expecting that
do_ffu_downloadwill be called with structmmc_ioc_multi_cmd*multi_cmdargument as well.
That each ffu<x> mode fills it according to its own logic. This you won't be needing thatffu_modeadditional parameter.I wanted to clarify why the
ffu_modeparameter is necessary during the download phase. By extracting the logic into a common approach and usingffu_modeto dynamically update themulti_cmdstructure across iterations per ffu-mode, I can handle the variations between different FFU modes more effectively. This allows me to extract more common code and avoid duplication, as theffu_modehelps determine which specific updates or adjustments need to be made to themulti_cmdat each stage.
Quoted 5 lines
Without this, it would be difficult to manage multiple loops or iterations of the download process, especially when the command structure needs to be modified in different ways depending on the mode.
The use offfu_modecentralizes this control, making the code cleaner and more maintainable.
I see your point.
Each mode is packing differently the multi-ioctl, and you need to update different offset of the cmds part. So how about instead of ffu mode, adjust set_ffu_single_cmd to do just that.
And maybe in few preparation phases - to make the review process more concise:
First let's make it get the cmds part of the multi-ioctl instead of the multi-ioctl:
+static void set_ffu_write_cmd(struct mmc_ioc_cmd *cmds, __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;
+ set_single_cmd(&cmds[0], MMC_SET_BLOCK_COUNT, 0, 0, bytes / 512);
+ cmds[0].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); + set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg); + mmc_ioc_cmd_set_data(cmds[1], buf + offset);
+}
And then, allow it to carry sbc or not:
+static void set_ffu_sbc_cmd(struct mmc_ioc_cmd *cmds, int bytes)
{
- __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; + set_single_cmd(&cmds[0], MMC_SET_BLOCK_COUNT, 0, 0, bytes / 512); + cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+}
+static void set_ffu_write_cmd(struct mmc_ioc_cmd *cmds, __u8 *ext_csd, unsigned int bytes,
+ __u8 *buf, off_t offset, bool sbc)
+{
+ __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
+
+ if (sbc)
+ set_ffu_sbc_cmd(cmds, bytes);
/*
* 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); + set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg); + mmc_ioc_cmd_set_data(cmds[1], buf + offset);
+}
+
And only then, as a 3rd preparation phase move the do_retry out.
And you need to tell it what offset the write commands are. I guess this is the equivalent of your ffu_mode. But the difference is that when I look into do_ffu2, do_ffu3 etc. - The logic of pointing to the write command is there.
+static int __do_ffu(int dev_fd, char *device, struct mmc_ioc_multi_cmd *multi_cmd, int cmd_off,
+ off_t fw_size, unsigned int sect_size, unsigned int default_chunk,
+ __u8 *ext_csd, __u8 *buf)
Maybe we can pack all the ffu arguments into a single control struct to get passed to __do_ffu as struct ffu_params *.
+{
+ off_t bytes_left, off;
+ __u32 sect_done = 0;
+ __u8 ffu_feature = ext_csd[EXT_CSD_FFU_FEATURES];
+ int write_command_offset = cmd_off;
+ int retry = 3;
+ int ret;
+
+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_write_cmd(&multi_cmd->cmds[write_command_offset],
+ ext_csd, chunk_size, buf, off, 1);
+
+ /* 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]);
+ return ret;
+ }
+
+ 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 (!ffu_feature) {
+ fprintf(stderr, "Please reboot to complete firmware installation on %s\n", device);
+ return 0;
+ }
+
+ ret = read_extcsd(dev_fd, ext_csd);
+ if (ret) {
+ fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
+ return ret;
+ }
+
+ /* 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");
+ return -EINVAL;+ }
+
+ fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)sect_done * sect_size, (intmax_t)fw_size);
+
+ if ((sect_done * sect_size) == fw_size) {
+ fprintf(stderr, "Programming finished successfully\n");
+ return 0;
+ }
+
+ fprintf(stderr, "FW size and number of sectors written mismatch\n");
+ return -EINVAL;
}
Each mode can have its own arguments - it’s a different de-facto util so you are not bounded by do_ffu arguments. Thus the argument parsing is done for each mode differently.
Also allocating the multi-ioctl is privet to each mode. Some have 4, some have 2, and some 1.
Thanks,
Avri
Quoted 3 lines
Kind regards,
Bean
Re: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 214 lines · Avri Altman
Quoted 20 lines
Quoted 6 lines
Quoted 2 lines
+ return
-ENOMEM;
+ }I was expecting that
do_ffu_downloadwill be called with structmmc_ioc_multi_cmd*multi_cmdargument as well.
That each ffu<x> mode fills it according to its own logic. This you won't be needing thatffu_modeadditional parameter.I wanted to clarify why the
ffu_modeparameter is necessary during the
download phase. By extracting the logic into a common approach and usingffu_modeto dynamically update themulti_cmdstructure across iterations
per ffu-mode, I can handle the variations between different FFU modes more
effectively. This allows me to extract more common code and avoid duplication, as theffu_modehelps determine which specific updates or
adjustments need to be made to themulti_cmdat each stage.Quoted 8 lines
Without this, it would be difficult to manage multiple loops or iterations of
the download process, especially when the command structure needs to be
modified in different ways depending on the mode.
The use offfu_modecentralizes this control, making the code cleaner and
more maintainable.I see your point.
Each mode is packing differently the multi-ioctl, and you need to update different offset of the cmds part.
So how about instead of ffu mode, adjustset_ffu_single_cmdto do just that.And maybe in few preparation phases - to make the review process more concise:
First let's make it get the cmds part of the multi-ioctl instead of the multi-ioctl:
+static void set_ffu_write_cmd(structmmc_ioc_cmd*cmds,__u8*ext_csd, unsigned int bytes,
+__u8*buf,off_toffset)
{
__u32arg =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;
+set_single_cmd(&cmds[0], MMC_SET_BLOCK_COUNT, 0, 0, bytes / 512);
+ cmds[0].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 isfw_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); +set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg);
+mmc_ioc_cmd_set_data(cmds[1], buf + offset);
+}And then, allow it to carry sbc or not:
+static voidset_ffu_sbc_cmd(struct mmc_ioc_cmd *cmds, int bytes){
-__u32arg =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;
+set_single_cmd(&cmds[0], MMC_SET_BLOCK_COUNT, 0, 0, bytes / 512);
+ cmds[0].flags =MMC_RSP_SPI_R1|MMC_RSP_R1|MMC_CMD_AC;mmc_cmds.c +2
-0+} +static void set_ffu_write_cmd(struct mmc_ioc_cmd *cmds, __u8
*ext_csd, unsigned int bytes,
+__u8*buf,off_toffset, bool sbc)(quoted diff) +5
-0+{ + __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]); + + if (sbc) + set_ffu_sbc_cmd(cmds, bytes);/*
* send image chunk: blksz and blocks essentially do not matter, as
* long as the product isfw_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); +set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes / 512, arg);
+mmc_ioc_cmd_set_data(cmds[1], buf + offset);(quoted diff) +2
-0+} +And only then, as a 3rd preparation phase move the
do_retryout. And you need to tell it what offset the write commands are. I guess this is the equivalent of yourffu_mode.
But the difference is that when I look intodo_ffu2,do_ffu3etc. - The logic of pointing to the write command is there.+static int __do_ffu(int
dev_fd, char *device, structmmc_ioc_multi_cmd*multi_cmd, intcmd_off,
+off_tfw_size, unsigned intsect_size, unsigned intdefault_chunk,
+__u8*ext_csd,__u8*buf)
Maybe we can pack all the ffu arguments into a single control struct to get passed to__do_ffuas structffu_params*.mmc_cmds.c +13
-0+{ + off_t bytes_left, off; + __u32 sect_done = 0; + __u8 ffu_feature = ext_csd[EXT_CSD_FFU_FEATURES]; + int write_command_offset = cmd_off; + int retry = 3; + int ret; + +do_retry: + bytes_left = fw_size; + off = 0; + while (bytes_left) { + unsigned int chunk_size = bytes_left < default_chunk?
+bytes_left:default_chunk; +
+ /* preparemulti_cmdfor FFU based on cmd to be used */
+ set_ffu_write_cmd(&multi_cmd-Quoted 1 line
cmds[write_command_offset],
+
ext_csd,chunk_size, buf, off, 1);
+
+ /* 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-Quoted 1 line
cmds[3]);
+ return ret;
+ }mmc_cmds.c +7
-0+ + 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 (!ffu_feature) {
+fprintf(stderr, "Please reboot to complete firmware installation on %s\n", device);
+ return 0;
+ }
+
+ ret =read_extcsd(dev_fd, ext_csd);
+ if (ret) {
+fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
+ return ret;
+ }
+
+ /* 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 ifsect_doneis 0 */
+ if (sect_done== 0) {
+ if (retry--) {
+ fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry);
+ gotodo_retry;
+ }
+fprintf(stderr, "Programming failed! Aborting...\n"); + return-EINVAL;
+ }
+
+fprintf(stderr, "Programmed %jd/%jd bytes\n", (intmax_t)sect_done * sect_size, (intmax_t)fw_size);
+
+ if ((sect_done*sect_size) ==fw_size) {
+fprintf(stderr, "Programming finished successfully\n");
+ return 0;
+ }
+
+fprintf(stderr, "FW size and number of sectors written mismatch\n");
+ return-EINVAL;
}
Quoted 8 lines
Each mode can have its own arguments - it’s a different de-facto util so you are not bounded by
do_ffuarguments.
Thus the argument parsing is done for each mode differently. Also allocating the multi-ioctl is privet to each mode. Some have 4, some have 2, and some 1.Thanks,
Avri
I can change your suggestions, before changing, I would like to have a solid agreement,
The approach in my patch is to concentrate differences in do_ffu_download() while keeping common functions outside it, the reason for this becuase the main difference btw ffu_modes are FW budnle download.
void _do_ffu(int ffu_mode) {
// Declare parameters specific to the FFU mode
do_ffu_prepare();
ffu_is_supported();
do_ffu_download(ffu_mode);
do_ffu_installation();
}void do_ffu1() {
_do_ffu(FFU_MODE_1);
}void do_ffu2() {
_do_ffu(FFU_MODE_2);
}Advantages:
Reduced Duplication: Each do_ffu<x>() avoids repeating the same logic. Improved Maintainability: Changes only need to be made in one location. Clear Separation: Common operations remain consistent across all modes.
The approach you suggested is to sperate them at the begigning of do_ffu<x>, then pass the different parameters to each different function:
It risks unnecessary duplication and increased complexity, as seen in this structure:
void do_ffu1() {
// Duplicate logic here
do_ffu_prepare(ffu1);
ffu_is_supported();
do_ffu_download(ffu_structure);
do_ffu_installation();
}void do_ffu2() {
// Duplicate logic here
do_ffu_prepare(ffu2);
ffu_is_supported();
do_ffu_download(ffu_structure);
do_ffu_installation();
}Please tell me was the second one what you expected?
Kind regards,
Bean
RE: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 275 lines · Avri Altman
Quoted 48 lines
Quoted 16 lines
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return -ENOMEM; + }I was expecting that
do_ffu_downloadwill be called with structmmc_ioc_multi_cmd*multi_cmdargument as well.
That each ffu<x> mode fills it according to its own logic. This you won't be needing thatffu_modeadditional parameter.I wanted to clarify why the
ffu_modeparameter is necessary during the download phase. By extracting the logic into a common approach and usingffu_modeto dynamically update themulti_cmdstructure across iterations per ffu-mode, I can handle the variations between different FFU modes more effectively. This allows me to extract more common code and avoid duplication, as theffu_modehelps determine which specific updates or adjustments need to be made to themulti_cmdat each stage.Quoted 6 lines
Without this, it would be difficult to manage multiple loops or iterations of the download process, especially when the command structure needs to be modified in different ways depending on the mode.
The use offfu_modecentralizes this control, making the code cleaner and more maintainable.I see your point.
Each mode is packing differently the multi-ioctl, and you need to update different offset of the cmds part.
So how about instead of ffu mode, adjustset_ffu_single_cmdto do just that.And maybe in few preparation phases - to make the review process more concise:
First let's make it get the cmds part of the multi-ioctl instead of the multi-ioctl:
+static void set_ffu_write_cmd(structmmc_ioc_cmd*cmds,__u8*ext_csd, unsigned int bytes,mmc_cmds.c +1
-1+ __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,
(quoted diff) +0
-2- bytes / 512); - multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
MMC_CMD_AC;
+ set_single_cmd(&cmds[0],MMC_SET_BLOCK_COUNT, 0, 0, bytes /
512);
+ cmds[0].flags =MMC_RSP_SPI_R1|MMC_RSP_R1|MMC_CMD_AC;Quoted 8 lines
/*
* send image chunk: blksz and blocks essentially do not
matter, as
* long as the product isfw_size, but some hosts don't handle
larger
* blksz well.
*/
- set_single_cmd(&multi_cmd->cmds[2],
MMC_WRITE_MULTIPLE_BLOCK,Quoted 31 lines
1,
(quoted diff) +1
-2- bytes / 512, arg); - mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf + offset); + set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes /512, arg);
+mmc_ioc_cmd_set_data(cmds[1], buf + offset); }And then, allow it to carry sbc or not:
(quoted diff) +1
-3+static void set_ffu_sbc_cmd(struct mmc_ioc_cmd *cmds, int bytes) { - __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,
(quoted diff) +0
-2- bytes / 512); - multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
MMC_CMD_AC;
+ set_single_cmd(&cmds[0],MMC_SET_BLOCK_COUNT, 0, 0, bytes /
512);(quoted diff) +2
-0+ cmds[0].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; } +static void set_ffu_write_cmd(struct mmc_ioc_cmd *cmds, __u8
*ext_csd, unsigned int bytes,(quoted diff) +5
-0+ __u8 *buf, off_t offset, bool sbc) { + __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]); + + if (sbc) + set_ffu_sbc_cmd(cmds, bytes); /* * send image chunk: blksz and blocks essentially do notQuoted 6 lines
matter, as
* long as the product isfw_size, but some hosts don't handle
larger
* blksz well.
*/
- set_single_cmd(&multi_cmd->cmds[2],
MMC_WRITE_MULTIPLE_BLOCK,Quoted 111 lines
1,
(quoted diff) +1
-2- bytes / 512, arg); - mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf + offset); + set_single_cmd(&cmds[1], MMC_WRITE_MULTIPLE_BLOCK, 1, bytes /512, arg);
(quoted diff) +2
-0+ mmc_ioc_cmd_set_data(cmds[1], buf + offset); } +And only then, as a 3rd preparation phase move the
do_retryout. And you need to tell it what offset the write commands are. I guess this is the equivalent of yourffu_mode.
But the difference is that when I look intodo_ffu2,do_ffu3etc. - The logic of pointing to the write command is there.+static int __do_ffu(int
dev_fd, char *device, structmmc_ioc_multi_cmd*multi_cmd, intcmd_off,
+off_tfw_size, unsigned intsect_size, unsigned
intdefault_chunk,
+__u8*ext_csd,__u8*buf)
Maybe we can pack all the ffu arguments into a single control struct to get passed to__do_ffuas structffu_params*.mmc_cmds.c +13
-0+{ +off_t bytes_left, off; +__u32 sect_done =0 ; +__u8 ffu_feature = ext_csd[EXT_CSD_FFU_FEATURES ]; +int write_command_offset = cmd_off; +int retry =3 ; +int ret; + +do_retry: + bytes_left = fw_size; + off =0 ; +while (bytes_left) { +unsigned int chunk_size = bytes_left < default_chunk?
(quoted diff) +3
-0+ bytes_left : default_chunk; + + /* prepare multi_cmd for FFU based on cmd to be used*/
+ set_ffu_write_cmd(&multi_cmd-Quoted 1 line
cmds[write_command_offset],
+
ext_csd,chunk_size, buf,
off, 1);mmc_cmds.c +7
-0+ +/* send ioctl with multi-cmd */ + ret =ioctl (dev_fd,MMC_IOC_MULTI_CMD , multi_cmd); +if (ret) { +perror ("Multi-cmd ioctl" ); +/* +* In case multi-cmd ioctl failed before exiting from
mmc_cmds.c +3
-0+* ffu mode +*/ + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd-Quoted 1 line
cmds[3]);
mmc_cmds.c +9
-0+return ret; + } + + 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(quoted diff) +3
-0+ */ + if (!ffu_feature) { + fprintf(stderr, "Please reboot to complete firmwareinstallation on %s\n", device);
mmc_cmds.c +6
-0+return 0 ; + } + + ret =read_extcsd (dev_fd, ext_csd); +if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n",device);
mmc_cmds.c +5
-0+return ret; + } + +/* 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 ifsect_doneis 0 */(quoted diff) +3
-0+ if (sect_done == 0) { + if (retry--) { + fprintf(stderr, "Programming failed.Retrying... (%d)\n", retry);
mmc_cmds.c +7
-0+ goto do_retry; + } +fprintf (stderr,"Programming failed! Aborting...\n" ); +return -EINVAL ; + } + + fprintf(stderr, "Programmed %jd/%jd bytes\n",(intmax_t)sect_done *
sect_size, (intmax_t)fw_size);(quoted diff) +3
-0+ + if ((sect_done * sect_size) == fw_size) { + fprintf(stderr, "Programming finishedsuccessfully\n");
mmc_cmds.c +4
-0+return 0 ; + } + + fprintf(stderr, "FW size and number of sectors writtenmismatch\n");
+ return-EINVAL;
}Quoted 8 lines
Each mode can have its own arguments - it’s a different de-facto util so you are not bounded by
do_ffuarguments.
Thus the argument parsing is done for each mode differently. Also allocating the multi-ioctl is privet to each mode. Some have 4, some have 2, and some 1.Thanks,
AvriI can change your suggestions, before changing, I would like to have a solid agreement,
The approach in my patch is to concentrate differences in
do_ffu_download()while keeping common functions outside it, the reason for this becuase the main difference btwffu_modesare FW budnle download.void
_do_ffu(int ffu_mode){// Declare parameters specific to the FFU mode do_ffu_prepare(); ffu_is_supported();do_ffu_download(ffu_mode); do_ffu_installation(); }void do_ffu1() { _do_ffu(FFU_MODE_1); }void do_ffu2() { _do_ffu(FFU_MODE_2); }Advantages:
Reduced Duplication: Each do_ffu<x>() avoids repeating the same logic. Improved Maintainability: Changes only need to be made in one location. Clear Separation: Common operations remain consistent across all modes.
The approach you suggested is to sperate them at the begigning of do_ffu<x>, then pass the different parameters to each different function:
It risks unnecessary duplication and increased complexity, as seen in this structure:
void do_ffu1() { // Duplicate logic here do_ffu_prepare(ffu1); ffu_is_supported(); do_ffu_download(ffu_structure); do_ffu_installation(); }void do_ffu2() { // Duplicate logic here do_ffu_prepare(ffu2); ffu_is_supported(); do_ffu_download(ffu_structure); do_ffu_installation(); }
Quoted 1 line
Please tell me was the second one what you expected?
I see that you still think that the ffu_mode is better.
I don't want to block the progress of the series - as I am aware that there are customers that are waiting. Please feel free to add my Acked-by tag to the whole series.
Thanks,
Avri
Quoted 4 lines
Kind regards,
Bean
Re: [RESENT PATCH v3 1/5] mmc-utils: Refactor common FFU code into functions to support additional FFU modes
Quoted 32 lines · Avri Altman
Quoted 32 lines
Reduced Duplication: Each do_ffu<x>() avoids repeating the same logic.
Improved Maintainability: Changes only need to be made in one location.
Clear Separation: Common operations remain consistent across all modes.The approach you suggested is to sperate them at the begigning of do_ffu<x>, then pass the different parameters to each different function:
It risks unnecessary duplication and increased complexity, as seen in this
structure:void
do_ffu1(){
// Duplicate logic here
do_ffu_prepare(ffu1);
ffu_is_supported();
do_ffu_download(ffu_structure);
do_ffu_installation();
}void
do_ffu2(){
// Duplicate logic here
do_ffu_prepare(ffu2);
ffu_is_supported();
do_ffu_download(ffu_structure);
do_ffu_installation();
}
Quoted 8 lines
Quoted 1 line
Please tell me was the second one what you expected?
I see that you still think that the
ffu_modeis better.
I don't want to block the progress of the series - as I am aware that there are customers that are waiting.
Please feel free to add my Acked-by tag to the whole series.Thanks,
Avri
Thanks, I plan to send out the v4 patch this week and will incorporate your inputs inline as possible as it can.
Kind regards,
Bean
PATCH v3 2/5 mmc-utils: Add FFU mode 2
In reply to Bean HuoFrom: Bean Huo <[email protected]>
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.
--- mmc.1 | 3 ++ mmc.c | 5 +++ mmc_cmds.c | 92 +++++++++++++++++++++++++++++++++++++++++------------- mmc_cmds.h | 1 + 4 files changed, 79 insertions(+), 22 deletions(-)
mmc.1 +3 -0
@@ -192,6 +192,9 @@ Run Field Firmware Update with \fIimage\-file\-name\fR on the device. .br if [\fIchunk\-bytes\fR] is omitted, mmc-utils will try to run ffu using the largest possible chunks: max(image-file, 512k). .TP +.BI ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br
mmc.c +5 -0
@@ -234,6 +234,11 @@ static struct Command commands[] = {"should be in decimal bytes and sector aligned.\n" ,NULL }, + { do_ffu2, -2, +"ffu2" ,"<image name> <device> [chunk-bytes]\n" +"Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion.\n" , +NULL + }, { do_erase, -4,"erase" ,"<type> " "<start address> " "<end address> " "<device>\n" "Send Erase CMD38 with specific argument to the <device>\n\n"
mmc_cmds.c +70 -22
158 lines not on this page · raw message
mmc_cmds.h +1 -0
@@ -42,6 +42,7 @@ int do_rpmb_write_block(int nargs, char **argv);int do_cache_en (int nargs,char **argv);int do_cache_dis (int nargs,char **argv);int do_ffu (int nargs,char **argv); +int do_ffu2 (int nargs,char **argv);int do_read_scr (int argc,char **argv);int do_read_cid (int argc,char **argv);int do_read_csd (int argc,char **argv);
Signature · 1 line
2.34.1
RE: [RESENT PATCH v3 2/5] mmc-utils: Add FFU mode 2
mmc_cmds.c out: · line 2837
Quoted 10 lines
(quoted diff) +2
-0+ } 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_cmds.c +3
-0+mmc_ioc_cmd_set_data (multi_cmd->cmds[1 ], buf + offset); + } +}
Actually, I was hoping to avoid the "ffu_mode" extra argument. So that each do_ffu<x> has its own fluf encapsulation logic.
Thanks,
Avri
Re: [RESENT PATCH v3 2/5] mmc-utils: Add FFU mode 2
Quoted 13 lines · Avri Altman
Quoted 5 lines
+ } 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|Quoted 8 lines
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);
+ }
+}
Quoted 5 lines
Actually, I was hoping to avoid the "
ffu_mode" extra argument. So that each do_ffu<x> has its own fluf encapsulation logic.Thanks,
Avri
Thank you for the suggestion. After reviewing the approach, I believe it's not feasible to encapsulate the above code logic entirely within each do_ffu<x> function.
The primary reason is that during the download phase, especially when multiple loops are required, I need to update certain state variables dynamically, such as the ffu_mode. This allows for handling the different stages of the process efficiently across iterations. The encapsulation logic would prevent us from adjusting these variables between loops, which is crucial for ensuring the proper progression and handling of larger data sets.
Without this flexibility, we'd run into issues when the process requires multiple passes or loops, making it difficult to adjust the behavior mid-operation.
Let me know if you have any further suggestions, or if you'd like to discuss alternate solutions.
Kind regards,
Bean
RE: [RESENT PATCH v3 2/5] mmc-utils: Add FFU mode 2
In reply to Bean HuoQuoted 13 lines
mmc_cmds.c +5
-6+static int enter_ffu_mode (int *dev_fd) +{ +int ret; +struct mmc_ioc_cmd cmd; +memset (&cmd,0 ,sizeof (cmd)); -/* -* 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_cmds.c out: · line 2850
Quoted 8 lines
mmc_cmds.c +7
-1-mmc_ioc_cmd_set_data (multi_cmd->cmds[2 ], buf + offset); +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; +}
Thinking aloud if we need the enter phase at all, because:
1) The first cmd6 is always part of the sequence, and
2) is it even safe to send it as a single ioctl?
I guess the answer to 2) is yes, and having a start phase actually make sense:
it makes clearer of what the ffu process comprised of:
Start (enter ffu mode), repeat...., finish up.
I would even add a comment in the code (and in the commit log) for all modes other than 1, emphasizing that from this point onward, the device is blocked in ffu mode.
Thanks,
Avri
PATCH v3 3/5 mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write download FW
In reply to Bean HuoFrom: Bean Huo <[email protected]>
Introduced a new FFU mode leveraging CMD25+CMD12 for Open-ended Multiple-block write to download the firmware bundle.
--- mmc.1 | 3 +++ mmc.c | 5 +++++ mmc.h | 1 + mmc_cmds.c | 10 ++++++++++ mmc_cmds.h | 1 + 5 files changed, 20 insertions(+)
mmc.1 +3 -0
@@ -195,6 +195,9 @@ if [\fIchunk\-bytes\fR] is omitted, mmc-utils will try to run ffu using the larg .BI ffu2 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion. .TP +.BI ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br
mmc.c +5 -0
@@ -239,6 +239,11 @@ static struct Command commands[] = {"Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mode until completion.\n" ,NULL }, + { do_ffu3, -2, +"ffu3" ,"<image name> <device> [chunk-bytes]\n" +"Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n" , +NULL + }, { do_erase, -4,"erase" ,"<type> " "<start address> " "<end address> " "<device>\n" "Send Erase CMD38 with specific argument to the <device>\n\n"
mmc.h +1 -0
@@ -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 */
mmc_cmds.c +10 -0
@@ -2833,6 +2833,12 @@ 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 ; } }static int enter_ffu_mode (int *dev_fd)
@@ -3140,6 +3146,10 @@ int do_ffu2(int nargs, char **argv) {return __do_ffu (nargs, argv,2 ); } +int do_ffu3 (int nargs,char **argv) { +return __do_ffu (nargs, argv,3 ); +} +int do_general_cmd_read (int nargs,char **argv) {int dev_fd;
mmc_cmds.h +1 -0
@@ -43,6 +43,7 @@ int do_cache_en(int nargs, char **argv);int do_cache_dis (int nargs,char **argv);int do_ffu (int nargs,char **argv);int do_ffu2 (int nargs,char **argv); +int do_ffu3 (int nargs,char **argv);int do_read_scr (int argc,char **argv);int do_read_cid (int argc,char **argv);int do_read_csd (int argc,char **argv);
Signature · 1 line
2.34.1
PATCH v3 4/5 mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block firmware download
In reply to Bean HuoFrom: Bean Huo <[email protected]>
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.
--- mmc.1 | 3 +++ mmc.c | 5 +++++ mmc_cmds.c | 17 ++++++++++++++++- mmc_cmds.h | 1 + 4 files changed, 25 insertions(+), 1 deletion(-)
mmc.1 +3 -0
@@ -198,6 +198,9 @@ Same as 'ffu', but uses CMD23+CMD25 for repeated downloads and remains in FFU mo .BI ffu3 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion. .TP +.BI ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br
mmc.c +5 -0
@@ -244,6 +244,11 @@ static struct Command commands[] = {"Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download and remains in FFU mode until completion.\n" ,NULL }, + { do_ffu4, -2, +"ffu4" ,"<image name> <device> [chunk-bytes]\n" +"Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block written.\n" , +NULL + }, { do_erase, -4,"erase" ,"<type> " "<start address> " "<end address> " "<device>\n" "Send Erase CMD38 with specific argument to the <device>\n\n"
mmc_cmds.c +16 -1
@@ -2839,6 +2839,11 @@ static void set_ffu_download_cmd(struct mmc_ioc_multi_cmd *multi_cmd, __u8 *ext_mmc_ioc_cmd_set_data (multi_cmd->cmds[0 ], buf + offset);set_single_cmd (&multi_cmd->cmds[1 ],MMC_STOP_TRANSMISSION ,0 ,0 ,0 ); multi_cmd->cmds[1 ].flags =MMC_RSP_SPI_R1B |MMC_RSP_R1B |MMC_CMD_AC ; + }else if (ffu_mode ==4 ) { +fill_switch_cmd (&multi_cmd->cmds[0 ],EXT_CSD_MODE_CONFIG ,EXT_CSD_FFU_MODE ); +set_single_cmd (&multi_cmd->cmds[1 ],MMC_WRITE_BLOCK ,1 ,1 , arg); +mmc_ioc_cmd_set_data (multi_cmd->cmds[1 ], buf + offset); +fill_switch_cmd (&multi_cmd->cmds[2 ],EXT_CSD_MODE_CONFIG ,EXT_CSD_NORMAL_MODE ); } }static int enter_ffu_mode (int *dev_fd)
@@ -2924,8 +2929,10 @@ static int do_ffu_download(int *dev_fd, __u8 *ext_csd, __u8 *fw_buf, off_t fw_sireturn -EINVAL ; } -if (ffu_mode !=1 )/* in FFU mode 1, mmc_ioc_multi_cmd contains 4 commands */ +if (ffu_mode ==2 || ffu_mode ==3 )/* in FFU mode 2, 3, mmc_ioc_multi_cmd contains 2 commands */ num_of_cmds =2 ; +else if (ffu_mode ==4 ) + num_of_cmds =3 ;/* in FFU mode 4, mmc_ioc_multi_cmd contains 3 commands */ /* allocate maximum required */ multi_cmd =calloc (1 ,sizeof (struct mmc_ioc_multi_cmd ) + num_of_cmds *sizeof (struct mmc_ioc_cmd ));
@@ -3068,6 +3075,10 @@ static int __do_ffu(int nargs, char **argv, __u8 ffu_mode) goto out; } +if (ffu_mode ==4 ) +/* FFU mode 4 uses CMD24, the write is limited to single-block operations*/ + default_chunk =512 ; + sect_done =do_ffu_download ((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk, ffu_mode);/* Check programmed sectors */
@@ -3150,6 +3161,10 @@ int do_ffu3(int nargs, char **argv) {return __do_ffu (nargs, argv,3 ); } +int do_ffu4 (int nargs,char **argv) { +return __do_ffu (nargs, argv,4 ); +} +int do_general_cmd_read (int nargs,char **argv) {int dev_fd;
mmc_cmds.h +1 -0
@@ -44,6 +44,7 @@ int do_cache_dis(int nargs, char **argv);int do_ffu (int nargs,char **argv);int do_ffu2 (int nargs,char **argv);int do_ffu3 (int nargs,char **argv); +int do_ffu4 (int nargs,char **argv);int do_read_scr (int argc,char **argv);int do_read_cid (int argc,char **argv);int do_read_csd (int argc,char **argv);
Signature · 1 line
2.34.1
PATCH v3 5/5 mmc-utils: add FFU mode 5 for firmware download using repeated CMD24 command to download FW bundle
In reply to Bean HuoFrom: Bean Huo <[email protected]>
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.
--- mmc.1 | 3 +++ mmc.c | 5 +++++ mmc_cmds.c | 23 ++++++++++++++++++----- mmc_cmds.h | 1 + 4 files changed, 27 insertions(+), 5 deletions(-)
mmc.1 +3 -0
@@ -201,6 +201,9 @@ Same as 'ffu', but uses CMD25+CMD12 Open-ended Multiple-block write to download .BI ffu4 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block is written. .TP +.BI ffu5 " \fIimage\-file\-name\fR " " \fIdevice\fR " " [\fIchunk\-bytes\fR] +Same as 'ffu', but uses CMD24 Single-block write for repeated downloads, remaining in FFU mode until completion. +.TP .BI erase " " \fItype\fR " " \fIstart-address\fR " " \fIend\-address\fR " " \fIdevice\fR Send Erase CMD38 with specific argument to the device. .br
mmc.c +5 -0
@@ -249,6 +249,11 @@ static struct Command commands[] = {"Same as 'ffu', but uses CMD24 Single-block write to download, exiting FFU mode after each block written.\n" ,NULL }, + { do_ffu5, -2, +"ffu5" ,"<image name> <device> [chunk-bytes]\n" +"Same as 'ffu', but uses CMD24 Single-block write for repeated downloads, remaining in FFU mode until completion.\n" , +NULL + }, { do_erase, -4,"erase" ,"<type> " "<start address> " "<end address> " "<device>\n" "Send Erase CMD38 with specific argument to the <device>\n\n"
mmc_cmds.c +18 -5
@@ -2844,8 +2844,12 @@ 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 if (ffu_mode ==5 ) { +set_single_cmd (&multi_cmd->cmds[0 ],MMC_WRITE_BLOCK ,1 ,1 , arg); +mmc_ioc_cmd_set_data (multi_cmd->cmds[0 ], buf + offset); } } +static int enter_ffu_mode (int *dev_fd) {int ret;
@@ -2933,6 +2937,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 ;/* in FFU mode 4, mmc_ioc_multi_cmd contains 3 commands */ + else if (ffu_mode == 5) + num_of_cmds =1 ;/* in FFU mode 5, it is single command mode */ /* allocate maximum required */ multi_cmd =calloc (1 ,sizeof (struct mmc_ioc_multi_cmd ) + num_of_cmds *sizeof (struct mmc_ioc_cmd ));
@@ -2957,11 +2963,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
@@ -3075,8 +3084,8 @@ static int __do_ffu(int nargs, char **argv, __u8 ffu_mode) goto out; } -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 ; sect_done =do_ffu_download ((int *)&dev_fd, ext_csd, fw_buf, fw_size, default_chunk, ffu_mode);
@@ -3165,6 +3174,10 @@ int do_ffu4(int nargs, char **argv) {return __do_ffu (nargs, argv,4 ); } +int do_ffu5 (int nargs,char **argv) { +return __do_ffu (nargs, argv,5 ); +} +int do_general_cmd_read (int nargs,char **argv) {int dev_fd;
mmc_cmds.h +1 -0
@@ -45,6 +45,7 @@ int do_ffu(int nargs, char **argv);int do_ffu2 (int nargs,char **argv);int do_ffu3 (int nargs,char **argv);int do_ffu4 (int nargs,char **argv); +int do_ffu5 (int nargs,char **argv);int do_read_scr (int argc,char **argv);int do_read_cid (int argc,char **argv);int do_read_csd (int argc,char **argv);
Signature · 1 line
2.34.1
Quoted 7 lines
Changelog:
v1 -- v2:
1. Addedmemset()to clean command structure
v2 -- V3:
1. Refactor patch, and remove ffu dedicated file mmc_ffu.c:w
^^^
A vim save leftover - only if another spin is needed.
Thanks,
Avri
Quoted 22 lines
Bean Huo (5):
mmc-utils: Refactor common FFU code into functions to support
additional FFU modes
mmc-utils: Add FFU mode 2
mmc-utils: Add new FFU mode using CMD25+CMD12 for Open-ended write
download FW
mmc-utils: Added FFU mode 4 that uses CMD6 and CMD24 for single-block
firmware download
mmc-utils: add FFU mode 5 for firmware download using repeated CMD24
command to download FW bundlemmc.1 | 12 ++ mmc.c | 20 +++ mmc.h | 1 + mmc_cmds.c | 389 ++++++++++++++++++++++++++++++++++++----------------- mmc_cmds.h | 4 + 5 files changed, 302 insertions(+), 124 deletions(-)--
2.34.1