You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.1 KiB

  1. From f54f401085e011e6f9c7b120aa1794b19c32b493 Mon Sep 17 00:00:00 2001
  2. From: "Shivamurthy Shastri (sshivamurthy)" <sshivamurthy@micron.com>
  3. Date: Wed, 10 Apr 2019 13:38:08 +0000
  4. Subject: [PATCH] mmc-utils: let FFU mode use CMD23 and CMD25
  5. As per specification, the host can use either CMD24 or CMD25 in
  6. closed-ended or open-ended way.
  7. CMD25 is better option as it can flash the firmware image in one go.
  8. Signed-off-by: Shivamurthy Shastri <sshivamurthy@micron.com>
  9. Reviewed-by: Avri Altman <avri.altman@wdc.com>
  10. ---
  11. mmc.h | 2 ++
  12. mmc_cmds.c | 70 ++++++++++++++++++++++++++++++------------------------
  13. 2 files changed, 41 insertions(+), 31 deletions(-)
  14. diff --git a/mmc.h b/mmc.h
  15. index 648fb26..d648f68 100644
  16. --- a/mmc.h
  17. +++ b/mmc.h
  18. @@ -25,10 +25,12 @@
  19. /* From kernel linux/mmc/mmc.h */
  20. #define MMC_SWITCH 6 /* ac [31:0] See below R1b */
  21. #define MMC_SEND_EXT_CSD 8 /* adtc R1 */
  22. +#define MMC_STOP_TRANSMISSION 12 /* ac R1b */
  23. #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
  24. #define R1_SWITCH_ERROR (1 << 7) /* sx, c */
  25. #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
  26. #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
  27. +#define MMC_SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
  28. #define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
  29. #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
  30. #define MMC_SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
  31. diff --git a/mmc_cmds.c b/mmc_cmds.c
  32. index 8f4a476..c006ef2 100644
  33. --- a/mmc_cmds.c
  34. +++ b/mmc_cmds.c
  35. @@ -2456,12 +2456,13 @@ int do_ffu(int nargs, char **argv)
  36. int sect_done = 0, retry = 3, ret = -EINVAL;
  37. unsigned int sect_size;
  38. __u8 ext_csd[512];
  39. - __u8 *buf;
  40. + __u8 *buf = NULL;
  41. __u32 arg;
  42. off_t fw_size;
  43. ssize_t chunk_size;
  44. char *device;
  45. - struct mmc_ioc_multi_cmd *multi_cmd;
  46. + struct mmc_ioc_multi_cmd *multi_cmd = NULL;
  47. + __u32 blocks = 1;
  48. if (nargs != 3) {
  49. fprintf(stderr, "Usage: ffu <image name> </path/to/mmcblkX> \n");
  50. @@ -2481,14 +2482,6 @@ int do_ffu(int nargs, char **argv)
  51. exit(1);
  52. }
  53. - buf = malloc(512);
  54. - multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
  55. - 3 * sizeof(struct mmc_ioc_cmd));
  56. - if (!buf || !multi_cmd) {
  57. - perror("failed to allocate memory");
  58. - goto out;
  59. - }
  60. -
  61. ret = read_extcsd(dev_fd, ext_csd);
  62. if (ret) {
  63. fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
  64. @@ -2513,9 +2506,17 @@ int do_ffu(int nargs, char **argv)
  65. }
  66. fw_size = lseek(img_fd, 0, SEEK_END);
  67. + if (fw_size > MMC_IOC_MAX_BYTES || fw_size == 0) {
  68. + fprintf(stderr, "Wrong firmware size");
  69. + goto out;
  70. + }
  71. - if (fw_size == 0) {
  72. - fprintf(stderr, "Firmware image is empty");
  73. + /* allocate maximum required */
  74. + buf = malloc(fw_size);
  75. + multi_cmd = calloc(1, sizeof(struct mmc_ioc_multi_cmd) +
  76. + 4 * sizeof(struct mmc_ioc_cmd));
  77. + if (!buf || !multi_cmd) {
  78. + perror("failed to allocate memory");
  79. goto out;
  80. }
  81. @@ -2525,14 +2526,19 @@ int do_ffu(int nargs, char **argv)
  82. goto out;
  83. }
  84. + /* calculate required fw blocks for CMD25 */
  85. + blocks = fw_size / sect_size;
  86. +
  87. /* set CMD ARG */
  88. arg = ext_csd[EXT_CSD_FFU_ARG_0] |
  89. ext_csd[EXT_CSD_FFU_ARG_1] << 8 |
  90. ext_csd[EXT_CSD_FFU_ARG_2] << 16 |
  91. ext_csd[EXT_CSD_FFU_ARG_3] << 24;
  92. + /* prepare multi_cmd for FFU based on cmd to be used */
  93. +
  94. /* prepare multi_cmd to be sent */
  95. - multi_cmd->num_of_cmds = 3;
  96. + multi_cmd->num_of_cmds = 4;
  97. /* put device into ffu mode */
  98. multi_cmd->cmds[0].opcode = MMC_SWITCH;
  99. @@ -2543,37 +2549,42 @@ int do_ffu(int nargs, char **argv)
  100. multi_cmd->cmds[0].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  101. multi_cmd->cmds[0].write_flag = 1;
  102. + /* send block count */
  103. + multi_cmd->cmds[1].opcode = MMC_SET_BLOCK_COUNT;
  104. + multi_cmd->cmds[1].arg = blocks;
  105. + multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  106. +
  107. /* send image chunk */
  108. - multi_cmd->cmds[1].opcode = MMC_WRITE_BLOCK;
  109. - multi_cmd->cmds[1].blksz = sect_size;
  110. - multi_cmd->cmds[1].blocks = 1;
  111. - multi_cmd->cmds[1].arg = arg;
  112. - multi_cmd->cmds[1].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  113. - multi_cmd->cmds[1].write_flag = 1;
  114. - mmc_ioc_cmd_set_data(multi_cmd->cmds[1], buf);
  115. + multi_cmd->cmds[2].opcode = MMC_WRITE_MULTIPLE_BLOCK;
  116. + multi_cmd->cmds[2].blksz = sect_size;
  117. + multi_cmd->cmds[2].blocks = blocks;
  118. + multi_cmd->cmds[2].arg = arg;
  119. + multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  120. + multi_cmd->cmds[2].write_flag = 1;
  121. + mmc_ioc_cmd_set_data(multi_cmd->cmds[2], buf);
  122. /* return device into normal mode */
  123. - multi_cmd->cmds[2].opcode = MMC_SWITCH;
  124. - multi_cmd->cmds[2].arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  125. + multi_cmd->cmds[3].opcode = MMC_SWITCH;
  126. + multi_cmd->cmds[3].arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  127. (EXT_CSD_MODE_CONFIG << 16) |
  128. (EXT_CSD_NORMAL_MODE << 8) |
  129. EXT_CSD_CMD_SET_NORMAL;
  130. - multi_cmd->cmds[2].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  131. - multi_cmd->cmds[2].write_flag = 1;
  132. + multi_cmd->cmds[3].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  133. + multi_cmd->cmds[3].write_flag = 1;
  134. do_retry:
  135. /* read firmware chunk */
  136. lseek(img_fd, 0, SEEK_SET);
  137. - chunk_size = read(img_fd, buf, 512);
  138. + chunk_size = read(img_fd, buf, fw_size);
  139. - while (chunk_size > 0) {
  140. + if (chunk_size > 0) {
  141. /* send ioctl with multi-cmd */
  142. ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd);
  143. if (ret) {
  144. perror("Multi-cmd ioctl");
  145. /* In case multi-cmd ioctl failed before exiting from ffu mode */
  146. - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[2]);
  147. + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]);
  148. goto out;
  149. }
  150. @@ -2600,9 +2611,6 @@ do_retry:
  151. } else {
  152. fprintf(stderr, "Programmed %d/%jd bytes\r", sect_done * sect_size, (intmax_t)fw_size);
  153. }
  154. -
  155. - /* read the next firmware chunk (if any) */
  156. - chunk_size = read(img_fd, buf, 512);
  157. }
  158. if ((sect_done * sect_size) == fw_size) {
  159. @@ -2639,7 +2647,7 @@ do_retry:
  160. if (ret) {
  161. perror("Multi-cmd ioctl failed setting install mode");
  162. /* In case multi-cmd ioctl failed before exiting from ffu mode */
  163. - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[2]);
  164. + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]);
  165. goto out;
  166. }
  167. --
  168. 2.17.1