annotate simtool/sstprog.c @ 209:eb16c8841486

fc-simtool write-sst command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Mar 2021 06:06:23 +0000
parents simtool/sstlist.c@9c16cf1a79af
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
2 * This module implements the write-sst command for admin-level
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
3 * programming of SIM cards.
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
7 #include <ctype.h>
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
9 #include <stdlib.h>
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
10 #include <string.h>
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
11 #include <strings.h>
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "curfile.h"
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "file_id.h"
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
15 extern FILE *open_script_input_file();
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
16
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
17 cmd_write_sst(argc, argv)
116
9c16cf1a79af fc-simtool sst: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents: 60
diff changeset
18 char **argv;
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
20 u_char sst[255];
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
21 FILE *inf;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
22 int lineno, rc;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
23 char linebuf[1024], *cp, *np;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
24 unsigned num, code, max_serv;
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 rc = select_op(DF_GSM);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (rc < 0)
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(rc);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 rc = select_op(EF_SST);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (rc < 0)
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 return(rc);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 rc = parse_ef_select_response();
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (rc < 0)
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 return(rc);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (curfile_structure != 0x00) {
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 fprintf(stderr, "error: EF_SST is not transparent\n");
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(-1);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (curfile_total_size < 2) {
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr,
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 "error: EF_SST is shorter than spec minimum of 2 bytes\n");
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return(-1);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
44 if (curfile_total_size > 255) {
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 fprintf(stderr,
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
46 "error: EF_SST is longer than our 255 byte limit\n");
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
47 return(-1);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
48 }
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
49 memset(sst, 0, curfile_total_size);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
50 max_serv = curfile_total_size * 4;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
51 inf = open_script_input_file(argv[1]);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
52 if (!inf) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
53 perror(argv[1]);
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(-1);
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
56 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
57 if (!index(linebuf, '\n')) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
58 fprintf(stderr,
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
59 "%s line %d: too long or missing newline\n",
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
60 argv[1], lineno);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
61 fclose(inf);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
62 return(-1);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
63 }
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
64 for (cp = linebuf; ; ) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
65 while (isspace(*cp))
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
66 cp++;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
67 if (*cp == '\0' || *cp == '#')
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
68 break;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
69 if (!isdigit(*cp)) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
70 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n",
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
71 argv[1], lineno);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
72 fclose(inf);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
73 return(-1);
49
bbc2821288aa fc-simtool sst-dump: much more compact output
Mychaela Falconia <falcon@freecalypso.org>
parents: 48
diff changeset
74 }
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
75 num = strtoul(cp, 0, 10);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
76 while (isdigit(*cp))
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
77 cp++;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
78 if (*cp == '^') {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
79 cp++;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
80 code = 1;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
81 } else
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
82 code = 3;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
83 if (*cp && !isspace(*cp))
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
84 goto inv_syntax;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
85 if (num < 1 || num > max_serv) {
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
86 fprintf(stderr,
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
87 "%s line %d: service number is out of range\n",
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
88 argv[1], lineno);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
89 fclose(inf);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
90 return(-1);
49
bbc2821288aa fc-simtool sst-dump: much more compact output
Mychaela Falconia <falcon@freecalypso.org>
parents: 48
diff changeset
91 }
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
92 num--;
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
93 sst[num >> 2] |= code << ((num & 3) << 1);
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
209
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
96 fclose(inf);
eb16c8841486 fc-simtool write-sst command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 116
diff changeset
97 return update_bin_op(0, sst, curfile_total_size);
48
9a21f4353158 fc-simtool sst-dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }