annotate simtool/hlread.c @ 161:d3128f3e1bde

fc-simtool and fc-uicc-tool moved to new fc-pcsc-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 05:39:38 +0000
parents 90eff13a72fd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements some high-level or user-friendly read commands.
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <pcsclite.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <winscard.h>
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "globals.h"
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "file_id.h"
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 encode_hex_digit(d)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 unsigned d;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (d <= 9)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 return(d + '0');
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 else
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 return(d - 10 + 'A');
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 decode_reversed_nibbles(bytes, nbytes, dest)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 u_char *bytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 unsigned nbytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char *dest;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 u_char *sp;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 char *dp;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 unsigned n, c;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 sp = bytes;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 dp = dest;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 for (n = 0; n < nbytes; n++) {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 c = *sp & 0xF;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 *dp++ = encode_hex_digit(c);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 c = *sp >> 4;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 *dp++ = encode_hex_digit(c);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 sp++;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 cmd_iccid()
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 int rc;
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 char buf[21];
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 rc = select_op(FILEID_MF);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 rc = select_op(EF_ICCID);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rc = parse_ef_select_response();
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (curfile_structure != 0x00 || curfile_total_size != 10) {
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr, "error: expected transparent EF of 10 bytes\n");
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return(-1);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 rc = readbin_op(0, 10);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (rc < 0)
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return(rc);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 decode_reversed_nibbles(sim_resp_data, 10, buf);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 buf[20] = '\0';
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 printf("%s\n", buf);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return(0);
a5dfab380a90 fc-simtool: iccid high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
97
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
68
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
69 cmd_imsi()
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
70 {
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
71 int rc;
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
72 char buf[17];
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
73
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
74 rc = select_op(DF_GSM);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
75 if (rc < 0)
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
76 return(rc);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
77 rc = select_op(EF_IMSI);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
78 if (rc < 0)
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
79 return(rc);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
80 rc = parse_ef_select_response();
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
81 if (rc < 0)
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
82 return(rc);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
83 if (curfile_structure != 0x00 || curfile_total_size != 9) {
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
84 fprintf(stderr, "error: expected transparent EF of 9 bytes\n");
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
85 return(-1);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
86 }
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
87 rc = readbin_op(0, 9);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
88 if (rc < 0)
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
89 return(rc);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
90 decode_reversed_nibbles(sim_resp_data + 1, 8, buf);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
91 buf[16] = '\0';
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
92 printf("%s parity=%c len=%u\n", buf + 1, buf[0], sim_resp_data[0]);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
93 return(0);
597c4e87a1f4 fc-simtool: imsi high-level read command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
94 }
103
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
95
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
96 cmd_spn()
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
97 {
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
98 int rc;
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
99 unsigned textlen;
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
100
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
101 rc = select_op(DF_GSM);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
102 if (rc < 0)
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
103 return(rc);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
104 rc = select_op(EF_SPN);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
105 if (rc < 0)
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
106 return(rc);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
107 rc = parse_ef_select_response();
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
108 if (rc < 0)
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
109 return(rc);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
110 if (curfile_structure != 0x00 || curfile_total_size != 17) {
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
111 fprintf(stderr, "error: expected transparent EF of 17 bytes\n");
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
112 return(-1);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
113 }
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
114 rc = readbin_op(0, 17);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
115 if (rc < 0)
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
116 return(rc);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
117 printf("Display condition: %02X\n", sim_resp_data[0]);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
118 printf("SPN: ");
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
119 rc = validate_alpha_field(sim_resp_data + 1, 16, &textlen);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
120 if (rc >= 0)
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
121 print_alpha_field(sim_resp_data, textlen, stdout);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
122 else
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
123 printf("malformed alpha field");
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
124 putchar('\n');
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
125 return(0);
90eff13a72fd fc-simtool: implemented alpha field decoding and spn command
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
126 }