blob: 2fc8d4a7a7759dfe09ff3a887c5928289aaf9ec4 [file] [log] [blame]
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -05001/*
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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/ioctl.h>
21#include <sys/types.h>
22#include <dirent.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26#include <libgen.h>
27#include <limits.h>
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050028#include <ctype.h>
Roman Peniaevc6cb0532014-08-12 23:25:45 +090029#include <errno.h>
30#include <stdint.h>
31#include <assert.h>
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050032
33#include "mmc.h"
34#include "mmc_cmds.h"
Roman Peniaevc6cb0532014-08-12 23:25:45 +090035#include "3rdparty/hmac_sha/hmac_sha2.h"
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050036
37int read_extcsd(int fd, __u8 *ext_csd)
38{
39 int ret = 0;
40 struct mmc_ioc_cmd idata;
41 memset(&idata, 0, sizeof(idata));
42 memset(ext_csd, 0, sizeof(__u8) * 512);
43 idata.write_flag = 0;
44 idata.opcode = MMC_SEND_EXT_CSD;
45 idata.arg = 0;
46 idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
47 idata.blksz = 512;
48 idata.blocks = 1;
49 mmc_ioc_cmd_set_data(idata, ext_csd);
50
51 ret = ioctl(fd, MMC_IOC_CMD, &idata);
52 if (ret)
53 perror("ioctl");
54
55 return ret;
56}
57
58int write_extcsd_value(int fd, __u8 index, __u8 value)
59{
60 int ret = 0;
61 struct mmc_ioc_cmd idata;
62
63 memset(&idata, 0, sizeof(idata));
64 idata.write_flag = 1;
65 idata.opcode = MMC_SWITCH;
66 idata.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
67 (index << 16) |
68 (value << 8) |
69 EXT_CSD_CMD_SET_NORMAL;
70 idata.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
71
72 ret = ioctl(fd, MMC_IOC_CMD, &idata);
73 if (ret)
74 perror("ioctl");
75
76 return ret;
77}
78
Ben Gardiner27c357d2013-05-30 17:12:47 -040079int send_status(int fd, __u32 *response)
80{
81 int ret = 0;
82 struct mmc_ioc_cmd idata;
83
84 memset(&idata, 0, sizeof(idata));
85 idata.opcode = MMC_SEND_STATUS;
86 idata.arg = (1 << 16);
87 idata.flags = MMC_RSP_R1 | MMC_CMD_AC;
88
89 ret = ioctl(fd, MMC_IOC_CMD, &idata);
90 if (ret)
91 perror("ioctl");
92
93 *response = idata.response[0];
94
95 return ret;
96}
97
Chris Ballb9c7a172012-02-20 12:34:25 -050098void print_writeprotect_status(__u8 *ext_csd)
99{
100 __u8 reg;
101 __u8 ext_csd_rev = ext_csd[192];
102
103 /* A43: reserved [174:0] */
104 if (ext_csd_rev >= 5) {
105 printf("Boot write protection status registers"
106 " [BOOT_WP_STATUS]: 0x%02x\n", ext_csd[174]);
107
108 reg = ext_csd[EXT_CSD_BOOT_WP];
109 printf("Boot Area Write protection [BOOT_WP]: 0x%02x\n", reg);
110 printf(" Power ro locking: ");
111 if (reg & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
112 printf("not possible\n");
113 else
114 printf("possible\n");
115
116 printf(" Permanent ro locking: ");
117 if (reg & EXT_CSD_BOOT_WP_B_PERM_WP_DIS)
118 printf("not possible\n");
119 else
120 printf("possible\n");
121
122 printf(" ro lock status: ");
123 if (reg & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
124 printf("locked until next power on\n");
125 else if (reg & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
126 printf("locked permanently\n");
127 else
128 printf("not locked\n");
129 }
130}
131
132int do_writeprotect_get(int nargs, char **argv)
133{
134 __u8 ext_csd[512];
135 int fd, ret;
136 char *device;
137
Chris Ball8ba44662012-04-19 13:22:54 -0400138 CHECK(nargs != 2, "Usage: mmc writeprotect get </path/to/mmcblkX>\n",
139 exit(1));
Chris Ballb9c7a172012-02-20 12:34:25 -0500140
141 device = argv[1];
142
143 fd = open(device, O_RDWR);
144 if (fd < 0) {
145 perror("open");
146 exit(1);
147 }
148
149 ret = read_extcsd(fd, ext_csd);
150 if (ret) {
151 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
152 exit(1);
153 }
154
155 print_writeprotect_status(ext_csd);
156
157 return ret;
158}
159
160int do_writeprotect_set(int nargs, char **argv)
161{
162 __u8 ext_csd[512], value;
163 int fd, ret;
164 char *device;
165
Chris Ball8ba44662012-04-19 13:22:54 -0400166 CHECK(nargs != 2, "Usage: mmc writeprotect set </path/to/mmcblkX>\n",
167 exit(1));
Chris Ballb9c7a172012-02-20 12:34:25 -0500168
169 device = argv[1];
170
171 fd = open(device, O_RDWR);
172 if (fd < 0) {
173 perror("open");
174 exit(1);
175 }
176
177 ret = read_extcsd(fd, ext_csd);
178 if (ret) {
179 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
180 exit(1);
181 }
182
183 value = ext_csd[EXT_CSD_BOOT_WP] |
184 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
185 ret = write_extcsd_value(fd, EXT_CSD_BOOT_WP, value);
186 if (ret) {
187 fprintf(stderr, "Could not write 0x%02x to "
188 "EXT_CSD[%d] in %s\n",
189 value, EXT_CSD_BOOT_WP, device);
190 exit(1);
191 }
192
193 return ret;
194}
195
Saugata Dasb7e25992012-05-17 09:26:34 -0400196int do_disable_512B_emulation(int nargs, char **argv)
197{
198 __u8 ext_csd[512], native_sector_size, data_sector_size, wr_rel_param;
199 int fd, ret;
200 char *device;
201
202 CHECK(nargs != 2, "Usage: mmc disable 512B emulation </path/to/mmcblkX>\n", exit(1));
203 device = argv[1];
204
205 fd = open(device, O_RDWR);
206 if (fd < 0) {
207 perror("open");
208 exit(1);
209 }
210
211 ret = read_extcsd(fd, ext_csd);
212 if (ret) {
213 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
214 exit(1);
215 }
216
217 wr_rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
218 native_sector_size = ext_csd[EXT_CSD_NATIVE_SECTOR_SIZE];
219 data_sector_size = ext_csd[EXT_CSD_DATA_SECTOR_SIZE];
220
221 if (native_sector_size && !data_sector_size &&
222 (wr_rel_param & EN_REL_WR)) {
223 ret = write_extcsd_value(fd, EXT_CSD_USE_NATIVE_SECTOR, 1);
224
225 if (ret) {
226 fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
227 1, EXT_CSD_BOOT_WP, device);
228 exit(1);
229 }
230 printf("MMC disable 512B emulation successful. Now reset the device to switch to 4KB native sector mode.\n");
231 } else if (native_sector_size && data_sector_size) {
232 printf("MMC 512B emulation mode is already disabled; doing nothing.\n");
233 } else {
234 printf("MMC does not support disabling 512B emulation mode.\n");
235 }
236
237 return ret;
238}
239
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +0200240int do_write_boot_en(int nargs, char **argv)
241{
242 __u8 ext_csd[512];
243 __u8 value = 0;
244 int fd, ret;
245 char *device;
246 int boot_area, send_ack;
247
248 CHECK(nargs != 4, "Usage: mmc bootpart enable <partition_number> "
249 "<send_ack> </path/to/mmcblkX>\n", exit(1));
250
251 /*
252 * If <send_ack> is 1, the device will send acknowledgment
253 * pattern "010" to the host when boot operation begins.
254 * If <send_ack> is 0, it won't.
255 */
256 boot_area = strtol(argv[1], NULL, 10);
257 send_ack = strtol(argv[2], NULL, 10);
258 device = argv[3];
259
260 fd = open(device, O_RDWR);
261 if (fd < 0) {
262 perror("open");
263 exit(1);
264 }
265
266 ret = read_extcsd(fd, ext_csd);
267 if (ret) {
268 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
269 exit(1);
270 }
271
272 value = ext_csd[EXT_CSD_PART_CONFIG];
273
274 switch (boot_area) {
275 case EXT_CSD_PART_CONFIG_ACC_BOOT0:
276 value |= (1 << 3);
277 value &= ~(3 << 4);
278 break;
279 case EXT_CSD_PART_CONFIG_ACC_BOOT1:
280 value |= (1 << 4);
281 value &= ~(1 << 3);
282 value &= ~(1 << 5);
283 break;
284 case EXT_CSD_PART_CONFIG_ACC_USER_AREA:
285 value |= (boot_area << 3);
286 break;
287 default:
288 fprintf(stderr, "Cannot enable the boot area\n");
289 exit(1);
290 }
291 if (send_ack)
292 value |= EXT_CSD_PART_CONFIG_ACC_ACK;
293 else
294 value &= ~EXT_CSD_PART_CONFIG_ACC_ACK;
295
296 ret = write_extcsd_value(fd, EXT_CSD_PART_CONFIG, value);
297 if (ret) {
298 fprintf(stderr, "Could not write 0x%02x to "
299 "EXT_CSD[%d] in %s\n",
300 value, EXT_CSD_PART_CONFIG, device);
301 exit(1);
302 }
303 return ret;
304}
305
Chris Ballf74dfe22012-10-19 16:49:55 -0400306int do_hwreset(int value, int nargs, char **argv)
307{
308 __u8 ext_csd[512];
309 int fd, ret;
310 char *device;
311
312 CHECK(nargs != 2, "Usage: mmc hwreset enable </path/to/mmcblkX>\n",
313 exit(1));
314
315 device = argv[1];
316
317 fd = open(device, O_RDWR);
318 if (fd < 0) {
319 perror("open");
320 exit(1);
321 }
322
323 ret = read_extcsd(fd, ext_csd);
324 if (ret) {
325 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
326 exit(1);
327 }
328
329 if ((ext_csd[EXT_CSD_RST_N_FUNCTION] & EXT_CSD_RST_N_EN_MASK) ==
330 EXT_CSD_HW_RESET_EN) {
331 fprintf(stderr,
332 "H/W Reset is already permanently enabled on %s\n",
333 device);
334 exit(1);
335 }
336 if ((ext_csd[EXT_CSD_RST_N_FUNCTION] & EXT_CSD_RST_N_EN_MASK) ==
337 EXT_CSD_HW_RESET_DIS) {
338 fprintf(stderr,
339 "H/W Reset is already permanently disabled on %s\n",
340 device);
341 exit(1);
342 }
343
344 ret = write_extcsd_value(fd, EXT_CSD_RST_N_FUNCTION, value);
345 if (ret) {
346 fprintf(stderr,
347 "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
348 value, EXT_CSD_RST_N_FUNCTION, device);
349 exit(1);
350 }
351
352 return ret;
353}
354
355int do_hwreset_en(int nargs, char **argv)
356{
357 return do_hwreset(EXT_CSD_HW_RESET_EN, nargs, argv);
358}
359
360int do_hwreset_dis(int nargs, char **argv)
361{
362 return do_hwreset(EXT_CSD_HW_RESET_DIS, nargs, argv);
363}
364
Jaehoon Chung86496512012-09-21 10:08:05 +0000365int do_write_bkops_en(int nargs, char **argv)
366{
367 __u8 ext_csd[512], value = 0;
368 int fd, ret;
369 char *device;
370
371 CHECK(nargs != 2, "Usage: mmc bkops enable </path/to/mmcblkX>\n",
372 exit(1));
373
374 device = argv[1];
375
376 fd = open(device, O_RDWR);
377 if (fd < 0) {
378 perror("open");
379 exit(1);
380 }
381
382 ret = read_extcsd(fd, ext_csd);
383 if (ret) {
384 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
385 exit(1);
386 }
387
388 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
389 fprintf(stderr, "%s doesn't support BKOPS\n", device);
390 exit(1);
391 }
392
393 ret = write_extcsd_value(fd, EXT_CSD_BKOPS_EN, BKOPS_ENABLE);
394 if (ret) {
395 fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
396 value, EXT_CSD_BKOPS_EN, device);
397 exit(1);
398 }
399
400 return ret;
401}
402
Ben Gardiner27c357d2013-05-30 17:12:47 -0400403int do_status_get(int nargs, char **argv)
404{
405 __u32 response;
406 int fd, ret;
407 char *device;
408
409 CHECK(nargs != 2, "Usage: mmc status get </path/to/mmcblkX>\n",
410 exit(1));
411
412 device = argv[1];
413
414 fd = open(device, O_RDWR);
415 if (fd < 0) {
416 perror("open");
417 exit(1);
418 }
419
420 ret = send_status(fd, &response);
421 if (ret) {
422 fprintf(stderr, "Could not read response to SEND_STATUS from %s\n", device);
423 exit(1);
424 }
425
426 printf("SEND_STATUS response: 0x%08x\n", response);
427
428 return ret;
429}
430
Ben Gardiner4e850232013-05-30 17:12:49 -0400431unsigned int get_sector_count(__u8 *ext_csd)
432{
433 return (ext_csd[EXT_CSD_SEC_COUNT_3] << 24) |
434 (ext_csd[EXT_CSD_SEC_COUNT_2] << 16) |
435 (ext_csd[EXT_CSD_SEC_COUNT_1] << 8) |
436 ext_csd[EXT_CSD_SEC_COUNT_0];
437}
438
439int is_blockaddresed(__u8 *ext_csd)
440{
441 unsigned int sectors = get_sector_count(ext_csd);
442
443 return (sectors > (2u * 1024 * 1024 * 1024) / 512);
444}
445
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400446unsigned int get_hc_wp_grp_size(__u8 *ext_csd)
447{
448 return ext_csd[221];
449}
450
451unsigned int get_hc_erase_grp_size(__u8 *ext_csd)
452{
453 return ext_csd[224];
454}
455
Ben Gardinere6e84e92013-09-19 11:14:27 -0400456int set_partitioning_setting_completed(int dry_run, const char * const device,
457 int fd)
458{
459 int ret;
460
461 if (dry_run) {
462 fprintf(stderr, "NOT setting PARTITION_SETTING_COMPLETED\n");
463 fprintf(stderr, "These changes will not take effect neither "
464 "now nor after a power cycle\n");
465 return 1;
466 }
467
468 fprintf(stderr, "setting OTP PARTITION_SETTING_COMPLETED!\n");
469 ret = write_extcsd_value(fd, EXT_CSD_PARTITION_SETTING_COMPLETED, 0x1);
470 if (ret) {
471 fprintf(stderr, "Could not write 0x1 to "
472 "EXT_CSD[%d] in %s\n",
473 EXT_CSD_PARTITION_SETTING_COMPLETED, device);
474 return 1;
475 }
476
477 __u32 response;
478 ret = send_status(fd, &response);
479 if (ret) {
480 fprintf(stderr, "Could not get response to SEND_STATUS "
481 "from %s\n", device);
482 return 1;
483 }
484
485 if (response & R1_SWITCH_ERROR) {
486 fprintf(stderr, "Setting OTP PARTITION_SETTING_COMPLETED "
487 "failed on %s\n", device);
488 return 1;
489 }
490
491 fprintf(stderr, "Setting OTP PARTITION_SETTING_COMPLETED on "
492 "%s SUCCESS\n", device);
493 fprintf(stderr, "Device power cycle needed for settings to "
494 "take effect.\n"
495 "Confirm that PARTITION_SETTING_COMPLETED bit is set "
496 "using 'extcsd read' after power cycle\n");
497
498 return 0;
499}
500
Balaji T K4afc8c82015-04-29 18:12:32 -0400501int check_enhanced_area_total_limit(const char * const device, int fd)
502{
503 __u8 ext_csd[512];
504 __u32 regl;
505 unsigned long max_enh_area_sz, user_area_sz, enh_area_sz = 0;
506 unsigned long gp4_part_sz, gp3_part_sz, gp2_part_sz, gp1_part_sz;
507 unsigned int wp_sz, erase_sz;
508 int ret;
509
510 ret = read_extcsd(fd, ext_csd);
511 if (ret) {
512 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
513 exit(1);
514 }
515 wp_sz = get_hc_wp_grp_size(ext_csd);
516 erase_sz = get_hc_erase_grp_size(ext_csd);
517
518 regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_4_2] << 16) |
519 (ext_csd[EXT_CSD_GP_SIZE_MULT_4_1] << 8) |
520 ext_csd[EXT_CSD_GP_SIZE_MULT_4_0];
521 gp4_part_sz = 512l * regl * erase_sz * wp_sz;
522 if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_4) {
523 enh_area_sz += gp4_part_sz;
524 printf("Enhanced GP4 Partition Size [GP_SIZE_MULT_4]: 0x%06x\n", regl);
525 printf(" i.e. %lu KiB\n", gp4_part_sz);
526 }
527
528 regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_3_2] << 16) |
529 (ext_csd[EXT_CSD_GP_SIZE_MULT_3_1] << 8) |
530 ext_csd[EXT_CSD_GP_SIZE_MULT_3_0];
531 gp3_part_sz = 512l * regl * erase_sz * wp_sz;
532 if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_3) {
533 enh_area_sz += gp3_part_sz;
534 printf("Enhanced GP3 Partition Size [GP_SIZE_MULT_3]: 0x%06x\n", regl);
535 printf(" i.e. %lu KiB\n", gp3_part_sz);
536 }
537
538 regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_2_2] << 16) |
539 (ext_csd[EXT_CSD_GP_SIZE_MULT_2_1] << 8) |
540 ext_csd[EXT_CSD_GP_SIZE_MULT_2_0];
541 gp2_part_sz = 512l * regl * erase_sz * wp_sz;
542 if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_2) {
543 enh_area_sz += gp2_part_sz;
544 printf("Enhanced GP2 Partition Size [GP_SIZE_MULT_2]: 0x%06x\n", regl);
545 printf(" i.e. %lu KiB\n", gp2_part_sz);
546 }
547
548 regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_1_2] << 16) |
549 (ext_csd[EXT_CSD_GP_SIZE_MULT_1_1] << 8) |
550 ext_csd[EXT_CSD_GP_SIZE_MULT_1_0];
551 gp1_part_sz = 512l * regl * erase_sz * wp_sz;
552 if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_1) {
553 enh_area_sz += gp1_part_sz;
554 printf("Enhanced GP1 Partition Size [GP_SIZE_MULT_1]: 0x%06x\n", regl);
555 printf(" i.e. %lu KiB\n", gp1_part_sz);
556 }
557
558 regl = (ext_csd[EXT_CSD_ENH_SIZE_MULT_2] << 16) |
559 (ext_csd[EXT_CSD_ENH_SIZE_MULT_1] << 8) |
560 ext_csd[EXT_CSD_ENH_SIZE_MULT_0];
561 user_area_sz = 512l * regl * erase_sz * wp_sz;
562 if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_USR) {
563 enh_area_sz += user_area_sz;
564 printf("Enhanced User Data Area Size [ENH_SIZE_MULT]: 0x%06x\n", regl);
565 printf(" i.e. %lu KiB\n", user_area_sz);
566 }
567
568 regl = (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_2] << 16) |
569 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_1] << 8) |
570 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_0];
571 max_enh_area_sz = 512l * regl * erase_sz * wp_sz;
572 printf("Max Enhanced Area Size [MAX_ENH_SIZE_MULT]: 0x%06x\n", regl);
573 printf(" i.e. %lu KiB\n", max_enh_area_sz);
574 if (enh_area_sz > max_enh_area_sz) {
575 fprintf(stderr,
576 "Programmed total enhanced size %lu KiB cannot exceed max enhanced area %lu KiB %s\n",
577 enh_area_sz, max_enh_area_sz, device);
578 return 1;
579 }
580
581 return 0;
582}
583
Ben Gardinerd91d3692013-05-30 17:12:51 -0400584int do_enh_area_set(int nargs, char **argv)
585{
586 __u8 value;
587 __u8 ext_csd[512];
588 int fd, ret;
589 char *device;
590 int dry_run = 1;
591 unsigned int start_kib, length_kib, enh_start_addr, enh_size_mult;
592 unsigned long align;
593
594 CHECK(nargs != 5, "Usage: mmc enh_area set <-y|-n> <start KiB> <length KiB> "
595 "</path/to/mmcblkX>\n", exit(1));
596
597 if (!strcmp("-y", argv[1]))
598 dry_run = 0;
599
600 start_kib = strtol(argv[2], NULL, 10);
601 length_kib = strtol(argv[3], NULL, 10);
602 device = argv[4];
603
604 fd = open(device, O_RDWR);
605 if (fd < 0) {
606 perror("open");
607 exit(1);
608 }
609
610 ret = read_extcsd(fd, ext_csd);
611 if (ret) {
612 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
613 exit(1);
614 }
615
616 /* assert ENH_ATTRIBUTE_EN */
617 if (!(ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & EXT_CSD_ENH_ATTRIBUTE_EN))
618 {
619 printf(" Device cannot have enhanced tech.\n");
620 exit(1);
621 }
622
623 /* assert not PARTITION_SETTING_COMPLETED */
624 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED])
625 {
626 printf(" Device is already partitioned\n");
627 exit(1);
628 }
629
630 align = 512l * get_hc_wp_grp_size(ext_csd) * get_hc_erase_grp_size(ext_csd);
631
632 enh_size_mult = (length_kib + align/2l) / align;
633
634 enh_start_addr = start_kib * 1024 / (is_blockaddresed(ext_csd) ? 512 : 1);
635 enh_start_addr /= align;
636 enh_start_addr *= align;
637
638 /* set EXT_CSD_ERASE_GROUP_DEF bit 0 */
639 ret = write_extcsd_value(fd, EXT_CSD_ERASE_GROUP_DEF, 0x1);
640 if (ret) {
641 fprintf(stderr, "Could not write 0x1 to "
642 "EXT_CSD[%d] in %s\n",
643 EXT_CSD_ERASE_GROUP_DEF, device);
644 exit(1);
645 }
646
647 /* write to ENH_START_ADDR and ENH_SIZE_MULT and PARTITIONS_ATTRIBUTE's ENH_USR bit */
648 value = (enh_start_addr >> 24) & 0xff;
649 ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_3, value);
650 if (ret) {
651 fprintf(stderr, "Could not write 0x%02x to "
652 "EXT_CSD[%d] in %s\n", value,
653 EXT_CSD_ENH_START_ADDR_3, device);
654 exit(1);
655 }
656 value = (enh_start_addr >> 16) & 0xff;
657 ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_2, value);
658 if (ret) {
659 fprintf(stderr, "Could not write 0x%02x to "
660 "EXT_CSD[%d] in %s\n", value,
661 EXT_CSD_ENH_START_ADDR_2, device);
662 exit(1);
663 }
664 value = (enh_start_addr >> 8) & 0xff;
665 ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_1, value);
666 if (ret) {
667 fprintf(stderr, "Could not write 0x%02x to "
668 "EXT_CSD[%d] in %s\n", value,
669 EXT_CSD_ENH_START_ADDR_1, device);
670 exit(1);
671 }
672 value = enh_start_addr & 0xff;
673 ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_0, value);
674 if (ret) {
675 fprintf(stderr, "Could not write 0x%02x to "
676 "EXT_CSD[%d] in %s\n", value,
677 EXT_CSD_ENH_START_ADDR_0, device);
678 exit(1);
679 }
680
681 value = (enh_size_mult >> 16) & 0xff;
682 ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_2, value);
683 if (ret) {
684 fprintf(stderr, "Could not write 0x%02x to "
685 "EXT_CSD[%d] in %s\n", value,
686 EXT_CSD_ENH_SIZE_MULT_2, device);
687 exit(1);
688 }
689 value = (enh_size_mult >> 8) & 0xff;
690 ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_1, value);
691 if (ret) {
692 fprintf(stderr, "Could not write 0x%02x to "
693 "EXT_CSD[%d] in %s\n", value,
694 EXT_CSD_ENH_SIZE_MULT_1, device);
695 exit(1);
696 }
697 value = enh_size_mult & 0xff;
698 ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_0, value);
699 if (ret) {
700 fprintf(stderr, "Could not write 0x%02x to "
701 "EXT_CSD[%d] in %s\n", value,
702 EXT_CSD_ENH_SIZE_MULT_0, device);
703 exit(1);
704 }
Balaji T K4afc8c82015-04-29 18:12:32 -0400705 value = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] | EXT_CSD_ENH_USR;
706 ret = write_extcsd_value(fd, EXT_CSD_PARTITIONS_ATTRIBUTE, value);
Ben Gardinerd91d3692013-05-30 17:12:51 -0400707 if (ret) {
708 fprintf(stderr, "Could not write EXT_CSD_ENH_USR to "
709 "EXT_CSD[%d] in %s\n",
710 EXT_CSD_PARTITIONS_ATTRIBUTE, device);
711 exit(1);
712 }
713
Balaji T K4afc8c82015-04-29 18:12:32 -0400714 ret = check_enhanced_area_total_limit(device, fd);
715 if (ret)
716 exit(1);
717
Ben Gardinere6e84e92013-09-19 11:14:27 -0400718 printf("Done setting ENH_USR area on %s\n", device);
Ben Gardinerd91d3692013-05-30 17:12:51 -0400719
Ben Gardinere6e84e92013-09-19 11:14:27 -0400720 if (!set_partitioning_setting_completed(dry_run, device, fd))
Ben Gardinerd91d3692013-05-30 17:12:51 -0400721 exit(1);
Ben Gardinerd91d3692013-05-30 17:12:51 -0400722
723 return 0;
724}
725
Ben Gardiner196d0d22013-09-19 11:14:29 -0400726int do_write_reliability_set(int nargs, char **argv)
727{
728 __u8 value;
729 __u8 ext_csd[512];
730 int fd, ret;
731
732 int dry_run = 1;
733 int partition;
734 char *device;
735
736 CHECK(nargs != 4, "Usage: mmc write_reliability set <-y|-n> "
737 "<partition> </path/to/mmcblkX>\n", exit(1));
738
739 if (!strcmp("-y", argv[1]))
740 dry_run = 0;
741
742 partition = strtol(argv[2], NULL, 10);
743 device = argv[3];
744
745 fd = open(device, O_RDWR);
746 if (fd < 0) {
747 perror("open");
748 exit(1);
749 }
750
751 ret = read_extcsd(fd, ext_csd);
752 if (ret) {
753 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
754 exit(1);
755 }
756
757 /* assert not PARTITION_SETTING_COMPLETED */
758 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED])
759 {
760 printf(" Device is already partitioned\n");
761 exit(1);
762 }
763
764 /* assert HS_CTRL_REL */
765 if (!(ext_csd[EXT_CSD_WR_REL_PARAM] & HS_CTRL_REL)) {
766 printf("Cannot set write reliability parameters, WR_REL_SET is "
767 "read-only\n");
768 exit(1);
769 }
770
771 value = ext_csd[EXT_CSD_WR_REL_SET] | (1<<partition);
772 ret = write_extcsd_value(fd, EXT_CSD_WR_REL_SET, value);
773 if (ret) {
774 fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
775 value, EXT_CSD_WR_REL_SET, device);
776 exit(1);
777 }
778
779 printf("Done setting EXT_CSD_WR_REL_SET to 0x%02x on %s\n",
780 value, device);
781
782 if (!set_partitioning_setting_completed(dry_run, device, fd))
783 exit(1);
784
785 return 0;
786}
787
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500788int do_read_extcsd(int nargs, char **argv)
789{
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100790 __u8 ext_csd[512], ext_csd_rev, reg;
Oliver Metz11f2cea2013-09-23 08:40:52 +0200791 __u32 regl;
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500792 int fd, ret;
793 char *device;
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100794 const char *str;
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500795
Chris Ball8ba44662012-04-19 13:22:54 -0400796 CHECK(nargs != 2, "Usage: mmc extcsd read </path/to/mmcblkX>\n",
797 exit(1));
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500798
799 device = argv[1];
800
801 fd = open(device, O_RDWR);
802 if (fd < 0) {
803 perror("open");
804 exit(1);
805 }
806
807 ret = read_extcsd(fd, ext_csd);
808 if (ret) {
809 fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
810 exit(1);
811 }
812
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100813 ext_csd_rev = ext_csd[192];
814
815 switch (ext_csd_rev) {
816 case 6:
817 str = "4.5";
818 break;
819 case 5:
820 str = "4.41";
821 break;
822 case 3:
823 str = "4.3";
824 break;
825 case 2:
826 str = "4.2";
827 break;
828 case 1:
829 str = "4.1";
830 break;
831 case 0:
832 str = "4.0";
833 break;
834 default:
835 goto out_free;
836 }
837 printf("=============================================\n");
838 printf(" Extended CSD rev 1.%d (MMC %s)\n", ext_csd_rev, str);
839 printf("=============================================\n\n");
840
841 if (ext_csd_rev < 3)
842 goto out_free; /* No ext_csd */
843
844 /* Parse the Extended CSD registers.
845 * Reserved bit should be read as "0" in case of spec older
846 * than A441.
847 */
848 reg = ext_csd[EXT_CSD_S_CMD_SET];
849 printf("Card Supported Command sets [S_CMD_SET: 0x%02x]\n", reg);
850 if (!reg)
Chris Ballb9c7a172012-02-20 12:34:25 -0500851 printf(" - Standard MMC command sets\n");
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100852
853 reg = ext_csd[EXT_CSD_HPI_FEATURE];
854 printf("HPI Features [HPI_FEATURE: 0x%02x]: ", reg);
855 if (reg & EXT_CSD_HPI_SUPP) {
856 if (reg & EXT_CSD_HPI_IMPL)
Chris Ballb9c7a172012-02-20 12:34:25 -0500857 printf("implementation based on CMD12\n");
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100858 else
859 printf("implementation based on CMD13\n");
860 }
861
862 printf("Background operations support [BKOPS_SUPPORT: 0x%02x]\n",
863 ext_csd[502]);
864
865 if (ext_csd_rev >= 6) {
866 printf("Max Packet Read Cmd [MAX_PACKED_READS: 0x%02x]\n",
867 ext_csd[501]);
868 printf("Max Packet Write Cmd [MAX_PACKED_WRITES: 0x%02x]\n",
869 ext_csd[500]);
870 printf("Data TAG support [DATA_TAG_SUPPORT: 0x%02x]\n",
871 ext_csd[499]);
872
873 printf("Data TAG Unit Size [TAG_UNIT_SIZE: 0x%02x]\n",
874 ext_csd[498]);
875 printf("Tag Resources Size [TAG_RES_SIZE: 0x%02x]\n",
876 ext_csd[497]);
877 printf("Context Management Capabilities"
878 " [CONTEXT_CAPABILITIES: 0x%02x]\n", ext_csd[496]);
879 printf("Large Unit Size [LARGE_UNIT_SIZE_M1: 0x%02x]\n",
880 ext_csd[495]);
881 printf("Extended partition attribute support"
882 " [EXT_SUPPORT: 0x%02x]\n", ext_csd[494]);
883 printf("Generic CMD6 Timer [GENERIC_CMD6_TIME: 0x%02x]\n",
884 ext_csd[248]);
885 printf("Power off notification [POWER_OFF_LONG_TIME: 0x%02x]\n",
886 ext_csd[247]);
887 printf("Cache Size [CACHE_SIZE] is %d KiB\n",
888 ext_csd[249] << 0 | (ext_csd[250] << 8) |
889 (ext_csd[251] << 16) | (ext_csd[252] << 24));
890 }
891
892 /* A441: Reserved [501:247]
893 A43: reserved [246:229] */
894 if (ext_csd_rev >= 5) {
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100895 printf("Background operations status"
Chris Ballb9c7a172012-02-20 12:34:25 -0500896 " [BKOPS_STATUS: 0x%02x]\n", ext_csd[246]);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100897
898 /* CORRECTLY_PRG_SECTORS_NUM [245:242] TODO */
899
900 printf("1st Initialisation Time after programmed sector"
901 " [INI_TIMEOUT_AP: 0x%02x]\n", ext_csd[241]);
902
903 /* A441: reserved [240] */
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100904 printf("Power class for 52MHz, DDR at 3.6V"
905 " [PWR_CL_DDR_52_360: 0x%02x]\n", ext_csd[239]);
906 printf("Power class for 52MHz, DDR at 1.95V"
907 " [PWR_CL_DDR_52_195: 0x%02x]\n", ext_csd[238]);
908
909 /* A441: reserved [237-236] */
910
911 if (ext_csd_rev >= 6) {
912 printf("Power class for 200MHz at 3.6V"
913 " [PWR_CL_200_360: 0x%02x]\n", ext_csd[237]);
914 printf("Power class for 200MHz, at 1.95V"
915 " [PWR_CL_200_195: 0x%02x]\n", ext_csd[236]);
916 }
Chris Ballb9c7a172012-02-20 12:34:25 -0500917 printf("Minimum Performance for 8bit at 52MHz in DDR mode:\n");
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100918 printf(" [MIN_PERF_DDR_W_8_52: 0x%02x]\n", ext_csd[235]);
919 printf(" [MIN_PERF_DDR_R_8_52: 0x%02x]\n", ext_csd[234]);
920 /* A441: reserved [233] */
921 printf("TRIM Multiplier [TRIM_MULT: 0x%02x]\n", ext_csd[232]);
922 printf("Secure Feature support [SEC_FEATURE_SUPPORT: 0x%02x]\n",
923 ext_csd[231]);
924 }
925 if (ext_csd_rev == 5) { /* Obsolete in 4.5 */
926 printf("Secure Erase Multiplier [SEC_ERASE_MULT: 0x%02x]\n",
927 ext_csd[230]);
928 printf("Secure TRIM Multiplier [SEC_TRIM_MULT: 0x%02x]\n",
929 ext_csd[229]);
930 }
931 reg = ext_csd[EXT_CSD_BOOT_INFO];
932 printf("Boot Information [BOOT_INFO: 0x%02x]\n", reg);
933 if (reg & EXT_CSD_BOOT_INFO_ALT)
934 printf(" Device supports alternative boot method\n");
935 if (reg & EXT_CSD_BOOT_INFO_DDR_DDR)
936 printf(" Device supports dual data rate during boot\n");
937 if (reg & EXT_CSD_BOOT_INFO_HS_MODE)
938 printf(" Device supports high speed timing during boot\n");
939
940 /* A441/A43: reserved [227] */
941 printf("Boot partition size [BOOT_SIZE_MULTI: 0x%02x]\n", ext_csd[226]);
942 printf("Access size [ACC_SIZE: 0x%02x]\n", ext_csd[225]);
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400943
944 reg = get_hc_erase_grp_size(ext_csd);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100945 printf("High-capacity erase unit size [HC_ERASE_GRP_SIZE: 0x%02x]\n",
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400946 reg);
947 printf(" i.e. %u KiB\n", 512 * reg);
948
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100949 printf("High-capacity erase timeout [ERASE_TIMEOUT_MULT: 0x%02x]\n",
950 ext_csd[223]);
951 printf("Reliable write sector count [REL_WR_SEC_C: 0x%02x]\n",
952 ext_csd[222]);
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400953
954 reg = get_hc_wp_grp_size(ext_csd);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100955 printf("High-capacity W protect group size [HC_WP_GRP_SIZE: 0x%02x]\n",
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400956 reg);
957 printf(" i.e. %lu KiB\n", 512l * get_hc_erase_grp_size(ext_csd) * reg);
958
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100959 printf("Sleep current (VCC) [S_C_VCC: 0x%02x]\n", ext_csd[220]);
960 printf("Sleep current (VCCQ) [S_C_VCCQ: 0x%02x]\n", ext_csd[219]);
961 /* A441/A43: reserved [218] */
962 printf("Sleep/awake timeout [S_A_TIMEOUT: 0x%02x]\n", ext_csd[217]);
963 /* A441/A43: reserved [216] */
Ben Gardiner4e850232013-05-30 17:12:49 -0400964
965 unsigned int sectors = get_sector_count(ext_csd);
966 printf("Sector Count [SEC_COUNT: 0x%08x]\n", sectors);
967 if (is_blockaddresed(ext_csd))
968 printf(" Device is block-addressed\n");
969 else
970 printf(" Device is NOT block-addressed\n");
971
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100972 /* A441/A43: reserved [211] */
973 printf("Minimum Write Performance for 8bit:\n");
974 printf(" [MIN_PERF_W_8_52: 0x%02x]\n", ext_csd[210]);
975 printf(" [MIN_PERF_R_8_52: 0x%02x]\n", ext_csd[209]);
976 printf(" [MIN_PERF_W_8_26_4_52: 0x%02x]\n", ext_csd[208]);
977 printf(" [MIN_PERF_R_8_26_4_52: 0x%02x]\n", ext_csd[207]);
978 printf("Minimum Write Performance for 4bit:\n");
979 printf(" [MIN_PERF_W_4_26: 0x%02x]\n", ext_csd[206]);
980 printf(" [MIN_PERF_R_4_26: 0x%02x]\n", ext_csd[205]);
981 /* A441/A43: reserved [204] */
982 printf("Power classes registers:\n");
983 printf(" [PWR_CL_26_360: 0x%02x]\n", ext_csd[203]);
984 printf(" [PWR_CL_52_360: 0x%02x]\n", ext_csd[202]);
985 printf(" [PWR_CL_26_195: 0x%02x]\n", ext_csd[201]);
986 printf(" [PWR_CL_52_195: 0x%02x]\n", ext_csd[200]);
987
988 /* A43: reserved [199:198] */
989 if (ext_csd_rev >= 5) {
990 printf("Partition switching timing "
991 "[PARTITION_SWITCH_TIME: 0x%02x]\n", ext_csd[199]);
992 printf("Out-of-interrupt busy timing"
993 " [OUT_OF_INTERRUPT_TIME: 0x%02x]\n", ext_csd[198]);
994 }
995
996 /* A441/A43: reserved [197] [195] [193] [190] [188]
997 * [186] [184] [182] [180] [176] */
998
999 if (ext_csd_rev >= 6)
1000 printf("I/O Driver Strength [DRIVER_STRENGTH: 0x%02x]\n",
1001 ext_csd[197]);
1002
Oleg Matcovschi64f63a32013-05-23 17:11:07 -07001003 /* DEVICE_TYPE in A45, CARD_TYPE in A441 */
1004 reg = ext_csd[196];
1005 printf("Card Type [CARD_TYPE: 0x%02x]\n", reg);
1006 if (reg & 0x20) printf(" HS200 Single Data Rate eMMC @200MHz 1.2VI/O\n");
1007 if (reg & 0x10) printf(" HS200 Single Data Rate eMMC @200MHz 1.8VI/O\n");
1008 if (reg & 0x08) printf(" HS Dual Data Rate eMMC @52MHz 1.2VI/O\n");
1009 if (reg & 0x04) printf(" HS Dual Data Rate eMMC @52MHz 1.8V or 3VI/O\n");
1010 if (reg & 0x02) printf(" HS eMMC @52MHz - at rated device voltage(s)\n");
1011 if (reg & 0x01) printf(" HS eMMC @26MHz - at rated device voltage(s)\n");
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001012
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001013 printf("CSD structure version [CSD_STRUCTURE: 0x%02x]\n", ext_csd[194]);
1014 /* ext_csd_rev = ext_csd[192] (already done!!!) */
1015 printf("Command set [CMD_SET: 0x%02x]\n", ext_csd[191]);
1016 printf("Command set revision [CMD_SET_REV: 0x%02x]\n", ext_csd[189]);
1017 printf("Power class [POWER_CLASS: 0x%02x]\n", ext_csd[187]);
1018 printf("High-speed interface timing [HS_TIMING: 0x%02x]\n",
1019 ext_csd[185]);
1020 /* bus_width: ext_csd[183] not readable */
1021 printf("Erased memory content [ERASED_MEM_CONT: 0x%02x]\n",
1022 ext_csd[181]);
1023 reg = ext_csd[EXT_CSD_BOOT_CFG];
1024 printf("Boot configuration bytes [PARTITION_CONFIG: 0x%02x]\n", reg);
Mario Schuknecht8c0c40d2013-05-15 08:28:04 +02001025 switch ((reg & EXT_CSD_BOOT_CFG_EN)>>3) {
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001026 case 0x0:
1027 printf(" Not boot enable\n");
1028 break;
1029 case 0x1:
1030 printf(" Boot Partition 1 enabled\n");
1031 break;
1032 case 0x2:
1033 printf(" Boot Partition 2 enabled\n");
1034 break;
1035 case 0x7:
1036 printf(" User Area Enabled for boot\n");
1037 break;
1038 }
1039 switch (reg & EXT_CSD_BOOT_CFG_ACC) {
1040 case 0x0:
1041 printf(" No access to boot partition\n");
1042 break;
1043 case 0x1:
1044 printf(" R/W Boot Partition 1\n");
1045 break;
1046 case 0x2:
1047 printf(" R/W Boot Partition 2\n");
1048 break;
Mario Schuknecht8c0c40d2013-05-15 08:28:04 +02001049 case 0x3:
1050 printf(" R/W Replay Protected Memory Block (RPMB)\n");
1051 break;
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001052 default:
Mario Schuknecht8c0c40d2013-05-15 08:28:04 +02001053 printf(" Access to General Purpose partition %d\n",
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001054 (reg & EXT_CSD_BOOT_CFG_ACC) - 3);
1055 break;
1056 }
1057
1058 printf("Boot config protection [BOOT_CONFIG_PROT: 0x%02x]\n",
1059 ext_csd[178]);
1060 printf("Boot bus Conditions [BOOT_BUS_CONDITIONS: 0x%02x]\n",
1061 ext_csd[177]);
1062 printf("High-density erase group definition"
Ben Gardinerd91d3692013-05-30 17:12:51 -04001063 " [ERASE_GROUP_DEF: 0x%02x]\n", ext_csd[EXT_CSD_ERASE_GROUP_DEF]);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001064
Chris Ballb9c7a172012-02-20 12:34:25 -05001065 print_writeprotect_status(ext_csd);
1066
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001067 if (ext_csd_rev >= 5) {
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001068 /* A441]: reserved [172] */
1069 printf("User area write protection register"
1070 " [USER_WP]: 0x%02x\n", ext_csd[171]);
1071 /* A441]: reserved [170] */
1072 printf("FW configuration [FW_CONFIG]: 0x%02x\n", ext_csd[169]);
1073 printf("RPMB Size [RPMB_SIZE_MULT]: 0x%02x\n", ext_csd[168]);
Ben Gardiner4da1c0d2013-09-19 11:14:28 -04001074
1075 reg = ext_csd[EXT_CSD_WR_REL_SET];
1076 const char * const fast = "existing data is at risk if a power "
1077 "failure occurs during a write operation";
1078 const char * const reliable = "the device protects existing "
1079 "data if a power failure occurs during a write "
1080 "operation";
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001081 printf("Write reliability setting register"
Ben Gardiner4da1c0d2013-09-19 11:14:28 -04001082 " [WR_REL_SET]: 0x%02x\n", reg);
1083
1084 printf(" user area: %s\n", reg & (1<<0) ? reliable : fast);
1085 int i;
1086 for (i = 1; i <= 4; i++) {
1087 printf(" partition %d: %s\n", i,
1088 reg & (1<<i) ? reliable : fast);
1089 }
1090
1091 reg = ext_csd[EXT_CSD_WR_REL_PARAM];
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001092 printf("Write reliability parameter register"
Ben Gardiner4da1c0d2013-09-19 11:14:28 -04001093 " [WR_REL_PARAM]: 0x%02x\n", reg);
1094 if (reg & 0x01)
1095 printf(" Device supports writing EXT_CSD_WR_REL_SET\n");
1096 if (reg & 0x04)
1097 printf(" Device supports the enhanced def. of reliable "
1098 "write\n");
1099
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001100 /* sanitize_start ext_csd[165]]: not readable
1101 * bkops_start ext_csd[164]]: only writable */
1102 printf("Enable background operations handshake"
1103 " [BKOPS_EN]: 0x%02x\n", ext_csd[163]);
1104 printf("H/W reset function"
1105 " [RST_N_FUNCTION]: 0x%02x\n", ext_csd[162]);
1106 printf("HPI management [HPI_MGMT]: 0x%02x\n", ext_csd[161]);
Ben Gardiner82bd9502013-06-27 11:04:10 -04001107 reg = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001108 printf("Partitioning Support [PARTITIONING_SUPPORT]: 0x%02x\n",
1109 reg);
Ben Gardiner82bd9502013-06-27 11:04:10 -04001110 if (reg & EXT_CSD_PARTITIONING_EN)
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001111 printf(" Device support partitioning feature\n");
1112 else
1113 printf(" Device NOT support partitioning feature\n");
Ben Gardiner82bd9502013-06-27 11:04:10 -04001114 if (reg & EXT_CSD_ENH_ATTRIBUTE_EN)
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001115 printf(" Device can have enhanced tech.\n");
1116 else
1117 printf(" Device cannot have enhanced tech.\n");
1118
Oliver Metz11f2cea2013-09-23 08:40:52 +02001119 regl = (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_2] << 16) |
Oliver Metz22f26412013-09-23 08:40:51 +02001120 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_1] << 8) |
1121 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_0];
1122
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001123 printf("Max Enhanced Area Size [MAX_ENH_SIZE_MULT]: 0x%06x\n",
Oliver Metz11f2cea2013-09-23 08:40:52 +02001124 regl);
Ben Gardinerf82e27a2013-05-30 17:12:50 -04001125 unsigned int wp_sz = get_hc_wp_grp_size(ext_csd);
1126 unsigned int erase_sz = get_hc_erase_grp_size(ext_csd);
Oliver Metz11f2cea2013-09-23 08:40:52 +02001127 printf(" i.e. %lu KiB\n", 512l * regl * wp_sz * erase_sz);
Ben Gardinerf82e27a2013-05-30 17:12:50 -04001128
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001129 printf("Partitions attribute [PARTITIONS_ATTRIBUTE]: 0x%02x\n",
Ben Gardinerd91d3692013-05-30 17:12:51 -04001130 ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]);
Ben Gardinera6cd98d2013-05-30 17:12:46 -04001131 reg = ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED];
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001132 printf("Partitioning Setting"
1133 " [PARTITION_SETTING_COMPLETED]: 0x%02x\n",
Ben Gardinera6cd98d2013-05-30 17:12:46 -04001134 reg);
1135 if (reg)
1136 printf(" Device partition setting complete\n");
1137 else
1138 printf(" Device partition setting NOT complete\n");
1139
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001140 printf("General Purpose Partition Size\n"
1141 " [GP_SIZE_MULT_4]: 0x%06x\n", (ext_csd[154] << 16) |
1142 (ext_csd[153] << 8) | ext_csd[152]);
1143 printf(" [GP_SIZE_MULT_3]: 0x%06x\n", (ext_csd[151] << 16) |
1144 (ext_csd[150] << 8) | ext_csd[149]);
1145 printf(" [GP_SIZE_MULT_2]: 0x%06x\n", (ext_csd[148] << 16) |
1146 (ext_csd[147] << 8) | ext_csd[146]);
1147 printf(" [GP_SIZE_MULT_1]: 0x%06x\n", (ext_csd[145] << 16) |
1148 (ext_csd[144] << 8) | ext_csd[143]);
1149
Oliver Metz11f2cea2013-09-23 08:40:52 +02001150 regl = (ext_csd[EXT_CSD_ENH_SIZE_MULT_2] << 16) |
Ben Gardinerf82e27a2013-05-30 17:12:50 -04001151 (ext_csd[EXT_CSD_ENH_SIZE_MULT_1] << 8) |
1152 ext_csd[EXT_CSD_ENH_SIZE_MULT_0];
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001153 printf("Enhanced User Data Area Size"
Oliver Metz11f2cea2013-09-23 08:40:52 +02001154 " [ENH_SIZE_MULT]: 0x%06x\n", regl);
1155 printf(" i.e. %lu KiB\n", 512l * regl *
Ben Gardinerf82e27a2013-05-30 17:12:50 -04001156 get_hc_erase_grp_size(ext_csd) *
1157 get_hc_wp_grp_size(ext_csd));
Ben Gardiner68f490b2013-05-30 17:12:48 -04001158
Oliver Metz11f2cea2013-09-23 08:40:52 +02001159 regl = (ext_csd[EXT_CSD_ENH_START_ADDR_3] << 24) |
Ben Gardiner68f490b2013-05-30 17:12:48 -04001160 (ext_csd[EXT_CSD_ENH_START_ADDR_2] << 16) |
1161 (ext_csd[EXT_CSD_ENH_START_ADDR_1] << 8) |
1162 ext_csd[EXT_CSD_ENH_START_ADDR_0];
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001163 printf("Enhanced User Data Start Address"
Oliver Metz11f2cea2013-09-23 08:40:52 +02001164 " [ENH_START_ADDR]: 0x%06x\n", regl);
Ben Gardiner4e850232013-05-30 17:12:49 -04001165 printf(" i.e. %lu bytes offset\n", (is_blockaddresed(ext_csd) ?
Oliver Metz11f2cea2013-09-23 08:40:52 +02001166 1l : 512l) * regl);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001167
1168 /* A441]: reserved [135] */
1169 printf("Bad Block Management mode"
1170 " [SEC_BAD_BLK_MGMNT]: 0x%02x\n", ext_csd[134]);
1171 /* A441: reserved [133:0] */
1172 }
1173 /* B45 */
1174 if (ext_csd_rev >= 6) {
1175 int j;
1176 /* tcase_support ext_csd[132] not readable */
1177 printf("Periodic Wake-up [PERIODIC_WAKEUP]: 0x%02x\n",
1178 ext_csd[131]);
1179 printf("Program CID/CSD in DDR mode support"
1180 " [PROGRAM_CID_CSD_DDR_SUPPORT]: 0x%02x\n",
1181 ext_csd[130]);
1182
1183 for (j = 127; j >= 64; j--)
1184 printf("Vendor Specific Fields"
1185 " [VENDOR_SPECIFIC_FIELD[%d]]: 0x%02x\n",
1186 j, ext_csd[j]);
1187
1188 printf("Native sector size [NATIVE_SECTOR_SIZE]: 0x%02x\n",
1189 ext_csd[63]);
1190 printf("Sector size emulation [USE_NATIVE_SECTOR]: 0x%02x\n",
1191 ext_csd[62]);
1192 printf("Sector size [DATA_SECTOR_SIZE]: 0x%02x\n", ext_csd[61]);
1193 printf("1st initialization after disabling sector"
1194 " size emulation [INI_TIMEOUT_EMU]: 0x%02x\n",
1195 ext_csd[60]);
1196 printf("Class 6 commands control [CLASS_6_CTRL]: 0x%02x\n",
1197 ext_csd[59]);
1198 printf("Number of addressed group to be Released"
1199 "[DYNCAP_NEEDED]: 0x%02x\n", ext_csd[58]);
1200 printf("Exception events control"
1201 " [EXCEPTION_EVENTS_CTRL]: 0x%04x\n",
1202 (ext_csd[57] << 8) | ext_csd[56]);
1203 printf("Exception events status"
1204 "[EXCEPTION_EVENTS_STATUS]: 0x%04x\n",
1205 (ext_csd[55] << 8) | ext_csd[54]);
1206 printf("Extended Partitions Attribute"
1207 " [EXT_PARTITIONS_ATTRIBUTE]: 0x%04x\n",
1208 (ext_csd[53] << 8) | ext_csd[52]);
1209
1210 for (j = 51; j >= 37; j--)
1211 printf("Context configuration"
1212 " [CONTEXT_CONF[%d]]: 0x%02x\n", j, ext_csd[j]);
1213
1214 printf("Packed command status"
1215 " [PACKED_COMMAND_STATUS]: 0x%02x\n", ext_csd[36]);
1216 printf("Packed command failure index"
1217 " [PACKED_FAILURE_INDEX]: 0x%02x\n", ext_csd[35]);
1218 printf("Power Off Notification"
1219 " [POWER_OFF_NOTIFICATION]: 0x%02x\n", ext_csd[34]);
Oleg Matcovschi64f63a32013-05-23 17:11:07 -07001220 printf("Control to turn the Cache ON/OFF"
1221 " [CACHE_CTRL]: 0x%02x\n", ext_csd[33]);
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +01001222 /* flush_cache ext_csd[32] not readable */
1223 /*Reserved [31:0] */
1224 }
1225
1226out_free:
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -05001227 return ret;
1228}
Yaniv Gardi21bb4732013-05-26 13:25:33 -04001229
1230int do_sanitize(int nargs, char **argv)
1231{
1232 int fd, ret;
1233 char *device;
1234
1235 CHECK(nargs != 2, "Usage: mmc sanitize </path/to/mmcblkX>\n",
1236 exit(1));
1237
1238 device = argv[1];
1239
1240 fd = open(device, O_RDWR);
1241 if (fd < 0) {
1242 perror("open");
1243 exit(1);
1244 }
1245
1246 ret = write_extcsd_value(fd, EXT_CSD_SANITIZE_START, 1);
1247 if (ret) {
1248 fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
1249 1, EXT_CSD_SANITIZE_START, device);
1250 exit(1);
1251 }
1252
1253 return ret;
1254
1255}
1256
Roman Peniaevc6cb0532014-08-12 23:25:45 +09001257#define DO_IO(func, fd, buf, nbyte) \
1258 ({ \
1259 ssize_t ret = 0, r; \
1260 do { \
1261 r = func(fd, buf + ret, nbyte - ret); \
1262 if (r < 0 && errno != EINTR) { \
1263 ret = -1; \
1264 break; \
1265 } \
1266 else if (r > 0) \
1267 ret += r; \
1268 } while (r != 0 && (size_t)ret != nbyte); \
1269 \
1270 ret; \
1271 })
1272
1273enum rpmb_op_type {
1274 MMC_RPMB_WRITE_KEY = 0x01,
1275 MMC_RPMB_READ_CNT = 0x02,
1276 MMC_RPMB_WRITE = 0x03,
1277 MMC_RPMB_READ = 0x04,
1278
1279 /* For internal usage only, do not use it directly */
1280 MMC_RPMB_READ_RESP = 0x05
1281};
1282
1283struct rpmb_frame {
1284 u_int8_t stuff[196];
1285 u_int8_t key_mac[32];
1286 u_int8_t data[256];
1287 u_int8_t nonce[16];
1288 u_int32_t write_counter;
1289 u_int16_t addr;
1290 u_int16_t block_count;
1291 u_int16_t result;
1292 u_int16_t req_resp;
1293};
1294
1295/* Performs RPMB operation.
1296 *
1297 * @fd: RPMB device on which we should perform ioctl command
1298 * @frame_in: input RPMB frame, should be properly inited
1299 * @frame_out: output (result) RPMB frame. Caller is responsible for checking
1300 * result and req_resp for output frame.
1301 * @out_cnt: count of outer frames. Used only for multiple blocks reading,
1302 * in the other cases -EINVAL will be returned.
1303 */
1304static int do_rpmb_op(int fd,
1305 const struct rpmb_frame *frame_in,
1306 struct rpmb_frame *frame_out,
1307 unsigned int out_cnt)
1308{
1309 int err;
1310 u_int16_t rpmb_type;
1311
1312 struct mmc_ioc_cmd ioc = {
1313 .arg = 0x0,
1314 .blksz = 512,
1315 .blocks = 1,
1316 .write_flag = 1,
1317 .opcode = MMC_WRITE_MULTIPLE_BLOCK,
1318 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC,
1319 .data_ptr = (uintptr_t)frame_in
1320 };
1321
1322 if (!frame_in || !frame_out || !out_cnt)
1323 return -EINVAL;
1324
1325 rpmb_type = be16toh(frame_in->req_resp);
1326
1327 switch(rpmb_type) {
1328 case MMC_RPMB_WRITE:
1329 case MMC_RPMB_WRITE_KEY:
1330 if (out_cnt != 1) {
1331 err = -EINVAL;
1332 goto out;
1333 }
1334
1335 /* Write request */
1336 ioc.write_flag |= (1<<31);
1337 err = ioctl(fd, MMC_IOC_CMD, &ioc);
1338 if (err < 0) {
1339 err = -errno;
1340 goto out;
1341 }
1342
1343 /* Result request */
1344 memset(frame_out, 0, sizeof(*frame_out));
1345 frame_out->req_resp = htobe16(MMC_RPMB_READ_RESP);
1346 ioc.write_flag = 1;
1347 ioc.data_ptr = (uintptr_t)frame_out;
1348 err = ioctl(fd, MMC_IOC_CMD, &ioc);
1349 if (err < 0) {
1350 err = -errno;
1351 goto out;
1352 }
1353
1354 /* Get response */
1355 ioc.write_flag = 0;
1356 ioc.opcode = MMC_READ_MULTIPLE_BLOCK;
1357 err = ioctl(fd, MMC_IOC_CMD, &ioc);
1358 if (err < 0) {
1359 err = -errno;
1360 goto out;
1361 }
1362
1363 break;
1364 case MMC_RPMB_READ_CNT:
1365 if (out_cnt != 1) {
1366 err = -EINVAL;
1367 goto out;
1368 }
1369 /* fall through */
1370
1371 case MMC_RPMB_READ:
1372 /* Request */
1373 err = ioctl(fd, MMC_IOC_CMD, &ioc);
1374 if (err < 0) {
1375 err = -errno;
1376 goto out;
1377 }
1378
1379 /* Get response */
1380 ioc.write_flag = 0;
1381 ioc.opcode = MMC_READ_MULTIPLE_BLOCK;
1382 ioc.blocks = out_cnt;
1383 ioc.data_ptr = (uintptr_t)frame_out;
1384 err = ioctl(fd, MMC_IOC_CMD, &ioc);
1385 if (err < 0) {
1386 err = -errno;
1387 goto out;
1388 }
1389
1390 break;
1391 default:
1392 err = -EINVAL;
1393 goto out;
1394 }
1395
1396out:
1397 return err;
1398}
1399
1400int do_rpmb_write_key(int nargs, char **argv)
1401{
1402 int ret, dev_fd, key_fd;
1403 struct rpmb_frame frame_in = {
1404 .req_resp = htobe16(MMC_RPMB_WRITE_KEY)
1405 }, frame_out;
1406
1407 CHECK(nargs != 3, "Usage: mmc rpmb write-key </path/to/mmcblkXrpmb> </path/to/key>\n",
1408 exit(1));
1409
1410 dev_fd = open(argv[1], O_RDWR);
1411 if (dev_fd < 0) {
1412 perror("device open");
1413 exit(1);
1414 }
1415
1416 if (0 == strcmp(argv[2], "-"))
1417 key_fd = STDIN_FILENO;
1418 else {
1419 key_fd = open(argv[2], O_RDONLY);
1420 if (key_fd < 0) {
1421 perror("can't open key file");
1422 exit(1);
1423 }
1424 }
1425
1426 /* Read the auth key */
1427 ret = DO_IO(read, key_fd, frame_in.key_mac, sizeof(frame_in.key_mac));
1428 if (ret < 0) {
1429 perror("read the key");
1430 exit(1);
1431 } else if (ret != sizeof(frame_in.key_mac)) {
1432 printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
1433 (unsigned long)sizeof(frame_in.key_mac),
1434 ret);
1435 exit(1);
1436 }
1437
1438 /* Execute RPMB op */
1439 ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
1440 if (ret != 0) {
1441 perror("RPMB ioctl failed");
1442 exit(1);
1443 }
1444
1445 /* Check RPMB response */
1446 if (frame_out.result != 0) {
1447 printf("RPMB operation failed, retcode 0x%04x\n",
1448 be16toh(frame_out.result));
1449 exit(1);
1450 }
1451
1452 close(dev_fd);
1453 if (key_fd != STDIN_FILENO)
1454 close(key_fd);
1455
1456 return ret;
1457}
1458
1459int rpmb_read_counter(int dev_fd, unsigned int *cnt)
1460{
1461 int ret;
1462 struct rpmb_frame frame_in = {
1463 .req_resp = htobe16(MMC_RPMB_READ_CNT)
1464 }, frame_out;
1465
1466 /* Execute RPMB op */
1467 ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
1468 if (ret != 0) {
1469 perror("RPMB ioctl failed");
1470 exit(1);
1471 }
1472
1473 /* Check RPMB response */
1474 if (frame_out.result != 0)
1475 return be16toh(frame_out.result);
1476
1477 *cnt = be32toh(frame_out.write_counter);
1478
1479 return 0;
1480}
1481
1482int do_rpmb_read_counter(int nargs, char **argv)
1483{
1484 int ret, dev_fd;
1485 unsigned int cnt;
1486
1487 CHECK(nargs != 2, "Usage: mmc rpmb read-counter </path/to/mmcblkXrpmb>\n",
1488 exit(1));
1489
1490 dev_fd = open(argv[1], O_RDWR);
1491 if (dev_fd < 0) {
1492 perror("device open");
1493 exit(1);
1494 }
1495
1496 ret = rpmb_read_counter(dev_fd, &cnt);
1497
1498 /* Check RPMB response */
1499 if (ret != 0) {
1500 printf("RPMB operation failed, retcode 0x%04x\n", ret);
1501 exit(1);
1502 }
1503
1504 close(dev_fd);
1505
1506 printf("Counter value: 0x%08x\n", cnt);
1507
1508 return ret;
1509}
1510
1511int do_rpmb_read_block(int nargs, char **argv)
1512{
1513 int i, ret, dev_fd, data_fd, key_fd = -1;
1514 uint16_t addr, blocks_cnt;
1515 unsigned char key[32];
1516 struct rpmb_frame frame_in = {
1517 .req_resp = htobe16(MMC_RPMB_READ),
1518 }, *frame_out_p;
1519
1520 CHECK(nargs != 5 && nargs != 6, "Usage: mmc rpmb read-block </path/to/mmcblkXrpmb> <address> <blocks count> </path/to/output_file> [/path/to/key]\n",
1521 exit(1));
1522
1523 dev_fd = open(argv[1], O_RDWR);
1524 if (dev_fd < 0) {
1525 perror("device open");
1526 exit(1);
1527 }
1528
1529 /* Get block address */
1530 errno = 0;
1531 addr = strtol(argv[2], NULL, 0);
1532 if (errno) {
1533 perror("incorrect address");
1534 exit(1);
1535 }
1536 frame_in.addr = htobe16(addr);
1537
1538 /* Get blocks count */
1539 errno = 0;
1540 blocks_cnt = strtol(argv[3], NULL, 0);
1541 if (errno) {
1542 perror("incorrect blocks count");
1543 exit(1);
1544 }
1545
1546 if (!blocks_cnt) {
1547 printf("please, specify valid blocks count number\n");
1548 exit(1);
1549 }
1550
1551 frame_out_p = calloc(sizeof(*frame_out_p), blocks_cnt);
1552 if (!frame_out_p) {
1553 printf("can't allocate memory for RPMB outer frames\n");
1554 exit(1);
1555 }
1556
1557 /* Write 256b data */
1558 if (0 == strcmp(argv[4], "-"))
1559 data_fd = STDOUT_FILENO;
1560 else {
1561 data_fd = open(argv[4], O_WRONLY | O_CREAT | O_APPEND,
1562 S_IRUSR | S_IWUSR);
1563 if (data_fd < 0) {
1564 perror("can't open output file");
1565 exit(1);
1566 }
1567 }
1568
1569 /* Key is specified */
1570 if (nargs == 6) {
1571 if (0 == strcmp(argv[5], "-"))
1572 key_fd = STDIN_FILENO;
1573 else {
1574 key_fd = open(argv[5], O_RDONLY);
1575 if (key_fd < 0) {
1576 perror("can't open input key file");
1577 exit(1);
1578 }
1579 }
1580
1581 ret = DO_IO(read, key_fd, key, sizeof(key));
1582 if (ret < 0) {
1583 perror("read the key data");
1584 exit(1);
1585 } else if (ret != sizeof(key)) {
1586 printf("Data must be %lu bytes length, but we read only %d, exit\n",
1587 (unsigned long)sizeof(key),
1588 ret);
1589 exit(1);
1590 }
1591 }
1592
1593 /* Execute RPMB op */
1594 ret = do_rpmb_op(dev_fd, &frame_in, frame_out_p, blocks_cnt);
1595 if (ret != 0) {
1596 perror("RPMB ioctl failed");
1597 exit(1);
1598 }
1599
1600 /* Check RPMB response */
1601 if (frame_out_p[blocks_cnt - 1].result != 0) {
1602 printf("RPMB operation failed, retcode 0x%04x\n",
1603 be16toh(frame_out_p[blocks_cnt - 1].result));
1604 exit(1);
1605 }
1606
1607 /* Do we have to verify data against key? */
1608 if (nargs == 6) {
1609 unsigned char mac[32];
1610 hmac_sha256_ctx ctx;
1611 struct rpmb_frame *frame_out = NULL;
1612
1613 hmac_sha256_init(&ctx, key, sizeof(key));
1614 for (i = 0; i < blocks_cnt; i++) {
1615 frame_out = &frame_out_p[i];
1616 hmac_sha256_update(&ctx, frame_out->data,
1617 sizeof(*frame_out) -
1618 offsetof(struct rpmb_frame, data));
1619 }
1620
1621 hmac_sha256_final(&ctx, mac, sizeof(mac));
1622
1623 /* Impossible */
1624 assert(frame_out);
1625
1626 /* Compare calculated MAC and MAC from last frame */
1627 if (memcmp(mac, frame_out->key_mac, sizeof(mac))) {
1628 printf("RPMB MAC missmatch\n");
1629 exit(1);
1630 }
1631 }
1632
1633 /* Write data */
1634 for (i = 0; i < blocks_cnt; i++) {
1635 struct rpmb_frame *frame_out = &frame_out_p[i];
1636 ret = DO_IO(write, data_fd, frame_out->data, sizeof(frame_out->data));
1637 if (ret < 0) {
1638 perror("write the data");
1639 exit(1);
1640 } else if (ret != sizeof(frame_out->data)) {
1641 printf("Data must be %lu bytes length, but we wrote only %d, exit\n",
1642 (unsigned long)sizeof(frame_out->data),
1643 ret);
1644 exit(1);
1645 }
1646 }
1647
1648 free(frame_out_p);
1649 close(dev_fd);
1650 if (data_fd != STDOUT_FILENO)
1651 close(data_fd);
1652 if (key_fd != -1 && key_fd != STDIN_FILENO)
1653 close(key_fd);
1654
1655 return ret;
1656}
1657
1658int do_rpmb_write_block(int nargs, char **argv)
1659{
1660 int ret, dev_fd, key_fd, data_fd;
1661 unsigned char key[32];
1662 uint16_t addr;
1663 unsigned int cnt;
1664 struct rpmb_frame frame_in = {
1665 .req_resp = htobe16(MMC_RPMB_WRITE),
1666 .block_count = htobe16(1)
1667 }, frame_out;
1668
1669 CHECK(nargs != 5, "Usage: mmc rpmb write-block </path/to/mmcblkXrpmb> <address> </path/to/input_file> </path/to/key>\n",
1670 exit(1));
1671
1672 dev_fd = open(argv[1], O_RDWR);
1673 if (dev_fd < 0) {
1674 perror("device open");
1675 exit(1);
1676 }
1677
1678 ret = rpmb_read_counter(dev_fd, &cnt);
1679 /* Check RPMB response */
1680 if (ret != 0) {
1681 printf("RPMB read counter operation failed, retcode 0x%04x\n", ret);
1682 exit(1);
1683 }
1684 frame_in.write_counter = htobe32(cnt);
1685
1686 /* Get block address */
1687 errno = 0;
1688 addr = strtol(argv[2], NULL, 0);
1689 if (errno) {
1690 perror("incorrect address");
1691 exit(1);
1692 }
1693 frame_in.addr = htobe16(addr);
1694
1695 /* Read 256b data */
1696 if (0 == strcmp(argv[3], "-"))
1697 data_fd = STDIN_FILENO;
1698 else {
1699 data_fd = open(argv[3], O_RDONLY);
1700 if (data_fd < 0) {
1701 perror("can't open input file");
1702 exit(1);
1703 }
1704 }
1705
1706 ret = DO_IO(read, data_fd, frame_in.data, sizeof(frame_in.data));
1707 if (ret < 0) {
1708 perror("read the data");
1709 exit(1);
1710 } else if (ret != sizeof(frame_in.data)) {
1711 printf("Data must be %lu bytes length, but we read only %d, exit\n",
1712 (unsigned long)sizeof(frame_in.data),
1713 ret);
1714 exit(1);
1715 }
1716
1717 /* Read the auth key */
1718 if (0 == strcmp(argv[4], "-"))
1719 key_fd = STDIN_FILENO;
1720 else {
1721 key_fd = open(argv[4], O_RDONLY);
1722 if (key_fd < 0) {
1723 perror("can't open key file");
1724 exit(1);
1725 }
1726 }
1727
1728 ret = DO_IO(read, key_fd, key, sizeof(key));
1729 if (ret < 0) {
1730 perror("read the key");
1731 exit(1);
1732 } else if (ret != sizeof(key)) {
1733 printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
1734 (unsigned long)sizeof(key),
1735 ret);
1736 exit(1);
1737 }
1738
1739 /* Calculate HMAC SHA256 */
1740 hmac_sha256(
1741 key, sizeof(key),
1742 frame_in.data, sizeof(frame_in) - offsetof(struct rpmb_frame, data),
1743 frame_in.key_mac, sizeof(frame_in.key_mac));
1744
1745 /* Execute RPMB op */
1746 ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
1747 if (ret != 0) {
1748 perror("RPMB ioctl failed");
1749 exit(1);
1750 }
1751
1752 /* Check RPMB response */
1753 if (frame_out.result != 0) {
1754 printf("RPMB operation failed, retcode 0x%04x\n",
1755 be16toh(frame_out.result));
1756 exit(1);
1757 }
1758
1759 close(dev_fd);
1760 if (data_fd != STDIN_FILENO)
1761 close(data_fd);
1762 if (key_fd != STDIN_FILENO)
1763 close(key_fd);
1764
1765 return ret;
1766}