mmc-utils: Implement alternative boot operation
PATCH 1/1 mmc-utils: Implement alternative boot operation
Implements the alternative boot operation for eMMCs. Note the limitations of the help.
This is mostly useful for testing purposes if you set up the boot partition configuration correctly.
Usage:
$ sudo dd if=/dev/mmcblk2boot0 of=bootdatammcblk count=2 2+0 records in 2+0 records out 1024 bytes (1.0 kB, 1.0 KiB) copied, 0.00482308 s, 212 kB/s
$ sudo ./mmc boot_operation bootdata 2 /dev/mmcblk2
$ diff -s bootdata bootdatammcblk Files bootdata and bootdatammcblk are identical
--- mmc.c | 12 +++++++++ mmc.h | 1 + mmc_cmds.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ mmc_cmds.h | 1 + 4 files changed, 90 insertions(+)
mmc.c +12 -0
@@ -255,6 +255,18 @@ static struct Command commands[] = { "Issues a CMD0 GO_PRE_IDLE", NULL }, + { do_alt_boot_op,-1 , +"boot_operation" ,"<boot_data_file> <boot_blocks> <device>\n" +"Does the alternative boot operation and writes the specified starting blocks of boot data into the requested file.\n\n" +"Note some limitations\n:" +"1. The boot operation must be configured, e.g. for legacy speed:\n" +"mmc-utils bootbus set single_backward retain x8 /dev/mmcblk2\n" +"mmc-utils bootpart enable 1 0 /dev/mmcblk2\n" +"2. The MMC must currently be running at the bus mode that is configured for the boot operation (HS200 and HS400 not supported at all).\n" +"3. Most hosts cannot do transfers of the typical size of the boot partition, adjust <boot_blocks> accordingly.\n" +"4. The MMC will perform a soft reset, if your system cannot handle that do not use the boot operation from mmc-utils.\n" , +NULL + }, { 0, 0, 0, 0 } };
mmc.h +1 -0
@@ -24,6 +24,7 @@#define MMC_GO_IDLE_STATE 0/* bc */ #define MMC_GO_IDLE_STATE_ARG 0x0#define MMC_GO_PRE_IDLE_STATE_ARG 0xF0F0F0F0 +#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_SEND_STATUS 13/* ac [31:16] RCA R1 */
mmc_cmds.c +76 -0
79 lines not on this page · raw message
mmc_cmds.h +1 -0
@@ -49,3 +49,4 @@ int do_erase(int nargs, char **argv);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);
Signature · 5 lines
2.37.3 Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director: Dr. Jan Peter Berns. Commercial register of local courts: Freiburg HRB381782
Quoted 43 lines
Implements the alternative boot operation for eMMCs. Note the limitations of the help.
This is mostly useful for testing purposes if you set up the boot partition configuration correctly.
Usage:
$ sudo dd if=/dev/mmcblk2boot0 of=bootdatammcblk count=2 2+0 records in 2+0 records out 1024 bytes (1.0 kB, 1.0 KiB) copied, 0.00482308 s, 212 kB/s$ sudo ./mmc boot_operation bootdata 2 /dev/mmcblk2$ diff -s bootdata bootdatammcblk Files bootdata and bootdatammcblk are identicalSigned-off-by Christian Loehle <[email protected]>--- mmc.c | 12 +++++++++ mmc.h | 1 + mmc_cmds.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ mmc_cmds.h | 1 + 4 files changed, 90 insertions(+)mmc.c +3
-0@@ -255,6 +255,18 @@ static struct Command commands[] = {"Issues a CMD0 GO_PRE_IDLE" ,NULL }, + { do_alt_boot_op, -1, +"boot_operation" ,"<boot_data_file> <boot_blocks> <device>\n" + "Does the alternative boot operation and writes the specified startingblocks of boot data into the requested file.\n\n"
mmc.c +5
-0+"Note some limitations\n:" +"1. The boot operation must be configured, e.g. for legacy speed:\n" +"mmc-utils bootbus set single_backward retain x8 /dev/mmcblk2\n" +"mmc-utils bootpart enable 1 0 /dev/mmcblk2\n" + "2. The MMC must currently be running at the bus mode that is
Quoted 3 lines
configured for the boot operation (HS200 and HS400 not supported at all).\n"
+ "3. Most hosts cannot do transfers of the typical size of the boot partition,
adjust <boot_blocks> accordingly.\n"
A redundant arg?
Since blksz is 512, Maybe just set it to be ext_csd[226] * 256?
Quoted 51 lines
+ "4. The MMC will perform a soft reset, if your system cannot handle that
do not use the boot operation from mmc-utils.\n",mmc.c +2
-0+NULL + }, { 0, 0, 0, 0 } };mmc.h +1
-0@@ -24,6 +24,7 @@#define MMC_GO_IDLE_STATE 0/* bc */ #define MMC_GO_IDLE_STATE_ARG 0x0#define MMC_GO_PRE_IDLE_STATE_ARG 0xF0F0F0F0 +#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_SEND_STATUS 13/* ac [31:16] RCA R1 */ mmc_cmds.c +11
-0@@ -3101,3 +3101,79 @@ int do_preidle(int nargs, char **argv)return 0 ; } + +int do_alt_boot_op (int nargs,char **argv) { +int fd, ret, boot_data_fd; +char *device, *boot_data_file; +struct mmc_ioc_multi_cmd *mioc; +__u8 ext_csd[512 ]; +__u8 *boot_buf; +unsigned int boot_blocks, ext_csd_boot_size; + +if (nargs !=4 ) { + fprintf(stderr, "Usage: mmc boot_op <boot_data_file> <boot_blocks></path/to/mmcblkX>\n");
mmc_cmds.c +12
-0+exit (1 ); + } + boot_data_file = argv[1 ]; + boot_blocks =strtol (argv[2 ],NULL ,10 ); + device = argv[3 ]; + + fd =open (device,O_RDWR ); +if (fd <0 ) { +perror ("open device" ); +exit (1 ); + } + boot_data_fd =open (boot_data_file,O_WRONLY |O_CREAT ,0644 );
mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3137
Quoted 10 lines
mmc_cmds.c +10
-0+if (boot_data_fd <0 ) { +perror ("open boot data file" ); +exit (1 ); + } + + ret =read_extcsd (fd, ext_csd); +if (ret) { +perror ("read extcsd" ); +exit (1 ); + }
One line space please.
Also, need to check alternative boot support: (ext_csd[EXT_CSD_BOOT_INFO] & EXT_CSD_BOOT_INFO_ALT)
Quoted 19 lines
mmc_cmds.c +19
-0+ ext_csd_boot_size = ext_csd[226 ] *128 *1024 ; +if (boot_blocks *512 > ext_csd_boot_size) { +perror ("Requested boot size bigger than boot partition" ); +exit (1 ); + } + + boot_buf =calloc (1 ,sizeof (__u8) * boot_blocks *512 ); + mioc =calloc (1 ,sizeof (struct mmc_ioc_multi_cmd ) + +2 *sizeof (struct mmc_ioc_cmd )); +if (!mioc || !boot_buf) { +perror ("Failed to allocate memory" ); +return -ENOMEM ; + } + + mioc->num_of_cmds =2 ; + mioc->cmds[0 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[0 ].arg =MMC_GO_PRE_IDLE_STATE_ARG ; + mioc->cmds[0 ].flags =MMC_RSP_NONE |MMC_CMD_AC ; + mioc->cmds[0 ].write_flag =0 ;
mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3166
Quoted 10 lines
mmc_cmds.c +10
-0+ + mioc->cmds[1 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[1 ].arg =MMC_BOOT_INITIATION_ARG ; + mioc->cmds[1 ].flags =MMC_RSP_NONE |MMC_CMD_ADTC ; + mioc->cmds[1 ].write_flag =0 ; + mioc->cmds[1 ].blksz =512 ; + mioc->cmds[1 ].blocks = boot_blocks; +/* Access time of boot part differs wildly, spec mandates 1s */ + mioc->cmds[1 ].data_timeout_ns =2 *1000 *1000 *1000 ; +mmc_ioc_cmd_set_data (mioc->cmds[1 ], boot_buf);
Don't you get "010" prior to the boot content? Most cards have their EXT_CSD_PART_CONFIG_ACC_ACK bit set. So you need to eliminate it from the boot file? But your compare show that the files are identical - how so?
mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3170
Quoted 4 lines
mmc_cmds.c +4
-0+ + ret =ioctl (fd,MMC_IOC_MULTI_CMD , mioc); +if (ret) +perror ("multi-cmd ioctl error %d\n" , ret);
If an error occurred - do you still want to output the boot file?
Quoted 1 line
+ close(fd);
Don't you need to send CMD1 to terminate the alternative boot sequence? Without it, isn't the card stack in Card-identification Mode?
mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3175
Quoted 4 lines
mmc_cmds.c +4
-0+ + ret =DO_IO (write, boot_data_fd, boot_buf, boot_blocks *512 ); +if (ret <0 ) { +perror ("Write error\n" );
Free whatever you alloc'ed
mmc_cmds.c
Quoted 2 lines
mmc_cmds.c +2
-0+exit (1 ); + }
ditto
Thanks,
Avri
Quoted 16 lines
mmc_cmds.c +2
-0+return 0 ; +}mmc_cmds.h +0
-0@@ -49,3 +49,4 @@ int do_erase(int nargs, char **argv); int
do_general_cmd_read(int nargs, char **argv); intdo_softreset(int nargs, char **argv); intdo_preidle(int nargs, char **argv);
+intdo_alt_boot_op(int nargs, char **argv);
--
2.37.3Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director:
Dr. Jan Peter Berns.
Commercial register of local courts: Freiburg HRB381782
Quoted 13 lines
Quoted 13 lines
mmc.c +4
-0@@ -255,6 +255,18 @@ static struct Command commands[] = {"Issues a CMD0 GO_PRE_IDLE" ,NULL }, + { do_alt_boot_op, -1, +"boot_operation" ,"<boot_data_file> <boot_blocks> <device>\n" + "Does the alternative boot operation and writes the + specified startingblocks of boot data into the requested file.\n\n"
mmc.c +4
-0+"Note some limitations\n:" +"1. The boot operation must be configured, e.g. for legacy speed:\n" +"mmc-utils bootbus set single_backward retain x8 /dev/mmcblk2\n" +"mmc-utils bootpart enable 1 0 /dev/mmcblk2\n"
Quoted 8 lines
Quoted 6 lines
+ "2. The MMC must currently be running at the bus mode that
+ is
configured for the boot operation (HS200 and HS400 not supported at all).\n"
+ "3. Most hosts cannot do transfers of the typical size of
+ the boot partition,
adjust <boot_blocks> accordingly.\n"A redundant arg?
Since blksz is 512, Maybe just set it to be ext_csd[226] * 256?
As the help above explains this will not work on most hosts. Limitations of maximum transfers of like 512K to a couple MB are very common. One could just read the max, but I found it less misleading that way.
Quoted 15 lines
Quoted 5 lines
mmc_cmds.c +5
-0+ mioc->num_of_cmds =2 ; + mioc->cmds[0 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[0 ].arg =MMC_GO_PRE_IDLE_STATE_ARG ; + mioc->cmds[0 ].flags =MMC_RSP_NONE |MMC_CMD_AC ; + mioc->cmds[0 ].write_flag =0 ;mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3166
Quoted 10 lines
mmc_cmds.c +10
-0+ + mioc->cmds[1 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[1 ].arg =MMC_BOOT_INITIATION_ARG ; + mioc->cmds[1 ].flags =MMC_RSP_NONE |MMC_CMD_ADTC ; + mioc->cmds[1 ].write_flag =0 ; + mioc->cmds[1 ].blksz =512 ; + mioc->cmds[1 ].blocks = boot_blocks; +/* Access time of boot part differs wildly, spec mandates 1s */ + mioc->cmds[1 ].data_timeout_ns =2 *1000 *1000 *1000 ; +mmc_ioc_cmd_set_data (mioc->cmds[1 ], boot_buf);
Quoted 4 lines
Don't you get "010" prior to the boot content? Most cards have their
EXT_CSD_PART_CONFIG_ACC_ACKbit set. So you need to eliminate it from the boot file? But your compare show that the files are identical - how so?
By configuring the card to not send BOOT ACK.
With it enabled there is unfortunately no way to get the transfer through from userspace. (The host will sample part of the ACK, CRC wont match and so on)
I will address the rest of you comments in v2.
Regards,
Christian
Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director: Dr. Jan Peter Berns. Commercial register of local courts: Freiburg HRB381782
Quoted 34 lines
-----Original Message-----
From: Christian Löhle <[email protected]>
Sent: Tuesday, October 25, 2022 5:14 PM
To: Avri Altman <[email protected]>; [email protected]; linux- [email protected]
Subject: RE: [PATCH] mmc-utils: Implement alternative boot operationCAUTION: This email originated from outside of Western Digital. Do not click on links or open attachments unless you recognize the sender and know that the content is safe.
Quoted 8 lines
Quoted 8 lines
mmc.c +4
-0@@ -255,6 +255,18 @@ static struct Command commands[] = {"Issues a CMD0 GO_PRE_IDLE" ,NULL }, + { do_alt_boot_op, -1, +"boot_operation" ,"<boot_data_file> <boot_blocks> <device>\n" + "Does the alternative boot operation and writes the + specified startingQuoted 8 lines
Quoted 8 lines
blocks of boot data into the requested file.\n\n"
mmc.c +6
-0+"Note some limitations\n:" +"1. The boot operation must be configured, e.g. for legacy speed:\n" +"mmc-utils bootbus set single_backward retain x8 /dev/mmcblk2\n" +"mmc-utils bootpart enable 1 0 /dev/mmcblk2\n" + "2. The MMC must currently be running at the bus mode that + isconfigured for the boot operation (HS200 and HS400 not supported at
all).\n"
Quoted 3 lines
Quoted 3 lines
+ "3. Most hosts cannot do transfers of the typical size of
+ the boot partition,
adjust <boot_blocks> accordingly.\n"
Quoted 7 lines
Quoted 2 lines
A redundant arg?
Since blksz is 512, Maybe just set it to be ext_csd[226] * 256?As the help above explains this will not work on most hosts. Limitations of maximum transfers of like 512K to a couple MB are very common.
One could just read the max, but I found it less misleading that way.
But isn't the mmc_blk_ioctl_copy_from_user impose a hard MMC_IOC_MAX_BYTES limit? So you won't be able to read more than 512K anyway?
Quoted 21 lines
Quoted 16 lines
Quoted 6 lines
mmc_cmds.c +5
-0+ mioc->num_of_cmds =2 ; + mioc->cmds[0 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[0 ].arg =MMC_GO_PRE_IDLE_STATE_ARG ; + mioc->cmds[0 ].flags =MMC_RSP_NONE |MMC_CMD_AC ; + mioc->cmds[0 ].write_flag =0 ;mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3166
Quoted 10 lines
mmc_cmds.c +10
-0+ + mioc->cmds[1 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[1 ].arg =MMC_BOOT_INITIATION_ARG ; + mioc->cmds[1 ].flags =MMC_RSP_NONE |MMC_CMD_ADTC ; + mioc->cmds[1 ].write_flag =0 ; + mioc->cmds[1 ].blksz =512 ; + mioc->cmds[1 ].blocks = boot_blocks; +/* Access time of boot part differs wildly, spec mandates 1s */ + mioc->cmds[1 ].data_timeout_ns =2 *1000 *1000 *1000 ; +mmc_ioc_cmd_set_data (mioc->cmds[1 ], boot_buf);Quoted 4 lines
Don't you get "010" prior to the boot content? Most cards have their
EXT_CSD_PART_CONFIG_ACC_ACKbit set. So you need to eliminate it from the boot file? But your compare show that the files are identical - how so?
Quoted 4 lines
By configuring the card to not send BOOT ACK.
With it enabled there is unfortunately no way to get the transfer through from userspace.
(The host will sample part of the ACK, CRC wont match and so on)
This doesn't seems to be a reasonable requirement from the average user - e.g. validation & field application engineers who address eMMC issues as well, but mostly other embedded flash protocols - ufs & others.
Thanks,
Avri
Quoted 8 lines
I will address the rest of you comments in v2.
Regards,
Christian
Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director:
Dr. Jan Peter Berns.
Commercial register of local courts: Freiburg HRB381782
Quoted 14 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
mmc.c +6
-0+"Note some limitations\n:" +"1. The boot operation must be configured, e.g. for legacy speed:\n" +"mmc-utils bootbus set single_backward retain x8 /dev/mmcblk2\n" +"mmc-utils bootpart enable 1 0 /dev/mmcblk2\n" + "2. The MMC must currently be running at the bus mode + that isconfigured for the boot operation (HS200 and HS400 not supported at
all).\n"
Quoted 5 lines
Quoted 3 lines
+ "3. Most hosts cannot do transfers of the typical size of
+ the boot partition,
adjust <boot_blocks> accordingly.\n"A redundant arg?
Since blksz is 512, Maybe just set it to be ext_csd[226] * 256?
Quoted 6 lines
Quoted 4 lines
As the help above explains this will not work on most hosts. Limitations of maximum transfers of like 512K to a couple MB are very common.
One could just read the max, but I found it less misleading that way.But isn't the
mmc_blk_ioctl_copy_from_userimpose a hardMMC_IOC_MAX_BYTESlimit? So you won't be able to read more than 512K anyway?
You're right, I changed that locally and missed it.
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 5 lines
mmc_cmds.c +5
-0+ mioc->num_of_cmds =2 ; + mioc->cmds[0 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[0 ].arg =MMC_GO_PRE_IDLE_STATE_ARG ; + mioc->cmds[0 ].flags =MMC_RSP_NONE |MMC_CMD_AC ; + mioc->cmds[0 ].write_flag =0 ;mmc_cmds.c int do_preidle(int nargs, char **argv) · line 3166
Quoted 10 lines
mmc_cmds.c +10
-0+ + mioc->cmds[1 ].opcode =MMC_GO_IDLE_STATE ; + mioc->cmds[1 ].arg =MMC_BOOT_INITIATION_ARG ; + mioc->cmds[1 ].flags =MMC_RSP_NONE |MMC_CMD_ADTC ; + mioc->cmds[1 ].write_flag =0 ; + mioc->cmds[1 ].blksz =512 ; + mioc->cmds[1 ].blocks = boot_blocks; +/* Access time of boot part differs wildly, spec mandates 1s */ + mioc->cmds[1 ].data_timeout_ns =2 *1000 *1000 *1000 ; +mmc_ioc_cmd_set_data (mioc->cmds[1 ], boot_buf);Don't you get "010" prior to the boot content? Most cards have their
EXT_CSD_PART_CONFIG_ACC_ACKbit set. So you need to eliminate it from the boot file? But your compare show that the files are identical - how so?
Quoted 5 lines
Quoted 4 lines
By configuring the card to not send BOOT ACK.
With it enabled there is unfortunately no way to get the transfer through from userspace.
(The host will sample part of the ACK, CRC wont match and so on)This doesn't seems to be a reasonable requirement from the average user - e.g. validation & field application engineers who address eMMC issues as well, but mostly other embedded flash protocols - ufs & others.
Is it? I mean usually they configure the boot partition, too and mmc-utils gives them all they need with:
e.g. mmc-utils bootpart enable 1 0 /dev/mmcblkX
But yeah, if this is too much of a limitation to be considered useful then this patch is not worth pursuing as pretending the boot operation is a normal block transfer for ioctl and the host driver only works with boot ack off. Anything with boot ack on cannot be done from userspace and not even host-controller-agnostic, i.e. would need its own host_ops call, which is a bit overkill for testing / debugging purposes I'd say.
Regards,
Christian
Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director: Dr. Jan Peter Berns. Commercial register of local courts: Freiburg HRB381782
Quoted 13 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Don't you get "010" prior to the boot content? Most cards have their
EXT_CSD_PART_CONFIG_ACC_ACKbit set. So you need to eliminate it from the boot file? But your compare show that the files are identical - how so?By configuring the card to not send BOOT ACK.
With it enabled there is unfortunately no way to get the transfer through from userspace.
(The host will sample part of the ACK, CRC wont match and so on)This doesn't seems to be a reasonable requirement from the average user -
e.g. validation & field application engineers who address eMMC issues as well, but mostly other embedded flash protocols - ufs & others.
Quoted 9 lines
Is it? I mean usually they configure the boot partition, too and mmc-utils gives them all they need with:
e.g. mmc-utils bootpart enable 1 0/dev/mmcblkXBut yeah, if this is too much of a limitation to be considered useful then this patch is not worth pursuing as pretending the boot operation is a normal block transfer for ioctl and the host driver only works with boot ack off.
Anything with boot ack on cannot be done from userspace and not even host- controller-agnostic, i.e. would need its ownhost_opscall, which is a bit overkill for testing / debugging purposes I'd say.
OK.
How about test that EXT_CSD_PART_CONFIG_ACC_ACK is not set, And bail out if it does?
Thanks,
Avri
Quoted 8 lines
Regards,
ChristianHyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director:
Dr. Jan Peter Berns.
Commercial register of local courts: Freiburg HRB381782