FreeCalypso > hg > freecalypso-hwlab
annotate simtool/saverestore.c @ 118:b563ff1c1a2a
simtool/saverestore.c: rm unused variable
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 28 Jan 2021 04:11:28 +0000 |
parents | fa7005185b84 |
children | c77b0d4cf89f |
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" |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 savebin_transparent(outf) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 FILE *outf; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 unsigned off, cc; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 int rc; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 for (off = 0; off < curfile_total_size; off += cc) { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 cc = curfile_total_size - off; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (cc > 256) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 cc = 256; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 rc = readbin_op(off, cc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 if (rc < 0) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 return(rc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fwrite(sim_resp_data, 1, cc, outf); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 return(0); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 } |
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 static |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 savebin_records(outf) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 FILE *outf; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 unsigned recno; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 int rc; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 for (recno = 1; recno <= curfile_record_count; recno++) { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 rc = readrec_op(recno, 0x04, curfile_record_len); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (rc < 0) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 return(rc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fwrite(sim_resp_data, curfile_record_len, 1, outf); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 return(0); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 } |
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 cmd_savebin(argc, argv) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 char **argv; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 int file_id, rc; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 FILE *of; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 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
|
59 file_id = strtoul(argv[1], 0, 16); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 else |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 file_id = find_symbolic_file_name(argv[1]); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (file_id < 0) { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 fprintf(stderr, |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 "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
|
65 return(-1); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 rc = select_op(file_id); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (rc < 0) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 return(rc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 rc = parse_ef_select_response(); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (rc < 0) |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 return(rc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 of = fopen(argv[2], "w"); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (!of) { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 perror(argv[2]); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 return(-1); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 } |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 switch (curfile_structure) { |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 case 0x00: |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 /* transparent */ |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 rc = savebin_transparent(of); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 break; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 case 0x01: |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 case 0x03: |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 /* record-based */ |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 rc = savebin_records(of); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 break; |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 } |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 fclose(of); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 return(rc); |
2e35070d289f
fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 } |
100
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
92 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
93 static |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
94 restore_transparent(data) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
95 u_char *data; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
96 { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
97 unsigned off, cc; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
98 int rc; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
99 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
100 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
|
101 cc = curfile_total_size - off; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
102 if (cc > 255) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
103 cc = 255; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
104 rc = update_bin_op(off, data + off, cc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
105 if (rc < 0) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
106 return(rc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
107 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
108 return(0); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
109 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
110 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
111 static |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
112 restore_records(data) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
113 u_char *data; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
114 { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
115 unsigned recno; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
116 u_char *dp; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
117 int rc; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
118 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
119 dp = data; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
120 for (recno = 1; recno <= curfile_record_count; recno++) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
121 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
|
122 if (rc < 0) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
123 return(rc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
124 dp += curfile_record_len; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
125 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
126 return(0); |
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 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
129 cmd_restore_file(argc, argv) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
130 char **argv; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
131 { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
132 int file_id, rc, fd; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
133 struct stat st; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
134 u_char *databuf; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
135 |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
136 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
|
137 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
|
138 file_id = strtoul(argv[1], 0, 16); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
139 else |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
140 file_id = find_symbolic_file_name(argv[1]); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
141 if (file_id < 0) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
142 fprintf(stderr, |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
143 "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
|
144 return(-1); |
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 rc = select_op(file_id); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
147 if (rc < 0) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
148 return(rc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
149 rc = parse_ef_select_response(); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
150 if (rc < 0) |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
151 return(rc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
152 if (!curfile_total_size) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
153 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
|
154 return(0); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
155 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
156 fd = open(argv[2], O_RDONLY); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
157 if (fd < 0) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
158 perror(argv[2]); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
159 return(-1); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
160 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
161 fstat(fd, &st); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
162 if ((st.st_mode & S_IFMT) != S_IFREG) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
163 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
|
164 close(fd); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
165 return(-1); |
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 (st.st_size != curfile_total_size) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
168 fprintf(stderr, |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
169 "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
|
170 argv[2], curfile_total_size); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
171 close(fd); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
172 return(-1); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
173 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
174 databuf = malloc(curfile_total_size); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
175 if (!databuf) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
176 perror("malloc for file data"); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
177 close(fd); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
178 return(-1); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
179 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
180 read(fd, databuf, curfile_total_size); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
181 close(fd); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
182 switch (curfile_structure) { |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
183 case 0x00: |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
184 /* transparent */ |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
185 rc = restore_transparent(databuf); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
186 break; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
187 case 0x01: |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
188 /* record-based */ |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
189 rc = restore_records(databuf); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
190 break; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
191 case 0x03: |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
192 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
|
193 rc = -1; |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
194 } |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
195 free(databuf); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
196 return(rc); |
fa7005185b84
fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
99
diff
changeset
|
197 } |