annotate uicc/bfsearch.c @ 89:db131929ee96

fc-uicc-tool: extended readef ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:17:58 +0000
parents b70d35f5476f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements a brute force search of file ID space,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * both MF and ADF trees.
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "simresp.h"
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include "file_id.h"
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern u_char *extract_select_resp_tag();
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 bfsearch_dir(path, pathlen, siblings, nsiblings, outf)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 unsigned *path, pathlen, *siblings, nsiblings;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 FILE *outf;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned bfs, n;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned df_children[255], ndfc;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 unsigned childpath[8];
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 u_char *tlv_file_desc, *tlv_file_size;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int rc;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 for (n = 0; n < pathlen; n++) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 rc = elem_select_op(path[n]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (rc < 0)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(rc);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if ((sim_resp_sw & 0xFF00) != 0x6100) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 fprintf(stderr,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 "error selecting 0x%04X: SW resp 0x%04X\n",
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 path[n], sim_resp_sw);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 return(-1);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 ndfc = 0;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 for (bfs = 0; bfs <= 0xFFFF; bfs++) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (bfs == FILEID_MF || bfs == FILEID_ADF)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 continue;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 for (n = 0; n < pathlen; n++) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (bfs == path[n])
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 break;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (n < pathlen)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 continue;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 for (n = 0; n < nsiblings; n++) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (bfs == siblings[n])
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 break;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (n < nsiblings)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 continue;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 rc = elem_select_op(bfs);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rc < 0)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return(rc);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (sim_resp_sw == 0x6A82)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 continue;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if ((sim_resp_sw & 0xFF00) != 0x6100) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 for (n = 0; n < pathlen; n++)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 fprintf(outf, "%04X/", path[n]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 fprintf(outf, "%04X: SW response 0x%04X\n", bfs,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 sim_resp_sw);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 continue;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 rc = get_response_op();
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (rc < 0)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return(rc);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 tlv_file_desc = extract_select_resp_tag(0x82);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (!tlv_file_desc)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 return(-1);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (tlv_file_desc[1] < 1) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 fprintf(stderr,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 "error: file desc TLV object is too short\n");
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 return(-1);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 for (n = 0; n < pathlen; n++)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fprintf(outf, "%04X/", path[n]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fprintf(outf, "%04X: file desc 0x%02X", bfs, tlv_file_desc[2]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if ((tlv_file_desc[2] & 0xBF) == 0x38) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 fprintf(outf, ", DF\n");
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (ndfc < 255)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 df_children[ndfc++] = bfs;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 } else {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 tlv_file_size = extract_select_resp_tag(0x80);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (tlv_file_size && tlv_file_size[1] == 2)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 fprintf(outf, ", total size %u",
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 (tlv_file_size[2] << 8) |
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 tlv_file_size[3]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (tlv_file_desc[1] == 5)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(outf, ", %u records of %u bytes",
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 tlv_file_desc[6],
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 (tlv_file_desc[4] << 8) |
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 tlv_file_desc[5]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 putc('\n', outf);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 rc = elem_select_op(path[pathlen-1]);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (rc < 0)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 return(rc);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if ((sim_resp_sw & 0xFF00) != 0x6100) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 fprintf(stderr,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 "reselecting starting file ID 0x%04X: SW resp 0x%04X\n",
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 path[pathlen-1], sim_resp_sw);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 return(-1);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 if (pathlen >= 8)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 return(0);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 for (n = 0; n < pathlen; n++)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 childpath[n] = path[n];
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 for (n = 0; n < ndfc; n++) {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 childpath[pathlen] = df_children[n];
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 rc = bfsearch_dir(childpath, pathlen + 1, df_children, ndfc,
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 outf);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 if (rc < 0)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 return(rc);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 return(0);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 cmd_bfsearch_mf(argc, argv, outf)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 char **argv;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 FILE *outf;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 unsigned initpath;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 initpath = FILEID_MF;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 cmd_bfsearch_adf(argc, argv, outf)
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 char **argv;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 FILE *outf;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 {
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 unsigned initpath;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 initpath = FILEID_ADF;
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
b70d35f5476f fc-uicc-tool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 }