annotate simtool/saverestore.c @ 100:fa7005185b84

fc-simtool: restore-file command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Jan 2021 01:23:54 +0000
parents 2e35070d289f
children b563ff1c1a2a
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"
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 unsigned readlen;
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
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
94 static
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
95 restore_transparent(data)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
96 u_char *data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
97 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
98 unsigned off, cc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
99 int rc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
100
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
101 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
102 cc = curfile_total_size - off;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
103 if (cc > 255)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
104 cc = 255;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
105 rc = update_bin_op(off, data + off, cc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
106 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
107 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
108 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
109 return(0);
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
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
112 static
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
113 restore_records(data)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
114 u_char *data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
115 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
116 unsigned recno;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
117 u_char *dp;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
118 int rc;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
119
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
120 dp = data;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
121 for (recno = 1; recno <= curfile_record_count; recno++) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
122 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
123 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
124 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
125 dp += curfile_record_len;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
126 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
127 return(0);
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
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
130 cmd_restore_file(argc, argv)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
131 char **argv;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
132 {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
133 int file_id, rc, fd;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
134 struct stat st;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
135 u_char *databuf;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
136
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
137 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
138 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
139 file_id = strtoul(argv[1], 0, 16);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
140 else
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
141 file_id = find_symbolic_file_name(argv[1]);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
142 if (file_id < 0) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
143 fprintf(stderr,
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
144 "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
145 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
146 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
147 rc = select_op(file_id);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
148 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
149 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
150 rc = parse_ef_select_response();
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
151 if (rc < 0)
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
152 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
153 if (!curfile_total_size) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
154 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
155 return(0);
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 fd = open(argv[2], O_RDONLY);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
158 if (fd < 0) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
159 perror(argv[2]);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
160 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
161 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
162 fstat(fd, &st);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
163 if ((st.st_mode & S_IFMT) != S_IFREG) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
164 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
165 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
166 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
167 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
168 if (st.st_size != curfile_total_size) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
169 fprintf(stderr,
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
170 "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
171 argv[2], curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
172 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
173 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
174 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
175 databuf = malloc(curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
176 if (!databuf) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
177 perror("malloc for file data");
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
178 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
179 return(-1);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
180 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
181 read(fd, databuf, curfile_total_size);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
182 close(fd);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
183 switch (curfile_structure) {
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
184 case 0x00:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
185 /* transparent */
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
186 rc = restore_transparent(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
187 break;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
188 case 0x01:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
189 /* record-based */
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
190 rc = restore_records(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
191 break;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
192 case 0x03:
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
193 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
194 rc = -1;
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
195 }
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
196 free(databuf);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
197 return(rc);
fa7005185b84 fc-simtool: restore-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
198 }