diff mbox series

mmc-utils: Implement alternative boot operation

Message ID d4ac7077d94743ed91c1b2b81fc5e164@hyperstone.com (mailing list archive)
State New, archived
Headers show
Series mmc-utils: Implement alternative boot operation | expand

Commit Message

Christian Loehle Oct. 24, 2022, 5:35 p.m. UTC
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

Signed-off-by: Christian Loehle <cloehle@hyperstone.com>
---
 mmc.c      | 12 +++++++++
 mmc.h      |  1 +
 mmc_cmds.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 4 files changed, 90 insertions(+)

Comments

Avri Altman Oct. 25, 2022, 12:40 p.m. UTC | #1
> 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
> 
> Signed-off-by: Christian Loehle <cloehle@hyperstone.com>
> ---
>  mmc.c      | 12 +++++++++
>  mmc.h      |  1 +
>  mmc_cmds.c | 76
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  mmc_cmds.h |  1 +
>  4 files changed, 90 insertions(+)
> 
> diff --git a/mmc.c b/mmc.c
> index 170ee39..f328585 100644
> --- a/mmc.c
> +++ b/mmc.c
> @@ -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"
A redundant arg?
Since blksz is 512, Maybe just set it to be ext_csd[226] * 256?

> +         "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 }
>  };
> 
> diff --git a/mmc.h b/mmc.h
> index 6511dbc..98fad16 100644
> --- a/mmc.h
> +++ b/mmc.h
> @@ -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  */
> diff --git a/mmc_cmds.c b/mmc_cmds.c
> index 3db17e1..1da61d4 100644
> --- a/mmc_cmds.c
> +++ b/mmc_cmds.c
> @@ -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");
> +               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);
> +       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)

> +       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;
> +
> +       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?

> +
> +       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?

> +       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?

> +
> +       ret = DO_IO(write, boot_data_fd, boot_buf, boot_blocks * 512);
> +       if (ret < 0) {
> +               perror("Write error\n");
Free whatever you alloc'ed

> +               exit(1);
> +       }
ditto

Thanks,
Avri
> +       return 0;
> +}
> diff --git a/mmc_cmds.h b/mmc_cmds.h
> index faab362..5f2bef1 100644
> --- a/mmc_cmds.h
> +++ b/mmc_cmds.h
> @@ -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);
> --
> 2.37.3
> 
> Hyperstone GmbH | Reichenaustr. 39a  | 78467 Konstanz Managing Director:
> Dr. Jan Peter Berns.
> Commercial register of local courts: Freiburg HRB381782
Christian Loehle Oct. 25, 2022, 2:14 p.m. UTC | #2
>> @@ -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"
> 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.

>> +       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;
>> +
>> +       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?

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
Avri Altman Oct. 25, 2022, 5:25 p.m. UTC | #3
> -----Original Message-----
> From: Christian Löhle <CLoehle@hyperstone.com>
> Sent: Tuesday, October 25, 2022 5:14 PM
> To: Avri Altman <Avri.Altman@wdc.com>; ulf.hansson@linaro.org; linux-
> mmc@vger.kernel.org
> Subject: RE: [PATCH] mmc-utils: Implement alternative boot operation
> 
> CAUTION: 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.
> 
> 
> >> @@ -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"
> > 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?

> 
> >> +       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;
> >> +
> >> +       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?
> 
> 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 

> 
> 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
Christian Loehle Oct. 25, 2022, 5:38 p.m. UTC | #4
>> >> +         "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"
>> > 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?

You're right, I changed that locally and missed it.

>> >> +       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;
>> >> +
>> >> +       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?
>> 
>> 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
Avri Altman Oct. 25, 2022, 7:15 p.m. UTC | #5
> >> > 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?
> >>
> >> 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.
OK.
How about test that EXT_CSD_PART_CONFIG_ACC_ACK is not set,
And bail out if it does?

Thanks,
Avri

> 
> Regards,
> Christian
> 
> 
> Hyperstone GmbH | Reichenaustr. 39a  | 78467 Konstanz Managing Director:
> Dr. Jan Peter Berns.
> Commercial register of local courts: Freiburg HRB381782
diff mbox series

Patch

diff --git a/mmc.c b/mmc.c
index 170ee39..f328585 100644
--- a/mmc.c
+++ b/mmc.c
@@ -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 }
 };
 
diff --git a/mmc.h b/mmc.h
index 6511dbc..98fad16 100644
--- a/mmc.h
+++ b/mmc.h
@@ -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  */
diff --git a/mmc_cmds.c b/mmc_cmds.c
index 3db17e1..1da61d4 100644
--- a/mmc_cmds.c
+++ b/mmc_cmds.c
@@ -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");
+		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);
+	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);
+	}
+	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;
+
+	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);
+
+	ret = ioctl(fd, MMC_IOC_MULTI_CMD, mioc);
+	if (ret)
+		perror("multi-cmd ioctl error %d\n", ret);
+	close(fd);
+
+	ret = DO_IO(write, boot_data_fd, boot_buf, boot_blocks * 512);
+	if (ret < 0) {
+		perror("Write error\n");
+		exit(1);
+	}
+	return 0;
+}
diff --git a/mmc_cmds.h b/mmc_cmds.h
index faab362..5f2bef1 100644
--- a/mmc_cmds.h
+++ b/mmc_cmds.h
@@ -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);