annotate uicc/bfsearch.c @ 153:2ef31306be22

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