Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public |
| 4 | * License v2 as published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | * General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public |
| 12 | * License along with this program; if not, write to the |
| 13 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 14 | * Boston, MA 021110-1307, USA. |
| 15 | */ |
| 16 | |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/ioctl.h> |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 22 | #include <sys/param.h> |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <dirent.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <libgen.h> |
| 29 | #include <limits.h> |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 30 | #include <ctype.h> |
| 31 | |
| 32 | #include "mmc.h" |
| 33 | #include "mmc_cmds.h" |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 34 | #include "ffu.h" |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 35 | |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 36 | #define EXT_CSD_SIZE 512 |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 37 | #define FFU_DATA_SIZE 512 |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 38 | #define CID_SIZE 16 |
| 39 | |
| 40 | |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 41 | int read_extcsd(int fd, __u8 *ext_csd) |
| 42 | { |
| 43 | int ret = 0; |
| 44 | struct mmc_ioc_cmd idata; |
| 45 | memset(&idata, 0, sizeof(idata)); |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 46 | memset(ext_csd, 0, sizeof(__u8) * EXT_CSD_SIZE); |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 47 | idata.write_flag = 0; |
| 48 | idata.opcode = MMC_SEND_EXT_CSD; |
| 49 | idata.arg = 0; |
| 50 | idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 51 | idata.blksz = EXT_CSD_SIZE; |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 52 | idata.blocks = 1; |
| 53 | mmc_ioc_cmd_set_data(idata, ext_csd); |
| 54 | |
| 55 | ret = ioctl(fd, MMC_IOC_CMD, &idata); |
| 56 | if (ret) |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 57 | perror("ioctl SEND_EXT_CSD"); |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | int write_extcsd_value(int fd, __u8 index, __u8 value) |
| 63 | { |
| 64 | int ret = 0; |
| 65 | struct mmc_ioc_cmd idata; |
| 66 | |
| 67 | memset(&idata, 0, sizeof(idata)); |
| 68 | idata.write_flag = 1; |
| 69 | idata.opcode = MMC_SWITCH; |
| 70 | idata.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 71 | (index << 16) | |
| 72 | (value << 8) | |
| 73 | EXT_CSD_CMD_SET_NORMAL; |
| 74 | idata.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 75 | |
| 76 | ret = ioctl(fd, MMC_IOC_CMD, &idata); |
| 77 | if (ret) |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 78 | perror("ioctl Write EXT CSD"); |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
Ben Gardiner | 27c357d | 2013-05-30 17:12:47 -0400 | [diff] [blame] | 83 | int send_status(int fd, __u32 *response) |
| 84 | { |
| 85 | int ret = 0; |
| 86 | struct mmc_ioc_cmd idata; |
| 87 | |
| 88 | memset(&idata, 0, sizeof(idata)); |
| 89 | idata.opcode = MMC_SEND_STATUS; |
| 90 | idata.arg = (1 << 16); |
| 91 | idata.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 92 | |
| 93 | ret = ioctl(fd, MMC_IOC_CMD, &idata); |
| 94 | if (ret) |
| 95 | perror("ioctl"); |
| 96 | |
| 97 | *response = idata.response[0]; |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 102 | void print_writeprotect_status(__u8 *ext_csd) |
| 103 | { |
| 104 | __u8 reg; |
| 105 | __u8 ext_csd_rev = ext_csd[192]; |
| 106 | |
| 107 | /* A43: reserved [174:0] */ |
| 108 | if (ext_csd_rev >= 5) { |
| 109 | printf("Boot write protection status registers" |
| 110 | " [BOOT_WP_STATUS]: 0x%02x\n", ext_csd[174]); |
| 111 | |
| 112 | reg = ext_csd[EXT_CSD_BOOT_WP]; |
| 113 | printf("Boot Area Write protection [BOOT_WP]: 0x%02x\n", reg); |
| 114 | printf(" Power ro locking: "); |
| 115 | if (reg & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 116 | printf("not possible\n"); |
| 117 | else |
| 118 | printf("possible\n"); |
| 119 | |
| 120 | printf(" Permanent ro locking: "); |
| 121 | if (reg & EXT_CSD_BOOT_WP_B_PERM_WP_DIS) |
| 122 | printf("not possible\n"); |
| 123 | else |
| 124 | printf("possible\n"); |
| 125 | |
| 126 | printf(" ro lock status: "); |
| 127 | if (reg & EXT_CSD_BOOT_WP_B_PWR_WP_EN) |
| 128 | printf("locked until next power on\n"); |
| 129 | else if (reg & EXT_CSD_BOOT_WP_B_PERM_WP_EN) |
| 130 | printf("locked permanently\n"); |
| 131 | else |
| 132 | printf("not locked\n"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int do_writeprotect_get(int nargs, char **argv) |
| 137 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 138 | __u8 ext_csd[EXT_CSD_SIZE]; |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 139 | int fd, ret; |
| 140 | char *device; |
| 141 | |
Chris Ball | 8ba4466 | 2012-04-19 13:22:54 -0400 | [diff] [blame] | 142 | CHECK(nargs != 2, "Usage: mmc writeprotect get </path/to/mmcblkX>\n", |
| 143 | exit(1)); |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 144 | |
| 145 | device = argv[1]; |
| 146 | |
| 147 | fd = open(device, O_RDWR); |
| 148 | if (fd < 0) { |
| 149 | perror("open"); |
| 150 | exit(1); |
| 151 | } |
| 152 | |
| 153 | ret = read_extcsd(fd, ext_csd); |
| 154 | if (ret) { |
| 155 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 156 | exit(1); |
| 157 | } |
| 158 | |
| 159 | print_writeprotect_status(ext_csd); |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | int do_writeprotect_set(int nargs, char **argv) |
| 165 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 166 | __u8 ext_csd[EXT_CSD_SIZE], value; |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 167 | int fd, ret; |
| 168 | char *device; |
| 169 | |
Chris Ball | 8ba4466 | 2012-04-19 13:22:54 -0400 | [diff] [blame] | 170 | CHECK(nargs != 2, "Usage: mmc writeprotect set </path/to/mmcblkX>\n", |
| 171 | exit(1)); |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 172 | |
| 173 | device = argv[1]; |
| 174 | |
| 175 | fd = open(device, O_RDWR); |
| 176 | if (fd < 0) { |
| 177 | perror("open"); |
| 178 | exit(1); |
| 179 | } |
| 180 | |
| 181 | ret = read_extcsd(fd, ext_csd); |
| 182 | if (ret) { |
| 183 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 184 | exit(1); |
| 185 | } |
| 186 | |
| 187 | value = ext_csd[EXT_CSD_BOOT_WP] | |
| 188 | EXT_CSD_BOOT_WP_B_PWR_WP_EN; |
| 189 | ret = write_extcsd_value(fd, EXT_CSD_BOOT_WP, value); |
| 190 | if (ret) { |
| 191 | fprintf(stderr, "Could not write 0x%02x to " |
| 192 | "EXT_CSD[%d] in %s\n", |
| 193 | value, EXT_CSD_BOOT_WP, device); |
| 194 | exit(1); |
| 195 | } |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
Saugata Das | b7e2599 | 2012-05-17 09:26:34 -0400 | [diff] [blame] | 200 | int do_disable_512B_emulation(int nargs, char **argv) |
| 201 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 202 | __u8 ext_csd[EXT_CSD_SIZE], native_sector_size, data_sector_size, wr_rel_param; |
Saugata Das | b7e2599 | 2012-05-17 09:26:34 -0400 | [diff] [blame] | 203 | int fd, ret; |
| 204 | char *device; |
| 205 | |
| 206 | CHECK(nargs != 2, "Usage: mmc disable 512B emulation </path/to/mmcblkX>\n", exit(1)); |
| 207 | device = argv[1]; |
| 208 | |
| 209 | fd = open(device, O_RDWR); |
| 210 | if (fd < 0) { |
| 211 | perror("open"); |
| 212 | exit(1); |
| 213 | } |
| 214 | |
| 215 | ret = read_extcsd(fd, ext_csd); |
| 216 | if (ret) { |
| 217 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 218 | exit(1); |
| 219 | } |
| 220 | |
| 221 | wr_rel_param = ext_csd[EXT_CSD_WR_REL_PARAM]; |
| 222 | native_sector_size = ext_csd[EXT_CSD_NATIVE_SECTOR_SIZE]; |
| 223 | data_sector_size = ext_csd[EXT_CSD_DATA_SECTOR_SIZE]; |
| 224 | |
| 225 | if (native_sector_size && !data_sector_size && |
| 226 | (wr_rel_param & EN_REL_WR)) { |
| 227 | ret = write_extcsd_value(fd, EXT_CSD_USE_NATIVE_SECTOR, 1); |
| 228 | |
| 229 | if (ret) { |
| 230 | fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", |
| 231 | 1, EXT_CSD_BOOT_WP, device); |
| 232 | exit(1); |
| 233 | } |
| 234 | printf("MMC disable 512B emulation successful. Now reset the device to switch to 4KB native sector mode.\n"); |
| 235 | } else if (native_sector_size && data_sector_size) { |
| 236 | printf("MMC 512B emulation mode is already disabled; doing nothing.\n"); |
| 237 | } else { |
| 238 | printf("MMC does not support disabling 512B emulation mode.\n"); |
| 239 | } |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
Giuseppe CAVALLARO | 7bd1320 | 2012-04-19 10:58:37 +0200 | [diff] [blame] | 244 | int do_write_boot_en(int nargs, char **argv) |
| 245 | { |
| 246 | __u8 ext_csd[512]; |
| 247 | __u8 value = 0; |
| 248 | int fd, ret; |
| 249 | char *device; |
| 250 | int boot_area, send_ack; |
| 251 | |
| 252 | CHECK(nargs != 4, "Usage: mmc bootpart enable <partition_number> " |
| 253 | "<send_ack> </path/to/mmcblkX>\n", exit(1)); |
| 254 | |
| 255 | /* |
| 256 | * If <send_ack> is 1, the device will send acknowledgment |
| 257 | * pattern "010" to the host when boot operation begins. |
| 258 | * If <send_ack> is 0, it won't. |
| 259 | */ |
| 260 | boot_area = strtol(argv[1], NULL, 10); |
| 261 | send_ack = strtol(argv[2], NULL, 10); |
| 262 | device = argv[3]; |
| 263 | |
| 264 | fd = open(device, O_RDWR); |
| 265 | if (fd < 0) { |
| 266 | perror("open"); |
| 267 | exit(1); |
| 268 | } |
| 269 | |
| 270 | ret = read_extcsd(fd, ext_csd); |
| 271 | if (ret) { |
| 272 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 273 | exit(1); |
| 274 | } |
| 275 | |
| 276 | value = ext_csd[EXT_CSD_PART_CONFIG]; |
| 277 | |
| 278 | switch (boot_area) { |
| 279 | case EXT_CSD_PART_CONFIG_ACC_BOOT0: |
| 280 | value |= (1 << 3); |
| 281 | value &= ~(3 << 4); |
| 282 | break; |
| 283 | case EXT_CSD_PART_CONFIG_ACC_BOOT1: |
| 284 | value |= (1 << 4); |
| 285 | value &= ~(1 << 3); |
| 286 | value &= ~(1 << 5); |
| 287 | break; |
| 288 | case EXT_CSD_PART_CONFIG_ACC_USER_AREA: |
| 289 | value |= (boot_area << 3); |
| 290 | break; |
| 291 | default: |
| 292 | fprintf(stderr, "Cannot enable the boot area\n"); |
| 293 | exit(1); |
| 294 | } |
| 295 | if (send_ack) |
| 296 | value |= EXT_CSD_PART_CONFIG_ACC_ACK; |
| 297 | else |
| 298 | value &= ~EXT_CSD_PART_CONFIG_ACC_ACK; |
| 299 | |
| 300 | ret = write_extcsd_value(fd, EXT_CSD_PART_CONFIG, value); |
| 301 | if (ret) { |
| 302 | fprintf(stderr, "Could not write 0x%02x to " |
| 303 | "EXT_CSD[%d] in %s\n", |
| 304 | value, EXT_CSD_PART_CONFIG, device); |
| 305 | exit(1); |
| 306 | } |
| 307 | return ret; |
| 308 | } |
| 309 | |
Chris Ball | f74dfe2 | 2012-10-19 16:49:55 -0400 | [diff] [blame] | 310 | int do_hwreset(int value, int nargs, char **argv) |
| 311 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 312 | __u8 ext_csd[EXT_CSD_SIZE]; |
Chris Ball | f74dfe2 | 2012-10-19 16:49:55 -0400 | [diff] [blame] | 313 | int fd, ret; |
| 314 | char *device; |
| 315 | |
| 316 | CHECK(nargs != 2, "Usage: mmc hwreset enable </path/to/mmcblkX>\n", |
| 317 | exit(1)); |
| 318 | |
| 319 | device = argv[1]; |
| 320 | |
| 321 | fd = open(device, O_RDWR); |
| 322 | if (fd < 0) { |
| 323 | perror("open"); |
| 324 | exit(1); |
| 325 | } |
| 326 | |
| 327 | ret = read_extcsd(fd, ext_csd); |
| 328 | if (ret) { |
| 329 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 330 | exit(1); |
| 331 | } |
| 332 | |
| 333 | if ((ext_csd[EXT_CSD_RST_N_FUNCTION] & EXT_CSD_RST_N_EN_MASK) == |
| 334 | EXT_CSD_HW_RESET_EN) { |
| 335 | fprintf(stderr, |
| 336 | "H/W Reset is already permanently enabled on %s\n", |
| 337 | device); |
| 338 | exit(1); |
| 339 | } |
| 340 | if ((ext_csd[EXT_CSD_RST_N_FUNCTION] & EXT_CSD_RST_N_EN_MASK) == |
| 341 | EXT_CSD_HW_RESET_DIS) { |
| 342 | fprintf(stderr, |
| 343 | "H/W Reset is already permanently disabled on %s\n", |
| 344 | device); |
| 345 | exit(1); |
| 346 | } |
| 347 | |
| 348 | ret = write_extcsd_value(fd, EXT_CSD_RST_N_FUNCTION, value); |
| 349 | if (ret) { |
| 350 | fprintf(stderr, |
| 351 | "Could not write 0x%02x to EXT_CSD[%d] in %s\n", |
| 352 | value, EXT_CSD_RST_N_FUNCTION, device); |
| 353 | exit(1); |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | int do_hwreset_en(int nargs, char **argv) |
| 360 | { |
| 361 | return do_hwreset(EXT_CSD_HW_RESET_EN, nargs, argv); |
| 362 | } |
| 363 | |
| 364 | int do_hwreset_dis(int nargs, char **argv) |
| 365 | { |
| 366 | return do_hwreset(EXT_CSD_HW_RESET_DIS, nargs, argv); |
| 367 | } |
| 368 | |
Jaehoon Chung | 8649651 | 2012-09-21 10:08:05 +0000 | [diff] [blame] | 369 | int do_write_bkops_en(int nargs, char **argv) |
| 370 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 371 | __u8 ext_csd[EXT_CSD_SIZE], value = 0; |
Jaehoon Chung | 8649651 | 2012-09-21 10:08:05 +0000 | [diff] [blame] | 372 | int fd, ret; |
| 373 | char *device; |
| 374 | |
| 375 | CHECK(nargs != 2, "Usage: mmc bkops enable </path/to/mmcblkX>\n", |
| 376 | exit(1)); |
| 377 | |
| 378 | device = argv[1]; |
| 379 | |
| 380 | fd = open(device, O_RDWR); |
| 381 | if (fd < 0) { |
| 382 | perror("open"); |
| 383 | exit(1); |
| 384 | } |
| 385 | |
| 386 | ret = read_extcsd(fd, ext_csd); |
| 387 | if (ret) { |
| 388 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 389 | exit(1); |
| 390 | } |
| 391 | |
| 392 | if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) { |
| 393 | fprintf(stderr, "%s doesn't support BKOPS\n", device); |
| 394 | exit(1); |
| 395 | } |
| 396 | |
| 397 | ret = write_extcsd_value(fd, EXT_CSD_BKOPS_EN, BKOPS_ENABLE); |
| 398 | if (ret) { |
| 399 | fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", |
| 400 | value, EXT_CSD_BKOPS_EN, device); |
| 401 | exit(1); |
| 402 | } |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
Ben Gardiner | 27c357d | 2013-05-30 17:12:47 -0400 | [diff] [blame] | 407 | int do_status_get(int nargs, char **argv) |
| 408 | { |
| 409 | __u32 response; |
| 410 | int fd, ret; |
| 411 | char *device; |
| 412 | |
| 413 | CHECK(nargs != 2, "Usage: mmc status get </path/to/mmcblkX>\n", |
| 414 | exit(1)); |
| 415 | |
| 416 | device = argv[1]; |
| 417 | |
| 418 | fd = open(device, O_RDWR); |
| 419 | if (fd < 0) { |
| 420 | perror("open"); |
| 421 | exit(1); |
| 422 | } |
| 423 | |
| 424 | ret = send_status(fd, &response); |
| 425 | if (ret) { |
| 426 | fprintf(stderr, "Could not read response to SEND_STATUS from %s\n", device); |
| 427 | exit(1); |
| 428 | } |
| 429 | |
| 430 | printf("SEND_STATUS response: 0x%08x\n", response); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 435 | __u32 get_word_from_ext_csd(__u8 *ext_csd_loc) |
| 436 | { |
| 437 | return (ext_csd_loc[3] << 24) | |
| 438 | (ext_csd_loc[2] << 16) | |
| 439 | (ext_csd_loc[1] << 8) | |
| 440 | ext_csd_loc[0]; |
| 441 | } |
| 442 | |
Ben Gardiner | 4e85023 | 2013-05-30 17:12:49 -0400 | [diff] [blame] | 443 | unsigned int get_sector_count(__u8 *ext_csd) |
| 444 | { |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 445 | return get_word_from_ext_csd(&ext_csd[EXT_CSD_SEC_COUNT_0]); |
Ben Gardiner | 4e85023 | 2013-05-30 17:12:49 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int is_blockaddresed(__u8 *ext_csd) |
| 449 | { |
| 450 | unsigned int sectors = get_sector_count(ext_csd); |
| 451 | |
| 452 | return (sectors > (2u * 1024 * 1024 * 1024) / 512); |
| 453 | } |
| 454 | |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 455 | unsigned int get_hc_wp_grp_size(__u8 *ext_csd) |
| 456 | { |
| 457 | return ext_csd[221]; |
| 458 | } |
| 459 | |
| 460 | unsigned int get_hc_erase_grp_size(__u8 *ext_csd) |
| 461 | { |
| 462 | return ext_csd[224]; |
| 463 | } |
| 464 | |
Ben Gardiner | e6e84e9 | 2013-09-19 11:14:27 -0400 | [diff] [blame] | 465 | int set_partitioning_setting_completed(int dry_run, const char * const device, |
| 466 | int fd) |
| 467 | { |
| 468 | int ret; |
| 469 | |
| 470 | if (dry_run) { |
| 471 | fprintf(stderr, "NOT setting PARTITION_SETTING_COMPLETED\n"); |
| 472 | fprintf(stderr, "These changes will not take effect neither " |
| 473 | "now nor after a power cycle\n"); |
| 474 | return 1; |
| 475 | } |
| 476 | |
| 477 | fprintf(stderr, "setting OTP PARTITION_SETTING_COMPLETED!\n"); |
| 478 | ret = write_extcsd_value(fd, EXT_CSD_PARTITION_SETTING_COMPLETED, 0x1); |
| 479 | if (ret) { |
| 480 | fprintf(stderr, "Could not write 0x1 to " |
| 481 | "EXT_CSD[%d] in %s\n", |
| 482 | EXT_CSD_PARTITION_SETTING_COMPLETED, device); |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | __u32 response; |
| 487 | ret = send_status(fd, &response); |
| 488 | if (ret) { |
| 489 | fprintf(stderr, "Could not get response to SEND_STATUS " |
| 490 | "from %s\n", device); |
| 491 | return 1; |
| 492 | } |
| 493 | |
| 494 | if (response & R1_SWITCH_ERROR) { |
| 495 | fprintf(stderr, "Setting OTP PARTITION_SETTING_COMPLETED " |
| 496 | "failed on %s\n", device); |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | fprintf(stderr, "Setting OTP PARTITION_SETTING_COMPLETED on " |
| 501 | "%s SUCCESS\n", device); |
| 502 | fprintf(stderr, "Device power cycle needed for settings to " |
| 503 | "take effect.\n" |
| 504 | "Confirm that PARTITION_SETTING_COMPLETED bit is set " |
| 505 | "using 'extcsd read' after power cycle\n"); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 510 | int do_enh_area_set(int nargs, char **argv) |
| 511 | { |
| 512 | __u8 value; |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 513 | __u8 ext_csd[EXT_CSD_SIZE]; |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 514 | int fd, ret; |
| 515 | char *device; |
| 516 | int dry_run = 1; |
| 517 | unsigned int start_kib, length_kib, enh_start_addr, enh_size_mult; |
| 518 | unsigned long align; |
| 519 | |
| 520 | CHECK(nargs != 5, "Usage: mmc enh_area set <-y|-n> <start KiB> <length KiB> " |
| 521 | "</path/to/mmcblkX>\n", exit(1)); |
| 522 | |
| 523 | if (!strcmp("-y", argv[1])) |
| 524 | dry_run = 0; |
| 525 | |
| 526 | start_kib = strtol(argv[2], NULL, 10); |
| 527 | length_kib = strtol(argv[3], NULL, 10); |
| 528 | device = argv[4]; |
| 529 | |
| 530 | fd = open(device, O_RDWR); |
| 531 | if (fd < 0) { |
| 532 | perror("open"); |
| 533 | exit(1); |
| 534 | } |
| 535 | |
| 536 | ret = read_extcsd(fd, ext_csd); |
| 537 | if (ret) { |
| 538 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 539 | exit(1); |
| 540 | } |
| 541 | |
| 542 | /* assert ENH_ATTRIBUTE_EN */ |
| 543 | if (!(ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & EXT_CSD_ENH_ATTRIBUTE_EN)) |
| 544 | { |
| 545 | printf(" Device cannot have enhanced tech.\n"); |
| 546 | exit(1); |
| 547 | } |
| 548 | |
| 549 | /* assert not PARTITION_SETTING_COMPLETED */ |
| 550 | if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED]) |
| 551 | { |
| 552 | printf(" Device is already partitioned\n"); |
| 553 | exit(1); |
| 554 | } |
| 555 | |
| 556 | align = 512l * get_hc_wp_grp_size(ext_csd) * get_hc_erase_grp_size(ext_csd); |
| 557 | |
| 558 | enh_size_mult = (length_kib + align/2l) / align; |
| 559 | |
| 560 | enh_start_addr = start_kib * 1024 / (is_blockaddresed(ext_csd) ? 512 : 1); |
| 561 | enh_start_addr /= align; |
| 562 | enh_start_addr *= align; |
| 563 | |
| 564 | /* set EXT_CSD_ERASE_GROUP_DEF bit 0 */ |
| 565 | ret = write_extcsd_value(fd, EXT_CSD_ERASE_GROUP_DEF, 0x1); |
| 566 | if (ret) { |
| 567 | fprintf(stderr, "Could not write 0x1 to " |
| 568 | "EXT_CSD[%d] in %s\n", |
| 569 | EXT_CSD_ERASE_GROUP_DEF, device); |
| 570 | exit(1); |
| 571 | } |
| 572 | |
| 573 | /* write to ENH_START_ADDR and ENH_SIZE_MULT and PARTITIONS_ATTRIBUTE's ENH_USR bit */ |
| 574 | value = (enh_start_addr >> 24) & 0xff; |
| 575 | ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_3, value); |
| 576 | if (ret) { |
| 577 | fprintf(stderr, "Could not write 0x%02x to " |
| 578 | "EXT_CSD[%d] in %s\n", value, |
| 579 | EXT_CSD_ENH_START_ADDR_3, device); |
| 580 | exit(1); |
| 581 | } |
| 582 | value = (enh_start_addr >> 16) & 0xff; |
| 583 | ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_2, value); |
| 584 | if (ret) { |
| 585 | fprintf(stderr, "Could not write 0x%02x to " |
| 586 | "EXT_CSD[%d] in %s\n", value, |
| 587 | EXT_CSD_ENH_START_ADDR_2, device); |
| 588 | exit(1); |
| 589 | } |
| 590 | value = (enh_start_addr >> 8) & 0xff; |
| 591 | ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_1, value); |
| 592 | if (ret) { |
| 593 | fprintf(stderr, "Could not write 0x%02x to " |
| 594 | "EXT_CSD[%d] in %s\n", value, |
| 595 | EXT_CSD_ENH_START_ADDR_1, device); |
| 596 | exit(1); |
| 597 | } |
| 598 | value = enh_start_addr & 0xff; |
| 599 | ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_0, value); |
| 600 | if (ret) { |
| 601 | fprintf(stderr, "Could not write 0x%02x to " |
| 602 | "EXT_CSD[%d] in %s\n", value, |
| 603 | EXT_CSD_ENH_START_ADDR_0, device); |
| 604 | exit(1); |
| 605 | } |
| 606 | |
| 607 | value = (enh_size_mult >> 16) & 0xff; |
| 608 | ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_2, value); |
| 609 | if (ret) { |
| 610 | fprintf(stderr, "Could not write 0x%02x to " |
| 611 | "EXT_CSD[%d] in %s\n", value, |
| 612 | EXT_CSD_ENH_SIZE_MULT_2, device); |
| 613 | exit(1); |
| 614 | } |
| 615 | value = (enh_size_mult >> 8) & 0xff; |
| 616 | ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_1, value); |
| 617 | if (ret) { |
| 618 | fprintf(stderr, "Could not write 0x%02x to " |
| 619 | "EXT_CSD[%d] in %s\n", value, |
| 620 | EXT_CSD_ENH_SIZE_MULT_1, device); |
| 621 | exit(1); |
| 622 | } |
| 623 | value = enh_size_mult & 0xff; |
| 624 | ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_0, value); |
| 625 | if (ret) { |
| 626 | fprintf(stderr, "Could not write 0x%02x to " |
| 627 | "EXT_CSD[%d] in %s\n", value, |
| 628 | EXT_CSD_ENH_SIZE_MULT_0, device); |
| 629 | exit(1); |
| 630 | } |
| 631 | |
| 632 | ret = write_extcsd_value(fd, EXT_CSD_PARTITIONS_ATTRIBUTE, EXT_CSD_ENH_USR); |
| 633 | if (ret) { |
| 634 | fprintf(stderr, "Could not write EXT_CSD_ENH_USR to " |
| 635 | "EXT_CSD[%d] in %s\n", |
| 636 | EXT_CSD_PARTITIONS_ATTRIBUTE, device); |
| 637 | exit(1); |
| 638 | } |
| 639 | |
Ben Gardiner | e6e84e9 | 2013-09-19 11:14:27 -0400 | [diff] [blame] | 640 | printf("Done setting ENH_USR area on %s\n", device); |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 641 | |
Ben Gardiner | e6e84e9 | 2013-09-19 11:14:27 -0400 | [diff] [blame] | 642 | if (!set_partitioning_setting_completed(dry_run, device, fd)) |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 643 | exit(1); |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
Ben Gardiner | 196d0d2 | 2013-09-19 11:14:29 -0400 | [diff] [blame] | 648 | int do_write_reliability_set(int nargs, char **argv) |
| 649 | { |
| 650 | __u8 value; |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 651 | __u8 ext_csd[EXT_CSD_SIZE]; |
Ben Gardiner | 196d0d2 | 2013-09-19 11:14:29 -0400 | [diff] [blame] | 652 | int fd, ret; |
| 653 | |
| 654 | int dry_run = 1; |
| 655 | int partition; |
| 656 | char *device; |
| 657 | |
| 658 | CHECK(nargs != 4, "Usage: mmc write_reliability set <-y|-n> " |
| 659 | "<partition> </path/to/mmcblkX>\n", exit(1)); |
| 660 | |
| 661 | if (!strcmp("-y", argv[1])) |
| 662 | dry_run = 0; |
| 663 | |
| 664 | partition = strtol(argv[2], NULL, 10); |
| 665 | device = argv[3]; |
| 666 | |
| 667 | fd = open(device, O_RDWR); |
| 668 | if (fd < 0) { |
| 669 | perror("open"); |
| 670 | exit(1); |
| 671 | } |
| 672 | |
| 673 | ret = read_extcsd(fd, ext_csd); |
| 674 | if (ret) { |
| 675 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 676 | exit(1); |
| 677 | } |
| 678 | |
| 679 | /* assert not PARTITION_SETTING_COMPLETED */ |
| 680 | if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED]) |
| 681 | { |
| 682 | printf(" Device is already partitioned\n"); |
| 683 | exit(1); |
| 684 | } |
| 685 | |
| 686 | /* assert HS_CTRL_REL */ |
| 687 | if (!(ext_csd[EXT_CSD_WR_REL_PARAM] & HS_CTRL_REL)) { |
| 688 | printf("Cannot set write reliability parameters, WR_REL_SET is " |
| 689 | "read-only\n"); |
| 690 | exit(1); |
| 691 | } |
| 692 | |
| 693 | value = ext_csd[EXT_CSD_WR_REL_SET] | (1<<partition); |
| 694 | ret = write_extcsd_value(fd, EXT_CSD_WR_REL_SET, value); |
| 695 | if (ret) { |
| 696 | fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", |
| 697 | value, EXT_CSD_WR_REL_SET, device); |
| 698 | exit(1); |
| 699 | } |
| 700 | |
| 701 | printf("Done setting EXT_CSD_WR_REL_SET to 0x%02x on %s\n", |
| 702 | value, device); |
| 703 | |
| 704 | if (!set_partitioning_setting_completed(dry_run, device, fd)) |
| 705 | exit(1); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 710 | int do_read_extcsd(int nargs, char **argv) |
| 711 | { |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 712 | __u8 ext_csd[EXT_CSD_SIZE], ext_csd_rev, reg; |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 713 | __u32 regl; |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 714 | int fd, ret; |
| 715 | char *device; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 716 | const char *str; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 717 | const char *ver_str[] = { |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 718 | "4.0", /* 0 */ |
| 719 | "4.1", /* 1 */ |
| 720 | "4.2", /* 2 */ |
| 721 | "4.3", /* 3 */ |
| 722 | "Obsolete", /* 4 */ |
| 723 | "4.41", /* 5 */ |
| 724 | "4.5", /* 6 */ |
| 725 | "5.0", /* 7 */ |
Puthikorn Voravootivat | c384aec | 2015-04-28 11:28:41 -0700 | [diff] [blame] | 726 | "5.1", /* 8 */ |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 727 | }; |
| 728 | int boot_access; |
| 729 | const char* boot_access_str[] = { |
| 730 | "No access to boot partition", /* 0 */ |
| 731 | "R/W Boot Partition 1", /* 1 */ |
| 732 | "R/W Boot Partition 2", /* 2 */ |
| 733 | "R/W Replay Protected Memory Block (RPMB)", /* 3 */ |
| 734 | }; |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 735 | |
Chris Ball | 8ba4466 | 2012-04-19 13:22:54 -0400 | [diff] [blame] | 736 | CHECK(nargs != 2, "Usage: mmc extcsd read </path/to/mmcblkX>\n", |
| 737 | exit(1)); |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 738 | |
| 739 | device = argv[1]; |
| 740 | |
| 741 | fd = open(device, O_RDWR); |
| 742 | if (fd < 0) { |
| 743 | perror("open"); |
| 744 | exit(1); |
| 745 | } |
| 746 | |
| 747 | ret = read_extcsd(fd, ext_csd); |
| 748 | if (ret) { |
| 749 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 750 | exit(1); |
| 751 | } |
| 752 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 753 | ext_csd_rev = ext_csd[192]; |
| 754 | |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 755 | if ((ext_csd_rev < sizeof(ver_str)/sizeof(char*)) && |
| 756 | (ext_csd_rev != 4)) |
| 757 | str = ver_str[ext_csd_rev]; |
| 758 | else |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 759 | goto out_free; |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 760 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 761 | printf("=============================================\n"); |
| 762 | printf(" Extended CSD rev 1.%d (MMC %s)\n", ext_csd_rev, str); |
| 763 | printf("=============================================\n\n"); |
| 764 | |
| 765 | if (ext_csd_rev < 3) |
| 766 | goto out_free; /* No ext_csd */ |
| 767 | |
| 768 | /* Parse the Extended CSD registers. |
| 769 | * Reserved bit should be read as "0" in case of spec older |
| 770 | * than A441. |
| 771 | */ |
| 772 | reg = ext_csd[EXT_CSD_S_CMD_SET]; |
| 773 | printf("Card Supported Command sets [S_CMD_SET: 0x%02x]\n", reg); |
| 774 | if (!reg) |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 775 | printf(" - Standard MMC command sets\n"); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 776 | |
| 777 | reg = ext_csd[EXT_CSD_HPI_FEATURE]; |
| 778 | printf("HPI Features [HPI_FEATURE: 0x%02x]: ", reg); |
| 779 | if (reg & EXT_CSD_HPI_SUPP) { |
| 780 | if (reg & EXT_CSD_HPI_IMPL) |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 781 | printf("implementation based on CMD12\n"); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 782 | else |
| 783 | printf("implementation based on CMD13\n"); |
| 784 | } |
| 785 | |
| 786 | printf("Background operations support [BKOPS_SUPPORT: 0x%02x]\n", |
| 787 | ext_csd[502]); |
| 788 | |
| 789 | if (ext_csd_rev >= 6) { |
| 790 | printf("Max Packet Read Cmd [MAX_PACKED_READS: 0x%02x]\n", |
| 791 | ext_csd[501]); |
| 792 | printf("Max Packet Write Cmd [MAX_PACKED_WRITES: 0x%02x]\n", |
| 793 | ext_csd[500]); |
| 794 | printf("Data TAG support [DATA_TAG_SUPPORT: 0x%02x]\n", |
| 795 | ext_csd[499]); |
| 796 | |
| 797 | printf("Data TAG Unit Size [TAG_UNIT_SIZE: 0x%02x]\n", |
| 798 | ext_csd[498]); |
| 799 | printf("Tag Resources Size [TAG_RES_SIZE: 0x%02x]\n", |
| 800 | ext_csd[497]); |
| 801 | printf("Context Management Capabilities" |
| 802 | " [CONTEXT_CAPABILITIES: 0x%02x]\n", ext_csd[496]); |
| 803 | printf("Large Unit Size [LARGE_UNIT_SIZE_M1: 0x%02x]\n", |
| 804 | ext_csd[495]); |
| 805 | printf("Extended partition attribute support" |
| 806 | " [EXT_SUPPORT: 0x%02x]\n", ext_csd[494]); |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 807 | } |
| 808 | if (ext_csd_rev >= 7) { |
| 809 | int j; |
| 810 | int eol_info; |
| 811 | char* eol_info_str[] = { |
| 812 | "Not Defined", /* 0 */ |
| 813 | "Normal", /* 1 */ |
| 814 | "Warning", /* 2 */ |
| 815 | "Urgent", /* 3 */ |
| 816 | }; |
| 817 | |
| 818 | printf("Supported modes [SUPPORTED_MODES: 0x%02x]\n", |
| 819 | ext_csd[493]); |
| 820 | printf("FFU features [FFU_FEATURES: 0x%02x]\n", |
| 821 | ext_csd[492]); |
| 822 | printf("Operation codes timeout" |
| 823 | " [OPERATION_CODE_TIMEOUT: 0x%02x]\n", |
| 824 | ext_csd[491]); |
| 825 | printf("FFU Argument [FFU_ARG: 0x%08x]\n", |
| 826 | get_word_from_ext_csd(&ext_csd[487])); |
| 827 | printf("Number of FW sectors correctly programmed" |
| 828 | " [NUMBER_OF_FW_SECTORS_CORRECTLY_PROGRAMMED: %d]\n", |
| 829 | get_word_from_ext_csd(&ext_csd[302])); |
| 830 | printf("Vendor proprietary health report:\n"); |
| 831 | for (j = 301; j >= 270; j--) |
| 832 | printf("[VENDOR_PROPRIETARY_HEALTH_REPORT[%d]]:" |
| 833 | " 0x%02x\n", j, ext_csd[j]); |
| 834 | for (j = 269; j >= 268; j--) { |
| 835 | __u8 life_used=ext_csd[j]; |
Puthikorn Voravootivat | 6bb37ea | 2014-03-03 17:55:51 -0800 | [diff] [blame] | 836 | char est_type = 'B' + (j - 269); |
| 837 | printf("Device life time estimation type %c" |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 838 | " [DEVICE_LIFE_TIME_EST_TYP_%c: 0x%02x]\n", |
Puthikorn Voravootivat | 6bb37ea | 2014-03-03 17:55:51 -0800 | [diff] [blame] | 839 | est_type, est_type, life_used); |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 840 | if (life_used >= 0x1 && life_used <= 0xa) |
| 841 | printf(" i.e. %d%% - %d%% device life time" |
| 842 | " used\n", |
| 843 | (life_used - 1) * 10, life_used * 10); |
| 844 | else if (life_used == 0xb) |
| 845 | printf(" i.e. Exceeded its maximum estimated" |
| 846 | " device life time\n"); |
| 847 | } |
| 848 | eol_info = ext_csd[267]; |
| 849 | printf("Pre EOL information [PRE_EOL_INFO: 0x%02x]\n", |
| 850 | eol_info); |
| 851 | if (eol_info < sizeof(eol_info_str)/sizeof(char*)) |
| 852 | printf(" i.e. %s\n", eol_info_str[eol_info]); |
| 853 | else |
| 854 | printf(" i.e. Reserved\n"); |
| 855 | |
| 856 | printf("Optimal read size [OPTIMAL_READ_SIZE: 0x%02x]\n", |
| 857 | ext_csd[266]); |
| 858 | printf("Optimal write size [OPTIMAL_WRITE_SIZE: 0x%02x]\n", |
| 859 | ext_csd[265]); |
| 860 | printf("Optimal trim unit size" |
| 861 | " [OPTIMAL_TRIM_UNIT_SIZE: 0x%02x]\n", ext_csd[264]); |
| 862 | printf("Device version [DEVICE_VERSION: 0x%02x - 0x%02x]\n", |
| 863 | ext_csd[263], ext_csd[262]); |
| 864 | printf("Firmware version:\n"); |
| 865 | for (j = 261; j >= 254; j--) |
| 866 | printf("[FIRMWARE_VERSION[%d]]:" |
| 867 | " 0x%02x\n", j, ext_csd[j]); |
| 868 | |
| 869 | printf("Power class for 200MHz, DDR at VCC= 3.6V" |
| 870 | " [PWR_CL_DDR_200_360: 0x%02x]\n", ext_csd[253]); |
| 871 | } |
| 872 | if (ext_csd_rev >= 6) { |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 873 | printf("Generic CMD6 Timer [GENERIC_CMD6_TIME: 0x%02x]\n", |
| 874 | ext_csd[248]); |
| 875 | printf("Power off notification [POWER_OFF_LONG_TIME: 0x%02x]\n", |
| 876 | ext_csd[247]); |
| 877 | printf("Cache Size [CACHE_SIZE] is %d KiB\n", |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 878 | get_word_from_ext_csd(&ext_csd[249])); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* A441: Reserved [501:247] |
| 882 | A43: reserved [246:229] */ |
| 883 | if (ext_csd_rev >= 5) { |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 884 | printf("Background operations status" |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 885 | " [BKOPS_STATUS: 0x%02x]\n", ext_csd[246]); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 886 | |
| 887 | /* CORRECTLY_PRG_SECTORS_NUM [245:242] TODO */ |
| 888 | |
| 889 | printf("1st Initialisation Time after programmed sector" |
| 890 | " [INI_TIMEOUT_AP: 0x%02x]\n", ext_csd[241]); |
| 891 | |
| 892 | /* A441: reserved [240] */ |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 893 | printf("Power class for 52MHz, DDR at 3.6V" |
| 894 | " [PWR_CL_DDR_52_360: 0x%02x]\n", ext_csd[239]); |
| 895 | printf("Power class for 52MHz, DDR at 1.95V" |
| 896 | " [PWR_CL_DDR_52_195: 0x%02x]\n", ext_csd[238]); |
| 897 | |
| 898 | /* A441: reserved [237-236] */ |
| 899 | |
| 900 | if (ext_csd_rev >= 6) { |
| 901 | printf("Power class for 200MHz at 3.6V" |
| 902 | " [PWR_CL_200_360: 0x%02x]\n", ext_csd[237]); |
| 903 | printf("Power class for 200MHz, at 1.95V" |
| 904 | " [PWR_CL_200_195: 0x%02x]\n", ext_csd[236]); |
| 905 | } |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 906 | printf("Minimum Performance for 8bit at 52MHz in DDR mode:\n"); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 907 | printf(" [MIN_PERF_DDR_W_8_52: 0x%02x]\n", ext_csd[235]); |
| 908 | printf(" [MIN_PERF_DDR_R_8_52: 0x%02x]\n", ext_csd[234]); |
| 909 | /* A441: reserved [233] */ |
| 910 | printf("TRIM Multiplier [TRIM_MULT: 0x%02x]\n", ext_csd[232]); |
| 911 | printf("Secure Feature support [SEC_FEATURE_SUPPORT: 0x%02x]\n", |
| 912 | ext_csd[231]); |
| 913 | } |
| 914 | if (ext_csd_rev == 5) { /* Obsolete in 4.5 */ |
| 915 | printf("Secure Erase Multiplier [SEC_ERASE_MULT: 0x%02x]\n", |
| 916 | ext_csd[230]); |
| 917 | printf("Secure TRIM Multiplier [SEC_TRIM_MULT: 0x%02x]\n", |
| 918 | ext_csd[229]); |
| 919 | } |
| 920 | reg = ext_csd[EXT_CSD_BOOT_INFO]; |
| 921 | printf("Boot Information [BOOT_INFO: 0x%02x]\n", reg); |
| 922 | if (reg & EXT_CSD_BOOT_INFO_ALT) |
| 923 | printf(" Device supports alternative boot method\n"); |
| 924 | if (reg & EXT_CSD_BOOT_INFO_DDR_DDR) |
| 925 | printf(" Device supports dual data rate during boot\n"); |
| 926 | if (reg & EXT_CSD_BOOT_INFO_HS_MODE) |
| 927 | printf(" Device supports high speed timing during boot\n"); |
| 928 | |
| 929 | /* A441/A43: reserved [227] */ |
| 930 | printf("Boot partition size [BOOT_SIZE_MULTI: 0x%02x]\n", ext_csd[226]); |
| 931 | printf("Access size [ACC_SIZE: 0x%02x]\n", ext_csd[225]); |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 932 | |
| 933 | reg = get_hc_erase_grp_size(ext_csd); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 934 | printf("High-capacity erase unit size [HC_ERASE_GRP_SIZE: 0x%02x]\n", |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 935 | reg); |
| 936 | printf(" i.e. %u KiB\n", 512 * reg); |
| 937 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 938 | printf("High-capacity erase timeout [ERASE_TIMEOUT_MULT: 0x%02x]\n", |
| 939 | ext_csd[223]); |
| 940 | printf("Reliable write sector count [REL_WR_SEC_C: 0x%02x]\n", |
| 941 | ext_csd[222]); |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 942 | |
| 943 | reg = get_hc_wp_grp_size(ext_csd); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 944 | printf("High-capacity W protect group size [HC_WP_GRP_SIZE: 0x%02x]\n", |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 945 | reg); |
| 946 | printf(" i.e. %lu KiB\n", 512l * get_hc_erase_grp_size(ext_csd) * reg); |
| 947 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 948 | printf("Sleep current (VCC) [S_C_VCC: 0x%02x]\n", ext_csd[220]); |
| 949 | printf("Sleep current (VCCQ) [S_C_VCCQ: 0x%02x]\n", ext_csd[219]); |
| 950 | /* A441/A43: reserved [218] */ |
| 951 | printf("Sleep/awake timeout [S_A_TIMEOUT: 0x%02x]\n", ext_csd[217]); |
| 952 | /* A441/A43: reserved [216] */ |
Ben Gardiner | 4e85023 | 2013-05-30 17:12:49 -0400 | [diff] [blame] | 953 | |
| 954 | unsigned int sectors = get_sector_count(ext_csd); |
| 955 | printf("Sector Count [SEC_COUNT: 0x%08x]\n", sectors); |
| 956 | if (is_blockaddresed(ext_csd)) |
| 957 | printf(" Device is block-addressed\n"); |
| 958 | else |
| 959 | printf(" Device is NOT block-addressed\n"); |
| 960 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 961 | /* A441/A43: reserved [211] */ |
| 962 | printf("Minimum Write Performance for 8bit:\n"); |
| 963 | printf(" [MIN_PERF_W_8_52: 0x%02x]\n", ext_csd[210]); |
| 964 | printf(" [MIN_PERF_R_8_52: 0x%02x]\n", ext_csd[209]); |
| 965 | printf(" [MIN_PERF_W_8_26_4_52: 0x%02x]\n", ext_csd[208]); |
| 966 | printf(" [MIN_PERF_R_8_26_4_52: 0x%02x]\n", ext_csd[207]); |
| 967 | printf("Minimum Write Performance for 4bit:\n"); |
| 968 | printf(" [MIN_PERF_W_4_26: 0x%02x]\n", ext_csd[206]); |
| 969 | printf(" [MIN_PERF_R_4_26: 0x%02x]\n", ext_csd[205]); |
| 970 | /* A441/A43: reserved [204] */ |
| 971 | printf("Power classes registers:\n"); |
| 972 | printf(" [PWR_CL_26_360: 0x%02x]\n", ext_csd[203]); |
| 973 | printf(" [PWR_CL_52_360: 0x%02x]\n", ext_csd[202]); |
| 974 | printf(" [PWR_CL_26_195: 0x%02x]\n", ext_csd[201]); |
| 975 | printf(" [PWR_CL_52_195: 0x%02x]\n", ext_csd[200]); |
| 976 | |
| 977 | /* A43: reserved [199:198] */ |
| 978 | if (ext_csd_rev >= 5) { |
| 979 | printf("Partition switching timing " |
| 980 | "[PARTITION_SWITCH_TIME: 0x%02x]\n", ext_csd[199]); |
| 981 | printf("Out-of-interrupt busy timing" |
| 982 | " [OUT_OF_INTERRUPT_TIME: 0x%02x]\n", ext_csd[198]); |
| 983 | } |
| 984 | |
| 985 | /* A441/A43: reserved [197] [195] [193] [190] [188] |
| 986 | * [186] [184] [182] [180] [176] */ |
| 987 | |
| 988 | if (ext_csd_rev >= 6) |
| 989 | printf("I/O Driver Strength [DRIVER_STRENGTH: 0x%02x]\n", |
| 990 | ext_csd[197]); |
| 991 | |
Oleg Matcovschi | 64f63a3 | 2013-05-23 17:11:07 -0700 | [diff] [blame] | 992 | /* DEVICE_TYPE in A45, CARD_TYPE in A441 */ |
Gwendal Grignou | c2faa3d | 2015-04-28 10:00:45 -0700 | [diff] [blame^] | 993 | printf("Card Type [CARD_TYPE: 0x%02x - %02x]\n", |
| 994 | ext_csd[196], ext_csd[195]); |
| 995 | reg = ext_csd[195]; |
| 996 | if (reg & 0x02) printf(" HS533 Dual Data Rate eMMC @266MHz 1.2VI/O\n"); |
| 997 | if (reg & 0x01) printf(" HS533 Dual Data Rate eMMC @266MHz 1.8VI/O\n"); |
Oleg Matcovschi | 64f63a3 | 2013-05-23 17:11:07 -0700 | [diff] [blame] | 998 | reg = ext_csd[196]; |
Gwendal Grignou | c2faa3d | 2015-04-28 10:00:45 -0700 | [diff] [blame^] | 999 | if (reg & 0x80) printf(" HS400 Dual Data Rate eMMC @200MHz 1.2VI/O\n"); |
| 1000 | if (reg & 0x40) printf(" HS400 Dual Data Rate eMMC @200MHz 1.8VI/O\n"); |
Oleg Matcovschi | 64f63a3 | 2013-05-23 17:11:07 -0700 | [diff] [blame] | 1001 | if (reg & 0x20) printf(" HS200 Single Data Rate eMMC @200MHz 1.2VI/O\n"); |
| 1002 | if (reg & 0x10) printf(" HS200 Single Data Rate eMMC @200MHz 1.8VI/O\n"); |
| 1003 | if (reg & 0x08) printf(" HS Dual Data Rate eMMC @52MHz 1.2VI/O\n"); |
| 1004 | if (reg & 0x04) printf(" HS Dual Data Rate eMMC @52MHz 1.8V or 3VI/O\n"); |
| 1005 | if (reg & 0x02) printf(" HS eMMC @52MHz - at rated device voltage(s)\n"); |
| 1006 | if (reg & 0x01) printf(" HS eMMC @26MHz - at rated device voltage(s)\n"); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1007 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1008 | printf("CSD structure version [CSD_STRUCTURE: 0x%02x]\n", ext_csd[194]); |
| 1009 | /* ext_csd_rev = ext_csd[192] (already done!!!) */ |
| 1010 | printf("Command set [CMD_SET: 0x%02x]\n", ext_csd[191]); |
| 1011 | printf("Command set revision [CMD_SET_REV: 0x%02x]\n", ext_csd[189]); |
| 1012 | printf("Power class [POWER_CLASS: 0x%02x]\n", ext_csd[187]); |
| 1013 | printf("High-speed interface timing [HS_TIMING: 0x%02x]\n", |
| 1014 | ext_csd[185]); |
| 1015 | /* bus_width: ext_csd[183] not readable */ |
| 1016 | printf("Erased memory content [ERASED_MEM_CONT: 0x%02x]\n", |
| 1017 | ext_csd[181]); |
| 1018 | reg = ext_csd[EXT_CSD_BOOT_CFG]; |
| 1019 | printf("Boot configuration bytes [PARTITION_CONFIG: 0x%02x]\n", reg); |
Mario Schuknecht | 8c0c40d | 2013-05-15 08:28:04 +0200 | [diff] [blame] | 1020 | switch ((reg & EXT_CSD_BOOT_CFG_EN)>>3) { |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1021 | case 0x0: |
| 1022 | printf(" Not boot enable\n"); |
| 1023 | break; |
| 1024 | case 0x1: |
| 1025 | printf(" Boot Partition 1 enabled\n"); |
| 1026 | break; |
| 1027 | case 0x2: |
| 1028 | printf(" Boot Partition 2 enabled\n"); |
| 1029 | break; |
| 1030 | case 0x7: |
| 1031 | printf(" User Area Enabled for boot\n"); |
| 1032 | break; |
| 1033 | } |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 1034 | boot_access = reg & EXT_CSD_BOOT_CFG_ACC; |
| 1035 | if (boot_access < sizeof(boot_access_str) / sizeof(char*)) |
| 1036 | printf(" %s\n", boot_access_str[boot_access]); |
| 1037 | else |
Mario Schuknecht | 8c0c40d | 2013-05-15 08:28:04 +0200 | [diff] [blame] | 1038 | printf(" Access to General Purpose partition %d\n", |
Gwendal Grignou | 9b8d99c | 2014-01-28 13:48:05 -0800 | [diff] [blame] | 1039 | boot_access - 3); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1040 | |
| 1041 | printf("Boot config protection [BOOT_CONFIG_PROT: 0x%02x]\n", |
| 1042 | ext_csd[178]); |
| 1043 | printf("Boot bus Conditions [BOOT_BUS_CONDITIONS: 0x%02x]\n", |
| 1044 | ext_csd[177]); |
| 1045 | printf("High-density erase group definition" |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 1046 | " [ERASE_GROUP_DEF: 0x%02x]\n", ext_csd[EXT_CSD_ERASE_GROUP_DEF]); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1047 | |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 1048 | print_writeprotect_status(ext_csd); |
| 1049 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1050 | if (ext_csd_rev >= 5) { |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1051 | /* A441]: reserved [172] */ |
| 1052 | printf("User area write protection register" |
| 1053 | " [USER_WP]: 0x%02x\n", ext_csd[171]); |
| 1054 | /* A441]: reserved [170] */ |
| 1055 | printf("FW configuration [FW_CONFIG]: 0x%02x\n", ext_csd[169]); |
| 1056 | printf("RPMB Size [RPMB_SIZE_MULT]: 0x%02x\n", ext_csd[168]); |
Ben Gardiner | 4da1c0d | 2013-09-19 11:14:28 -0400 | [diff] [blame] | 1057 | |
| 1058 | reg = ext_csd[EXT_CSD_WR_REL_SET]; |
| 1059 | const char * const fast = "existing data is at risk if a power " |
| 1060 | "failure occurs during a write operation"; |
| 1061 | const char * const reliable = "the device protects existing " |
| 1062 | "data if a power failure occurs during a write " |
| 1063 | "operation"; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1064 | printf("Write reliability setting register" |
Ben Gardiner | 4da1c0d | 2013-09-19 11:14:28 -0400 | [diff] [blame] | 1065 | " [WR_REL_SET]: 0x%02x\n", reg); |
| 1066 | |
| 1067 | printf(" user area: %s\n", reg & (1<<0) ? reliable : fast); |
| 1068 | int i; |
| 1069 | for (i = 1; i <= 4; i++) { |
| 1070 | printf(" partition %d: %s\n", i, |
| 1071 | reg & (1<<i) ? reliable : fast); |
| 1072 | } |
| 1073 | |
| 1074 | reg = ext_csd[EXT_CSD_WR_REL_PARAM]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1075 | printf("Write reliability parameter register" |
Ben Gardiner | 4da1c0d | 2013-09-19 11:14:28 -0400 | [diff] [blame] | 1076 | " [WR_REL_PARAM]: 0x%02x\n", reg); |
| 1077 | if (reg & 0x01) |
| 1078 | printf(" Device supports writing EXT_CSD_WR_REL_SET\n"); |
| 1079 | if (reg & 0x04) |
| 1080 | printf(" Device supports the enhanced def. of reliable " |
| 1081 | "write\n"); |
| 1082 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1083 | /* sanitize_start ext_csd[165]]: not readable |
| 1084 | * bkops_start ext_csd[164]]: only writable */ |
| 1085 | printf("Enable background operations handshake" |
| 1086 | " [BKOPS_EN]: 0x%02x\n", ext_csd[163]); |
| 1087 | printf("H/W reset function" |
| 1088 | " [RST_N_FUNCTION]: 0x%02x\n", ext_csd[162]); |
| 1089 | printf("HPI management [HPI_MGMT]: 0x%02x\n", ext_csd[161]); |
Ben Gardiner | 82bd950 | 2013-06-27 11:04:10 -0400 | [diff] [blame] | 1090 | reg = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1091 | printf("Partitioning Support [PARTITIONING_SUPPORT]: 0x%02x\n", |
| 1092 | reg); |
Ben Gardiner | 82bd950 | 2013-06-27 11:04:10 -0400 | [diff] [blame] | 1093 | if (reg & EXT_CSD_PARTITIONING_EN) |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1094 | printf(" Device support partitioning feature\n"); |
| 1095 | else |
| 1096 | printf(" Device NOT support partitioning feature\n"); |
Ben Gardiner | 82bd950 | 2013-06-27 11:04:10 -0400 | [diff] [blame] | 1097 | if (reg & EXT_CSD_ENH_ATTRIBUTE_EN) |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1098 | printf(" Device can have enhanced tech.\n"); |
| 1099 | else |
| 1100 | printf(" Device cannot have enhanced tech.\n"); |
| 1101 | |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1102 | regl = (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_2] << 16) | |
Oliver Metz | 22f2641 | 2013-09-23 08:40:51 +0200 | [diff] [blame] | 1103 | (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_1] << 8) | |
| 1104 | ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_0]; |
| 1105 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1106 | printf("Max Enhanced Area Size [MAX_ENH_SIZE_MULT]: 0x%06x\n", |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1107 | regl); |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 1108 | unsigned int wp_sz = get_hc_wp_grp_size(ext_csd); |
| 1109 | unsigned int erase_sz = get_hc_erase_grp_size(ext_csd); |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1110 | printf(" i.e. %lu KiB\n", 512l * regl * wp_sz * erase_sz); |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 1111 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1112 | printf("Partitions attribute [PARTITIONS_ATTRIBUTE]: 0x%02x\n", |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 1113 | ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]); |
Ben Gardiner | a6cd98d | 2013-05-30 17:12:46 -0400 | [diff] [blame] | 1114 | reg = ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1115 | printf("Partitioning Setting" |
| 1116 | " [PARTITION_SETTING_COMPLETED]: 0x%02x\n", |
Ben Gardiner | a6cd98d | 2013-05-30 17:12:46 -0400 | [diff] [blame] | 1117 | reg); |
| 1118 | if (reg) |
| 1119 | printf(" Device partition setting complete\n"); |
| 1120 | else |
| 1121 | printf(" Device partition setting NOT complete\n"); |
| 1122 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1123 | printf("General Purpose Partition Size\n" |
| 1124 | " [GP_SIZE_MULT_4]: 0x%06x\n", (ext_csd[154] << 16) | |
| 1125 | (ext_csd[153] << 8) | ext_csd[152]); |
| 1126 | printf(" [GP_SIZE_MULT_3]: 0x%06x\n", (ext_csd[151] << 16) | |
| 1127 | (ext_csd[150] << 8) | ext_csd[149]); |
| 1128 | printf(" [GP_SIZE_MULT_2]: 0x%06x\n", (ext_csd[148] << 16) | |
| 1129 | (ext_csd[147] << 8) | ext_csd[146]); |
| 1130 | printf(" [GP_SIZE_MULT_1]: 0x%06x\n", (ext_csd[145] << 16) | |
| 1131 | (ext_csd[144] << 8) | ext_csd[143]); |
| 1132 | |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1133 | regl = (ext_csd[EXT_CSD_ENH_SIZE_MULT_2] << 16) | |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 1134 | (ext_csd[EXT_CSD_ENH_SIZE_MULT_1] << 8) | |
| 1135 | ext_csd[EXT_CSD_ENH_SIZE_MULT_0]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1136 | printf("Enhanced User Data Area Size" |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1137 | " [ENH_SIZE_MULT]: 0x%06x\n", regl); |
| 1138 | printf(" i.e. %lu KiB\n", 512l * regl * |
Ben Gardiner | f82e27a | 2013-05-30 17:12:50 -0400 | [diff] [blame] | 1139 | get_hc_erase_grp_size(ext_csd) * |
| 1140 | get_hc_wp_grp_size(ext_csd)); |
Ben Gardiner | 68f490b | 2013-05-30 17:12:48 -0400 | [diff] [blame] | 1141 | |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1142 | regl = (ext_csd[EXT_CSD_ENH_START_ADDR_3] << 24) | |
Ben Gardiner | 68f490b | 2013-05-30 17:12:48 -0400 | [diff] [blame] | 1143 | (ext_csd[EXT_CSD_ENH_START_ADDR_2] << 16) | |
| 1144 | (ext_csd[EXT_CSD_ENH_START_ADDR_1] << 8) | |
| 1145 | ext_csd[EXT_CSD_ENH_START_ADDR_0]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1146 | printf("Enhanced User Data Start Address" |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1147 | " [ENH_START_ADDR]: 0x%06x\n", regl); |
Ben Gardiner | 4e85023 | 2013-05-30 17:12:49 -0400 | [diff] [blame] | 1148 | printf(" i.e. %lu bytes offset\n", (is_blockaddresed(ext_csd) ? |
Oliver Metz | 11f2cea | 2013-09-23 08:40:52 +0200 | [diff] [blame] | 1149 | 1l : 512l) * regl); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1150 | |
| 1151 | /* A441]: reserved [135] */ |
| 1152 | printf("Bad Block Management mode" |
| 1153 | " [SEC_BAD_BLK_MGMNT]: 0x%02x\n", ext_csd[134]); |
| 1154 | /* A441: reserved [133:0] */ |
| 1155 | } |
| 1156 | /* B45 */ |
| 1157 | if (ext_csd_rev >= 6) { |
| 1158 | int j; |
| 1159 | /* tcase_support ext_csd[132] not readable */ |
| 1160 | printf("Periodic Wake-up [PERIODIC_WAKEUP]: 0x%02x\n", |
| 1161 | ext_csd[131]); |
| 1162 | printf("Program CID/CSD in DDR mode support" |
| 1163 | " [PROGRAM_CID_CSD_DDR_SUPPORT]: 0x%02x\n", |
| 1164 | ext_csd[130]); |
| 1165 | |
| 1166 | for (j = 127; j >= 64; j--) |
| 1167 | printf("Vendor Specific Fields" |
| 1168 | " [VENDOR_SPECIFIC_FIELD[%d]]: 0x%02x\n", |
| 1169 | j, ext_csd[j]); |
| 1170 | |
Gwendal Grignou | e966e67 | 2014-07-07 14:03:13 -0700 | [diff] [blame] | 1171 | reg = ext_csd[63]; |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1172 | printf("Native sector size [NATIVE_SECTOR_SIZE]: 0x%02x\n", |
Gwendal Grignou | e966e67 | 2014-07-07 14:03:13 -0700 | [diff] [blame] | 1173 | reg); |
| 1174 | if (reg == 0x00) |
| 1175 | printf(" i.e. 512 B\n"); |
| 1176 | else if (reg == 0x01) |
| 1177 | printf(" i.e. 4 KiB\n"); |
| 1178 | else |
| 1179 | printf(" i.e. Reserved\n"); |
| 1180 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1181 | printf("Sector size emulation [USE_NATIVE_SECTOR]: 0x%02x\n", |
| 1182 | ext_csd[62]); |
Gwendal Grignou | e966e67 | 2014-07-07 14:03:13 -0700 | [diff] [blame] | 1183 | reg = ext_csd[61]; |
| 1184 | printf("Sector size [DATA_SECTOR_SIZE]: 0x%02x\n", reg); |
| 1185 | if (reg == 0x00) |
| 1186 | printf(" i.e. 512 B\n"); |
| 1187 | else if (reg == 0x01) |
| 1188 | printf(" i.e. 4 KiB\n"); |
| 1189 | else |
| 1190 | printf(" i.e. Reserved\n"); |
| 1191 | |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1192 | printf("1st initialization after disabling sector" |
| 1193 | " size emulation [INI_TIMEOUT_EMU]: 0x%02x\n", |
| 1194 | ext_csd[60]); |
| 1195 | printf("Class 6 commands control [CLASS_6_CTRL]: 0x%02x\n", |
| 1196 | ext_csd[59]); |
| 1197 | printf("Number of addressed group to be Released" |
| 1198 | "[DYNCAP_NEEDED]: 0x%02x\n", ext_csd[58]); |
| 1199 | printf("Exception events control" |
| 1200 | " [EXCEPTION_EVENTS_CTRL]: 0x%04x\n", |
| 1201 | (ext_csd[57] << 8) | ext_csd[56]); |
| 1202 | printf("Exception events status" |
| 1203 | "[EXCEPTION_EVENTS_STATUS]: 0x%04x\n", |
| 1204 | (ext_csd[55] << 8) | ext_csd[54]); |
| 1205 | printf("Extended Partitions Attribute" |
| 1206 | " [EXT_PARTITIONS_ATTRIBUTE]: 0x%04x\n", |
| 1207 | (ext_csd[53] << 8) | ext_csd[52]); |
| 1208 | |
| 1209 | for (j = 51; j >= 37; j--) |
| 1210 | printf("Context configuration" |
| 1211 | " [CONTEXT_CONF[%d]]: 0x%02x\n", j, ext_csd[j]); |
| 1212 | |
| 1213 | printf("Packed command status" |
| 1214 | " [PACKED_COMMAND_STATUS]: 0x%02x\n", ext_csd[36]); |
| 1215 | printf("Packed command failure index" |
| 1216 | " [PACKED_FAILURE_INDEX]: 0x%02x\n", ext_csd[35]); |
| 1217 | printf("Power Off Notification" |
| 1218 | " [POWER_OFF_NOTIFICATION]: 0x%02x\n", ext_csd[34]); |
Oleg Matcovschi | 64f63a3 | 2013-05-23 17:11:07 -0700 | [diff] [blame] | 1219 | printf("Control to turn the Cache ON/OFF" |
| 1220 | " [CACHE_CTRL]: 0x%02x\n", ext_csd[33]); |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1221 | /* flush_cache ext_csd[32] not readable */ |
| 1222 | /*Reserved [31:0] */ |
| 1223 | } |
Gwendal Grignou | e966e67 | 2014-07-07 14:03:13 -0700 | [diff] [blame] | 1224 | if (ext_csd_rev >= 7) { |
| 1225 | printf("Mode config [MODE_CONFIG: 0x%02x]\n", ext_csd[30]); |
| 1226 | printf("Mode operation codes [MODE_OPERATION_CODES: 0x%02x]\n", |
| 1227 | ext_csd[29]); |
| 1228 | |
| 1229 | reg = ext_csd[26]; |
| 1230 | printf("FFU status [FFU_STATUS: 0x%02x]\n", reg); |
| 1231 | switch (reg) { |
| 1232 | case 0x00: |
| 1233 | printf(" Success\n"); |
| 1234 | break; |
| 1235 | case 0x10: |
| 1236 | printf(" General error\n"); |
| 1237 | break; |
| 1238 | case 0x11: |
| 1239 | printf(" Firmware install error\n"); |
| 1240 | break; |
| 1241 | case 0x12: |
| 1242 | printf(" Error in downloading firmware\n"); |
| 1243 | break; |
| 1244 | default: |
| 1245 | printf(" Reserved\n"); |
| 1246 | } |
| 1247 | printf("Pre loading data size [PRE_LOADING_DATA_SIZE] is" |
| 1248 | " %d sector size\n", |
| 1249 | get_word_from_ext_csd(&ext_csd[22])); |
| 1250 | printf("Max pre loading data size [MAX_PRE_LOADING_DATA_SIZE] is" |
| 1251 | " %d sector size\n", |
| 1252 | get_word_from_ext_csd(&ext_csd[18])); |
| 1253 | printf("Product state awareness enablement" |
| 1254 | " [PRODUCT_STATE_AWARENESS_ENABLEMENT: 0x%02x]\n", |
| 1255 | ext_csd[17]); |
| 1256 | printf("Secure Removal Type [SECURE_REMOVAL_TYPE: 0x%02x]\n", |
| 1257 | ext_csd[16]); |
| 1258 | } |
Giuseppe CAVALLARO | a5bf4a2 | 2012-02-20 09:45:29 +0100 | [diff] [blame] | 1259 | |
| 1260 | out_free: |
Johan RUDHOLM | a8bfde7 | 2012-02-12 11:46:44 -0500 | [diff] [blame] | 1261 | return ret; |
| 1262 | } |
Yaniv Gardi | 21bb473 | 2013-05-26 13:25:33 -0400 | [diff] [blame] | 1263 | |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 1264 | int do_dump_extcsd(int nargs, char **argv) |
| 1265 | { |
| 1266 | __u8 ext_csd[EXT_CSD_SIZE]; |
| 1267 | int fd, ret; |
| 1268 | char *device; |
| 1269 | int i, j; |
| 1270 | |
| 1271 | CHECK(nargs != 2, "Usage: mmc extcsd dump </path/to/mmcblkX>\n", |
| 1272 | exit(1)); |
| 1273 | |
| 1274 | device = argv[1]; |
| 1275 | |
| 1276 | fd = open(device, O_RDWR); |
| 1277 | if (fd < 0) { |
| 1278 | perror("Failed to open mmc device"); |
| 1279 | exit(1); |
| 1280 | } |
| 1281 | |
| 1282 | ret = read_extcsd(fd, ext_csd); |
| 1283 | if (ret) { |
| 1284 | fprintf(stderr, "Could not read EXT_CSD from %s\n", device); |
| 1285 | exit(1); |
| 1286 | } |
| 1287 | |
| 1288 | /* Dump all bytes so that any undecoded or proprietary registers */ |
| 1289 | /* can be acessed. */ |
| 1290 | printf("EXT_CSD binary dump:\n"); |
| 1291 | for (i = 0; i < EXT_CSD_SIZE; i+= 16) { |
| 1292 | printf(" %3d: %3x: ", i, i); |
| 1293 | for (j = 0; (j < 16) && (i + j < EXT_CSD_SIZE); j++) { |
| 1294 | printf(" %02x", ext_csd[i+j]); |
| 1295 | } |
| 1296 | printf("\n"); |
| 1297 | } |
| 1298 | |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
Yaniv Gardi | 21bb473 | 2013-05-26 13:25:33 -0400 | [diff] [blame] | 1302 | int do_sanitize(int nargs, char **argv) |
| 1303 | { |
| 1304 | int fd, ret; |
| 1305 | char *device; |
| 1306 | |
| 1307 | CHECK(nargs != 2, "Usage: mmc sanitize </path/to/mmcblkX>\n", |
| 1308 | exit(1)); |
| 1309 | |
| 1310 | device = argv[1]; |
| 1311 | |
| 1312 | fd = open(device, O_RDWR); |
| 1313 | if (fd < 0) { |
| 1314 | perror("open"); |
| 1315 | exit(1); |
| 1316 | } |
| 1317 | |
| 1318 | ret = write_extcsd_value(fd, EXT_CSD_SANITIZE_START, 1); |
| 1319 | if (ret) { |
| 1320 | fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", |
| 1321 | 1, EXT_CSD_SANITIZE_START, device); |
| 1322 | exit(1); |
| 1323 | } |
| 1324 | |
| 1325 | return ret; |
| 1326 | |
| 1327 | } |
| 1328 | |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1329 | static const char* const mmc_ffu_hack_names[] = { |
| 1330 | [MMC_OVERRIDE_FFU_ARG] = "ffu_arg", |
| 1331 | }; |
| 1332 | |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1333 | int do_emmc50_ffu (int nargs, char **argv) |
| 1334 | { |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1335 | int fd, ret, i, argc=1, ffu_hack=0; |
| 1336 | char *device, *type, *path; |
| 1337 | __u64 value; |
| 1338 | union { |
| 1339 | __u8 data[FFU_DATA_SIZE]; |
| 1340 | struct mmc_ffu_args ffu_args; |
| 1341 | } ffu_data; |
| 1342 | struct mmc_ffu_args *ffu_args = &ffu_data.ffu_args; |
Gwendal Grignou | 0f75734 | 2014-10-16 16:52:46 -0700 | [diff] [blame] | 1343 | struct mmc_ioc_cmd mmc_ioc_cmd; |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1344 | |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1345 | while (!strcmp("-k", argv[argc])) { |
| 1346 | ret = sscanf(argv[++argc], "%m[^:]:0x%llx", &type, &value); |
| 1347 | if (ret < 1) { |
| 1348 | fprintf(stderr, "Invalid hack: %s\n", argv[argc]); |
| 1349 | exit(1); |
| 1350 | } |
| 1351 | for (i = 0; i < MMC_HACK_LEN; i++) { |
| 1352 | if (!strcmp(type, mmc_ffu_hack_names[i])) { |
| 1353 | ffu_args->hack[ffu_hack].type = i; |
| 1354 | if (ret == 2) { |
| 1355 | ffu_args->hack[ffu_hack].value = value; |
| 1356 | } |
| 1357 | ffu_hack++; |
| 1358 | if (ffu_hack * sizeof(struct mmc_ffu_hack) + |
| 1359 | sizeof(struct mmc_ffu_args) > |
| 1360 | FFU_DATA_SIZE) { |
| 1361 | fprintf(stderr, "Too many %d hacks", |
| 1362 | ffu_hack); |
| 1363 | exit(1); |
| 1364 | } |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | if (i == MMC_HACK_LEN) { |
| 1369 | fprintf(stderr, "Hack type %s not found\n", type); |
| 1370 | fprintf(stderr, "Supported types are: "); |
| 1371 | for (i = 0; i < MMC_HACK_LEN; i++) |
| 1372 | fprintf(stderr, "%s%s", mmc_ffu_hack_names[i], |
| 1373 | (i == MMC_HACK_LEN-1 ? "\n": ", ")); |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1374 | |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1375 | exit(1); |
| 1376 | } |
| 1377 | free(type); |
| 1378 | argc++; |
| 1379 | } |
| 1380 | ffu_args->hack_nb = ffu_hack; |
| 1381 | |
| 1382 | path = argv[argc++]; |
| 1383 | if (strlen(path) >= FFU_NAME_LEN) { |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1384 | fprintf(stderr, "Filename \"%.20s\" too long\n", path); |
| 1385 | exit(1); |
| 1386 | } |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1387 | strcpy(ffu_args->name, path); |
| 1388 | device = argv[argc++]; |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1389 | fd = open(device, O_RDWR); |
| 1390 | if (fd < 0) { |
| 1391 | perror("open"); |
| 1392 | exit(1); |
| 1393 | } |
| 1394 | |
Gwendal Grignou | 0f75734 | 2014-10-16 16:52:46 -0700 | [diff] [blame] | 1395 | /* prepare and send ioctl */ |
| 1396 | memset(&mmc_ioc_cmd, 0, sizeof(mmc_ioc_cmd)); |
| 1397 | mmc_ioc_cmd.opcode = MMC_FFU_INVOKE_OP; |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1398 | mmc_ioc_cmd.blksz = FFU_DATA_SIZE; |
Gwendal Grignou | 0f75734 | 2014-10-16 16:52:46 -0700 | [diff] [blame] | 1399 | mmc_ioc_cmd.blocks = 1; |
| 1400 | mmc_ioc_cmd.arg = 0; |
| 1401 | mmc_ioc_cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1402 | mmc_ioc_cmd.write_flag = 1; |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 1403 | mmc_ioc_cmd_set_data(mmc_ioc_cmd, ffu_args); |
Gwendal Grignou | 0f75734 | 2014-10-16 16:52:46 -0700 | [diff] [blame] | 1404 | ret = ioctl(fd, MMC_IOC_CMD, &mmc_ioc_cmd); |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 1405 | if (ret) { |
| 1406 | fprintf(stderr, "FFU install failed : %s\n", strerror(errno)); |
| 1407 | exit(1); |
| 1408 | } |
| 1409 | |
| 1410 | close(fd); |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |