annotate simtool/saverestore.c @ 174:4f5abad5dd40

doc/Unbuffered-FT2232x-JTAG: update for fc-usbser-tools
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Sep 2023 07:23:18 +0000
parents c77b0d4cf89f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
99
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements commands for saving SIM file content in UNIX host
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * files and restoring these backups back to the SIM.
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
100
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
7 #include <sys/file.h>
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
8 #include <sys/stat.h>
99
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
100
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
11 #include <unistd.h>
99
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <pcsclite.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <winscard.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "globals.h"
119
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
15 #include "file_id.h"
99
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 savebin_transparent(outf)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 FILE *outf;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 unsigned off, cc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 for (off = 0; off < curfile_total_size; off += cc) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 cc = curfile_total_size - off;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (cc > 256)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 cc = 256;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 rc = readbin_op(off, cc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fwrite(sim_resp_data, 1, cc, outf);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(0);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 static
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 savebin_records(outf)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 FILE *outf;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 unsigned recno;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 int rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 for (recno = 1; recno <= curfile_record_count; recno++) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 rc = readrec_op(recno, 0x04, curfile_record_len);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fwrite(sim_resp_data, curfile_record_len, 1, outf);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(0);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 cmd_savebin(argc, argv)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 char **argv;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 int file_id, rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 FILE *of;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 file_id = strtoul(argv[1], 0, 16);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 else
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 file_id = find_symbolic_file_name(argv[1]);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (file_id < 0) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(stderr,
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 "error: file ID argument is not a hex value or a recognized symbolic name\n");
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return(-1);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 rc = select_op(file_id);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 rc = parse_ef_select_response();
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 of = fopen(argv[2], "w");
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (!of) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 perror(argv[2]);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 return(-1);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 switch (curfile_structure) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 case 0x00:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 /* transparent */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 rc = savebin_transparent(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 break;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 case 0x01:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 case 0x03:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 /* record-based */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 rc = savebin_records(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 break;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 fclose(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
100
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
93
119
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
94 cmd_save_sms_bin(argc, argv)
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
95 char **argv;
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
96 {
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
97 int rc;
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
98 FILE *of;
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
99
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
100 rc = select_op(DF_TELECOM);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
101 if (rc < 0)
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
102 return(rc);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
103 rc = select_op(EF_SMS);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
104 if (rc < 0)
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
105 return(rc);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
106 rc = parse_ef_select_response();
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
107 if (rc < 0)
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
108 return(rc);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
109 if (curfile_structure != 0x01 || curfile_record_len != 176) {
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
110 fprintf(stderr,
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
111 "error: EF_SMS is not linear fixed with 176-byte records\n");
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
112 return(-1);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
113 }
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
114 of = fopen(argv[1], "w");
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
115 if (!of) {
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
116 perror(argv[1]);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
117 return(-1);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
118 }
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
119 rc = savebin_records(of);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
120 fclose(of);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
121 return(rc);
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
122 }
c77b0d4cf89f fc-simtool: save-sms-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 118
diff changeset
123
100
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
124 static
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
125 restore_transparent(data)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
126 u_char *data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
127 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
128 unsigned off, cc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
129 int rc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
130
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
131 for (off = 0; off < curfile_total_size; off += cc) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
132 cc = curfile_total_size - off;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
133 if (cc > 255)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
134 cc = 255;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
135 rc = update_bin_op(off, data + off, cc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
136 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
137 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
138 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
139 return(0);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
140 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
141
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
142 static
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
143 restore_records(data)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
144 u_char *data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
145 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
146 unsigned recno;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
147 u_char *dp;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
148 int rc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
149
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
150 dp = data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
151 for (recno = 1; recno <= curfile_record_count; recno++) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
152 rc = update_rec_op(recno, 0x04, dp, curfile_record_len);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
153 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
154 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
155 dp += curfile_record_len;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
156 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
157 return(0);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
158 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
159
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
160 cmd_restore_file(argc, argv)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
161 char **argv;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
162 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
163 int file_id, rc, fd;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
164 struct stat st;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
165 u_char *databuf;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
166
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
167 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
168 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
169 file_id = strtoul(argv[1], 0, 16);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
170 else
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
171 file_id = find_symbolic_file_name(argv[1]);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
172 if (file_id < 0) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
173 fprintf(stderr,
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
174 "error: file ID argument is not a hex value or a recognized symbolic name\n");
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
175 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
176 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
177 rc = select_op(file_id);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
178 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
179 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
180 rc = parse_ef_select_response();
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
181 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
182 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
183 if (!curfile_total_size) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
184 printf("SIM indicates file of zero length, nothing to do\n");
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
185 return(0);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
186 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
187 fd = open(argv[2], O_RDONLY);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
188 if (fd < 0) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
189 perror(argv[2]);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
190 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
191 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
192 fstat(fd, &st);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
193 if ((st.st_mode & S_IFMT) != S_IFREG) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
194 fprintf(stderr, "error: %s is not a regular file\n", argv[2]);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
195 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
196 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
197 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
198 if (st.st_size != curfile_total_size) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
199 fprintf(stderr,
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
200 "error: length of %s does not match SIM EF length of %u bytes\n",
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
201 argv[2], curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
202 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
203 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
204 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
205 databuf = malloc(curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
206 if (!databuf) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
207 perror("malloc for file data");
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
208 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
209 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
210 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
211 read(fd, databuf, curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
212 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
213 switch (curfile_structure) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
214 case 0x00:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
215 /* transparent */
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
216 rc = restore_transparent(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
217 break;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
218 case 0x01:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
219 /* record-based */
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
220 rc = restore_records(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
221 break;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
222 case 0x03:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
223 fprintf(stderr, "error: cyclic files are not supported\n");
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
224 rc = -1;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
225 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
226 free(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
227 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
228 }