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.

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