1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2019, Joyent, Inc. 14 * Copyright 2024 Oxide Computer Company 15 */ 16 17 /* 18 * ATR parsing routines shared between userland (ccidadm) and the kernel (CCID 19 * driver) 20 */ 21 22 #include "atr.h" 23 #include <sys/debug.h> 24 #include <sys/sysmacros.h> 25 #include <sys/hexdump.h> 26 27 #ifdef _KERNEL 28 #include <sys/inttypes.h> 29 #include <sys/sunddi.h> 30 #include <sys/kmem.h> 31 #else 32 #include <inttypes.h> 33 #include <strings.h> 34 #include <stdlib.h> 35 #include <stdio.h> 36 #include <ctype.h> 37 #endif 38 39 /* 40 * The ATR must have at least 2 bytes and then may have up to 33 bytes. The 41 * first byte is always TS and the second required byte is T0. 42 */ 43 #define ATR_TS_IDX 0 44 #define ATR_T0_IDX 1 45 46 /* 47 * There are two valid values for TS. It must either be 0x3F or 0x3B. This is 48 * required per ISO/IEC 7816-3:2006 section 8.1. 49 */ 50 #define ATR_TS_INVERSE 0x3F 51 #define ATR_TS_DIRECT 0x3B 52 53 /* 54 * After TS, each word is used to indicate a combination of protocol and the 55 * number of bits defined for that protocol. The lower nibble is treated as the 56 * protocol. The upper nibble is treated to indicate which of four defined words 57 * are present. These are usually referred to as TA, TB, TC, and TD. TD is 58 * always used to indicate the next protocol and the number of bytes present for 59 * that. T0 works in a similar way, except that it defines the number of 60 * historical bytes present in its protocol section and then it refers to a set 61 * of pre-defined global bytes that may be present. 62 */ 63 #define ATR_TD_PROT(x) ((x) & 0x0f) 64 #define ATR_TD_NBITS(x) (((x) & 0xf0) >> 4) 65 #define ATR_TA_MASK 0x1 66 #define ATR_TB_MASK 0x2 67 #define ATR_TC_MASK 0x4 68 #define ATR_TD_MASK 0x8 69 70 #define ATR_TA1_FTABLE(x) (((x) & 0xf0) >> 4) 71 #define ATR_TA1_DITABLE(x) ((x) & 0x0f) 72 73 #define ATR_TA2_CANCHANGE(x) (((x) & 0x80) == 0) 74 #define ATR_TA2_HONORTA1(x) (((x) & 0x10) == 0) 75 #define ATR_TA2_PROTOCOL(x) ((x) & 0x0f) 76 77 /* 78 * When the checksum is required in the ATR, each byte must XOR to zero. 79 */ 80 #define ATR_CKSUM_TARGET 0 81 82 /* 83 * Maximum number of historic ATR bytes. This is limited by the fact that it's a 84 * 4-bit nibble. 85 */ 86 #define ATR_HISTORICAL_MAX 15 87 88 /* 89 * The maximum number of TA, TB, TC, and TD levels that can be encountered in a 90 * given structure. In the best case, there are 30 bytes available (TS, T0, and 91 * TCK use the others). Given that each one of these needs 4 bytes to be 92 * represented, the maximum number of layers that can fit is seven. 93 */ 94 #define ATR_TI_MAX 7 95 96 /* 97 * Defined protocol values. See ISO/IEC 7816-3:2006 8.2.3 for this list. 98 * Reserved values are noted but not defined. 99 */ 100 #define ATR_PROTOCOL_T0 0 101 #define ATR_PROTOCOL_T1 1 102 103 #define ATR_T1_TB0_CWI(x) ((x) & 0x0f) 104 #define ATR_T1_TB0_BWI(x) (((x) & 0xf0) >> 4) 105 #define ATR_T1_TC0_CRC(x) (((x) & 0x01) != 0) 106 107 /* 108 * T=2 and T=3 are reserved for future full-duplex operation. 109 * T=4 is reserved for enhanced half-duplex character transmission. 110 * T=5-13 are reserved for future use by ISO/IEC JTC 1/SC 17. 111 * T=14 is for protocols not standardized by ISO/IEC JTC 1/SC 17. 112 */ 113 #define ATR_PROTOCOL_T15 15 114 115 #define ATR_T15_TA0_CLOCK(x) (((x) & 0xc0) >> 6) 116 #define ATR_T15_TA0_VOLTAGE(x) ((x) & 0x3f) 117 118 #define ATR_T15_TB0_SPU_STANDARD(x) (((x & 0x80)) != 0) 119 120 /* 121 * Various definitions for the configuration of historical data. This comes from 122 * ISO/IEC 7816-4:2013 Section 12.1.1. 123 */ 124 125 /* 126 * The first historical byte is used to indicate the encoding of the data. Only 127 * values 0x00, 0x80-0x8f are defined. All others are proprietary. 0x81-0x8f are 128 * reserved for future use. 129 */ 130 #define ATR_HIST_CAT_MAND_STATUS 0x00 131 #define ATR_HIST_CAT_TLV_STATUS 0x80 132 #define ATR_HIST_CAT_RFU_MIN 0x81 133 #define ATR_HIST_CAT_RFU_MAX 0x8f 134 135 /* 136 * From ISO/IEC 7816-3:2006 Section 8.3. 137 * 138 * The default value for Fi is 372 which is table entry 1. The default value for 139 * Di is 1, which is table entry 1. 140 */ 141 #define ATR_FI_DEFAULT_INDEX 1 142 #define ATR_DI_DEFAULT_INDEX 1 143 #define ATR_EXTRA_GUARDTIME_DEFAULT 0 144 145 /* 146 * From ISO/IEC 7816-3:2006 Section 10.2. 147 */ 148 #define ATR_T0_WI_DEFAULT 10 149 150 /* 151 * From ISO/IEC 7816-3:2006 Section 11.4.3. 152 */ 153 #define ATR_T1_CWI_DEFAULT 13 154 155 /* 156 * From ISO/IEC 7816-3:2006 Section 11.4.3. 157 */ 158 #define ATR_T1_BWI_DEFAULT 4 159 160 /* 161 * From ISO/IEC 7816-3:2006 Section 11.4.2. 162 */ 163 #define ATR_T1_IFSC_DEFAULT 32 164 165 /* 166 * From ISO/IEC 7816-3:2006 Section 11.4.4 167 */ 168 #define ATR_T1_CHECKSUM_DEFAULT ATR_T1_CHECKSUM_LRC 169 170 /* 171 * Definitions for PPS construction. These are derived from ISO/IEC 7816-3:2006 172 * section 9, Protocol and parameters selection. 173 */ 174 #define PPS_LEN_MIN 3 /* PPSS, PPS0, PCK */ 175 #define PPS_LEN_MAX PPS_BUFFER_MAX 176 #define PPS_PPSS_INDEX 0 177 #define PPS_PPSS_VAL 0xff 178 #define PPS_PPS0_INDEX 0x01 179 #define PPS_PPS0_PROT(x) ((x) & 0x0f) 180 #define PPS_PPS0_PPS1 (1 << 4) 181 #define PPS_PPS0_PPS2 (1 << 5) 182 #define PPS_PPS0_PPS3 (1 << 6) 183 #define PPS_PPS1_SETVAL(f, d) ((((f) & 0x0f) << 4) | ((d) & 0x0f)) 184 185 /* 186 * This enum and subsequent structure is used to represent a single level of 187 * 'T'. This includes the possibility for all three values to be set and records 188 * the protocol. 189 */ 190 typedef enum atr_ti_flags { 191 ATR_TI_HAVE_TA = 1 << 0, 192 ATR_TI_HAVE_TB = 1 << 1, 193 ATR_TI_HAVE_TC = 1 << 2, 194 ATR_TI_HAVE_TD = 1 << 3 195 } atr_ti_flags_t; 196 197 typedef struct atr_ti { 198 uint8_t atrti_protocol; 199 uint8_t atrti_ti_val; 200 uint8_t atrti_td_idx; 201 atr_ti_flags_t atrti_flags; 202 uint8_t atrti_ta; 203 uint8_t atrti_tb; 204 uint8_t atrti_tc; 205 uint8_t atrti_td; 206 } atr_ti_t; 207 208 typedef enum atr_flags { 209 ATR_F_USES_DIRECT = 1 << 0, 210 ATR_F_USES_INVERSE = 1 << 1, 211 ATR_F_HAS_CHECKSUM = 1 << 2, 212 ATR_F_VALID = 1 << 3 213 } atr_flags_t; 214 215 216 struct atr_data { 217 atr_flags_t atr_flags; 218 uint8_t atr_nti; 219 atr_ti_t atr_ti[ATR_TI_MAX]; 220 uint8_t atr_nhistoric; 221 uint8_t atr_historic[ATR_HISTORICAL_MAX]; 222 uint8_t atr_cksum; 223 uint8_t atr_raw[ATR_LEN_MAX]; 224 uint8_t atr_nraw; 225 }; 226 227 /* 228 * These tables maps the bit values for Fi from 7816-3:2006 section 8.3 Table 7. 229 */ 230 static uint_t atr_fi_valtable[16] = { 231 372, /* 0000 */ 232 372, /* 0001 */ 233 558, /* 0010 */ 234 744, /* 0011 */ 235 1116, /* 0100 */ 236 1488, /* 0101 */ 237 1860, /* 0110 */ 238 0, /* 0111 */ 239 0, /* 1000 */ 240 512, /* 1001 */ 241 768, /* 1010 */ 242 1024, /* 1011 */ 243 1536, /* 1100 */ 244 2048, /* 1101 */ 245 0, /* 1110 */ 246 0 /* 1111 */ 247 }; 248 249 static const char *atr_fi_table[16] = { 250 "372", /* 0000 */ 251 "372", /* 0001 */ 252 "558", /* 0010 */ 253 "744", /* 0011 */ 254 "1116", /* 0100 */ 255 "1488", /* 0101 */ 256 "1860", /* 0110 */ 257 "RFU", /* 0111 */ 258 "RFU", /* 1000 */ 259 "512", /* 1001 */ 260 "768", /* 1010 */ 261 "1024", /* 1011 */ 262 "1536", /* 1100 */ 263 "2048", /* 1101 */ 264 "RFU", /* 1110 */ 265 "RFU", /* 1111 */ 266 }; 267 268 /* 269 * This table maps the bit values for f(max) from 7816-3:2006 section 8.3 270 * Table 7. 271 */ 272 static const char *atr_fmax_table[16] = { 273 "4", /* 0000 */ 274 "5", /* 0001 */ 275 "6", /* 0010 */ 276 "8", /* 0011 */ 277 "12", /* 0100 */ 278 "16", /* 0101 */ 279 "20", /* 0110 */ 280 "-", /* 0111 */ 281 "-", /* 1000 */ 282 "5", /* 1001 */ 283 "7.5", /* 1010 */ 284 "10", /* 1011 */ 285 "15", /* 1100 */ 286 "20", /* 1101 */ 287 "-", /* 1110 */ 288 "-", /* 1111 */ 289 }; 290 291 /* 292 * This table maps the bit values for Di from 7816-3:2006 section 8.3 Table 8. 293 */ 294 static uint_t atr_di_valtable[16] = { 295 0, /* 0000 */ 296 1, /* 0001 */ 297 2, /* 0010 */ 298 4, /* 0011 */ 299 8, /* 0100 */ 300 16, /* 0101 */ 301 32, /* 0110 */ 302 64, /* 0111 */ 303 12, /* 1000 */ 304 20, /* 1001 */ 305 0, /* 1010 */ 306 0, /* 1011 */ 307 0, /* 1100 */ 308 0, /* 1101 */ 309 0, /* 1110 */ 310 0 /* 1111 */ 311 }; 312 313 static const char *atr_di_table[16] = { 314 "RFU", /* 0000 */ 315 "1", /* 0001 */ 316 "2", /* 0010 */ 317 "4", /* 0011 */ 318 "8", /* 0100 */ 319 "16", /* 0101 */ 320 "32", /* 0110 */ 321 "64", /* 0111 */ 322 "12", /* 1000 */ 323 "20", /* 1001 */ 324 "RFU", /* 1010 */ 325 "RFU", /* 1011 */ 326 "RFU", /* 1100 */ 327 "RFU", /* 1101 */ 328 "RFU", /* 1110 */ 329 "RFU", /* 1111 */ 330 }; 331 332 /* 333 * This table maps the bit values for the clock stop indicator from 7816-3:2006 334 * section 8.3 Table 9. 335 */ 336 static const char *atr_clock_table[4] = { 337 "disallowed", /* 00 */ 338 "signal low", /* 01 */ 339 "signal high", /* 10 */ 340 "signal low or high" /* 11 */ 341 }; 342 343 uint_t 344 atr_fi_index_to_value(uint8_t val) 345 { 346 if (val >= ARRAY_SIZE(atr_fi_valtable)) { 347 return (0); 348 } 349 350 return (atr_fi_valtable[val]); 351 } 352 353 const char * 354 atr_fi_index_to_string(uint8_t val) 355 { 356 if (val >= ARRAY_SIZE(atr_fi_table)) { 357 return ("<invalid>"); 358 } 359 360 return (atr_fi_table[val]); 361 } 362 363 const char * 364 atr_fmax_index_to_string(uint8_t val) 365 { 366 if (val >= ARRAY_SIZE(atr_fmax_table)) { 367 return ("<invalid>"); 368 } 369 370 return (atr_fmax_table[val]); 371 } 372 373 uint_t 374 atr_di_index_to_value(uint8_t val) 375 { 376 if (val >= ARRAY_SIZE(atr_di_valtable)) { 377 return (0); 378 } 379 380 return (atr_di_valtable[val]); 381 } 382 const char * 383 atr_di_index_to_string(uint8_t val) 384 { 385 if (val >= ARRAY_SIZE(atr_di_table)) { 386 return ("<invalid>"); 387 } 388 389 return (atr_di_table[val]); 390 } 391 392 const char * 393 atr_clock_stop_to_string(atr_clock_stop_t val) 394 { 395 if (val >= ARRAY_SIZE(atr_clock_table)) { 396 return ("<invalid>"); 397 } 398 399 return (atr_clock_table[val]); 400 } 401 402 const char * 403 atr_protocol_to_string(atr_protocol_t prot) 404 { 405 if (prot == ATR_P_NONE) { 406 return ("none"); 407 } 408 409 if ((prot & ATR_P_T0) == ATR_P_T0) { 410 return ("T=0"); 411 } else if ((prot & ATR_P_T1) == ATR_P_T1) { 412 return ("T=1"); 413 } else { 414 return ("T=0, T=1"); 415 } 416 } 417 418 const char * 419 atr_convention_to_string(atr_convention_t conv) 420 { 421 if (conv == ATR_CONVENTION_DIRECT) { 422 return ("direct"); 423 } else if (conv == ATR_CONVENTION_INVERSE) { 424 return ("inverse"); 425 } else { 426 return ("<invalid convention>"); 427 } 428 } 429 430 const char * 431 atr_strerror(atr_parsecode_t code) 432 { 433 switch (code) { 434 case ATR_CODE_OK: 435 return ("ATR parsed successfully"); 436 case ATR_CODE_TOO_SHORT: 437 return ("Specified buffer too short"); 438 case ATR_CODE_TOO_LONG: 439 return ("Specified buffer too long"); 440 case ATR_CODE_INVALID_TS: 441 return ("ATR has invalid TS byte value"); 442 case ATR_CODE_OVERRUN: 443 return ("ATR data requires more bytes than provided"); 444 case ATR_CODE_UNDERRUN: 445 return ("ATR data did not use all provided bytes"); 446 case ATR_CODE_CHECKSUM_ERROR: 447 return ("ATR data did not checksum correctly"); 448 case ATR_CODE_INVALID_TD1: 449 return ("ATR data has invalid protocol in TD1"); 450 default: 451 return ("Unknown Parse Code"); 452 } 453 } 454 455 static uint_t 456 atr_count_cbits(uint8_t x) 457 { 458 uint_t ret = 0; 459 460 if (x & ATR_TA_MASK) 461 ret++; 462 if (x & ATR_TB_MASK) 463 ret++; 464 if (x & ATR_TC_MASK) 465 ret++; 466 if (x & ATR_TD_MASK) 467 ret++; 468 return (ret); 469 } 470 471 /* 472 * Parse out ATR values. Focus on only parsing it and not interpreting it. 473 * Interpretation should be done in other functions that can walk over the data 474 * and be more protocol-aware. 475 */ 476 atr_parsecode_t 477 atr_parse(const uint8_t *buf, size_t len, atr_data_t *data) 478 { 479 uint_t nhist, cbits, ncbits, idx, Ti, prot; 480 uint_t ncksum = 0; 481 atr_ti_t *atp; 482 483 /* 484 * Zero out data in case someone's come back around for another loop on 485 * the same data. 486 */ 487 bzero(data, sizeof (atr_data_t)); 488 489 if (len < ATR_LEN_MIN) { 490 return (ATR_CODE_TOO_SHORT); 491 } 492 493 if (len > ATR_LEN_MAX) { 494 return (ATR_CODE_TOO_LONG); 495 } 496 497 if (buf[ATR_TS_IDX] != ATR_TS_INVERSE && 498 buf[ATR_TS_IDX] != ATR_TS_DIRECT) { 499 return (ATR_CODE_INVALID_TS); 500 } 501 502 bcopy(buf, data->atr_raw, len); 503 data->atr_nraw = len; 504 505 if (buf[ATR_TS_IDX] == ATR_TS_DIRECT) { 506 data->atr_flags |= ATR_F_USES_DIRECT; 507 } else { 508 data->atr_flags |= ATR_F_USES_INVERSE; 509 } 510 511 /* 512 * The protocol of T0 is the number of historical bits present. 513 */ 514 nhist = ATR_TD_PROT(buf[ATR_T0_IDX]); 515 cbits = ATR_TD_NBITS(buf[ATR_T0_IDX]); 516 idx = ATR_T0_IDX + 1; 517 ncbits = atr_count_cbits(cbits); 518 519 /* 520 * Ti is used to track the current iteration of T[A,B,C,D] that we are 521 * on, as the ISO/IEC standard suggests. The way that values are 522 * interpreted depends on the value of Ti. 523 * 524 * When Ti is one, TA, TB, and TC represent global properties. TD's 525 * protocol represents the preferred protocol. 526 * 527 * When Ti is two, TA, TB, and TC also represent global properties. 528 * However, TC only has meaning if the protocol is T=0. 529 * 530 * When Ti is 15, it indicates more global properties. 531 * 532 * For all other values of Ti, the meaning depends on the protocol in 533 * question and they are all properties specific to that protocol. 534 */ 535 Ti = 1; 536 /* 537 * Initialize prot to an invalid protocol to help us deal with the 538 * normal workflow and make sure that we don't mistakenly do anything. 539 */ 540 prot = UINT32_MAX; 541 for (;;) { 542 atp = &data->atr_ti[data->atr_nti]; 543 data->atr_nti++; 544 ASSERT3U(data->atr_nti, <=, ATR_TI_MAX); 545 546 /* 547 * Make sure that we have enough space to read all the cbits. 548 * idx points to the first cbit, which could also potentially be 549 * over the length of the buffer. This is why we subtract one 550 * from idx when doing the calculation. 551 */ 552 if (idx - 1 + ncbits >= len) { 553 return (ATR_CODE_OVERRUN); 554 } 555 556 ASSERT3U(Ti, !=, 0); 557 558 /* 559 * At the moment we opt to ignore reserved protocols. 560 */ 561 atp->atrti_protocol = prot; 562 atp->atrti_ti_val = Ti; 563 atp->atrti_td_idx = idx - 1; 564 565 if (cbits & ATR_TA_MASK) { 566 atp->atrti_flags |= ATR_TI_HAVE_TA; 567 atp->atrti_ta = buf[idx]; 568 idx++; 569 } 570 571 if (cbits & ATR_TB_MASK) { 572 atp->atrti_flags |= ATR_TI_HAVE_TB; 573 atp->atrti_tb = buf[idx]; 574 idx++; 575 } 576 577 if (cbits & ATR_TC_MASK) { 578 atp->atrti_flags |= ATR_TI_HAVE_TC; 579 atp->atrti_tc = buf[idx]; 580 idx++; 581 } 582 583 if (cbits & ATR_TD_MASK) { 584 atp->atrti_flags |= ATR_TI_HAVE_TD; 585 atp->atrti_td = buf[idx]; 586 cbits = ATR_TD_NBITS(buf[idx]); 587 prot = ATR_TD_PROT(buf[idx]); 588 ncbits = atr_count_cbits(cbits); 589 if (prot != 0) 590 ncksum = 1; 591 592 /* 593 * T=15 is not allowed in TD1 per 8.2.3. 594 */ 595 if (Ti == 1 && prot == 0xf) 596 return (ATR_CODE_INVALID_TD1); 597 598 idx++; 599 /* 600 * Encountering TD means that once we take the next loop 601 * and we need to increment Ti. 602 */ 603 Ti++; 604 } else { 605 break; 606 } 607 } 608 609 /* 610 * We've parsed all of the cbits. At this point, we should take into 611 * account all of the historical bits and potentially the checksum. 612 */ 613 if (idx - 1 + nhist + ncksum >= len) { 614 return (ATR_CODE_OVERRUN); 615 } 616 617 if (idx + nhist + ncksum != len) { 618 return (ATR_CODE_UNDERRUN); 619 } 620 621 if (nhist > 0) { 622 data->atr_nhistoric = nhist; 623 bcopy(&buf[idx], data->atr_historic, nhist); 624 } 625 626 if (ncksum > 0) { 627 size_t i; 628 uint8_t val; 629 630 /* 631 * Per ISO/IEC 7816-3:2006 Section 8.2.5 the checksum is all 632 * bytes excluding TS. Therefore, we must start at byte 1. 633 */ 634 for (val = 0, i = 1; i < len; i++) { 635 val ^= buf[i]; 636 } 637 638 if (val != ATR_CKSUM_TARGET) { 639 return (ATR_CODE_CHECKSUM_ERROR); 640 } 641 data->atr_flags |= ATR_F_HAS_CHECKSUM; 642 data->atr_cksum = buf[len - 1]; 643 } 644 645 data->atr_flags |= ATR_F_VALID; 646 return (ATR_CODE_OK); 647 } 648 649 uint8_t 650 atr_fi_default_index(void) 651 { 652 return (ATR_FI_DEFAULT_INDEX); 653 } 654 655 uint8_t 656 atr_di_default_index(void) 657 { 658 return (ATR_DI_DEFAULT_INDEX); 659 } 660 661 /* 662 * Parse the data to determine which protocols are supported in this atr data. 663 * Based on this, users can come and ask us to fill in protocol information. 664 */ 665 atr_protocol_t 666 atr_supported_protocols(atr_data_t *data) 667 { 668 uint_t i; 669 atr_protocol_t prot; 670 671 if ((data->atr_flags & ATR_F_VALID) == 0) 672 return (ATR_P_NONE); 673 674 /* 675 * Based on 8.2.3 of ISO/IEC 7816-3:2006, if TD1 is present, then that 676 * indicates the first protocol. However, if it is not present, then 677 * that implies that T=0 is the only supported protocol. Otherwise, all 678 * protocols are referenced in ascending order. The first entry in 679 * atr_ti refers to data from T0, so the protocol in the second entry 680 * would have the TD1 data. 681 */ 682 if (data->atr_nti < 2) { 683 return (ATR_P_T0); 684 } 685 686 prot = ATR_P_NONE; 687 for (i = 0; i < data->atr_nti; i++) { 688 switch (data->atr_ti[i].atrti_protocol) { 689 case ATR_PROTOCOL_T0: 690 prot |= ATR_P_T0; 691 break; 692 case ATR_PROTOCOL_T1: 693 prot |= ATR_P_T1; 694 break; 695 default: 696 /* 697 * T=15 is not a protocol, and all other protocol values 698 * are currently reserved for future use. 699 */ 700 continue; 701 } 702 } 703 704 /* 705 * It's possible we've found nothing specific in the above loop (for 706 * example, only T=15 global bits were found). In that case, the card 707 * defaults to T=0. 708 */ 709 if (prot == ATR_P_NONE) 710 prot = ATR_P_T0; 711 712 return (prot); 713 } 714 715 boolean_t 716 atr_params_negotiable(atr_data_t *data) 717 { 718 /* If for some reason we're called with invalid data, assume it's not */ 719 if ((data->atr_flags & ATR_F_VALID) == 0) 720 return (B_FALSE); 721 722 723 /* 724 * Whether or not we're negotiable is in the second global page, so atr 725 * index 1. If TA2 is missing, then the card always is negotiable. 726 */ 727 if (data->atr_nti < 2 || 728 (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) == 0) { 729 return (B_TRUE); 730 } 731 732 if (ATR_TA2_CANCHANGE(data->atr_ti[1].atrti_ta)) { 733 return (B_TRUE); 734 } 735 736 return (B_FALSE); 737 } 738 739 atr_protocol_t 740 atr_default_protocol(atr_data_t *data) 741 { 742 uint8_t prot; 743 744 if ((data->atr_flags & ATR_F_VALID) == 0) 745 return (ATR_P_NONE); 746 /* 747 * If we don't have an TA2 byte, then the system defaults to T=0. 748 */ 749 if (data->atr_nti < 2) { 750 return (ATR_P_T0); 751 } 752 753 /* 754 * If TA2 is present, then it encodes the default protocol. Otherwise, 755 * we have to grab the protocol value from TD1, which is called the 756 * 'first offered protocol'. 757 */ 758 if ((data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) { 759 prot = ATR_TA2_PROTOCOL(data->atr_ti[1].atrti_ta); 760 } else { 761 prot = data->atr_ti[1].atrti_protocol; 762 } 763 764 switch (prot) { 765 case ATR_PROTOCOL_T0: 766 return (ATR_P_T0); 767 case ATR_PROTOCOL_T1: 768 return (ATR_P_T1); 769 default: 770 return (ATR_P_NONE); 771 } 772 } 773 774 uint8_t 775 atr_fi_index(atr_data_t *data) 776 { 777 if (data->atr_nti < 1) { 778 return (ATR_FI_DEFAULT_INDEX); 779 } 780 781 /* 782 * If TA is specified, it is present in TA1. TA2 may override its 783 * presence, so if it is here, check that first to determine whether or 784 * not we should check TA1. 785 */ 786 if (data->atr_nti >= 2 && 787 (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) { 788 if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) { 789 return (ATR_FI_DEFAULT_INDEX); 790 } 791 } 792 793 if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) { 794 return (ATR_TA1_FTABLE(data->atr_ti[0].atrti_ta)); 795 } 796 797 return (ATR_FI_DEFAULT_INDEX); 798 } 799 800 uint8_t 801 atr_di_index(atr_data_t *data) 802 { 803 if (data->atr_nti < 1) { 804 return (ATR_DI_DEFAULT_INDEX); 805 } 806 807 /* 808 * If TA is specified, it is present in TA1. TA2 may override its 809 * presence, so if it is here, check that first to determine whether or 810 * not we should check TA1. 811 */ 812 if (data->atr_nti >= 2 && 813 (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) { 814 if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) { 815 return (ATR_DI_DEFAULT_INDEX); 816 } 817 } 818 819 if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) { 820 return (ATR_TA1_DITABLE(data->atr_ti[0].atrti_ta)); 821 } 822 823 return (ATR_DI_DEFAULT_INDEX); 824 } 825 826 atr_convention_t 827 atr_convention(atr_data_t *data) 828 { 829 if ((data->atr_flags & ATR_F_USES_DIRECT) != 0) { 830 return (ATR_CONVENTION_DIRECT); 831 } 832 return (ATR_CONVENTION_INVERSE); 833 } 834 835 uint8_t 836 atr_extra_guardtime(atr_data_t *data) 837 { 838 if ((data->atr_flags & ATR_F_VALID) == 0) 839 return (ATR_EXTRA_GUARDTIME_DEFAULT); 840 841 if (data->atr_nti >= 1 && 842 (data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TC) != 0) { 843 return (data->atr_ti[0].atrti_tc); 844 } 845 846 return (ATR_EXTRA_GUARDTIME_DEFAULT); 847 } 848 849 uint8_t 850 atr_t0_wi(atr_data_t *data) 851 { 852 if ((data->atr_flags & ATR_F_VALID) == 0) 853 return (ATR_T0_WI_DEFAULT); 854 855 /* 856 * This is stored in the optional global byte in TC2; however, it only 857 * applies to T=0. 858 */ 859 if (data->atr_nti >= 2 && 860 data->atr_ti[1].atrti_protocol == ATR_PROTOCOL_T0 && 861 (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TC) != 0) { 862 return (data->atr_ti[1].atrti_tc); 863 } 864 865 return (ATR_T0_WI_DEFAULT); 866 } 867 868 uint8_t 869 atr_t1_cwi(atr_data_t *data) 870 { 871 uint8_t i; 872 873 if (data->atr_nti <= 2) { 874 return (ATR_T1_CWI_DEFAULT); 875 } 876 877 for (i = 2; i < data->atr_nti; i++) { 878 if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) { 879 if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) != 880 0) { 881 uint8_t tb = data->atr_ti[i].atrti_tb; 882 return (ATR_T1_TB0_CWI(tb)); 883 } 884 885 return (ATR_T1_CWI_DEFAULT); 886 } 887 } 888 889 return (ATR_T1_CWI_DEFAULT); 890 } 891 892 atr_clock_stop_t 893 atr_clock_stop(atr_data_t *data) 894 { 895 uint8_t i; 896 897 for (i = 0; i < data->atr_nti; i++) { 898 if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T15) { 899 if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) != 900 0) { 901 uint8_t ta = data->atr_ti[i].atrti_ta; 902 return (ATR_T15_TA0_CLOCK(ta)); 903 } 904 905 return (ATR_CLOCK_STOP_NONE); 906 } 907 } 908 909 return (ATR_CLOCK_STOP_NONE); 910 } 911 912 atr_t1_checksum_t 913 atr_t1_checksum(atr_data_t *data) 914 { 915 uint8_t i; 916 917 if (data->atr_nti <= 2) { 918 return (ATR_T1_CHECKSUM_DEFAULT); 919 } 920 921 for (i = 2; i < data->atr_nti; i++) { 922 if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) { 923 if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TC) != 924 0) { 925 if (ATR_T1_TC0_CRC(data->atr_ti[i].atrti_tc)) { 926 return (ATR_T1_CHECKSUM_CRC); 927 } else { 928 return (ATR_T1_CHECKSUM_LRC); 929 } 930 } 931 932 return (ATR_T1_CHECKSUM_DEFAULT); 933 } 934 } 935 936 return (ATR_T1_CHECKSUM_DEFAULT); 937 938 } 939 940 uint8_t 941 atr_t1_bwi(atr_data_t *data) 942 { 943 uint8_t i; 944 945 if (data->atr_nti <= 2) { 946 return (ATR_T1_BWI_DEFAULT); 947 } 948 949 for (i = 2; i < data->atr_nti; i++) { 950 if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) { 951 if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) != 952 0) { 953 uint8_t tb = data->atr_ti[i].atrti_tb; 954 return (ATR_T1_TB0_BWI(tb)); 955 } 956 957 return (ATR_T1_BWI_DEFAULT); 958 } 959 } 960 961 return (ATR_T1_BWI_DEFAULT); 962 } 963 964 uint8_t 965 atr_t1_ifsc(atr_data_t *data) 966 { 967 uint8_t i; 968 969 if (data->atr_nti <= 2) { 970 return (ATR_T1_IFSC_DEFAULT); 971 } 972 973 for (i = 2; i < data->atr_nti; i++) { 974 if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) { 975 if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) != 976 0) { 977 return (data->atr_ti[i].atrti_ta); 978 } 979 980 return (ATR_T1_IFSC_DEFAULT); 981 } 982 } 983 984 return (ATR_T1_IFSC_DEFAULT); 985 } 986 987 /* 988 * Attempt to determine which set of data rates we should be able to use for a 989 * given class of protocol. Here we want to do the calculation based on the CCID 990 * specification, section 9.4.x. To use these higher rates we need: 991 * 992 * + Reader's data rate > frequency * Di / Fi. 993 * 994 * To determine which rate and frequency we use, we look at the reader's 995 * features. If the reader supports both the Automatic baud rate and automatic 996 * ICC clock frequency change, then we use the _maximum_ rate. Otherwise we will 997 * indicate that we can use the ATR's properties, but will require changing the 998 * default data rate. 999 * 1000 * Now, some ICC devices are not negotiable. In those cases, we'll see if we can 1001 * fit it in with either the default or maximum data rates. If not, then we'll 1002 * not be able to support this card. 1003 * 1004 * There are two wrinkles that exist in this. The first is supported frequencies 1005 * and data rates. If there are no additional data rates supported, then all of 1006 * the data rates between the default and max are supported. If not, then only 1007 * those specified in the data rates array are supported. 1008 * 1009 * The second hurdle is that we need to do this division and try and avoid the 1010 * pitfalls of floating point arithmetic, as floating point is not allowed in 1011 * the kernel (and this is shared). Importantly that means only integers are 1012 * allowed here. 1013 */ 1014 atr_data_rate_choice_t 1015 atr_data_rate(atr_data_t *data, ccid_class_descr_t *class, uint32_t *rates, 1016 uint_t nrates, uint32_t *dataratep) 1017 { 1018 uint_t nfeats = CCID_CLASS_F_AUTO_ICC_CLOCK | CCID_CLASS_F_AUTO_BAUD; 1019 uint8_t di, fi; 1020 uint_t dival, fival; 1021 boolean_t autospeed, negotiable, exprates; 1022 uint64_t maxval, defval; 1023 1024 if ((data->atr_flags & ATR_F_VALID) == 0) 1025 return (ATR_RATE_UNSUPPORTED); 1026 1027 di = atr_di_index(data); 1028 fi = atr_fi_index(data); 1029 dival = atr_di_index_to_value(di); 1030 fival = atr_fi_index_to_value(fi); 1031 autospeed = (class->ccd_dwFeatures & nfeats) == nfeats; 1032 exprates = class->ccd_bNumDataRatesSupported != 0; 1033 negotiable = atr_params_negotiable(data); 1034 1035 /* 1036 * We don't support cards with fixed rates at this time as it's not 1037 * clear what that rate should be. If it's negotiable, we'll let them 1038 * run at the default. Otherwise, we have to fail the request until 1039 * we implement the logic to search their data rates. 1040 */ 1041 if (exprates) { 1042 if (negotiable) { 1043 return (ATR_RATE_USEDEFAULT); 1044 } 1045 return (ATR_RATE_UNSUPPORTED); 1046 } 1047 1048 /* 1049 * This indicates that the card gave us values that were reserved for 1050 * future use. If we could negotiate it, then just stick with the 1051 * default paramters. Otherwise, return that we can't support this ICC. 1052 */ 1053 if (dival == 0 || fival == 0) { 1054 if (negotiable) 1055 return (ATR_RATE_USEDEFAULT); 1056 return (ATR_RATE_UNSUPPORTED); 1057 } 1058 1059 /* 1060 * Calculate the maximum and default values. 1061 */ 1062 maxval = class->ccd_dwMaximumClock * 1000; 1063 maxval *= dival; 1064 maxval /= fival; 1065 1066 defval = class->ccd_dwDefaultClock * 1000; 1067 defval *= dival; 1068 defval /= fival; 1069 1070 /* 1071 * We're allowed any set of data rates between the default and the 1072 * maximum. Check if the maximum data rate will work for either the 1073 * default or maximum clock. If so, then we can use the cards rates. 1074 * 1075 * To account for the fact that we may have had a fractional value, 1076 * we require a strict greater than comparison. 1077 */ 1078 if ((uint64_t)class->ccd_dwMaxDataRate > maxval || 1079 (uint64_t)class->ccd_dwMaxDataRate > defval) { 1080 if (autospeed) { 1081 return (ATR_RATE_USEATR); 1082 } 1083 } 1084 1085 /* 1086 * If the CCID reader can't handle the ICC's proposed rates, then fall 1087 * back to the defaults if we're allowed to negotiate. Otherwise, we're 1088 * not able to use this ICC. 1089 */ 1090 if (negotiable) { 1091 return (ATR_RATE_USEDEFAULT); 1092 } 1093 1094 return (ATR_RATE_UNSUPPORTED); 1095 } 1096 1097 void 1098 atr_data_reset(atr_data_t *data) 1099 { 1100 bzero(data, sizeof (*data)); 1101 } 1102 1103 #ifdef _KERNEL 1104 atr_data_t * 1105 atr_data_alloc(void) 1106 { 1107 return (kmem_zalloc(sizeof (atr_data_t), KM_SLEEP)); 1108 } 1109 1110 void 1111 atr_data_free(atr_data_t *data) 1112 { 1113 kmem_free(data, sizeof (atr_data_t)); 1114 } 1115 1116 /* 1117 * Make sure that the response we got from the ICC is valid. It must pass 1118 * checksum and have the PPSS value set correctly. The protocol must match 1119 * what we requested; however, the PPS1-3 bits are a bit different. They may 1120 * only be set in the response if we set them in the request. However, they 1121 * do not have to be set in the response. 1122 */ 1123 boolean_t 1124 atr_pps_valid(void *reqbuf, size_t reqlen, void *respbuf, size_t resplen) 1125 { 1126 uint8_t val, i, reqidx, respidx; 1127 uint8_t *req = reqbuf, *resp = respbuf; 1128 1129 if (resplen > PPS_LEN_MAX || resplen < PPS_LEN_MIN) 1130 return (B_FALSE); 1131 1132 /* 1133 * Before we validate the data, make sure the checksum is valid. 1134 */ 1135 for (i = 0, val = 0; i < resplen; i++) { 1136 val ^= resp[i]; 1137 } 1138 1139 /* Checksum failure */ 1140 if (val != 0) { 1141 return (B_FALSE); 1142 } 1143 1144 /* 1145 * We should always have PPSS echoed back as we set it. 1146 */ 1147 if (resp[PPS_PPSS_INDEX] != PPS_PPSS_VAL) { 1148 return (B_FALSE); 1149 } 1150 1151 /* 1152 * Go through and make sure the number of bytes present makes sense for 1153 * the number of bits set in PPS1. 1154 */ 1155 val = PPS_LEN_MIN; 1156 if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) 1157 val++; 1158 if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) 1159 val++; 1160 if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3) 1161 val++; 1162 if (val != resplen) 1163 return (B_FALSE); 1164 1165 /* 1166 * Now we've finally verified that the response is syntactically valid. 1167 * We must go through and make sure that it is semantically valid. 1168 */ 1169 if (PPS_PPS0_PROT(req[PPS_PPS0_INDEX]) != 1170 PPS_PPS0_PROT(resp[PPS_PPS0_INDEX])) { 1171 return (B_FALSE); 1172 } 1173 1174 /* 1175 * When checking the PPS bit and extensions, we first check in the 1176 * response as a bit in the request is allowed to not be in the 1177 * response. But not the opposite way around. We also have to keep track 1178 * of the fact that the index for values will vary. 1179 */ 1180 reqidx = respidx = PPS_PPS0_INDEX + 1; 1181 if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) { 1182 if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) == 0) { 1183 return (B_FALSE); 1184 } 1185 1186 if (req[reqidx] != resp[respidx]) { 1187 return (B_FALSE); 1188 } 1189 1190 reqidx++; 1191 respidx++; 1192 } else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) { 1193 reqidx++; 1194 } 1195 1196 if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) { 1197 if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) == 0) { 1198 return (B_FALSE); 1199 } 1200 1201 if (req[reqidx] != resp[respidx]) { 1202 return (B_FALSE); 1203 } 1204 1205 reqidx++; 1206 respidx++; 1207 } else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) { 1208 reqidx++; 1209 } 1210 1211 if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3) != 0) { 1212 /* 1213 * At this time, we never specify PPS3 in a request. Therefore 1214 * if it is present in the response, treat this as an invalid 1215 * request. 1216 */ 1217 return (B_FALSE); 1218 } 1219 1220 return (B_TRUE); 1221 } 1222 1223 uint_t 1224 atr_pps_generate(uint8_t *buf, size_t buflen, atr_protocol_t prot, 1225 boolean_t pps1, uint8_t fi, uint8_t di, boolean_t pps2, uint8_t spu) 1226 { 1227 uint8_t protval, cksum, i; 1228 uint_t len = 0; 1229 1230 if (buflen < PPS_BUFFER_MAX) 1231 return (0); 1232 1233 buf[PPS_PPSS_INDEX] = PPS_PPSS_VAL; 1234 switch (prot) { 1235 case ATR_P_T0: 1236 protval = 0; 1237 break; 1238 case ATR_P_T1: 1239 protval = 1; 1240 break; 1241 default: 1242 return (0); 1243 } 1244 1245 buf[PPS_PPS0_INDEX] = PPS_PPS0_PROT(protval); 1246 len = 2; 1247 if (pps1) { 1248 buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS1; 1249 buf[len++] = PPS_PPS1_SETVAL(fi, di); 1250 } 1251 1252 if (pps2) { 1253 buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS2; 1254 buf[len++] = spu; 1255 } 1256 1257 /* 1258 * The checksum must xor to zero. 1259 */ 1260 for (i = 0, cksum = 0; i < len; i++) { 1261 cksum ^= buf[i]; 1262 } 1263 buf[len++] = cksum; 1264 return (len); 1265 } 1266 1267 /* 1268 * The caller of this wants to know if the Fi/Di values that they proposed were 1269 * accepted. The caller must have already called atr_pps_valid(). At this point, 1270 * we can say that the value was accepted if the PPS1 bit is set. 1271 */ 1272 boolean_t 1273 atr_pps_fidi_accepted(void *respbuf, size_t len) 1274 { 1275 uint8_t *resp = respbuf; 1276 return ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0); 1277 } 1278 1279 #else /* !_KERNEL */ 1280 atr_data_t * 1281 atr_data_alloc(void) 1282 { 1283 return (calloc(1, sizeof (atr_data_t))); 1284 } 1285 1286 void 1287 atr_data_free(atr_data_t *data) 1288 { 1289 if (data == NULL) 1290 return; 1291 free(data); 1292 } 1293 1294 /* 1295 * This table maps the bit values for Fi from 7816-3:2006 section 8.3 Table 9. 1296 * The table is up to 6 bits wide. Entries not present are RFU. We use NULL as a 1297 * sentinel to indicate that. 1298 */ 1299 static const char *atr_voltage_table[64] = { 1300 NULL, /* 00 0000 */ 1301 "5V", /* 00 0001 */ 1302 "3V", /* 00 0010 */ 1303 "5V, 3V", /* 00 0011 */ 1304 "1.5V", /* 00 0100 */ 1305 NULL, /* 00 0101 */ 1306 "3V, 1.5V", /* 00 0110 */ 1307 "5V, 3V, 1.5V" /* 00 0111 */ 1308 }; 1309 1310 static void 1311 atr_data_dump_ta(atr_ti_t *atp, FILE *out, uint_t level) 1312 { 1313 uint8_t ta; 1314 1315 if (!(atp->atrti_flags & ATR_TI_HAVE_TA)) { 1316 return; 1317 } 1318 1319 ta = atp->atrti_ta; 1320 (void) fprintf(out, " %c%c%c+-> TA%u 0x%02x", 1321 atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ', 1322 atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ', 1323 atp->atrti_flags & ATR_TI_HAVE_TB ? '|' : ' ', 1324 atp->atrti_ti_val, ta); 1325 switch (atp->atrti_ti_val) { 1326 case 1: 1327 (void) fprintf(out, "; Fi: %s, F(max): %s MHz, Di: %s", 1328 atr_fi_table[ATR_TA1_FTABLE(ta)], 1329 atr_fmax_table[ATR_TA1_FTABLE(ta)], 1330 atr_di_table[ATR_TA1_DITABLE(ta)]); 1331 break; 1332 case 2: 1333 (void) fprintf(out, "; ICC in %s mode; %shonoring TA1; default " 1334 "T=%u", 1335 ATR_TA2_CANCHANGE(ta) ? "negotiable" : "specific", 1336 ATR_TA2_HONORTA1(ta) ? "" : "not ", 1337 ATR_TA2_PROTOCOL(ta)); 1338 break; 1339 default: 1340 switch (atp->atrti_protocol) { 1341 case ATR_PROTOCOL_T1: 1342 if (level != 0) 1343 break; 1344 if (ta == 0 || ta == 0xff) { 1345 (void) fprintf(out, "; IFSC: RFU"); 1346 } else { 1347 (void) fprintf(out, "; IFSC: %u", ta); 1348 } 1349 break; 1350 case ATR_PROTOCOL_T15: 1351 if (level != 0) 1352 break; 1353 (void) fprintf(out, "; Clock stop: %s, Supported " 1354 "Voltage: %s", 1355 atr_clock_table[ATR_T15_TA0_CLOCK(ta)], 1356 atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] != NULL ? 1357 atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] : "RFU"); 1358 break; 1359 default: 1360 break; 1361 } 1362 } 1363 (void) fprintf(out, "\n"); 1364 } 1365 1366 static void 1367 atr_data_dump_tb(atr_ti_t *atp, FILE *out, uint_t level) 1368 { 1369 uint8_t tb; 1370 1371 if (!(atp->atrti_flags & ATR_TI_HAVE_TB)) { 1372 return; 1373 } 1374 1375 tb = atp->atrti_tb; 1376 (void) fprintf(out, " %c%c+--> TB%u 0x%02x", 1377 atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ', 1378 atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ', 1379 atp->atrti_ti_val, tb); 1380 switch (atp->atrti_ti_val) { 1381 case 1: 1382 case 2: 1383 (void) fprintf(out, "; deprecated"); 1384 break; 1385 default: 1386 switch (atp->atrti_protocol) { 1387 case ATR_PROTOCOL_T1: 1388 if (level != 0) 1389 break; 1390 (void) fprintf(out, "; CWI: %u, BWI: %u\n", 1391 ATR_T1_TB0_CWI(tb), 1392 ATR_T1_TB0_BWI(tb)); 1393 break; 1394 case ATR_PROTOCOL_T15: 1395 if (level != 0) 1396 break; 1397 (void) fprintf(out, "; SPU: %s", tb == 0 ? "not used" : 1398 ATR_T15_TB0_SPU_STANDARD(tb) ? "standard" : 1399 "proprietary"); 1400 break; 1401 default: 1402 break; 1403 } 1404 } 1405 (void) fprintf(out, "\n"); 1406 } 1407 1408 static void 1409 atr_data_dump_tc(atr_ti_t *atp, FILE *out, uint_t level) 1410 { 1411 uint8_t tc; 1412 1413 if (!(atp->atrti_flags & ATR_TI_HAVE_TC)) { 1414 return; 1415 } 1416 1417 tc = atp->atrti_tc; 1418 (void) fprintf(out, " %c+---> TC%u 0x%02x", 1419 atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ', 1420 atp->atrti_ti_val, tc); 1421 1422 switch (atp->atrti_ti_val) { 1423 case 1: 1424 (void) fprintf(out, "; Extra Guard Time Integer: %u", tc); 1425 break; 1426 case 2: 1427 if (atp->atrti_protocol != ATR_PROTOCOL_T0) { 1428 (void) fprintf(out, "; illegal value -- only valid for " 1429 "T=0"); 1430 } else { 1431 (void) fprintf(out, "; Waiting Time Integer: %u", tc); 1432 } 1433 break; 1434 default: 1435 switch (atp->atrti_protocol) { 1436 case ATR_PROTOCOL_T1: 1437 if (level != 0) 1438 break; 1439 (void) fprintf(out, "; Error Detection Code: %s", 1440 ATR_T1_TC0_CRC(tc) ? "CRC" : "LRC"); 1441 break; 1442 default: 1443 break; 1444 } 1445 } 1446 (void) fprintf(out, "\n"); 1447 } 1448 1449 void 1450 atr_data_hexdump(const uint8_t *buf, size_t nbytes, FILE *out) 1451 { 1452 hexdump_t h; 1453 1454 hexdump_init(&h); 1455 hexdump_set_grouping(&h, 4); 1456 (void) hexdump_fileh(&h, buf, nbytes, HDF_DEFAULT, out); 1457 hexdump_fini(&h); 1458 } 1459 1460 static void 1461 atr_data_hexdump_historical(atr_data_t *data, FILE *out) 1462 { 1463 (void) fprintf(out, "Dumping raw historical bytes\n"); 1464 1465 atr_data_hexdump(data->atr_historic, data->atr_nhistoric, out); 1466 } 1467 1468 static void 1469 atr_data_dump_historical(atr_data_t *data, FILE *out) 1470 { 1471 uint8_t cat; 1472 1473 (void) fprintf(out, "Historic Data: %u bytes", data->atr_nhistoric); 1474 if (data->atr_nhistoric == 0) { 1475 (void) fprintf(out, "\n"); 1476 return; 1477 } 1478 1479 cat = data->atr_historic[0]; 1480 (void) fprintf(out, "; format (0x%02x) ", cat); 1481 if (cat == ATR_HIST_CAT_MAND_STATUS) { 1482 (void) fprintf(out, "card status, not shown"); 1483 } else if (cat == ATR_HIST_CAT_TLV_STATUS) { 1484 (void) fprintf(out, "COMPACT-TLV, not shown"); 1485 } else if (cat >= ATR_HIST_CAT_RFU_MIN && cat <= ATR_HIST_CAT_RFU_MAX) { 1486 (void) fprintf(out, "reserved\n"); 1487 atr_data_hexdump_historical(data, out); 1488 return; 1489 } else { 1490 (void) fprintf(out, "proprietary\n"); 1491 atr_data_hexdump_historical(data, out); 1492 return; 1493 } 1494 } 1495 1496 void 1497 atr_data_dump(atr_data_t *data, FILE *out) 1498 { 1499 uint8_t i, level; 1500 if ((data->atr_flags & ATR_F_VALID) == 0) 1501 return; 1502 1503 (void) fprintf(out, "TS 0x%02u - ", data->atr_raw[0]); 1504 if (data->atr_flags & ATR_F_USES_DIRECT) { 1505 (void) fprintf(out, "direct convention\n"); 1506 } else { 1507 (void) fprintf(out, "inverse convention\n"); 1508 } 1509 1510 level = 0; 1511 for (i = 0; i < data->atr_nti; i++) { 1512 atr_ti_t *atp = &data->atr_ti[i]; 1513 1514 /* 1515 * Various protocols may appear multiple times, indicating 1516 * different sets of bits each time. When dealing with T0 and 1517 * TD1, the protocol doesn't matter. Otherwise if we have the 1518 * same value, we should increment this. 1519 */ 1520 if (i <= 2) { 1521 level = 0; 1522 } else if (atp->atrti_protocol == 1523 data->atr_ti[i - 1].atrti_protocol) { 1524 level++; 1525 } else { 1526 level = 0; 1527 } 1528 1529 if (i == 0) { 1530 (void) fprintf(out, "T0 "); 1531 } else { 1532 (void) fprintf(out, "TD%u ", i); 1533 } 1534 (void) fprintf(out, "0x%02x\n", 1535 data->atr_raw[atp->atrti_td_idx]); 1536 (void) fprintf(out, " |+-> "); 1537 if (i == 0) { 1538 (void) fprintf(out, "%u historical bytes\n", 1539 data->atr_nhistoric); 1540 } else { 1541 (void) fprintf(out, "protocol T=%u\n", 1542 atp->atrti_protocol); 1543 } 1544 (void) fprintf(out, " v\n"); 1545 (void) fprintf(out, " 0r%u%u%u%u\n", 1546 atp->atrti_flags & ATR_TI_HAVE_TD ? 1 : 0, 1547 atp->atrti_flags & ATR_TI_HAVE_TC ? 1 : 0, 1548 atp->atrti_flags & ATR_TI_HAVE_TB ? 1 : 0, 1549 atp->atrti_flags & ATR_TI_HAVE_TA ? 1 : 0); 1550 1551 atr_data_dump_ta(atp, out, level); 1552 atr_data_dump_tb(atp, out, level); 1553 atr_data_dump_tc(atp, out, level); 1554 if (atp->atrti_flags & ATR_TI_HAVE_TD) { 1555 (void) fprintf(out, " v\n"); 1556 } 1557 } 1558 1559 atr_data_dump_historical(data, out); 1560 1561 if (data->atr_flags & ATR_F_HAS_CHECKSUM) { 1562 (void) fprintf(out, "TCK 0x%02x\n", data->atr_cksum); 1563 } else { 1564 (void) fprintf(out, "TCK ----; Checksum not present\n"); 1565 } 1566 1567 } 1568 #endif /* _KERNEL */ 1569